msdfgen-ts 0.1.0 → 0.3.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/README.md +42 -6
- package/dist/atlas-worker.js +2727 -1307
- package/dist/msdfgen-ts.iife.js +3 -2
- package/dist/msdfgen-ts.js +3153 -1378
- package/dist/types/atlas-gen.d.ts +58 -28
- package/dist/types/font/font.d.ts +38 -3
- package/dist/types/font/glyph-run.d.ts +50 -0
- package/dist/types/font/script.d.ts +47 -0
- package/dist/types/font/shaper.d.ts +41 -0
- package/dist/types/font/tables/gdef.d.ts +25 -0
- package/dist/types/font/tables/glyf.d.ts +3 -1
- package/dist/types/font/tables/gpos.d.ts +85 -0
- package/dist/types/font/tables/kern.d.ts +31 -14
- package/dist/types/font/tables/otl.d.ts +27 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/msdf/shape-msdf.d.ts +51 -0
- package/dist/types/shape/normalize.d.ts +12 -1
- package/dist/types/shape/union.d.ts +58 -0
- package/dist/types/svg/svg.d.ts +78 -0
- package/package.json +40 -18
package/README.md
CHANGED
|
@@ -5,8 +5,8 @@ Crisp text at any size for WebGL and WebGPU. Load a TrueType font, get a multi-c
|
|
|
5
5
|
**[Live demos](https://w8r.github.io/msdfgen-ts/)** · minimal examples: [WebGPU](demo/hello-webgpu/main.ts), [WebGL2](demo/hello-webgl/main.ts)
|
|
6
6
|
|
|
7
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
|
-
-
|
|
8
|
+
- Built-in TrueType parser, with kerning that matches Chrome's (GPOS and `kern`)
|
|
9
|
+
- 30 KB gzipped, no WASM, one tiny dependency ([potpack](https://github.com/mapbox/potpack))
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
@@ -30,8 +30,7 @@ const { glyphs } = atlas.layout("Hello, world!");
|
|
|
30
30
|
```ts
|
|
31
31
|
for (const { glyph: g, penX, penY } of glyphs) {
|
|
32
32
|
const quad = [penX + g.planeLeft, penY + g.planeBottom, penX + g.planeRight, penY + g.planeTop]; // y-up
|
|
33
|
-
const uv = [
|
|
34
|
-
(g.w - 1) / atlas.width, (g.h - 1) / atlas.height]; // x, y, w, h
|
|
33
|
+
const uv = [g.x / atlas.width, g.y / atlas.height, g.w / atlas.width, g.h / atlas.height]; // x, y, w, h
|
|
35
34
|
}
|
|
36
35
|
```
|
|
37
36
|
|
|
@@ -55,21 +54,58 @@ Enable alpha blending (glyph quads overlap), use linear texture filtering, and m
|
|
|
55
54
|
- **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
55
|
- **Icon fonts:** `atlas.glyphsByIndex(ids)` addresses glyphs by index, for glyphs with no codepoint. `font.numGlyphs` gives the count.
|
|
57
56
|
- **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.
|
|
57
|
+
- **Deep zoom:** `new Atlas(font, { float: true })` also fills `atlas.floatTexture` with the same cells as raw float RGB, unclamped, for zoom levels that outrun 8 bits.
|
|
58
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
59
|
|
|
60
|
+
### SVG
|
|
61
|
+
|
|
62
|
+
The same atlas takes SVG icons. Parse each SVG into a shape, then add the shapes under keys of your choice:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { Atlas, parseSvg } from "msdfgen-ts";
|
|
66
|
+
|
|
67
|
+
const { shape, width } = parseSvg(svgText);
|
|
68
|
+
const atlas = new Atlas(null, { pixelsPerEm: 64, pxrange: 8 }); // null: no font
|
|
69
|
+
const [heart] = atlas.shapes([["heart", shape]]);
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
- **Units:** 1 shape unit is the viewBox height, y-up, origin at the viewBox's bottom-left. `pixelsPerEm` is texels per icon height, and `atlas.pxrangeEm × iconHeightPx` is the shader's `screenPxRange`.
|
|
73
|
+
- **Placement:** the cell's `planeLeft`/`planeBottom`/`planeRight`/`planeTop` are relative to the viewBox, and `width` is the viewBox width (height is 1), so an icon lands where its viewBox puts it.
|
|
74
|
+
- **Fonts and icons together:** pass a font instead of `null` and call both `glyphs()` and `shapes()`; they share one texture.
|
|
75
|
+
- **Without an atlas:** `generateShapeMSDF(shape, pixelsPerUnit, pxrange)` returns one float bitmap and its plane bounds.
|
|
76
|
+
- **Colour:** `parseSvgLayers(svgText)` returns `{ layers, width }`, one shape per filled path with its colour or gradient, back to front. Add every layer with `shapes()` (keys like `` `${name}:${i}` ``) and draw them in order. [demo/svg/renderer.ts](demo/svg/renderer.ts) is a small WebGL2 renderer for it.
|
|
77
|
+
- **Bundle size:** only what you import ships. A font-only app doesn't carry the SVG parser, and an SVG-only app doesn't carry the TrueType parser.
|
|
78
|
+
|
|
79
|
+
The complete setup, colour layers included, is in [demo/hello-svg/main.ts](demo/hello-svg/main.ts).
|
|
80
|
+
|
|
60
81
|
## Limitations
|
|
61
82
|
|
|
62
83
|
- TrueType (`glyf`) outlines only. No CFF/OTF, no variable fonts.
|
|
63
|
-
-
|
|
84
|
+
- SVG: fills only (paths and basic shapes, transforms, `.class` CSS, solid / linear / radial gradient fills, clip paths, group opacity, percentage lengths, clipped to the viewBox). No strokes, masks, filters, blend modes, `<use>` (outside clip paths) or `<text>`.
|
|
85
|
+
- Text layout is kerning only: no ligatures or other glyph substitution (GSUB), no complex scripts, no mark positioning. Kerning (GPOS or the `kern` table, per script run) matches Chrome's.
|
|
64
86
|
- 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
87
|
|
|
66
88
|
## Demos
|
|
67
89
|
|
|
68
90
|
```sh
|
|
69
91
|
npm install
|
|
70
|
-
npm run dev:hello-webgpu # also: dev:hello-webgl, dev:webgpu-zoom, dev:webgl-zoom, dev:lucide, dev:bench
|
|
92
|
+
npm run dev:hello-webgpu # also: dev:hello-webgl, dev:hello-svg, dev:webgpu-zoom, dev:webgl-zoom, dev:lucide, dev:svg, dev:svg-bounce, dev:bench
|
|
71
93
|
```
|
|
72
94
|
|
|
95
|
+
## Framework examples
|
|
96
|
+
|
|
97
|
+
The same atlas feeds three JS stacks. Each example is a self-contained module under `demo/`, meant to be copied into an app.
|
|
98
|
+
|
|
99
|
+
| Stack | Demo | Source | Dev command |
|
|
100
|
+
| ----------------- | --------------------------------------------------------- | ------------------------------------------- | ------------------- |
|
|
101
|
+
| three.js | [live](https://w8r.github.io/msdfgen-ts/three/index.html) | [`demo/three/`](demo/three/MSDFText.ts) | `npm run dev:three` |
|
|
102
|
+
| react-three-fiber | [live](https://w8r.github.io/msdfgen-ts/r3f/index.html) | [`demo/r3f/`](demo/r3f/MSDFText.tsx) | `npm run dev:r3f` |
|
|
103
|
+
| PixiJS v8 | [live](https://w8r.github.io/msdfgen-ts/pixi/index.html) | [`demo/pixi/`](demo/pixi/msdfBitmapFont.ts) | `npm run dev:pixi` |
|
|
104
|
+
|
|
105
|
+
- **three.js**: `MSDFText` is a `THREE.Mesh`. One atlas and one shader per font, shared by every text mesh. Glyphs can be added at runtime.
|
|
106
|
+
- **react-three-fiber**: a thin `<MSDFText>` over the three.js mesh, plus a Suspense-friendly `useMSDFFont(url)`.
|
|
107
|
+
- **PixiJS v8**: builds a Pixi `BitmapFont` from the atlas and uses Pixi's own MSDF path, so `BitmapText` needs no custom shader.
|
|
108
|
+
|
|
73
109
|
## Development
|
|
74
110
|
|
|
75
111
|
```sh
|