Hello!
I was working with fonts and faced a necessity of obtainig of kerning of glyph pair.
Documentation here: http://www.libsdl.org/projects/SDL_ttf/docs/ gave me no info on it.
So I spent some time to write own routine. When I finally looked into SDL_ttf.c I was quite surprised to find TTF_GetFontKerningSize function.
So the first point about TTF_GetFontKerningSize is old documentation on top of google search. Please, somebody, fix it
Second point is the fact that TTF_GetFontKerningSize behaviour looks strange (or even incorrect).
That is, it looks like the function takes characters codes and returns pair’s kerning. But it doesn’t.
If you look at the code of this function and at the code of appropriate text rendering function, you may notice that technically
TTF_GetFontKerningSize takes glyph’s indices:
Code:
int TTF_GetFontKerningSize(TTF_Font* f, int prev_index, int index)
{
FT_Vector delta;
FT_Get_Kerning( font->face, prev_index, index, ft_kerning_default, &delta );
return (delta.x >> 6);
}
And those indices cannot be obtained using other API’s functions.
Considering above, I think its body should look like as follows (calculations in double is to increase acuracy ):
Code:
int TTF_GetFontKerningSize(TTF_Font* f, int p, int c)
{
double res = 0.0;
int glyph_index = -1;
int prev_index = -1;
if(c == UNICODE_BOM_NATIVE || c == UNICODE_BOM_SWAPPED)
{
return 0.0;
}
if(p == UNICODE_BOM_NATIVE || p == UNICODE_BOM_SWAPPED)
{
return 0.0;
}
int error = Find_Glyph(f, c, CACHED_METRICS);
if(error)
{
TTF_SetFTError("Couldn't find glyph", error);
return -1;
}
glyph_index = f->current->index;
error = Find_Glyph(f, p, CACHED_METRICS);
if ( error )
{
TTF_SetFTError("Couldn't find glyph", error);
return -1;
}
prev_index = f->current->index;
if ( prev_index && glyph_index )
{
FT_Vector delta;
FT_Get_Kerning( f->face, prev_index, glyph_index, ft_kerning_default, &delta );
res = delta.x;
res /= 64.0;
}
return (int)res;
}
I’ve tried this solution and it looks working.
Thank you for your attention.