2009-11-17 Jeffrey Stedfast <fejj@novell.com>
[moon.git] / cairo / test / ft-text-vertical-layout-type1.c
blob1dbe31d83b1434e7ea8d722a6e709a8d9964b13c
1 /*
2 * Copyright © 2006 Jinghua Luo
4 * Permission to use, copy, modify, distribute, and sell this software
5 * and its documentation for any purpose is hereby granted without
6 * fee, provided that the above copyright notice appear in all copies
7 * and that both that copyright notice and this permission notice
8 * appear in supporting documentation, and that the name of
9 * Red Hat, Inc. not be used in advertising or publicity pertaining to
10 * distribution of the software without specific, written prior
11 * permission. Red Hat, Inc. makes no representations about the
12 * suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * JINGHUA LUO DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
18 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
21 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Author: Jinghua Luo <sunmoon1997@gmail.com>
24 * Derived from:
25 * text-antialias-none.c,
26 * ft-font-create-for-ft-face.c.
27 * Original Author: Carl D. Worth <cworth@cworth.org>
29 #include "cairo-test.h"
30 #include <cairo-ft.h>
32 #define WIDTH 80
33 #define HEIGHT 240
34 #define TEXT_SIZE 30
36 static cairo_test_draw_function_t draw;
38 static const cairo_test_t test = {
39 "ft-text-vertical-layout-type1",
40 "Tests text rendering for vertical layout with Type1 fonts"
41 "\nCan fail if an incorrect font is loaded---need to bundle the desired font",
42 WIDTH, HEIGHT,
43 draw
46 static cairo_status_t
47 create_scaled_font (cairo_t * cr,
48 cairo_scaled_font_t **out)
50 FcPattern *pattern, *resolved;
51 FcResult result;
52 cairo_font_face_t *font_face;
53 cairo_scaled_font_t *scaled_font;
54 cairo_font_options_t *font_options;
55 cairo_matrix_t font_matrix, ctm;
56 cairo_status_t status;
57 double pixel_size;
59 font_options = cairo_font_options_create ();
61 cairo_get_font_options (cr, font_options);
63 pattern = FcPatternCreate ();
64 if (pattern == NULL)
65 return CAIRO_STATUS_NO_MEMORY;
67 FcPatternAddString (pattern, FC_FAMILY, (FcChar8 *)"Nimbus Sans L");
68 FcPatternAddDouble (pattern, FC_PIXEL_SIZE, TEXT_SIZE);
69 FcConfigSubstitute (NULL, pattern, FcMatchPattern);
71 cairo_ft_font_options_substitute (font_options, pattern);
73 FcDefaultSubstitute (pattern);
74 resolved = FcFontMatch (NULL, pattern, &result);
75 if (resolved == NULL) {
76 FcPatternDestroy (pattern);
77 return CAIRO_STATUS_NO_MEMORY;
80 /* set layout to vertical */
81 FcPatternDel (resolved, FC_VERTICAL_LAYOUT);
82 FcPatternAddBool (resolved, FC_VERTICAL_LAYOUT, FcTrue);
84 FcPatternGetDouble (resolved, FC_PIXEL_SIZE, 0, &pixel_size);
86 font_face = cairo_ft_font_face_create_for_pattern (resolved);
88 cairo_matrix_init_translate (&font_matrix, 10, 30);
89 cairo_matrix_rotate (&font_matrix, M_PI_2/3);
90 cairo_matrix_scale (&font_matrix, pixel_size, pixel_size);
92 cairo_get_matrix (cr, &ctm);
94 scaled_font = cairo_scaled_font_create (font_face,
95 &font_matrix,
96 &ctm,
97 font_options);
99 cairo_font_options_destroy (font_options);
100 cairo_font_face_destroy (font_face);
101 FcPatternDestroy (pattern);
102 FcPatternDestroy (resolved);
104 status = cairo_scaled_font_status (scaled_font);
105 if (status) {
106 cairo_scaled_font_destroy (scaled_font);
107 return status;
110 *out = scaled_font;
111 return CAIRO_STATUS_SUCCESS;
114 static cairo_test_status_t
115 draw (cairo_t *cr, int width, int height)
117 cairo_text_extents_t extents;
118 cairo_scaled_font_t *scaled_font;
119 cairo_status_t status;
120 const char text[] = "i-W";
121 double line_width, x, y;
123 line_width = cairo_get_line_width (cr);
125 /* We draw in the default black, so paint white first. */
126 cairo_save (cr);
127 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
128 cairo_paint (cr);
129 cairo_restore (cr);
131 status = create_scaled_font (cr, &scaled_font);
132 if (status) {
133 return cairo_test_status_from_status (cairo_test_get_context (cr),
134 status);
137 cairo_set_scaled_font (cr, scaled_font);
139 cairo_set_line_width (cr, 1.0);
140 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
141 cairo_text_extents (cr, text, &extents);
142 x = width - (extents.width + extents.x_bearing) - 5;
143 y = height - (extents.height + extents.y_bearing) - 5;
144 cairo_move_to (cr, x, y);
145 cairo_show_text (cr, text);
146 cairo_rectangle (cr,
147 x + extents.x_bearing - line_width / 2,
148 y + extents.y_bearing - line_width / 2,
149 extents.width + line_width,
150 extents.height + line_width);
151 cairo_stroke (cr);
153 cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
154 cairo_text_extents (cr, text, &extents);
155 x = -extents.x_bearing + 5;
156 y = -extents.y_bearing + 5;
157 cairo_move_to (cr, x, y);
158 cairo_text_path (cr, text);
159 cairo_fill (cr);
160 cairo_rectangle (cr,
161 x + extents.x_bearing - line_width / 2,
162 y + extents.y_bearing - line_width / 2,
163 extents.width + line_width,
164 extents.height + line_width);
165 cairo_stroke (cr);
167 cairo_scaled_font_destroy (scaled_font);
169 return CAIRO_TEST_SUCCESS;
173 main (void)
175 return cairo_test (&test);