簡體   English   中英

計算FreeType字形渲染的位置?

[英]Calculating position for FreeType glyph rendering?

我正在嘗試使用FreeType和OpenGL渲染一些文本:

示例圖片

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM