Skip to content

Commit

Permalink
add support options to ttfwriter
Browse files Browse the repository at this point in the history
  • Loading branch information
kekee000 committed Nov 9, 2019
1 parent 2a493d3 commit a052130
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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.0.6",
"version": "2.0.7",
"description": "fonts (ttf, woff, woff2, eot, svg, otf) parse, write, transform, glyph adjust.",
"keywords": [
"sfnt",
Expand Down
33 changes: 33 additions & 0 deletions src/ttf/table/OS2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
5 changes: 3 additions & 2 deletions src/ttf/ttfwriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 // 自定义的导出表结构,可以自己修改某些表项目
};
}

Expand Down Expand Up @@ -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;
Expand Down
61 changes: 61 additions & 0 deletions test/spec/ttf/ttfwriter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a052130

Please sign in to comment.