#include <pango/pangocairo.h>
#include <stdio.h>

int
main (int argc, char **argv)
{
	PangoContext* pango_context;
	PangoLayout* pango_layout;
	PangoFontDescription* font_description;
	cairo_font_options_t *font_options;
	int i;
	double minw = 1000, maxw = 0;
	int size = argc > 1 ? atoi (argv[1]) : 10;
	int from = argc > 2 ? atoi (argv[2]) : 50;
	int to   = argc > 3 ? atoi (argv[3]) : 200;
	
	g_type_init ();

	font_description = pango_font_description_new ();
	pango_font_description_set_family (font_description, "Sans");
	pango_font_description_set_size (font_description, size*PANGO_SCALE);

	pango_context = pango_cairo_font_map_create_context ( PANGO_CAIRO_FONT_MAP (pango_cairo_font_map_get_default ()) );

	pango_layout = pango_layout_new (pango_context);
	pango_layout_set_font_description (pango_layout, font_description);

	font_options = cairo_font_options_create ();
	cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
	cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);

	pango_cairo_context_set_font_options (pango_context, font_options);

	cairo_font_options_destroy (font_options);

	for (i = from; i <= to; i++) {
		PangoRectangle rect;
		PangoLayoutLine *line;
		PangoLayoutRun *run;
		double w, h;

		pango_cairo_context_set_resolution (pango_context, i);
		pango_layout_context_changed (pango_layout);
		pango_layout_set_text (pango_layout, "M", -1);
		pango_layout_get_extents (pango_layout, NULL, &rect);
		

		w = (double)rect.width*(72. / (PANGO_SCALE*i));
		h = (double)rect.height*(72. / (PANGO_SCALE*i));

		printf ("%i ; %d ; %d ; %lf ; %lf\n", i,
			rect.width, rect.height, w, h);

		if (w > maxw) maxw = w;
		if (w < minw) minw = w;

		/*
		line = pango_layout_get_line (pango_layout, 0);
		run = line->runs->data;
		pango_font_get_glyph_extents (run->item->analysis.font,
					      run->glyphs->glyphs[0].glyph,
					      NULL,
					      &rect);

		printf ("%i ; %d ; %d ; %lf ; %lf\n", i,
			rect.width, rect.height,
			(double)rect.width*(72. / (PANGO_SCALE*i)),
			(double)rect.height*(72. / (PANGO_SCALE*i)));
		*/
	}

	printf ("minw %f maxw %f error %g%%\n", minw, maxw, (maxw - minw) * 100 / minw);
	pango_font_description_free(font_description);
	g_object_unref(pango_layout);
	g_object_unref(pango_context);

	return 0;
}
