Initialize Tizen 2.3
[framework/graphics/cairo.git] / test / text-rotate.c
1 /*
2  * Copyright © 2004 Red Hat, Inc.
3  *
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.
14  *
15  * RED HAT, INC. 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.
22  *
23  * Author: Carl D. Worth <cworth@cworth.org>
24  */
25
26 /* Bug history
27  *
28  * 2004-11-03 Steve Chaplin <stevech1097@yahoo.com.au>
29  *
30  *   Reported bug on mailing list:
31  *
32  *      From: Steve Chaplin <stevech1097@yahoo.com.au>
33  *      To: cairo@cairographics.org
34  *      Date: Thu, 04 Nov 2004 00:00:17 +0800
35  *      Subject: [cairo] Rotated text bug on drawable target
36  *
37  *      The attached file draws text rotated 90 degrees first to a PNG file and
38  *      then to a drawable. The PNG file looks fine, the text on the drawable is
39  *      unreadable.
40  *
41  *      Steve
42  *
43  * 2004-11-03 Carl Worth <cworth@cworth.org>
44  *
45  *   Looks like the major problems with this bug appeared in the great
46  *   font rework between 0.1.23 and 0.2.0. And it looks like we need
47  *   to fix the regression test suite to test the xlib target (since
48  *   the bug does not show up in the png backend).
49  *
50  *   Hmm... Actually, things don't look perfect even in the PNG
51  *   output. Look at how that 'o' moves around. It's particularly off
52  *   in the case where it's rotated by PI.
53  *
54  *   And I'm still not sure about what to do for test cases with
55  *   text--a new version of freetype will change everything. We may
56  *   need to add a simple backend for stroked fonts and add a simple
57  *   builtin font to cairo for pixel-perfect tests with text.
58  *
59  * 2005-08-23
60  *
61  *   It appears that the worst placement and glyph selection problems
62  *   have now been resolved. In the past some letters were noticeably
63  *   of a different size at some rotations, and there was a lot of
64  *   drift away from the baseline. These problems do not appear
65  *   anymore.
66  *
67  *   Another thing that helps is that we now have font options which
68  *   we can use to disable hinting in order to get more repeatable
69  *   results. I'm doing that in this test now.
70  *
71  *   There are still some subtle positioning problems which I'm
72  *   assuming are due to the lack of finer-than-whole-pixel glyph
73  *   positioning. I'm generating a reference image now by replacing
74  *   cairo_show_text with cairo_text_path; cairo_fill. This will let
75  *   us look more closely at the remaining positioning problems. (In
76  *   particular, I want to make sure we're rounding as well as
77  *   possible).
78  *
79  * 2007-02-21
80  *
81  *   Seems like all the "bugs" have been fixed and all remaining is
82  *   missing support for subpixel glyph positioning.  Removing from
83  *   XFAIL now.
84  */
85
86 #include "cairo-test.h"
87
88 #define WIDTH  150
89 #define HEIGHT 150
90 #define NUM_TEXT 20
91 #define TEXT_SIZE 12
92
93 /* Draw the word cairo at NUM_TEXT different angles.
94  * We separate the circle into quadrants to reduce
95  * numerical errors i.e. so each quarter is pixel-aligned.
96  */
97 static void
98 draw_quadrant (cairo_t *cr,
99                const char *text,
100                const cairo_text_extents_t *extents,
101                const cairo_matrix_t *transform,
102                int x_off, int y_off)
103 {
104     int i;
105
106     for (i = 0; i < NUM_TEXT/4; i++) {
107         cairo_save (cr);
108         cairo_rotate (cr, 2*M_PI*i/NUM_TEXT);
109         cairo_transform (cr, transform);
110         cairo_set_line_width (cr, 1.0);
111         cairo_rectangle (cr, x_off - 0.5, y_off - 0.5, extents->width + 1, extents->height + 1);
112         cairo_set_source_rgb (cr, 1, 0, 0);
113         cairo_stroke (cr);
114         cairo_move_to (cr, x_off - extents->x_bearing, y_off - extents->y_bearing);
115         cairo_set_source_rgb (cr, 0, 0, 0);
116 #if CAIRO_TEST_GENERATE_REFERENCE_IMAGE
117         cairo_text_path (cr, text);
118         cairo_fill (cr);
119 #else
120         cairo_show_text (cr, text);
121 #endif
122         cairo_restore (cr);
123     }
124 }
125
126 static cairo_test_status_t
127 draw (cairo_t *cr, int width, int height)
128 {
129     cairo_text_extents_t extents;
130     cairo_font_options_t *font_options;
131     const char text[] = "cairo";
132     int x_off, y_off;
133     cairo_matrix_t m;
134
135     /* paint white so we don't need separate ref images for
136      * RGB24 and ARGB32 */
137     cairo_set_source_rgb (cr, 1., 1., 1.);
138     cairo_paint (cr);
139
140     cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
141                             CAIRO_FONT_SLANT_NORMAL,
142                             CAIRO_FONT_WEIGHT_NORMAL);
143     cairo_set_font_size (cr, TEXT_SIZE);
144
145     font_options = cairo_font_options_create ();
146
147     cairo_get_font_options (cr, font_options);
148     cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
149
150     cairo_set_font_options (cr, font_options);
151     cairo_font_options_destroy (font_options);
152
153     cairo_set_source_rgb (cr, 0, 0, 0);
154
155     cairo_translate (cr, WIDTH/2.0, HEIGHT/2.0);
156
157     cairo_text_extents (cr, text, &extents);
158
159     if (NUM_TEXT == 1) {
160         x_off = y_off = 0;
161     } else {
162         y_off = - floor (0.5 + extents.height / 2.0);
163         x_off = floor (0.5 + (extents.height+1) / (2 * tan (M_PI/NUM_TEXT)));
164     }
165
166     cairo_save (cr);
167     cairo_matrix_init_identity (&m);
168     draw_quadrant (cr, text, &extents, &m, x_off, y_off);
169     cairo_matrix_init (&m, 0, 1, -1, 0, 0, 0);
170     draw_quadrant (cr, text, &extents, &m, x_off, y_off);
171     cairo_restore (cr);
172
173     cairo_save (cr);
174     cairo_scale (cr, -1, -1);
175     cairo_matrix_init_identity (&m);
176     draw_quadrant (cr, text, &extents, &m, x_off, y_off);
177     cairo_matrix_init (&m, 0, 1, -1, 0, 0, 0);
178     draw_quadrant (cr, text, &extents, &m, x_off, y_off);
179     cairo_restore (cr);
180
181     return CAIRO_TEST_SUCCESS;
182 }
183
184 CAIRO_TEST (text_rotate,
185             "Tests show_text under various rotations",
186             "text, transform", /* keywords */
187             NULL, /* requirements */
188             WIDTH, HEIGHT,
189             NULL, draw)