Bizarre character spacing issues with Freetype

Started by
1 comment, last by LittleJimmy 7 years, 2 months ago

Hi Everyone,

I'm trying to render text using OpenGL and the Freetype library. Text rendering is working fine except for one thing. Every now and then, a character is spaced incorrectly:

text1.png

Above you can see the 'j' in jumps is clashing with the 'u'. I've tried different fonts, but they also produce odd spacing errors:

text2.png

In that case, the 'p' in jumps and 'g' in dog isn't spaced correctly either.

the way I create the horizontal offsets for characters in the final texture is by using the advance.x property on the resulting glyph and adding any kerning values for the character:


//first get kerning if we're passed the first character and this font has kerning
if ((i >= 1) && (Font->HasKerning == true))
{
    FT_Get_Kerning(FontFace, PreviousGlyph, CurrentGlyph, FT_KERNING_DEFAULT, &KerningVector);
    HorizontalOffset += KerningVector.x >> 6;
}

/* This is where the pixels are copied from the glyph into the texture */
/* HorizontalOffset is the 'X' value starting point for the new glyph */

//now that we're done copying over the pixel data, we update the Offset based on the glyphs advance
HorizontalOffset += FontFace->glyph->advance.x >> 6

On closer inspection, I noticed the advance.x value for the character 'j' for the font in the first image is much smaller than the other characters, 13 as opposed to the average of +-28. I believe that is the cause of the 'u' overwriting it. If I open both font's in the windows font viewer, they render correctly, they also render correctly in GIMP. It seems to be only my application thats rendering them incorrectly

My question is, how should I go about fixing this issue? Is there something I'm missing here?

Thanks.

Advertisement

Are you respecting bearingX in the freetype metrics?

In your first image the space between the x and the j looks too large, I suspect you're ignoring a negative bearingX which would make the tail of the j almost hang under the x.

metrics.png

Are you respecting bearingX in the freetype metrics?

In your first image the space between the x and the j looks too large, I suspect you're ignoring a negative bearingX which would make the tail of the j almost hang under the x.

Damn, good show! You are correct, I wasn't taking the horizontal bearing into consideration! That fixed it:

text1fixed.png

text2fixed.png

Thank you very much!

This topic is closed to new replies.

Advertisement