mirror of
https://github.com/2234839/web-font.git
synced 2025-05-31 10:59:18 +08:00
27 lines
694 B
TypeScript
27 lines
694 B
TypeScript
import { Font, type FontEditor } from "fonteditor-core";
|
|
|
|
export const fontSubset = async (
|
|
fontBuffer: ArrayBuffer,
|
|
subString: string,
|
|
option: {
|
|
sourceType: FontEditor.FontType;
|
|
outType: FontEditor.FontType;
|
|
},
|
|
) => {
|
|
const font = Font.create(fontBuffer, {
|
|
type: option.sourceType,
|
|
subset: [...subString].map((char) => char.codePointAt(0)!),
|
|
});
|
|
// 优化字体
|
|
let optimizedFont = font.optimize();
|
|
optimizedFont = optimizedFont.compound2simple();
|
|
optimizedFont = optimizedFont.sort();
|
|
const newFont = optimizedFont.write({
|
|
type: option.outType,
|
|
});
|
|
if (typeof newFont !== "string") {
|
|
return new Uint8Array(newFont);
|
|
}
|
|
return newFont;
|
|
};
|