I'm trying to write a simple HB test and thought using FreeType/Cairo
would be the shortest path. I got the code to shape the text, but glyph
positions are wrong (at least horizontal positions); some glyphs are too
close, others are spaced out than they should.
I'm not sure what is wrong with the attached code, but I'm not familiar
with any of the FreeType/HarfBuzz/Cairo black magic.
Regards,
khaled
--
Khaled Hosny
Egyptian
/*
$ cc `freetype-config --cflags --libs` \
`pkg-config --cflags --libs harfbuzz` \
`pkg-config --cflags --libs cairo` \
`pkg-config --cflags --libs glib-2.0` \
test_hb.c -o test_hb
$ test_hb "text" font.ttf img.png
*/
#include <cairo.h>
#include <hb-ft.h>
#include <hb-glib.h>
int
main (int argc, char *argv[])
{
int size = 128;
FT_Library ft_library;
FT_Face ft_face;
if (FT_Init_FreeType (&ft_library)) {
printf("FT_Init_FreeType failed\n");
return;
}
if (FT_New_Face (ft_library, argv[2], 0, &ft_face)) {
printf("FT_New_Face failed\n");
return;
}
FT_Set_Char_Size (ft_face, 0, size, 0, 0);
char *text = argv[1];
int length = strlen(text);
hb_face_t *hb_face = hb_ft_face_create (ft_face, NULL);
hb_font_t *hb_font = hb_ft_font_create (ft_face, NULL);
hb_buffer_t *hb_buffer = hb_buffer_create (length);
hb_buffer_set_unicode_funcs (hb_buffer, hb_glib_get_unicode_funcs ());
hb_buffer_set_direction (hb_buffer, HB_DIRECTION_RTL);
hb_buffer_set_script (hb_buffer, HB_SCRIPT_ARABIC);
hb_buffer_set_language (hb_buffer, hb_language_from_string ("ar"));
hb_buffer_add_utf8 (hb_buffer, text, length, 0, length);
hb_shape (hb_font, hb_face, hb_buffer, NULL, 0);
int num_glyphs = hb_buffer_get_length (hb_buffer);
hb_glyph_info_t *hb_glyph = hb_buffer_get_glyph_infos (hb_buffer);
hb_glyph_position_t *hb_position = hb_buffer_get_glyph_positions (hb_buffer);
cairo_glyph_t cr_glyphs[num_glyphs];
int i;
int32_t y, x;
y = 200;
x = 20;
for (i = 0; i < num_glyphs; i++)
{
cr_glyphs[i].index = hb_glyph->codepoint;
cr_glyphs[i].x = x + hb_position->x_offset;
cr_glyphs[i].y = y - hb_position->y_offset;
x += hb_position->x_advance;
hb_glyph++;
hb_position++;
//printf("id=%ld, x=%d, y=%d\n", cr_glyphs[i].index, (int)cr_glyphs[i].x, (int)cr_glyphs[i].y);
}
cairo_surface_t *cr_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, x+20, 300);
cairo_t *cr = cairo_create (cr_surface);
cairo_font_face_t *cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
cairo_set_font_face (cr, cr_face);
cairo_set_font_size (cr, size);
cairo_show_glyphs (cr, cr_glyphs, num_glyphs);
cairo_surface_write_to_png (cr_surface, argv[3]);
return 0;
}
_______________________________________________
HarfBuzz mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/harfbuzz