{"version":3,"file":"createAPI.mjs","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"],"names":["transType","props","data","fs","createAPI","font_split","createCallback","finallyFn","config","midType","input","api_interface","key","handles","res","buf","appCallback","e","handle","path"],"mappings":";;;AAOA,MAAMA,IAAY,OAAOC,MAAgD;AACrE,QAAMC,IAA6B,CAAA;AACnC,SAAI,OAAOD,EAAM,SAAU,WACvBC,EAAK,QAAQ,MAAMC,EAAG,SAASF,EAAM,KAAK,IAE1CC,EAAK,QAAQD,EAAM,OAEnB,MAAM,QAAQA,EAAM,OAAO,MAC3BC,EAAK,UAAUD,EAAM,QAAQ;AAAA,IACzB,CAAC,MAAM,IAAI,WAAW,IAAI,YAAY,CAAC,EAAE,MAAM;AAAA,EAAA,IAGhD,EAAE,GAAGA,GAAO,GAAGC,EAAA;AAC1B,GACaE,IAAY,CAGrBC,GACAC,GACAC,MAEO,eAAyBC,GAAwB;AACpD,QAAMC,IAAU,MAAMT,EAAUQ,CAAM,GAChCE,IAAQC,EAAc,cAAc,WAAWF,CAAO;AAC5D,MAAI,CAACC,EAAM,OAAQ,OAAM,IAAI,MAAM,2BAA2B;AAC9D,QAAME,IAAM,KAAK,OAAA,EAAS,WAAW,MAAM,GAAG,CAAC;AAC/C,UAAQ,KAAK,mBAAmBA,CAAG;AACnC,MAAIC,IAA2B,CAAA;AAC/B,SAAO,IAAI,QAAc,CAACC,MAAQ;AAC9B,UAAMC,IAAML,EAAM,UAAA,GACZM,IAAc,CAACd,MAA2B;AAC5C,UAAIe,IAAIN,EAAc,aAAa,YAAYT,CAAI;AACnD,cAAQe,EAAE,OAAA;AAAA,QACN,KAAKN,EAAc,UAAU;AACzB,UAAAG,EAAA;AACA;AAAA,QACJ,KAAKH,EAAc,UAAU;AACzB,WAACH,EAAO,UAAU,QAAQ,IAAIS,EAAE,OAAO;AACvC,cAAIC,KAAUV,EAAO,cAAcL,EAAG;AAAA,YAClCgB,EAAK,KAAKT,EAAM,QAAQO,EAAE,OAAO;AAAA,YACjCA,EAAE;AAAA,UAAA;AAEN,UAAAJ,EAAQ,KAAKK,CAAM;AACnB;AAAA,MACJ;AAAA,IAGR;AACA,IAAAb,EAAWU,GAAYA,EAAI,QAAQT,EAAeU,CAAW,CAAC;AAAA,EAClE,CAAC,EACI,KAAK,OAAOF,OACT,MAAM,QAAQ,IAAID,CAAO,GAClBC,EACV,EACA,QAAQ,MAAM;AACX,YAAQ,QAAQ,mBAAmBF,CAAG,GACtCL,IAAA;AAAA,EACJ,CAAC;AACT;"}