I want to render the text as this:
But this is what I got:
here’s the full code but for the sake of conciseness I also wrote a small demo in c:
#include "freetype2/ft2build.h"
#include "harfbuzz/hb-ft.h"
#include "hb.h"
#include <stdio.h>
#include FT_FREETYPE_H
int main() {
FT_Library library;
FT_Init_FreeType(&library);
FT_Face face;
FT_New_Face(library, "./LXGWWenKaiLite-Regular.ttf", 0, &face);
FT_Set_Char_Size(face, 0, 1000, 0, 0);
hb_font_t *font = hb_ft_font_create(face, NULL);
hb_ft_font_set_funcs(font);
hb_buffer_t *buf = hb_buffer_create();
hb_buffer_add_utf8(buf, "你好——“我说”", -1, 0, -1);
hb_buffer_guess_segment_properties(buf);
hb_shape(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);
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;
hb_position_t y_offset = glyph_pos[i].y_offset;
hb_position_t x_advance = glyph_pos[i].x_advance;
hb_position_t y_advance = glyph_pos[i].y_advance;
printf("offset: (%d, %d), advance: (%d, %d)n", x_offset, y_offset,
x_advance, y_advance);
/* draw_glyph(glyphid, cursor_x + x_offset, cursor_y + y_offset); */
cursor_x += x_advance;
cursor_y += y_advance;
}
}
And I got this:
offset: (0, 0), advance: (1000, 0)
offset: (0, 0), advance: (1000, 0)
offset: (0, 0), advance: (1000, 0)
offset: (0, 0), advance: (1000, 0)
offset: (0, 0), advance: (350, 0)
offset: (0, 0), advance: (1000, 0)
offset: (0, 0), advance: (1000, 0)
offset: (0, 0), advance: (350, 0)
Which is not what I want as offset is always 0, but in the string “你好——“我说””, I expect the “——” to be centered, which obviously isn’t here.
Is it I’m getting something wrong or that harfbuzz doesn’t solve the bearing y for me?
2