On Tue, Feb 22, 2011 at 01:01:35PM -0500, Behdad Esfahbod wrote:
> On 02/20/11 15:29, Khaled Hosny wrote:
> > I'm suspecting that the size setting in FT_Set_Char_Size() and
> > cairo_set_font_size() don't line together, but I'm not sure what would
> > be the correct use of both.
> 
> Instead of setting size on the face yourself, just get a prepared face from
> cairo using cairo_ft_scaled_font_lock_face().

I thought about this already, but it give me much worse spacing; glyps
are too widely spaced that two of them can't fit in the screen, see the
attached file.

Regards,
 Khaled

-- 
 Khaled Hosny
 Egyptian
 Arab
/*
 $ 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);

    cairo_surface_t *cr_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, /*x+20*/1000, 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_scaled_font_t * cr_scaled_font = cairo_get_scaled_font (cr);
    FT_Face ft_face2 = cairo_ft_scaled_font_lock_face (cr_scaled_font);

    char *text = argv[1];
    int length = strlen(text);

    hb_face_t *hb_face = hb_ft_face_create (ft_face2, NULL);
    hb_font_t *hb_font = hb_ft_font_create (ft_face2, 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);

    cairo_ft_scaled_font_unlock_face (cr_scaled_font);

    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_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

Reply via email to