single-file-core 1.6.41 → 1.7.1
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/core/filename.js +11 -2
- package/core/helper.js +16 -3
- package/core/index.js +13 -2
- package/core/lib/processor-helper-common.js +38 -0
- package/core/lib/processor-helper-inline.js +25 -0
- package/core/lib/processor-helper.js +25 -0
- package/core/util.js +1 -0
- package/modules/css-fonts-minifier.js +4 -1
- package/modules/font-glyphs-minifier.js +1570 -0
- package/modules/template-formatter.js +2 -0
- package/package.json +1 -1
- package/processors/frame-tree/content/content-frame-tree.js +20 -8
- package/vendor/css-tree.js +3 -2
|
@@ -0,0 +1,1570 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2010-2024 Gildas Lormeau
|
|
3
|
+
* contact : gildas.lormeau <at> gmail.com
|
|
4
|
+
*
|
|
5
|
+
* This file is part of SingleFile.
|
|
6
|
+
*
|
|
7
|
+
* The code in this file is free software: you can redistribute it and/or
|
|
8
|
+
* modify it under the terms of the GNU Affero General Public License
|
|
9
|
+
* (GNU AGPL) as published by the Free Software Foundation, either version 3
|
|
10
|
+
* of the License, or (at your option) any later version.
|
|
11
|
+
*
|
|
12
|
+
* The code in this file is distributed in the hope that it will be useful,
|
|
13
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
|
|
15
|
+
* General Public License for more details.
|
|
16
|
+
*
|
|
17
|
+
* As additional permission under GNU AGPL version 3 section 7, you may
|
|
18
|
+
* distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
|
|
19
|
+
* AGPL normally required by section 4, provided you include this license
|
|
20
|
+
* notice and a URL through which recipients can access the Corresponding
|
|
21
|
+
* Source.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/* global Blob, CompressionStream, DecompressionStream, Response */
|
|
25
|
+
|
|
26
|
+
const SFNT_TRUETYPE = 0x00010000;
|
|
27
|
+
const SFNT_TRUE = 0x74727565;
|
|
28
|
+
const SFNT_WOFF = 0x774F4646;
|
|
29
|
+
const SFNT_WOFF2 = 0x774F4632;
|
|
30
|
+
const SFNT_HEADER_LENGTH = 12;
|
|
31
|
+
const SFNT_ENTRY_LENGTH = 16;
|
|
32
|
+
const WOFF_HEADER_LENGTH = 44;
|
|
33
|
+
const WOFF_ENTRY_LENGTH = 20;
|
|
34
|
+
const WOFF2_HEADER_LENGTH = 48;
|
|
35
|
+
const WOFF2_KNOWN_TAGS = ["cmap", "head", "hhea", "hmtx", "maxp", "name", "OS/2", "post", "cvt ", "fpgm", "glyf", "loca", "prep", "CFF ", "VORG", "EBDT", "EBLC", "gasp", "hdmx", "kern", "LTSH", "PCLT", "VDMX", "vhea", "vmtx", "BASE", "GDEF", "GPOS", "GSUB", "EBSC", "JSTF", "MATH", "CBDT", "CBLC", "COLR", "CPAL", "SVG ", "sbix", "acnt", "avar", "bdat", "bloc", "bsln", "cvar", "fdsc", "feat", "fmtx", "fvar", "gvar", "hsty", "just", "lcar", "mort", "morx", "opbd", "prop", "trak", "Zapf", "Silf", "Glat", "Gloc", "Feat", "Sill"];
|
|
36
|
+
const WOFF2_ARBITRARY_TAG = 0x3F;
|
|
37
|
+
const WOFF2_TRANSFORM_VERSION_SHIFT = 6;
|
|
38
|
+
const WOFF2_GLYF_TRANSFORM = 0;
|
|
39
|
+
const WOFF2_GLYF_NULL_TRANSFORM = 3;
|
|
40
|
+
const WOFF2_HMTX_TRANSFORM = 1;
|
|
41
|
+
const WOFF2_GLYF_HEADER_LENGTH = 36;
|
|
42
|
+
const WOFF2_OVERLAP_BITMAP_FLAG = 0x0001;
|
|
43
|
+
const WOFF2_HMTX_NO_LSB_FLAG = 0x01;
|
|
44
|
+
const WOFF2_HMTX_NO_LEFT_SIDE_BEARING_FLAG = 0x02;
|
|
45
|
+
const WOFF2_WORD_CODE = 253;
|
|
46
|
+
const WOFF2_ONE_MORE_BYTE_CODE_1 = 255;
|
|
47
|
+
const WOFF2_ONE_MORE_BYTE_CODE_2 = 254;
|
|
48
|
+
const WOFF2_LOWEST_U_CODE = 253;
|
|
49
|
+
const WOFF2_TRIPLET_OFF_CURVE = 0x80;
|
|
50
|
+
const CHECKSUM_ADJUSTMENT_MAGIC = 0xB1B0AFBA;
|
|
51
|
+
const HEAD_CHECKSUM_OFFSET = 8;
|
|
52
|
+
const HEAD_FLAGS_OFFSET = 16;
|
|
53
|
+
const HEAD_FLAGS_TRANSFORMED = 0x0800;
|
|
54
|
+
const HEAD_LOCA_FORMAT_OFFSET = 50;
|
|
55
|
+
const HHEA_METRICS_COUNT_OFFSET = 34;
|
|
56
|
+
const MAXP_GLYPHS_OFFSET = 4;
|
|
57
|
+
const FSTYPE_OFFSET = 8;
|
|
58
|
+
const FSTYPE_NO_SUBSETTING = 0x0100;
|
|
59
|
+
const LONG_LOCA_FORMAT = 1;
|
|
60
|
+
const NOTDEF_GLYPH_ID = 0;
|
|
61
|
+
const GLYPH_HEADER_LENGTH = 10;
|
|
62
|
+
const GLYPH_FLAG_ON_CURVE = 0x01;
|
|
63
|
+
const GLYPH_FLAG_X_SHORT = 0x02;
|
|
64
|
+
const GLYPH_FLAG_Y_SHORT = 0x04;
|
|
65
|
+
const GLYPH_FLAG_REPEAT = 0x08;
|
|
66
|
+
const GLYPH_FLAG_X_SAME_OR_POSITIVE = 0x10;
|
|
67
|
+
const GLYPH_FLAG_Y_SAME_OR_POSITIVE = 0x20;
|
|
68
|
+
const GLYPH_FLAG_OVERLAP_SIMPLE = 0x40;
|
|
69
|
+
const COMPOSITE_ARG_1_AND_2_ARE_WORDS = 0x0001;
|
|
70
|
+
const COMPOSITE_WE_HAVE_A_SCALE = 0x0008;
|
|
71
|
+
const COMPOSITE_MORE_COMPONENTS = 0x0020;
|
|
72
|
+
const COMPOSITE_WE_HAVE_AN_X_AND_Y_SCALE = 0x0040;
|
|
73
|
+
const COMPOSITE_WE_HAVE_A_TWO_BY_TWO = 0x0080;
|
|
74
|
+
const COMPOSITE_WE_HAVE_INSTRUCTIONS = 0x0100;
|
|
75
|
+
const GSUB_LOOKUP_SINGLE = 1;
|
|
76
|
+
const GSUB_LOOKUP_MULTIPLE = 2;
|
|
77
|
+
const GSUB_LOOKUP_ALTERNATE = 3;
|
|
78
|
+
const GSUB_LOOKUP_LIGATURE = 4;
|
|
79
|
+
const GSUB_LOOKUP_EXTENSION = 7;
|
|
80
|
+
const GSUB_LOOKUP_REVERSE_CHAINING = 8;
|
|
81
|
+
const CMAP_FORMAT_BYTE = 0;
|
|
82
|
+
const CMAP_FORMAT_SEGMENT = 4;
|
|
83
|
+
const CMAP_FORMAT_TRIMMED = 6;
|
|
84
|
+
const CMAP_FORMAT_GROUPS = 12;
|
|
85
|
+
const CMAP_FORMAT_VARIATION = 14;
|
|
86
|
+
const CMAP_PLATFORM_UNICODE = 0;
|
|
87
|
+
const CMAP_PLATFORM_WINDOWS = 3;
|
|
88
|
+
const CMAP_ENCODING_UNICODE_BMP = 3;
|
|
89
|
+
const CMAP_ENCODING_UNICODE_FULL = 4;
|
|
90
|
+
const CMAP_ENCODING_UNICODE_VARIATION = 5;
|
|
91
|
+
const CMAP_ENCODING_WINDOWS_BMP = 1;
|
|
92
|
+
const CMAP_ENCODING_WINDOWS_FULL = 10;
|
|
93
|
+
const MAX_BMP_CHAR_CODE = 0xFFFF;
|
|
94
|
+
const UNSUPPORTED_TABLES = ["CFF ", "CFF2", "gvar", "fvar", "COLR", "CBDT", "sbix", "SVG ", "morx", "mort", "MATH"];
|
|
95
|
+
const REGEXP_COMBINING_CHARACTER = /^[\p{M}ᅠ-ᇿ]$/u;
|
|
96
|
+
const BIDI_MIRRORING_PAIRS = "\u0028\u0029\u003C\u003E\u005B\u005D\u007B\u007D\u00AB\u00BB\u0F3A\u0F3B\u0F3C\u0F3D\u169B\u169C\u2039\u203A\u2045\u2046\u207D\u207E\u208D\u208E\u2208\u220B\u2209\u220C\u220A\u220D\u2215\u29F5\u221D\u{1DB10}\u221F\u2BFE\u2220\u29A3\u2221\u299B\u2222\u29A0\u2224\u2AEE\u223C\u223D\u2243\u22CD\u2245\u224C\u2252\u2253\u2254\u2255\u2264\u2265\u2266\u2267\u2268\u2269\u226A\u226B\u226E\u226F\u2270\u2271\u2272\u2273\u2274\u2275\u2276\u2277\u2278\u2279\u227A\u227B\u227C\u227D\u227E\u227F\u2280\u2281\u2282\u2283\u2284\u2285\u2286\u2287\u2288\u2289\u228A\u228B\u228F\u2290\u2291\u2292\u2298\u29B8\u22A2\u22A3\u22A6\u2ADE\u22A8\u2AE4\u22A9\u2AE3\u22AB\u2AE5\u22B0\u22B1\u22B2\u22B3\u22B4\u22B5\u22B6\u22B7\u22B8\u27DC\u22C9\u22CA\u22CB\u22CC\u22D0\u22D1\u22D6\u22D7\u22D8\u22D9\u22DA\u22DB\u22DC\u22DD\u22DE\u22DF\u22E0\u22E1\u22E2\u22E3\u22E4\u22E5\u22E6\u22E7\u22E8\u22E9\u22EA\u22EB\u22EC\u22ED\u22F0\u22F1\u22F2\u22FA\u22F3\u22FB\u22F4\u22FC\u22F6\u22FD\u22F7\u22FE\u2308\u2309\u230A\u230B\u2329\u232A\u2768\u2769\u276A\u276B\u276C\u276D\u276E\u276F\u2770\u2771\u2772\u2773\u2774\u2775\u27C3\u27C4\u27C5\u27C6\u27C8\u27C9\u27CB\u27CD\u27D5\u27D6\u27DD\u27DE\u27E2\u27E3\u27E4\u27E5\u27E6\u27E7\u27E8\u27E9\u27EA\u27EB\u27EC\u27ED\u27EE\u27EF\u2983\u2984\u2985\u2986\u2987\u2988\u2989\u298A\u298B\u298C\u298D\u2990\u298E\u298F\u2991\u2992\u2993\u2994\u2995\u2996\u2997\u2998\u29A4\u29A5\u29A8\u29A9\u29AA\u29AB\u29AC\u29AD\u29AE\u29AF\u29C0\u29C1\u29C4\u29C5\u29CF\u29D0\u29D1\u29D2\u29D4\u29D5\u29D8\u29D9\u29DA\u29DB\u29E8\u29E9\u29F8\u29F9\u29FC\u29FD\u2A2B\u2A2C\u2A2D\u2A2E\u2A34\u2A35\u2A3C\u2A3D\u2A64\u2A65\u2A79\u2A7A\u2A7B\u2A7C\u2A7D\u2A7E\u2A7F\u2A80\u2A81\u2A82\u2A83\u2A84\u2A85\u2A86\u2A87\u2A88\u2A89\u2A8A\u2A8B\u2A8C\u2A8D\u2A8E\u2A8F\u2A90\u2A91\u2A92\u2A93\u2A94\u2A95\u2A96\u2A97\u2A98\u2A99\u2A9A\u2A9B\u2A9C\u2A9D\u2A9E\u2A9F\u2AA0\u2AA1\u2AA2\u2AA6\u2AA7\u2AA8\u2AA9\u2AAA\u2AAB\u2AAC\u2AAD\u2AAF\u2AB0\u2AB1\u2AB2\u2AB3\u2AB4\u2AB5\u2AB6\u2AB7\u2AB8\u2AB9\u2ABA\u2ABB\u2ABC\u2ABD\u2ABE\u2ABF\u2AC0\u2AC1\u2AC2\u2AC3\u2AC4\u2AC5\u2AC6\u2AC7\u2AC8\u2AC9\u2ACA\u2ACB\u2ACC\u2ACD\u2ACE\u2ACF\u2AD0\u2AD1\u2AD2\u2AD3\u2AD4\u2AD5\u2AD6\u2AEC\u2AED\u2AF7\u2AF8\u2AF9\u2AFA\u2E02\u2E03\u2E04\u2E05\u2E09\u2E0A\u2E0C\u2E0D\u2E1C\u2E1D\u2E20\u2E21\u2E22\u2E23\u2E24\u2E25\u2E26\u2E27\u2E28\u2E29\u2E55\u2E56\u2E57\u2E58\u2E59\u2E5A\u2E5B\u2E5C\u2E62\u2E63\u3008\u3009\u300A\u300B\u300C\u300D\u300E\u300F\u3010\u3011\u3014\u3015\u3016\u3017\u3018\u3019\u301A\u301B\uFE59\uFE5A\uFE5B\uFE5C\uFE5D\uFE5E\uFE64\uFE65\uFF08\uFF09\uFF1C\uFF1E\uFF3B\uFF3D\uFF5B\uFF5D\uFF5F\uFF60\uFF62\uFF63\u{1DB03}\u{1DB04}\u{1DB05}\u{1DB06}\u{1DB08}\u{1DB09}";
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
const DROPPED_TABLES = ["DSIG"];
|
|
100
|
+
const TAG_CMAP = "cmap";
|
|
101
|
+
const TAG_GLYF = "glyf";
|
|
102
|
+
const TAG_LOCA = "loca";
|
|
103
|
+
const TAG_HEAD = "head";
|
|
104
|
+
const TAG_MAXP = "maxp";
|
|
105
|
+
const TAG_HMTX = "hmtx";
|
|
106
|
+
const TAG_HHEA = "hhea";
|
|
107
|
+
const TAG_OS2 = "OS/2";
|
|
108
|
+
const TAG_GSUB = "GSUB";
|
|
109
|
+
const REQUIRED_TABLES = [TAG_CMAP, TAG_GLYF, TAG_LOCA, TAG_HEAD, TAG_MAXP, TAG_HMTX];
|
|
110
|
+
const BIDI_MIRRORING = getBidiMirroring();
|
|
111
|
+
|
|
112
|
+
export {
|
|
113
|
+
process
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
async function process(content, charCodeRanges, options = {}) {
|
|
117
|
+
const data = new Uint8Array(content.buffer ? content : new Uint8Array(content));
|
|
118
|
+
const brotli = getBrotli(options);
|
|
119
|
+
let font;
|
|
120
|
+
try {
|
|
121
|
+
font = await parseFont(data, brotli);
|
|
122
|
+
} catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
if (!font) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
const tables = font.tables;
|
|
129
|
+
if (REQUIRED_TABLES.find(tag => !tables.has(tag)) || UNSUPPORTED_TABLES.find(tag => tables.has(tag))) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
const os2 = tables.get(TAG_OS2);
|
|
133
|
+
if (os2 && os2.length > FSTYPE_OFFSET + 1 && (readUint16(os2, FSTYPE_OFFSET) & FSTYPE_NO_SUBSETTING)) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const glyphCount = readUint16(tables.get(TAG_MAXP), MAXP_GLYPHS_OFFSET);
|
|
138
|
+
const locaOffsets = readLoca(tables.get(TAG_LOCA), glyphCount, readInt16(tables.get(TAG_HEAD), HEAD_LOCA_FORMAT_OFFSET));
|
|
139
|
+
if (!locaOffsets) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
const cmap = readCmap(tables.get(TAG_CMAP));
|
|
143
|
+
const keptCharCodes = new Map();
|
|
144
|
+
getDrawableCharCodes(charCodeRanges).forEach(charCode => {
|
|
145
|
+
const glyphId = cmap.mapping.get(charCode);
|
|
146
|
+
if (glyphId !== undefined && glyphId < glyphCount) {
|
|
147
|
+
keptCharCodes.set(charCode, glyphId);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
const keptGlyphIds = new Set([NOTDEF_GLYPH_ID, ...keptCharCodes.values()]);
|
|
151
|
+
cmap.variationGlyphIds.forEach((glyphIds, charCode) => {
|
|
152
|
+
if (keptCharCodes.has(charCode)) {
|
|
153
|
+
glyphIds.forEach(glyphId => keptGlyphIds.add(glyphId));
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
if (tables.has(TAG_GSUB)) {
|
|
157
|
+
addGsubClosure(tables.get(TAG_GSUB), keptGlyphIds);
|
|
158
|
+
}
|
|
159
|
+
const glyf = tables.get(TAG_GLYF);
|
|
160
|
+
Array.from(keptGlyphIds).forEach(glyphId => addCompositeComponents(glyf, locaOffsets, glyphId, keptGlyphIds));
|
|
161
|
+
let removedGlyphs = 0;
|
|
162
|
+
for (let glyphId = 0; glyphId < glyphCount; glyphId++) {
|
|
163
|
+
if (!keptGlyphIds.has(glyphId) && locaOffsets[glyphId + 1] > locaOffsets[glyphId]) {
|
|
164
|
+
removedGlyphs++;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (!removedGlyphs && keptCharCodes.size == cmap.mapping.size) {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
const { glyfTable, locaTable } = writeGlyf(glyf, locaOffsets, glyphCount, keptGlyphIds);
|
|
171
|
+
tables.set(TAG_GLYF, glyfTable);
|
|
172
|
+
tables.set(TAG_LOCA, locaTable);
|
|
173
|
+
tables.set(TAG_CMAP, writeCmap(keptCharCodes, cmap.variationSubtable));
|
|
174
|
+
const head = new Uint8Array(tables.get(TAG_HEAD));
|
|
175
|
+
writeInt16(head, HEAD_LOCA_FORMAT_OFFSET, LONG_LOCA_FORMAT);
|
|
176
|
+
writeUint32(head, HEAD_CHECKSUM_OFFSET, 0);
|
|
177
|
+
if (font.woff2) {
|
|
178
|
+
writeUint16(head, HEAD_FLAGS_OFFSET, readUint16(head, HEAD_FLAGS_OFFSET) | HEAD_FLAGS_TRANSFORMED);
|
|
179
|
+
}
|
|
180
|
+
tables.set(TAG_HEAD, head);
|
|
181
|
+
DROPPED_TABLES.forEach(tag => tables.delete(tag));
|
|
182
|
+
const sfnt = writeSfnt(font.flavor, tables);
|
|
183
|
+
let result;
|
|
184
|
+
if (font.woff2) {
|
|
185
|
+
result = await writeWoff2(sfnt, font, brotli, glyphCount);
|
|
186
|
+
} else if (font.woff) {
|
|
187
|
+
result = await writeWoff(sfnt, font);
|
|
188
|
+
} else {
|
|
189
|
+
result = sfnt.data;
|
|
190
|
+
}
|
|
191
|
+
if (result.length >= data.length || !await testFont(result)) {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
return result;
|
|
195
|
+
} catch {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function getBidiMirroring() {
|
|
201
|
+
const mirroring = new Map();
|
|
202
|
+
const characters = Array.from(BIDI_MIRRORING_PAIRS);
|
|
203
|
+
for (let indexPair = 0; indexPair < characters.length; indexPair += 2) {
|
|
204
|
+
mirroring.set(characters[indexPair], characters[indexPair + 1]);
|
|
205
|
+
mirroring.set(characters[indexPair + 1], characters[indexPair]);
|
|
206
|
+
}
|
|
207
|
+
return mirroring;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function getDrawableCharCodes(charCodeRanges) {
|
|
211
|
+
const charCodes = new Set();
|
|
212
|
+
const combiners = new Set();
|
|
213
|
+
const addCharacter = character => {
|
|
214
|
+
const charCode = character.codePointAt(0);
|
|
215
|
+
if (!charCodes.has(charCode)) {
|
|
216
|
+
charCodes.add(charCode);
|
|
217
|
+
if (REGEXP_COMBINING_CHARACTER.test(character)) {
|
|
218
|
+
combiners.add(character);
|
|
219
|
+
}
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
return false;
|
|
223
|
+
};
|
|
224
|
+
charCodeRanges.forEach(([fromCharCode, toCharCode]) => {
|
|
225
|
+
for (let charCode = fromCharCode; charCode <= toCharCode; charCode++) {
|
|
226
|
+
const character = String.fromCodePoint(charCode);
|
|
227
|
+
addCharacter(character);
|
|
228
|
+
Array.from(character.normalize("NFD")).forEach(addCharacter);
|
|
229
|
+
Array.from(character.normalize("NFC")).forEach(addCharacter);
|
|
230
|
+
if (BIDI_MIRRORING.has(character)) {
|
|
231
|
+
addCharacter(BIDI_MIRRORING.get(character));
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
let composed;
|
|
236
|
+
do {
|
|
237
|
+
composed = false;
|
|
238
|
+
Array.from(charCodes).forEach(charCode => {
|
|
239
|
+
const base = String.fromCodePoint(charCode);
|
|
240
|
+
combiners.forEach(combiner => {
|
|
241
|
+
const composition = (base + combiner).normalize("NFC");
|
|
242
|
+
if (Array.from(composition).length == 1 && addCharacter(composition)) {
|
|
243
|
+
composed = true;
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
} while (composed);
|
|
248
|
+
return charCodes;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function getBrotli(options) {
|
|
252
|
+
if (options.brotli) {
|
|
253
|
+
return options.brotli;
|
|
254
|
+
}
|
|
255
|
+
try {
|
|
256
|
+
new CompressionStream("brotli");
|
|
257
|
+
new DecompressionStream("brotli");
|
|
258
|
+
} catch {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
return {
|
|
262
|
+
compress: data => transform(data, new CompressionStream("brotli")),
|
|
263
|
+
decompress: data => transform(data, new DecompressionStream("brotli"))
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
async function testFont(data) {
|
|
268
|
+
if (typeof globalThis.FontFace == "undefined") {
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
try {
|
|
272
|
+
await new globalThis.FontFace("single-file-test", data.buffer).load();
|
|
273
|
+
return true;
|
|
274
|
+
} catch {
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
async function parseFont(data, brotli) {
|
|
280
|
+
if (data.length < SFNT_HEADER_LENGTH) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
const signature = readUint32(data, 0);
|
|
284
|
+
if (signature == SFNT_TRUETYPE || signature == SFNT_TRUE) {
|
|
285
|
+
const tableCount = readUint16(data, 4);
|
|
286
|
+
const tables = new Map();
|
|
287
|
+
for (let indexTable = 0; indexTable < tableCount; indexTable++) {
|
|
288
|
+
const entryOffset = SFNT_HEADER_LENGTH + indexTable * SFNT_ENTRY_LENGTH;
|
|
289
|
+
const tag = readTag(data, entryOffset);
|
|
290
|
+
const offset = readUint32(data, entryOffset + 8);
|
|
291
|
+
const length = readUint32(data, entryOffset + 12);
|
|
292
|
+
tables.set(tag, data.subarray(offset, offset + length));
|
|
293
|
+
}
|
|
294
|
+
return { flavor: signature, tables, woff: false };
|
|
295
|
+
} else if (signature == SFNT_WOFF) {
|
|
296
|
+
const flavor = readUint32(data, 4);
|
|
297
|
+
if (flavor != SFNT_TRUETYPE && flavor != SFNT_TRUE) {
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
const tableCount = readUint16(data, 12);
|
|
301
|
+
const metaOffset = readUint32(data, 24);
|
|
302
|
+
const metaLength = readUint32(data, 28);
|
|
303
|
+
const metaOriginalLength = readUint32(data, 32);
|
|
304
|
+
const privateOffset = readUint32(data, 36);
|
|
305
|
+
const privateLength = readUint32(data, 40);
|
|
306
|
+
const tables = new Map();
|
|
307
|
+
for (let indexTable = 0; indexTable < tableCount; indexTable++) {
|
|
308
|
+
const entryOffset = WOFF_HEADER_LENGTH + indexTable * WOFF_ENTRY_LENGTH;
|
|
309
|
+
const tag = readTag(data, entryOffset);
|
|
310
|
+
const offset = readUint32(data, entryOffset + 4);
|
|
311
|
+
const compressedLength = readUint32(data, entryOffset + 8);
|
|
312
|
+
const originalLength = readUint32(data, entryOffset + 12);
|
|
313
|
+
const tableData = data.subarray(offset, offset + compressedLength);
|
|
314
|
+
tables.set(tag, compressedLength < originalLength ? await inflate(tableData, originalLength) : tableData);
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
flavor,
|
|
318
|
+
tables,
|
|
319
|
+
woff: true,
|
|
320
|
+
version: readUint32(data, 20),
|
|
321
|
+
metadata: metaLength ? data.subarray(metaOffset, metaOffset + metaLength) : null,
|
|
322
|
+
metaOriginalLength,
|
|
323
|
+
privateData: privateLength ? data.subarray(privateOffset, privateOffset + privateLength) : null
|
|
324
|
+
};
|
|
325
|
+
} else if (signature == SFNT_WOFF2) {
|
|
326
|
+
return parseWoff2(data, brotli);
|
|
327
|
+
}
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
async function parseWoff2(data, brotli) {
|
|
332
|
+
const flavor = readUint32(data, 4);
|
|
333
|
+
if ((flavor != SFNT_TRUETYPE && flavor != SFNT_TRUE) || !brotli || data.length < WOFF2_HEADER_LENGTH) {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
const tableCount = readUint16(data, 12);
|
|
337
|
+
const totalCompressedSize = readUint32(data, 20);
|
|
338
|
+
const metaOffset = readUint32(data, 28);
|
|
339
|
+
const metaLength = readUint32(data, 32);
|
|
340
|
+
const metaOriginalLength = readUint32(data, 36);
|
|
341
|
+
const privateOffset = readUint32(data, 40);
|
|
342
|
+
const privateLength = readUint32(data, 44);
|
|
343
|
+
const entries = [];
|
|
344
|
+
let offset = WOFF2_HEADER_LENGTH;
|
|
345
|
+
for (let indexTable = 0; indexTable < tableCount; indexTable++) {
|
|
346
|
+
const flags = data[offset++];
|
|
347
|
+
let tag;
|
|
348
|
+
if ((flags & WOFF2_ARBITRARY_TAG) == WOFF2_ARBITRARY_TAG) {
|
|
349
|
+
tag = readTag(data, offset);
|
|
350
|
+
offset += 4;
|
|
351
|
+
} else {
|
|
352
|
+
tag = WOFF2_KNOWN_TAGS[flags & WOFF2_ARBITRARY_TAG];
|
|
353
|
+
}
|
|
354
|
+
const transformVersion = flags >> WOFF2_TRANSFORM_VERSION_SHIFT;
|
|
355
|
+
let originalLength, transformedLength, transformed;
|
|
356
|
+
[originalLength, offset] = readBase128(data, offset);
|
|
357
|
+
if (tag == TAG_GLYF || tag == TAG_LOCA) {
|
|
358
|
+
if (transformVersion != WOFF2_GLYF_TRANSFORM && transformVersion != WOFF2_GLYF_NULL_TRANSFORM) {
|
|
359
|
+
return null;
|
|
360
|
+
}
|
|
361
|
+
transformed = transformVersion == WOFF2_GLYF_TRANSFORM;
|
|
362
|
+
} else if (tag == TAG_HMTX) {
|
|
363
|
+
if (transformVersion > WOFF2_HMTX_TRANSFORM) {
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
transformed = transformVersion == WOFF2_HMTX_TRANSFORM;
|
|
367
|
+
} else if (transformVersion) {
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
370
|
+
if (transformed) {
|
|
371
|
+
[transformedLength, offset] = readBase128(data, offset);
|
|
372
|
+
} else {
|
|
373
|
+
transformedLength = originalLength;
|
|
374
|
+
}
|
|
375
|
+
entries.push({ tag, transformedLength, transformed });
|
|
376
|
+
}
|
|
377
|
+
const decompressed = await brotli.decompress(data.subarray(offset, offset + totalCompressedSize));
|
|
378
|
+
if (decompressed.length != entries.reduce((length, entry) => length + entry.transformedLength, 0)) {
|
|
379
|
+
throw new Error("Invalid WOFF2 data length");
|
|
380
|
+
}
|
|
381
|
+
const tables = new Map();
|
|
382
|
+
let transformedGlyf, transformedHmtx, tableOffset = 0;
|
|
383
|
+
entries.forEach(entry => {
|
|
384
|
+
const tableData = decompressed.subarray(tableOffset, tableOffset + entry.transformedLength);
|
|
385
|
+
tableOffset += entry.transformedLength;
|
|
386
|
+
if (!entry.transformed) {
|
|
387
|
+
tables.set(entry.tag, tableData);
|
|
388
|
+
} else if (entry.tag == TAG_GLYF) {
|
|
389
|
+
transformedGlyf = tableData;
|
|
390
|
+
} else if (entry.tag == TAG_HMTX) {
|
|
391
|
+
transformedHmtx = tableData;
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
if (!tables.has(TAG_HEAD) || !tables.has(TAG_MAXP)) {
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
const glyphCount = readUint16(tables.get(TAG_MAXP), MAXP_GLYPHS_OFFSET);
|
|
398
|
+
let xMins;
|
|
399
|
+
if (transformedGlyf) {
|
|
400
|
+
const glyf = reconstructGlyf(transformedGlyf, glyphCount);
|
|
401
|
+
tables.set(TAG_GLYF, glyf.glyfTable);
|
|
402
|
+
tables.set(TAG_LOCA, glyf.locaTable);
|
|
403
|
+
const head = new Uint8Array(tables.get(TAG_HEAD));
|
|
404
|
+
writeInt16(head, HEAD_LOCA_FORMAT_OFFSET, LONG_LOCA_FORMAT);
|
|
405
|
+
tables.set(TAG_HEAD, head);
|
|
406
|
+
xMins = glyf.xMins;
|
|
407
|
+
}
|
|
408
|
+
if (transformedHmtx) {
|
|
409
|
+
if (!tables.has(TAG_HHEA) || !tables.has(TAG_GLYF) || !tables.has(TAG_LOCA)) {
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
if (!xMins) {
|
|
413
|
+
const locaOffsets = readLoca(tables.get(TAG_LOCA), glyphCount, readInt16(tables.get(TAG_HEAD), HEAD_LOCA_FORMAT_OFFSET));
|
|
414
|
+
if (!locaOffsets) {
|
|
415
|
+
return null;
|
|
416
|
+
}
|
|
417
|
+
const glyf = tables.get(TAG_GLYF);
|
|
418
|
+
xMins = locaOffsets.slice(0, glyphCount).map((glyphOffset, glyphId) => locaOffsets[glyphId + 1] - glyphOffset >= GLYPH_HEADER_LENGTH ? readInt16(glyf, glyphOffset + 2) : 0);
|
|
419
|
+
}
|
|
420
|
+
tables.set(TAG_HMTX, reconstructHmtx(transformedHmtx, xMins, readUint16(tables.get(TAG_HHEA), HHEA_METRICS_COUNT_OFFSET), glyphCount));
|
|
421
|
+
}
|
|
422
|
+
return {
|
|
423
|
+
flavor,
|
|
424
|
+
tables,
|
|
425
|
+
woff: false,
|
|
426
|
+
woff2: true,
|
|
427
|
+
version: readUint32(data, 24),
|
|
428
|
+
metadata: metaLength ? data.subarray(metaOffset, metaOffset + metaLength) : null,
|
|
429
|
+
metaOriginalLength,
|
|
430
|
+
privateData: privateLength ? data.subarray(privateOffset, privateOffset + privateLength) : null
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function reconstructGlyf(data, glyphCount) {
|
|
435
|
+
const optionFlags = readUint16(data, 2);
|
|
436
|
+
const streamSizes = [];
|
|
437
|
+
for (let indexStream = 0; indexStream < 7; indexStream++) {
|
|
438
|
+
streamSizes.push(readUint32(data, 8 + indexStream * 4));
|
|
439
|
+
}
|
|
440
|
+
let offset = WOFF2_GLYF_HEADER_LENGTH;
|
|
441
|
+
const [nContourStream, nPointsStream, flagStream, glyphStream, compositeStream, bboxData, instructionStream] = streamSizes.map(size => {
|
|
442
|
+
const stream = data.subarray(offset, offset + size);
|
|
443
|
+
offset += size;
|
|
444
|
+
return stream;
|
|
445
|
+
});
|
|
446
|
+
const bboxBitmapLength = 4 * Math.floor((glyphCount + 31) / 32);
|
|
447
|
+
const bboxBitmap = bboxData.subarray(0, bboxBitmapLength);
|
|
448
|
+
const bboxStream = bboxData.subarray(bboxBitmapLength);
|
|
449
|
+
const overlapBitmap = optionFlags & WOFF2_OVERLAP_BITMAP_FLAG ? data.subarray(offset, offset + ((glyphCount + 7) >> 3)) : null;
|
|
450
|
+
const cursors = { nPoints: 0, flag: 0, glyph: 0, composite: 0, bbox: 0, instruction: 0 };
|
|
451
|
+
const glyphs = [];
|
|
452
|
+
const xMins = [];
|
|
453
|
+
for (let glyphId = 0; glyphId < glyphCount; glyphId++) {
|
|
454
|
+
const contourCount = readInt16(nContourStream, glyphId * 2);
|
|
455
|
+
const explicitBbox = Boolean(bboxBitmap[glyphId >> 3] & (0x80 >> (glyphId & 7)));
|
|
456
|
+
if (contourCount == 0) {
|
|
457
|
+
if (explicitBbox) {
|
|
458
|
+
throw new Error("Invalid empty glyph");
|
|
459
|
+
}
|
|
460
|
+
glyphs.push(new Uint8Array(0));
|
|
461
|
+
xMins.push(0);
|
|
462
|
+
} else if (contourCount > 0) {
|
|
463
|
+
const pointCounts = [];
|
|
464
|
+
let pointCount = 0;
|
|
465
|
+
for (let indexContour = 0; indexContour < contourCount; indexContour++) {
|
|
466
|
+
const count = read255Uint16(nPointsStream, cursors, "nPoints");
|
|
467
|
+
pointCounts.push(count);
|
|
468
|
+
pointCount += count;
|
|
469
|
+
}
|
|
470
|
+
const flags = flagStream.subarray(cursors.flag, cursors.flag + pointCount);
|
|
471
|
+
cursors.flag += pointCount;
|
|
472
|
+
const points = [];
|
|
473
|
+
let x = 0, y = 0;
|
|
474
|
+
for (let indexPoint = 0; indexPoint < pointCount; indexPoint++) {
|
|
475
|
+
const [dx, dy] = readTriplet(flags[indexPoint], glyphStream, cursors);
|
|
476
|
+
x += dx;
|
|
477
|
+
y += dy;
|
|
478
|
+
points.push({ dx, dy, x, y, onCurve: !(flags[indexPoint] & WOFF2_TRIPLET_OFF_CURVE) });
|
|
479
|
+
}
|
|
480
|
+
const instructionLength = read255Uint16(glyphStream, cursors, "glyph");
|
|
481
|
+
const instructions = instructionStream.subarray(cursors.instruction, cursors.instruction + instructionLength);
|
|
482
|
+
cursors.instruction += instructionLength;
|
|
483
|
+
let bbox;
|
|
484
|
+
if (explicitBbox) {
|
|
485
|
+
bbox = readBbox(bboxStream, cursors);
|
|
486
|
+
} else {
|
|
487
|
+
bbox = { xMin: 0, yMin: 0, xMax: 0, yMax: 0 };
|
|
488
|
+
points.forEach(({ x, y }, indexPoint) => {
|
|
489
|
+
if (!indexPoint || x < bbox.xMin) {
|
|
490
|
+
bbox.xMin = x;
|
|
491
|
+
}
|
|
492
|
+
if (!indexPoint || x > bbox.xMax) {
|
|
493
|
+
bbox.xMax = x;
|
|
494
|
+
}
|
|
495
|
+
if (!indexPoint || y < bbox.yMin) {
|
|
496
|
+
bbox.yMin = y;
|
|
497
|
+
}
|
|
498
|
+
if (!indexPoint || y > bbox.yMax) {
|
|
499
|
+
bbox.yMax = y;
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
const overlap = Boolean(overlapBitmap && (overlapBitmap[glyphId >> 3] & (0x80 >> (glyphId & 7))));
|
|
504
|
+
glyphs.push(writeSimpleGlyph(pointCounts, points, instructions, bbox, overlap));
|
|
505
|
+
xMins.push(bbox.xMin);
|
|
506
|
+
} else {
|
|
507
|
+
const componentsOffset = cursors.composite;
|
|
508
|
+
let flags, hasInstructions = false;
|
|
509
|
+
do {
|
|
510
|
+
flags = readUint16(compositeStream, cursors.composite);
|
|
511
|
+
cursors.composite += getComponentLength(flags);
|
|
512
|
+
hasInstructions = hasInstructions || Boolean(flags & COMPOSITE_WE_HAVE_INSTRUCTIONS);
|
|
513
|
+
} while (flags & COMPOSITE_MORE_COMPONENTS);
|
|
514
|
+
const components = compositeStream.subarray(componentsOffset, cursors.composite);
|
|
515
|
+
let instructions = null;
|
|
516
|
+
if (hasInstructions) {
|
|
517
|
+
const instructionLength = read255Uint16(glyphStream, cursors, "glyph");
|
|
518
|
+
instructions = instructionStream.subarray(cursors.instruction, cursors.instruction + instructionLength);
|
|
519
|
+
cursors.instruction += instructionLength;
|
|
520
|
+
}
|
|
521
|
+
if (!explicitBbox) {
|
|
522
|
+
throw new Error("Missing composite glyph bounding box");
|
|
523
|
+
}
|
|
524
|
+
const bbox = readBbox(bboxStream, cursors);
|
|
525
|
+
glyphs.push(writeCompositeGlyph(components, instructions, bbox));
|
|
526
|
+
xMins.push(bbox.xMin);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return { ...assembleGlyf(glyphs), xMins };
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function readBbox(bboxStream, cursors) {
|
|
533
|
+
const bbox = {
|
|
534
|
+
xMin: readInt16(bboxStream, cursors.bbox),
|
|
535
|
+
yMin: readInt16(bboxStream, cursors.bbox + 2),
|
|
536
|
+
xMax: readInt16(bboxStream, cursors.bbox + 4),
|
|
537
|
+
yMax: readInt16(bboxStream, cursors.bbox + 6)
|
|
538
|
+
};
|
|
539
|
+
cursors.bbox += 8;
|
|
540
|
+
return bbox;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
function readTriplet(flag, glyphStream, cursors) {
|
|
544
|
+
const offset = cursors.glyph;
|
|
545
|
+
flag &= 0x7F;
|
|
546
|
+
let dx, dy;
|
|
547
|
+
if (flag < 10) {
|
|
548
|
+
dx = 0;
|
|
549
|
+
dy = withSign(flag, ((flag & 14) << 7) + glyphStream[offset]);
|
|
550
|
+
cursors.glyph += 1;
|
|
551
|
+
} else if (flag < 20) {
|
|
552
|
+
dx = withSign(flag, (((flag - 10) & 14) << 7) + glyphStream[offset]);
|
|
553
|
+
dy = 0;
|
|
554
|
+
cursors.glyph += 1;
|
|
555
|
+
} else if (flag < 84) {
|
|
556
|
+
const b0 = flag - 20;
|
|
557
|
+
const b1 = glyphStream[offset];
|
|
558
|
+
dx = withSign(flag, 1 + (b0 & 0x30) + (b1 >> 4));
|
|
559
|
+
dy = withSign(flag >> 1, 1 + ((b0 & 0x0C) << 2) + (b1 & 0x0F));
|
|
560
|
+
cursors.glyph += 1;
|
|
561
|
+
} else if (flag < 120) {
|
|
562
|
+
const b0 = flag - 84;
|
|
563
|
+
dx = withSign(flag, 1 + (Math.floor(b0 / 12) << 8) + glyphStream[offset]);
|
|
564
|
+
dy = withSign(flag >> 1, 1 + (((b0 % 12) >> 2) << 8) + glyphStream[offset + 1]);
|
|
565
|
+
cursors.glyph += 2;
|
|
566
|
+
} else if (flag < 124) {
|
|
567
|
+
const b2 = glyphStream[offset + 1];
|
|
568
|
+
dx = withSign(flag, (glyphStream[offset] << 4) + (b2 >> 4));
|
|
569
|
+
dy = withSign(flag >> 1, ((b2 & 0x0F) << 8) + glyphStream[offset + 2]);
|
|
570
|
+
cursors.glyph += 3;
|
|
571
|
+
} else {
|
|
572
|
+
dx = withSign(flag, (glyphStream[offset] << 8) + glyphStream[offset + 1]);
|
|
573
|
+
dy = withSign(flag >> 1, (glyphStream[offset + 2] << 8) + glyphStream[offset + 3]);
|
|
574
|
+
cursors.glyph += 4;
|
|
575
|
+
}
|
|
576
|
+
return [dx, dy];
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
function withSign(flag, value) {
|
|
580
|
+
return flag & 1 ? value : -value;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function writeTriplet(dx, dy, onCurve, flagStream, glyphStream) {
|
|
584
|
+
const onCurveFlag = onCurve ? 0 : WOFF2_TRIPLET_OFF_CURVE;
|
|
585
|
+
const xSign = dx < 0 ? 0 : 1;
|
|
586
|
+
const ySign = dy < 0 ? 0 : 1;
|
|
587
|
+
const absDx = Math.abs(dx);
|
|
588
|
+
const absDy = Math.abs(dy);
|
|
589
|
+
if (dx == 0 && absDy < 1280) {
|
|
590
|
+
flagStream.uint8(onCurveFlag + ((absDy & 0xF00) >> 7) + ySign);
|
|
591
|
+
glyphStream.uint8(absDy & 0xFF);
|
|
592
|
+
} else if (dy == 0 && absDx < 1280) {
|
|
593
|
+
flagStream.uint8(onCurveFlag + 10 + ((absDx & 0xF00) >> 7) + xSign);
|
|
594
|
+
glyphStream.uint8(absDx & 0xFF);
|
|
595
|
+
} else if (absDx < 65 && absDy < 65) {
|
|
596
|
+
flagStream.uint8(onCurveFlag + 20 + ((absDx - 1) & 0x30) + (((absDy - 1) & 0x30) >> 2) + xSign + (ySign << 1));
|
|
597
|
+
glyphStream.uint8((((absDx - 1) & 0x0F) << 4) | ((absDy - 1) & 0x0F));
|
|
598
|
+
} else if (absDx < 769 && absDy < 769) {
|
|
599
|
+
flagStream.uint8(onCurveFlag + 84 + (12 * (((absDx - 1) & 0x300) >> 8)) + (((absDy - 1) & 0x300) >> 6) + xSign + (ySign << 1));
|
|
600
|
+
glyphStream.uint8((absDx - 1) & 0xFF);
|
|
601
|
+
glyphStream.uint8((absDy - 1) & 0xFF);
|
|
602
|
+
} else if (absDx < 4096 && absDy < 4096) {
|
|
603
|
+
flagStream.uint8(onCurveFlag + 120 + xSign + (ySign << 1));
|
|
604
|
+
glyphStream.uint8(absDx >> 4);
|
|
605
|
+
glyphStream.uint8(((absDx & 0x0F) << 4) | (absDy >> 8));
|
|
606
|
+
glyphStream.uint8(absDy & 0xFF);
|
|
607
|
+
} else {
|
|
608
|
+
flagStream.uint8(onCurveFlag + 124 + xSign + (ySign << 1));
|
|
609
|
+
glyphStream.uint8(absDx >> 8);
|
|
610
|
+
glyphStream.uint8(absDx & 0xFF);
|
|
611
|
+
glyphStream.uint8(absDy >> 8);
|
|
612
|
+
glyphStream.uint8(absDy & 0xFF);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
function writeSimpleGlyph(pointCounts, points, instructions, bbox, overlap) {
|
|
617
|
+
const writer = new ByteWriter();
|
|
618
|
+
writer.int16(pointCounts.length);
|
|
619
|
+
writer.int16(bbox.xMin);
|
|
620
|
+
writer.int16(bbox.yMin);
|
|
621
|
+
writer.int16(bbox.xMax);
|
|
622
|
+
writer.int16(bbox.yMax);
|
|
623
|
+
let endPoint = -1;
|
|
624
|
+
pointCounts.forEach(count => {
|
|
625
|
+
endPoint += count;
|
|
626
|
+
writer.uint16(endPoint);
|
|
627
|
+
});
|
|
628
|
+
writer.uint16(instructions.length);
|
|
629
|
+
writer.bytes(instructions);
|
|
630
|
+
points.forEach(({ dx, dy, onCurve }, indexPoint) => {
|
|
631
|
+
let flag = onCurve ? GLYPH_FLAG_ON_CURVE : 0;
|
|
632
|
+
if (!indexPoint && overlap) {
|
|
633
|
+
flag |= GLYPH_FLAG_OVERLAP_SIMPLE;
|
|
634
|
+
}
|
|
635
|
+
if (dx == 0) {
|
|
636
|
+
flag |= GLYPH_FLAG_X_SAME_OR_POSITIVE;
|
|
637
|
+
} else if (dx > -256 && dx < 256) {
|
|
638
|
+
flag |= GLYPH_FLAG_X_SHORT | (dx > 0 ? GLYPH_FLAG_X_SAME_OR_POSITIVE : 0);
|
|
639
|
+
}
|
|
640
|
+
if (dy == 0) {
|
|
641
|
+
flag |= GLYPH_FLAG_Y_SAME_OR_POSITIVE;
|
|
642
|
+
} else if (dy > -256 && dy < 256) {
|
|
643
|
+
flag |= GLYPH_FLAG_Y_SHORT | (dy > 0 ? GLYPH_FLAG_Y_SAME_OR_POSITIVE : 0);
|
|
644
|
+
}
|
|
645
|
+
writer.uint8(flag);
|
|
646
|
+
});
|
|
647
|
+
points.forEach(({ dx }) => {
|
|
648
|
+
if (dx != 0) {
|
|
649
|
+
if (dx > -256 && dx < 256) {
|
|
650
|
+
writer.uint8(Math.abs(dx));
|
|
651
|
+
} else {
|
|
652
|
+
writer.int16(dx);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
});
|
|
656
|
+
points.forEach(({ dy }) => {
|
|
657
|
+
if (dy != 0) {
|
|
658
|
+
if (dy > -256 && dy < 256) {
|
|
659
|
+
writer.uint8(Math.abs(dy));
|
|
660
|
+
} else {
|
|
661
|
+
writer.int16(dy);
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
return writer.toUint8Array();
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
function writeCompositeGlyph(components, instructions, bbox) {
|
|
669
|
+
const writer = new ByteWriter();
|
|
670
|
+
writer.int16(-1);
|
|
671
|
+
writer.int16(bbox.xMin);
|
|
672
|
+
writer.int16(bbox.yMin);
|
|
673
|
+
writer.int16(bbox.xMax);
|
|
674
|
+
writer.int16(bbox.yMax);
|
|
675
|
+
writer.bytes(components);
|
|
676
|
+
if (instructions) {
|
|
677
|
+
writer.uint16(instructions.length);
|
|
678
|
+
writer.bytes(instructions);
|
|
679
|
+
}
|
|
680
|
+
return writer.toUint8Array();
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
function getComponentLength(flags) {
|
|
684
|
+
let length = flags & COMPOSITE_ARG_1_AND_2_ARE_WORDS ? 8 : 6;
|
|
685
|
+
if (flags & COMPOSITE_WE_HAVE_A_SCALE) {
|
|
686
|
+
length += 2;
|
|
687
|
+
} else if (flags & COMPOSITE_WE_HAVE_AN_X_AND_Y_SCALE) {
|
|
688
|
+
length += 4;
|
|
689
|
+
} else if (flags & COMPOSITE_WE_HAVE_A_TWO_BY_TWO) {
|
|
690
|
+
length += 8;
|
|
691
|
+
}
|
|
692
|
+
return length;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
function assembleGlyf(glyphs) {
|
|
696
|
+
const glyphCount = glyphs.length;
|
|
697
|
+
const glyfTable = new Uint8Array(glyphs.reduce((length, glyphData) => length + align(glyphData.length), 0));
|
|
698
|
+
const locaTable = new Uint8Array((glyphCount + 1) * 4);
|
|
699
|
+
let offset = 0;
|
|
700
|
+
glyphs.forEach((glyphData, glyphId) => {
|
|
701
|
+
writeUint32(locaTable, glyphId * 4, offset);
|
|
702
|
+
glyfTable.set(glyphData, offset);
|
|
703
|
+
offset += align(glyphData.length);
|
|
704
|
+
});
|
|
705
|
+
writeUint32(locaTable, glyphCount * 4, offset);
|
|
706
|
+
return { glyfTable, locaTable };
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
function reconstructHmtx(data, xMins, metricsCount, glyphCount) {
|
|
710
|
+
const flags = data[0];
|
|
711
|
+
if (!(flags & (WOFF2_HMTX_NO_LSB_FLAG | WOFF2_HMTX_NO_LEFT_SIDE_BEARING_FLAG)) || metricsCount > glyphCount) {
|
|
712
|
+
throw new Error("Invalid transformed hmtx table");
|
|
713
|
+
}
|
|
714
|
+
const hmtx = new Uint8Array(metricsCount * 4 + (glyphCount - metricsCount) * 2);
|
|
715
|
+
let offset = 1;
|
|
716
|
+
for (let glyphId = 0; glyphId < metricsCount; glyphId++) {
|
|
717
|
+
writeUint16(hmtx, glyphId * 4, readUint16(data, offset));
|
|
718
|
+
offset += 2;
|
|
719
|
+
}
|
|
720
|
+
for (let glyphId = 0; glyphId < metricsCount; glyphId++) {
|
|
721
|
+
if (flags & WOFF2_HMTX_NO_LSB_FLAG) {
|
|
722
|
+
writeInt16(hmtx, glyphId * 4 + 2, xMins[glyphId]);
|
|
723
|
+
} else {
|
|
724
|
+
writeInt16(hmtx, glyphId * 4 + 2, readInt16(data, offset));
|
|
725
|
+
offset += 2;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
for (let glyphId = metricsCount; glyphId < glyphCount; glyphId++) {
|
|
729
|
+
const lsbOffset = metricsCount * 4 + (glyphId - metricsCount) * 2;
|
|
730
|
+
if (flags & WOFF2_HMTX_NO_LEFT_SIDE_BEARING_FLAG) {
|
|
731
|
+
writeInt16(hmtx, lsbOffset, xMins[glyphId]);
|
|
732
|
+
} else {
|
|
733
|
+
writeInt16(hmtx, lsbOffset, readInt16(data, offset));
|
|
734
|
+
offset += 2;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
return hmtx;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
function transformGlyf(glyf, locaOffsets, glyphCount) {
|
|
741
|
+
const nContourStream = new ByteWriter();
|
|
742
|
+
const nPointsStream = new ByteWriter();
|
|
743
|
+
const flagStream = new ByteWriter();
|
|
744
|
+
const glyphStream = new ByteWriter();
|
|
745
|
+
const compositeStream = new ByteWriter();
|
|
746
|
+
const bboxStream = new ByteWriter();
|
|
747
|
+
const instructionStream = new ByteWriter();
|
|
748
|
+
const bboxBitmap = new Uint8Array(4 * Math.floor((glyphCount + 31) / 32));
|
|
749
|
+
const overlapBitmap = new Uint8Array((glyphCount + 7) >> 3);
|
|
750
|
+
let hasOverlap = false;
|
|
751
|
+
for (let glyphId = 0; glyphId < glyphCount; glyphId++) {
|
|
752
|
+
const glyphOffset = locaOffsets[glyphId];
|
|
753
|
+
const glyphEnd = locaOffsets[glyphId + 1];
|
|
754
|
+
const contourCount = glyphEnd - glyphOffset >= GLYPH_HEADER_LENGTH ? readInt16(glyf, glyphOffset) : 0;
|
|
755
|
+
if (contourCount == 0) {
|
|
756
|
+
nContourStream.int16(0);
|
|
757
|
+
} else if (contourCount > 0) {
|
|
758
|
+
nContourStream.int16(contourCount);
|
|
759
|
+
let offset = glyphOffset + GLYPH_HEADER_LENGTH;
|
|
760
|
+
let pointCount = 0;
|
|
761
|
+
for (let indexContour = 0; indexContour < contourCount; indexContour++) {
|
|
762
|
+
const endPoint = readUint16(glyf, offset);
|
|
763
|
+
write255Uint16(nPointsStream, endPoint + 1 - pointCount);
|
|
764
|
+
pointCount = endPoint + 1;
|
|
765
|
+
offset += 2;
|
|
766
|
+
}
|
|
767
|
+
const instructionLength = readUint16(glyf, offset);
|
|
768
|
+
offset += 2;
|
|
769
|
+
const instructions = glyf.subarray(offset, offset + instructionLength);
|
|
770
|
+
offset += instructionLength;
|
|
771
|
+
const flags = [];
|
|
772
|
+
while (flags.length < pointCount) {
|
|
773
|
+
const flag = glyf[offset++];
|
|
774
|
+
flags.push(flag);
|
|
775
|
+
if (flag & GLYPH_FLAG_REPEAT) {
|
|
776
|
+
let repeatCount = glyf[offset++];
|
|
777
|
+
while (repeatCount-- > 0 && flags.length < pointCount) {
|
|
778
|
+
flags.push(flag);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
const deltas = [];
|
|
783
|
+
flags.forEach(flag => {
|
|
784
|
+
let dx = 0;
|
|
785
|
+
if (flag & GLYPH_FLAG_X_SHORT) {
|
|
786
|
+
dx = flag & GLYPH_FLAG_X_SAME_OR_POSITIVE ? glyf[offset] : -glyf[offset];
|
|
787
|
+
offset += 1;
|
|
788
|
+
} else if (!(flag & GLYPH_FLAG_X_SAME_OR_POSITIVE)) {
|
|
789
|
+
dx = readInt16(glyf, offset);
|
|
790
|
+
offset += 2;
|
|
791
|
+
}
|
|
792
|
+
deltas.push({ dx });
|
|
793
|
+
});
|
|
794
|
+
flags.forEach((flag, indexPoint) => {
|
|
795
|
+
let dy = 0;
|
|
796
|
+
if (flag & GLYPH_FLAG_Y_SHORT) {
|
|
797
|
+
dy = flag & GLYPH_FLAG_Y_SAME_OR_POSITIVE ? glyf[offset] : -glyf[offset];
|
|
798
|
+
offset += 1;
|
|
799
|
+
} else if (!(flag & GLYPH_FLAG_Y_SAME_OR_POSITIVE)) {
|
|
800
|
+
dy = readInt16(glyf, offset);
|
|
801
|
+
offset += 2;
|
|
802
|
+
}
|
|
803
|
+
deltas[indexPoint].dy = dy;
|
|
804
|
+
});
|
|
805
|
+
const bbox = { xMin: 0, yMin: 0, xMax: 0, yMax: 0 };
|
|
806
|
+
let x = 0, y = 0;
|
|
807
|
+
deltas.forEach(({ dx, dy }, indexPoint) => {
|
|
808
|
+
x += dx;
|
|
809
|
+
y += dy;
|
|
810
|
+
if (!indexPoint || x < bbox.xMin) {
|
|
811
|
+
bbox.xMin = x;
|
|
812
|
+
}
|
|
813
|
+
if (!indexPoint || x > bbox.xMax) {
|
|
814
|
+
bbox.xMax = x;
|
|
815
|
+
}
|
|
816
|
+
if (!indexPoint || y < bbox.yMin) {
|
|
817
|
+
bbox.yMin = y;
|
|
818
|
+
}
|
|
819
|
+
if (!indexPoint || y > bbox.yMax) {
|
|
820
|
+
bbox.yMax = y;
|
|
821
|
+
}
|
|
822
|
+
writeTriplet(dx, dy, Boolean(flags[indexPoint] & GLYPH_FLAG_ON_CURVE), flagStream, glyphStream);
|
|
823
|
+
});
|
|
824
|
+
write255Uint16(glyphStream, instructionLength);
|
|
825
|
+
instructionStream.bytes(instructions);
|
|
826
|
+
if (readInt16(glyf, glyphOffset + 2) != bbox.xMin || readInt16(glyf, glyphOffset + 4) != bbox.yMin || readInt16(glyf, glyphOffset + 6) != bbox.xMax || readInt16(glyf, glyphOffset + 8) != bbox.yMax) {
|
|
827
|
+
bboxBitmap[glyphId >> 3] |= 0x80 >> (glyphId & 7);
|
|
828
|
+
bboxStream.bytes(glyf.subarray(glyphOffset + 2, glyphOffset + GLYPH_HEADER_LENGTH));
|
|
829
|
+
}
|
|
830
|
+
if (flags.length && (flags[0] & GLYPH_FLAG_OVERLAP_SIMPLE)) {
|
|
831
|
+
overlapBitmap[glyphId >> 3] |= 0x80 >> (glyphId & 7);
|
|
832
|
+
hasOverlap = true;
|
|
833
|
+
}
|
|
834
|
+
} else {
|
|
835
|
+
nContourStream.int16(-1);
|
|
836
|
+
bboxBitmap[glyphId >> 3] |= 0x80 >> (glyphId & 7);
|
|
837
|
+
bboxStream.bytes(glyf.subarray(glyphOffset + 2, glyphOffset + GLYPH_HEADER_LENGTH));
|
|
838
|
+
let offset = glyphOffset + GLYPH_HEADER_LENGTH;
|
|
839
|
+
let flags, hasInstructions = false;
|
|
840
|
+
do {
|
|
841
|
+
flags = readUint16(glyf, offset);
|
|
842
|
+
offset += getComponentLength(flags);
|
|
843
|
+
hasInstructions = hasInstructions || Boolean(flags & COMPOSITE_WE_HAVE_INSTRUCTIONS);
|
|
844
|
+
} while ((flags & COMPOSITE_MORE_COMPONENTS) && offset < glyphEnd);
|
|
845
|
+
compositeStream.bytes(glyf.subarray(glyphOffset + GLYPH_HEADER_LENGTH, offset));
|
|
846
|
+
if (hasInstructions) {
|
|
847
|
+
const instructionLength = readUint16(glyf, offset);
|
|
848
|
+
write255Uint16(glyphStream, instructionLength);
|
|
849
|
+
instructionStream.bytes(glyf.subarray(offset + 2, offset + 2 + instructionLength));
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
const streams = [nContourStream, nPointsStream, flagStream, glyphStream, compositeStream, null, instructionStream].map(stream => stream ? stream.toUint8Array() : concat([bboxBitmap, bboxStream.toUint8Array()]));
|
|
854
|
+
const writer = new ByteWriter();
|
|
855
|
+
writer.uint16(0);
|
|
856
|
+
writer.uint16(hasOverlap ? WOFF2_OVERLAP_BITMAP_FLAG : 0);
|
|
857
|
+
writer.uint16(glyphCount);
|
|
858
|
+
writer.uint16(LONG_LOCA_FORMAT);
|
|
859
|
+
streams.forEach(stream => writer.uint32(stream.length));
|
|
860
|
+
streams.forEach(stream => writer.bytes(stream));
|
|
861
|
+
if (hasOverlap) {
|
|
862
|
+
writer.bytes(overlapBitmap);
|
|
863
|
+
}
|
|
864
|
+
return writer.toUint8Array();
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
function concat(arrays) {
|
|
868
|
+
const result = new Uint8Array(arrays.reduce((length, array) => length + array.length, 0));
|
|
869
|
+
let offset = 0;
|
|
870
|
+
arrays.forEach(array => {
|
|
871
|
+
result.set(array, offset);
|
|
872
|
+
offset += array.length;
|
|
873
|
+
});
|
|
874
|
+
return result;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
function read255Uint16(data, cursors, name) {
|
|
878
|
+
const code = data[cursors[name]++];
|
|
879
|
+
if (code == WOFF2_WORD_CODE) {
|
|
880
|
+
const value = readUint16(data, cursors[name]);
|
|
881
|
+
cursors[name] += 2;
|
|
882
|
+
return value;
|
|
883
|
+
} else if (code == WOFF2_ONE_MORE_BYTE_CODE_1) {
|
|
884
|
+
return data[cursors[name]++] + WOFF2_LOWEST_U_CODE;
|
|
885
|
+
} else if (code == WOFF2_ONE_MORE_BYTE_CODE_2) {
|
|
886
|
+
return data[cursors[name]++] + WOFF2_LOWEST_U_CODE * 2;
|
|
887
|
+
} else {
|
|
888
|
+
return code;
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
function write255Uint16(writer, value) {
|
|
893
|
+
if (value < WOFF2_LOWEST_U_CODE) {
|
|
894
|
+
writer.uint8(value);
|
|
895
|
+
} else if (value < WOFF2_LOWEST_U_CODE * 2) {
|
|
896
|
+
writer.uint8(WOFF2_ONE_MORE_BYTE_CODE_1);
|
|
897
|
+
writer.uint8(value - WOFF2_LOWEST_U_CODE);
|
|
898
|
+
} else if (value < WOFF2_LOWEST_U_CODE * 3) {
|
|
899
|
+
writer.uint8(WOFF2_ONE_MORE_BYTE_CODE_2);
|
|
900
|
+
writer.uint8(value - WOFF2_LOWEST_U_CODE * 2);
|
|
901
|
+
} else {
|
|
902
|
+
writer.uint8(WOFF2_WORD_CODE);
|
|
903
|
+
writer.uint16(value);
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
function readBase128(data, offset) {
|
|
908
|
+
let value = 0;
|
|
909
|
+
for (let indexByte = 0; indexByte < 5; indexByte++) {
|
|
910
|
+
const byte = data[offset++];
|
|
911
|
+
if ((!indexByte && byte == 0x80) || value >= 0x2000000) {
|
|
912
|
+
throw new Error("Invalid UIntBase128");
|
|
913
|
+
}
|
|
914
|
+
value = value * 128 + (byte & 0x7F);
|
|
915
|
+
if (!(byte & 0x80)) {
|
|
916
|
+
return [value, offset];
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
throw new Error("Invalid UIntBase128");
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
function writeBase128(writer, value) {
|
|
923
|
+
const bytes = [];
|
|
924
|
+
do {
|
|
925
|
+
bytes.unshift(value & 0x7F);
|
|
926
|
+
value = Math.floor(value / 128);
|
|
927
|
+
} while (value > 0);
|
|
928
|
+
bytes.forEach((byte, indexByte) => writer.uint8(indexByte < bytes.length - 1 ? byte | 0x80 : byte));
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
class ByteWriter {
|
|
932
|
+
constructor() {
|
|
933
|
+
this.data = new Uint8Array(256);
|
|
934
|
+
this.length = 0;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
ensure(count) {
|
|
938
|
+
if (this.length + count > this.data.length) {
|
|
939
|
+
let size = this.data.length * 2;
|
|
940
|
+
while (size < this.length + count) {
|
|
941
|
+
size *= 2;
|
|
942
|
+
}
|
|
943
|
+
const data = new Uint8Array(size);
|
|
944
|
+
data.set(this.data.subarray(0, this.length));
|
|
945
|
+
this.data = data;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
uint8(value) {
|
|
950
|
+
this.ensure(1);
|
|
951
|
+
this.data[this.length++] = value & 0xFF;
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
uint16(value) {
|
|
955
|
+
this.ensure(2);
|
|
956
|
+
writeUint16(this.data, this.length, value);
|
|
957
|
+
this.length += 2;
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
int16(value) {
|
|
961
|
+
this.uint16(value & 0xFFFF);
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
uint32(value) {
|
|
965
|
+
this.ensure(4);
|
|
966
|
+
writeUint32(this.data, this.length, value);
|
|
967
|
+
this.length += 4;
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
bytes(data) {
|
|
971
|
+
this.ensure(data.length);
|
|
972
|
+
this.data.set(data, this.length);
|
|
973
|
+
this.length += data.length;
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
toUint8Array() {
|
|
977
|
+
return this.data.subarray(0, this.length);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
function readLoca(loca, glyphCount, locaFormat) {
|
|
982
|
+
const offsets = [];
|
|
983
|
+
if (locaFormat == LONG_LOCA_FORMAT) {
|
|
984
|
+
if (loca.length < (glyphCount + 1) * 4) {
|
|
985
|
+
return null;
|
|
986
|
+
}
|
|
987
|
+
for (let glyphId = 0; glyphId <= glyphCount; glyphId++) {
|
|
988
|
+
offsets.push(readUint32(loca, glyphId * 4));
|
|
989
|
+
}
|
|
990
|
+
} else {
|
|
991
|
+
if (loca.length < (glyphCount + 1) * 2) {
|
|
992
|
+
return null;
|
|
993
|
+
}
|
|
994
|
+
for (let glyphId = 0; glyphId <= glyphCount; glyphId++) {
|
|
995
|
+
offsets.push(readUint16(loca, glyphId * 2) * 2);
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
return offsets;
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
function readCmap(cmap) {
|
|
1002
|
+
const mapping = new Map();
|
|
1003
|
+
const variationGlyphIds = new Map();
|
|
1004
|
+
let variationSubtable = null;
|
|
1005
|
+
const subtableCount = readUint16(cmap, 2);
|
|
1006
|
+
for (let indexSubtable = 0; indexSubtable < subtableCount; indexSubtable++) {
|
|
1007
|
+
const recordOffset = 4 + indexSubtable * 8;
|
|
1008
|
+
const platformId = readUint16(cmap, recordOffset);
|
|
1009
|
+
const encodingId = readUint16(cmap, recordOffset + 2);
|
|
1010
|
+
const offset = readUint32(cmap, recordOffset + 4);
|
|
1011
|
+
const format = readUint16(cmap, offset);
|
|
1012
|
+
if (format == CMAP_FORMAT_VARIATION) {
|
|
1013
|
+
if (platformId == CMAP_PLATFORM_UNICODE && encodingId == CMAP_ENCODING_UNICODE_VARIATION) {
|
|
1014
|
+
const length = readUint32(cmap, offset + 2);
|
|
1015
|
+
variationSubtable = cmap.subarray(offset, offset + length);
|
|
1016
|
+
readVariationGlyphIds(cmap, offset, variationGlyphIds);
|
|
1017
|
+
}
|
|
1018
|
+
} else if (platformId == CMAP_PLATFORM_UNICODE || (platformId == CMAP_PLATFORM_WINDOWS && (encodingId == CMAP_ENCODING_WINDOWS_BMP || encodingId == CMAP_ENCODING_WINDOWS_FULL))) {
|
|
1019
|
+
readCmapSubtable(cmap, offset, format, mapping);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
return { mapping, variationGlyphIds, variationSubtable };
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
function readCmapSubtable(cmap, offset, format, mapping) {
|
|
1026
|
+
const setMapping = (charCode, glyphId) => {
|
|
1027
|
+
if (glyphId && !mapping.has(charCode)) {
|
|
1028
|
+
mapping.set(charCode, glyphId);
|
|
1029
|
+
}
|
|
1030
|
+
};
|
|
1031
|
+
if (format == CMAP_FORMAT_BYTE) {
|
|
1032
|
+
for (let charCode = 0; charCode < 256; charCode++) {
|
|
1033
|
+
setMapping(charCode, cmap[offset + 6 + charCode]);
|
|
1034
|
+
}
|
|
1035
|
+
} else if (format == CMAP_FORMAT_SEGMENT) {
|
|
1036
|
+
const segmentCount = readUint16(cmap, offset + 6) / 2;
|
|
1037
|
+
const endCodesOffset = offset + 14;
|
|
1038
|
+
const startCodesOffset = endCodesOffset + segmentCount * 2 + 2;
|
|
1039
|
+
const deltasOffset = startCodesOffset + segmentCount * 2;
|
|
1040
|
+
const rangeOffsetsOffset = deltasOffset + segmentCount * 2;
|
|
1041
|
+
for (let indexSegment = 0; indexSegment < segmentCount; indexSegment++) {
|
|
1042
|
+
const endCode = readUint16(cmap, endCodesOffset + indexSegment * 2);
|
|
1043
|
+
const startCode = readUint16(cmap, startCodesOffset + indexSegment * 2);
|
|
1044
|
+
const delta = readUint16(cmap, deltasOffset + indexSegment * 2);
|
|
1045
|
+
const rangeOffsetOffset = rangeOffsetsOffset + indexSegment * 2;
|
|
1046
|
+
const rangeOffset = readUint16(cmap, rangeOffsetOffset);
|
|
1047
|
+
for (let charCode = startCode; charCode <= endCode && charCode != MAX_BMP_CHAR_CODE; charCode++) {
|
|
1048
|
+
if (rangeOffset) {
|
|
1049
|
+
const glyphOffset = rangeOffsetOffset + rangeOffset + (charCode - startCode) * 2;
|
|
1050
|
+
if (glyphOffset + 1 < cmap.length) {
|
|
1051
|
+
const glyphId = readUint16(cmap, glyphOffset);
|
|
1052
|
+
setMapping(charCode, glyphId ? (glyphId + delta) & 0xFFFF : 0);
|
|
1053
|
+
}
|
|
1054
|
+
} else {
|
|
1055
|
+
setMapping(charCode, (charCode + delta) & 0xFFFF);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
} else if (format == CMAP_FORMAT_TRIMMED) {
|
|
1060
|
+
const firstCode = readUint16(cmap, offset + 6);
|
|
1061
|
+
const entryCount = readUint16(cmap, offset + 8);
|
|
1062
|
+
for (let indexEntry = 0; indexEntry < entryCount; indexEntry++) {
|
|
1063
|
+
setMapping(firstCode + indexEntry, readUint16(cmap, offset + 10 + indexEntry * 2));
|
|
1064
|
+
}
|
|
1065
|
+
} else if (format == CMAP_FORMAT_GROUPS) {
|
|
1066
|
+
const groupCount = readUint32(cmap, offset + 12);
|
|
1067
|
+
for (let indexGroup = 0; indexGroup < groupCount; indexGroup++) {
|
|
1068
|
+
const groupOffset = offset + 16 + indexGroup * 12;
|
|
1069
|
+
const startCharCode = readUint32(cmap, groupOffset);
|
|
1070
|
+
const endCharCode = readUint32(cmap, groupOffset + 4);
|
|
1071
|
+
const startGlyphId = readUint32(cmap, groupOffset + 8);
|
|
1072
|
+
for (let charCode = startCharCode; charCode <= endCharCode; charCode++) {
|
|
1073
|
+
setMapping(charCode, startGlyphId + charCode - startCharCode);
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
function readVariationGlyphIds(cmap, offset, glyphIds) {
|
|
1080
|
+
const recordCount = readUint32(cmap, offset + 6);
|
|
1081
|
+
for (let indexRecord = 0; indexRecord < recordCount; indexRecord++) {
|
|
1082
|
+
const recordOffset = offset + 10 + indexRecord * 11;
|
|
1083
|
+
const nonDefaultOffset = readUint32(cmap, recordOffset + 7);
|
|
1084
|
+
if (nonDefaultOffset) {
|
|
1085
|
+
const mappingsOffset = offset + nonDefaultOffset;
|
|
1086
|
+
const mappingCount = readUint32(cmap, mappingsOffset);
|
|
1087
|
+
for (let indexMapping = 0; indexMapping < mappingCount; indexMapping++) {
|
|
1088
|
+
const mappingOffset = mappingsOffset + 4 + indexMapping * 5;
|
|
1089
|
+
const charCode = (cmap[mappingOffset] << 16) | (cmap[mappingOffset + 1] << 8) | cmap[mappingOffset + 2];
|
|
1090
|
+
if (!glyphIds.has(charCode)) {
|
|
1091
|
+
glyphIds.set(charCode, new Set());
|
|
1092
|
+
}
|
|
1093
|
+
glyphIds.get(charCode).add(readUint16(cmap, mappingOffset + 3));
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
function addGsubClosure(gsub, keptGlyphIds) {
|
|
1100
|
+
const rules = [];
|
|
1101
|
+
const lookupListOffset = readUint16(gsub, 8);
|
|
1102
|
+
const lookupCount = readUint16(gsub, lookupListOffset);
|
|
1103
|
+
for (let indexLookup = 0; indexLookup < lookupCount; indexLookup++) {
|
|
1104
|
+
const lookupOffset = lookupListOffset + readUint16(gsub, lookupListOffset + 2 + indexLookup * 2);
|
|
1105
|
+
const lookupType = readUint16(gsub, lookupOffset);
|
|
1106
|
+
const subtableCount = readUint16(gsub, lookupOffset + 4);
|
|
1107
|
+
for (let indexSubtable = 0; indexSubtable < subtableCount; indexSubtable++) {
|
|
1108
|
+
readGsubRules(gsub, lookupType, lookupOffset + readUint16(gsub, lookupOffset + 6 + indexSubtable * 2), rules);
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
let added;
|
|
1112
|
+
do {
|
|
1113
|
+
added = false;
|
|
1114
|
+
rules.forEach(rule => {
|
|
1115
|
+
if (!rule.done && rule.inputs.every(glyphId => keptGlyphIds.has(glyphId))) {
|
|
1116
|
+
rule.done = true;
|
|
1117
|
+
rule.outputs.forEach(glyphId => {
|
|
1118
|
+
if (!keptGlyphIds.has(glyphId)) {
|
|
1119
|
+
keptGlyphIds.add(glyphId);
|
|
1120
|
+
added = true;
|
|
1121
|
+
}
|
|
1122
|
+
});
|
|
1123
|
+
}
|
|
1124
|
+
});
|
|
1125
|
+
} while (added);
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
function readGsubRules(gsub, lookupType, offset, rules) {
|
|
1129
|
+
const format = readUint16(gsub, offset);
|
|
1130
|
+
if (lookupType == GSUB_LOOKUP_EXTENSION) {
|
|
1131
|
+
readGsubRules(gsub, readUint16(gsub, offset + 2), offset + readUint32(gsub, offset + 4), rules);
|
|
1132
|
+
} else if (lookupType == GSUB_LOOKUP_SINGLE) {
|
|
1133
|
+
const coverage = readCoverage(gsub, offset + readUint16(gsub, offset + 2));
|
|
1134
|
+
if (format == 1) {
|
|
1135
|
+
const delta = readInt16(gsub, offset + 4);
|
|
1136
|
+
coverage.forEach(glyphId => rules.push({ inputs: [glyphId], outputs: [(glyphId + delta) & 0xFFFF] }));
|
|
1137
|
+
} else {
|
|
1138
|
+
const substitutes = readGlyphArray(gsub, offset + 4);
|
|
1139
|
+
coverage.forEach((glyphId, indexGlyph) => rules.push({ inputs: [glyphId], outputs: substitutes.slice(indexGlyph, indexGlyph + 1) }));
|
|
1140
|
+
}
|
|
1141
|
+
} else if (lookupType == GSUB_LOOKUP_MULTIPLE || lookupType == GSUB_LOOKUP_ALTERNATE) {
|
|
1142
|
+
const coverage = readCoverage(gsub, offset + readUint16(gsub, offset + 2));
|
|
1143
|
+
const setCount = readUint16(gsub, offset + 4);
|
|
1144
|
+
for (let indexSet = 0; indexSet < setCount && indexSet < coverage.length; indexSet++) {
|
|
1145
|
+
rules.push({ inputs: [coverage[indexSet]], outputs: readGlyphArray(gsub, offset + readUint16(gsub, offset + 6 + indexSet * 2)) });
|
|
1146
|
+
}
|
|
1147
|
+
} else if (lookupType == GSUB_LOOKUP_LIGATURE) {
|
|
1148
|
+
const coverage = readCoverage(gsub, offset + readUint16(gsub, offset + 2));
|
|
1149
|
+
const setCount = readUint16(gsub, offset + 4);
|
|
1150
|
+
for (let indexSet = 0; indexSet < setCount && indexSet < coverage.length; indexSet++) {
|
|
1151
|
+
const setOffset = offset + readUint16(gsub, offset + 6 + indexSet * 2);
|
|
1152
|
+
const ligatureCount = readUint16(gsub, setOffset);
|
|
1153
|
+
for (let indexLigature = 0; indexLigature < ligatureCount; indexLigature++) {
|
|
1154
|
+
const ligatureOffset = setOffset + readUint16(gsub, setOffset + 2 + indexLigature * 2);
|
|
1155
|
+
const componentCount = readUint16(gsub, ligatureOffset + 2);
|
|
1156
|
+
const inputs = [coverage[indexSet]];
|
|
1157
|
+
for (let indexComponent = 1; indexComponent < componentCount; indexComponent++) {
|
|
1158
|
+
inputs.push(readUint16(gsub, ligatureOffset + 4 + (indexComponent - 1) * 2));
|
|
1159
|
+
}
|
|
1160
|
+
rules.push({ inputs, outputs: [readUint16(gsub, ligatureOffset)] });
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
} else if (lookupType == GSUB_LOOKUP_REVERSE_CHAINING) {
|
|
1164
|
+
const coverage = readCoverage(gsub, offset + readUint16(gsub, offset + 2));
|
|
1165
|
+
const backtrackCount = readUint16(gsub, offset + 4);
|
|
1166
|
+
const lookaheadOffset = offset + 6 + backtrackCount * 2;
|
|
1167
|
+
const lookaheadCount = readUint16(gsub, lookaheadOffset);
|
|
1168
|
+
const substitutes = readGlyphArray(gsub, lookaheadOffset + 2 + lookaheadCount * 2);
|
|
1169
|
+
coverage.forEach((glyphId, indexGlyph) => rules.push({ inputs: [glyphId], outputs: substitutes.slice(indexGlyph, indexGlyph + 1) }));
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
function readGlyphArray(data, offset) {
|
|
1174
|
+
const glyphIds = [];
|
|
1175
|
+
const glyphCount = readUint16(data, offset);
|
|
1176
|
+
for (let indexGlyph = 0; indexGlyph < glyphCount; indexGlyph++) {
|
|
1177
|
+
glyphIds.push(readUint16(data, offset + 2 + indexGlyph * 2));
|
|
1178
|
+
}
|
|
1179
|
+
return glyphIds;
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
function readCoverage(data, offset) {
|
|
1183
|
+
const glyphIds = [];
|
|
1184
|
+
const format = readUint16(data, offset);
|
|
1185
|
+
if (format == 1) {
|
|
1186
|
+
const glyphCount = readUint16(data, offset + 2);
|
|
1187
|
+
for (let indexGlyph = 0; indexGlyph < glyphCount; indexGlyph++) {
|
|
1188
|
+
glyphIds.push(readUint16(data, offset + 4 + indexGlyph * 2));
|
|
1189
|
+
}
|
|
1190
|
+
} else if (format == 2) {
|
|
1191
|
+
const rangeCount = readUint16(data, offset + 2);
|
|
1192
|
+
for (let indexRange = 0; indexRange < rangeCount; indexRange++) {
|
|
1193
|
+
const rangeOffset = offset + 4 + indexRange * 6;
|
|
1194
|
+
const startGlyphId = readUint16(data, rangeOffset);
|
|
1195
|
+
const endGlyphId = readUint16(data, rangeOffset + 2);
|
|
1196
|
+
for (let glyphId = startGlyphId; glyphId <= endGlyphId; glyphId++) {
|
|
1197
|
+
glyphIds.push(glyphId);
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
return glyphIds;
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
function addCompositeComponents(glyf, locaOffsets, glyphId, keptGlyphIds) {
|
|
1205
|
+
const glyphOffset = locaOffsets[glyphId];
|
|
1206
|
+
const glyphEnd = locaOffsets[glyphId + 1];
|
|
1207
|
+
if (glyphEnd - glyphOffset < 10 || readInt16(glyf, glyphOffset) >= 0) {
|
|
1208
|
+
return;
|
|
1209
|
+
}
|
|
1210
|
+
let offset = glyphOffset + 10;
|
|
1211
|
+
let flags;
|
|
1212
|
+
do {
|
|
1213
|
+
flags = readUint16(glyf, offset);
|
|
1214
|
+
const componentGlyphId = readUint16(glyf, offset + 2);
|
|
1215
|
+
offset += (flags & COMPOSITE_ARG_1_AND_2_ARE_WORDS) ? 8 : 6;
|
|
1216
|
+
if (flags & COMPOSITE_WE_HAVE_A_SCALE) {
|
|
1217
|
+
offset += 2;
|
|
1218
|
+
} else if (flags & COMPOSITE_WE_HAVE_AN_X_AND_Y_SCALE) {
|
|
1219
|
+
offset += 4;
|
|
1220
|
+
} else if (flags & COMPOSITE_WE_HAVE_A_TWO_BY_TWO) {
|
|
1221
|
+
offset += 8;
|
|
1222
|
+
}
|
|
1223
|
+
if (!keptGlyphIds.has(componentGlyphId)) {
|
|
1224
|
+
keptGlyphIds.add(componentGlyphId);
|
|
1225
|
+
addCompositeComponents(glyf, locaOffsets, componentGlyphId, keptGlyphIds);
|
|
1226
|
+
}
|
|
1227
|
+
} while ((flags & COMPOSITE_MORE_COMPONENTS) && offset < glyphEnd);
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
function writeGlyf(glyf, locaOffsets, glyphCount, keptGlyphIds) {
|
|
1231
|
+
const glyphs = [];
|
|
1232
|
+
let glyfLength = 0;
|
|
1233
|
+
for (let glyphId = 0; glyphId < glyphCount; glyphId++) {
|
|
1234
|
+
const glyphData = keptGlyphIds.has(glyphId) ? glyf.subarray(locaOffsets[glyphId], locaOffsets[glyphId + 1]) : new Uint8Array(0);
|
|
1235
|
+
glyphs.push(glyphData);
|
|
1236
|
+
glyfLength += align(glyphData.length);
|
|
1237
|
+
}
|
|
1238
|
+
const glyfTable = new Uint8Array(glyfLength);
|
|
1239
|
+
const locaTable = new Uint8Array((glyphCount + 1) * 4);
|
|
1240
|
+
let offset = 0;
|
|
1241
|
+
glyphs.forEach((glyphData, glyphId) => {
|
|
1242
|
+
writeUint32(locaTable, glyphId * 4, offset);
|
|
1243
|
+
glyfTable.set(glyphData, offset);
|
|
1244
|
+
offset += align(glyphData.length);
|
|
1245
|
+
});
|
|
1246
|
+
writeUint32(locaTable, glyphCount * 4, offset);
|
|
1247
|
+
return { glyfTable, locaTable };
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
function writeCmap(keptCharCodes, variationSubtable) {
|
|
1251
|
+
const charCodes = Array.from(keptCharCodes.keys()).sort((charCode, otherCharCode) => charCode - otherCharCode);
|
|
1252
|
+
const bmpCharCodes = charCodes.filter(charCode => charCode <= MAX_BMP_CHAR_CODE);
|
|
1253
|
+
const segmentSubtable = writeSegmentSubtable(bmpCharCodes, keptCharCodes);
|
|
1254
|
+
const groupsSubtable = charCodes.length > bmpCharCodes.length ? writeGroupsSubtable(charCodes, keptCharCodes) : null;
|
|
1255
|
+
const records = [[CMAP_PLATFORM_UNICODE, CMAP_ENCODING_UNICODE_BMP, segmentSubtable]];
|
|
1256
|
+
if (groupsSubtable) {
|
|
1257
|
+
records.push([CMAP_PLATFORM_UNICODE, CMAP_ENCODING_UNICODE_FULL, groupsSubtable]);
|
|
1258
|
+
}
|
|
1259
|
+
if (variationSubtable) {
|
|
1260
|
+
records.push([CMAP_PLATFORM_UNICODE, CMAP_ENCODING_UNICODE_VARIATION, variationSubtable]);
|
|
1261
|
+
}
|
|
1262
|
+
records.push([CMAP_PLATFORM_WINDOWS, CMAP_ENCODING_WINDOWS_BMP, segmentSubtable]);
|
|
1263
|
+
if (groupsSubtable) {
|
|
1264
|
+
records.push([CMAP_PLATFORM_WINDOWS, CMAP_ENCODING_WINDOWS_FULL, groupsSubtable]);
|
|
1265
|
+
}
|
|
1266
|
+
const subtables = [];
|
|
1267
|
+
const subtableOffsets = new Map();
|
|
1268
|
+
let offset = 4 + records.length * 8;
|
|
1269
|
+
records.forEach(([, , subtable]) => {
|
|
1270
|
+
if (!subtableOffsets.has(subtable)) {
|
|
1271
|
+
subtableOffsets.set(subtable, offset);
|
|
1272
|
+
subtables.push(subtable);
|
|
1273
|
+
offset += subtable.length;
|
|
1274
|
+
}
|
|
1275
|
+
});
|
|
1276
|
+
const cmap = new Uint8Array(offset);
|
|
1277
|
+
writeUint16(cmap, 0, 0);
|
|
1278
|
+
writeUint16(cmap, 2, records.length);
|
|
1279
|
+
records.forEach(([platformId, encodingId, subtable], indexRecord) => {
|
|
1280
|
+
const recordOffset = 4 + indexRecord * 8;
|
|
1281
|
+
writeUint16(cmap, recordOffset, platformId);
|
|
1282
|
+
writeUint16(cmap, recordOffset + 2, encodingId);
|
|
1283
|
+
writeUint32(cmap, recordOffset + 4, subtableOffsets.get(subtable));
|
|
1284
|
+
});
|
|
1285
|
+
subtables.forEach(subtable => cmap.set(subtable, subtableOffsets.get(subtable)));
|
|
1286
|
+
return cmap;
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
function writeSegmentSubtable(charCodes, keptCharCodes) {
|
|
1290
|
+
const segments = [];
|
|
1291
|
+
charCodes.forEach(charCode => {
|
|
1292
|
+
const lastSegment = segments[segments.length - 1];
|
|
1293
|
+
if (lastSegment && charCode == lastSegment.endCode + 1) {
|
|
1294
|
+
lastSegment.endCode = charCode;
|
|
1295
|
+
lastSegment.glyphIds.push(keptCharCodes.get(charCode));
|
|
1296
|
+
} else {
|
|
1297
|
+
segments.push({ startCode: charCode, endCode: charCode, glyphIds: [keptCharCodes.get(charCode)] });
|
|
1298
|
+
}
|
|
1299
|
+
});
|
|
1300
|
+
segments.forEach(segment => {
|
|
1301
|
+
const delta = (segment.glyphIds[0] - segment.startCode) & 0xFFFF;
|
|
1302
|
+
segment.delta = segment.glyphIds.every((glyphId, indexGlyph) => ((segment.startCode + indexGlyph + delta) & 0xFFFF) == glyphId) ? delta : null;
|
|
1303
|
+
});
|
|
1304
|
+
segments.push({ startCode: MAX_BMP_CHAR_CODE, endCode: MAX_BMP_CHAR_CODE, glyphIds: [], delta: 1 });
|
|
1305
|
+
const segmentCount = segments.length;
|
|
1306
|
+
const glyphIdArrayLength = segments.reduce((length, segment) => length + (segment.delta === null ? segment.glyphIds.length : 0), 0);
|
|
1307
|
+
const length = 16 + segmentCount * 8 + glyphIdArrayLength * 2;
|
|
1308
|
+
const subtable = new Uint8Array(length);
|
|
1309
|
+
const searchRange = 2 * Math.pow(2, Math.floor(Math.log2(segmentCount)));
|
|
1310
|
+
writeUint16(subtable, 0, CMAP_FORMAT_SEGMENT);
|
|
1311
|
+
writeUint16(subtable, 2, length);
|
|
1312
|
+
writeUint16(subtable, 4, 0);
|
|
1313
|
+
writeUint16(subtable, 6, segmentCount * 2);
|
|
1314
|
+
writeUint16(subtable, 8, searchRange);
|
|
1315
|
+
writeUint16(subtable, 10, Math.log2(searchRange / 2));
|
|
1316
|
+
writeUint16(subtable, 12, segmentCount * 2 - searchRange);
|
|
1317
|
+
const endCodesOffset = 14;
|
|
1318
|
+
const startCodesOffset = endCodesOffset + segmentCount * 2 + 2;
|
|
1319
|
+
const deltasOffset = startCodesOffset + segmentCount * 2;
|
|
1320
|
+
const rangeOffsetsOffset = deltasOffset + segmentCount * 2;
|
|
1321
|
+
const glyphIdArrayOffset = rangeOffsetsOffset + segmentCount * 2;
|
|
1322
|
+
let glyphIdArrayIndex = 0;
|
|
1323
|
+
segments.forEach((segment, indexSegment) => {
|
|
1324
|
+
writeUint16(subtable, endCodesOffset + indexSegment * 2, segment.endCode);
|
|
1325
|
+
writeUint16(subtable, startCodesOffset + indexSegment * 2, segment.startCode);
|
|
1326
|
+
if (segment.delta === null) {
|
|
1327
|
+
writeUint16(subtable, deltasOffset + indexSegment * 2, 0);
|
|
1328
|
+
writeUint16(subtable, rangeOffsetsOffset + indexSegment * 2, glyphIdArrayOffset + glyphIdArrayIndex * 2 - (rangeOffsetsOffset + indexSegment * 2));
|
|
1329
|
+
segment.glyphIds.forEach(glyphId => {
|
|
1330
|
+
writeUint16(subtable, glyphIdArrayOffset + glyphIdArrayIndex * 2, glyphId);
|
|
1331
|
+
glyphIdArrayIndex++;
|
|
1332
|
+
});
|
|
1333
|
+
} else {
|
|
1334
|
+
writeUint16(subtable, deltasOffset + indexSegment * 2, segment.delta);
|
|
1335
|
+
writeUint16(subtable, rangeOffsetsOffset + indexSegment * 2, 0);
|
|
1336
|
+
}
|
|
1337
|
+
});
|
|
1338
|
+
return subtable;
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
function writeGroupsSubtable(charCodes, keptCharCodes) {
|
|
1342
|
+
const groups = [];
|
|
1343
|
+
charCodes.forEach(charCode => {
|
|
1344
|
+
const glyphId = keptCharCodes.get(charCode);
|
|
1345
|
+
const lastGroup = groups[groups.length - 1];
|
|
1346
|
+
if (lastGroup && charCode == lastGroup.endCharCode + 1 && glyphId == lastGroup.startGlyphId + (charCode - lastGroup.startCharCode)) {
|
|
1347
|
+
lastGroup.endCharCode = charCode;
|
|
1348
|
+
} else {
|
|
1349
|
+
groups.push({ startCharCode: charCode, endCharCode: charCode, startGlyphId: glyphId });
|
|
1350
|
+
}
|
|
1351
|
+
});
|
|
1352
|
+
const length = 16 + groups.length * 12;
|
|
1353
|
+
const subtable = new Uint8Array(length);
|
|
1354
|
+
writeUint16(subtable, 0, CMAP_FORMAT_GROUPS);
|
|
1355
|
+
writeUint16(subtable, 2, 0);
|
|
1356
|
+
writeUint32(subtable, 4, length);
|
|
1357
|
+
writeUint32(subtable, 8, 0);
|
|
1358
|
+
writeUint32(subtable, 12, groups.length);
|
|
1359
|
+
groups.forEach((group, indexGroup) => {
|
|
1360
|
+
const groupOffset = 16 + indexGroup * 12;
|
|
1361
|
+
writeUint32(subtable, groupOffset, group.startCharCode);
|
|
1362
|
+
writeUint32(subtable, groupOffset + 4, group.endCharCode);
|
|
1363
|
+
writeUint32(subtable, groupOffset + 8, group.startGlyphId);
|
|
1364
|
+
});
|
|
1365
|
+
return subtable;
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
function writeSfnt(flavor, tables) {
|
|
1369
|
+
const tags = Array.from(tables.keys()).sort();
|
|
1370
|
+
const entries = [];
|
|
1371
|
+
let offset = SFNT_HEADER_LENGTH + tags.length * SFNT_ENTRY_LENGTH;
|
|
1372
|
+
tags.forEach(tag => {
|
|
1373
|
+
const table = tables.get(tag);
|
|
1374
|
+
entries.push({ tag, offset, length: table.length, checksum: checksum(table) });
|
|
1375
|
+
offset += align(table.length);
|
|
1376
|
+
});
|
|
1377
|
+
const data = new Uint8Array(offset);
|
|
1378
|
+
const searchRange = 16 * Math.pow(2, Math.floor(Math.log2(tags.length)));
|
|
1379
|
+
writeUint32(data, 0, flavor);
|
|
1380
|
+
writeUint16(data, 4, tags.length);
|
|
1381
|
+
writeUint16(data, 6, searchRange);
|
|
1382
|
+
writeUint16(data, 8, Math.log2(searchRange / 16));
|
|
1383
|
+
writeUint16(data, 10, tags.length * 16 - searchRange);
|
|
1384
|
+
entries.forEach((entry, indexEntry) => {
|
|
1385
|
+
const entryOffset = SFNT_HEADER_LENGTH + indexEntry * SFNT_ENTRY_LENGTH;
|
|
1386
|
+
writeTag(data, entryOffset, entry.tag);
|
|
1387
|
+
writeUint32(data, entryOffset + 4, entry.checksum);
|
|
1388
|
+
writeUint32(data, entryOffset + 8, entry.offset);
|
|
1389
|
+
writeUint32(data, entryOffset + 12, entry.length);
|
|
1390
|
+
data.set(tables.get(entry.tag), entry.offset);
|
|
1391
|
+
});
|
|
1392
|
+
const headEntry = entries.find(entry => entry.tag == TAG_HEAD);
|
|
1393
|
+
writeUint32(data, headEntry.offset + HEAD_CHECKSUM_OFFSET, (CHECKSUM_ADJUSTMENT_MAGIC - checksum(data)) >>> 0);
|
|
1394
|
+
return { data, entries };
|
|
1395
|
+
}
|
|
1396
|
+
|
|
1397
|
+
async function writeWoff(sfnt, font) {
|
|
1398
|
+
const tables = await Promise.all(sfnt.entries.map(async entry => {
|
|
1399
|
+
const original = sfnt.data.subarray(entry.offset, entry.offset + entry.length);
|
|
1400
|
+
const compressed = await deflate(original);
|
|
1401
|
+
return { entry, data: compressed.length < original.length ? compressed : original };
|
|
1402
|
+
}));
|
|
1403
|
+
let offset = WOFF_HEADER_LENGTH + tables.length * WOFF_ENTRY_LENGTH;
|
|
1404
|
+
const tableOffsets = tables.map(({ data }) => {
|
|
1405
|
+
const tableOffset = offset;
|
|
1406
|
+
offset += align(data.length);
|
|
1407
|
+
return tableOffset;
|
|
1408
|
+
});
|
|
1409
|
+
const metaOffset = font.metadata ? offset : 0;
|
|
1410
|
+
offset += font.metadata ? align(font.metadata.length) : 0;
|
|
1411
|
+
const privateOffset = font.privateData ? offset : 0;
|
|
1412
|
+
offset += font.privateData ? font.privateData.length : 0;
|
|
1413
|
+
const data = new Uint8Array(offset);
|
|
1414
|
+
writeUint32(data, 0, SFNT_WOFF);
|
|
1415
|
+
writeUint32(data, 4, font.flavor);
|
|
1416
|
+
writeUint32(data, 8, offset);
|
|
1417
|
+
writeUint16(data, 12, tables.length);
|
|
1418
|
+
writeUint32(data, 16, sfnt.data.length);
|
|
1419
|
+
writeUint32(data, 20, font.version);
|
|
1420
|
+
writeUint32(data, 24, metaOffset);
|
|
1421
|
+
writeUint32(data, 28, font.metadata ? font.metadata.length : 0);
|
|
1422
|
+
writeUint32(data, 32, font.metadata ? font.metaOriginalLength : 0);
|
|
1423
|
+
writeUint32(data, 36, privateOffset);
|
|
1424
|
+
writeUint32(data, 40, font.privateData ? font.privateData.length : 0);
|
|
1425
|
+
tables.forEach(({ entry, data: tableData }, indexTable) => {
|
|
1426
|
+
const entryOffset = WOFF_HEADER_LENGTH + indexTable * WOFF_ENTRY_LENGTH;
|
|
1427
|
+
writeTag(data, entryOffset, entry.tag);
|
|
1428
|
+
writeUint32(data, entryOffset + 4, tableOffsets[indexTable]);
|
|
1429
|
+
writeUint32(data, entryOffset + 8, tableData.length);
|
|
1430
|
+
writeUint32(data, entryOffset + 12, entry.length);
|
|
1431
|
+
writeUint32(data, entryOffset + 16, entry.checksum);
|
|
1432
|
+
data.set(tableData, tableOffsets[indexTable]);
|
|
1433
|
+
});
|
|
1434
|
+
if (font.metadata) {
|
|
1435
|
+
data.set(font.metadata, metaOffset);
|
|
1436
|
+
}
|
|
1437
|
+
if (font.privateData) {
|
|
1438
|
+
data.set(font.privateData, privateOffset);
|
|
1439
|
+
}
|
|
1440
|
+
return data;
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
async function writeWoff2(sfnt, font, brotli, glyphCount) {
|
|
1444
|
+
const locaEntry = sfnt.entries.find(entry => entry.tag == TAG_LOCA);
|
|
1445
|
+
const locaOffsets = readLoca(sfnt.data.subarray(locaEntry.offset, locaEntry.offset + locaEntry.length), glyphCount, LONG_LOCA_FORMAT);
|
|
1446
|
+
const directory = new ByteWriter();
|
|
1447
|
+
const payload = new ByteWriter();
|
|
1448
|
+
sfnt.entries.forEach(entry => {
|
|
1449
|
+
const table = sfnt.data.subarray(entry.offset, entry.offset + entry.length);
|
|
1450
|
+
const knownTag = WOFF2_KNOWN_TAGS.indexOf(entry.tag);
|
|
1451
|
+
let transformVersion = 0, transformedTable = table;
|
|
1452
|
+
if (entry.tag == TAG_GLYF) {
|
|
1453
|
+
transformVersion = WOFF2_GLYF_TRANSFORM;
|
|
1454
|
+
transformedTable = transformGlyf(table, locaOffsets, glyphCount);
|
|
1455
|
+
} else if (entry.tag == TAG_LOCA) {
|
|
1456
|
+
transformVersion = WOFF2_GLYF_TRANSFORM;
|
|
1457
|
+
transformedTable = new Uint8Array(0);
|
|
1458
|
+
}
|
|
1459
|
+
directory.uint8((knownTag < 0 ? WOFF2_ARBITRARY_TAG : knownTag) | (transformVersion << WOFF2_TRANSFORM_VERSION_SHIFT));
|
|
1460
|
+
if (knownTag < 0) {
|
|
1461
|
+
directory.bytes(new Uint8Array([...entry.tag].map(character => character.charCodeAt(0))));
|
|
1462
|
+
}
|
|
1463
|
+
writeBase128(directory, entry.length);
|
|
1464
|
+
if (entry.tag == TAG_GLYF || entry.tag == TAG_LOCA) {
|
|
1465
|
+
writeBase128(directory, transformedTable.length);
|
|
1466
|
+
}
|
|
1467
|
+
payload.bytes(transformedTable);
|
|
1468
|
+
});
|
|
1469
|
+
const compressed = await brotli.compress(payload.toUint8Array());
|
|
1470
|
+
const directoryData = directory.toUint8Array();
|
|
1471
|
+
let offset = align(WOFF2_HEADER_LENGTH + directoryData.length + compressed.length);
|
|
1472
|
+
const metaOffset = font.metadata ? offset : 0;
|
|
1473
|
+
offset = font.metadata ? align(metaOffset + font.metadata.length) : offset;
|
|
1474
|
+
const privateOffset = font.privateData ? offset : 0;
|
|
1475
|
+
offset = font.privateData ? privateOffset + font.privateData.length : offset;
|
|
1476
|
+
const data = new Uint8Array(offset);
|
|
1477
|
+
writeUint32(data, 0, SFNT_WOFF2);
|
|
1478
|
+
writeUint32(data, 4, font.flavor);
|
|
1479
|
+
writeUint32(data, 8, offset);
|
|
1480
|
+
writeUint16(data, 12, sfnt.entries.length);
|
|
1481
|
+
writeUint32(data, 16, sfnt.data.length);
|
|
1482
|
+
writeUint32(data, 20, compressed.length);
|
|
1483
|
+
writeUint32(data, 24, font.version);
|
|
1484
|
+
writeUint32(data, 28, metaOffset);
|
|
1485
|
+
writeUint32(data, 32, font.metadata ? font.metadata.length : 0);
|
|
1486
|
+
writeUint32(data, 36, font.metadata ? font.metaOriginalLength : 0);
|
|
1487
|
+
writeUint32(data, 40, privateOffset);
|
|
1488
|
+
writeUint32(data, 44, font.privateData ? font.privateData.length : 0);
|
|
1489
|
+
data.set(directoryData, WOFF2_HEADER_LENGTH);
|
|
1490
|
+
data.set(compressed, WOFF2_HEADER_LENGTH + directoryData.length);
|
|
1491
|
+
if (font.metadata) {
|
|
1492
|
+
data.set(font.metadata, metaOffset);
|
|
1493
|
+
}
|
|
1494
|
+
if (font.privateData) {
|
|
1495
|
+
data.set(font.privateData, privateOffset);
|
|
1496
|
+
}
|
|
1497
|
+
return data;
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
async function inflate(data, originalLength) {
|
|
1501
|
+
const inflated = await transform(data, new DecompressionStream("deflate"));
|
|
1502
|
+
if (inflated.length != originalLength) {
|
|
1503
|
+
throw new Error("Invalid WOFF table length");
|
|
1504
|
+
}
|
|
1505
|
+
return inflated;
|
|
1506
|
+
}
|
|
1507
|
+
|
|
1508
|
+
function deflate(data) {
|
|
1509
|
+
return transform(data, new CompressionStream("deflate"));
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
async function transform(data, stream) {
|
|
1513
|
+
return new Uint8Array(await new Response(new Blob([data]).stream().pipeThrough(stream)).arrayBuffer());
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
function checksum(data) {
|
|
1517
|
+
let sum = 0;
|
|
1518
|
+
const paddedLength = align(data.length);
|
|
1519
|
+
for (let offset = 0; offset < paddedLength; offset += 4) {
|
|
1520
|
+
let value = 0;
|
|
1521
|
+
for (let indexByte = 0; indexByte < 4; indexByte++) {
|
|
1522
|
+
value = (value << 8) | (offset + indexByte < data.length ? data[offset + indexByte] : 0);
|
|
1523
|
+
}
|
|
1524
|
+
sum = (sum + (value >>> 0)) >>> 0;
|
|
1525
|
+
}
|
|
1526
|
+
return sum;
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
function align(length) {
|
|
1530
|
+
return (length + 3) & ~3;
|
|
1531
|
+
}
|
|
1532
|
+
|
|
1533
|
+
function readTag(data, offset) {
|
|
1534
|
+
return String.fromCharCode(data[offset], data[offset + 1], data[offset + 2], data[offset + 3]);
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
function writeTag(data, offset, tag) {
|
|
1538
|
+
for (let indexCharacter = 0; indexCharacter < 4; indexCharacter++) {
|
|
1539
|
+
data[offset + indexCharacter] = tag.charCodeAt(indexCharacter);
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
|
|
1543
|
+
function readUint16(data, offset) {
|
|
1544
|
+
return (data[offset] << 8) | data[offset + 1];
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
function readInt16(data, offset) {
|
|
1548
|
+
const value = readUint16(data, offset);
|
|
1549
|
+
return value & 0x8000 ? value - 0x10000 : value;
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
function readUint32(data, offset) {
|
|
1553
|
+
return ((data[offset] << 24) | (data[offset + 1] << 16) | (data[offset + 2] << 8) | data[offset + 3]) >>> 0;
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
function writeUint16(data, offset, value) {
|
|
1557
|
+
data[offset] = (value >>> 8) & 0xFF;
|
|
1558
|
+
data[offset + 1] = value & 0xFF;
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
function writeInt16(data, offset, value) {
|
|
1562
|
+
writeUint16(data, offset, value & 0xFFFF);
|
|
1563
|
+
}
|
|
1564
|
+
|
|
1565
|
+
function writeUint32(data, offset, value) {
|
|
1566
|
+
data[offset] = (value >>> 24) & 0xFF;
|
|
1567
|
+
data[offset + 1] = (value >>> 16) & 0xFF;
|
|
1568
|
+
data[offset + 2] = (value >>> 8) & 0xFF;
|
|
1569
|
+
data[offset + 3] = value & 0xFF;
|
|
1570
|
+
}
|