Skip to content

Commit

Permalink
feat: add svg parsed Document as input
Browse files Browse the repository at this point in the history
  • Loading branch information
kekee000 committed Mar 4, 2024
1 parent 9b5e312 commit 00f1c38
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 17 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 6 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 3 additions & 3 deletions src/ttf/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}) {
Expand Down Expand Up @@ -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 字体类型
*
Expand Down Expand Up @@ -337,7 +337,7 @@ export default class Font {
/**
* 读取字体数据返回字体对象
*
* @param {ArrayBuffer|Buffer|string} buffer 字体数据
* @param {ArrayBuffer|Buffer|string|Document} buffer 字体数据
* @param {Object} options 读取参数
* @return {Font}
*/
Expand Down
12 changes: 6 additions & 6 deletions src/ttf/svg2ttfobject.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import reduceGlyf from './util/reduceGlyf';
* 加载xml字符串
*
* @param {string} xml xml字符串
* @return {XMLDocument}
* @return {Document}
*/
function loadXML(xml) {
if (DOMParser) {
Expand Down Expand Up @@ -170,7 +170,7 @@ function resolve(ttf) {
/**
* 解析字体信息相关节点
*
* @param {XMLDocument} xmlDoc XML文档对象
* @param {Document} xmlDoc XML文档对象
* @param {Object} ttf ttf对象
* @return {Object} ttf对象
*/
Expand Down Expand Up @@ -235,7 +235,7 @@ function parseFont(xmlDoc, ttf) {
/**
* 解析字体信息相关节点
*
* @param {XMLDocument} xmlDoc XML文档对象
* @param {Document} xmlDoc XML文档对象
* @param {Object} ttf ttf对象
* @return {Object} ttf对象
*/
Expand Down Expand Up @@ -315,7 +315,7 @@ function parseGlyf(xmlDoc, ttf) {
/**
* 解析字体信息相关节点
*
* @param {XMLDocument} xmlDoc XML文档对象
* @param {Document} xmlDoc XML文档对象
* @param {Object} ttf ttf对象
*/
function parsePath(xmlDoc, ttf) {
Expand Down Expand Up @@ -355,7 +355,7 @@ function parsePath(xmlDoc, ttf) {
/**
* 解析xml文档
*
* @param {XMLDocument} xmlDoc XML文档对象
* @param {Document} xmlDoc XML文档对象
* @param {Object} options 导入选项
*
* @return {Object} 解析后对象
Expand Down Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions test/node-spec/svg2ttf.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions test/spec/ttf/font.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {

Expand Down Expand Up @@ -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 () {

Expand Down

0 comments on commit 00f1c38

Please sign in to comment.