Skip to content

Commit

Permalink
fix hasChar() for CMAP fonts and add tests (fix #330)
Browse files Browse the repository at this point in the history
  • Loading branch information
Connum committed Oct 30, 2023
1 parent 7a1b1ac commit 2570773
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function Font(options) {
* @return {Boolean}
*/
Font.prototype.hasChar = function(c) {
return this.encoding.charToGlyphIndex(c) !== null;
return this.encoding.charToGlyphIndex(c) > 0;
};

/**
Expand Down
13 changes: 13 additions & 0 deletions test/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,17 @@ describe('font.js', function() {
});

});

describe('hasChar', function() {
it('returns correct results for non-CMAP fonts', function() {
assert.equal(font.hasChar('i'), true);
assert.equal(font.hasChar('x'), false);
});

it('returns correct results for CMAP fonts', function() {
const cmapFont = loadSync('./test/fonts/TestCMAP14.otf');
assert.equal(cmapFont.hasChar('a'), false);
assert.equal(cmapFont.hasChar('≩'), true);
});
});
});

0 comments on commit 2570773

Please sign in to comment.