opentype.js icon indicating copy to clipboard operation
opentype.js copied to clipboard

charToGlyphIndex() in encoding.js never returns null

Open debussy2k opened this issue 8 years ago • 5 comments

So the hasChar() always returns true if this.encoding is an object of CmapEncoding.

// font.js
Font.prototype.hasChar = function(c) {
    return this.encoding.charToGlyphIndex(c) !== null;
};

Below code, this.cmap.glyphIndexMap[c.charCodeAt(0)] never returns null. Maybe the caller of charToGlyphIndex() except hasChar() want to receive 0(zero) for using it as index value if there were no mapping glyph.

// encoding.js
CmapEncoding.prototype.charToGlyphIndex = function(c) {
    return this.cmap.glyphIndexMap[c.charCodeAt(0)] || 0;
};

debussy2k avatar Mar 08 '18 07:03 debussy2k

Hi, 0 is the standard code for the .notdefglyph, to be displayed as a fallback.

Regardless of the encoding scheme, character codes that do not correspond to any glyph in the font should be mapped to glyph index 0. The glyph at this location must be a special glyph representing a missing character, commonly known as .notdef.

( https://docs.microsoft.com/en-us/typography/opentype/spec/cmap )

fpirsch avatar Mar 08 '18 11:03 fpirsch

@fpirsch you are definitely right. Because charToGlypnIndex() do not return null in any case, hasChar() will return true always.

// font.js
Font.prototype.hasChar = function(c) {
    return this.encoding.charToGlyphIndex(c) !== 0;  // instead of !== null
};

debussy2k avatar Mar 08 '18 12:03 debussy2k

suggestion:

// font.js
Font.prototype.hasChar = function(c) {
    return this.encoding.charToGlyphIndex(c) > 0;  // checks, 0, null, undefined, false...
};

fpirsch avatar Mar 08 '18 14:03 fpirsch

@debussy2k Indeed...! @fpirsch Your solution looks good, should we add this in a next patch or minor version?

Jolg42 avatar Mar 14 '18 17:03 Jolg42

Are there still plans to implement this fix? This bug caused a lot of confusion until I found out about this issue.

tomvdbussche avatar Dec 03 '19 13:12 tomvdbussche

Are there still plans to implement this fix? This bug caused a lot of confusion until I found out about this issue.

It took only 5 years, but I just opened PR #632 😉

Connum avatar Oct 30 '23 11:10 Connum