1 /*
2 * Copyright © 2005 Red Hat, Inc.
3 * Copyright © 2011 Intel Corporation
4 *
5 * Permission to use, copy, modify, distribute, and sell this software
6 * and its documentation for any purpose is hereby granted without
7 * fee, provided that the above copyright notice appear in all copies
8 * and that both that copyright notice and this permission notice
9 * appear in supporting documentation, and that the name of
10 * Red Hat, Inc. not be used in advertising or publicity pertaining to
11 * distribution of the software without specific, written prior
12 * permission. Red Hat, Inc. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as
14 * is" without express or implied warranty.
15 *
16 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
19 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 *
24 * Authors:
25 * Carl D. Worth <cworth@cworth.org>
26 * Chris Wilson <chris@chris-wilson.co.uk>
27 */
28
29 #include "cairo-test.h"
30
31 #define TEXT_SIZE 12
32 #define SIZE 60 /* needs to be big to check large area effects (dithering) */
33 #define PAD 2
34
35 #define TT_SIZE 100
36 #define TT_PAD 5
37 #define TT_FONT_SIZE 32.0
38
39 #define GENERATE_REF 0
40
41 static uint32_t data[16] = {
42 0xffffffff, 0xffffffff, 0xffff0000, 0xffff0000,
43 0xffffffff, 0xffffffff, 0xffff0000, 0xffff0000,
44
45 0xff00ff00, 0xff00ff00, 0xff0000ff, 0xff0000ff,
46 0xff00ff00, 0xff00ff00, 0xff0000ff, 0xff0000ff
47 };
48
49 static const char *png_filename = "romedalen.png";
50
51 static cairo_t *
paint(cairo_t * cr)52 paint (cairo_t *cr)
53 {
54 cairo_set_source_rgb (cr, 0, 0, 1);
55 cairo_paint (cr);
56
57 cairo_translate (cr, 2, 2);
58 cairo_scale (cr, 0.5, 0.5);
59
60 cairo_set_source_rgb (cr, 1, 0, 0);
61 cairo_paint (cr);
62
63 return cr;
64 }
65
66 static cairo_t *
paint_alpha(cairo_t * cr)67 paint_alpha (cairo_t *cr)
68 {
69 cairo_surface_t *surface;
70
71 surface = cairo_image_surface_create_for_data ((unsigned char *) data,
72 CAIRO_FORMAT_RGB24, 4, 4, 16);
73
74 cairo_test_paint_checkered (cr);
75
76 cairo_scale (cr, 4, 4);
77
78 cairo_set_source_surface (cr, surface, 2 , 2);
79 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
80 cairo_paint_with_alpha (cr, 0.5);
81
82 cairo_surface_finish (surface); /* data will go out of scope */
83 cairo_surface_destroy (surface);
84
85 return cr;
86 }
87
88 static cairo_t *
paint_alpha_solid_clip(cairo_t * cr)89 paint_alpha_solid_clip (cairo_t *cr)
90 {
91 cairo_test_paint_checkered (cr);
92
93 cairo_rectangle (cr, 2.5, 2.5, 27, 27);
94 cairo_clip (cr);
95
96 cairo_set_source_rgb (cr, 1., 0.,0.);
97 cairo_paint_with_alpha (cr, 0.5);
98
99 return cr;
100 }
101
102 static cairo_t *
paint_alpha_clip(cairo_t * cr)103 paint_alpha_clip (cairo_t *cr)
104 {
105 cairo_surface_t *surface;
106
107 surface = cairo_image_surface_create_for_data ((unsigned char *) data,
108 CAIRO_FORMAT_RGB24, 4, 4, 16);
109
110 cairo_test_paint_checkered (cr);
111
112 cairo_rectangle (cr, 10.5, 10.5, 11, 11);
113 cairo_clip (cr);
114
115 cairo_scale (cr, 4, 4);
116
117 cairo_set_source_surface (cr, surface, 2 , 2);
118 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
119 cairo_paint_with_alpha (cr, 0.5);
120
121 cairo_surface_finish (surface); /* data will go out of scope */
122 cairo_surface_destroy (surface);
123
124 return cr;
125 }
126
127 static cairo_t *
paint_alpha_clip_mask(cairo_t * cr)128 paint_alpha_clip_mask (cairo_t *cr)
129 {
130 cairo_surface_t *surface;
131
132 surface = cairo_image_surface_create_for_data ((unsigned char *) data,
133 CAIRO_FORMAT_RGB24, 4, 4, 16);
134
135 cairo_test_paint_checkered (cr);
136
137 cairo_move_to (cr, 16, 5);
138 cairo_line_to (cr, 5, 16);
139 cairo_line_to (cr, 16, 27);
140 cairo_line_to (cr, 27, 16);
141 cairo_clip (cr);
142
143 cairo_scale (cr, 4, 4);
144
145 cairo_set_source_surface (cr, surface, 2 , 2);
146 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_NEAREST);
147 cairo_paint_with_alpha (cr, 0.5);
148
149 cairo_surface_finish (surface); /* data will go out of scope */
150 cairo_surface_destroy (surface);
151
152 return cr;
153 }
154
155 static cairo_t *
select_font_face(cairo_t * cr)156 select_font_face (cairo_t *cr)
157 {
158 /* We draw in the default black, so paint white first. */
159 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* white */
160 cairo_paint (cr);
161
162 cairo_set_source_rgb (cr, 0, 0, 0); /* black */
163
164 cairo_set_font_size (cr, TEXT_SIZE);
165 cairo_move_to (cr, 0, TEXT_SIZE);
166
167 cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Serif",
168 CAIRO_FONT_SLANT_NORMAL,
169 CAIRO_FONT_WEIGHT_NORMAL);
170 cairo_show_text (cr, "i-am-serif");
171
172 cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
173 CAIRO_FONT_SLANT_NORMAL,
174 CAIRO_FONT_WEIGHT_NORMAL);
175 cairo_show_text (cr, " i-am-sans");
176
177 cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans Mono",
178 CAIRO_FONT_SLANT_NORMAL,
179 CAIRO_FONT_WEIGHT_NORMAL);
180 cairo_show_text (cr, " i-am-mono");
181
182 return cr;
183 }
184
185 static cairo_t *
fill_alpha(cairo_t * cr)186 fill_alpha (cairo_t *cr)
187 {
188 const double alpha = 1./3;
189 int n;
190
191 /* flatten to white */
192 cairo_set_source_rgb (cr, 1, 1, 1);
193 cairo_paint (cr);
194
195 /* square */
196 cairo_rectangle (cr, PAD, PAD, SIZE, SIZE);
197 cairo_set_source_rgba (cr, 1, 0, 0, alpha);
198 cairo_fill (cr);
199
200 /* circle */
201 cairo_translate (cr, SIZE + 2 * PAD, 0);
202 cairo_arc (cr, PAD + SIZE / 2., PAD + SIZE / 2., SIZE / 2., 0, 2 * M_PI);
203 cairo_set_source_rgba (cr, 0, 1, 0, alpha);
204 cairo_fill (cr);
205
206 /* triangle */
207 cairo_translate (cr, 0, SIZE + 2 * PAD);
208 cairo_move_to (cr, PAD + SIZE / 2, PAD);
209 cairo_line_to (cr, PAD + SIZE, PAD + SIZE);
210 cairo_line_to (cr, PAD, PAD + SIZE);
211 cairo_set_source_rgba (cr, 0, 0, 1, alpha);
212 cairo_fill (cr);
213
214 /* star */
215 cairo_translate (cr, -(SIZE + 2 * PAD) + SIZE/2., SIZE/2.);
216 for (n = 0; n < 5; n++) {
217 cairo_line_to (cr,
218 SIZE/2 * cos (2*n * 2*M_PI / 10),
219 SIZE/2 * sin (2*n * 2*M_PI / 10));
220
221 cairo_line_to (cr,
222 SIZE/4 * cos ((2*n+1)*2*M_PI / 10),
223 SIZE/4 * sin ((2*n+1)*2*M_PI / 10));
224 }
225 cairo_set_source_rgba (cr, 0, 0, 0, alpha);
226 cairo_fill (cr);
227
228 return cr;
229 }
230
231 static cairo_t *
self_intersecting(cairo_t * cr)232 self_intersecting (cairo_t *cr)
233 {
234 cairo_set_source_rgb (cr, 1, 1, 1);
235 cairo_paint (cr);
236
237 cairo_translate (cr, 1.0, 1.0);
238
239 cairo_set_source_rgb (cr, 1, 0, 0); /* red */
240
241 /* First draw the desired shape with a fill */
242 cairo_rectangle (cr, 0.5, 0.5, 4.0, 4.0);
243 cairo_rectangle (cr, 3.5, 3.5, 4.0, 4.0);
244 cairo_rectangle (cr, 3.5, 1.5, -2.0, 2.0);
245 cairo_rectangle (cr, 6.5, 4.5, -2.0, 2.0);
246
247 cairo_fill (cr);
248
249 /* Then try the same thing with a stroke */
250 cairo_translate (cr, 0, 10);
251 cairo_move_to (cr, 1.0, 1.0);
252 cairo_rel_line_to (cr, 3.0, 0.0);
253 cairo_rel_line_to (cr, 0.0, 6.0);
254 cairo_rel_line_to (cr, 3.0, 0.0);
255 cairo_rel_line_to (cr, 0.0, -3.0);
256 cairo_rel_line_to (cr, -6.0, 0.0);
257 cairo_close_path (cr);
258
259 cairo_set_line_width (cr, 1.0);
260 cairo_stroke (cr);
261
262 return cr;
263 }
264
265 static void
draw_text_transform(cairo_t * cr)266 draw_text_transform (cairo_t *cr)
267 {
268 cairo_matrix_t tm;
269
270 /* skew */
271 cairo_matrix_init (&tm, 1, 0,
272 -0.25, 1,
273 0, 0);
274 cairo_matrix_scale (&tm, TT_FONT_SIZE, TT_FONT_SIZE);
275 cairo_set_font_matrix (cr, &tm);
276
277 cairo_new_path (cr);
278 cairo_move_to (cr, 50, TT_SIZE-TT_PAD);
279 cairo_show_text (cr, "A");
280
281 /* rotate and scale */
282 cairo_matrix_init_rotate (&tm, M_PI / 2);
283 cairo_matrix_scale (&tm, TT_FONT_SIZE, TT_FONT_SIZE * 2.0);
284 cairo_set_font_matrix (cr, &tm);
285
286 cairo_new_path (cr);
287 cairo_move_to (cr, TT_PAD, TT_PAD + 25);
288 cairo_show_text (cr, "A");
289
290 cairo_matrix_init_rotate (&tm, M_PI / 2);
291 cairo_matrix_scale (&tm, TT_FONT_SIZE * 2.0, TT_FONT_SIZE);
292 cairo_set_font_matrix (cr, &tm);
293
294 cairo_new_path (cr);
295 cairo_move_to (cr, TT_PAD, TT_PAD + 50);
296 cairo_show_text (cr, "A");
297 }
298
299 static cairo_t *
text_transform(cairo_t * cr)300 text_transform (cairo_t *cr)
301 {
302 const cairo_test_context_t *ctx = cairo_test_get_context (cr);
303 cairo_pattern_t *pattern;
304
305 cairo_set_source_rgb (cr, 1., 1., 1.);
306 cairo_paint (cr);
307
308 cairo_set_source_rgb (cr, 0., 0., 0.);
309
310 cairo_select_font_face (cr, CAIRO_TEST_FONT_FAMILY " Sans",
311 CAIRO_FONT_SLANT_NORMAL,
312 CAIRO_FONT_WEIGHT_NORMAL);
313
314 draw_text_transform (cr);
315
316 cairo_translate (cr, TT_SIZE, TT_SIZE);
317 cairo_rotate (cr, M_PI);
318
319 pattern = cairo_test_create_pattern_from_png (ctx, png_filename);
320 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
321 cairo_set_source (cr, pattern);
322 cairo_pattern_destroy (pattern);
323
324 draw_text_transform (cr);
325
326 return cr;
327 }
328
329 /* And here begins the recording and replaying... */
330
331 static cairo_t *
record_create(cairo_t * target)332 record_create (cairo_t *target)
333 {
334 cairo_surface_t *surface;
335 cairo_t *cr;
336
337 surface = cairo_recording_surface_create (cairo_surface_get_content (cairo_get_target (target)), NULL);
338 cr = cairo_test_create (surface, cairo_test_get_context (target));
339 cairo_surface_destroy (surface);
340
341 return cr;
342 }
343
344 static cairo_surface_t *
record_get(cairo_t * target)345 record_get (cairo_t *target)
346 {
347 cairo_surface_t *surface;
348
349 surface = cairo_surface_reference (cairo_get_target (target));
350 cairo_destroy (target);
351
352 return surface;
353 }
354
355 static cairo_test_status_t
record_replay(cairo_t * cr,cairo_t * (* func)(cairo_t *),int width,int height)356 record_replay (cairo_t *cr, cairo_t *(*func)(cairo_t *), int width, int height)
357 {
358 cairo_surface_t *surface;
359 int x, y;
360
361 #if GENERATE_REF
362 cairo_scale (cr, 2, 2);
363 func(cr);
364 #else
365 surface = record_get (func (record_create (cr)));
366
367 cairo_scale (cr, 2, 2);
368 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
369 cairo_set_source_surface (cr, surface, 0, 0);
370 cairo_surface_destroy (surface);
371 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_NONE);
372
373 for (y = 0; y < height; y += 2) {
374 for (x = 0; x < width; x += 2) {
375 cairo_rectangle (cr, x, y, 2, 2);
376 cairo_clip (cr);
377 cairo_paint (cr);
378 cairo_reset_clip (cr);
379 }
380 }
381 #endif
382
383 return CAIRO_TEST_SUCCESS;
384 }
385
386 static cairo_test_status_t
record_paint(cairo_t * cr,int width,int height)387 record_paint (cairo_t *cr, int width, int height)
388 {
389 return record_replay (cr, paint, width, height);
390 }
391
392 static cairo_test_status_t
record_paint_alpha(cairo_t * cr,int width,int height)393 record_paint_alpha (cairo_t *cr, int width, int height)
394 {
395 return record_replay (cr, paint_alpha, width, height);
396 }
397
398 static cairo_test_status_t
record_paint_alpha_solid_clip(cairo_t * cr,int width,int height)399 record_paint_alpha_solid_clip (cairo_t *cr, int width, int height)
400 {
401 return record_replay (cr, paint_alpha_solid_clip, width, height);
402 }
403
404 static cairo_test_status_t
record_paint_alpha_clip(cairo_t * cr,int width,int height)405 record_paint_alpha_clip (cairo_t *cr, int width, int height)
406 {
407 return record_replay (cr, paint_alpha_clip, width, height);
408 }
409
410 static cairo_test_status_t
record_paint_alpha_clip_mask(cairo_t * cr,int width,int height)411 record_paint_alpha_clip_mask (cairo_t *cr, int width, int height)
412 {
413 return record_replay (cr, paint_alpha_clip_mask, width, height);
414 }
415
416 static cairo_test_status_t
record_fill_alpha(cairo_t * cr,int width,int height)417 record_fill_alpha (cairo_t *cr, int width, int height)
418 {
419 return record_replay (cr, fill_alpha, width, height);
420 }
421
422 static cairo_test_status_t
record_self_intersecting(cairo_t * cr,int width,int height)423 record_self_intersecting (cairo_t *cr, int width, int height)
424 {
425 return record_replay (cr, self_intersecting, width, height);
426 }
427
428 static cairo_test_status_t
record_select_font_face(cairo_t * cr,int width,int height)429 record_select_font_face (cairo_t *cr, int width, int height)
430 {
431 return record_replay (cr, select_font_face, width, height);
432 }
433
434 static cairo_test_status_t
record_text_transform(cairo_t * cr,int width,int height)435 record_text_transform (cairo_t *cr, int width, int height)
436 {
437 return record_replay (cr, text_transform, width, height);
438 }
439
440 CAIRO_TEST (record2x_paint,
441 "Test replayed calls to cairo_paint",
442 "paint,record", /* keywords */
443 NULL, /* requirements */
444 2*8, 2*8,
445 NULL, record_paint)
446 CAIRO_TEST (record2x_paint_alpha,
447 "Simple test of cairo_paint_with_alpha",
448 "record, paint, alpha", /* keywords */
449 NULL, /* requirements */
450 2*32, 2*32,
451 NULL, record_paint_alpha)
452 CAIRO_TEST (record2x_paint_alpha_solid_clip,
453 "Simple test of cairo_paint_with_alpha+unaligned clip",
454 "record, paint, alpha, clip", /* keywords */
455 NULL, /* requirements */
456 2*32, 2*32,
457 NULL, record_paint_alpha_solid_clip)
458 CAIRO_TEST (record2x_paint_alpha_clip,
459 "Simple test of cairo_paint_with_alpha+unaligned clip",
460 "record, paint, alpha, clip", /* keywords */
461 NULL, /* requirements */
462 2*32, 2*32,
463 NULL, record_paint_alpha_clip)
464 CAIRO_TEST (record2x_paint_alpha_clip_mask,
465 "Simple test of cairo_paint_with_alpha+triangular clip",
466 "record, paint, alpha, clip", /* keywords */
467 NULL, /* requirements */
468 2*32, 2*32,
469 NULL, record_paint_alpha_clip_mask)
470 CAIRO_TEST (record2x_fill_alpha,
471 "Tests using set_rgba();fill()",
472 "record,fill, alpha", /* keywords */
473 NULL, /* requirements */
474 2*(2*SIZE + 4*PAD), 2*(2*SIZE + 4*PAD),
475 NULL, record_fill_alpha)
476 CAIRO_TEST (record2x_select_font_face,
477 "Tests using cairo_select_font_face to draw text in different faces",
478 "record, font", /* keywords */
479 NULL, /* requirements */
480 2*192, 2*(TEXT_SIZE + 4),
481 NULL, record_select_font_face)
482 CAIRO_TEST (record2x_self_intersecting,
483 "Test strokes of self-intersecting paths",
484 "record, stroke, trap", /* keywords */
485 NULL, /* requirements */
486 2*10, 2*20,
487 NULL, record_self_intersecting)
488 CAIRO_TEST (record2x_text_transform,
489 "Test various applications of the font matrix",
490 "record, text, transform", /* keywords */
491 NULL, /* requirements */
492 2*TT_SIZE, 2*TT_SIZE,
493 NULL, record_text_transform)
494