-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
322 lines (294 loc) Β· 8.76 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import init, {
from_bytes as fromBytes,
horizontal_line_metrics as horizontalLineMetrics,
lookup_glyph_index as lookupGlyphIndex,
metrics_indexed as metricsIndexed,
rasterize_indexed as rasterizeIndexed,
scale_factor as scaleFactor,
source,
units_per_em as unitsPerEm,
vertical_line_metrics as verticalLineMetrics,
} from "./wasm.js";
function memoize<A extends unknown[] | [], T>(
fn: (...args: A) => T,
): (...args: A) => T {
if (fn.length === 0) {
let cache: T | undefined = undefined;
return (...args: A): T => {
if (cache === undefined) {
cache = fn(...args);
}
return cache;
};
} else {
const cache: Record<string, T> = {};
return (...args: A): T => {
const key = args.join();
if (cache[key] === undefined) {
cache[key] = fn(...args);
}
return cache[key];
};
}
}
await init(source);
/**
* Settings for controlling specific font and layout behavior.
*/
export interface LoadOptions {
/**
* The default is true. This offsets glyphs relative to their position in their
* scaled bounding box. This is required for laying out glyphs correctly, but
* can be disabled to make some incorrect fonts crisper.
*/
enableOffsetBoundingBox: boolean;
/**
* The default is 0. The index of the font to use if parsing a font collection.
*/
collectionIndex: number;
/**
* The default is 40. The scale in px the font geometry is optimized for. Fonts
* rendered at the scale defined here will be the most optimal in terms of looks
* and performance. Glyphs rendered smaller than this scale will look the same
* but perform slightly worse, while glyphs rendered larger than this will looks
* worse but perform slightly better.
*/
scale: number;
}
/**
* Encapsulates all layout information associated with a glyph for a fixed scale.
*/
export interface Metrics {
/**
* Whole pixel offset of the left-most edge of the bitmap. This may be negative
* to reflect the glyph is positioned to the left of the origin.
*/
xmin: number;
/**
* Whole pixel offset of the bottom-most edge of the bitmap. This may be negative
* to refelct the glyph is positioned below the baseline.
*/
ymin: number;
/**
* The width of the bitmap in whole pixels.
*/
width: number;
/**
* The height of the bitmap in whole pixels.
*/
height: number;
/**
* Advance width of the glyph in subpixels. Used in horizontal fonts.
*/
advanceWidth: number;
/**
* Advance height of the glyph in subpixels. Used in vertical fonts.
*/
advanceHeight: number;
/**
* The bounding box that contains the glyph's outline at the offsets specified
* by the font. This is always a smaller box than the bitmap bounds.
*/
bounds: OutlineBounds;
}
/**
* Defines the bounds for a glyph's outline in subpixels. A glyph's outline is
* always contained in its bitmap.
*/
export interface OutlineBounds {
/**
* Subpixel offset of the left-most edge of the glyph's outline.
*/
xmin: number;
/**
* Subpixel offset of the bottom-most edge of the glyph's outline.
*/
ymin: number;
/**
* The width of the outline in subpixels.
*/
width: number;
/**
* The height of the outline in subpixels.
*/
height: number;
}
/**
* Metrics associated with line positioning.
*/
export interface LineMetrics {
/**
* The highest point that any glyph in the font extends to above the baseline.
* Typically positive.
*/
ascent: number;
/**
* The lowest point that any glyph in the font extends to below the baseline.
* Typically negative.
*/
descent: number;
/**
* The gap to leave between the descent of one line and the ascent of the next.
* This is of course only a guideline given by the font's designers.
*/
lineGap: number;
/**
* A precalculated value for the height or width of the line depending on if
* the font is laid out horizontally or vertically. It's calculated by:
* `ascent - descent + line_gap`.
*/
newLineSize: number;
}
/**
* The metrics and bitmap results of a rasterized glyph
*/
export interface RasterizeResult {
/**
* Sizing and positioning metadata for the rasterized glyph.
*/
metrics: Metrics;
/**
* Coverage array for the glyph. Coverage is a linear scale where 0 represents
* 0% coverage of that pixel by the glyph and 255 represents 100% coverage.
* The array starts at the top left corner of the glyph.
*/
bitmap: Uint8Array;
}
/**
* Represents a font.
*/
export class Font {
readonly #id: number;
constructor(bytes: Uint8Array, {
enableOffsetBoundingBox = true,
collectionIndex = 0,
scale = 40,
}: LoadOptions = {
enableOffsetBoundingBox: true,
collectionIndex: 0,
scale: 40,
}) {
this.#id = fromBytes(
bytes,
enableOffsetBoundingBox,
collectionIndex,
scale,
);
}
/**
* The new line height for the font. Only populated for fonts with vertical
* text layout metrics. `undefined` if unpopulated.
*/
horizontalLineMetrics(px: number): LineMetrics | undefined {
return horizontalLineMetrics(this.#id, px);
}
/**
* The new line height for the font. Only populated for fonts with horizontal
* text layout metrics. `undefined` if unpopulated.
*/
verticalLineMetrics(px: number): LineMetrics | undefined {
return verticalLineMetrics(this.#id, px);
}
/**
* Gets the font's units per em.
*/
unitsPerEm(): number {
return unitsPerEm(this.#id);
}
/**
* Calculates the glyph's outline scale factor for a given px size.
*/
scaleFactor(px: number): number {
return scaleFactor(this.#id, px);
}
/**
* Retrieves the layout metrics for the given character. If the character
* isn't present in the font, then the layout for the font's default character
* is returned instead.
*/
metrics(character: string, px: number): Metrics {
return this.metricsIndexed(this.lookupGlyphIndex(character), px);
}
/**
* Retrieves the layout metrics at the given index. You normally want to be
* using `metrics` instead, unless your glyphs are pre-indexed.
*/
metricsIndexed(index: number, px: number): Metrics {
return metricsIndexed(this.#id, index, px);
}
/**
* Retrieves the layout metrics and rasterized bitmap for the given character.
* If the character isn't present in the font, then the layout and bitmap for
* the font's default character is returned instead.
*/
rasterize(character: string, px: number): RasterizeResult {
return this.rasterizeIndexed(this.lookupGlyphIndex(character), px);
}
/**
* Retrieves the layout metrics and rasterized bitmap at the given index.
* You normally want to be using `rasterize` instead, unless your
* glyphs are pre-indexed.
*/
rasterizeIndexed(index: number, px: number): RasterizeResult {
return rasterizeIndexed(this.#id, index, px);
}
/**
* Finds the internal glyph index for the given character. If the character
* is not present in the font then 0 is returned.
*/
lookupGlyphIndex(character: string): number {
return lookupGlyphIndex(this.#id, character);
}
}
/**
* Represents a font which caches (memoizes) all returns, increasing performance.
*/
export class FontCached {
readonly #id: number;
horizontalLineMetrics: (px: number) => LineMetrics | undefined;
verticalLineMetrics: (px: number) => LineMetrics | undefined;
unitsPerEm: () => number;
scaleFactor: (px: number) => number;
metricsIndexed: (index: number, px: number) => Metrics;
rasterizeIndexed: (index: number, px: number) => RasterizeResult;
lookupGlyphIndex: (character: string) => number;
constructor(bytes: Uint8Array, {
enableOffsetBoundingBox = true,
collectionIndex = 0,
scale = 40,
}: LoadOptions = {
enableOffsetBoundingBox: true,
collectionIndex: 0,
scale: 40,
}) {
this.#id = fromBytes(
bytes,
enableOffsetBoundingBox,
collectionIndex,
scale,
);
this.horizontalLineMetrics = memoize((px) =>
horizontalLineMetrics(this.#id, px)
);
this.verticalLineMetrics = memoize((px) =>
verticalLineMetrics(this.#id, px)
);
this.unitsPerEm = memoize(() => unitsPerEm(this.#id));
this.scaleFactor = memoize((px) => scaleFactor(this.#id, px));
this.metricsIndexed = memoize((index, px) =>
metricsIndexed(this.#id, index, px)
);
this.rasterizeIndexed = memoize((index, px) =>
rasterizeIndexed(this.#id, index, px)
);
this.lookupGlyphIndex = memoize((character) =>
lookupGlyphIndex(this.#id, character)
);
}
metrics(character: string, px: number): Metrics {
return this.metricsIndexed(this.lookupGlyphIndex(character), px);
}
rasterize(character: string, px: number): RasterizeResult {
return this.rasterizeIndexed(this.lookupGlyphIndex(character), px);
}
}