photopea / Typr.js

Typr.js - process fonts in Javascript
MIT License
914 stars 73 forks source link

Any way to get the glyph name? #47

Open opcodewriter opened 11 months ago

opcodewriter commented 11 months ago

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

photopea commented 11 months ago

what is a glyphs name?

opcodewriter commented 11 months ago

@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 commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

@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 commented 11 months ago

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

opcodewriter commented 11 months ago

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.