/*
 * pangocairo vertical writing example
 * Young-Ho, Cha (ganadist@gmail.com)
 * based pango-viewer in pango 1.16.0
 * Compare with http://en.wikipedia.org/wiki/Horizontal_and_vertical_writing_in_East_Asian_scripts
 */

#include <pango/pangocairo.h>

int main()
{
		PangoRectangle rect;
		PangoFontMap * fontmap = pango_cairo_font_map_get_default();
		PangoContext * ctx = pango_cairo_font_map_create_context(PANGO_CAIRO_FONT_MAP (fontmap));

		PangoMatrix matrix = PANGO_MATRIX_INIT;
		pango_matrix_rotate (&matrix, -90);

		pango_context_set_base_gravity (ctx, PANGO_GRAVITY_AUTO);
		//pango_context_set_gravity_hint(ctx, PANGO_GRAVITY_HINT_STRONG);
		pango_context_set_matrix (ctx, &matrix);

		PangoLayout * layout = pango_layout_new(ctx);
		PangoFontDescription *desc;
		desc = pango_font_description_from_string ("Sans 12");
		pango_layout_set_font_description (layout, desc);
		pango_font_description_free(desc);

		pango_layout_set_text(layout, "「ノートを買つた.」", -1);
		
		PangoRectangle logical_rect;
		pango_layout_get_pixel_extents(layout, NULL, &logical_rect);

		int width, height;
		width = logical_rect.width;
		height= logical_rect.height;

		rect.x = rect.y = 0;
		rect.width = width;
		rect.height = height;

		pango_matrix_transform_pixel_rectangle (&matrix, &rect);
		matrix.x0 = -rect.x;
		matrix.y0 = -rect.y;

		pango_context_set_matrix (ctx, &matrix);

		cairo_matrix_t cairo_matrix;
		cairo_matrix.xx = matrix.xx;
		cairo_matrix.yx = matrix.yx;
		cairo_matrix.xy = matrix.xy;
		cairo_matrix.yy = matrix.yy;
		cairo_matrix.x0 = matrix.x0;
		cairo_matrix.y0 = matrix.y0;

		cairo_surface_t *sur = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 300, 300);
		cairo_t * cairo_ctx = cairo_create(sur);
		cairo_set_source_rgb (cairo_ctx, 1.0, 1.0, 1.0);
		cairo_set_matrix (cairo_ctx, &cairo_matrix);
		pango_layout_get_pixel_extents(layout, NULL, &logical_rect);

		pango_cairo_show_layout(cairo_ctx, layout);
		cairo_surface_write_to_png (sur, "test.png");

		cairo_destroy(cairo_ctx);
		cairo_surface_destroy(sur);

		g_object_unref(layout);
		g_object_unref(ctx);

		return  0;
}
