2 * Copyright © 2006 Red Hat, Inc.
3 * Copyright © 2008 Chris Wilson
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.
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.
24 * Author: Carl D. Worth <cworth@cworth.org>
25 * Chris Wilson <chris@chris-wilson.co.uk>
35 #include <cairo-pdf.h>
45 #include "cairo-test.h"
46 #include "buffer-diff.h"
48 /* This test exists to test cairo_surface_set_fallback_resolution
50 * <behdad> one more thing.
51 * if you can somehow incorporate cairo_show_page stuff in the
52 * test suite. such that fallback-resolution can actually be
54 * if we could get a callback on surface when that function is
55 * called, we could do cool stuff like making other backends
56 * draw a long strip of images, one for each page...
59 #define INCHES_TO_POINTS(in) ((in) * 72.0)
60 #define SIZE INCHES_TO_POINTS(2)
62 /* cairo_set_tolerance() is not respected by the PS/PDF backends currently */
63 #define SET_TOLERANCE 0
65 #define GENERATE_REFERENCE 0
68 draw (cairo_t *cr, double width, double height)
70 const char *text = "cairo";
71 cairo_text_extents_t extents;
72 const double dash[2] = { 8, 16 };
73 cairo_pattern_t *pattern;
79 cairo_set_line_width (cr, .05 * SIZE / 2.0);
81 cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
86 /* use dashes to demonstrate bugs:
87 * https://bugs.freedesktop.org/show_bug.cgi?id=9189
88 * https://bugs.freedesktop.org/show_bug.cgi?id=17223
91 cairo_set_dash (cr, dash, 2, 0);
92 cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
99 cairo_rectangle (cr, 0, 0, SIZE/2, SIZE);
101 cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
107 /* use a pattern to exercise bug:
108 * https://bugs.launchpad.net/inkscape/+bug/234546
111 cairo_rectangle (cr, SIZE/2, 0, SIZE/2, SIZE);
113 pattern = cairo_pattern_create_linear (SIZE/2, 0, SIZE, 0);
114 cairo_pattern_add_color_stop_rgba (pattern, 0, 0, 0, 0, 1.);
115 cairo_pattern_add_color_stop_rgba (pattern, 1, 0, 0, 0, 0.);
116 cairo_set_source (cr, pattern);
117 cairo_pattern_destroy (pattern);
118 cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
124 cairo_set_source_rgb (cr, 1, 1, 1); /* white */
125 cairo_set_font_size (cr, .25 * SIZE / 2.0);
126 cairo_text_extents (cr, text, &extents);
127 cairo_move_to (cr, (SIZE-extents.width)/2.0-extents.x_bearing,
128 (SIZE-extents.height)/2.0-extents.y_bearing);
129 cairo_show_text (cr, text);
135 _xunlink (const cairo_test_context_t *ctx, const char *pathname)
137 if (unlink (pathname) < 0 && errno != ENOENT) {
138 cairo_test_log (ctx, "Error: Cannot remove %s: %s\n",
139 pathname, strerror (errno));
145 check_result (cairo_test_context_t *ctx,
146 const cairo_boilerplate_target_t *target,
147 const char *test_name,
148 const char *base_name,
149 cairo_surface_t *surface)
155 cairo_surface_t *test_image, *ref_image, *diff_image;
156 buffer_diff_result_t result;
157 cairo_status_t status;
160 /* XXX log target, OUTPUT, REFERENCE, DIFFERENCE for index.html */
162 if (target->finish_surface != NULL) {
163 status = target->finish_surface (surface);
165 cairo_test_log (ctx, "Error: Failed to finish surface: %s\n",
166 cairo_status_to_string (status));
167 cairo_surface_destroy (surface);
172 xasprintf (&png_name, "%s.out.png", base_name);
173 xasprintf (&diff_name, "%s.diff.png", base_name);
175 test_image = target->get_image_surface (surface, 0, SIZE, SIZE);
176 if (cairo_surface_status (test_image)) {
177 cairo_test_log (ctx, "Error: Failed to extract page: %s\n",
178 cairo_status_to_string (cairo_surface_status (test_image)));
179 cairo_surface_destroy (test_image);
185 _xunlink (ctx, png_name);
186 status = cairo_surface_write_to_png (test_image, png_name);
188 cairo_test_log (ctx, "Error: Failed to write output image: %s\n",
189 cairo_status_to_string (status));
190 cairo_surface_destroy (test_image);
196 format = cairo_boilerplate_content_name (target->content);
197 ref_name = cairo_test_reference_filename (ctx,
203 CAIRO_TEST_REF_SUFFIX,
204 CAIRO_TEST_PNG_EXTENSION);
205 if (ref_name == NULL) {
206 cairo_test_log (ctx, "Error: Cannot find reference image for %s\n",
208 cairo_surface_destroy (test_image);
215 ref_image = cairo_test_get_reference_image (ctx, ref_name,
216 target->content == CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED);
217 if (cairo_surface_status (ref_image)) {
218 cairo_test_log (ctx, "Error: Cannot open reference image for %s: %s\n",
220 cairo_status_to_string (cairo_surface_status (ref_image)));
221 cairo_surface_destroy (ref_image);
222 cairo_surface_destroy (test_image);
229 diff_image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
233 status = image_diff (ctx,
234 test_image, ref_image, diff_image,
236 _xunlink (ctx, diff_name);
238 cairo_test_log (ctx, "Error: Failed to compare images: %s\n",
239 cairo_status_to_string (status));
241 } else if (image_diff_is_failure (&result, target->error_tolerance))
245 status = cairo_surface_write_to_png (diff_image, diff_name);
247 cairo_test_log (ctx, "Error: Failed to write differences image: %s\n",
248 cairo_status_to_string (status));
252 cairo_surface_destroy (test_image);
253 cairo_surface_destroy (diff_image);
261 #if GENERATE_REFERENCE
263 generate_reference (double ppi_x, double ppi_y, const char *filename)
265 cairo_surface_t *surface, *target;
267 cairo_status_t status;
269 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
270 SIZE*ppi_x/72, SIZE*ppi_y/72);
271 cr = cairo_create (surface);
272 cairo_surface_destroy (surface);
274 /* As we wish to mimic a PDF surface, copy across the default font options
275 * from the PDF backend.
278 cairo_surface_t *pdf;
279 cairo_font_options_t *options;
281 options = cairo_font_options_create ();
283 pdf = cairo_pdf_surface_create ("tmp.pdf", 1, 1);
284 cairo_surface_get_font_options (pdf, options);
285 cairo_surface_destroy (pdf);
287 cairo_set_font_options (cr, options);
288 cairo_font_options_destroy (options);
292 cairo_set_tolerance (cr, 3.0);
296 cairo_set_source_rgb (cr, 1, 1, 1);
298 } cairo_restore (cr);
300 cairo_scale (cr, ppi_x/72., ppi_y/72.);
301 draw (cr, SIZE, SIZE);
303 surface = cairo_surface_reference (cairo_get_target (cr));
306 target = cairo_image_surface_create (CAIRO_FORMAT_RGB24, SIZE, SIZE);
307 cr = cairo_create (target);
308 cairo_scale (cr, 72./ppi_x, 72./ppi_y);
309 cairo_set_source_surface (cr, surface, 0, 0);
312 status = cairo_surface_write_to_png (cairo_get_target (cr), filename);
316 fprintf (stderr, "Failed to generate reference image '%s': %s\n",
317 filename, cairo_status_to_string (status));
324 _cairo_test_mkdir (const char *path)
328 #elif HAVE_MKDIR == 1
329 if (mkdir (path) == 0)
331 #elif HAVE_MKDIR == 2
332 if (mkdir (path, 0770) == 0)
335 #error Bad value for HAVE_MKDIR
338 return errno == EEXIST;
341 static cairo_test_status_t
342 preamble (cairo_test_context_t *ctx)
345 cairo_test_status_t ret = CAIRO_TEST_UNTESTED;
365 const char *path = _cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
367 num_ppi = ARRAY_LENGTH (ppi);
369 #if GENERATE_REFERENCE
370 for (n = 0; n < num_ppi; n++) {
372 xasprintf (&ref_name, "reference/fallback-resolution.ppi%gx%g.ref.png",
374 generate_reference (ppi[n].x, ppi[n].y, ref_name);
379 for (i = 0; i < ctx->num_targets; i++) {
380 const cairo_boilerplate_target_t *target = ctx->targets_to_test[i];
381 cairo_surface_t *surface = NULL;
385 cairo_status_t status;
387 if (! target->is_vector)
390 if (! cairo_test_is_target_enabled (ctx, target->name))
393 format = cairo_boilerplate_content_name (target->content);
394 xasprintf (&base_name, "%s/fallback-resolution.%s.%s",
398 surface = (target->create_surface) (base_name,
402 CAIRO_BOILERPLATE_MODE_TEST,
405 if (surface == NULL) {
410 if (ret == CAIRO_TEST_UNTESTED)
411 ret = CAIRO_TEST_SUCCESS;
413 cairo_surface_destroy (surface);
415 target->cleanup (closure);
418 /* we need to recreate the surface for each resolution as we include
419 * SVG in testing which does not support the paginated interface.
421 for (n = 0; n < num_ppi; n++) {
425 xasprintf (&test_name, "fallback-resolution.ppi%gx%g",
427 xasprintf (&base_name, "%s/%s.%s.%s",
432 surface = (target->create_surface) (base_name,
434 SIZE + 25, SIZE + 25,
435 SIZE + 25, SIZE + 25,
436 CAIRO_BOILERPLATE_MODE_TEST,
438 if (surface == NULL || cairo_surface_status (surface)) {
439 cairo_test_log (ctx, "Failed to generate surface: %s.%s\n",
444 ret = CAIRO_TEST_FAILURE;
449 "Testing fallback-resolution %gx%g with %s target\n",
450 ppi[n].x, ppi[n].y, target->name);
451 printf ("%s:\t", base_name);
454 if (target->force_fallbacks != NULL)
455 target->force_fallbacks (surface, ppi[n].x, ppi[n].y);
456 cr = cairo_create (surface);
458 cairo_set_tolerance (cr, 3.0);
461 cairo_surface_set_device_offset (surface, 25, 25);
464 cairo_set_source_rgb (cr, 1, 1, 1);
466 } cairo_restore (cr);
468 /* First draw the top half in a conventional way. */
470 cairo_rectangle (cr, 0, 0, SIZE, SIZE / 2.0);
473 draw (cr, SIZE, SIZE);
474 } cairo_restore (cr);
476 /* Then draw the bottom half in a separate group,
477 * (exposing a bug in 1.6.4 with the group not being
478 * rendered with the correct fallback resolution). */
480 cairo_rectangle (cr, 0, SIZE / 2.0, SIZE, SIZE / 2.0);
483 cairo_push_group (cr); {
484 draw (cr, SIZE, SIZE);
485 } cairo_pop_group_to_source (cr);
488 } cairo_restore (cr);
490 status = cairo_status (cr);
495 cairo_test_log (ctx, "Error: Failed to create target surface: %s\n",
496 cairo_status_to_string (status));
497 ret = CAIRO_TEST_FAILURE;
499 /* extract the image and compare it to our reference */
500 if (! check_result (ctx, target, test_name, base_name, surface))
501 ret = CAIRO_TEST_FAILURE;
505 cairo_surface_destroy (surface);
507 target->cleanup (closure);
524 CAIRO_TEST (fallback_resolution,
525 "Check handling of fallback resolutions",
526 "fallback", /* keywords */
527 NULL, /* requirements */