cn-font-split
Version:
划时代的字体切割工具,CJK与任何字符!支持 otf、ttf、woff2 字体多线程切割,完美地细颗粒度地进行包大小控制。A revolutionary font subetter that supports CJK and any characters! It enables multi-threaded subset of otf, ttf, and woff2 fonts, allowing for precise control over package size.
48 lines (46 loc) • 1.44 kB
text/typescript
/** @ts-ignore */
import { dlopen, FFIType, ptr, JSCallback, toArrayBuffer } from 'bun:ffi';
import path from 'path';
import { FontSplitProps } from '../interface.js';
import { getBinName, matchPlatform } from '../load.js';
import { isMusl } from '../node/isMusl.js';
import { createAPI } from '../createAPI.js';
export * from '../interface.js';
export * from '../createAPI.js';
let binPath = process.env.CN_FONT_SPLIT_BIN;
if (!binPath) {
binPath = path.resolve(
__dirname,
'../' +
getBinName(matchPlatform(process.platform, process.arch, isMusl)),
);
// console.log(binPath);
// throw new Error('CN_FONT_SPLIT_BIN is undefined!');
}
const {
symbols: { font_split },
close,
} = dlopen(binPath, {
font_split: {
args: [FFIType.ptr, FFIType.usize, FFIType.callback],
returns: FFIType.void,
},
});
const createCallback = (cb: (data: Uint8Array) => void) =>
new JSCallback(
(ptr: any, length: BigInt) => {
const data = new Uint8Array(
toArrayBuffer(ptr, 0, Number(length)).slice(),
0,
Number(length),
);
cb(data);
},
{
returns: FFIType.void,
args: [FFIType.ptr, FFIType.usize],
},
).ptr;
export const fontSplit = createAPI((buffer, length, cb) => {
return font_split(ptr(buffer), length, cb);
}, createCallback);