summaryrefslogtreecommitdiff
path: root/test/get-path-extents.c
blob: a06e301d9d5bd79bac97161bcdaa9c50c8531e92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright © 2006 Novell, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Novell, Inc. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. Novell, Inc. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * NOVELL, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Robert O'Callahan <rocallahan@novell.com>
 */

#include "cairo-test.h"
#include <stddef.h>
#include <math.h>

static cairo_test_draw_function_t draw;

cairo_test_t test = {
    "get-path-extents",
    "Test cairo_fill_extents and cairo_stroke_extents",
    0, 0,
    draw
};

enum ExtentsType { FILL, STROKE, PATH };

enum Relation { EQUALS, APPROX_EQUALS, CONTAINS };

static cairo_bool_t
check_extents (const char *message, cairo_t *cr, enum ExtentsType type,
               enum Relation relation,
               double x, double y, double width, double height)
{
    double ext_x1, ext_y1, ext_x2, ext_y2;
    const char *type_string;
    const char *relation_string;

    switch (type) {
    default:
    case FILL:
        type_string = "fill";
        cairo_fill_extents (cr, &ext_x1, &ext_y1, &ext_x2, &ext_y2);
        break;
    case STROKE:
        type_string = "stroke";
        cairo_stroke_extents (cr, &ext_x1, &ext_y1, &ext_x2, &ext_y2);
        break;
    case PATH:
        type_string = "path";
        cairo_path_extents (cr, &ext_x1, &ext_y1, &ext_x2, &ext_y2);
        break;
    }

    /* let empty rects match */
    if ((ext_x1 == ext_x2 || ext_y1 == ext_y2) && (width == 0 || height == 0))
        return 1;

    switch (relation) {
    default:
    case EQUALS:
        relation_string = "equal";
        if (ext_x1 == x && ext_y1 == y && ext_x2 == x + width && ext_y2 == y + height)
            return 1;
        break;
    case APPROX_EQUALS:
        relation_string = "approx. equal";
        if (round (ext_x1) == x && round (ext_y1) == y && round (ext_x2) == x + width && round (ext_y2) == y + height)
            return 1;
        break;
    case CONTAINS:
        relation_string = "contain";
        if (width == 0 || height == 0) {
            /* odd test that doesn't really test anything... */
            return 1;
        }
        if (ext_x1 <= x && ext_y1 <= y && ext_x2 >= x + width && ext_y2 >= y + height)
            return 1;
        break;
    }

    cairo_test_log ("Error: %s; %s extents (%f, %f) x (%f, %f) should %s (%f, %f) x (%f, %f)\n",
                    message, type_string,
                    ext_x1, ext_y1, ext_x2 - ext_x1, ext_y2 - ext_y1,
                    relation_string,
                    x, y, width, height);
    return 0;
}

