From a05213020a90539f0ab659d697fbea920c94af53 Mon Sep 17 00:00:00 2001 From: kekee000 Date: Sat, 9 Nov 2019 22:30:39 +0800 Subject: [PATCH] add support options to ttfwriter --- README.md | 1 + package.json | 2 +- src/ttf/table/OS2.js | 33 ++++++++++++++++++ src/ttf/ttfwriter.js | 5 +-- test/spec/ttf/ttfwriter.spec.js | 61 +++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 73d1b5d..1977a7d 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ let buffer = font.write({ type: 'woff', // support ttf, woff, woff2, eot, svg hinting: true, // save font hinting deflate: null, // deflate function for woff + support: {head: {}, hhea: {}} // for user to overwrite head.xMin, head.xMax, head.yMin, head.yMax, hhea etc. }); // fs.writeFileSync('font.woff', buffer); diff --git a/package.json b/package.json index 88a66a9..90212f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fonteditor-core", - "version": "2.0.6", + "version": "2.0.7", "description": "fonts (ttf, woff, woff2, eot, svg, otf) parse, write, transform, glyph adjust.", "keywords": [ "sfnt", diff --git a/src/ttf/table/OS2.js b/src/ttf/table/OS2.js index b89d480..8c8ac99 100644 --- a/src/ttf/table/OS2.js +++ b/src/ttf/table/OS2.js @@ -265,6 +265,39 @@ export default table.create( ttf.head.xMax = xMax; ttf.head.yMax = yMax; + // head rewrite + if (ttf.support.head) { + let {xMin, yMin, xMax, yMax} = ttf.support.head; + if (xMin != null) { + ttf.head.xMin = xMin; + } + if (yMin != null) { + ttf.head.yMin = yMin; + } + if (xMax != null) { + ttf.head.xMax = xMax; + } + if (yMax != null) { + ttf.head.yMax = yMax; + } + + } + // hhea rewrite + if (ttf.support.hhea) { + let {advanceWidthMax, xMaxExtent, minLeftSideBearing, minRightSideBearing} = ttf.support.hhea; + if (advanceWidthMax != null) { + ttf.hhea.advanceWidthMax = advanceWidthMax; + } + if (xMaxExtent != null) { + ttf.hhea.xMaxExtent = xMaxExtent; + } + if (minLeftSideBearing != null) { + ttf.hhea.minLeftSideBearing = minLeftSideBearing; + } + if (minRightSideBearing != null) { + ttf.hhea.minRightSideBearing = minRightSideBearing; + } + } // 这里根据存储的maxp来设置新的maxp,避免重复计算maxp ttf.maxp = ttf.maxp || {}; ttf.support.maxp = { diff --git a/src/ttf/ttfwriter.js b/src/ttf/ttfwriter.js index 246a26e..68a34ed 100644 --- a/src/ttf/ttfwriter.js +++ b/src/ttf/ttfwriter.js @@ -26,7 +26,8 @@ const SUPPORT_TABLES = [ export default class TTFWriter { constructor(options = {}) { this.options = { - hinting: options.hinting || false // 不保留hints信息 + hinting: options.hinting || false, // 不保留hints信息 + support: options.support // 自定义的导出表结构,可以自己修改某些表项目 }; } @@ -96,7 +97,7 @@ export default class TTFWriter { dump(ttf) { // 用来做写入缓存的对象,用完后删掉 - ttf.support = {}; + ttf.support = Object.assign({}, this.options.support); // head + directory let ttfSize = 12 + ttf.numTables * 16; diff --git a/test/spec/ttf/ttfwriter.spec.js b/test/spec/ttf/ttfwriter.spec.js index c02041f..eea05c4 100644 --- a/test/spec/ttf/ttfwriter.spec.js +++ b/test/spec/ttf/ttfwriter.spec.js @@ -49,6 +49,67 @@ describe('写ttf数据', function () { }); + it('test write ttf with support', function () { + let buffer = new TTFWriter({ + support: { + head: { + xMin: 1, + yMin: -30, + xMax: 610, + yMax: 485, + minLeftSideBearing: 1, + minRightSideBearing: 1 + }, + hhea: { + advanceWidthMax: 1000, + xMaxExtent: 1000, + minLeftSideBearing: 1, + minRightSideBearing: 1 + } + } + }).write(fontObject); + + assert.ok(buffer.byteLength > 1000); + assert.ok(buffer.byteLength < 10000); + + let ttf = new TTFReader().read(buffer); + + assert.equal(ttf.version, 1); + + assert.equal(ttf.head.magickNumber, 1594834165); + assert.equal(ttf.head.unitsPerEm, 512); + + assert.equal(ttf.post.format, 2); + assert.equal(ttf.post.underlinePosition, 0); + assert.equal(ttf.post.underlineThickness, 0); + + assert.equal(ttf.hhea.ascent, 480); + assert.equal(ttf.hhea.descent, -33); + + assert.equal(ttf.maxp.version, 1); + assert.equal(ttf.maxp.numGlyphs, 17); + + assert.equal(ttf.glyf[0].advanceWidth, 512); + assert.equal(ttf.glyf[0].leftSideBearing, 0); + assert.equal(ttf.glyf[0].name, '.notdef'); + assert.equal(ttf.glyf[3].contours[0].length, 31); + assert.equal(ttf.glyf[16].compound, true); + assert.equal(ttf.glyf[16].glyfs.length, 2); + + assert.equal(ttf.cmap[0], 1); + assert.equal(ttf.cmap[57400], 16); + assert.equal(+ttf.head.created === +fontObject.head.created, true); + assert.equal(+ttf.head.modified === +fontObject.head.modified, true); + assert.equal(ttf.head.xMin, 1); + assert.equal(ttf.head.yMin, -30); + assert.equal(ttf.head.xMax, 610); + assert.equal(ttf.head.yMax, 485); + assert.equal(ttf.hhea.advanceWidthMax, 1000); + assert.equal(ttf.hhea.xMaxExtent, 1000); + assert.equal(ttf.hhea.minLeftSideBearing, 1); + assert.equal(ttf.hhea.minRightSideBearing, 1); + }); + it('test write ttf error', function () { assert.throws(function () { let ttf = Object.assign({}, fontObject);