/*
 * Copyright © 2006 Jinghua Luo
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. Red Hat, Inc. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * JINGHUA LUO DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Jinghua Luo <sunmoon1997@gmail.com>
 * Derived from:
 *  text-antialias-none.c,
 *  ft-font-create-for-ft-face.c.
 * Original Author: Carl D. Worth <cworth@cworth.org>
 */
#include <cairo.h>
#include <cairo-ft.h>

#define WIDTH  300
#define HEIGHT 600
#define TEXT_SIZE 24
#define OUTPUT_FILENAME "cairo-vertical-layout-text.png"

static cairo_scaled_font_t *
create_scaled_font (cairo_t * cr)
{
    FcPattern *pattern, *resolved;
    FcResult result;
    cairo_font_face_t *font_face;
    cairo_scaled_font_t *scaled_font;
    cairo_font_options_t *font_options;
    cairo_matrix_t font_matrix, ctm;
    double pixel_size;

    font_options = cairo_font_options_create ();

    pattern = FcPatternCreate ();

    FcPatternAddString (pattern, FC_FAMILY, (FcChar8 *) "AR PL ZenKai Big5");
    FcPatternAddDouble (pattern, FC_PIXEL_SIZE, TEXT_SIZE);
    FcConfigSubstitute (NULL, pattern, FcMatchPattern);

    cairo_ft_font_options_substitute (font_options, pattern);

    FcDefaultSubstitute (pattern);
    resolved = FcFontMatch (NULL, pattern, &result);

    /* set layout to vertical */
    FcPatternDel (resolved, FC_VERTICAL_LAYOUT);
    FcPatternAddBool (resolved, FC_VERTICAL_LAYOUT, FcTrue);

    FcPatternGetDouble (resolved, FC_PIXEL_SIZE, 0, &pixel_size);

    font_face = cairo_ft_font_face_create_for_pattern (resolved);

    cairo_matrix_init_identity (&font_matrix);
    cairo_matrix_scale (&font_matrix, pixel_size, pixel_size);

    cairo_get_matrix (cr, &ctm);

    scaled_font = cairo_scaled_font_create (font_face,
					    &font_matrix,
					    &ctm,
					    font_options);

    cairo_font_options_destroy (font_options);
    cairo_font_face_destroy (font_face);
    FcPatternDestroy (pattern);
    FcPatternDestroy (resolved);

    return scaled_font;
}

/*
 * draw strings from right to left and top to bottom.
 */
static void
draw_strings (cairo_t              *cr, 
	      int                   width, 
	      int                   height,
	      cairo_scaled_font_t * scaled_font,
	      const char            *strs[])
{
    cairo_font_extents_t font_extents;
    cairo_text_extents_t text_extents;
    int i;

    if (!strs || !*strs)
	return;

    cairo_save (cr);

    cairo_set_scaled_font (cr, scaled_font);

    cairo_scaled_font_extents (scaled_font, &font_extents);

    i = 0;

    cairo_translate (cr, width, 0);

    for (i = 0; strs[i]; i++) {
	cairo_text_extents (cr, strs[i], &text_extents);

	cairo_translate (cr, -font_extents.height * 1.5, 0);

	/* draw bounding box */
	cairo_rectangle (cr, 
			 0, 
			 (height - text_extents.height) / 2.0  - text_extents.y_bearing,
			 text_extents.width + 5, text_extents.height + 10);
	cairo_stroke (cr);

	cairo_move_to (cr,
		       -text_extents.x_bearing,
		       (height - text_extents.height) / 2.0  - text_extents.y_bearing);

	cairo_show_text (cr, strs[i]);
    }

    cairo_restore (cr);
}

static void
draw (cairo_t *cr, int width, int height)
{
    cairo_text_extents_t extents;
    cairo_scaled_font_t * scaled_font;
    const static char *strs[] = {
	"贈鄰女",
	"魚玄機",
	"羞日遮羅袖，愁春懶起妝；",
	"易求無價寶，難得有情郎。",
	"枕上潛垂淚，花間暗斷腸；",
	"自能窺宋玉，何必恨王昌。",
	NULL };

    /* We draw in the default black, so paint white first. */
    cairo_save (cr);
    cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
    cairo_paint (cr);
    cairo_restore (cr);

    scaled_font = create_scaled_font (cr);

    cairo_set_line_width (cr, 1.0);
    cairo_set_source_rgb (cr, 1.0, 0, 1.0); /* blue */

    draw_strings (cr, width, height, scaled_font, strs);

    cairo_scaled_font_destroy (scaled_font);
}

int
main (void)
{
    cairo_surface_t * sr;
    cairo_t * cr;
    cairo_pattern_t *pat;

    sr =  cairo_image_surface_create (CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT);
    cr = cairo_create (sr);
    
    draw (cr, WIDTH, HEIGHT);

    cairo_surface_write_to_png (sr, OUTPUT_FILENAME);
    cairo_surface_destroy (sr);
    cairo_destroy (cr);


    return 0;
}
