gpt4 book ai didi

c++ - 计算 FreeType 字形渲染的位置?

转载 作者:行者123 更新时间:2023-11-30 03:19:58 25 4
gpt4 key购买 nike

我正在尝试使用 FreeType 和 OpenGL 呈现一些文本:

example image

text = "fghjRT-123VWYUGHJ$@%"

还有两个问题:

  1. 为什么字母之间有这么奇怪的空格? (提前有什么问题?)
  2. 如何计算顶部位置(或原点位置)? (黄色边框和字符之间有一些空间,我想把它附加到上边框)

我的渲染代码:

/* 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();
}

我的字形加载代码片段:

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;

最佳答案

好吧,我自己找到了答案。
我对 char_rect 位置的计算有误。正确方法:

RectF char_rect(
pos.x() + char_info->bearing().x(), /* left */
pos.y() - char_info->bearing().y(), /* top */
char_info->glyph_rect().size() /* width, height */
);

以及找到从顶部边界调整的基线(原点)的方法:

Vector<2> pos = params.rect.left_top();
pos.y() += font->ascend(); /* move pen down on the screen */

地点:

FT_Face face = ...;
font->ascend() = face->ascender / 32;

关于c++ - 计算 FreeType 字形渲染的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53267905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com