forked from Hopding/fontkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fontkit.d.ts
640 lines (594 loc) · 15.7 KB
/
fontkit.d.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
export as namespace fontkit;
/**
* Represents a glyph bounding box
*/
export interface BoundingBox {
minX: number /** The minimum X position in the bounding box */;
minY: number /** The minimum Y position in the bounding box */;
maxX: number /** The maxmimum X position in the bounding box */;
maxY: number /** The maxmimum Y position in the bounding box */;
width: number /** The width of the bounding box */;
height: number /** The height of the bounding box */;
}
/**
* Path objects are returned by glyphs and represent the actual vector outlines
* for each glyph in the font. Paths can be converted to SVG path data strings,
* or to functions that can be applied to render the path to a graphics context.
*/
export interface Path {
/**
* This property represents the path’s bounding box, i.e. the smallest
* rectangle that contains the entire path shape. This is the exact
* bounding box, taking into account control points that may be outside the
* visible shape.
*/
bbox: BoundingBox;
/**
* This property represents the path’s control box. It is like the
* bounding box, but it includes all points of the path, including control
* points of bezier segments. It is much faster to compute than the real
* bounding box, but less accurate if there are control points outside of the
* visible shape.
*/
cbox: BoundingBox;
/**
* Moves the virtual pen to the given x, y coordinates.
*/
moveTo(x: number, y: number): void;
/**
* Adds a line to the path from the current point to the
* given x, y coordinates.
*/
lineTo(x: number, y: number): void;
/**
* Adds a quadratic curve to the path from the current point to the
* given x, y coordinates using cpx, cpy as a control point.
*/
quadraticCurveTo(cpx: number, cpy: number, x: number, y: number): void;
/**
* Adds a bezier curve to the path from the current point to the
* given x, y coordinates using cp1x, cp1y and cp2x, cp2y as control points.
*/
bezierCurveTo(
cp1x: number,
cp1y: number,
cp2x: number,
cp2y: number,
x: number,
y: number,
): void;
/**
* Closes the current sub-path by drawing a straight line back to the
* starting point.
*/
closePath(): void;
/**
* Compiles the path to a JavaScript function that can be applied with a
* graphics context in order to render the path.
*/
toFunction(): Function;
/**
* Converts the path to an SVG path data string.
*/
toSVG(): string;
}
/**
* Glyph objects represent a glyph in the font. They have various properties for
* accessing metrics and the actual vector path the glyph represents, and
* methods for rendering the glyph to a graphics context.
*
* You do not create glyph objects directly. They are created by various methods
* on the Font object. There are several subclasses of the base Glyph class
* internally that may be returned depending on the font format, but they all
* include the following API.
*/
export interface Glyph {
// Properties
id: number /** The glyph's id in the font */;
/**
* An array of unicode code points that are represented by this glyph. There
* can be multiple code points in the case of ligatures and other glyphs that
* represent multiple visual characters.
*/
codePoints: number[];
path: Path /** Vector Path object representing the glyph */;
/**
* The Glyph’s bounding box, i.e. the rectangle that encloses the glyph
* outline as tightly as possible.
*/
bbox: BoundingBox;
/**
* The Glyph’s control box. This is often the same as the bounding box, but is
* faster to compute. Because of the way bezier curves are defined, some of
* the control points can be outside of the bounding box. Where bbox takes
* this into account, cbox does not. Thus, cbox is less accurate, but faster
* to compute.
*/
cbox: BoundingBox;
advanceWidth: number /** The Glyph's advance width */;
// Methods
/**
* Renders the glyph to the given graphics context, at the specified
* font size.
*/
render(context: any, size: number): void;
// Color Glyph Properties/Methods
/**
* For SBIX glyphs, which are bitmap based, this returns an object containing
* some properties about the image, along with the image data itself
* (usually PNG).
*/
getImageForSize(size: number): Uint8Array;
/**
* For COLR glyphs, which are vector based, this returns an array of objects
* representing the glyphs and colors for each layer in render order.
*/
layers: any[];
}
/**
* Represents positioning information for a glyph in a GlyphRun.
*/
export interface GlyphPosition {
/**
* The amount to move the virtual pen in the X direction after rendering
* this glyph.
*/
xAdvance: number;
/**
* The amount to move the virtual pen in the Y direction after rendering
* this glyph.
*/
yAdvance: number;
/**
* The offset from the pen position in the X direction at which to render
* this glyph.
*/
xOffset: number;
/**
* The offset from the pen position in the Y direction at which to render
* this glyph.
*/
yOffset: number;
}
/**
* Represents a run of Glyph and GlyphPosition objects.
* Returned by the Font.layout method.
*/
export interface GlyphRun {
/**
* An array of Glyph objects in the run
*/
glyphs: Glyph[];
/**
* An array of GlyphPosition objects for each glyph in the run
*/
positions: GlyphPosition[];
/**
* The script that was requested for shaping. This was either passed in or detected automatically.
*/
script: string;
/**
* The language requested for shaping, as passed in. If `null`, the default language for the
* script was used.
*/
language: string | null;
/**
* The direction requested for shaping, as passed in (either ltr or rtl).
* If `null`, the default direction of the script is used.
*/
direction: 'ltr' | 'rtl' | null;
/**
* The features requested during shaping. This is a combination of user
* specified features and features chosen by the shaper.
*/
features: object;
/**
* The total advance width of the run.
*/
advanceWidth: number;
/**
* The total advance height of the run.
*/
advanceHeight: number;
/**
* The bounding box containing all glyphs in the run.
*/
bbox: BoundingBox;
}
export interface SubsetStream {
on: (
eventType: 'data' | 'end',
callback: (data: Uint8Array) => any,
) => SubsetStream;
}
export interface Subset {
/**
* Includes the given glyph object or glyph ID in the subset.
* Returns the glyph's new ID in the subset.
*/
includeGlyph(glyph: number | Glyph): number;
/**
* Returns a stream containing the encoded font file that can be piped to a
* destination, such as a file.
*/
encodeStream(): SubsetStream;
}
/**
* A map of OpenType features as described in OpenType's spec:
* https://docs.microsoft.com/en-gb/typography/opentype/spec/featurelist.
*/
export interface OpenTypeFeatures {
aalt?: boolean;
abvf?: boolean;
abvm?: boolean;
abvs?: boolean;
afrc?: boolean;
akhn?: boolean;
blwf?: boolean;
blwm?: boolean;
blws?: boolean;
calt?: boolean;
case?: boolean;
ccmp?: boolean;
cfar?: boolean;
cjct?: boolean;
clig?: boolean;
cpct?: boolean;
cpsp?: boolean;
cswh?: boolean;
curs?: boolean;
cv01?: boolean;
cv02?: boolean;
cv03?: boolean;
cv04?: boolean;
cv05?: boolean;
cv06?: boolean;
cv07?: boolean;
cv08?: boolean;
cv09?: boolean;
cv10?: boolean;
cv11?: boolean;
cv22?: boolean;
cv23?: boolean;
cv24?: boolean;
cv25?: boolean;
cv26?: boolean;
cv27?: boolean;
cv28?: boolean;
cv29?: boolean;
cv30?: boolean;
cv31?: boolean;
cv32?: boolean;
cv33?: boolean;
cv34?: boolean;
cv35?: boolean;
cv36?: boolean;
cv37?: boolean;
cv38?: boolean;
cv39?: boolean;
cv40?: boolean;
cv41?: boolean;
cv42?: boolean;
cv43?: boolean;
cv44?: boolean;
cv45?: boolean;
cv46?: boolean;
cv47?: boolean;
cv48?: boolean;
cv49?: boolean;
cv50?: boolean;
cv51?: boolean;
cv52?: boolean;
cv53?: boolean;
cv54?: boolean;
cv55?: boolean;
cv56?: boolean;
cv57?: boolean;
cv58?: boolean;
cv59?: boolean;
cv60?: boolean;
cv61?: boolean;
cv62?: boolean;
cv63?: boolean;
cv64?: boolean;
cv65?: boolean;
cv66?: boolean;
cv67?: boolean;
cv68?: boolean;
cv69?: boolean;
cv70?: boolean;
cv71?: boolean;
cv72?: boolean;
cv73?: boolean;
cv74?: boolean;
cv75?: boolean;
cv76?: boolean;
cv77?: boolean;
cv78?: boolean;
cv79?: boolean;
cv80?: boolean;
cv81?: boolean;
cv82?: boolean;
cv83?: boolean;
cv84?: boolean;
cv85?: boolean;
cv86?: boolean;
cv87?: boolean;
cv88?: boolean;
cv89?: boolean;
cv90?: boolean;
cv91?: boolean;
cv92?: boolean;
cv93?: boolean;
cv94?: boolean;
cv95?: boolean;
cv96?: boolean;
cv97?: boolean;
cv98?: boolean;
cv99?: boolean;
c2pc?: boolean;
c2sc?: boolean;
dist?: boolean;
dlig?: boolean;
dnom?: boolean;
dtls?: boolean;
expt?: boolean;
falt?: boolean;
fin2?: boolean;
fin3?: boolean;
fina?: boolean;
flac?: boolean;
frac?: boolean;
fwid?: boolean;
half?: boolean;
haln?: boolean;
halt?: boolean;
hist?: boolean;
hkna?: boolean;
hlig?: boolean;
hngl?: boolean;
hojo?: boolean;
hwid?: boolean;
init?: boolean;
isol?: boolean;
ital?: boolean;
jalt?: boolean;
jp78?: boolean;
jp83?: boolean;
jp90?: boolean;
jp04?: boolean;
kern?: boolean;
lfbd?: boolean;
liga?: boolean;
ljmo?: boolean;
lnum?: boolean;
locl?: boolean;
ltra?: boolean;
ltrm?: boolean;
mark?: boolean;
med2?: boolean;
medi?: boolean;
mgrk?: boolean;
mkmk?: boolean;
mset?: boolean;
nalt?: boolean;
nlck?: boolean;
nukt?: boolean;
numr?: boolean;
onum?: boolean;
opbd?: boolean;
ordn?: boolean;
ornm?: boolean;
palt?: boolean;
pcap?: boolean;
pkna?: boolean;
pnum?: boolean;
pref?: boolean;
pres?: boolean;
pstf?: boolean;
psts?: boolean;
pwid?: boolean;
qwid?: boolean;
rand?: boolean;
rclt?: boolean;
rkrf?: boolean;
rlig?: boolean;
rphf?: boolean;
rtbd?: boolean;
rtla?: boolean;
rtlm?: boolean;
ruby?: boolean;
rvrn?: boolean;
salt?: boolean;
sinf?: boolean;
size?: boolean;
smcp?: boolean;
smpl?: boolean;
ss01?: boolean;
ss02?: boolean;
ss03?: boolean;
ss04?: boolean;
ss05?: boolean;
ss06?: boolean;
ss07?: boolean;
ss08?: boolean;
ss09?: boolean;
ss10?: boolean;
ss11?: boolean;
ss12?: boolean;
ss13?: boolean;
ss14?: boolean;
ss15?: boolean;
ss16?: boolean;
ss17?: boolean;
ss18?: boolean;
ss19?: boolean;
ss20?: boolean;
ssty?: boolean;
stch?: boolean;
subs?: boolean;
sups?: boolean;
swsh?: boolean;
titl?: boolean;
tjmo?: boolean;
tnam?: boolean;
tnum?: boolean;
trad?: boolean;
twid?: boolean;
unic?: boolean;
valt?: boolean;
vatu?: boolean;
vert?: boolean;
vhal?: boolean;
vjmo?: boolean;
vkna?: boolean;
vkrn?: boolean;
vpal?: boolean;
vrt2?: boolean;
vrtr?: boolean;
zero?: boolean;
}
/**
* A map of Apple Advanced Typography (AAT) as decribed by Apple’s TrueType
* Reference manual:
* https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6AATIntro.html
*/
export interface AATFeatures {
acnt?: boolean;
ankr?: boolean;
avar?: boolean;
bdat?: boolean;
bhed?: boolean;
bloc?: boolean;
bsln?: boolean;
cmap?: boolean;
cvar?: boolean;
cvt?: boolean;
EBSC?: boolean;
fdsc?: boolean;
feat?: boolean;
fmtx?: boolean;
fond?: boolean;
fpgm?: boolean;
fvar?: boolean;
gasp?: boolean;
gcid?: boolean;
glyf?: boolean;
gvar?: boolean;
hdmx?: boolean;
head?: boolean;
hhea?: boolean;
hmtx?: boolean;
just?: boolean;
kern?: boolean;
kerx?: boolean;
lcar?: boolean;
loca?: boolean;
ltag?: boolean;
maxp?: boolean;
meta?: boolean;
mort?: boolean;
morx?: boolean;
name?: boolean;
opbd?: boolean;
'OS/2'?: boolean;
post?: boolean;
prep?: boolean;
prop?: boolean;
sbix?: boolean;
trak?: boolean;
vhea?: boolean;
vmtx?: boolean;
xref?: boolean;
Zapf?: boolean;
}
/**
* The features is an object mapping OpenType features to a boolean
* enabling or disabling each. If this is an AAT font,
* the OpenType feature tags are mapped to AAT features.
*/
export interface TypeFeatures extends OpenTypeFeatures, AATFeatures {
[key: string]: boolean;
}
/**
* There are several different types of font objects that are returned by
* fontkit depending on the font format. They all inherit from the TTFFont class
* and have the same public API.
*/
export interface Font {
// Metadata properties
postscriptName: string | null;
fullName: string | null;
familyName: string | null;
subfamilyName: string | null;
copyright: string | null;
version: string | null;
// Metrics properties
unitsPerEm: number /** Size of the font’s internal coordinate grid */;
ascent: number /** The font’s ascender */;
descent: number /** The font’s descender */;
lineGap: number /** Amount of space that should be included between lines */;
underlinePosition: number /** Offset from the normal underline position that should be used */;
underlineThickness: number /** Weight of the underline that should be used */;
italicAngle: number /** If this is an italic font, the angle the cursor should be drawn at to match the font design */;
capHeight: number /** Height of capital letters above the baseline. */;
xHeight: number /** Height of lower case letters. */;
bbox: BoundingBox /** Font’s bounding box, i.e. the box that encloses all glyphs in the font */;
// Other properties
numGlyphs: number /** Number of glyphs in the font */;
characterSet: number[] /** Array of all of the unicode code points supported by the font */;
availableFeatures: (keyof TypeFeatures)[] /** OpenType feature tags (or mapped AAT tags) supported by the font */;
cff: any;
'OS/2': { sFamilyClass: number };
head: { macStyle: { italic: boolean } };
post: { isFixedPitch: boolean };
// Character to Glyph Mapping Methods
/**
* Maps a single unicode code point (number) to a Glyph object.
* Does not perform any advanced substitutions (there is no context to do so).
*/
glyphForCodePoint(codePoint: number): Glyph;
/**
* Returns whether there is glyph in the font for the given
* unicode code point.
*/
hasGlyphForCodePoint(codePoint: number): boolean;
/**
* This method returns an array of Glyph objects for the given string.
* This is only a one-to-one mapping from characters to glyphs. For most uses,
* you should use Font.layout, which provides a much more advanced mapping
* supporting AAT and OpenType shaping.
*/
glyphsForString(string: string): Glyph[];
// Glyph Metrics and Layout Methods
/**
* Returns the advance width (described above) for a single glyph id.
*/
widthOfGlyph(glyphId: number): number;
/**
* This method returns a GlyphRun object, which includes an array of Glyphs
* and GlyphPositions for the given string. Glyph objects are described below.
* GlyphPosition objects include 4 properties: xAdvance, yAdvance, xOffset,
* and yOffset.
*
* The features parameter is an array of OpenType feature tags to be applied
* in addition to the default set. If this is an AAT font, the OpenType
* feature tags are mapped to AAT features.
*/
layout(
string: string,
features?: TypeFeatures | (keyof TypeFeatures)[],
): GlyphRun;
// Other Methods
/**
* Returns a glyph object for the given glyph id. You can pass the array of
* code points this glyph represents for your use later, and it will be
* stored in the glyph object.
*/
getGlyph(glyphId: number, codePoints?: number[]): Glyph;
/**
* Returns a Subset object for this font.
*/
createSubset(): Subset;
}
export function create(buffer: Uint8Array, postscriptName?: string): Font;