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: Behdad Esfahbod <behdad@behdad.org>
24
 */
25

            
26
/* Related bug 5177
27
 *
28
 * In short:
29
 *
30
 * _cairo_atsui_font_text_to_glyph with a zero-sized string crashes.
31
 *
32
 * Moreover, the fallback path in cairo_scaled_font_text_to_glyphs()
33
 * when handling a zero-sized string, allocates a zero-sized glyph array
34
 * and when NULL is returned by malloc, recognizes that as an out-of-memory
35
 * error.  The glibc implementation of malloc() does not return NULL from
36
 * malloc(0), but I don't think it's a safe assumption.
37
 *
38
 * By just bailing out on zero-sized text, we fix both issues.
39
 */
40

            
41
#include "cairo-test.h"
42

            
43
#define NUM_TEXT 20
44
#define TEXT_SIZE 12
45

            
46
static cairo_bool_t
47
21
text_extents_equal (const cairo_text_extents_t *A,
48
	            const cairo_text_extents_t *B)
49
{
50
42
    return A->x_bearing == B->x_bearing &&
51
21
	   A->y_bearing == B->y_bearing &&
52
21
	   A->width     == B->width     &&
53
21
	   A->height    == B->height    &&
54
63
	   A->x_advance == B->x_advance &&
55
21
	   A->y_advance == B->y_advance;
56
}
57

            
58
static cairo_bool_t
59
6
font_extents_equal (const cairo_font_extents_t *A,
60
	            const cairo_font_extents_t *B)
61
{
62
12
    return A->ascent    == B->ascent        &&
63
6
	   A->descent   == B->descent       &&
64
6
	   A->height    == B->height        &&
65
18
       A->max_x_advance == B->max_x_advance &&
66
6
       A->max_y_advance == B->max_y_advance;
67
}
68

            
69
static cairo_test_status_t
70
3
draw (cairo_t *cr, int width, int height)
71
{
72
3
    const cairo_test_context_t *ctx = cairo_test_get_context (cr);
73
    cairo_text_extents_t extents, nil_extents;
74
    cairo_font_extents_t font_extents, nil_font_extents;
75
    cairo_scaled_font_t *scaled_font;
76

            
77
3
    cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
78
			    CAIRO_FONT_SLANT_NORMAL,
79
			    CAIRO_FONT_WEIGHT_NORMAL);
80
3
    cairo_set_font_size (cr, 16);
81

            
82
3
    cairo_move_to (cr, 10, 25);
83
3
    cairo_show_text (cr, NULL);
84
3
    cairo_show_text (cr, "");
85
3
    cairo_show_glyphs (cr, NULL, 0);
86
3
    cairo_show_glyphs (cr, (void*)8, 0);
87

            
88
3
    cairo_move_to (cr, 10, 55);
89
3
    cairo_text_path (cr, NULL);
90
3
    cairo_text_path (cr, "");
91
3
    cairo_glyph_path (cr, (void*)8, 0);
92
3
    cairo_fill (cr);
93

            
94
3
    memset (&nil_extents, 0, sizeof (cairo_text_extents_t));
95

            
96
3
    memset (&extents, 0xff, sizeof (cairo_text_extents_t));
97
3
    cairo_text_extents (cr, "", &extents);
98
3
    if (! text_extents_equal (&extents, &nil_extents)) {
99
	cairo_test_log (ctx, "Error: cairo_text_extents(\"\"); extents (%g, %g, %g, %g, %g, %g)\n",
100
		        extents.x_bearing, extents.y_bearing,
101
			extents.width, extents.height,
102
			extents.x_advance, extents.y_advance);
103
	return CAIRO_TEST_FAILURE;
104
    }
105

            
106
3
    memset (&extents, 0xff, sizeof (cairo_text_extents_t));
107
3
    cairo_text_extents (cr, NULL, &extents);
108
3
    if (! text_extents_equal (&extents, &nil_extents)) {
109
	cairo_test_log (ctx, "Error: cairo_text_extents(NULL); extents (%g, %g, %g, %g, %g, %g)\n",
110
		        extents.x_bearing, extents.y_bearing,
111
			extents.width, extents.height,
112
			extents.x_advance, extents.y_advance);
113
	return CAIRO_TEST_FAILURE;
114
    }
115

            
116
3
    memset (&extents, 0xff, sizeof (cairo_text_extents_t));
117
3
    cairo_glyph_extents (cr, (void*)8, 0, &extents);
118
3
    if (! text_extents_equal (&extents, &nil_extents)) {
119
	cairo_test_log (ctx, "Error: cairo_glyph_extents(); extents (%g, %g, %g, %g, %g, %g)\n",
120
		        extents.x_bearing, extents.y_bearing,
121
			extents.width, extents.height,
122
			extents.x_advance, extents.y_advance);
123
	return CAIRO_TEST_FAILURE;
124
    }
