将Google字体路径转换为SVG路径

时间:2020-07-13 09:33:59

标签: javascript svg opentype

我正在尝试按照https://github.com/danmarshall/google-font-to-svg-path上的示例进行渲染,以将Google字体创建为svg。我的代码如下:

opentype.load('https://fonts.googleapis.com/css2?family=' + 'ABeeZee', (err, font) => {
  const textModel = new makerjs.models.Text(font, 'Test', 20, false, false, 1);
  const svg = makerjs.exporter.toSVG(textModel);
  console.log(svg);
});

我不断收到错误消息:ERROR TypeError: Cannot read property 'forEachGlyph' of null,该如何解决?我的代码基于此处的示例:https://github.com/danmarshall/google-font-to-svg-path/blob/master/index.ts#L73-L97

1 个答案:

答案 0 :(得分:1)

我如下解决了该问题:

const url = 'http://fonts.gstatic.com/s/quicksand/v21/6xK-dSZaM9iE8KbpRA_LJ3z8mH9BOJvgkKEo18G0wx40QDw.ttf';
const text = 'hello';
const size = 14;
const union = false;
const bezierAccuracy = 0;
let svg = null;
opentype.load(url, (err, font) => {
  const textModel = new makerjs.models.Text(font, text, size, union, false, bezierAccuracy);
  svg = makerjs.exporter.toSVG(textModel);
});
return svg;
相关问题