Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed error running get with null index to resolve charToGlyph bug per #735 #736

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Balearica
Copy link
Contributor

Description

This change prevents an error when the GlyphSet get method is called with an index of null/undefined. This resolves a bug with charToGlyph described in #735. A test is also added to prevent the bug from re-occurring in the future.

Motivation and Context

See #735. In short, when using charToGlyph with (1) a Font object created by opentype.js and (2) a character that has no glyph in the file, an error is thrown. This occurs because opentype.js runs this.glyphs.get(glyphIndex) where glyphIndex is null.

This simple change fixes this bug, and makes get return undefined if the glyph does not exist. Looking at the code in charToGlyph (linked below), this is the behavior that is expected by that function.

opentype.js/src/font.mjs

Lines 201 to 210 in e9c090e

Font.prototype.charToGlyph = function(c) {
const glyphIndex = this.charToGlyphIndex(c);
let glyph = this.glyphs.get(glyphIndex);
if (!glyph) {
// .notdef
glyph = this.glyphs.get(0);
}
return glyph;
};

After the change, running charToGlyph with a Font object created by opentype.js defaults to returning .notdef, which is consistent with the behavior when using an imported font.

How Has This Been Tested?

This fixed the issue described in #735 in my project. The existing automated tests pass, and I added a new automated test for the bug this solves.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I did npm run test and all tests passed green (including code styling checks).
  • I have added tests to cover my changes.
  • My change requires a change to the documentation.
  • I have updated the README accordingly.
  • I have read the CONTRIBUTING document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants