/* Compile with:
 * gcc -o lang-test `pkg-config --cflags --libs gtk+-2.0 cairo-ft fontconfig freetype2` lang-test.c
 */

#include <cairo.h>
#include <cairo-ft.h>
#include <pango/pango.h>
#include <pango/pangocairo.h>
#include <gtk/gtk.h>
#include <fontconfig/fontconfig.h>
#include <fontconfig/fcfreetype.h>

/* Copied from WebCore/platform/graphics/gtk/GlyphPageTreeNodePango.cpp */
static PangoGlyph
pango_font_get_glyph(PangoFont* font, PangoContext* context, gunichar wc)
{
    PangoGlyph result = 0;
    gchar buffer[7];

    gint  length = g_unichar_to_utf8(wc, buffer);
    g_return_val_if_fail(length, 0);

    GList* items = pango_itemize(context, buffer, 0, length, NULL, NULL);

    if (g_list_length(items) == 1) {
        PangoGlyphString* glyphs = pango_glyph_string_new();

        pango_shape(buffer, length, &((PangoItem*)items->data)->analysis, glyphs);

        if (glyphs->num_glyphs == 1)
            result = glyphs->glyphs[0].glyph;
        else
            g_warning("didn't get 1 glyph but %d", glyphs->num_glyphs);

        pango_glyph_string_free(glyphs);
    }

    g_list_foreach(items, (GFunc)pango_item_free, NULL);
    g_list_free(items);

    return result;
}

int
main(gint argc, gchar *argv[])
{
    gtk_init (&argc, &argv);

    PangoFontDescription *description = pango_font_description_new ();
    pango_font_description_set_absolute_size (description, 13  * PANGO_SCALE);
    pango_font_description_set_family (description, "sans-serif");

    PangoFontMap *font_map = pango_cairo_font_map_get_default ();
    PangoContext *context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (font_map));
    pango_context_set_font_description (context, description);

    PangoFont *font = pango_font_map_load_font (font_map, context, description);
    cairo_scaled_font_t *scaled_font = cairo_scaled_font_reference (pango_cairo_font_get_scaled_font(PANGO_CAIRO_FONT (font)));

    FT_Face face = cairo_ft_scaled_font_lock_face(scaled_font);
    guint ft_glyph = FcFreeTypeCharIndex(face, 'H');
    cairo_ft_scaled_font_unlock_face(scaled_font);

    PangoGlyph pango_glyph = pango_font_get_glyph (font, context, 'H');

    g_printerr ("freetype: %u, pango: %u\n", ft_glyph, pango_glyph);

    return 0;
}