static cairo_test_status_t
draw (cairo_t *cr, int width, int height)
{
    cairo_surface_t *surface;
    cairo_t         *cr2;
    const char      *phase;
    cairo_text_extents_t extents, scaled_font_extents;
    cairo_test_status_t ret = CAIRO_TEST_SUCCESS;

    surface = cairo_surface_create_similar (cairo_get_group_target (cr),
                                            CAIRO_CONTENT_COLOR, 100, 100);
    /* don't use cr accidentally */
    cr = NULL;
    cr2 = cairo_create (surface);
    cairo_surface_destroy (surface);

    cairo_set_line_width (cr2, 10);
    cairo_set_line_join (cr2, CAIRO_LINE_JOIN_MITER);
    cairo_set_miter_limit (cr2, 100);

    phase = "No path";
    if (!check_extents (phase, cr2, FILL, EQUALS, 0, 0, 0, 0) ||
        !check_extents (phase, cr2, STROKE, EQUALS, 0, 0, 0, 0) ||
        !check_extents (phase, cr2, PATH, EQUALS, 0, 0, 0, 0))
	ret = CAIRO_TEST_FAILURE;

    cairo_save (cr2);

    cairo_new_path (cr2);
    cairo_move_to (cr2, 200, 400);
    cairo_rel_line_to (cr2, 0., 0.);
    phase = "Degenerate line";
    if (!check_extents (phase, cr2, FILL, EQUALS, 0, 0, 0, 0) ||
        !check_extents (phase, cr2, STROKE, EQUALS, 0, 0, 0, 0))
	ret = CAIRO_TEST_FAILURE;

    cairo_new_path (cr2);
    cairo_move_to (cr2, 200, 400);
    cairo_rel_curve_to (cr2, 0., 0., 0., 0., 0., 0.);
    phase = "Degenerate curve";
    if (!check_extents (phase, cr2, FILL, EQUALS, 0, 0, 0, 0) ||
        !check_extents (phase, cr2, STROKE, EQUALS, 0, 0, 0, 0))
	ret = CAIRO_TEST_FAILURE;

    cairo_new_path (cr2);
    cairo_arc (cr2, 200, 400, 0., 0, 2 * M_PI);
    phase = "Degenerate arc (R=0)";
    if (!check_extents (phase, cr2, FILL, EQUALS, 0, 0, 0, 0) ||
        !check_extents (phase, cr2, STROKE, EQUALS, 0, 0, 0, 0))
	ret = CAIRO_TEST_FAILURE;

    cairo_new_path (cr2);
    cairo_arc (cr2, 200, 400, 10., 0, 0);
    phase = "Degenerate arc (Θ=0)";
    if (!check_extents (phase, cr2, FILL, EQUALS, 0, 0, 0, 0) ||
        !check_extents (phase, cr2, STROKE, EQUALS, 0, 0, 0, 0))
	ret = CAIRO_TEST_FAILURE;

    cairo_new_path (cr2);
    cairo_restore (cr2);

    /* http://bugs.freedesktop.org/show_bug.cgi?id=7965 */
    phase = "A vertical, open path";
    cairo_save (cr2);
    cairo_set_line_cap (cr2, CAIRO_LINE_CAP_ROUND);
    cairo_set_line_join (cr2, CAIRO_LINE_JOIN_ROUND);
    cairo_move_to (cr2, 0, 180);
    cairo_line_to (cr2, 750, 180);
    if (!check_extents (phase, cr2, FILL, EQUALS, 0, 0, 0, 0) ||
        !check_extents (phase, cr2, STROKE, EQUALS, -5, 175, 760, 10) ||
        !check_extents (phase, cr2, PATH, EQUALS, 0, 180, 755, 0))
	ret = CAIRO_TEST_FAILURE;
    cairo_new_path (cr2);
    cairo_restore (cr2);

    phase = "Simple rect";
    cairo_save (cr2);
    cairo_rectangle (cr2, 10, 10, 80, 80);
    if (!check_extents (phase, cr2, FILL, EQUALS, 10, 10, 80, 80) ||
        !check_extents (phase, cr2, STROKE, EQUALS, 5, 5, 90, 90) ||
        !check_extents (phase, cr2, PATH, EQUALS, 10, 10, 80, 80))
	ret = CAIRO_TEST_FAILURE;
    cairo_new_path (cr2);
    cairo_restore (cr2);

    phase = "Two rects";
    cairo_save (cr2);
    cairo_rectangle (cr2, 10, 10, 10, 10);
    cairo_rectangle (cr2, 20, 20, 10, 10);
    if (!check_extents (phase, cr2, FILL, EQUALS, 10, 10, 20, 20) ||
        !check_extents (phase, cr2, STROKE, EQUALS, 5, 5, 30, 30) ||
        !check_extents (phase, cr2, PATH, EQUALS, 10, 10, 20, 20))
	ret = CAIRO_TEST_FAILURE;
    cairo_new_path (cr2);
    cairo_restore (cr2);

    phase = "Triangle";
    cairo_save (cr2);
    cairo_move_to (cr2, 10, 10);
    cairo_line_to (cr2, 90, 90);
    cairo_line_to (cr2, 90, 10);
    cairo_close_path (cr2);
    /* miter joins protrude 5*(1+sqrt(2)) above the top-left corner and to
       the right of the bottom-right corner */
    if (!check_extents (phase, cr2, FILL, EQUALS, 10, 10, 80, 80) ||
        !check_extents (phase, cr2, STROKE, CONTAINS, 0, 5, 95, 95) ||
        !check_extents (phase, cr2, PATH, CONTAINS, 10, 10, 80, 80))
	ret = CAIRO_TEST_FAILURE;
    cairo_new_path (cr2);
    cairo_restore (cr2);

    /* http://bugs.freedesktop.org/show_bug.cgi?id=7245 */
    phase = "Arc";
    cairo_save (cr2);
    cairo_arc (cr2, 250.0, 250.0, 157.0, 5.147, 3.432);
    cairo_set_line_width (cr2, 154.0);
    if (!check_extents (phase, cr2, STROKE, APPROX_EQUALS, 16, 38, 468, 446))
	ret = CAIRO_TEST_FAILURE;
    cairo_new_path (cr2);
    cairo_restore (cr2);

    phase = "Text";
    cairo_save (cr2);
    cairo_select_font_face (cr2, "Bitstream Vera Sans",
			    CAIRO_FONT_SLANT_NORMAL,
			    CAIRO_FONT_WEIGHT_NORMAL);
    cairo_set_font_size (cr2, 12);
    cairo_text_extents (cr2, "The quick brown fox jumped over the lazy dog.", &extents);
    /* double check that the two methods of measuring the text agree... */
    cairo_scaled_font_text_extents (cairo_get_scaled_font (cr2),
	                     "The quick brown fox jumped over the lazy dog.",
			     &scaled_font_extents);
    if (memcmp (&extents, &scaled_font_extents, sizeof (extents))) {
	cairo_test_log ("Error: cairo_text_extents() does not match cairo_scaled_font_text_extents() - font extents (%f, %f) x (%f, %f) should be (%f, %f) x (%f, %f)\n",
		        scaled_font_extents.x_bearing,
			scaled_font_extents.y_bearing,
			scaled_font_extents.width,
			scaled_font_extents.height,
			extents.x_bearing,
			extents.y_bearing,
			extents.width,
			extents.height);
	ret = CAIRO_TEST_FAILURE;
    }

    cairo_move_to (cr2, -extents.x_bearing, -extents.y_bearing);
    cairo_text_path (cr2, "The quick brown fox jumped over the lazy dog.");
    cairo_set_line_width (cr2, 2.0);
    if (!check_extents (phase, cr2, FILL, EQUALS, 0, 0, extents.width, extents.height) ||
	!check_extents (phase, cr2, STROKE, EQUALS, -1, -1, extents.width+2, extents.height+2) ||
	!check_extents (phase, cr2, PATH, EQUALS, 0, 0, extents.width, extents.height))
	ret = CAIRO_TEST_FAILURE;
    cairo_new_path (cr2);
    cairo_restore (cr2);

    phase = "User space, simple scale, getting extents with same transform";
    cairo_save (cr2);
    cairo_scale (cr2, 2, 2);
    cairo_rectangle (cr2, 5, 5, 40, 40);
    if (!check_extents (phase, cr2, FILL, EQUALS, 5, 5, 40, 40) ||
        !check_extents (phase, cr2, STROKE, EQUALS, 0, 0, 50, 50) ||
        !check_extents (phase, cr2, PATH, EQUALS, 5, 5, 40, 40))
	ret = CAIRO_TEST_FAILURE;
    cairo_new_path (cr2);
    cairo_restore (cr2);

    phase = "User space, simple scale, getting extents with no transform";
    cairo_save (cr2);
    cairo_save (cr2);
    cairo_scale (cr2, 2, 2);
    cairo_rectangle (cr2, 5, 5, 40, 40);
    cairo_restore (cr2);
    if (!check_extents (phase, cr2, FILL, EQUALS, 10, 10, 80, 80) ||
        !check_extents (phase, cr2, STROKE, EQUALS, 5, 5, 90, 90) ||
        !check_extents (phase, cr2, PATH, EQUALS, 10, 10, 80, 80))
	ret = CAIRO_TEST_FAILURE;
    cairo_new_path (cr2);
    cairo_restore (cr2);

    phase = "User space, rotation, getting extents with transform";
    cairo_save (cr2);
    cairo_rectangle (cr2, -50, -50, 50, 50);
    cairo_rotate (cr2, -M_PI/4);
    /* the path in user space is now (nearly) the square rotated by
       45 degrees about the origin. Thus its x1 and x2 are both nearly 0.
       This should show any bugs where we just transform device-space
       x1,y1 and x2,y2 to get the extents. */
    /* The largest axis-aligned square inside the rotated path has
       side lengths 50*sqrt(2), so a bit over 35 on either side of
       the axes. With the stroke width added to the rotated path,
       the largest axis-aligned square is a bit over 38 on either side of
       the axes. */
    if (!check_extents (phase, cr2, FILL, CONTAINS, -35, -35, 35, 35) ||
        !check_extents (phase, cr2, STROKE, CONTAINS, -38, -38, 38, 38) ||
        !check_extents (phase, cr2, PATH, CONTAINS, -35, -35, 35, 35))
	ret = CAIRO_TEST_FAILURE;
    cairo_new_path (cr2);
    cairo_restore (cr2);

    cairo_destroy (cr2);
    return ret;
}

int
main (void)
{
    return cairo_test (&test);
}