Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/tables/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ 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 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;
Copy link
Contributor Author

@Pomax Pomax Oct 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L47 guarantees that L49 is always* integer

*) up to the font-impossible value of Math.log(2**750), at least.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you may need to add a Babel plugin for the exponentiation operator

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rewrite it to Math.pow form - eventually IE will be dead and all those can be cleaned up, until then it's not really worth asking babel to do the conversion for.

this.rangeShift = this.numTables * 16 - this.searchRange;
};

Expand Down
20 changes: 20 additions & 0 deletions test/directory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fontkit from '../src';
import assert from 'assert';

describe('metadata', function() {
let font = fontkit.openSync(__dirname + '/data/OpenSans/OpenSans-Regular.ttf');

it('decodes SFNT directory values correctly', function() {
let dir = font.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 = font.directory;
assert.equal(Object.keys(dir.tables).length, dir.numTables);
});

});