Skip to content

Commit 00f1c38

Browse files
committed
feat: add svg parsed Document as input
1 parent 9b5e312 commit 00f1c38

File tree

7 files changed

+58
-17
lines changed

7 files changed

+58
-17
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ import {Font} from 'fonteditor-core';
2828
import fs from 'fs';
2929

3030
const buffer = fs.readFileSync('font.ttf');
31-
// read font data, support ArrayBuffer | Buffer | string
31+
// read font data, support format:
32+
// - for ttf, otf, woff, woff2, support ArrayBuffer, Buffer
33+
// - for svg, support string or Document(parsed svg)
3234
const font = Font.create(buffer, {
3335
// support ttf, woff, woff2, eot, otf, svg
3436
type: 'ttf',

index.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ export namespace FontEditor {
176176

177177
type FontType = 'ttf' | 'otf' | 'eot' | 'woff' | 'woff2' | 'svg';
178178

179-
type FontInput = ArrayBuffer | Buffer | string;
180-
type FontOutput = FontInput;
179+
type FontInput = ArrayBuffer | Buffer | string | Document;
180+
type FontOutput = ArrayBuffer | Buffer | string;
181181

182182
type UInt8 = number;
183183

@@ -335,7 +335,9 @@ export namespace FontEditor {
335335
/**
336336
* create font object with font data
337337
*
338-
* @param buffer font data, support format: ArrayBuffer, Buffer, string
338+
* @param buffer font data, support format
339+
* - for ttf, otf, woff, woff2, support ArrayBuffer, Buffer
340+
* - for svg, support string or Document(parsed svg)
339341
* @param options font read options
340342
*/
341343
static create(buffer: FontInput, options: FontReadOptions): Font;
@@ -355,7 +357,7 @@ export namespace FontEditor {
355357
/**
356358
* read font data
357359
*
358-
* @param buffer font data, support format: ArrayBuffer, Buffer, string
360+
* @param buffer font data, support format: ArrayBuffer, Buffer, string, Document(parsed svg)
359361
* @param options font read options
360362
*/
361363
read(buffer: FontInput, options: FontReadOptions): Font;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fonteditor-core",
3-
"version": "2.3.2",
3+
"version": "2.3.3",
44
"description": "fonts (ttf, woff, woff2, eot, svg, otf) parse, write, transform, glyph adjust.",
55
"keywords": [
66
"sfnt",

src/ttf/font.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default class Font {
4343
/**
4444
* 字体对象构造函数
4545
*
46-
* @param {ArrayBuffer|Buffer|string} buffer 字体数据
46+
* @param {ArrayBuffer|Buffer|string|Document} buffer 字体数据
4747
* @param {Object} options 读取参数
4848
*/
4949
constructor(buffer, options = {type: 'ttf'}) {
@@ -74,7 +74,7 @@ export default class Font {
7474
/**
7575
* 读取字体数据
7676
*
77-
* @param {ArrayBuffer|Buffer|string} buffer 字体数据
77+
* @param {ArrayBuffer|Buffer|string|Document} buffer 字体数据
7878
* @param {Object} options 读取参数
7979
* @param {string} options.type 字体类型
8080
*
@@ -337,7 +337,7 @@ export default class Font {
337337
/**
338338
* 读取字体数据返回字体对象
339339
*
340-
* @param {ArrayBuffer|Buffer|string} buffer 字体数据
340+
* @param {ArrayBuffer|Buffer|string|Document} buffer 字体数据
341341
* @param {Object} options 读取参数
342342
* @return {Font}
343343
*/

src/ttf/svg2ttfobject.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import reduceGlyf from './util/reduceGlyf';
1818
* 加载xml字符串
1919
*
2020
* @param {string} xml xml字符串
21-
* @return {XMLDocument}
21+
* @return {Document}
2222
*/
2323
function loadXML(xml) {
2424
if (DOMParser) {
@@ -170,7 +170,7 @@ function resolve(ttf) {
170170
/**
171171
* 解析字体信息相关节点
172172
*
173-
* @param {XMLDocument} xmlDoc XML文档对象
173+
* @param {Document} xmlDoc XML文档对象
174174
* @param {Object} ttf ttf对象
175175
* @return {Object} ttf对象
176176
*/
@@ -235,7 +235,7 @@ function parseFont(xmlDoc, ttf) {
235235
/**
236236
* 解析字体信息相关节点
237237
*
238-
* @param {XMLDocument} xmlDoc XML文档对象
238+
* @param {Document} xmlDoc XML文档对象
239239
* @param {Object} ttf ttf对象
240240
* @return {Object} ttf对象
241241
*/
@@ -315,7 +315,7 @@ function parseGlyf(xmlDoc, ttf) {
315315
/**
316316
* 解析字体信息相关节点
317317
*
318-
* @param {XMLDocument} xmlDoc XML文档对象
318+
* @param {Document} xmlDoc XML文档对象
319319
* @param {Object} ttf ttf对象
320320
*/
321321
function parsePath(xmlDoc, ttf) {
@@ -355,7 +355,7 @@ function parsePath(xmlDoc, ttf) {
355355
/**
356356
* 解析xml文档
357357
*
358-
* @param {XMLDocument} xmlDoc XML文档对象
358+
* @param {Document} xmlDoc XML文档对象
359359
* @param {Object} options 导入选项
360360
*
361361
* @return {Object} 解析后对象
@@ -414,7 +414,7 @@ function parseXML(xmlDoc, options) {
414414
/**
415415
* svg格式转ttfObject格式
416416
*
417-
* @param {string} svg svg格式
417+
* @param {string|Document} svg svg格式
418418
* @param {Object=} options 导入选项
419419
* @param {boolean} options.combinePath 是否合并成单个字形,仅限于普通svg导入
420420
* @return {Object} ttfObject

test/node-spec/svg2ttf.spec.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const fs = require('fs');
77
const TTFWriter = require('./fonteditor-core').TTFWriter;
88
const svg2ttfobject = require('./fonteditor-core').svg2ttfobject;
99
const util = require('./util');
10+
const DOMParser = require('@xmldom/xmldom').DOMParser;
1011

1112
function getEmpty() {
1213
let data = fs.readFileSync(__dirname + '/empty.json');
@@ -16,9 +17,23 @@ function getEmpty() {
1617
describe('svg2ttf', function () {
1718
it('svg2ttf', function () {
1819

19-
let svg = fs.readFileSync(__dirname + '/../data/iconmoon.svg');
20+
let svg = fs.readFileSync(__dirname + '/../data/iconmoon.svg', 'utf-8');
2021
let emptyTTFObject = getEmpty();
21-
let ttfObject = svg2ttfobject(String(svg));
22+
let ttfObject = svg2ttfobject(svg);
23+
assert.strictEqual(ttfObject.glyf.length, 3, 'glyf length');
24+
emptyTTFObject.glyf = ttfObject.glyf;
25+
let ttfBuffer = new TTFWriter().write(emptyTTFObject);
26+
// test
27+
assert.ok(util.toBuffer(ttfBuffer).length, 'test svg2ttf');
28+
});
29+
30+
it('xmldocument to ttf', function () {
31+
32+
const svgText = fs.readFileSync(__dirname + '/../data/iconmoon.svg', 'utf-8');
33+
const doc = new DOMParser().parseFromString(svgText, 'text/xml');
34+
let emptyTTFObject = getEmpty();
35+
let ttfObject = svg2ttfobject(doc);
36+
assert.strictEqual(ttfObject.glyf.length, 3, 'glyf length');
2237
emptyTTFObject.glyf = ttfObject.glyf;
2338
let ttfBuffer = new TTFWriter().write(emptyTTFObject);
2439
// test

test/spec/ttf/font.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import assert from 'assert';
77
import {readData} from '../data';
88
import Font from 'fonteditor-core/ttf/font';
99
import main from 'fonteditor-core/main';
10+
import {DOMParser} from '@xmldom/xmldom';
1011

1112
describe('test Font Class ============================', function () {
1213

@@ -148,6 +149,27 @@ describe('read svg font text', function () {
148149
});
149150

150151

152+
describe('read svg font from xmldocument', function () {
153+
const doc = new DOMParser().parseFromString(readData('icomoon.svg'), 'text/xml');
154+
let font = Font.create(doc, {
155+
type: 'svg'
156+
});
157+
it('test read svg font', function () {
158+
assert.equal(font.data.from, 'svgfont');
159+
assert.equal(font.data.id, 'icomoon');
160+
assert.equal(font.data.name.fontFamily, 'icomoon');
161+
assert.equal(font.data.metadata, 'Generated by IcoMoon');
162+
});
163+
164+
it('test svg font glyf', function () {
165+
assert.equal(font.data.glyf.length, 3);
166+
assert.equal(font.data.glyf[2].leftSideBearing, 0);
167+
assert.equal(font.data.glyf[2].advanceWidth, 1024);
168+
assert.equal(font.data.glyf[2].contours.length, 7);
169+
assert.equal(font.data.glyf[2].unicode[0], 57345);
170+
});
171+
});
172+
151173

152174
describe('write ttf buffer', function () {
153175

0 commit comments

Comments
 (0)