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

Any way to get the glyph name? #47

Open
opcodewriter opened this issue Oct 2, 2023 · 8 comments
Open

Any way to get the glyph name? #47

opcodewriter opened this issue Oct 2, 2023 · 8 comments

Comments

@opcodewriter
Copy link

opcodewriter commented Oct 2, 2023

Can I use your library to get the glyph's name (if it exists)?
Some fonts have it set I think.
Thanks!

@photopea
Copy link
Owner

photopea commented Oct 2, 2023

what is a glyphs name?

@opcodewriter
Copy link
Author

opcodewriter commented Oct 3, 2023

@photopea
Other JS libraries (opentype and fontkit) are able to parse and return the glyph names.

See fontkit's glyph object for example: https://github.com/foliojs/fontkit#glyph-objects

name - the glyph name in the font

Please also see https://stackoverflow.com/questions/23185221/getting-the-glyph-name-from-a-ttf-or-otf-font-file

@photopea
Copy link
Owner

photopea commented Oct 3, 2023

I am familiar with the OpenType specification, but I have never heard of glyph names. Could you show us some examples, what glyph should have what names? Also, why would you need a name of a glyph?

@opcodewriter
Copy link
Author

opcodewriter commented Oct 4, 2023

Sure, it's easy to parse the glyphs with a capable library, here's an example with fontkit:

const fontkit = require('fontkit');

const font = fontkit.openSync('Font Awesome 5 Free-Solid-900.otf');

for (let iGlyph = 0; iGlyph < font.numGlyphs; ++iGlyph) {
    const glyph = font.getGlyph(iGlyph);
    if (!glyph) {
        continue;
    }

    console.log(glyph.name);
}

Output:

image

Also, why would you need a name of a glyph?

Glyph names are used in icon fonts to allow developers use the icons easier. Instead of hexadecimal, developers specify the glyph name: https://fontawesome.com/docs/desktop/add-icons/glyphs

I'm trying to parse fonts for glyph names for a little tool I'm working on for myself.

I'd like to use Typr.js instead of other fonts, your library seems slimmer.

Would you add support for glyph names?

@photopea
Copy link
Owner

photopea commented Oct 4, 2023

Ok, I see that there are glyph names in the CFF table. So this is possible only for some OTF files, impossible for TTF files.

But each glyph in the FontAwesome has an unicode code, e.g. this https://fontawesome.com/icons/magnifying-glass?f=classic&s=solid has an unicode code 0xf002 , so you can use Typr.U.codeToGlyph(font, 0xf002)

@opcodewriter
Copy link
Author

opcodewriter commented Oct 5, 2023

@photopea Thanks for your reply, but I'm not following you.

How does your example Typr.U.codeToGlyph(font, 0xf002) helps in getting the glyph name?

Typr.U.codeToGlyph seems to be returning an index.
How can I use the index to get the glyph name?

@photopea
Copy link
Owner

photopea commented Oct 5, 2023

I am suggesting to use only unicode codes, do not use the glyph names at all. Refer to glyphs by their unicode codes.

@opcodewriter
Copy link
Author

Oh, but I need the glyph names, that's the whole point :) I need to be able to automatically get the icon (glyph) names out of an icon font.

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