nothings / stb

stb single-file public domain libraries for C/C++
https://twitter.com/nothings
Other
26.81k stars 7.72k forks source link

stb_truetype.h : Get Glyph Name #908

Open aiekick opened 4 years ago

aiekick commented 4 years ago

Hello,

in first, thank you, for your greats libraries

I have searched a bit in the file, i dont know how to get the glyph name. Did you know how i can acheive that ?

You have some font with special Glyph Names like the forkawesome-webfont.ttf

Im creating a free tool for generate font header files like this tool but with a gui and many others options (c++/ImGui), and i need to extract the glyph name.

Thanks

aiekick commented 4 years ago

hello,

i found the solution :)

I modified your version of stbtt__find_table for return table length if needed

static stbtt_uint32 stbtt__find_table(stbtt_uint8 *data, stbtt_uint32 fontstart, const char *tag, stbtt_uint32 *len)
{
   stbtt_int32 num_tables = ttUSHORT(data+fontstart+4);
   stbtt_uint32 tabledir = fontstart + 12;
   stbtt_int32 i;
   for (i=0; i < num_tables; ++i) {
      stbtt_uint32 loc = tabledir + 16*i;
      if (stbtt_tag(data+loc+0, tag)) {
        if (len) *len = ttULONG(data+loc+12);
        return ttULONG(data+loc+8);
      }
   }
   return 0;
}

and here the code im using for get all glyph names adapted from the lib otfcc

static const char *standardMacNames[258] = { ".notdef", ".null", "nonmarkingreturn", "space", "exclam", "quotedbl", "numbersign", "dollar", "percent", "ampersand", "quotesingle", "parenleft", "parenright", "asterisk", "plus", "comma", "hyphen", "period", "slash", "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "colon", "semicolon", "less", "equal", "greater", "question", "at", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft", "backslash", "bracketright", "asciicircum", "underscore", "grave", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar", "braceright", "asciitilde", "Adieresis", "Aring", "Ccedilla", "Eacute", "Ntilde", "Odieresis", "Udieresis", "aacute", "agrave", "acircumflex", "adieresis", "atilde", "aring", "ccedilla", "eacute", "egrave", "ecircumflex", "edieresis", "iacute", "igrave", "icircumflex", "idieresis", "ntilde", "oacute", "ograve", "ocircumflex", "odieresis", "otilde", "uacute", "ugrave", "ucircumflex", "udieresis", "dagger", "degree", "cent", "sterling", "section", "bullet", "paragraph", "germandbls", "registered", "copyright", "trademark", "acute", "dieresis", "notequal", "AE", "Oslash", "infinity", "plusminus", "lessequal", "greaterequal", "yen", "mu", "partialdiff", "summation", "product", "pi", "integral", "ordfeminine", "ordmasculine", "Omega", "ae", "oslash", "questiondown", "exclamdown", "logicalnot", "radical", "florin", "approxequal", "Delta", "guillemotleft", "guillemotright", "ellipsis", "nonbreakingspace", "Agrave", "Atilde", "Otilde", "OE", "oe", "endash", "emdash", "quotedblleft", "quotedblright", "quoteleft", "quoteright", "divide", "lozenge", "ydieresis", "Ydieresis", "fraction", "currency", "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl", "periodcentered", "quotesinglbase", "quotedblbase", "perthousand", "Acircumflex", "Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute", "Icircumflex", "Idieresis", "Igrave", "Oacute", "Ocircumflex", "apple", "Ograve", "Uacute", "Ucircumflex", "Ugrave", "dotlessi", "circumflex", "tilde", "macron", "breve", "dotaccent", "ring", "cedilla", "hungarumlaut", "ogonek", "caron", "Lslash", "lslash", "Scaron", "scaron", "Zcaron", "zcaron", "brokenbar", "Eth", "eth", "Yacute", "yacute", "Thorn", "thorn", "minus", "multiply", "onesuperior", "twosuperior", "threesuperior", "onehalf", "onequarter", "threequarters", "franc", "Gbreve", "gbreve", "Idotaccent", "Scedilla", "scedilla", "Cacute", "cacute", "Ccaron", "ccaron", "dcroat" };

std::vector<std::string> glyphNames;

stbtt_uint32 tableLen = 0;
stbtt_uint32 tablePos = stbtt__find_table(fontInfo.data, fontInfo.fontstart, "post", &tableLen);
stbtt_uint8 *data = fontInfo.data + tablePos;
stbtt_int32 version = ttUSHORT(data);
if (version == 2)
{
    std::vector<std::string> pendingNames;
    stbtt_uint16 countGlyphs = ttUSHORT(data + 32);
    stbtt_uint32 offset = 34 + 2 * countGlyphs;
    while (offset < tableLen)
    {
        uint8_t len = data[offset];
        std::string s;
        if (len > 0)
        {
            s = std::string((const char *)data + offset + 1, len);
        }
        offset += len + 1;
        pendingNames.push_back(s);
    }
    for (stbtt_uint16 i = 0; i < countGlyphs; i++)
    {
        stbtt_uint16 mapIdx = ttUSHORT(data + 34 + 2 * i);
        if (mapIdx >= 258)
        {
            stbtt_uint16 idx = mapIdx - 258;
            if (idx < pendingNames.size())
                glyphNames.push_back(pendingNames[idx]);
        }
        else
        {
            glyphNames.push_back(standardMacNames[mapIdx]);
        }
    }
}

after that for get the name of a specific glyph, i get the index of the glyph and use it with glyphNames

int glyphIndex = stbtt_FindGlyphIndex(&fontInfo, glyphCodePoint);
std::string glyphName = glyphNames(glyphIndex);

It work like a charm, by the way i use the std::vector tool. i dont how do the same in pure C. by the way if you think it can be cool to add this feature in your lib, this code can help you, i think :)

let me know if i can help :)

rygorous commented 3 years ago

We might add a function to do this at some point but don't hold your breath, it's pretty far outside the intended use case for stb_truetype.

aiekick commented 3 years ago

i have managed that in my soft internally, so no pb :) but thanks for the answaer :)