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.
1 lines • 3.89 kB
Source Map (JSON)
{"version":3,"file":"createAPI.mjs","names":[],"sources":["../src/createAPI.ts"],"sourcesContent":["import { api_interface } from './gen/index';\nimport { FontSplitProps, OriginInput } from './interface';\nexport * from './decodeReporter';\nimport fs from 'fs-extra';\nimport path from 'path';\nexport { api_interface as proto };\n/** 转换 JS 接口数据为 proto 接口数据 */\nconst transType = async (props: FontSplitProps): Promise<OriginInput> => {\n const data: Partial<OriginInput> = {};\n if (typeof props.input === 'string') {\n data.input = await fs.readFile(props.input);\n } else {\n data.input = props.input;\n }\n if (Array.isArray(props.subsets)) {\n data.subsets = props.subsets.map(\n (i) => new Uint8Array(new Uint32Array(i).buffer),\n );\n }\n return { ...props, ...data } as any as OriginInput;\n};\nexport const createAPI = <\n OriginCB extends (buffer: any, length: number) => void,\n>(\n font_split: (buffer: Uint8Array, length: number, cb: OriginCB) => void,\n createCallback: (cb: (data: Uint8Array) => void) => OriginCB,\n finallyFn?: () => void,\n) => {\n return async function fontSplit(config: FontSplitProps) {\n const midType = await transType(config);\n const input = api_interface.InputTemplate.fromObject(midType);\n if (!input.outDir) throw new Error('cn-font-split need outDir');\n const key = Math.random().toString().slice(2, 5);\n console.time('cn-font-split ' + key);\n let handles: Promise<void>[] = [];\n return new Promise<void>((res) => {\n const buf = input.serialize();\n const appCallback = (data: Uint8Array): void => {\n let e = api_interface.EventMessage.deserialize(data);\n switch (e.event) {\n case api_interface.EventName.END:\n res();\n break;\n case api_interface.EventName.OUTPUT_DATA:\n !config.silent && console.log(e.message);\n let handle = (config.outputFile || fs.outputFile)(\n path.join(input.outDir, e.message),\n e.data,\n );\n handles.push(handle);\n break;\n default:\n // console.log(e.event);\n }\n };\n font_split(buf as any, buf.length, createCallback(appCallback));\n })\n .then(async (res) => {\n await Promise.all(handles);\n return res;\n })\n .finally(() => {\n console.timeEnd('cn-font-split ' + key);\n finallyFn?.();\n });\n };\n};\n"],"mappings":";;;;;AAOA,IAAM,IAAY,OAAO,MAAgD;CACrE,IAAM,IAA6B,CAAC;CAWpC,OAVI,OAAO,EAAM,SAAU,WACvB,EAAK,QAAQ,MAAM,EAAG,SAAS,EAAM,KAAK,IAE1C,EAAK,QAAQ,EAAM,OAEnB,MAAM,QAAQ,EAAM,OAAO,MAC3B,EAAK,UAAU,EAAM,QAAQ,KACxB,MAAM,IAAI,WAAW,IAAI,YAAY,CAAC,EAAE,MAAM,CACnD,IAEG;EAAE,GAAG;EAAO,GAAG;CAAK;AAC/B,GACa,KAGT,GACA,GACA,MAEO,eAAyB,GAAwB;CACpD,IAAM,IAAU,MAAM,EAAU,CAAM,GAChC,IAAQ,EAAc,cAAc,WAAW,CAAO;CAC5D,IAAI,CAAC,EAAM,QAAQ,MAAU,MAAM,2BAA2B;CAC9D,IAAM,IAAM,KAAK,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,CAAC;CAC/C,QAAQ,KAAK,mBAAmB,CAAG;CACnC,IAAI,IAA2B,CAAC;CAChC,OAAO,IAAI,SAAe,MAAQ;EAC9B,IAAM,IAAM,EAAM,UAAU;EAmB5B,EAAW,GAAY,EAAI,QAAQ,GAlBd,MAA2B;GAC5C,IAAI,IAAI,EAAc,aAAa,YAAY,CAAI;GACnD,QAAQ,EAAE,OAAV;IACI,KAAK,EAAc,UAAU;KACzB,EAAI;KACJ;IACJ,KAAK,EAAc,UAAU;KACzB,CAAC,EAAO,UAAU,QAAQ,IAAI,EAAE,OAAO;KACvC,IAAI,KAAU,EAAO,cAAc,EAAG,YAClC,EAAK,KAAK,EAAM,QAAQ,EAAE,OAAO,GACjC,EAAE,IACN;KACA,EAAQ,KAAK,CAAM;KACnB;IACJ;GAEJ;EACJ,CAC6D,CAAC;CAClE,CAAC,EACI,KAAK,OAAO,OACT,MAAM,QAAQ,IAAI,CAAO,GAClB,EACV,EACA,cAAc;EAEX,AADA,QAAQ,QAAQ,mBAAmB,CAAG,GACtC,IAAY;CAChB,CAAC;AACT"}