Skip to content
Myr

Add support for obtaining the entire kerning table from a font all at once, for use in rendering packed textures offline.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Jorge Rodriguez · committed by Jorge Rodriguez · parent 6f6e11f · Browse files · 1 file+54 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+54 added−0 removed

  • other:+54 added−0 removed

1 file+54 added−0 removed

modified:stb_truetype.h+54 added
732 unmodified lines
733STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2);734STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1);735// as above, but takes one or more glyph indices for greater efficiency736737typedef struct stbtt_kerningentry738{739 int glyph1; // use stbtt_FindGlyphIndex740 int glyph2;741 int advance;742} stbtt_kerningentry;743744STBTT_DEF int stbtt_GetKerningTableLength(const stbtt_fontinfo *info);745STBTT_DEF int stbtt_GetKerningTable(const stbtt_fontinfo *info, stbtt_kerningentry* table, int table_length);746// Retrieves a complete list of all of the kerning pairs provided by the font747// stbtt_GetKerningTable never writes more than table_length entries and returns how many entries it did write.748// The table will be sorted by (a.glyph1 == b.glyph1)?(a.glyph2 < b.glyph2):(a.glyph1 < b.glyph1)749750//////////////////////////////////////////////////////////////////////////////751//752// GLYPH SHAPES (you probably don't need these, but they have to go before753// the bitmaps for C declaration-order reasons)
1409 unmodified lines
2163 if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));2164 }2165}21662167STBTT_DEF int stbtt_GetKerningTableLength(const stbtt_fontinfo *info)2168{2169 stbtt_uint8 *data = info->data + info->kern;21702171 // we only look at the first table. it must be 'horizontal' and format 0.2172 if (!info->kern)2173 return 0;2174 if (ttUSHORT(data+2) < 1) // number of tables, need at least 12175 return 0;2176 if (ttUSHORT(data+8) != 1) // horizontal flag must be set in format2177 return 0;21782179 return ttUSHORT(data+10);2180}21812182STBTT_DEF int stbtt_GetKerningTable(const stbtt_fontinfo *info, stbtt_kerningentry* table, int table_length)2183{2184 stbtt_uint8 *data = info->data + info->kern;2185 int k, length;21862187 // we only look at the first table. it must be 'horizontal' and format 0.2188 if (!info->kern)2189 return 0;2190 if (ttUSHORT(data+2) < 1) // number of tables, need at least 12191 return 0;2192 if (ttUSHORT(data+8) != 1) // horizontal flag must be set in format2193 return 0;21942195 length = ttUSHORT(data+10);2196 if (table_length < length)2197 length = table_length;21982199 for (k = 0; k < length; k++)2200 {2201 table[k].glyph1 = ttUSHORT(data+18+(k*6));2202 table[k].glyph2 = ttUSHORT(data+20+(k*6));2203 table[k].advance = ttSHORT(data+22+(k*6));2204 }22052206 return length;2207}22082209STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2)2210{2211 stbtt_uint8 *data = info->data + info->kern;2212 stbtt_uint32 needle, straw;
1903 unmodified lines