@cantoo/fontkit 2.0.4
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 +21 -0
- package/README.md +274 -0
- package/dist/browser-module.mjs +13660 -0
- package/dist/browser-module.mjs.map +1 -0
- package/dist/browser.cjs +13689 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/main.cjs +13712 -0
- package/dist/main.cjs.map +1 -0
- package/dist/module.mjs +13673 -0
- package/dist/module.mjs.map +1 -0
- package/package.json +99 -0
- package/src/CmapProcessor.js +285 -0
- package/src/DFont.js +113 -0
- package/src/TTFFont.js +621 -0
- package/src/TrueTypeCollection.js +65 -0
- package/src/WOFF2Font.js +222 -0
- package/src/WOFFFont.js +56 -0
- package/src/aat/AATFeatureMap.js +545 -0
- package/src/aat/AATLayoutEngine.js +48 -0
- package/src/aat/AATLookupTable.js +125 -0
- package/src/aat/AATMorxProcessor.js +429 -0
- package/src/aat/AATStateMachine.js +96 -0
- package/src/base.js +29 -0
- package/src/cff/CFFCharsets.js +99 -0
- package/src/cff/CFFDict.js +166 -0
- package/src/cff/CFFEncodings.js +48 -0
- package/src/cff/CFFFont.js +158 -0
- package/src/cff/CFFIndex.js +150 -0
- package/src/cff/CFFOperand.js +135 -0
- package/src/cff/CFFPointer.js +50 -0
- package/src/cff/CFFPrivateDict.js +39 -0
- package/src/cff/CFFStandardStrings.js +71 -0
- package/src/cff/CFFTop.js +236 -0
- package/src/decorators.js +36 -0
- package/src/encodings.js +219 -0
- package/src/fs.js +12 -0
- package/src/glyph/BBox.js +72 -0
- package/src/glyph/CBDTGlyph.js +9 -0
- package/src/glyph/CFFGlyph.js +635 -0
- package/src/glyph/COLRGlyph.js +108 -0
- package/src/glyph/Glyph.js +270 -0
- package/src/glyph/GlyphVariationProcessor.js +487 -0
- package/src/glyph/Path.js +257 -0
- package/src/glyph/SBIXGlyph.js +54 -0
- package/src/glyph/StandardNames.js +27 -0
- package/src/glyph/TTFGlyph.js +408 -0
- package/src/glyph/TTFGlyphEncoder.js +158 -0
- package/src/glyph/WOFF2Glyph.js +70 -0
- package/src/index.js +15 -0
- package/src/layout/GlyphPosition.js +30 -0
- package/src/layout/GlyphRun.js +108 -0
- package/src/layout/KernProcessor.js +97 -0
- package/src/layout/LayoutEngine.js +190 -0
- package/src/layout/Script.js +231 -0
- package/src/layout/UnicodeLayoutEngine.js +250 -0
- package/src/node.js +16 -0
- package/src/opentype/GPOSProcessor.js +311 -0
- package/src/opentype/GSUBProcessor.js +208 -0
- package/src/opentype/GlyphInfo.js +57 -0
- package/src/opentype/GlyphIterator.js +82 -0
- package/src/opentype/OTLayoutEngine.js +117 -0
- package/src/opentype/OTProcessor.js +472 -0
- package/src/opentype/ShapingPlan.js +121 -0
- package/src/opentype/shapers/ArabicShaper.js +124 -0
- package/src/opentype/shapers/DefaultShaper.js +116 -0
- package/src/opentype/shapers/HangulShaper.js +285 -0
- package/src/opentype/shapers/HebrewShaper.js +105 -0
- package/src/opentype/shapers/IndicShaper.js +914 -0
- package/src/opentype/shapers/ThaiShaper.js +238 -0
- package/src/opentype/shapers/UniversalShaper.js +186 -0
- package/src/opentype/shapers/data.trie +0 -0
- package/src/opentype/shapers/gen-indic.js +208 -0
- package/src/opentype/shapers/gen-use.js +287 -0
- package/src/opentype/shapers/generate-data.js +33 -0
- package/src/opentype/shapers/index.js +109 -0
- package/src/opentype/shapers/indic-data.js +175 -0
- package/src/opentype/shapers/indic.json +1 -0
- package/src/opentype/shapers/indic.machine +31 -0
- package/src/opentype/shapers/indic.trie +0 -0
- package/src/opentype/shapers/use.json +1 -0
- package/src/opentype/shapers/use.machine +44 -0
- package/src/opentype/shapers/use.trie +0 -0
- package/src/packages/unicode-properties/LICENSE +8 -0
- package/src/packages/unicode-properties/README.md +12 -0
- package/src/packages/unicode-properties/data-trie.js +1 -0
- package/src/packages/unicode-properties/data.js +1 -0
- package/src/packages/unicode-properties/data.json +1 -0
- package/src/packages/unicode-properties/data.trie +0 -0
- package/src/packages/unicode-properties/dist/index.cjs +257 -0
- package/src/packages/unicode-properties/dist/index.cjs.map +7 -0
- package/src/packages/unicode-properties/dist/index.mjs +234 -0
- package/src/packages/unicode-properties/dist/index.mjs.map +7 -0
- package/src/packages/unicode-properties/generate.js +107 -0
- package/src/packages/unicode-properties/index.js +184 -0
- package/src/packages/unicode-properties/package.json +50 -0
- package/src/packages/unicode-trie/LICENSE +8 -0
- package/src/packages/unicode-trie/README.md +13 -0
- package/src/packages/unicode-trie/builder.js +965 -0
- package/src/packages/unicode-trie/dist/builder.cjs +711 -0
- package/src/packages/unicode-trie/dist/builder.cjs.map +7 -0
- package/src/packages/unicode-trie/dist/builder.mjs +690 -0
- package/src/packages/unicode-trie/dist/builder.mjs.map +7 -0
- package/src/packages/unicode-trie/dist/index.cjs +112 -0
- package/src/packages/unicode-trie/dist/index.cjs.map +7 -0
- package/src/packages/unicode-trie/dist/index.mjs +91 -0
- package/src/packages/unicode-trie/dist/index.mjs.map +7 -0
- package/src/packages/unicode-trie/index.js +135 -0
- package/src/packages/unicode-trie/package.json +49 -0
- package/src/packages/unicode-trie/swap.js +21 -0
- package/src/subset/CFFSubset.js +176 -0
- package/src/subset/Subset.js +63 -0
- package/src/subset/TTFSubset.js +130 -0
- package/src/tables/BASE.js +78 -0
- package/src/tables/COLR.js +25 -0
- package/src/tables/CPAL.js +24 -0
- package/src/tables/DSIG.js +21 -0
- package/src/tables/EBDT.js +91 -0
- package/src/tables/EBLC.js +80 -0
- package/src/tables/GDEF.js +57 -0
- package/src/tables/GPOS.js +209 -0
- package/src/tables/GSUB.js +85 -0
- package/src/tables/HVAR.js +44 -0
- package/src/tables/JSTF.js +43 -0
- package/src/tables/LTSH.js +10 -0
- package/src/tables/OS2.js +84 -0
- package/src/tables/PCLT.js +21 -0
- package/src/tables/STAT.js +99 -0
- package/src/tables/VDMX.js +33 -0
- package/src/tables/VORG.js +14 -0
- package/src/tables/WOFF2Directory.js +73 -0
- package/src/tables/WOFFDirectory.js +37 -0
- package/src/tables/aat.js +157 -0
- package/src/tables/avar.js +19 -0
- package/src/tables/bsln.js +31 -0
- package/src/tables/cmap.js +127 -0
- package/src/tables/cvt.js +6 -0
- package/src/tables/directory.js +58 -0
- package/src/tables/feat.js +28 -0
- package/src/tables/fpgm.js +8 -0
- package/src/tables/fvar.js +31 -0
- package/src/tables/gasp.js +15 -0
- package/src/tables/glyf.js +4 -0
- package/src/tables/gvar.js +27 -0
- package/src/tables/hdmx.js +15 -0
- package/src/tables/head.js +25 -0
- package/src/tables/hhea.js +19 -0
- package/src/tables/hmtx.js +11 -0
- package/src/tables/index.js +119 -0
- package/src/tables/just.js +81 -0
- package/src/tables/kern.js +91 -0
- package/src/tables/loca.js +30 -0
- package/src/tables/maxp.js +20 -0
- package/src/tables/morx.js +79 -0
- package/src/tables/name.js +157 -0
- package/src/tables/opbd.js +15 -0
- package/src/tables/opentype.js +217 -0
- package/src/tables/post.js +34 -0
- package/src/tables/prep.js +6 -0
- package/src/tables/sbix.js +17 -0
- package/src/tables/variations.js +80 -0
- package/src/tables/vhea.js +19 -0
- package/src/tables/vmtx.js +12 -0
- package/src/utils.js +62 -0
package/src/TTFFont.js
ADDED
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
import * as r from 'restructure';
|
|
2
|
+
import { cache } from './decorators';
|
|
3
|
+
import * as fontkit from './base';
|
|
4
|
+
import Directory from './tables/directory';
|
|
5
|
+
import tables from './tables';
|
|
6
|
+
import CmapProcessor from './CmapProcessor';
|
|
7
|
+
import LayoutEngine from './layout/LayoutEngine';
|
|
8
|
+
import TTFGlyph from './glyph/TTFGlyph';
|
|
9
|
+
import CFFGlyph from './glyph/CFFGlyph';
|
|
10
|
+
import SBIXGlyph from './glyph/SBIXGlyph';
|
|
11
|
+
import COLRGlyph from './glyph/COLRGlyph';
|
|
12
|
+
import CBDTGlyph from './glyph/CBDTGlyph';
|
|
13
|
+
import GlyphVariationProcessor from './glyph/GlyphVariationProcessor';
|
|
14
|
+
import TTFSubset from './subset/TTFSubset';
|
|
15
|
+
import CFFSubset from './subset/CFFSubset';
|
|
16
|
+
import BBox from './glyph/BBox';
|
|
17
|
+
import { asciiDecoder } from './utils';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* This is the base class for all SFNT-based font formats in fontkit.
|
|
21
|
+
* It supports TrueType, and PostScript glyphs, and several color glyph formats.
|
|
22
|
+
*/
|
|
23
|
+
export default class TTFFont {
|
|
24
|
+
type = 'TTF';
|
|
25
|
+
|
|
26
|
+
static probe(buffer) {
|
|
27
|
+
let format = asciiDecoder.decode(buffer.slice(0, 4));
|
|
28
|
+
return format === 'true' || format === 'OTTO' || format === String.fromCharCode(0, 1, 0, 0);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
constructor(stream, variationCoords = null) {
|
|
32
|
+
this.defaultLanguage = null;
|
|
33
|
+
this.stream = stream;
|
|
34
|
+
this.variationCoords = variationCoords;
|
|
35
|
+
|
|
36
|
+
this._directoryPos = this.stream.pos;
|
|
37
|
+
this._tables = {};
|
|
38
|
+
this._glyphs = {};
|
|
39
|
+
this._decodeDirectory();
|
|
40
|
+
this._installTableGetters();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
_installTableGetters() {
|
|
44
|
+
for (let tag in this.directory.tables) {
|
|
45
|
+
let table = this.directory.tables[tag];
|
|
46
|
+
if (tables[tag] && table.length > 0) {
|
|
47
|
+
Object.defineProperty(this, tag, {
|
|
48
|
+
get: this._getTable.bind(this, table),
|
|
49
|
+
configurable: true
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
setDefaultLanguage(lang = null) {
|
|
56
|
+
this.defaultLanguage = lang;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
_getTable(table) {
|
|
60
|
+
if (!(table.tag in this._tables)) {
|
|
61
|
+
try {
|
|
62
|
+
this._tables[table.tag] = this._decodeTable(table);
|
|
63
|
+
} catch (e) {
|
|
64
|
+
if (fontkit.logErrors) {
|
|
65
|
+
console.error(`Error decoding table ${table.tag}`);
|
|
66
|
+
console.error(e.stack);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return this._tables[table.tag];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
_getTableStream(tag) {
|
|
75
|
+
let table = this.directory.tables[tag];
|
|
76
|
+
if (table) {
|
|
77
|
+
this.stream.pos = table.offset;
|
|
78
|
+
return this.stream;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
_decodeDirectory() {
|
|
85
|
+
return this.directory = Directory.decode(this.stream, {_startOffset: 0});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
_decodeTable(table) {
|
|
89
|
+
let pos = this.stream.pos;
|
|
90
|
+
|
|
91
|
+
let stream = this._getTableStream(table.tag);
|
|
92
|
+
let result = tables[table.tag].decode(stream, this, table.length);
|
|
93
|
+
|
|
94
|
+
this.stream.pos = pos;
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
_getMetrics() {
|
|
99
|
+
if (this._metrics) {
|
|
100
|
+
return this._metrics;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Same compromise as FreeType for ascender/descender/height:
|
|
104
|
+
// https://gitlab.freedesktop.org/freetype/freetype/-/blob/master/src/sfnt/sfobjs.c
|
|
105
|
+
// typo*/win* fields exist only in OS/2 version >= 1
|
|
106
|
+
let os2 = this['OS/2'];
|
|
107
|
+
let hasTypo = os2 && os2.version > 0;
|
|
108
|
+
let ascent, descent, lineGap;
|
|
109
|
+
|
|
110
|
+
if (hasTypo && os2.fsSelection.useTypoMetrics) {
|
|
111
|
+
({typoAscender: ascent, typoDescender: descent, typoLineGap: lineGap} = os2);
|
|
112
|
+
} else {
|
|
113
|
+
({ascent, descent, lineGap} = this.hhea);
|
|
114
|
+
|
|
115
|
+
// Only when both hhea values are zero — FreeType uses !(ascender || descender)
|
|
116
|
+
if (!(ascent || descent) && hasTypo) {
|
|
117
|
+
if (os2.typoAscender || os2.typoDescender) {
|
|
118
|
+
({typoAscender: ascent, typoDescender: descent, typoLineGap: lineGap} = os2);
|
|
119
|
+
} else {
|
|
120
|
+
ascent = os2.winAscent;
|
|
121
|
+
descent = -os2.winDescent;
|
|
122
|
+
lineGap = 0;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return this._metrics = {
|
|
128
|
+
ascent,
|
|
129
|
+
descent,
|
|
130
|
+
lineGap,
|
|
131
|
+
lineHeight: ascent - descent + lineGap
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Gets a string from the font's `name` table
|
|
137
|
+
* `lang` is a BCP-47 language code.
|
|
138
|
+
* @return {string}
|
|
139
|
+
*/
|
|
140
|
+
getName(key, lang = this.defaultLanguage || fontkit.defaultLanguage) {
|
|
141
|
+
let record = this.name && this.name.records[key];
|
|
142
|
+
if (record) {
|
|
143
|
+
// Attempt to retrieve the entry, depending on which translation is available:
|
|
144
|
+
return (
|
|
145
|
+
record[lang]
|
|
146
|
+
|| record[this.defaultLanguage]
|
|
147
|
+
|| record[fontkit.defaultLanguage]
|
|
148
|
+
|| record['en']
|
|
149
|
+
|| record[Object.keys(record)[0]] // Seriously, ANY language would be fine
|
|
150
|
+
|| null
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* The unique PostScript name for this font, e.g. "Helvetica-Bold"
|
|
159
|
+
* @type {string}
|
|
160
|
+
*/
|
|
161
|
+
get postscriptName() {
|
|
162
|
+
return this.getName('postscriptName');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The font's full name, e.g. "Helvetica Bold"
|
|
167
|
+
* @type {string}
|
|
168
|
+
*/
|
|
169
|
+
get fullName() {
|
|
170
|
+
return this.getName('fullName');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The font's family name, e.g. "Helvetica"
|
|
175
|
+
* @type {string}
|
|
176
|
+
*/
|
|
177
|
+
get familyName() {
|
|
178
|
+
return this.getName('fontFamily');
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* The font's sub-family, e.g. "Bold".
|
|
183
|
+
* @type {string}
|
|
184
|
+
*/
|
|
185
|
+
get subfamilyName() {
|
|
186
|
+
return this.getName('fontSubfamily');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The font's copyright information
|
|
191
|
+
* @type {string}
|
|
192
|
+
*/
|
|
193
|
+
get copyright() {
|
|
194
|
+
return this.getName('copyright');
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* The font's version number
|
|
199
|
+
* @type {string}
|
|
200
|
+
*/
|
|
201
|
+
get version() {
|
|
202
|
+
return this.getName('version');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The font’s [ascender](https://en.wikipedia.org/wiki/Ascender_(typography))
|
|
207
|
+
* @type {number}
|
|
208
|
+
*/
|
|
209
|
+
get ascent() {
|
|
210
|
+
return this._getMetrics().ascent;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* The font’s [descender](https://en.wikipedia.org/wiki/Descender)
|
|
215
|
+
* @type {number}
|
|
216
|
+
*/
|
|
217
|
+
get descent() {
|
|
218
|
+
return this._getMetrics().descent;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* The amount of space that should be included between lines
|
|
223
|
+
* @type {number}
|
|
224
|
+
*/
|
|
225
|
+
get lineGap() {
|
|
226
|
+
return this._getMetrics().lineGap;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* The offset from the normal underline position that should be used
|
|
231
|
+
* @type {number}
|
|
232
|
+
*/
|
|
233
|
+
get underlinePosition() {
|
|
234
|
+
return this.post.underlinePosition;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* The weight of the underline that should be used
|
|
239
|
+
* @type {number}
|
|
240
|
+
*/
|
|
241
|
+
get underlineThickness() {
|
|
242
|
+
return this.post.underlineThickness;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* If this is an italic font, the angle the cursor should be drawn at to match the font design
|
|
247
|
+
* @type {number}
|
|
248
|
+
*/
|
|
249
|
+
get italicAngle() {
|
|
250
|
+
return this.post.italicAngle;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* The vertical space between adjacent lines (their baselines) of text.
|
|
255
|
+
* See [here](https://en.wikipedia.org/wiki/Leading) for more details.
|
|
256
|
+
* @type {number}
|
|
257
|
+
*/
|
|
258
|
+
get lineHeight() {
|
|
259
|
+
return this._getMetrics().lineHeight;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* The height of capital letters above the baseline.
|
|
264
|
+
* See [here](https://en.wikipedia.org/wiki/Cap_height) for more details.
|
|
265
|
+
* @type {number}
|
|
266
|
+
*/
|
|
267
|
+
get capHeight() {
|
|
268
|
+
let os2 = this['OS/2'];
|
|
269
|
+
return os2 ? os2.capHeight : this.ascent;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* The height of lower case letters in the font.
|
|
274
|
+
* See [here](https://en.wikipedia.org/wiki/X-height) for more details.
|
|
275
|
+
* @type {number}
|
|
276
|
+
*/
|
|
277
|
+
get xHeight() {
|
|
278
|
+
let os2 = this['OS/2'];
|
|
279
|
+
return os2 ? os2.xHeight : 0;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* The number of glyphs in the font.
|
|
284
|
+
* @type {number}
|
|
285
|
+
*/
|
|
286
|
+
get numGlyphs() {
|
|
287
|
+
return this.maxp.numGlyphs;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* The size of the font’s internal coordinate grid
|
|
292
|
+
* @type {number}
|
|
293
|
+
*/
|
|
294
|
+
get unitsPerEm() {
|
|
295
|
+
return this.head.unitsPerEm;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* The font’s bounding box, i.e. the box that encloses all glyphs in the font.
|
|
300
|
+
* @type {BBox}
|
|
301
|
+
*/
|
|
302
|
+
@cache
|
|
303
|
+
get bbox() {
|
|
304
|
+
return Object.freeze(new BBox(this.head.xMin, this.head.yMin, this.head.xMax, this.head.yMax));
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
@cache
|
|
308
|
+
get _cmapProcessor() {
|
|
309
|
+
return new CmapProcessor(this.cmap);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* An array of all of the unicode code points supported by the font.
|
|
314
|
+
* @type {number[]}
|
|
315
|
+
*/
|
|
316
|
+
@cache
|
|
317
|
+
get characterSet() {
|
|
318
|
+
return this._cmapProcessor.getCharacterSet();
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Returns whether there is glyph in the font for the given unicode code point.
|
|
323
|
+
*
|
|
324
|
+
* @param {number} codePoint
|
|
325
|
+
* @return {boolean}
|
|
326
|
+
*/
|
|
327
|
+
hasGlyphForCodePoint(codePoint) {
|
|
328
|
+
return !!this._cmapProcessor.lookup(codePoint);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Maps a single unicode code point to a Glyph object.
|
|
333
|
+
* Does not perform any advanced substitutions (there is no context to do so).
|
|
334
|
+
*
|
|
335
|
+
* @param {number} codePoint
|
|
336
|
+
* @return {Glyph}
|
|
337
|
+
*/
|
|
338
|
+
glyphForCodePoint(codePoint) {
|
|
339
|
+
return this.getGlyph(this._cmapProcessor.lookup(codePoint), [codePoint]);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Returns an array of Glyph objects for the given string.
|
|
344
|
+
* This is only a one-to-one mapping from characters to glyphs.
|
|
345
|
+
* For most uses, you should use font.layout (described below), which
|
|
346
|
+
* provides a much more advanced mapping supporting AAT and OpenType shaping.
|
|
347
|
+
*
|
|
348
|
+
* @param {string} string
|
|
349
|
+
* @return {Glyph[]}
|
|
350
|
+
*/
|
|
351
|
+
glyphsForString(string) {
|
|
352
|
+
let glyphs = [];
|
|
353
|
+
let len = string.length;
|
|
354
|
+
let idx = 0;
|
|
355
|
+
let last = -1;
|
|
356
|
+
let state = -1;
|
|
357
|
+
|
|
358
|
+
while (idx <= len) {
|
|
359
|
+
let code = 0;
|
|
360
|
+
let nextState = 0;
|
|
361
|
+
|
|
362
|
+
if (idx < len) {
|
|
363
|
+
// Decode the next codepoint from UTF 16
|
|
364
|
+
code = string.charCodeAt(idx++);
|
|
365
|
+
if (0xd800 <= code && code <= 0xdbff && idx < len) {
|
|
366
|
+
let next = string.charCodeAt(idx);
|
|
367
|
+
if (0xdc00 <= next && next <= 0xdfff) {
|
|
368
|
+
idx++;
|
|
369
|
+
code = ((code & 0x3ff) << 10) + (next & 0x3ff) + 0x10000;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Compute the next state: 1 if the next codepoint is a variation selector, 0 otherwise.
|
|
374
|
+
nextState = ((0xfe00 <= code && code <= 0xfe0f) || (0xe0100 <= code && code <= 0xe01ef)) ? 1 : 0;
|
|
375
|
+
} else {
|
|
376
|
+
idx++;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (state === 0 && nextState === 1) {
|
|
380
|
+
// Variation selector following normal codepoint.
|
|
381
|
+
glyphs.push(this.getGlyph(this._cmapProcessor.lookup(last, code), [last, code]));
|
|
382
|
+
} else if (state === 0 && nextState === 0) {
|
|
383
|
+
// Normal codepoint following normal codepoint.
|
|
384
|
+
glyphs.push(this.glyphForCodePoint(last));
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
last = code;
|
|
388
|
+
state = nextState;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return glyphs;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
@cache
|
|
395
|
+
get _layoutEngine() {
|
|
396
|
+
return new LayoutEngine(this);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Returns a GlyphRun object, which includes an array of Glyphs and GlyphPositions for the given string.
|
|
401
|
+
*
|
|
402
|
+
* @param {string} string
|
|
403
|
+
* @param {string[]} [userFeatures]
|
|
404
|
+
* @param {string} [script]
|
|
405
|
+
* @param {string} [language]
|
|
406
|
+
* @param {string} [direction]
|
|
407
|
+
* @return {GlyphRun}
|
|
408
|
+
*/
|
|
409
|
+
layout(string, userFeatures, script, language, direction) {
|
|
410
|
+
return this._layoutEngine.layout(string, userFeatures, script, language, direction);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Returns an array of strings that map to the given glyph id.
|
|
415
|
+
* @param {number} gid - glyph id
|
|
416
|
+
*/
|
|
417
|
+
stringsForGlyph(gid) {
|
|
418
|
+
return this._layoutEngine.stringsForGlyph(gid);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* An array of all [OpenType feature tags](https://www.microsoft.com/typography/otspec/featuretags.htm)
|
|
423
|
+
* (or mapped AAT tags) supported by the font.
|
|
424
|
+
* The features parameter is an array of OpenType feature tags to be applied in addition to the default set.
|
|
425
|
+
* If this is an AAT font, the OpenType feature tags are mapped to AAT features.
|
|
426
|
+
*
|
|
427
|
+
* @type {string[]}
|
|
428
|
+
*/
|
|
429
|
+
get availableFeatures() {
|
|
430
|
+
return this._layoutEngine.getAvailableFeatures();
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
getAvailableFeatures(script, language) {
|
|
434
|
+
return this._layoutEngine.getAvailableFeatures(script, language);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
_getBaseGlyph(glyph, characters = []) {
|
|
438
|
+
let cached = this._glyphs[glyph];
|
|
439
|
+
if (cached?._getContours) {
|
|
440
|
+
return cached;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
let outline = null;
|
|
444
|
+
if (this.directory.tables.glyf) {
|
|
445
|
+
outline = new TTFGlyph(glyph, characters, this);
|
|
446
|
+
} else if (this.directory.tables['CFF '] || this.directory.tables.CFF2) {
|
|
447
|
+
outline = new CFFGlyph(glyph, characters, this);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
if (outline && !cached) {
|
|
451
|
+
this._glyphs[glyph] = outline;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
return outline;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Returns a glyph object for the given glyph id.
|
|
459
|
+
* You can pass the array of code points this glyph represents for
|
|
460
|
+
* your use later, and it will be stored in the glyph object.
|
|
461
|
+
*
|
|
462
|
+
* @param {number} glyph
|
|
463
|
+
* @param {number[]} characters
|
|
464
|
+
* @return {Glyph}
|
|
465
|
+
*/
|
|
466
|
+
getGlyph(glyph, characters = []) {
|
|
467
|
+
if (!this._glyphs[glyph]) {
|
|
468
|
+
if (this.directory.tables.sbix) {
|
|
469
|
+
this._glyphs[glyph] = new SBIXGlyph(glyph, characters, this);
|
|
470
|
+
|
|
471
|
+
} else if ((this.directory.tables.COLR) && (this.directory.tables.CPAL)) {
|
|
472
|
+
this._glyphs[glyph] = new COLRGlyph(glyph, characters, this);
|
|
473
|
+
|
|
474
|
+
} else if (this.directory.tables.CBLC || this.directory.tables.EBLC) {
|
|
475
|
+
this._glyphs[glyph] = new CBDTGlyph(glyph, characters, this);
|
|
476
|
+
|
|
477
|
+
} else {
|
|
478
|
+
this._getBaseGlyph(glyph, characters);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
return this._glyphs[glyph] || null;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Returns a Subset for this font.
|
|
487
|
+
* @return {Subset}
|
|
488
|
+
*/
|
|
489
|
+
createSubset() {
|
|
490
|
+
if (this.directory.tables['CFF ']) {
|
|
491
|
+
return new CFFSubset(this);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return new TTFSubset(this);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* Returns an object describing the available variation axes
|
|
499
|
+
* that this font supports. Keys are setting tags, and values
|
|
500
|
+
* contain the axis name, range, and default value.
|
|
501
|
+
*
|
|
502
|
+
* @type {object}
|
|
503
|
+
*/
|
|
504
|
+
@cache
|
|
505
|
+
get variationAxes() {
|
|
506
|
+
let res = {};
|
|
507
|
+
if (!this.fvar) {
|
|
508
|
+
return res;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
for (let axis of this.fvar.axis) {
|
|
512
|
+
res[axis.axisTag.trim()] = {
|
|
513
|
+
name: axis.name.en,
|
|
514
|
+
min: axis.minValue,
|
|
515
|
+
default: axis.defaultValue,
|
|
516
|
+
max: axis.maxValue
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
return res;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Returns an object describing the named variation instances
|
|
525
|
+
* that the font designer has specified. Keys are variation names
|
|
526
|
+
* and values are the variation settings for this instance.
|
|
527
|
+
*
|
|
528
|
+
* @type {object}
|
|
529
|
+
*/
|
|
530
|
+
@cache
|
|
531
|
+
get namedVariations() {
|
|
532
|
+
let res = {};
|
|
533
|
+
if (!this.fvar) {
|
|
534
|
+
return res;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
for (let instance of this.fvar.instance) {
|
|
538
|
+
let settings = {};
|
|
539
|
+
for (let i = 0; i < this.fvar.axis.length; i++) {
|
|
540
|
+
let axis = this.fvar.axis[i];
|
|
541
|
+
settings[axis.axisTag.trim()] = instance.coord[i];
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
res[instance.name.en] = settings;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
return res;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Returns a new font with the given variation settings applied.
|
|
552
|
+
* Settings can either be an instance name, or an object containing
|
|
553
|
+
* variation tags as specified by the `variationAxes` property.
|
|
554
|
+
*
|
|
555
|
+
* @param {object} settings
|
|
556
|
+
* @return {TTFFont}
|
|
557
|
+
*/
|
|
558
|
+
getVariation(settings) {
|
|
559
|
+
if (!(this.directory.tables.fvar && ((this.directory.tables.gvar && this.directory.tables.glyf) || this.directory.tables.CFF2))) {
|
|
560
|
+
throw new Error('Variations require a font with the fvar, gvar and glyf, or CFF2 tables.');
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
if (typeof settings === 'string') {
|
|
564
|
+
settings = this.namedVariations[settings];
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if (typeof settings !== 'object') {
|
|
568
|
+
throw new Error('Variation settings must be either a variation name or settings object.');
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// normalize the coordinates
|
|
572
|
+
let coords = this.fvar.axis.map(axis => {
|
|
573
|
+
let axisTag = axis.axisTag.trim();
|
|
574
|
+
if (axisTag in settings) {
|
|
575
|
+
return Math.max(axis.minValue, Math.min(axis.maxValue, settings[axisTag]));
|
|
576
|
+
} else {
|
|
577
|
+
return axis.defaultValue;
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
// Decompress WOFF/WOFF2 on the source once so the clone shares resolved tables.
|
|
582
|
+
if (typeof this._decompress === 'function') {
|
|
583
|
+
this._decompress();
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// Preserve subclass (WOFF/WOFF2) and share decoded state. @cache values and
|
|
587
|
+
// table getters are non-enumerable, so Object.assign skips them; we reinstall
|
|
588
|
+
// getters and clear the glyph cache for this variation instance.
|
|
589
|
+
let font = Object.create(Object.getPrototypeOf(this));
|
|
590
|
+
Object.assign(font, this);
|
|
591
|
+
font.variationCoords = coords;
|
|
592
|
+
font._glyphs = {};
|
|
593
|
+
font._installTableGetters();
|
|
594
|
+
return font;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
@cache
|
|
598
|
+
get _variationProcessor() {
|
|
599
|
+
if (!this.fvar) {
|
|
600
|
+
return null;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
let variationCoords = this.variationCoords;
|
|
604
|
+
|
|
605
|
+
// Ignore if no variation coords and not CFF2
|
|
606
|
+
if (!variationCoords && !this.CFF2) {
|
|
607
|
+
return null;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
if (!variationCoords) {
|
|
611
|
+
variationCoords = this.fvar.axis.map(axis => axis.defaultValue);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
return new GlyphVariationProcessor(this, variationCoords);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// Standardized format plugin API
|
|
618
|
+
getFont(name) {
|
|
619
|
+
return this.getVariation(name);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as r from 'restructure';
|
|
2
|
+
import TTFFont from './TTFFont';
|
|
3
|
+
import { asciiDecoder } from './utils';
|
|
4
|
+
|
|
5
|
+
let TTCHeader = new r.VersionedStruct(r.uint32, {
|
|
6
|
+
0x00010000: {
|
|
7
|
+
numFonts: r.uint32,
|
|
8
|
+
offsets: new r.Array(r.uint32, 'numFonts')
|
|
9
|
+
},
|
|
10
|
+
0x00020000: {
|
|
11
|
+
numFonts: r.uint32,
|
|
12
|
+
offsets: new r.Array(r.uint32, 'numFonts'),
|
|
13
|
+
dsigTag: r.uint32,
|
|
14
|
+
dsigLength: r.uint32,
|
|
15
|
+
dsigOffset: r.uint32
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export default class TrueTypeCollection {
|
|
20
|
+
type = 'TTC';
|
|
21
|
+
|
|
22
|
+
static probe(buffer) {
|
|
23
|
+
return asciiDecoder.decode(buffer.slice(0, 4)) === 'ttcf';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
constructor(stream) {
|
|
27
|
+
this.stream = stream;
|
|
28
|
+
if (stream.readString(4) !== 'ttcf') {
|
|
29
|
+
throw new Error('Not a TrueType collection');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
this.header = TTCHeader.decode(stream);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
getFont(name) {
|
|
36
|
+
for (let offset of this.header.offsets) {
|
|
37
|
+
let stream = new r.DecodeStream(this.stream.buffer);
|
|
38
|
+
stream.pos = offset;
|
|
39
|
+
let font = new TTFFont(stream);
|
|
40
|
+
if (
|
|
41
|
+
font.postscriptName === name ||
|
|
42
|
+
(
|
|
43
|
+
font.postscriptName instanceof Uint8Array &&
|
|
44
|
+
name instanceof Uint8Array &&
|
|
45
|
+
font.postscriptName.every((v, i) => name[i] === v)
|
|
46
|
+
)
|
|
47
|
+
) {
|
|
48
|
+
return font;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
get fonts() {
|
|
56
|
+
let fonts = [];
|
|
57
|
+
for (let offset of this.header.offsets) {
|
|
58
|
+
let stream = new r.DecodeStream(this.stream.buffer);
|
|
59
|
+
stream.pos = offset;
|
|
60
|
+
fonts.push(new TTFFont(stream));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return fonts;
|
|
64
|
+
}
|
|
65
|
+
}
|