const fs = require('fs');
const { createCanvas, registerFont } = require('canvas');
registerFont(env.url('iconfont/iconfont.ttf'), { family: 'iconfont' });
for (let v of (await env.file.readAllText('iconfont/demo_index.html', 'utf-8')).match(/&#x\w+;/g)) {
let num = parseInt(v.substr(3), 16);
let char = String.fromCharCode(num);
let canvas = createCanvas(72, 72);
let ctx = canvas.getContext('2d');
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#000';
ctx.font = '36px iconfont';
let size = ctx.measureText(char);
ctx.fillText(char, (canvas.width - size.width) / 2, (canvas.height - size.width) / 2 + size.width);
let out = fs.createWriteStream(env.url(`iconfont/images/${num}.png`));
canvas.createPNGStream().pipe(out);
}
node-canvas使用自定义字体
最新推荐文章于 2024-01-26 18:26:56 发布