125

            
126
3
    scaled_font = cairo_get_scaled_font (cr);
127

            
128
3
    memset (&extents, 0xff, sizeof (cairo_text_extents_t));
129
3
    cairo_scaled_font_text_extents (scaled_font, "", &extents);
130
3
    if (! text_extents_equal (&extents, &nil_extents)) {
131
	cairo_test_log (ctx, "Error: cairo_scaled_font_text_extents(\"\"); extents (%g, %g, %g, %g, %g, %g)\n",
132
		        extents.x_bearing, extents.y_bearing,
133
			extents.width, extents.height,
134
			extents.x_advance, extents.y_advance);
135
	return CAIRO_TEST_FAILURE;
136
    }
137

            
138
3
    memset (&extents, 0xff, sizeof (cairo_text_extents_t));
139
3
    cairo_scaled_font_text_extents (scaled_font, NULL, &extents);
140
3
    if (! text_extents_equal (&extents, &nil_extents)) {
141
	cairo_test_log (ctx, "Error: cairo_scaled_font_text_extents(NULL); extents (%g, %g, %g, %g, %g, %g)\n",
142
		        extents.x_bearing, extents.y_bearing,
143
			extents.width, extents.height,
144
			extents.x_advance, extents.y_advance);
145
	return CAIRO_TEST_FAILURE;
146
    }
147

            
148
3
    memset (&extents, 0xff, sizeof (cairo_text_extents_t));
149
3
    cairo_scaled_font_glyph_extents (scaled_font, (void*)8, 0, &extents);
150
3
    if (! text_extents_equal (&extents, &nil_extents)) {
151
	cairo_test_log (ctx, "Error: cairo_scaled_font_glyph_extents(NULL); extents (%g, %g, %g, %g, %g, %g)\n",
152
		        extents.x_bearing, extents.y_bearing,
153
			extents.width, extents.height,
154
			extents.x_advance, extents.y_advance);
155
	return CAIRO_TEST_FAILURE;
156
    }
157

            
158
    /* Lets also try font size 0 while here */
159
3
    cairo_set_font_size (cr, 0);
160

            
161
3
    memset (&extents, 0xff, sizeof (cairo_text_extents_t));
162
3
    cairo_text_extents (cr, "test", &extents);
163
3
    if (! text_extents_equal (&extents, &nil_extents)) {
164
	cairo_test_log (ctx, "Error: cairo_set_font_size(0); cairo_text_extents(\"test\"); extents (%g, %g, %g, %g, %g, %g)\n",
165
		        extents.x_bearing, extents.y_bearing,
166
			extents.width, extents.height,
167
			extents.x_advance, extents.y_advance);
168
	return CAIRO_TEST_FAILURE;
169
    }
170

            
171
3
    memset (&nil_font_extents, 0, sizeof (cairo_font_extents_t));
172

            
173
3
    memset (&font_extents, 0xff, sizeof (cairo_font_extents_t));
174
3
    cairo_font_extents (cr,  &font_extents);
175
3
    if (! font_extents_equal (&font_extents, &nil_font_extents)) {
176
	cairo_test_log (ctx, "Error: cairo_set_font_size(0); cairo_font_extents(); extents (%g, %g, %g, %g, %g)\n",
177
		        font_extents.ascent, font_extents.descent,
178
			font_extents.height,
179
			font_extents.max_x_advance, font_extents.max_y_advance);
180
	return CAIRO_TEST_FAILURE;
181
    }
182

            
183
3
    scaled_font = cairo_get_scaled_font (cr);
184

            
185
3
    memset (&font_extents, 0xff, sizeof (cairo_font_extents_t));
186
3
    cairo_scaled_font_extents (scaled_font,  &font_extents);
187
3
    if (! font_extents_equal (&font_extents, &nil_font_extents)) {
188
	cairo_test_log (ctx, "Error: cairo_set_font_size(0); cairo_scaled_font_extents(); extents (%g, %g, %g, %g, %g)\n",
189
		        font_extents.ascent, font_extents.descent,
190
			font_extents.height,
191
			font_extents.max_x_advance, font_extents.max_y_advance);
192
	return CAIRO_TEST_FAILURE;
193
    }
194

            
195
3
    return CAIRO_TEST_SUCCESS;
196
}
197

            
198
1
CAIRO_TEST (text_zero_len,
199
	    "Tests show_text and text_path with a zero-sized string",
200
	    "text, stress, extents", /* keywords */
201
	    NULL, /* requirements */
202
	    0, 0,
203
	    NULL, draw)