为什么水平前进不足以正确格式化字形?

时间:2019-08-24 21:31:22

标签: c++ fonts rendering freetype

我正在用vulkan制作一个简单的文本渲染器,并且正在使用freetype格式化文本。

我阅读了freetype教程,并提出了以下功能:

void Scribe::CreateSingleLineGeometry(const string &text, ScGeomMetaInfo &info,
    ScGeomtry &geometry)
{
    float texture_length = info.texture_len;
    float h_offset = info.h_offset;

    float char_l = info.char_len;
    float v_anchor = info.v_offset;

    auto &vertices = geometry.first;
    auto &indices = geometry.second;

    auto length = max(ft_face->bbox.xMax - ft_face->bbox.xMin,
        ft_face->bbox.yMax - ft_face->bbox.yMin);
    for(auto &c: text)
    {
        auto glyph_index = FT_Get_Char_Index(ft_face, c);
        auto error = FT_Load_Glyph(ft_face, glyph_index, FT_LOAD_NO_HINTING);

        auto metrics = ft_face->glyph->metrics;
        float g_height = metrics.height;
        float g_bearing = metrics.horiBearingY;
        float correction = 1.0 - (g_height) / float(length) -
            float(metrics.horiBearingY - g_height) / float(length);

        correction *= char_l;
        float bearingX = char_l * float(metrics.horiBearingX) / float(length);
        // Insert the vertex positions and uvs into the returned geometry
        float h_coords[] = {h_offset + bearingX, h_offset + bearingX + char_l};
        float v_coords[] = {v_anchor + correction, v_anchor + correction + char_l};
        auto glyph_data = glyph_map[glyph_index];
        float tex_h_coords[] = {glyph_data.lt_uv.x, glyph_data.rb_uv.x};
        float tex_v_coords[] = {glyph_data.lt_uv.y, glyph_data.rb_uv.y};
        for(int x=0; x<2; x++) {
            for(int y=0; y<2; y++) {
                vertices.insert(end(vertices), {h_coords[x], v_coords[y], 0});
                vertices.insert(end(vertices), {tex_h_coords[x], tex_v_coords[y]});
            }
        }

        // Setup the indices of the current quad
        // There's 4 vertices in a quad, so we offset each index by (quad_size * num_quads)
        uint delta =  4 * info.c_num++;
        indices.insert(end(indices), {0+delta, 3+delta, 1+delta, 0+delta, 3+delta, 2+delta});
        h_offset += char_l * float(metrics.horiAdvance) / float(length) + 0.03;
    }
}

我要特别强调这一行:

h_offset += char_l * float(metrics.horiAdvance) / float(length) + 0.03;

该行末尾的0.03并非来自任何地方,我将其插入此处以使外观看起来不错。

这是具有额外偏移量的结果:

enter image description here

我认为这看起来还不错。但是,如果我要删除多余的偏移量:

h_offset += char_l * float(metrics.horiAdvance) / float(length);

enter image description here

我得到的东西看起来根本不正确。为什么预付款不足以正确设置字体格式?

0 个答案:

没有答案