Files
NapCatQQ/.pnpm-store/v3/files/dc/8c7cb4162d0273d3cefc9867a77ade36f99787c020905643a75015df54ab6a97363e3bafac0f810ee3cfd5d078cafd512ccd90dba2b6670da8cf3758532717
TanTan e311ca3275
Build NapCat Artifacts / Windows Loader Test (push) Canceled after 0s
Build NapCat Artifacts / Type Check & Test (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Framework (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Shell (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

106 lines
2.9 KiB
Plaintext

/**
* @file woff2 wasm build of google woff2
* thanks to woff2-asm
* https://github.com/alimilhim/woff2-wasm
* @author mengke01(kekee000@gmail.com)
*/
// Require the woff2 module
const woff2ModuleLoader = require('./woff2');
function convertFromVecToUint8Array(vector) {
const arr = [];
for (let i = 0, l = vector.size(); i < l; i++) {
arr.push(vector.get(i));
}
return new Uint8Array(arr);
}
// Define as a named object that can be exported with CommonJS
const woff2Module = {
woff2Module: null,
/**
* 是否已经加载完毕
*
* @return {boolean}
*/
isInited() {
return (
this.woff2Module && this.woff2Module.woff2Enc && this.woff2Module.woff2Dec
);
},
/**
* 初始化 woff 模块
*
* @param {string|ArrayBuffer} wasmUrl woff2.wasm file url
* @return {Promise}
*/
init(wasmUrl) {
return new Promise((resolve) => {
if (this.woff2Module) {
resolve(this);
return;
}
let moduleLoaderConfig = null;
if (typeof window !== 'undefined') {
moduleLoaderConfig = {
locateFile(path) {
if (path.endsWith('.wasm')) {
return wasmUrl;
}
return path;
},
};
}
// for nodejs
else {
// Use path resolution that works in both ESM and CommonJS
let wasmPath = './woff2.wasm';
// If running in Node.js with __dirname available (CommonJS)
if (typeof __dirname !== 'undefined') {
wasmPath = __dirname + '/woff2.wasm';
}
moduleLoaderConfig = {
wasmBinaryFile: wasmPath,
};
}
const woffModule = woff2ModuleLoader(moduleLoaderConfig);
woffModule.onRuntimeInitialized = () => {
this.woff2Module = woffModule;
resolve(this);
};
});
},
/**
* 将ttf buffer 转换成 woff2 buffer
*
* @param {ArrayBuffer|Buffer|Array} ttfBuffer ttf buffer
* @return {Uint8Array} uint8 array
*/
encode(ttfBuffer) {
const buffer = new Uint8Array(ttfBuffer);
const woffbuff = this.woff2Module.woff2Enc(buffer, buffer.byteLength);
return convertFromVecToUint8Array(woffbuff);
},
/**
* 将woff2 buffer 转换成 ttf buffer
*
* @param {ArrayBuffer|Buffer|Array} woff2Buffer woff2 buffer
* @return {Uint8Array} uint8 array
*/
decode(woff2Buffer) {
const buffer = new Uint8Array(woff2Buffer);
const ttfbuff = this.woff2Module.woff2Dec(buffer, buffer.byteLength);
return convertFromVecToUint8Array(ttfbuff);
},
};
// Export for CommonJS
module.exports = woff2Module;