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

Should not throw error on glyph without outline for U+0020 #124

Open
btangmu opened this issue Aug 30, 2023 · 0 comments
Open

Should not throw error on glyph without outline for U+0020 #124

btangmu opened this issue Aug 30, 2023 · 0 comments

Comments

@btangmu
Copy link

btangmu commented Aug 30, 2023

svg2ttf fails if a glyph has no outline, that is, no 'd' attribute. For example, given this glyph:

<glyph unicode=" "/>

or, equivalently

<glyph unicode="&#x0020;" />

It should not be treated as an error. The space character (U+0020) can be included in a font. Some software (such as XeTeX) complains if a font does not contain it. There should be no requirement for this glyph (or any glyph), to have a 'd' attribute.

The bug is in this code:

function getGlyph(glyphElem, fontInfo) {
  var glyph = {};

  if (glyphElem.hasAttribute('d')) {
    glyph.d = glyphElem.getAttribute('d').trim();
  } else {
    // try nested <path>
    var pathElem = glyphElem.getElementsByTagName('path')[0];

    if (pathElem.hasAttribute('d')) {
      // <path> has reversed Y axis
      glyph.d = svgpath(pathElem.getAttribute('d'))
        .scale(1, -1)
        .translate(0, fontInfo.ascent)
        .toString();
    } else {
      throw new Error("Can't find 'd' attribute of <glyph> tag.");
    }
  }

First, since pathElem may be undefined, the condition should be if (pathElem && pathElem.hasAttribute('d')) or more briefly if (pathElem?.hasAttribute('d')) (with question mark) to prevent this error:

TypeError: Cannot read properties of undefined (reading 'hasAttribute')

Second, this line should be deleted: throw new Error("Can't find 'd' attribute of <glyph> tag.");

@puzrin puzrin closed this as completed Dec 14, 2023
@puzrin puzrin reopened this Dec 14, 2023
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

No branches or pull requests

2 participants