mirror of
https://github.com/misskey-dev/misskey.git
synced 2026-05-26 21:54:16 +02:00
feat(frontend): tabler-iconsのサブセット化 (#15340)
* feat(frontend): tabler-iconsの使用されていないアイコンを削除するように * fix * fix * fix * fix * fix * Update Changelog * enhance: tablerのCSSを使用されているクラスのみに限定 * 使用するアイコンパッケージをそろえる * Update CONTRIBUTING.md * Update CONTRIBUTING.md * spdx * typo * fix: サブセットから除外される書き方をしている部分を修正 * fix: 同じunicodeに複数のアイコンclassが割り当てられている場合に対応 * remove debug code * Update CHANGELOG.md * fix merge error * setup renovate * fix: woff2ではなくwoffに変換していたのを修正 * update deps * update changelog
This commit is contained in:
81
packages/icons-subsetter/src/subsetter.ts
Normal file
81
packages/icons-subsetter/src/subsetter.ts
Normal file
@@ -0,0 +1,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;
|
||||
}
|
||||
Reference in New Issue
Block a user