1 /*
2  * Copyright © 2006 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 #include "cairo-test.h"
27 
28 #define TEXT_SIZE 12
29 #define PAD 4
30 #define TEXT "text"
31 
32 static cairo_bool_t
text_extents_equal(const cairo_text_extents_t * A,const cairo_text_extents_t * B)33 text_extents_equal (const cairo_text_extents_t *A,
34 	            const cairo_text_extents_t *B)
35 {
36     return A->x_bearing == B->x_bearing &&
37 	   A->y_bearing == B->y_bearing &&
38 	   A->width     == B->width     &&
39 	   A->height    == B->height    &&
40 	   A->x_advance == B->x_advance &&
41 	   A->y_advance == B->y_advance;
42 }
43 
44 static cairo_test_status_t
box_text(const cairo_test_context_t * ctx,cairo_t * cr,const char * utf8,double x,double y)45 box_text (const cairo_test_context_t *ctx, cairo_t *cr,
46 	  const char *utf8,
47 	  double x, double y)
48 {
49     double line_width;
50     cairo_text_extents_t extents = {0}, scaled_extents = {0};
51     cairo_scaled_font_t *scaled_font;
52     cairo_status_t status;
53 
54     cairo_save (cr);
55 
56     cairo_text_extents (cr, utf8, &extents);
57 
58     scaled_font = cairo_get_scaled_font (cr);
59     cairo_scaled_font_text_extents (scaled_font, TEXT, &scaled_extents);
60     status = cairo_scaled_font_status (scaled_font);
61     if (status)
62 	return cairo_test_status_from_status (ctx, status);
63 
64     if (! text_extents_equal (&extents, &scaled_extents)) {
65         cairo_test_log (ctx,
66 			"Error: extents differ when they shouldn't:\n"
67 			"cairo_text_extents(); extents (%g, %g, %g, %g, %g, %g)\n"
68 			"cairo_scaled_font_text_extents(); extents (%g, %g, %g, %g, %g, %g)\n",
69 		        extents.x_bearing, extents.y_bearing,
70 			extents.width, extents.height,
71 			extents.x_advance, extents.y_advance,
72 		        scaled_extents.x_bearing, scaled_extents.y_bearing,
73 			scaled_extents.width, scaled_extents.height,
74 			scaled_extents.x_advance, scaled_extents.y_advance);
75         return CAIRO_TEST_FAILURE;
76     }
77 
78     line_width = cairo_get_line_width (cr);
79     cairo_rectangle (cr,
80 		     x + extents.x_bearing - line_width / 2,
81 		     y + extents.y_bearing - line_width / 2,
82 		     extents.width  + line_width,
83 		     extents.height + line_width);
84     cairo_stroke (cr);
85 
86     cairo_move_to (cr, x, y);
87     cairo_show_text (cr, utf8);
88 
89     cairo_restore (cr);
90 
91     return CAIRO_TEST_SUCCESS;
92 }
93 
94 static cairo_test_status_t
draw(cairo_t * cr,int width,int height)95 draw (cairo_t *cr, int width, int height)
96 {
97     const cairo_test_context_t *ctx = cairo_test_get_context (cr);
98     cairo_test_status_t status;
99     cairo_text_extents_t extents;
100     cairo_matrix_t matrix;
101 
102     cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
103     cairo_paint (cr);
104 
105     cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
106 			    CAIRO_FONT_SLANT_NORMAL,
107 			    CAIRO_FONT_WEIGHT_NORMAL);
108     cairo_set_font_size (cr, TEXT_SIZE);
109 
110     cairo_translate (cr, PAD, PAD);
111     cairo_set_line_width (cr, 1.0);
112 
113     cairo_text_extents (cr, TEXT, &extents);
114 
115     /* Draw text and bounding box */
116     cairo_set_source_rgb (cr, 0, 0, 0); /* black */
117     status = box_text (ctx, cr, TEXT, 0, - extents.y_bearing);
118     if (status)
119 	return status;
120 
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
123      * right. */
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);
127 
128     cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
129     status = box_text (ctx, cr, TEXT, 0, - extents.y_bearing);
130     if (status)
131 	return status;
132 
133     return CAIRO_TEST_SUCCESS;
134 }
135 
136 CAIRO_TEST (font_matrix_translation,
137 	    "Test that translation in a font matrix can be used to offset a string",
138 	    "font", /* keywords */
139 	    NULL, /* requirements */
140 	    38, 34,
141 	    NULL, draw)
142