diff --git a/.github/workflows/node.yml b/.github/workflows/node.yml new file mode 100644 index 0000000..5baeff5 --- /dev/null +++ b/.github/workflows/node.yml @@ -0,0 +1,22 @@ +name: Node +on: [push, pull_request] +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Install dependencies + run: npm ci + + - name: Install CJK font + run: wget https://github.com/googlefonts/noto-cjk/raw/main/Sans/OTF/SimplifiedChinese/NotoSansCJKsc-Regular.otf -O /usr/share/fonts/NotoSansCJKsc-Regular.otf && fc-cache -fv + + - name: Run tests + run: npm test diff --git a/.gitignore b/.gitignore index 239ecff..3c3629e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ node_modules -yarn.lock diff --git a/LICENSE.txt b/LICENSE.txt index 94c832c..f061cfd 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,7 +1,9 @@ -Copyright © 2016-2021 Mapbox, Inc. -This code available under the terms of the BSD 2-Clause license. +BSD-2-Clause +Copyright (c) 2016-2024 Mapbox, Inc. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md index abbbac3..c7843ce 100644 --- a/README.md +++ b/README.md @@ -1,41 +1,63 @@ -# TinySDF +# TinySDF [![Volodymyr Agafonkin's projects](https://img.shields.io/badge/simply-awesome-brightgreen.svg)](https://github.com/mourner/projects) [![Node](https://github.com/mapbox/tiny-sdf/actions/workflows/node.yml/badge.svg)](https://github.com/mapbox/tiny-sdf/actions/workflows/node.yml) TinySDF is a tiny and fast JavaScript library for generating SDF (signed distance field) from system fonts on the browser using Canvas 2D and [Felzenszwalb/Huttenlocher distance transform](https://cs.brown.edu/~pff/papers/dt-final.pdf). This is very useful for [rendering text with WebGL](https://www.mapbox.com/blog/text-signed-distance-fields/). -This implementation is based directly on the algorithm published in the Felzenszwalb/Huttenlocher paper, and is not a port of the existing C++ implementation provided by the paper's authors. - -Demo: http://mapbox.github.io/tiny-sdf/ +## [Demo](http://mapbox.github.io/tiny-sdf) ## Usage -Create a TinySDF for drawing SDFs based on font parameters: + +Create a TinySDF for drawing glyph SDFs based on font parameters: + +```js +const tinySdf = new TinySDF({ + fontSize: 24, // Font size in pixels + fontFamily: 'sans-serif', // CSS font-family + fontWeight: 'normal', // CSS font-weight + fontStyle: 'normal', // CSS font-style + buffer: 3, // Whitespace buffer around a glyph in pixels + radius: 8, // How many pixels around the glyph shape to use for encoding distance + cutoff: 0.25 // How much of the radius (relative) is used for the inside part of the glyph +}); + +const glyph = tinySdf.draw('泽'); // draw a single character +``` + +Returns an object with the following properties: + +- `data` is a `Uint8ClampedArray` array of alpha values (0–255) for a `width` x `height` grid. +- `width`: Width of the returned bitmap. +- `height`: Height of the returned bitmap. +- `glyphTop`: Maximum ascent of the glyph from alphabetic baseline. +- `glyphLeft`: Currently hardwired to 0 (actual glyph differences are encoded in the rasterization). +- `glyphWidth`: Width of the rasterized portion of the glyph. +- `glyphHeight` Height of the rasterized portion of the glyph. +- `glyphAdvance`: Layout advance. + +TinySDF is provided as a ES module, so it's only supported on modern browsers, excluding IE. + +```html + +``` + +In Node, you can't use `require` — only `import` in ESM-capable versions (v12.15+): ```js -var fontsize = 24; // Font size in pixels -var buffer = 3; // Whitespace buffer around a glyph in pixels -var radius = 8; // How many pixels around the glyph shape to use for encoding distance -var cutoff = 0.25 // How much of the radius (relative) is used for the inside part the glyph - -var fontFamily = 'sans-serif'; // css font-family -var fontWeight = 'normal'; // css font-weight -var tinySDFGenerator = new TinySDF(fontsize, buffer, radius, cutoff, fontFamily, fontWeight); - -var oneSDF = tinySDFGenerator.draw('泽'); -// returns a Uint8ClampedArray array of alpha values (0–255) for a size x size square grid - -// To generate glyphs with variable advances (e.g. non-ideographic glyphs), -// use `drawWithMetrics` -var sdfWithMetrics = tinySDFGenerator.drawWithMetrics('A'); -// sdfWithMetrics.data is the same as in `draw`, except the size may be clipped to fit the glyph -// sdfWithMetrics.metrics contains: -// top: Top alignment: glyph ascent - 'top' = baseline -// left: Currently hardwired to 0 -// width: Width of rasterized portion of glyph -// height -// advance: Layout advance -// sdfWidth: Width of the returned bitmap, usually but not always width + 2 * buffer -// sdfHeight -// fontAscent: Maximum ascent of font from baseline +import TinySDF from '@mapbox/tiny-sdf'; +``` + +## Development + +```bash +npm test # run tests +npm start # start server for the demo page ``` + +## License + +This implementation is licensed under the [BSD 2-Clause license](https://opensource.org/licenses/BSD-2-Clause). It's based directly on the algorithm published in the Felzenszwalb/Huttenlocher paper, and is not a port of the existing C++ implementation provided by the paper's authors. diff --git a/eslint.config.js b/eslint.config.js new file mode 100644 index 0000000..730d5e4 --- /dev/null +++ b/eslint.config.js @@ -0,0 +1,10 @@ +import config from 'eslint-config-mourner'; +import html from 'eslint-plugin-html'; + +export default [ + ...config, + { + files: ['index.js', 'index.html', 'test/*.js'], + plugins: {html} + } +]; diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..679ee9f --- /dev/null +++ b/index.d.ts @@ -0,0 +1,24 @@ +export declare type TinySDFOptions = { + fontSize?: number; + buffer?: number; + radius?: number; + cutoff?: number; + fontFamily?: string; + fontWeight?: string; + fontStyle?: string; + lang?: string; +}; + +export default class TinySDF { + constructor(options: TinySDFOptions); + draw(char: string): { + data: Uint8ClampedArray; + width: number; + height: number; + glyphWidth: number; + glyphHeight: number; + glyphTop: number; + glyphLeft: number; + glyphAdvance: number; + }; +} diff --git a/index.html b/index.html index aab394e..94674aa 100644 --- a/index.html +++ b/index.html @@ -1,7 +1,7 @@ -SDF test +TinySDF demo