We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2572

Summary: TTF_GetFontKerningSize issue
Product: SDL_ttf Reporter: D <Dmitry216>
Component: miscAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: i8degrees
Version: unspecified   
Hardware: All   
OS: All   
Attachments: font kerning size fix

Description D 2014-06-05 15:23:08 UTC
Hello! I've posted this to the forum, but no reaction there. So I duplicate this here to ease tracking.



 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.
Comment 1 Jeffrey Carpenter 2014-06-12 23:23:36 UTC
Created attachment 1680 [details]
font kerning size fix

I had the same trouble as the original author, and verified that his proposed patch does work for me, under Mac OS X v10.9.3. (Correspondence with the author via the SDL-dev mailing list shows his platform to be Xubuntu 14). 

I'm not sure if this bug affects Windows at all, nor if the proposed changes change things. 

Cheers,
Jeffrey Carpenter <i8degrees@gmail.com>
Comment 2 Sam Lantinga 2014-06-16 01:25:10 UTC
Fixed, thanks!
https://hg.libsdl.org/SDL_ttf/rev/4ecbc07f149f