From f92e5c44b04cfdd50bc8f26cf921cb97e016584a Mon Sep 17 00:00:00 2001 From: Pomax Date: Wed, 10 Oct 2018 10:19:52 -0700 Subject: [PATCH 1/4] fix the entrySelector calculation The spec for the searchRange and entrySelector specified the following: >uint16 searchRange (Maximum power of 2 <= numTables) x 16. >uint16 entrySelector Log2(maximum power of 2 <= numTables). >uint16 rangeShift NumTables x 16-searchRange. --- src/tables/directory.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/tables/directory.js b/src/tables/directory.js index 1e62df27..d9fdfd3a 100644 --- a/src/tables/directory.js +++ b/src/tables/directory.js @@ -44,8 +44,9 @@ Directory.preEncode = function(stream) { this.numTables = tables.length; this.tables = tables; - this.searchRange = Math.floor(Math.log(this.numTables) / Math.LN2) * 16; - this.entrySelector = Math.floor(this.searchRange / Math.LN2); + let maxPowerOf2 = 2 ** Math.floor((Math.log(this.numTables) / Math.LN2)); + this.searchRange = maxPowerOf2 * 16; + this.entrySelector = Math.log(maxPowerOf2) / Math.LN2; this.rangeShift = this.numTables * 16 - this.searchRange; }; From 203a8f17bd30003c1b2a24437fceec3c361e51cb Mon Sep 17 00:00:00 2001 From: Pomax Date: Mon, 5 Nov 2018 07:56:44 -0800 Subject: [PATCH 2/4] Math.pow instead of ** --- src/tables/directory.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tables/directory.js b/src/tables/directory.js index d9fdfd3a..c1ef2ab2 100644 --- a/src/tables/directory.js +++ b/src/tables/directory.js @@ -44,7 +44,8 @@ Directory.preEncode = function(stream) { this.numTables = tables.length; this.tables = tables; - let maxPowerOf2 = 2 ** Math.floor((Math.log(this.numTables) / Math.LN2)); + let maxExponentFor2 = Math.floor((Math.log(this.numTables) / Math.LN2)); + let maxPowerOf2 = Math.pow(2, maxExponentFor2); this.searchRange = maxPowerOf2 * 16; this.entrySelector = Math.log(maxPowerOf2) / Math.LN2; this.rangeShift = this.numTables * 16 - this.searchRange; From 3f6700fa3c1d876d6ccae54648393dcd81ee30e3 Mon Sep 17 00:00:00 2001 From: Pomax Date: Mon, 5 Nov 2018 08:52:25 -0800 Subject: [PATCH 3/4] added a test --- src/tables/directory.js | 1 + test/directory.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 test/directory.js diff --git a/src/tables/directory.js b/src/tables/directory.js index c1ef2ab2..dbe69270 100644 --- a/src/tables/directory.js +++ b/src/tables/directory.js @@ -46,6 +46,7 @@ Directory.preEncode = function(stream) { let maxExponentFor2 = Math.floor((Math.log(this.numTables) / Math.LN2)); let maxPowerOf2 = Math.pow(2, maxExponentFor2); + this.searchRange = maxPowerOf2 * 16; this.entrySelector = Math.log(maxPowerOf2) / Math.LN2; this.rangeShift = this.numTables * 16 - this.searchRange; diff --git a/test/directory.js b/test/directory.js new file mode 100644 index 00000000..826589a0 --- /dev/null +++ b/test/directory.js @@ -0,0 +1,21 @@ +import fontkit from '../src'; +import assert from 'assert'; +import BBox from '../src/glyph/BBox'; + +describe('metadata', function() { + let ttfFont = fontkit.openSync(__dirname + '/data/OpenSans/OpenSans-Regular.ttf'); + + it('decodes SFNT directory values correctly', function() { + let dir = ttfFont.directory; + assert.equal(dir.numTables, 19); + assert.equal(dir.searchRange, 256); + assert.equal(dir.entrySelector, 4); + assert.equal(dir.rangeShift, 48); + }); + + it('numTables matches table collection', function() { + let dir = ttfFont.directory; + assert.equal(Object.keys(dir.tables).length, dir.numTables); + }); + +}); From eba8a0bbb6956eaa1aa0f1fa60b8e2bc006e3ecc Mon Sep 17 00:00:00 2001 From: Pomax Date: Mon, 5 Nov 2018 08:54:33 -0800 Subject: [PATCH 4/4] cleanup --- test/directory.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/test/directory.js b/test/directory.js index 826589a0..b47ed283 100644 --- a/test/directory.js +++ b/test/directory.js @@ -1,12 +1,11 @@ import fontkit from '../src'; import assert from 'assert'; -import BBox from '../src/glyph/BBox'; describe('metadata', function() { - let ttfFont = fontkit.openSync(__dirname + '/data/OpenSans/OpenSans-Regular.ttf'); + let font = fontkit.openSync(__dirname + '/data/OpenSans/OpenSans-Regular.ttf'); it('decodes SFNT directory values correctly', function() { - let dir = ttfFont.directory; + let dir = font.directory; assert.equal(dir.numTables, 19); assert.equal(dir.searchRange, 256); assert.equal(dir.entrySelector, 4); @@ -14,7 +13,7 @@ describe('metadata', function() { }); it('numTables matches table collection', function() { - let dir = ttfFont.directory; + let dir = font.directory; assert.equal(Object.keys(dir.tables).length, dir.numTables); });