我想知道如何在 node-canvas 中使用自定义字体。
这是我的尝试,但到目前为止它不起作用:
var Canvas = require('canvas')
, Image = Canvas.Image
, Font = Canvas.Font
, path = require('path');
var gubblebum = new Font('gubblebum', fontFile('GUBBLO___.ttf'));
function fontFile(name) {
return path.join(__dirname, '../fonts', name);
}
function draw() {
var canvas = new Canvas(100, 100)
, ctx = canvas.getContext('2d');
ctx.addFont(gubblebum);
ctx.font = 'bold 40 gubblebum';
ctx.fillText('test', 25, 45);
return canvas;
}
module.exports = draw;
在浏览器中呈现时,字体和大小保持不变。
字体文件已正确加载。否则会抛出异常。
有趣的是,当我做 ctx.font = 'bold 40 someGibberish'; 字体大小应用正确。