summaryrefslogtreecommitdiff
path: root/packages/icons-subsetter/src/subsetter.ts
blob: cd1aed28902352b6634e829c3943428270386481 (plain)
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
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { promises as fsp } from 'fs';
import { compress } from 'wawoff2';

export async function generateSubsettedFont(ttfPath: string, unicodeRangeValues: Map<string, number[]>) {
	const ttf = await fsp.readFile(ttfPath);

	const {
		instance: { exports: harfbuzzWasm },
	}: any = await WebAssembly.instantiate(await fsp.readFile('./node_modules/harfbuzzjs/hb-subset.wasm'));

	const heapu8 = new Uint8Array(harfbuzzWasm.memory.buffer);

	const subsetFonts = new Map<string, Buffer>();

	let i = 0;
	for (const [key, unicodeValues] of unicodeRangeValues) {
		i++;
		console.log(`Generating subset ${i} of ${unicodeRangeValues.size}...`);

		// サブセット入力を作成
		const input = harfbuzzWasm.hb_subset_input_create_or_fail();
		if (input === 0) {
			throw new Error('hb_subset_input_create_or_fail (harfbuzz) returned zero');
		}

		// フォントバッファにフォントデータをセット
		const fontBuffer = harfbuzzWasm.malloc(ttf.byteLength);
		heapu8.set(new Uint8Array(ttf), fontBuffer);

		// フォントフェイスを作成
		const blob = harfbuzzWasm.hb_blob_create(fontBuffer, ttf.byteLength, 2, 0, 0);
		const face = harfbuzzWasm.hb_face_create(blob, 0);
		harfbuzzWasm.hb_blob_destroy(blob);

		// Unicodeセットに指定されたUnicodeポイントを追加
		const inputUnicodes = harfbuzzWasm.hb_subset_input_unicode_set(input);
		for (const unicode of unicodeValues) {
			harfbuzzWasm.hb_set_add(inputUnicodes, unicode);
		}

		// サブセットを作成
		let subset;
		try {
			subset = harfbuzzWasm.hb_subset_or_fail(face, input);
			if (subset === 0) {
				harfbuzzWasm.hb_face_destroy(face);
				harfbuzzWasm.free(fontBuffer);
				throw new Error('hb_subset_or_fail (harfbuzz) returned zero');
			}
		} finally {
			harfbuzzWasm.hb_subset_input_destroy(input);
		}

		// サブセットフォントデータを取得
		const result = harfbuzzWasm.hb_face_reference_blob(subset);
		const offset = harfbuzzWasm.hb_blob_get_data(result, 0);
		const subsetByteLength = harfbuzzWasm.hb_blob_get_length(result);
		if (subsetByteLength === 0) {
			harfbuzzWasm.hb_face_destroy(face);
			harfbuzzWasm.hb_blob_destroy(result);
			harfbuzzWasm.free(fontBuffer);
			throw new Error('hb_blob_get_length (harfbuzz) returned zero');
		}

		// サブセットフォントをバッファに格納
		subsetFonts.set(key, Buffer.from(await compress(heapu8.slice(offset, offset + subsetByteLength))));

		// メモリを解放
		harfbuzzWasm.hb_blob_destroy(result);
		harfbuzzWasm.hb_face_destroy(subset);
		harfbuzzWasm.hb_face_destroy(face);
		harfbuzzWasm.free(fontBuffer);
	}

	return subsetFonts;
}