#include <glib.h>
#include <pango/pango.h>

static PangoAttribute *
at (unsigned s, unsigned e, PangoAttribute *a)
{
	a->start_index = s;
	a->end_index = e;
	return a;
}

/* ------------------------------------------------------------------------- */

static const char *
color_to_string (PangoColor color)
{
	return g_strdup_printf ("%04x:%04x:%04x", color.red, color.green, color.blue);
}

static gboolean
cb_gnm_pango_attr_dump (PangoAttribute *attr, gpointer user_data)
{
	g_print ("  start=%u; end=%u\n", attr->start_index, attr->end_index);
	switch (attr->klass->type) {
	case PANGO_ATTR_FAMILY: g_print ("    family=\"%s\"\n", ((PangoAttrString *)attr)->value); break;
	case PANGO_ATTR_LANGUAGE: g_print ("    language=\"%s\"\n", pango_language_to_string (((PangoAttrLanguage *)attr)->value)); break;
	case PANGO_ATTR_STYLE: g_print ("    style=%d\n", ((PangoAttrInt *)attr)->value); break;
	case PANGO_ATTR_WEIGHT: g_print ("    weight=%d\n", ((PangoAttrInt *)attr)->value); break;
	case PANGO_ATTR_VARIANT: g_print ("    variant=%d\n", ((PangoAttrInt *)attr)->value); break;
	case PANGO_ATTR_STRETCH: g_print ("    stretch=%d\n", ((PangoAttrInt *)attr)->value); break;
	case PANGO_ATTR_UNDERLINE: g_print ("    underline=%d\n", ((PangoAttrInt *)attr)->value); break;
	case PANGO_ATTR_STRIKETHROUGH: g_print ("    strikethrough=%d\n", ((PangoAttrInt *)attr)->value); break;
	case PANGO_ATTR_RISE: g_print ("    rise=%d\n", ((PangoAttrInt *)attr)->value); break;
	case PANGO_ATTR_FALLBACK: g_print ("    fallback=%d\n", ((PangoAttrInt *)attr)->value); break;
	case PANGO_ATTR_LETTER_SPACING: g_print ("    letter_spacing=%d\n", ((PangoAttrInt *)attr)->value); break;
	case PANGO_ATTR_SIZE: g_print ("    size=%d%s\n",
				       ((PangoAttrSize *)attr)->size,
				       ((PangoAttrSize *)attr)->absolute ? " abs" : ""); break;
	case PANGO_ATTR_SCALE: g_print ("    scale=%g\n", ((PangoAttrFloat *)attr)->value); break;
	case PANGO_ATTR_FOREGROUND: g_print ("    foreground=%s\n", color_to_string (((PangoAttrColor *)attr)->color)); break;
	case PANGO_ATTR_BACKGROUND: g_print ("    background=%s\n", color_to_string (((PangoAttrColor *)attr)->color)); break;
	case PANGO_ATTR_UNDERLINE_COLOR: g_print ("    underline_color=%s\n", color_to_string (((PangoAttrColor *)attr)->color)); break;
	case PANGO_ATTR_STRIKETHROUGH_COLOR: g_print ("    strikethrough_color=%s\n", color_to_string (((PangoAttrColor *)attr)->color)); break;
	default: g_print ("    type=%d\n", attr->klass->type);
	}

	return FALSE;
}

static void
gnm_pango_attr_dump (PangoAttrList *list)
{
	g_print ("PangoAttrList at %p\n", list);
	pango_attr_list_filter (list, cb_gnm_pango_attr_dump, NULL);
}

/* ------------------------------------------------------------------------- */

int
main ()
{
	PangoAttrList *al = pango_attr_list_new ();
	PangoAttrList *al2 = pango_attr_list_new ();

	pango_attr_list_insert (al, at (6, 9, pango_attr_weight_new (700)));
	pango_attr_list_insert (al, at (8, 9, pango_attr_weight_new (400)));

	g_print ("Original list: ");
	gnm_pango_attr_dump (al);

	pango_attr_list_insert (al2, at (0, 1, pango_attr_weight_new (700)));
	g_print ("Other list: ");
	gnm_pango_attr_dump (al2);

	pango_attr_list_splice (al, al2, 8, 1);
	g_print ("After splice: ");
	gnm_pango_attr_dump (al);

	pango_attr_list_change (al, at (8, 9, pango_attr_weight_new (700)));
	g_print ("After change: ");
	gnm_pango_attr_dump (al);

	return 0;
}
