From bf054a15075171bb0b72620004ea02c0c28f6ee1 Mon Sep 17 00:00:00 2001 From: Olli Meier Date: Mon, 17 Apr 2023 12:19:27 +0200 Subject: [PATCH] includeTable for subset Adding new functionality to keep specific tables if specified via includeTable() --- src/subset/Subset.js | 8 ++++++++ src/subset/TTFSubset.js | 37 +++++++++++++++++++------------------ test/subset.js | 13 +++++++++++++ 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/subset/Subset.js b/src/subset/Subset.js index 06692b5..a134547 100644 --- a/src/subset/Subset.js +++ b/src/subset/Subset.js @@ -7,6 +7,7 @@ export default class Subset { this.font = font; this.glyphs = []; this.mapping = {}; + this.tables = []; // always include the missing glyph this.includeGlyph(0); @@ -24,4 +25,11 @@ export default class Subset { return this.mapping[glyph]; } + + includeTable(table) { + if (typeof table === 'string' && table.length === 4) { + this.tables.push(table); + } + } + } diff --git a/src/subset/TTFSubset.js b/src/subset/TTFSubset.js index f13e5ce..fe3b700 100644 --- a/src/subset/TTFSubset.js +++ b/src/subset/TTFSubset.js @@ -109,23 +109,24 @@ export default class TTFSubset extends Subset { // ] // TODO: subset prep, cvt, fpgm? - return Directory.toBuffer({ - tables: { - head, - hhea, - loca: this.loca, - maxp, - 'cvt ': this.font['cvt '], - prep: this.font.prep, - glyf: this.glyf, - hmtx: this.hmtx, - fpgm: this.font.fpgm - - // name: clone @font.name - // 'OS/2': clone @font['OS/2'] - // post: clone @font.post - // cmap: cmap - } - }); + // The following is the minimum set of tables. + let t = { + head, + hhea, + loca: this.loca, + maxp, + 'cvt ': this.font['cvt '], + prep: this.font.prep, + glyf: this.glyf, + hmtx: this.hmtx, + fpgm: this.font.fpgm, + } + + for (const i in this.tables) { + const table = this.tables[i]; + t[table] = cloneDeep(this.font[table]); + } + + return Directory.toBuffer({tables: t}); } } diff --git a/test/subset.js b/test/subset.js index bb01e06..5f32a7e 100644 --- a/test/subset.js +++ b/test/subset.js @@ -58,6 +58,19 @@ describe('font subsetting', function () { // must test also second glyph which has an odd loca index assert.equal(f.getGlyph(2).path.toSVG(), font.glyphsForString('b')[0].path.toSVG()); }); + + it('should produce a subset including font table OS/2', function () { + let subset = font.createSubset(); + for (let glyph of font.glyphsForString('hello')) { + subset.includeGlyph(glyph); + } + subset.includeTable('OS/2'); + + let buf = subset.encode(); + let f = fontkit.create(buf); + assert.equal(f.hasOwnProperty('OS/2'), true); + }); + }); describe('CFF subsetting', function () {