From 55cf26cc2e1e3ee6095fc7168b885ed11c4e4dc6 Mon Sep 17 00:00:00 2001 From: Connum Date: Mon, 30 Oct 2023 12:42:39 +0100 Subject: [PATCH] fix hasChar() for CMAP fonts and add tests (fix #330) --- src/font.js | 2 +- test/font.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/font.js b/src/font.js index 6e02c189..a4ca07f3 100644 --- a/src/font.js +++ b/src/font.js @@ -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; }; /** diff --git a/test/font.js b/test/font.js index 626b634b..b114d5a3 100644 --- a/test/font.js +++ b/test/font.js @@ -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); + }); + }); });