From 00f1c3844eebc24f712d8510f478a48a89d05710 Mon Sep 17 00:00:00 2001 From: kekee000 Date: Mon, 4 Mar 2024 17:11:53 +0800 Subject: [PATCH] feat: add svg parsed Document as input --- README.md | 4 +++- index.d.ts | 10 ++++++---- package.json | 2 +- src/ttf/font.js | 6 +++--- src/ttf/svg2ttfobject.js | 12 ++++++------ test/node-spec/svg2ttf.spec.js | 19 +++++++++++++++++-- test/spec/ttf/font.spec.js | 22 ++++++++++++++++++++++ 7 files changed, 58 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f3cd3f2..d172e9c 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,9 @@ import {Font} from 'fonteditor-core'; import fs from 'fs'; const buffer = fs.readFileSync('font.ttf'); -// read font data, support ArrayBuffer | Buffer | string +// read font data, support format: +// - for ttf, otf, woff, woff2, support ArrayBuffer, Buffer +// - for svg, support string or Document(parsed svg) const font = Font.create(buffer, { // support ttf, woff, woff2, eot, otf, svg type: 'ttf', diff --git a/index.d.ts b/index.d.ts index 16f1406..9fd5519 100644 --- a/index.d.ts +++ b/index.d.ts @@ -176,8 +176,8 @@ export namespace FontEditor { type FontType = 'ttf' | 'otf' | 'eot' | 'woff' | 'woff2' | 'svg'; - type FontInput = ArrayBuffer | Buffer | string; - type FontOutput = FontInput; + type FontInput = ArrayBuffer | Buffer | string | Document; + type FontOutput = ArrayBuffer | Buffer | string; type UInt8 = number; @@ -335,7 +335,9 @@ export namespace FontEditor { /** * create font object with font data * - * @param buffer font data, support format: ArrayBuffer, Buffer, string + * @param buffer font data, support format + * - for ttf, otf, woff, woff2, support ArrayBuffer, Buffer + * - for svg, support string or Document(parsed svg) * @param options font read options */ static create(buffer: FontInput, options: FontReadOptions): Font; @@ -355,7 +357,7 @@ export namespace FontEditor { /** * read font data * - * @param buffer font data, support format: ArrayBuffer, Buffer, string + * @param buffer font data, support format: ArrayBuffer, Buffer, string, Document(parsed svg) * @param options font read options */ read(buffer: FontInput, options: FontReadOptions): Font; diff --git a/package.json b/package.json index 46729ab..aed5dac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fonteditor-core", - "version": "2.3.2", + "version": "2.3.3", "description": "fonts (ttf, woff, woff2, eot, svg, otf) parse, write, transform, glyph adjust.", "keywords": [ "sfnt", diff --git a/src/ttf/font.js b/src/ttf/font.js index 6a73e74..69c5b0e 100644 --- a/src/ttf/font.js +++ b/src/ttf/font.js @@ -43,7 +43,7 @@ export default class Font { /** * 字体对象构造函数 * - * @param {ArrayBuffer|Buffer|string} buffer 字体数据 + * @param {ArrayBuffer|Buffer|string|Document} buffer 字体数据 * @param {Object} options 读取参数 */ constructor(buffer, options = {type: 'ttf'}) { @@ -74,7 +74,7 @@ export default class Font { /** * 读取字体数据 * - * @param {ArrayBuffer|Buffer|string} buffer 字体数据 + * @param {ArrayBuffer|Buffer|string|Document} buffer 字体数据 * @param {Object} options 读取参数 * @param {string} options.type 字体类型 * @@ -337,7 +337,7 @@ export default class Font { /** * 读取字体数据返回字体对象 * - * @param {ArrayBuffer|Buffer|string} buffer 字体数据 + * @param {ArrayBuffer|Buffer|string|Document} buffer 字体数据 * @param {Object} options 读取参数 * @return {Font} */ diff --git a/src/ttf/svg2ttfobject.js b/src/ttf/svg2ttfobject.js index 8924751..2b693f8 100644 --- a/src/ttf/svg2ttfobject.js +++ b/src/ttf/svg2ttfobject.js @@ -18,7 +18,7 @@ import reduceGlyf from './util/reduceGlyf'; * 加载xml字符串 * * @param {string} xml xml字符串 - * @return {XMLDocument} + * @return {Document} */ function loadXML(xml) { if (DOMParser) { @@ -170,7 +170,7 @@ function resolve(ttf) { /** * 解析字体信息相关节点 * - * @param {XMLDocument} xmlDoc XML文档对象 + * @param {Document} xmlDoc XML文档对象 * @param {Object} ttf ttf对象 * @return {Object} ttf对象 */ @@ -235,7 +235,7 @@ function parseFont(xmlDoc, ttf) { /** * 解析字体信息相关节点 * - * @param {XMLDocument} xmlDoc XML文档对象 + * @param {Document} xmlDoc XML文档对象 * @param {Object} ttf ttf对象 * @return {Object} ttf对象 */ @@ -315,7 +315,7 @@ function parseGlyf(xmlDoc, ttf) { /** * 解析字体信息相关节点 * - * @param {XMLDocument} xmlDoc XML文档对象 + * @param {Document} xmlDoc XML文档对象 * @param {Object} ttf ttf对象 */ function parsePath(xmlDoc, ttf) { @@ -355,7 +355,7 @@ function parsePath(xmlDoc, ttf) { /** * 解析xml文档 * - * @param {XMLDocument} xmlDoc XML文档对象 + * @param {Document} xmlDoc XML文档对象 * @param {Object} options 导入选项 * * @return {Object} 解析后对象 @@ -414,7 +414,7 @@ function parseXML(xmlDoc, options) { /** * svg格式转ttfObject格式 * - * @param {string} svg svg格式 + * @param {string|Document} svg svg格式 * @param {Object=} options 导入选项 * @param {boolean} options.combinePath 是否合并成单个字形,仅限于普通svg导入 * @return {Object} ttfObject diff --git a/test/node-spec/svg2ttf.spec.js b/test/node-spec/svg2ttf.spec.js index 3c0fc95..b4b12d1 100644 --- a/test/node-spec/svg2ttf.spec.js +++ b/test/node-spec/svg2ttf.spec.js @@ -7,6 +7,7 @@ const fs = require('fs'); const TTFWriter = require('./fonteditor-core').TTFWriter; const svg2ttfobject = require('./fonteditor-core').svg2ttfobject; const util = require('./util'); +const DOMParser = require('@xmldom/xmldom').DOMParser; function getEmpty() { let data = fs.readFileSync(__dirname + '/empty.json'); @@ -16,9 +17,23 @@ function getEmpty() { describe('svg2ttf', function () { it('svg2ttf', function () { - let svg = fs.readFileSync(__dirname + '/../data/iconmoon.svg'); + let svg = fs.readFileSync(__dirname + '/../data/iconmoon.svg', 'utf-8'); let emptyTTFObject = getEmpty(); - let ttfObject = svg2ttfobject(String(svg)); + let ttfObject = svg2ttfobject(svg); + assert.strictEqual(ttfObject.glyf.length, 3, 'glyf length'); + emptyTTFObject.glyf = ttfObject.glyf; + let ttfBuffer = new TTFWriter().write(emptyTTFObject); + // test + assert.ok(util.toBuffer(ttfBuffer).length, 'test svg2ttf'); + }); + + it('xmldocument to ttf', function () { + + const svgText = fs.readFileSync(__dirname + '/../data/iconmoon.svg', 'utf-8'); + const doc = new DOMParser().parseFromString(svgText, 'text/xml'); + let emptyTTFObject = getEmpty(); + let ttfObject = svg2ttfobject(doc); + assert.strictEqual(ttfObject.glyf.length, 3, 'glyf length'); emptyTTFObject.glyf = ttfObject.glyf; let ttfBuffer = new TTFWriter().write(emptyTTFObject); // test diff --git a/test/spec/ttf/font.spec.js b/test/spec/ttf/font.spec.js index bcc35f4..cb478c4 100644 --- a/test/spec/ttf/font.spec.js +++ b/test/spec/ttf/font.spec.js @@ -7,6 +7,7 @@ import assert from 'assert'; import {readData} from '../data'; import Font from 'fonteditor-core/ttf/font'; import main from 'fonteditor-core/main'; +import {DOMParser} from '@xmldom/xmldom'; describe('test Font Class ============================', function () { @@ -148,6 +149,27 @@ describe('read svg font text', function () { }); +describe('read svg font from xmldocument', function () { + const doc = new DOMParser().parseFromString(readData('icomoon.svg'), 'text/xml'); + let font = Font.create(doc, { + type: 'svg' + }); + it('test read svg font', function () { + assert.equal(font.data.from, 'svgfont'); + assert.equal(font.data.id, 'icomoon'); + assert.equal(font.data.name.fontFamily, 'icomoon'); + assert.equal(font.data.metadata, 'Generated by IcoMoon'); + }); + + it('test svg font glyf', function () { + assert.equal(font.data.glyf.length, 3); + assert.equal(font.data.glyf[2].leftSideBearing, 0); + assert.equal(font.data.glyf[2].advanceWidth, 1024); + assert.equal(font.data.glyf[2].contours.length, 7); + assert.equal(font.data.glyf[2].unicode[0], 57345); + }); +}); + describe('write ttf buffer', function () {