-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
188 lines (167 loc) · 5.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* global WebAssembly */
const { readFile } = require('fs').promises;
const _ = require('lodash');
const fontverter = require('fontverter');
const loadAndInitializeHarfbuzz = _.once(async () => {
const {
instance: { exports: harfbuzzJsWasm },
} = await WebAssembly.instantiate(
await readFile(require.resolve('harfbuzzjs/hb-subset.wasm'))
);
const heapu8 = new Uint8Array(harfbuzzJsWasm.memory.buffer);
return { harfbuzzJsWasm, heapu8 };
});
function HB_TAG(str) {
return str.split('').reduce(function (a, ch) {
return (a << 8) + ch.charCodeAt(0);
}, 0);
}
async function subsetFont(
originalFont,
text,
{
targetFormat = fontverter.detectFormat(originalFont),
preserveNameIds,
variationAxes,
noLayoutClosure,
} = {}
) {
if (typeof text !== 'string') {
throw new Error('The subset text must be given as a string');
}
const { harfbuzzJsWasm, heapu8 } = await loadAndInitializeHarfbuzz();
originalFont = await fontverter.convert(originalFont, 'truetype');
const input = harfbuzzJsWasm.hb_subset_input_create_or_fail();
if (input === 0) {
throw new Error(
'hb_subset_input_create_or_fail (harfbuzz) returned zero, indicating failure'
);
}
const fontBuffer = harfbuzzJsWasm.malloc(originalFont.byteLength);
heapu8.set(new Uint8Array(originalFont), fontBuffer);
// Create the face
const blob = harfbuzzJsWasm.hb_blob_create(
fontBuffer,
originalFont.byteLength,
2, // HB_MEMORY_MODE_WRITABLE
0,
0
);
const face = harfbuzzJsWasm.hb_face_create(blob, 0);
harfbuzzJsWasm.hb_blob_destroy(blob);
// Do the equivalent of --font-features=*
const layoutFeatures = harfbuzzJsWasm.hb_subset_input_set(
input,
6 // HB_SUBSET_SETS_LAYOUT_FEATURE_TAG
);
harfbuzzJsWasm.hb_set_clear(layoutFeatures);
harfbuzzJsWasm.hb_set_invert(layoutFeatures);
if (preserveNameIds) {
const inputNameIds = harfbuzzJsWasm.hb_subset_input_set(
input,
4 // HB_SUBSET_SETS_NAME_ID
);
for (const nameId of preserveNameIds) {
harfbuzzJsWasm.hb_set_add(inputNameIds, nameId);
}
}
if (noLayoutClosure) {
harfbuzzJsWasm.hb_subset_input_set_flags(
input,
harfbuzzJsWasm.hb_subset_input_get_flags(input) | 0x00000200 // HB_SUBSET_FLAGS_NO_LAYOUT_CLOSURE
);
}
// Add unicodes indices
const inputUnicodes = harfbuzzJsWasm.hb_subset_input_unicode_set(input);
for (const c of text) {
harfbuzzJsWasm.hb_set_add(inputUnicodes, c.codePointAt(0));
}
if (variationAxes) {
for (const [axisName, value] of Object.entries(variationAxes)) {
if (typeof value === 'number') {
// Simple case: Pin/instance the variation axis to a single value
if (
!harfbuzzJsWasm.hb_subset_input_pin_axis_location(
input,
face,
HB_TAG(axisName),
value
)
) {
harfbuzzJsWasm.hb_face_destroy(face);
harfbuzzJsWasm.free(fontBuffer);
throw new Error(
`hb_subset_input_pin_axis_location (harfbuzz) returned zero when pinning ${axisName} to ${value}, indicating failure. Maybe the axis does not exist in the font?`
);
}
} else if (value && typeof value === 'object') {
// Complex case: Reduce the variation space of the axis
if (
typeof value.min === 'undefined' ||
typeof value.max === 'undefined'
) {
harfbuzzJsWasm.hb_face_destroy(face);
harfbuzzJsWasm.free(fontBuffer);
throw new Error(
`${axisName}: You must provide both a min and a max value when setting the axis range`
);
}
if (
!harfbuzzJsWasm.hb_subset_input_set_axis_range(
input,
face,
HB_TAG(axisName),
value.min,
value.max,
// An explicit NaN makes harfbuzz use the existing default value, clamping to the new range if necessary
value.default ?? NaN
)
) {
harfbuzzJsWasm.hb_face_destroy(face);
harfbuzzJsWasm.free(fontBuffer);
throw new Error(
`hb_subset_input_set_axis_range (harfbuzz) returned zero when setting the range of ${axisName} to [${value.min}; ${value.max}] and a default value of ${value.default}, indicating failure. Maybe the axis does not exist in the font?`
);
}
}
}
}
let subset;
try {
subset = harfbuzzJsWasm.hb_subset_or_fail(face, input);
if (subset === 0) {
harfbuzzJsWasm.hb_face_destroy(face);
harfbuzzJsWasm.free(fontBuffer);
throw new Error(
'hb_subset_or_fail (harfbuzz) returned zero, indicating failure. Maybe the input file is corrupted?'
);
}
} finally {
// Clean up
harfbuzzJsWasm.hb_subset_input_destroy(input);
}
// Get result blob
const result = harfbuzzJsWasm.hb_face_reference_blob(subset);
const offset = harfbuzzJsWasm.hb_blob_get_data(result, 0);
const subsetByteLength = harfbuzzJsWasm.hb_blob_get_length(result);
if (subsetByteLength === 0) {
harfbuzzJsWasm.hb_blob_destroy(result);
harfbuzzJsWasm.hb_face_destroy(subset);
harfbuzzJsWasm.hb_face_destroy(face);
harfbuzzJsWasm.free(fontBuffer);
throw new Error(
'Failed to create subset font, maybe the input file is corrupted?'
);
}
const subsetFont = Buffer.from(
heapu8.subarray(offset, offset + subsetByteLength)
);
// Clean up
harfbuzzJsWasm.hb_blob_destroy(result);
harfbuzzJsWasm.hb_face_destroy(subset);
harfbuzzJsWasm.hb_face_destroy(face);
harfbuzzJsWasm.free(fontBuffer);
return await fontverter.convert(subsetFont, targetFormat, 'truetype');
}
const limiter = require('p-limit')(1);
module.exports = (...args) => limiter(() => subsetFont(...args));