Calculating position for FreeType glyph rendering?
I'm trying to render some text using FreeType and OpenGL:

text = "fghjRT-123VWYUGHJ$@%"
And have two questions:
- Why there are so weird spaces between letters? (what wrong with advance?)
- How to calculate top position (or origin position)? (there are some space between yellow border and characters. I want to attach it to the top border)
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;
c++ opengl true-type-fonts freetype
add a comment |
I'm trying to render some text using FreeType and OpenGL:

text = "fghjRT-123VWYUGHJ$@%"
And have two questions:
- Why there are so weird spaces between letters? (what wrong with advance?)
- How to calculate top position (or origin position)? (there are some space between yellow border and characters. I want to attach it to the top border)
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;
c++ opengl true-type-fonts freetype
add a comment |
I'm trying to render some text using FreeType and OpenGL:

text = "fghjRT-123VWYUGHJ$@%"
And have two questions:
- Why there are so weird spaces between letters? (what wrong with advance?)
- How to calculate top position (or origin position)? (there are some space between yellow border and characters. I want to attach it to the top border)
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;
c++ opengl true-type-fonts freetype
I'm trying to render some text using FreeType and OpenGL:

text = "fghjRT-123VWYUGHJ$@%"
And have two questions:
- Why there are so weird spaces between letters? (what wrong with advance?)
- How to calculate top position (or origin position)? (there are some space between yellow border and characters. I want to attach it to the top border)
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;
c++ opengl true-type-fonts freetype
c++ opengl true-type-fonts freetype
edited Nov 12 '18 at 18:32
genpfault
42.3k953100
42.3k953100
asked Nov 12 '18 at 18:17
kerrytazikerrytazi
29127
29127
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267905%2fcalculating-position-for-freetype-glyph-rendering%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
add a comment |
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;
add a comment |
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;
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;
answered Nov 13 '18 at 20:05
kerrytazikerrytazi
29127
29127
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267905%2fcalculating-position-for-freetype-glyph-rendering%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown