I'm trying to render some text using FreeType and OpenGL:
text = "fghjRT-123VWYUGHJ$@%"
And have two questions:
My render code:
/* top left position on screen to render text (yellow border) */
Vector<2> pos = params.rect.left_top();
/* adjust to bottom (how to get correct origin position?) */
pos.y() += font->char_size();
for (char ch : params.text)
{
/* contains info after freetype FT_LoadGlyph */
FontChar const* char_info = font->find_char(ch);
RectF char_rect(
pos - char_info->bearing(), /* left, top */
char_info->glyph_rect().size() /* width, height */
);
/* convert screen coordinates to OpenGL coordinates */
RectF dr = calc_coord(m_screen_size, char_rect);
/* pack position of glyph and it's texture from bitmap
* into one sequence of data
*/
Vector<16> spr_coords = pack_spr_info(dr, char_info->rect());
m_sprite_buffer.push_back(spr_coords);
/* move pen by advance */
pos.x() += char_info->advance();
}
Piece of my glyph loading code:
FT_GlyphSlot slot = face->glyph;
char_info->bearing() = {
slot->metrics.horiBearingX / 64.0f,
slot->metrics.horiBearingY / 64.0f
};
char_info->glyph_rect() = {
slot->metrics.horiBearingX / 64.0f, /* left */
slot->metrics.horiBearingY / 64.0f, /* top */
slot->metrics.width / 64.0f, /* width */
slot->metrics.height / 64.0f /* height */
};
char_info->advance() = slot->metrics.horiAdvance / 64.0f;
Ok, I found out the answer by myself.
There are was a mistake in my calculation for char_rect
position. Correct way:
RectF char_rect(
pos.x() + char_info->bearing().x(), /* left */
pos.y() - char_info->bearing().y(), /* top */
char_info->glyph_rect().size() /* width, height */
);
And the way to find baseline(origin) adjusted from top border:
Vector<2> pos = params.rect.left_top();
pos.y() += font->ascend(); /* move pen down on the screen */
Where:
FT_Face face = ...;
font->ascend() = face->ascender / 32;