2 * Copyright © 2006 Red Hat, Inc.
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 * 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.
23 * Author: Carl D. Worth <cworth@cworth.org>
26 #include "cairo-test.h"
32 static cairo_test_draw_function_t draw
;
35 "font-matrix-translation",
36 "Test that translation in a font matrix can be used to offset a string",
42 text_extents_equal (const cairo_text_extents_t
*A
,
43 const cairo_text_extents_t
*B
)
45 return A
->x_bearing
== B
->x_bearing
&&
46 A
->y_bearing
== B
->y_bearing
&&
47 A
->width
== B
->width
&&
48 A
->height
== B
->height
&&
49 A
->x_advance
== B
->x_advance
&&
50 A
->y_advance
== B
->y_advance
;
53 static cairo_test_status_t
54 box_text (cairo_t
*cr
, const char *utf8
, double x
, double y
)
57 cairo_text_extents_t extents
= {0}, scaled_extents
= {0};
58 cairo_scaled_font_t
*scaled_font
;
62 cairo_text_extents (cr
, utf8
, &extents
);
64 scaled_font
= cairo_get_scaled_font (cr
);
65 cairo_scaled_font_text_extents (scaled_font
, TEXT
, &scaled_extents
);
66 if (! text_extents_equal (&extents
, &scaled_extents
)) {
67 cairo_test_log ("Error: extents differ when they shouldn't:\n"
68 "cairo_text_extents(); extents (%g, %g, %g, %g, %g, %g)\n"
69 "cairo_scaled_font_text_extents(); extents (%g, %g, %g, %g, %g, %g)\n",
70 extents
.x_bearing
, extents
.y_bearing
,
71 extents
.width
, extents
.height
,
72 extents
.x_advance
, extents
.y_advance
,
73 scaled_extents
.x_bearing
, scaled_extents
.y_bearing
,
74 scaled_extents
.width
, scaled_extents
.height
,
75 scaled_extents
.x_advance
, scaled_extents
.y_advance
);
76 return CAIRO_TEST_FAILURE
;
79 line_width
= cairo_get_line_width (cr
);
81 x
+ extents
.x_bearing
- line_width
/ 2,
82 y
+ extents
.y_bearing
- line_width
/ 2,
83 extents
.width
+ line_width
,
84 extents
.height
+ line_width
);
87 cairo_move_to (cr
, x
, y
);
88 cairo_show_text (cr
, utf8
);
92 return CAIRO_TEST_SUCCESS
;
95 static cairo_test_status_t
96 draw (cairo_t
*cr
, int width
, int height
)
98 cairo_test_status_t status
;
99 cairo_text_extents_t extents
;
100 cairo_matrix_t matrix
;
102 cairo_set_source_rgb (cr
, 1.0, 1.0, 1.0); /* white */
105 cairo_select_font_face (cr
, "Bitstream Vera Sans",
106 CAIRO_FONT_SLANT_NORMAL
,
107 CAIRO_FONT_WEIGHT_NORMAL
);
108 cairo_set_font_size (cr
, TEXT_SIZE
);
110 cairo_translate (cr
, PAD
, PAD
);
111 cairo_set_line_width (cr
, 1.0);
113 cairo_text_extents (cr
, TEXT
, &extents
);
115 /* Draw text and bounding box */
116 cairo_set_source_rgb (cr
, 0, 0, 0); /* black */
117 status
= box_text (cr
, TEXT
, 0, - extents
.y_bearing
);
121 /* Then draw again with the same coordinates, but with a font
122 * matrix to position the text below and shifted a bit to the
124 cairo_matrix_init_translate (&matrix
, TEXT_SIZE
/ 2, TEXT_SIZE
+ PAD
);
125 cairo_matrix_scale (&matrix
, TEXT_SIZE
, TEXT_SIZE
);
126 cairo_set_font_matrix (cr
, &matrix
);
128 cairo_set_source_rgb (cr
, 0, 0, 1); /* blue */
129 status
= box_text (cr
, TEXT
, 0, - extents
.y_bearing
);
133 return CAIRO_TEST_SUCCESS
;
139 return cairo_test (&test
);