# [FreeType] Characters are not on the base line

## Recommended Posts

Hi everyone,

I have been struggling with this issue for the past two days. I cannot render all my letters on the base line.

I have used this function to set my font size

unsigned int charPixelSize = 50;
FT_Set_Pixel_Sizes(face, 0, charPixelSize);

and when it's time to render a character I do this

y = face->glyph->bitmap.rows - face->glyph->bitmap_top;

But the line of code above will produce the image above.

The only way I could get all my characters to render properly is by doing this

y = (50 / 1.33333) - face->glyph->bitmap_top;

in theory 50 should be my font size in pixels. But it seems that it is in points?? I don't know.

If I divide 50 by 1.3333 this woks somewhat.  The reason I'm diving by 1.33333 is because each point has 1.33333 pixels.

I feel this whole division thing is a hacky way of getting this to work. Because 50 should be the font size in pixels not points.

I don't know. I'm just lost.

Any help would be appreciated.

Cheers.

Alright, after a lot of reading and digging around, I finally was able to make this work. So what I did was loop through the entire list of characters and find the largest face->glyph->metrics.horiBearingY.

After that I simply subtracted face->glyph->bitmap_top from the largest horiBearingY.

int maxY = 0;

//Loop through every character and get the biggest horiBearingY.
for()
{
if (face->glyph->metrics.horiBearingY > maxY)
maxY = face->glyph->metrics.horiBearingY >> 6;
}

void RenderText(std::string& text, Vector2 position)
{
Vector2 pos = position;

//Loop through my text
for()
{
pos.y = position.y + maxY - face->glyph->bitmap_top;
}
}

Hope this will help someone else.

Cheers.

Edited by Kylotan
removed the 'solved' tag

##### Share on other sites

It is normal that letters are not on the baseline, letter like "j" should be partly below it.

If you look at the documentation (like https://www.freetype.org/freetype2/docs/glyphs/glyphs-3.html ) you can see it's bearingY rather than top of the image, since the image is fully cropped. (That is, the top of the lowercase letters is not transparent or so, it's simply not there).

Edited by Alberth

##### Share on other sites
1 hour ago, Alberth said:

It is normal that letters are not on the baseline, letter like "j" should be partly below it.

If you look at the documentation (like https://www.freetype.org/freetype2/docs/glyphs/glyphs-3.html ) you can see it's bearingY rather than top of the image, since the image is fully cropped. (That is, the top of the lowercase letters is not transparent or so, it's simply not there).

Sorry if I wasn't clear. I'm not talking about letters like j. Letters like j, g, p, q look fine to me. I'm taking about letters like a, c e, m, n, u. If you look at the first picture you will notice that all of these letters are above the base line where they should actually be on same line.

I did try to do this, it gave me the same result as the first image.

y = face->glyph->bitmap.rows - (face->glyph->metrics.horiBearingY >> 6);

As far as I can tell from the values in my code both horiBearingY and bitmap_top are the same.

This is a better image

Alright, this is the most progress that I have done in two days.

if I do this

unsigned int height = (face->bbox.yMax - face->bbox.yMin) >> 6;
pos.y = height - face->glyph->bitmap.rows;

all the characters line up nicely, but characters like j, g, p, q are now messed up.

Edited by FantasyVII

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• 48
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631378
• Total Posts
2999665
×