kekee000 / fonteditor-core

fonteditor core functions
MIT License
353 stars 67 forks source link

SVG文件要求读取格式? #33

Closed StringKe closed 4 years ago

StringKe commented 4 years ago
const font = require('fonteditor-core/lib/ttf/font').default;
const TTF = require('fonteditor-core/lib/ttf/ttf').default;
const glyfAdjust = require('fonteditor-core/lib/ttf/util/glyfAdjust').default;
const fs = require('fs');

const baseFont = font.create(fs.readFileSync('font.ttf'));
let fontObj = new TTF(baseFont.data);
let writeSvgString = '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 1000 1000"><rect y="0" x="0" width="1000" height="1000" fill="#000"/></svg>'

function svg2ttf(buffer) {
    let options = {combinePath: true,};
    options.type = 'svg';
    return font.create(buffer, options).data;
}

function loadSVGFile(file) {
    let buffer = Buffer.from(file);
    let imported = svg2ttf(buffer);
    if (imported.glyf && imported.glyf.length === 1) {
        imported.glyf[0].name = "fullBlock";
    }
    merge(imported, {
        scale: true,
        adjustGlyf: imported.from === 'svg'
    })
}

function merge(imported, options) {
    let list = fontObj.mergeGlyf(imported, options);
    if (list.length) {
        list.forEach(function (g) {
            g.modify = 'new';
        });
    }
}

loadSVGFile(writeSvgString)

出现错误,部分逻辑是看 fonteditor 来的

E:\company\design-fonts\node_modules\fonteditor-core\lib\ttf\svg2ttfobject.js:364
  if (!xmlDoc.getElementsByTagName('svg').length) {
              ^

TypeError: xmlDoc.getElementsByTagName is not a function
kekee000 commented 4 years ago

svg 是svg 格式的字体,具体参考: https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/SVG_fonts