简体   繁体   English

Harfbuzz 在调用 hb_shape() 后重新运行不正确的字形代码点

[英]Harfbuzz retruns incorrect glyph codepoints after calling hb_shape()

First I create hb_font_t from FT_Face :首先,我从FT_Face hb_font_t

    FT_Library ft;
    FT_Init_FreeType(&ft);
    FT_Face face;
    FT_New_Face(ft, path.c_str(), 0, &face);
    FT_Set_Pixel_Sizes(face, 0, size);

    _ft_face = face;
    _hb_font = hb_ft_font_create(_ft_face, NULL);

Then I'm shaping a buffer:然后我正在塑造一个缓冲区:

std::vector<Glyph> Font::Shape(hb_buffer_t* buf)
{
    std::vector<Glyph> temp;
    hb_shape(_hb_font, buf, NULL, 0);

    unsigned int glyph_count;
    hb_glyph_info_t* glyph_info = hb_buffer_get_glyph_infos(buf, &glyph_count);
    hb_glyph_position_t* glyph_pos = hb_buffer_get_glyph_positions(buf, &glyph_count);

    Glyph g;
    hb_position_t cursor_x = 0;
    hb_position_t cursor_y = 0;
    for (unsigned int i = 0; i < glyph_count; ++i) {
        hb_codepoint_t glyphid = glyph_info[i].codepoint;
        hb_position_t x_offset = glyph_pos[i].x_offset >> 6;
        hb_position_t y_offset = glyph_pos[i].y_offset >> 6;
        hb_position_t x_advance = glyph_pos[i].x_advance >> 6;
        hb_position_t y_advance = glyph_pos[i].y_advance >> 6;

        //Getting pre-generated glyph data (i.e. position and size in the font atlas.
        g = _glyphs->at(glyphid); 
        g.pos = glm::vec2(cursor_x + x_offset, cursor_y + y_offset);
        temp.push_back(g);

        cursor_x += x_advance;
        cursor_y += y_advance;
    }

    return temp;
}

The problem is that glyph_info[i].codepoint stores the incorrect codepoint, but only if hb_shape() is called.问题是glyph_info[i].codepoint存储了不正确的代码点,但前提是调用了hb_shape() For example, with i = 0 it stores 43 , however it should store 72 , which corresponds to 'H' .例如,当i = 0时,它存储43 ,但它应该存储72 ,它对应于'H'

(Buffer is created in the following way): (缓冲区是通过以下方式创建的):

char* text = "Hello, world!";
hb_buffer_t* buf;
buf = hb_buffer_create();
hb_buffer_add_utf8(buf, text, -1, 0, -1);
hb_buffer_set_direction(buf, HB_DIRECTION_LTR);
hb_buffer_set_script(buf, HB_SCRIPT_LATIN);
hb_buffer_set_language(buf, hb_language_from_string("en", -1));

If hb_shape() isn't called, it returns absolutely correct codepoints ( 72 for 'H' , 101 for 'e' , etc).如果未调用hb_shape() ,它会返回绝对正确的代码点( 'H'72'e'101 ,等等)。

From the definition of the hb_glyph_info_t struct:根据hb_glyph_info_t结构的定义

codepoint;代码点;

either a Unicode code point (before shaping) or a glyph index (after shaping). Unicode 代码点(整形前)或字形索引(整形后)。

So this is expected behaviour所以这是预期的行为

  • codepoint will be a Unicode value before shaping.在整形之前,代码点将是一个codepoint值。
  • But after shaping it will be the glyph index in the font.但是在整形之后它将是字体中的字形索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM