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

Missing Non Unicode characters from fontkit object #296

Open
prasadsawant12 opened this issue Nov 23, 2022 · 1 comment
Open

Missing Non Unicode characters from fontkit object #296

prasadsawant12 opened this issue Nov 23, 2022 · 1 comment

Comments

@prasadsawant12
Copy link

I am using following code to read and iterate over all glyphs in a font

let font = fontkit.create(Buffer.from(buffer));
          font.characterSet.forEach(character => {
               const glyphObj = font.glyphForCodePoint(character);
         });

however when i try to load calibri font, in characterSet object i am getting around 3k glyphs but actually it contains 6.5 k glyphs.
While debugging i came to know that characterSet object doesn't contain non-Unicode characters.

Is there any way to iterate over non-Unicode characters ?

@Pomax
Copy link
Contributor

Pomax commented Nov 23, 2022

The readme does mention that over in https://github.com/foliojs/fontkit#other-properties, the characterSet property is specifically for Unicode codepoint glyphs only. If you compare its length to numGlyphs, though, you will see that Fontkit does know that Calibri has over 6.5k glyphs:

const fontkit = require("fontkit");
const fs = require("fs/promises");
fs.readFile("./calibri.ttf")
  .then((font) => {
    font = fontkit.create(Buffer.from(font));
    console.log(font.numGlyphs);
    console.log(font.characterSet.length);
  })
  .catch(err => console.error(err));

outputs:

6954
3663

So if you want to work with the non-unicode glyphs, you'll have to build that list, but then you might still not be able to actually access those characters given that Fontkit's API is built around working specifically with Unicode codepoints.

However, also note that a ton of glyphs are not "letters": glyphs are just vector graphics, and while some glyphs are actual letters, with an associated code point, plenty are just "components" that used in other glyphs, and would make no sense when rendered in isolation: if you load Calibri in an actual font editor, you'll see it contains 3378 "unmapped" glyphs, meaning they are not meant to ever be rendered in isolation, only to be used as parts of other, real, glyphs. In terms of glyphs that you should actually be able to "use" for Calibri, there are only 3660 glyphs with outlines, plus three "empty" glyphs (notdef, the nonmarking return, and space)

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