msdfgen-ts 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Viktor Chlumský (original C++ msdfgen)
4
+ Copyright (c) 2026 Alexander Milevski (TypeScript port)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # msdfgen-ts
2
+
3
+ Crisp text at any size for WebGL and WebGPU. Load a TrueType font, get a multi-channel signed distance field (MSDF) atlas, all at runtime in the browser.
4
+
5
+ **[Live demos](https://w8r.github.io/msdfgen-ts/)** · minimal examples: [WebGPU](demo/hello-webgpu/main.ts), [WebGL2](demo/hello-webgl/main.ts)
6
+
7
+ - TypeScript port of [msdfgen](https://github.com/Chlumsky/msdfgen) that matches the C++ output to 1e-4 per texel
8
+ - Built-in TrueType parser with kerning
9
+ - 14 KB gzipped, no WASM, one tiny dependency ([potpack](https://github.com/mapbox/potpack))
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ npm install msdfgen-ts
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ import { Font, Atlas } from "msdfgen-ts";
21
+
22
+ const font = new Font(await (await fetch("Roboto.ttf")).arrayBuffer());
23
+ const atlas = new Atlas(font, { pixelsPerEm: 48, pxrange: 4 });
24
+
25
+ const { glyphs } = atlas.layout("Hello, world!");
26
+ ```
27
+
28
+ `atlas.texture` is an RGBA8 `Uint8Array` (`atlas.width × atlas.height`, row 0 at the top), ready for `writeTexture` / `texImage2D`. Each laid-out glyph gives you one quad, in em units (multiply by your font size in pixels):
29
+
30
+ ```ts
31
+ for (const { glyph: g, penX, penY } of glyphs) {
32
+ const quad = [penX + g.planeLeft, penY + g.planeBottom, penX + g.planeRight, penY + g.planeTop]; // y-up
33
+ const uv = [(g.x + 0.5) / atlas.width, (g.y + 0.5) / atlas.height,
34
+ (g.w - 1) / atlas.width, (g.h - 1) / atlas.height]; // x, y, w, h
35
+ }
36
+ ```
37
+
38
+ Draw it with the standard MSDF fragment shader, where `screenPxRange = atlas.pxrangeEm * fontSizePx`:
39
+
40
+ ```glsl
41
+ float median(float a, float b, float c) { return max(min(a, b), min(max(a, b), c)); }
42
+
43
+ void main() {
44
+ vec3 s = texture(atlas, uv).rgb;
45
+ float opacity = clamp(screenPxRange * (median(s.r, s.g, s.b) - 0.5) + 0.5, 0.0, 1.0);
46
+ color = vec4(textColor.rgb, textColor.a * opacity);
47
+ }
48
+ ```
49
+
50
+ Enable alpha blending (glyph quads overlap), use linear texture filtering, and measure `fontSizePx` in device pixels (CSS size × `devicePixelRatio`). The complete setup is in the minimal examples: [WebGPU](demo/hello-webgpu/main.ts) and [WebGL2](demo/hello-webgl/main.ts).
51
+
52
+ ### More
53
+
54
+ - **Multi-line text:** `atlas.layoutMultiline("line one\nline two")`.
55
+ - **Preload a character range:** `atlas.glyphs(codepoints)` generates any iterable of codepoints in one batch, e.g. all of Latin and Cyrillic up front.
56
+ - **Icon fonts:** `atlas.glyphsByIndex(ids)` addresses glyphs by index, for glyphs with no codepoint. `font.numGlyphs` gives the count.
57
+ - **Adding glyphs repacks the atlas.** Glyph objects are updated in place, so keep the objects you got and re-read `x`/`y` after adding more; don't copy them into variables.
58
+ - **Off the main thread:** `msdfgen-ts/worker` runs the same build in a Web Worker. See [`src/atlas-worker.ts`](src/atlas-worker.ts) for the message protocol.
59
+
60
+ ## Limitations
61
+
62
+ - TrueType (`glyf`) outlines only. No CFF/OTF, no variable fonts.
63
+ - No text shaping (ligatures, complex scripts). Kerning comes from the `kern` table only.
64
+ - Generation takes about 2–10 ms per glyph at 48 px/em, depending on complexity. Build atlases up front or in the worker, not per frame.
65
+
66
+ ## Demos
67
+
68
+ ```sh
69
+ npm install
70
+ npm run dev:hello-webgpu # also: dev:hello-webgl, dev:webgpu-zoom, dev:webgl-zoom, dev:lucide, dev:bench
71
+ ```
72
+
73
+ ## Development
74
+
75
+ ```sh
76
+ npm run setup-reference # build the C++ msdfgen reference binary (needs cmake, freetype, libpng)
77
+ npm run gate:all # full test suite, including the 1158-case golden comparison against msdfgen
78
+ npm run build # dist/: ES module, IIFE, worker, types
79
+ ```
80
+
81
+ How the algorithm works, with diagrams: [docs/algorithm.md](docs/algorithm.md). Project conventions: [CLAUDE.md](CLAUDE.md).
82
+
83
+ ## License
84
+
85
+ MIT. Includes a port of [msdfgen](https://github.com/Chlumsky/msdfgen) © Viktor Chlumský, also MIT.