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 +22 -0
- package/README.md +85 -0
- package/dist/atlas-worker.js +2142 -0
- package/dist/msdfgen-ts.iife.js +2 -0
- package/dist/msdfgen-ts.js +2114 -0
- package/dist/types/atlas-gen.d.ts +165 -0
- package/dist/types/atlas-worker.d.ts +51 -0
- package/dist/types/font/font.d.ts +105 -0
- package/dist/types/font/reader.d.ts +55 -0
- package/dist/types/font/sfnt.d.ts +20 -0
- package/dist/types/font/tables/cmap.d.ts +21 -0
- package/dist/types/font/tables/glyf.d.ts +70 -0
- package/dist/types/font/tables/head.d.ts +30 -0
- package/dist/types/font/tables/hhea.d.ts +30 -0
- package/dist/types/font/tables/hmtx.d.ts +18 -0
- package/dist/types/font/tables/kern.d.ts +17 -0
- package/dist/types/font/tables/loca.d.ts +19 -0
- package/dist/types/font/tables/maxp.d.ts +10 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/math/cubic.d.ts +12 -0
- package/dist/types/math/scalar.d.ts +101 -0
- package/dist/types/msdf/edge-coloring.d.ts +36 -0
- package/dist/types/msdf/error-correction.d.ts +61 -0
- package/dist/types/msdf/generate.d.ts +42 -0
- package/dist/types/msdf/sdf.d.ts +53 -0
- package/dist/types/shape/contour.d.ts +7 -0
- package/dist/types/shape/normalize.d.ts +38 -0
- package/dist/types/shape/scanline.d.ts +75 -0
- package/dist/types/shape/segments.d.ts +148 -0
- package/dist/types/shape/shape.d.ts +12 -0
- package/package.json +100 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MSDF glyph atlas — single source of truth for generation AND consumption.
|
|
3
|
+
*
|
|
4
|
+
* Every glyph is generated at the SAME uniform em-to-texel scale
|
|
5
|
+
* (`pixelsPerEm`), but each glyph's cell is CROPPED to its own tight
|
|
6
|
+
* bounding box (plus `pxrange` texels of SDF safe-region padding on each
|
|
7
|
+
* side). This gives:
|
|
8
|
+
* - No wasted atlas texels — a `.` doesn't get the same S×S slot as `M`.
|
|
9
|
+
* - A uniform `pxrangeEm` across all glyphs — the shader keeps a single
|
|
10
|
+
* `screenPxRange = atlas.pxrangeEm × pixelPerEm` uniform, no per-instance
|
|
11
|
+
* range varying and no per-glyph reasoning about crispness.
|
|
12
|
+
*
|
|
13
|
+
* The MSDF pipeline (normalize → colour → generate → sign-correct →
|
|
14
|
+
* error-correct → byte-quantise → y-flip) is byte-for-byte identical to
|
|
15
|
+
* the C++ msdfgen reference — see test/msdf/msdf.test.ts for the direct
|
|
16
|
+
* pipeline golden. The atlas layout on top (crop + shelf-pack) is our own.
|
|
17
|
+
*
|
|
18
|
+
* msdfgen is MIT © Viktor Chlumský; this file is original.
|
|
19
|
+
*/
|
|
20
|
+
import { type Font } from "./font/font.js";
|
|
21
|
+
/** Converts a msdfgen float channel value to a byte, matching C++
|
|
22
|
+
* `pixelFloatToByte` exactly. */
|
|
23
|
+
export declare function pixelFloatToByte(x: number): number;
|
|
24
|
+
/** Options for constructing an {@link Atlas}. */
|
|
25
|
+
export interface AtlasOptions {
|
|
26
|
+
/** Uniform texel-per-em generation scale. Default 40. */
|
|
27
|
+
pixelsPerEm?: number;
|
|
28
|
+
/** MSDF distance range in texels (msdfgen `-pxrange`). Default 4. */
|
|
29
|
+
pxrange?: number;
|
|
30
|
+
}
|
|
31
|
+
/** Cached layout + metrics for one atlas-resident glyph. */
|
|
32
|
+
export interface AtlasGlyph {
|
|
33
|
+
/** Atlas texel rect (x, y, w, h). w=h=0 for glyphs with no outline. */
|
|
34
|
+
x: number;
|
|
35
|
+
y: number;
|
|
36
|
+
w: number;
|
|
37
|
+
h: number;
|
|
38
|
+
/** Advance width, in em units. */
|
|
39
|
+
advance: number;
|
|
40
|
+
/** Cell rect in em units, relative to the pen origin (y-up, baseline y=0). */
|
|
41
|
+
planeLeft: number;
|
|
42
|
+
planeBottom: number;
|
|
43
|
+
planeRight: number;
|
|
44
|
+
planeTop: number;
|
|
45
|
+
}
|
|
46
|
+
/** One glyph positioned in a text run. */
|
|
47
|
+
export interface LaidOutGlyph {
|
|
48
|
+
glyph: AtlasGlyph;
|
|
49
|
+
/** Pen position (em, world-space X, relative to the glyph's own line). */
|
|
50
|
+
penX: number;
|
|
51
|
+
/** Baseline Y (em, world-space, y-up) — 0 for {@link Atlas.layout}'s
|
|
52
|
+
* single line; successive lines from {@link Atlas.layoutMultiline} get
|
|
53
|
+
* more negative, y-up matching the rest of this codebase's convention. */
|
|
54
|
+
penY: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Runtime MSDF glyph atlas backed by a single `Font`.
|
|
58
|
+
*
|
|
59
|
+
* Prefer {@link Atlas.glyphs} (batched — one potpack for the whole set) over
|
|
60
|
+
* calling {@link Atlas.glyph} in a loop (one potpack per insert).
|
|
61
|
+
* {@link Atlas.layout} already uses the batched path internally.
|
|
62
|
+
*
|
|
63
|
+
* `AtlasGlyph` objects are mutated in place across repacks, so keeping
|
|
64
|
+
* a reference and re-reading `x`/`y` is safe; caching them in locals
|
|
65
|
+
* before a subsequent insert is not.
|
|
66
|
+
*/
|
|
67
|
+
export declare class Atlas {
|
|
68
|
+
private readonly _font;
|
|
69
|
+
private readonly _pxPerEm;
|
|
70
|
+
private readonly _pxrange;
|
|
71
|
+
private _width;
|
|
72
|
+
private _height;
|
|
73
|
+
private _pixels;
|
|
74
|
+
/** Cached glyph + retained float MSDF (for repacking on new inserts).
|
|
75
|
+
* Keyed by Unicode codepoint — see {@link Atlas.glyphs}. */
|
|
76
|
+
private readonly _cache;
|
|
77
|
+
/** Same shape as {@link Atlas._cache}, keyed by raw glyph index instead —
|
|
78
|
+
* see {@link Atlas.glyphsByIndex}. Separate map: a codepoint and a glyph
|
|
79
|
+
* index are different numbers in the same integer space. */
|
|
80
|
+
private readonly _cacheByGid;
|
|
81
|
+
/** SDF distance range in em — UNIFORM across all glyphs (that's the point). */
|
|
82
|
+
readonly pxrangeEm: number;
|
|
83
|
+
constructor(font: Font, opts?: AtlasOptions);
|
|
84
|
+
/** Packed atlas texture width in texels (chosen by potpack). */
|
|
85
|
+
get width(): number;
|
|
86
|
+
/** Packed atlas texture height in texels (chosen by potpack). */
|
|
87
|
+
get height(): number;
|
|
88
|
+
/** Packed atlas texture, flat RGBA8 bytes, row-major, y-down. */
|
|
89
|
+
get texture(): Uint8Array;
|
|
90
|
+
/**
|
|
91
|
+
* Batched generate + pack. Generates MSDFs for any codepoints not
|
|
92
|
+
* already cached, then runs potpack **once** over the whole cache.
|
|
93
|
+
* Returns the `AtlasGlyph` for each requested codepoint, in order.
|
|
94
|
+
*/
|
|
95
|
+
glyphs(codepoints: Iterable<number>): AtlasGlyph[];
|
|
96
|
+
/**
|
|
97
|
+
* Single-glyph convenience: generates + packs if missing. Repacks the
|
|
98
|
+
* whole atlas on every new insert — for more than one glyph, use
|
|
99
|
+
* {@link Atlas.glyphs} instead.
|
|
100
|
+
*/
|
|
101
|
+
glyph(codepoint: number): AtlasGlyph;
|
|
102
|
+
/**
|
|
103
|
+
* Batched generate + pack, addressed by raw glyph index instead of
|
|
104
|
+
* Unicode codepoint — the {@link Atlas.glyphs} twin for glyphs with no
|
|
105
|
+
* reachable cmap entry. Icon fonts (e.g. Lucide) commonly have far more
|
|
106
|
+
* glyphs than mapped codepoints; this is how the rest of the set gets
|
|
107
|
+
* generated. Shares one packed atlas texture with the codepoint-addressed
|
|
108
|
+
* cache — {@link Atlas.texture} always reflects everything requested
|
|
109
|
+
* through either method. Cached separately from {@link Atlas.glyphs} (a
|
|
110
|
+
* codepoint and a glyph index are different numbers in the same integer
|
|
111
|
+
* space — sharing one cache would collide them).
|
|
112
|
+
*/
|
|
113
|
+
glyphsByIndex(glyphIds: Iterable<number>): AtlasGlyph[];
|
|
114
|
+
/** Single-glyph convenience for {@link Atlas.glyphsByIndex} — see its
|
|
115
|
+
* doc. For more than one glyph, prefer {@link Atlas.glyphsByIndex}
|
|
116
|
+
* (one potpack for the whole batch instead of one per insert). */
|
|
117
|
+
glyphByIndex(glyphId: number): AtlasGlyph;
|
|
118
|
+
/**
|
|
119
|
+
* Generates the MSDF for `glyphId`. Does not cache and does not pack —
|
|
120
|
+
* callers own the cache key (a codepoint for {@link Atlas.glyphs}, the
|
|
121
|
+
* glyph ID itself for {@link Atlas.glyphsByIndex}) and the repack.
|
|
122
|
+
*/
|
|
123
|
+
private _generateForGlyphId;
|
|
124
|
+
/**
|
|
125
|
+
* Lays out `text` left-to-right on a single line, applying kerning.
|
|
126
|
+
* Generates any missing glyphs and packs the atlas **once** for the
|
|
127
|
+
* whole run. Baseline is y=0 (`penY` on every result is 0); each `penX`
|
|
128
|
+
* is the pen position in em. `text` must not contain `\n` — use
|
|
129
|
+
* {@link Atlas.layoutMultiline} for multi-line input.
|
|
130
|
+
*/
|
|
131
|
+
layout(text: string): {
|
|
132
|
+
glyphs: LaidOutGlyph[];
|
|
133
|
+
widthEm: number;
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* Lays out `text` as one or more `\n`-separated lines, each run through
|
|
137
|
+
* {@link Atlas.layout} independently (so kerning never crosses a line
|
|
138
|
+
* break) and stacked with `penY` decreasing by one line height per line
|
|
139
|
+
* (y-up, matching this codebase's convention — line 0's baseline stays
|
|
140
|
+
* at y=0). Line height is the font's own metrics:
|
|
141
|
+
* `(ascender − descender + lineGap) / unitsPerEm`, matching typical
|
|
142
|
+
* single-spaced typesetting — not a msdfgen concept, original to this
|
|
143
|
+
* file (msdfgen has no text layout at all).
|
|
144
|
+
*
|
|
145
|
+
* `widthEm` is the widest line's; `heightEm` spans from line 0's top to
|
|
146
|
+
* the last line's bottom, both approximate (ascender/descender-based,
|
|
147
|
+
* not per-glyph ink bounds) — fine for camera framing, not for tight
|
|
148
|
+
* cropping.
|
|
149
|
+
*/
|
|
150
|
+
layoutMultiline(text: string): {
|
|
151
|
+
glyphs: LaidOutGlyph[];
|
|
152
|
+
widthEm: number;
|
|
153
|
+
heightEm: number;
|
|
154
|
+
};
|
|
155
|
+
/** Repacks every cached glyph (both the codepoint- and glyph-index-keyed
|
|
156
|
+
* caches — one shared texture) with potpack, reallocates the texture,
|
|
157
|
+
* and re-blits each MSDF at its assigned rect. Called by `glyphs()`/
|
|
158
|
+
* `glyphsByIndex()` after any batch of new inserts — once per batch. */
|
|
159
|
+
private _repack;
|
|
160
|
+
/**
|
|
161
|
+
* Quantise `msdf` (float, y-up, row 0 = bottom, w×h×3) into `_pixels` at
|
|
162
|
+
* atlas rect (`ax`, `ay`, `w`, `h`) — the single y-flip point.
|
|
163
|
+
*/
|
|
164
|
+
private _blit;
|
|
165
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type AtlasGlyph } from "./atlas-gen.js";
|
|
2
|
+
/**
|
|
3
|
+
* Requests one atlas build.
|
|
4
|
+
*
|
|
5
|
+
* `fontKey` is a caller-chosen cache key (a font URL is a natural choice) —
|
|
6
|
+
* the worker parses+caches one `Font` per key across every request it sees.
|
|
7
|
+
* `font` is required on the FIRST request for a given `fontKey`; omit it on
|
|
8
|
+
* later requests reusing that key to skip re-sending + re-parsing the font.
|
|
9
|
+
* `font` is transferred, not copied — do not reuse that `ArrayBuffer` after
|
|
10
|
+
* posting (it will be detached).
|
|
11
|
+
*/
|
|
12
|
+
export interface BuildRequest {
|
|
13
|
+
type: "build";
|
|
14
|
+
id: number;
|
|
15
|
+
fontKey: string;
|
|
16
|
+
font?: ArrayBuffer;
|
|
17
|
+
pixelsPerEm: number;
|
|
18
|
+
pxrange: number;
|
|
19
|
+
/** `\n`-separated lines are laid out via `Atlas.layoutMultiline` — see
|
|
20
|
+
* its doc for line-height handling. */
|
|
21
|
+
text: string;
|
|
22
|
+
}
|
|
23
|
+
/** One glyph in a built response — {@link AtlasGlyph} plus its laid-out pen
|
|
24
|
+
* position, flattened into one plain object for structured clone. */
|
|
25
|
+
export interface BuiltGlyph extends AtlasGlyph {
|
|
26
|
+
penX: number;
|
|
27
|
+
penY: number;
|
|
28
|
+
}
|
|
29
|
+
/** Successful build result. `texture` is transferred, not copied. */
|
|
30
|
+
export interface BuiltResponse {
|
|
31
|
+
type: "built";
|
|
32
|
+
id: number;
|
|
33
|
+
pixelsPerEm: number;
|
|
34
|
+
pxrange: number;
|
|
35
|
+
pxrangeEm: number;
|
|
36
|
+
width: number;
|
|
37
|
+
height: number;
|
|
38
|
+
texture: ArrayBuffer;
|
|
39
|
+
glyphs: BuiltGlyph[];
|
|
40
|
+
widthEm: number;
|
|
41
|
+
heightEm: number;
|
|
42
|
+
/** Wall-clock time spent generating + packing this tier's atlas (ms). */
|
|
43
|
+
genMs: number;
|
|
44
|
+
}
|
|
45
|
+
/** Build failure — `message` is `String(error)`, not the original object
|
|
46
|
+
* (errors don't structured-clone reliably across the worker boundary). */
|
|
47
|
+
export interface ErrorResponse {
|
|
48
|
+
type: "error";
|
|
49
|
+
id: number;
|
|
50
|
+
message: string;
|
|
51
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { type Shape } from "../shape/shape.js";
|
|
2
|
+
/** Font-level metrics exposed from `head` and `hhea`. */
|
|
3
|
+
export interface FontMetrics {
|
|
4
|
+
/** Design units per em-square. */
|
|
5
|
+
unitsPerEm: number;
|
|
6
|
+
/** Typographic ascender in font units. */
|
|
7
|
+
ascender: number;
|
|
8
|
+
/** Typographic descender in font units (typically negative). */
|
|
9
|
+
descender: number;
|
|
10
|
+
/** Recommended additional line spacing in font units. */
|
|
11
|
+
lineGap: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Public Font class — wraps a parsed TrueType font buffer.
|
|
15
|
+
*
|
|
16
|
+
* Raw font units are returned throughout M1.
|
|
17
|
+
* Em-normalisation (÷ unitsPerEm) is applied at a single named boundary in M3
|
|
18
|
+
* to match the `FONT_SCALING_EM_NORMALIZED` convention used in the goldens.
|
|
19
|
+
*/
|
|
20
|
+
export declare class Font {
|
|
21
|
+
/** @type {ArrayBuffer} */
|
|
22
|
+
private _buffer;
|
|
23
|
+
/** @type {TableMap} */
|
|
24
|
+
private _tables;
|
|
25
|
+
/** @type {HeadTable} */
|
|
26
|
+
private _head;
|
|
27
|
+
/** @type {HheaTable} */
|
|
28
|
+
private _hhea;
|
|
29
|
+
/** @type {HMetric[]} */
|
|
30
|
+
private _hmtx;
|
|
31
|
+
/** @type {number[]} */
|
|
32
|
+
private _loca;
|
|
33
|
+
/** @type {CmapLookup} */
|
|
34
|
+
private _cmap;
|
|
35
|
+
/** @type {KernMap} */
|
|
36
|
+
private _kern;
|
|
37
|
+
/** Font-level metrics (unitsPerEm, ascender, descender, lineGap). */
|
|
38
|
+
readonly metrics: FontMetrics;
|
|
39
|
+
/**
|
|
40
|
+
* Parses a TrueType font from an ArrayBuffer.
|
|
41
|
+
*
|
|
42
|
+
* Supports single TTF files and TTC collections (first font only).
|
|
43
|
+
* Variable fonts (gvar/fvar) are accepted; variation deltas are ignored —
|
|
44
|
+
* the default-instance glyf outlines are used as-is.
|
|
45
|
+
*
|
|
46
|
+
* @param buffer Raw font file bytes.
|
|
47
|
+
*/
|
|
48
|
+
constructor(buffer: ArrayBuffer);
|
|
49
|
+
/**
|
|
50
|
+
* Maps a Unicode codepoint to its glyph ID.
|
|
51
|
+
* Returns 0 (.notdef) if the codepoint is not in the font.
|
|
52
|
+
*
|
|
53
|
+
* @param codepoint Unicode scalar value.
|
|
54
|
+
*/
|
|
55
|
+
glyphId(codepoint: number): number;
|
|
56
|
+
/**
|
|
57
|
+
* Total glyph count (`maxp.numGlyphs`), including `.notdef` at index 0.
|
|
58
|
+
* Valid glyph IDs for {@link Font.shape}/{@link Font.advance} are
|
|
59
|
+
* `0..numGlyphs-1`. Icon fonts (e.g. Lucide) often have far more glyphs
|
|
60
|
+
* than reachable Unicode codepoints — this is how a consumer enumerates
|
|
61
|
+
* "every glyph" rather than "every mapped character"; derived from
|
|
62
|
+
* `loca` (always `numGlyphs + 1` entries), no extra stored state.
|
|
63
|
+
*/
|
|
64
|
+
get numGlyphs(): number;
|
|
65
|
+
/**
|
|
66
|
+
* Advance width of a glyph in font units.
|
|
67
|
+
*
|
|
68
|
+
* @param glyphId Glyph index.
|
|
69
|
+
*/
|
|
70
|
+
advance(glyphId: number): number;
|
|
71
|
+
/**
|
|
72
|
+
* Left side bearing of a glyph in font units.
|
|
73
|
+
*
|
|
74
|
+
* @param glyphId Glyph index.
|
|
75
|
+
*/
|
|
76
|
+
lsb(glyphId: number): number;
|
|
77
|
+
/**
|
|
78
|
+
* Kerning adjustment between two glyphs in font units.
|
|
79
|
+
* Returns 0 if the font has no kern table or the pair is not listed.
|
|
80
|
+
*
|
|
81
|
+
* @param leftGlyphId Left glyph index.
|
|
82
|
+
* @param rightGlyphId Right glyph index.
|
|
83
|
+
*/
|
|
84
|
+
kerning(leftGlyphId: number, rightGlyphId: number): number;
|
|
85
|
+
/**
|
|
86
|
+
* Parses and returns the outline of a glyph as a Shape (raw font units, y-up).
|
|
87
|
+
*
|
|
88
|
+
* Composite glyphs are recursively resolved. The returned Shape has all
|
|
89
|
+
* contours in absolute coordinates of the root glyph's coordinate space.
|
|
90
|
+
*
|
|
91
|
+
* Returns an empty Shape for glyphs with no outline (space, .notdef with no
|
|
92
|
+
* outline, etc.).
|
|
93
|
+
*
|
|
94
|
+
* @param glyphId Glyph index.
|
|
95
|
+
* @returns Shape with contours in font units, inverseYAxis=false.
|
|
96
|
+
*/
|
|
97
|
+
shape(glyphId: number): Shape;
|
|
98
|
+
/**
|
|
99
|
+
* Parses a glyph to its raw (pre-midpoint) outline, used as the composite
|
|
100
|
+
* recursion primitive so implied midpoints are expanded only on the final
|
|
101
|
+
* merged outline (matching FreeType).
|
|
102
|
+
* @internal
|
|
103
|
+
*/
|
|
104
|
+
private _glyphRaw;
|
|
105
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BinaryReader — a stateful big-endian DataView cursor.
|
|
3
|
+
*
|
|
4
|
+
* All multi-byte reads use big-endian byte order (OpenType/TrueType spec).
|
|
5
|
+
* Scalar fields are plain numbers; there are no allocations per read.
|
|
6
|
+
*/
|
|
7
|
+
export declare class BinaryReader {
|
|
8
|
+
/** @type {DataView} */
|
|
9
|
+
private _view;
|
|
10
|
+
/** Current read position in bytes. */
|
|
11
|
+
private _pos;
|
|
12
|
+
/**
|
|
13
|
+
* @param buffer Source buffer (the full font file).
|
|
14
|
+
* @param byteOffset Starting position (default 0).
|
|
15
|
+
*/
|
|
16
|
+
constructor(buffer: ArrayBuffer, byteOffset?: number);
|
|
17
|
+
/** Current byte position. */
|
|
18
|
+
get pos(): number;
|
|
19
|
+
/** Move to an absolute byte position. */
|
|
20
|
+
seek(n: number): void;
|
|
21
|
+
/** Advance position by `n` bytes without reading. */
|
|
22
|
+
skip(n: number): void;
|
|
23
|
+
/** Read uint8 (1 byte). */
|
|
24
|
+
u8(): number;
|
|
25
|
+
/** Read uint16 big-endian (2 bytes). */
|
|
26
|
+
u16(): number;
|
|
27
|
+
/** Read int16 big-endian (2 bytes). */
|
|
28
|
+
i16(): number;
|
|
29
|
+
/** Read uint32 big-endian (4 bytes). */
|
|
30
|
+
u32(): number;
|
|
31
|
+
/** Read int32 big-endian (4 bytes). */
|
|
32
|
+
i32(): number;
|
|
33
|
+
/**
|
|
34
|
+
* Read F2Dot14 (2 bytes): 2-bit signed integer + 14-bit fraction.
|
|
35
|
+
* Used for 2×2 scale components in composite glyphs.
|
|
36
|
+
* @returns Value in [-2, 2).
|
|
37
|
+
*/
|
|
38
|
+
f2dot14(): number;
|
|
39
|
+
/**
|
|
40
|
+
* Read a 16-bit offset (uint16). Alias for u16(), documents intent.
|
|
41
|
+
* @returns Byte offset relative to some base (caller's responsibility).
|
|
42
|
+
*/
|
|
43
|
+
offset16(): number;
|
|
44
|
+
/**
|
|
45
|
+
* Read a 32-bit offset (uint32). Alias for u32(), documents intent.
|
|
46
|
+
* @returns Byte offset relative to some base (caller's responsibility).
|
|
47
|
+
*/
|
|
48
|
+
offset32(): number;
|
|
49
|
+
/** Read uint16 big-endian at an absolute byte offset without moving pos. */
|
|
50
|
+
peekU16(at: number): number;
|
|
51
|
+
/** Read uint32 big-endian at an absolute byte offset without moving pos. */
|
|
52
|
+
peekU32(at: number): number;
|
|
53
|
+
/** Read int16 big-endian at an absolute byte offset without moving pos. */
|
|
54
|
+
peekI16(at: number): number;
|
|
55
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Maps a 4-char tag to its byte range within the font buffer. */
|
|
2
|
+
export interface TableRecord {
|
|
3
|
+
offset: number;
|
|
4
|
+
length: number;
|
|
5
|
+
}
|
|
6
|
+
/** Parsed SFNT table directory. */
|
|
7
|
+
export type TableMap = Map<string, TableRecord>;
|
|
8
|
+
/**
|
|
9
|
+
* Parses the SFNT table directory from a font buffer.
|
|
10
|
+
*
|
|
11
|
+
* Handles:
|
|
12
|
+
* - TrueType (.ttf): sfntVersion = 0x00010000 or 0x74727565 ('true')
|
|
13
|
+
* - CFF/OTF (.otf): sfntVersion = 0x4F54544F ('OTTO') — table map still usable,
|
|
14
|
+
* but `glyf` table won't be present
|
|
15
|
+
* - TTC (.ttc): magic = 0x74746366 ('ttcf') — picks font at index 0
|
|
16
|
+
*
|
|
17
|
+
* @param buffer Full font file bytes.
|
|
18
|
+
* @returns Map of 4-char tag → {offset, length}.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseSfnt(buffer: ArrayBuffer): TableMap;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A compiled codepoint-to-glyph-ID lookup function.
|
|
3
|
+
* Returns 0 (the .notdef glyph) when the codepoint is not found.
|
|
4
|
+
*/
|
|
5
|
+
export type CmapLookup = (codepoint: number) => number;
|
|
6
|
+
/**
|
|
7
|
+
* Parses the `cmap` table and returns a fast lookup function.
|
|
8
|
+
*
|
|
9
|
+
* Subtable selection priority (higher = preferred):
|
|
10
|
+
* 1. Platform 3, Encoding 10, Format 12 — Windows Unicode full repertoire (UCS-4)
|
|
11
|
+
* 2. Platform 0, Encoding 6, Format 12 — Unicode full repertoire
|
|
12
|
+
* 3. Platform 0, Encoding 4, Format 12 — Unicode 2.0+ full
|
|
13
|
+
* 4. Platform 3, Encoding 1, Format 4 — Windows Unicode BMP
|
|
14
|
+
* 5. Platform 0, Encoding ≤3, Format 4 — Unicode BMP
|
|
15
|
+
*
|
|
16
|
+
* If no supported format is found, returns a no-op lookup (always 0).
|
|
17
|
+
*
|
|
18
|
+
* @param buffer Full font buffer.
|
|
19
|
+
* @param offset Byte offset of the cmap table.
|
|
20
|
+
*/
|
|
21
|
+
export declare function parseCmap(buffer: ArrayBuffer, offset: number): CmapLookup;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* glyf.ts — TrueType glyph outline parser.
|
|
3
|
+
*
|
|
4
|
+
* Parses the `glyf` table to produce Shape objects (raw font units, y-up).
|
|
5
|
+
* Both simple and composite glyphs are supported.
|
|
6
|
+
*
|
|
7
|
+
* Canonical contour representation:
|
|
8
|
+
* - Implied on-curve midpoints between consecutive off-curve points are
|
|
9
|
+
* inserted explicitly.
|
|
10
|
+
* - Contours starting on an off-curve point are handled per spec:
|
|
11
|
+
* if the last point is also off-curve, start at their midpoint;
|
|
12
|
+
* otherwise rotate to start at the first on-curve point.
|
|
13
|
+
* - The resulting EdgeSegment sequence closes back to p0 of the first segment.
|
|
14
|
+
*
|
|
15
|
+
* Composite transforms supported:
|
|
16
|
+
* ARG_1_AND_2_ARE_WORDS, ARGS_ARE_XY_VALUES,
|
|
17
|
+
* WE_HAVE_A_SCALE, WE_HAVE_AN_X_AND_Y_SCALE, WE_HAVE_A_TWO_BY_TWO,
|
|
18
|
+
* USE_MY_METRICS, MORE_COMPONENTS.
|
|
19
|
+
*
|
|
20
|
+
* Not supported (silently ignored / treated as identity):
|
|
21
|
+
* ARGS_ARE_XY_VALUES not set (anchor point alignment) — returns origin offset
|
|
22
|
+
* of 0 for that component.
|
|
23
|
+
*/
|
|
24
|
+
import { type Shape } from "../../shape/shape.js";
|
|
25
|
+
/**
|
|
26
|
+
* A single raw outline point in font units, before implied on-curve midpoints
|
|
27
|
+
* are inserted. Composite transforms operate on these raw points so that
|
|
28
|
+
* midpoint expansion happens on the final merged integer outline — exactly as
|
|
29
|
+
* FreeType's FT_Outline_Decompose does after assembling all components.
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
interface RawPoint {
|
|
33
|
+
x: number;
|
|
34
|
+
y: number;
|
|
35
|
+
onCurve: boolean;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A glyph as a list of raw contours (arrays of RawPoint), in font units, with
|
|
39
|
+
* no implied midpoints inserted yet. This is the intermediate representation
|
|
40
|
+
* shared by simple and composite glyph parsing and by the composite recursion.
|
|
41
|
+
*/
|
|
42
|
+
export type RawGlyph = RawPoint[][];
|
|
43
|
+
/**
|
|
44
|
+
* Parses a single glyph from the `glyf` table.
|
|
45
|
+
*
|
|
46
|
+
* @param buffer Full font buffer.
|
|
47
|
+
* @param glyfTableOffset Byte offset of the `glyf` table within buffer.
|
|
48
|
+
* @param glyphOffset Byte offset of this glyph within the `glyf` table
|
|
49
|
+
* (from the `loca` table).
|
|
50
|
+
* @param glyphSize Size in bytes of this glyph entry (loca[i+1] - loca[i]).
|
|
51
|
+
* If 0, the glyph has no outline (space, empty glyph).
|
|
52
|
+
* @param getGlyphRaw Callback returning the raw (pre-midpoint) outline of a
|
|
53
|
+
* component glyph, used when assembling composite glyphs.
|
|
54
|
+
* @returns {Shape} Parsed outline in font units (y-up). Empty shape if no outline.
|
|
55
|
+
*/
|
|
56
|
+
export declare function parseGlyph(buffer: ArrayBuffer, glyfTableOffset: number, glyphOffset: number, glyphSize: number, getGlyphRaw: (glyphId: number) => RawGlyph): Shape;
|
|
57
|
+
/**
|
|
58
|
+
* Parses a glyph to its raw outline (contours of RawPoint, no implied midpoints).
|
|
59
|
+
* Recurses through composite components, applying each component's transform to
|
|
60
|
+
* the raw points before merging.
|
|
61
|
+
*
|
|
62
|
+
* @param buffer Full font buffer.
|
|
63
|
+
* @param glyfTableOffset Byte offset of the `glyf` table within buffer.
|
|
64
|
+
* @param glyphOffset Byte offset of this glyph within the `glyf` table.
|
|
65
|
+
* @param glyphSize Size in bytes of this glyph entry; 0 = no outline.
|
|
66
|
+
* @param getGlyphRaw Callback returning the raw outline of a component glyph.
|
|
67
|
+
* @returns {RawGlyph} Raw contours in font units.
|
|
68
|
+
*/
|
|
69
|
+
export declare function parseGlyphRaw(buffer: ArrayBuffer, glyfTableOffset: number, glyphOffset: number, glyphSize: number, getGlyphRaw: (glyphId: number) => RawGlyph): RawGlyph;
|
|
70
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface HeadTable {
|
|
2
|
+
/** Font design units per em-square (typically 1000 or 2048). */
|
|
3
|
+
unitsPerEm: number;
|
|
4
|
+
/** 0 = short loca offsets (uint16 × 2), 1 = long loca offsets (uint32). */
|
|
5
|
+
indexToLocFormat: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Parses the `head` table.
|
|
9
|
+
*
|
|
10
|
+
* head layout (OpenType spec §Table: head):
|
|
11
|
+
* u16 majorVersion
|
|
12
|
+
* u16 minorVersion
|
|
13
|
+
* i32 fontRevision (Fixed 16.16)
|
|
14
|
+
* u32 checksumAdjustment
|
|
15
|
+
* u32 magicNumber (0x5F0F3CF5)
|
|
16
|
+
* u16 flags
|
|
17
|
+
* u16 unitsPerEm
|
|
18
|
+
* i64 created
|
|
19
|
+
* i64 modified
|
|
20
|
+
* i16 xMin, yMin, xMax, yMax
|
|
21
|
+
* u16 macStyle
|
|
22
|
+
* u16 lowestRecPPEM
|
|
23
|
+
* i16 fontDirectionHint
|
|
24
|
+
* i16 indexToLocFormat
|
|
25
|
+
* i16 glyphDataFormat
|
|
26
|
+
*
|
|
27
|
+
* @param buffer Full font buffer.
|
|
28
|
+
* @param offset Byte offset of the head table.
|
|
29
|
+
*/
|
|
30
|
+
export declare function parseHead(buffer: ArrayBuffer, offset: number): HeadTable;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface HheaTable {
|
|
2
|
+
ascender: number;
|
|
3
|
+
descender: number;
|
|
4
|
+
lineGap: number;
|
|
5
|
+
numberOfHMetrics: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Parses the `hhea` table.
|
|
9
|
+
*
|
|
10
|
+
* hhea layout (OpenType spec §Table: hhea):
|
|
11
|
+
* u16 majorVersion
|
|
12
|
+
* u16 minorVersion
|
|
13
|
+
* i16 ascender
|
|
14
|
+
* i16 descender
|
|
15
|
+
* i16 lineGap
|
|
16
|
+
* u16 advanceWidthMax
|
|
17
|
+
* i16 minLeftSideBearing
|
|
18
|
+
* i16 minRightSideBearing
|
|
19
|
+
* i16 xMaxExtent
|
|
20
|
+
* i16 caretSlopeRise
|
|
21
|
+
* i16 caretSlopeRun
|
|
22
|
+
* i16 caretOffset
|
|
23
|
+
* i16 reserved × 4
|
|
24
|
+
* i16 metricDataFormat
|
|
25
|
+
* u16 numberOfHMetrics
|
|
26
|
+
*
|
|
27
|
+
* @param buffer Full font buffer.
|
|
28
|
+
* @param offset Byte offset of the hhea table.
|
|
29
|
+
*/
|
|
30
|
+
export declare function parseHhea(buffer: ArrayBuffer, offset: number): HheaTable;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface HMetric {
|
|
2
|
+
advanceWidth: number;
|
|
3
|
+
lsb: number;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Parses the `hmtx` table.
|
|
7
|
+
*
|
|
8
|
+
* Layout: `numberOfHMetrics` × (advanceWidth u16, lsb i16), then
|
|
9
|
+
* `(numGlyphs - numberOfHMetrics)` × lsb-only i16 entries (all sharing the
|
|
10
|
+
* last advanceWidth).
|
|
11
|
+
*
|
|
12
|
+
* @param buffer Full font buffer.
|
|
13
|
+
* @param offset Byte offset of the hmtx table.
|
|
14
|
+
* @param numGlyphs Total glyph count from maxp.
|
|
15
|
+
* @param numberOfHMetrics Count of full hMetric records from hhea.
|
|
16
|
+
* @returns Array of per-glyph {advanceWidth, lsb}, length = numGlyphs.
|
|
17
|
+
*/
|
|
18
|
+
export declare function parseHmtx(buffer: ArrayBuffer, offset: number, numGlyphs: number, numberOfHMetrics: number): HMetric[];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Kerning pair map: key = `(leftGlyphId << 16) | rightGlyphId`, value = kern value in font units.
|
|
3
|
+
* Returns 0 for unknown pairs.
|
|
4
|
+
*/
|
|
5
|
+
export type KernMap = (left: number, right: number) => number;
|
|
6
|
+
/**
|
|
7
|
+
* Parses the `kern` table (format 0 horizontal kerning pairs only).
|
|
8
|
+
*
|
|
9
|
+
* Silently skips sub-tables with unsupported formats (vertical kerning,
|
|
10
|
+
* cross-stream, format 2, etc.). Returns a no-op lookup if the table is
|
|
11
|
+
* absent or contains no usable format-0 horizontal pairs.
|
|
12
|
+
*
|
|
13
|
+
* @param buffer Full font buffer.
|
|
14
|
+
* @param offset Byte offset of the kern table (from TableMap).
|
|
15
|
+
* @returns Kern lookup function.
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseKern(buffer: ArrayBuffer, offset: number): KernMap;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses the `loca` table.
|
|
3
|
+
*
|
|
4
|
+
* Returns byte offsets into the `glyf` table for each glyph.
|
|
5
|
+
* Length of the returned array is `numGlyphs + 1`; the size of glyph `i`
|
|
6
|
+
* in the glyf table is `offsets[i+1] - offsets[i]`.
|
|
7
|
+
* A zero-size entry (offsets[i] === offsets[i+1]) means the glyph has no
|
|
8
|
+
* outline (e.g. space).
|
|
9
|
+
*
|
|
10
|
+
* Short format (indexToLocFormat === 0): entries are uint16, multiply by 2.
|
|
11
|
+
* Long format (indexToLocFormat === 1): entries are uint32, use directly.
|
|
12
|
+
*
|
|
13
|
+
* @param buffer Full font buffer.
|
|
14
|
+
* @param offset Byte offset of the loca table.
|
|
15
|
+
* @param numGlyphs Total glyph count from maxp.
|
|
16
|
+
* @param indexToLocFormat 0 or 1, from head table.
|
|
17
|
+
* @returns Array of byte offsets, length = numGlyphs + 1.
|
|
18
|
+
*/
|
|
19
|
+
export declare function parseLoca(buffer: ArrayBuffer, offset: number, numGlyphs: number, indexToLocFormat: number): number[];
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface MaxpTable {
|
|
2
|
+
numGlyphs: number;
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Parses the `maxp` table (version 0.5 and 1.0).
|
|
6
|
+
*
|
|
7
|
+
* @param buffer Full font buffer.
|
|
8
|
+
* @param offset Byte offset of the maxp table.
|
|
9
|
+
*/
|
|
10
|
+
export declare function parseMaxp(buffer: ArrayBuffer, offset: number): MaxpTable;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type { Shape } from "./shape/shape.js";
|
|
2
|
+
export type { Contour } from "./shape/contour.js";
|
|
3
|
+
export { EdgeSegment, LINEAR, QUADRATIC, CUBIC } from "./shape/segments.js";
|
|
4
|
+
export type { SegmentType } from "./shape/segments.js";
|
|
5
|
+
export { Font } from "./font/font.js";
|
|
6
|
+
export type { FontMetrics } from "./font/font.js";
|
|
7
|
+
export { Atlas, pixelFloatToByte } from "./atlas-gen.js";
|
|
8
|
+
export type { AtlasOptions, AtlasGlyph, LaidOutGlyph } from "./atlas-gen.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Real roots of a*x^3 + b*x^2 + c*x + d on a bounded interval [x0, x1].
|
|
3
|
+
*
|
|
4
|
+
* Bracketed Newton with bisection fallback. No transcendentals, no closures,
|
|
5
|
+
* no allocation. Roots are written into `out` in ascending order; returns count.
|
|
6
|
+
*
|
|
7
|
+
* Clip x0/x1 to your actual domain (e.g. 0..1 for Bezier parameters) — that's
|
|
8
|
+
* where most of the speed comes from.
|
|
9
|
+
*/
|
|
10
|
+
/** Stable quadratic fallback: a x^2 + b x + c on [x0, x1]. */
|
|
11
|
+
export declare function quadraticRoots(a: number, b: number, c: number, x0: number, x1: number, out: Float64Array | number[]): number;
|
|
12
|
+
export declare function cubicRoots(a: number, b: number, c: number, d: number, x0: number, x1: number, out: Float64Array | number[]): number;
|