Files
NapCatQQ/.pnpm-store/v3/files/c7/9ba5d05033efb36c234ade7e898b71487940589a4ec05c4a57c9f8e0ab33d1fd9e5528c83ab29c095b63ad01ba205d70c29fe545f4ec3be4dac22abaf56b18
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

83 lines
1.5 KiB
Plaintext

/**
* @file ttf2svg
* @author junmer
*/
/* eslint-env node */
var isTtf = require('is-ttf');
var through = require('through2');
var ttf2svg = require('fonteditor-core').ttf2svg;
var b2ab = require('b3b').b2ab;
var replaceExt = require('replace-ext');
var _ = require('lodash');
function compileTtf(buffer, cb) {
var output;
try {
output = Buffer.from(ttf2svg(b2ab(buffer)));
}
catch (ex) {
cb(ex);
}
output && cb(null, output);
}
/**
* ttf2svg fontmin plugin
*
* @param {Object} opts opts
* @return {Object} stream.Transform instance
* @api public
*/
module.exports = function (opts) {
opts = _.extend({clone: true}, opts);
return through.ctor({
objectMode: true
}, function (file, enc, cb) {
// check null
if (file.isNull()) {
cb(null, file);
return;
}
// check stream
if (file.isStream()) {
cb(new Error('Streaming is not supported'));
return;
}
// check ttf
if (!isTtf(file.contents)) {
cb(null, file);
return;
}
// clone
if (opts.clone) {
this.push(file.clone(false));
}
// replace ext
file.path = replaceExt(file.path, '.svg');
compileTtf(file.contents, function (err, buffer) {
if (err) {
cb(err);
return;
}
file.contents = buffer;
cb(null, file);
});
});
};