-
Notifications
You must be signed in to change notification settings - Fork 35
/
harfbuzz.ts
289 lines (249 loc) · 9.15 KB
/
harfbuzz.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
type Pointer = number;
const HB_MEMORY_MODE_WRITABLE: number = 2;
const HB_SET_VALUE_INVALID: Pointer = -1;
class HarfBuzzExports {
readonly heapu8: Uint8Array;
readonly heapu32: Uint32Array;
readonly heapi32: Int32Array;
readonly utf8Encoder: TextEncoder;
//exported HarfBuzz methods
readonly malloc: (length: number) => Pointer
readonly free: (ptr: Pointer) => void
readonly free_ptr: () => Pointer
readonly hb_blob_create: (data: Pointer, length: number, memoryMode: number, useData: Pointer, destroyFunction: Pointer) => Pointer
readonly hb_blob_destroy: (ptr: Pointer) => void
readonly hb_face_create: (blobPtr: Pointer, index: number) => Pointer
readonly hb_face_get_upem: (facePtr: Pointer) => number
readonly hb_face_destroy: (ptr: Pointer) => void
readonly hb_font_create: (facePtr: Pointer) => Pointer
readonly hb_font_set_scale: (fontPtr: Pointer, xScale: number, yScale: number) => void
readonly hb_font_destroy: (ptr: Pointer) => void
readonly hb_face_collect_unicodes: (facePtr: Pointer, setPtr: Pointer) => void
readonly hb_set_create: () => Pointer
readonly hb_set_destroy: (setPtr: Pointer) => void
readonly hb_set_get_population: (setPtr: Pointer) => number
readonly hb_set_next_many: (
setPtr: Pointer,
greaterThanUnicodePtr: Pointer,
outputU32ArrayPtr: Pointer,
size: number,
) => number
readonly hb_buffer_create: () => Pointer
readonly hb_buffer_add_utf8: (bufferPtr: Pointer, stringPtr: Pointer, stringLength: number, itemOffset: number, itemLength: number) => void
readonly hb_buffer_guess_segment_properties: (bufferPtr: Pointer) => void
readonly hb_buffer_set_direction: (bufferPtr: Pointer, direction: number) => void
readonly hb_shape: (fontPtr: Pointer, bufferPtr: Pointer, features: any, numFeatures: number) => void
readonly hb_buffer_get_length: (bufferPtr: Pointer) => number
readonly hb_buffer_get_glyph_infos: (bufferPtr: Pointer, length: number) => any
readonly hb_buffer_get_glyph_positions: (bufferPtr: Pointer, length: number) => any
readonly hb_buffer_destroy: (bufferPtr: Pointer) => void
constructor(exports: any) {
this.heapu8 = new Uint8Array(exports.memory.buffer);
this.heapu32 = new Uint32Array(exports.memory.buffer);
this.heapi32 = new Int32Array(exports.memory.buffer);
this.utf8Encoder = new TextEncoder();
this.malloc = exports.malloc;
this.free = exports.free;
this.free_ptr = exports.free_ptr;
this.hb_blob_destroy = exports.hb_blob_destroy;
this.hb_blob_create = exports.hb_blob_create;
this.hb_face_create = exports.hb_face_create;
this.hb_face_get_upem = exports.hb_face_get_upem;
this.hb_face_destroy = exports.hb_face_destroy;
this.hb_face_collect_unicodes = exports.hb_face_collect_unicodes;
this.hb_set_create = exports.hb_set_create;
this.hb_set_destroy = exports.hb_set_destroy;
this.hb_set_get_population = exports.hb_set_get_population;
this.hb_set_next_many = exports.hb_set_next_many;
this.hb_font_create = exports.hb_font_create;
this.hb_font_set_scale = exports.hb_font_set_scale;
this.hb_font_destroy = exports.hb_font_destroy;
this.hb_buffer_create = exports.hb_buffer_create;
this.hb_buffer_add_utf8 = exports.hb_buffer_add_utf8;
this.hb_buffer_guess_segment_properties = exports.hb_buffer_guess_segment_properties;
this.hb_buffer_set_direction = exports.hb_buffer_set_direction;
this.hb_shape = exports.hb_shape;
this.hb_buffer_get_length = exports.hb_buffer_get_length;
this.hb_buffer_get_glyph_infos = exports.hb_buffer_get_glyph_infos;
this.hb_buffer_get_glyph_positions = exports.hb_buffer_get_glyph_positions;
this.hb_buffer_destroy = exports.hb_buffer_destroy;
}
}
let hb: HarfBuzzExports;
class CString {
readonly ptr: Pointer;
readonly length: number;
constructor(text: string) {
var bytes = hb.utf8Encoder.encode(text);
this.ptr = hb.malloc(bytes.byteLength);
hb.heapu8.set(bytes, this.ptr);
this.length = bytes.byteLength;
}
destroy() {
hb.free(this.ptr);
}
}
export class HarfBuzzBlob {
readonly ptr: Pointer;
constructor(data: Uint8Array) {
let blobPtr = hb.malloc(data.length);
hb.heapu8.set(data, blobPtr);
this.ptr = hb.hb_blob_create(blobPtr, data.byteLength, HB_MEMORY_MODE_WRITABLE, blobPtr, hb.free_ptr());
}
destroy() {
hb.hb_blob_destroy(this.ptr);
}
}
function typedArrayFromSet<T extends 'u8' | 'u32' | 'i32'>(setPtr: Pointer, arrayType: T) {
const heap = hb[`heap${arrayType}`];
const bytesPerElment = heap.BYTES_PER_ELEMENT;
const setCount = hb.hb_set_get_population(setPtr);
const arrayPtr = hb.malloc(
setCount * bytesPerElment,
);
const arrayOffset = arrayPtr / bytesPerElment;
const array = heap.subarray(
arrayOffset,
arrayOffset + setCount,
) as typeof hb[`heap${T}`];
heap.set(array, arrayOffset);
hb.hb_set_next_many(
setPtr,
HB_SET_VALUE_INVALID,
arrayPtr,
setCount,
);
return array;
}
export class HarfBuzzFace {
readonly ptr: Pointer;
constructor(blob: HarfBuzzBlob, index: number) {
this.ptr = hb.hb_face_create(blob.ptr, index);
}
getUnitsPerEM() {
return hb.hb_face_get_upem(this.ptr);
}
collectUnicodes() {
const unicodeSetPtr = hb.hb_set_create();
hb.hb_face_collect_unicodes(this.ptr, unicodeSetPtr);
const result = typedArrayFromSet(unicodeSetPtr, 'u32');
hb.hb_set_destroy(unicodeSetPtr);
return result;
}
destroy() {
hb.hb_face_destroy(this.ptr);
}
}
export class HarfBuzzFont {
readonly ptr: Pointer
readonly unitsPerEM: number
constructor(face: HarfBuzzFace) {
this.ptr = hb.hb_font_create(face.ptr);
this.unitsPerEM = face.getUnitsPerEM();
}
setScale(xScale: number, yScale: number) {
hb.hb_font_set_scale(this.ptr, xScale, yScale);
}
destroy() {
hb.hb_font_destroy(this.ptr);
}
}
export type HarfBuzzDirection = "ltr" | "rtl" | "ttb" | "btt"
class GlyphInformation {
readonly GlyphId: number
readonly Cluster: number
readonly XAdvance: number
readonly YAdvance: number
readonly XOffset: number
readonly YOffset: number
constructor(glyphId: number, cluster: number, xAdvance: number, yAdvance: number, xOffset: number, yOffset: number) {
this.GlyphId = glyphId;
this.Cluster = cluster;
this.XAdvance = xAdvance;
this.YAdvance = yAdvance;
this.XOffset = xOffset;
this.YOffset = yOffset;
}
}
export class HarfBuzzBuffer {
readonly ptr: Pointer
constructor() {
this.ptr = hb.hb_buffer_create();
}
addText(text: string) {
let str = new CString(text);
hb.hb_buffer_add_utf8(this.ptr, str.ptr, str.length, 0, str.length);
str.destroy();
}
guessSegmentProperties() {
hb.hb_buffer_guess_segment_properties(this.ptr);
}
setDirection(direction: HarfBuzzDirection) {
let d = { "ltr": 4, "rtl": 5, "ttb": 6, "btt": 7 }[direction];
hb.hb_buffer_set_direction(this.ptr, d);
}
json() {
var length = hb.hb_buffer_get_length(this.ptr);
var result = new Array<GlyphInformation>();
var infosPtr32 = hb.hb_buffer_get_glyph_infos(this.ptr, 0) / 4;
var positionsPtr32 = hb.hb_buffer_get_glyph_positions(this.ptr, 0) / 4;
var infos = hb.heapu32.subarray(infosPtr32, infosPtr32 + 5 * length);
var positions = hb.heapi32.subarray(positionsPtr32, positionsPtr32 + 5 * length);
for (var i = 0; i < length; ++i) {
result.push(new GlyphInformation(
infos[i * 5 + 0],
infos[i * 5 + 2],
positions[i * 5 + 0],
positions[i * 5 + 1],
positions[i * 5 + 2],
positions[i * 5 + 3]));
}
return result;
}
destroy() {
hb.hb_buffer_destroy(this.ptr)
}
}
export function shape(text: string, font: HarfBuzzFont, features: any): Array<GlyphInformation> {
let buffer = new HarfBuzzBuffer();
buffer.addText(text);
buffer.guessSegmentProperties();
buffer.shape(font, features);
let result = buffer.json();
buffer.destroy();
return result;
}
export function getWidth(text: string, font: HarfBuzzFont, fontSizeInPixel: number, features: any): number {
let scale = fontSizeInPixel / font.unitsPerEM;
let shapeResult = shape(text, font, features);
let totalWidth = shapeResult.map((glyphInformation) => {
return glyphInformation.XAdvance;
}).reduce((previous, current, i, arr) => {
return previous + current;
}, 0.0);
return totalWidth * scale;
}
export const harfbuzzFonts = new Map<string, HarfBuzzFont>();
export function loadHarfbuzz(webAssemblyUrl: string): Promise<void> {
return fetch(webAssemblyUrl).then(response => {
return response.arrayBuffer();
}).then(wasm => {
return WebAssembly.instantiate(wasm);
}).then(result => {
//@ts-ignore
hb = new HarfBuzzExports(result.instance.exports);
});
}
export function loadAndCacheFont(fontName: string, fontUrl: string): Promise<void> {
return fetch(fontUrl).then((response) => {
return response.arrayBuffer().then((blob) => {
let fontBlob = new Uint8Array(blob);
let harfbuzzBlob = new HarfBuzzBlob(fontBlob);
let harfbuzzFace = new HarfBuzzFace(harfbuzzBlob, 0);
let harfbuzzFont = new HarfBuzzFont(harfbuzzFace);
harfbuzzFonts.set(fontName, harfbuzzFont);
harfbuzzFace.destroy();
harfbuzzBlob.destroy();
});
});
}