bf83e312b1095f4db48bd1365087d1c3a5cfef4b
[framework/graphics/cairo.git] / test / fallback-resolution.c
1 /*
2  * Copyright © 2006 Red Hat, Inc.
3  * Copyright © 2008 Chris Wilson
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  * Author: Carl D. Worth <cworth@cworth.org>
25  *         Chris Wilson <chris@chris-wilson.co.uk>
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <cairo.h>
35 #include <cairo-pdf.h>
36
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #include <errno.h>
40 #endif
41 #if HAVE_SYS_STAT_H
42 #include <sys/stat.h>
43 #endif
44
45 #include "cairo-test.h"
46 #include "buffer-diff.h"
47
48 /* This test exists to test cairo_surface_set_fallback_resolution
49  *
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
53  *          automated..
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...
57  */
58
59 #define INCHES_TO_POINTS(in) ((in) * 72.0)
60 #define SIZE INCHES_TO_POINTS(2)
61
62 /* cairo_set_tolerance() is not respected by the PS/PDF backends currently */
63 #define SET_TOLERANCE 0
64
65 #define GENERATE_REFERENCE 0
66
67 static void
68 draw (cairo_t *cr, double width, double height)
69 {
70     const char *text = "cairo";
71     cairo_text_extents_t extents;
72     const double dash[2] = { 8, 16 };
73     cairo_pattern_t *pattern;
74
75     cairo_save (cr);
76
77     cairo_new_path (cr);
78
79     cairo_set_line_width (cr, .05 * SIZE / 2.0);
80
81     cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
82                0.875 * SIZE / 2.0,
83                0, 2.0 * M_PI);
84     cairo_stroke (cr);
85
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
89      */
90     cairo_save (cr);
91     cairo_set_dash (cr, dash, 2, 0);
92     cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
93                0.75 * SIZE / 2.0,
94                0, 2.0 * M_PI);
95     cairo_stroke (cr);
96     cairo_restore (cr);
97
98     cairo_save (cr);
99     cairo_rectangle (cr, 0, 0, SIZE/2, SIZE);
100     cairo_clip (cr);
101     cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
102                0.6 * SIZE / 2.0,
103                0, 2.0 * M_PI);
104     cairo_fill (cr);
105     cairo_restore (cr);
106
107     /* use a pattern to exercise bug:
108      *   https://bugs.launchpad.net/inkscape/+bug/234546
109      */
110     cairo_save (cr);
111     cairo_rectangle (cr, SIZE/2, 0, SIZE/2, SIZE);
112     cairo_clip (cr);
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,
119                0.6 * SIZE / 2.0,
120                0, 2.0 * M_PI);
121     cairo_fill (cr);
122     cairo_restore (cr);
123
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);
130
131     cairo_restore (cr);
132 }
133
134 static void
135 _xunlink (const cairo_test_context_t *ctx, const char *pathname)
136 {
137     if (unlink (pathname) < 0 && errno != ENOENT) {
138         cairo_test_log (ctx, "Error: Cannot remove %s: %s\n",
139                         pathname, strerror (errno));
140         exit (1);
141     }
142 }
143
144 static cairo_bool_t
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)
150 {
151     const char *format;
152     char *ref_name;
153     char *png_name;
154     char *diff_name;
155     cairo_surface_t *test_image, *ref_image, *diff_image;
156     buffer_diff_result_t result;
157     cairo_status_t status;
158     cairo_bool_t ret;
159
160     /* XXX log target, OUTPUT, REFERENCE, DIFFERENCE for index.html */
161
162     if (target->finish_surface != NULL) {
163         status = target->finish_surface (surface);
164         if (status) {
165             cairo_test_log (ctx, "Error: Failed to finish surface: %s\n",
166                     cairo_status_to_string (status));
167             cairo_surface_destroy (surface);
168             return FALSE;
169         }
170     }
171
172     xasprintf (&png_name,  "%s.out.png", base_name);
173     xasprintf (&diff_name, "%s.diff.png", base_name);
174
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);
180         free (png_name);
181         free (diff_name);
182         return FALSE;
183     }
184
185     _xunlink (ctx, png_name);
186     status = cairo_surface_write_to_png (test_image, png_name);
187     if (status) {
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);
191         free (png_name);
192         free (diff_name);
193         return FALSE;
194     }
195
196     format = cairo_boilerplate_content_name (target->content);
197     ref_name = cairo_test_reference_filename (ctx,
198                                               base_name,
199                                               test_name,
200                                               target->name,
201                                               target->basename,
202                                               format,
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",
207                         base_name);
208         cairo_surface_destroy (test_image);
209         free (png_name);
210         free (diff_name);
211         return FALSE;
212     }
213
214
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",
219                         ref_name,
220                 cairo_status_to_string (cairo_surface_status (ref_image)));
221         cairo_surface_destroy (ref_image);
222         cairo_surface_destroy (test_image);
223         free (png_name);
224         free (diff_name);
225         free (ref_name);
226         return FALSE;
227     }
228
229     diff_image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
230             SIZE, SIZE);
231
232     ret = TRUE;
233     status = image_diff (ctx,
234             test_image, ref_image, diff_image,
235             &result);
236     _xunlink (ctx, diff_name);
237     if (status) {
238         cairo_test_log (ctx, "Error: Failed to compare images: %s\n",
239                         cairo_status_to_string (status));
240         ret = FALSE;
241     } else if (image_diff_is_failure (&result, target->error_tolerance))
242     {
243         ret = FALSE;
244
245         status = cairo_surface_write_to_png (diff_image, diff_name);
246         if (status) {
247             cairo_test_log (ctx, "Error: Failed to write differences image: %s\n",
248                     cairo_status_to_string (status));
249         }
250     }
251
252     cairo_surface_destroy (test_image);
253     cairo_surface_destroy (diff_image);
254     free (png_name);
255     free (diff_name);
256     free (ref_name);
257
258     return ret;
259 }
260
261 #if GENERATE_REFERENCE
262 static void
263 generate_reference (double ppi_x, double ppi_y, const char *filename)
264 {
265     cairo_surface_t *surface, *target;
266     cairo_t *cr;
267     cairo_status_t status;
268
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);
273
274     /* As we wish to mimic a PDF surface, copy across the default font options
275      * from the PDF backend.
276      */
277     {
278         cairo_surface_t *pdf;
279         cairo_font_options_t *options;
280
281         options = cairo_font_options_create ();
282
283         pdf = cairo_pdf_surface_create ("tmp.pdf", 1, 1);
284         cairo_surface_get_font_options (pdf, options);
285         cairo_surface_destroy (pdf);
286
287         cairo_set_font_options (cr, options);
288         cairo_font_options_destroy (options);
289     }
290
291 #if SET_TOLERANCE
292     cairo_set_tolerance (cr, 3.0);
293 #endif
294
295     cairo_save (cr); {
296         cairo_set_source_rgb (cr, 1, 1, 1);
297         cairo_paint (cr);
298     } cairo_restore (cr);
299
300     cairo_scale (cr, ppi_x/72., ppi_y/72.);
301     draw (cr, SIZE, SIZE);
302
303     surface = cairo_surface_reference (cairo_get_target (cr));
304     cairo_destroy (cr);
305
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);
310     cairo_paint (cr);
311
312     status = cairo_surface_write_to_png (cairo_get_target (cr), filename);
313     cairo_destroy (cr);
314
315     if (status) {
316         fprintf (stderr, "Failed to generate reference image '%s': %s\n",
317                  filename, cairo_status_to_string (status));
318         exit (1);
319     }
320 }
321 #endif
322
323 static cairo_bool_t
324 _cairo_test_mkdir (const char *path)
325 {
326 #if ! HAVE_MKDIR
327     return FALSE;
328 #elif HAVE_MKDIR == 1
329     if (mkdir (path) == 0)
330         return TRUE;
331 #elif HAVE_MKDIR == 2
332     if (mkdir (path, 0770) == 0)
333         return TRUE;
334 #else
335 #error Bad value for HAVE_MKDIR
336 #endif
337
338     return errno == EEXIST;
339 }
340
341 static cairo_test_status_t
342 preamble (cairo_test_context_t *ctx)
343 {
344     cairo_t *cr;
345     cairo_test_status_t ret = CAIRO_TEST_UNTESTED;
346     struct {
347         double x, y;
348     } ppi[] = {
349         { 576, 576 },
350         { 576, 72 },
351
352         { 288, 288 },
353         { 288, 72 },
354
355         { 144, 144 },
356         { 144, 72 },
357
358         { 72, 576 },
359         { 72, 288 },
360         { 72, 144 },
361         { 72, 72 },
362     };
363     unsigned int i;
364     int n, num_ppi;
365     const char *path = _cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
366
367     num_ppi = ARRAY_LENGTH (ppi);
368
369 #if GENERATE_REFERENCE
370     for (n = 0; n < num_ppi; n++) {
371         char *ref_name;
372         xasprintf (&ref_name, "reference/fallback-resolution.ppi%gx%g.ref.png",
373                    ppi[n].x, ppi[n].y);
374         generate_reference (ppi[n].x, ppi[n].y, ref_name);
375         free (ref_name);
376     }
377 #endif
378
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;
382         char *base_name;
383         void *closure;
384         const char *format;
385         cairo_status_t status;
386
387         if (! target->is_vector)
388             continue;
389
390         if (! cairo_test_is_target_enabled (ctx, target->name))
391             continue;
392
393         format = cairo_boilerplate_content_name (target->content);
394         xasprintf (&base_name, "%s/fallback-resolution.%s.%s",
395                    path, target->name,
396                    format);
397
398         surface = (target->create_surface) (base_name,
399                                             target->content,
400                                             SIZE, SIZE,
401                                             SIZE, SIZE,
402                                             CAIRO_BOILERPLATE_MODE_TEST,
403                                             &closure);
404
405         if (surface == NULL) {
406             free (base_name);
407             continue;
408         }
409
410         if (ret == CAIRO_TEST_UNTESTED)
411             ret = CAIRO_TEST_SUCCESS;
412
413         cairo_surface_destroy (surface);
414         if (target->cleanup)
415             target->cleanup (closure);
416         free (base_name);
417
418         /* we need to recreate the surface for each resolution as we include
419          * SVG in testing which does not support the paginated interface.
420          */
421         for (n = 0; n < num_ppi; n++) {
422             char *test_name;
423             cairo_bool_t pass;
424
425             xasprintf (&test_name, "fallback-resolution.ppi%gx%g",
426                        ppi[n].x, ppi[n].y);
427             xasprintf (&base_name, "%s/%s.%s.%s",
428                        path, test_name,
429                        target->name,
430                        format);
431
432             surface = (target->create_surface) (base_name,
433                                                 target->content,
434                                                 SIZE + 25, SIZE + 25,
435                                                 SIZE + 25, SIZE + 25,
436                                                 CAIRO_BOILERPLATE_MODE_TEST,
437                                                 &closure);
438             if (surface == NULL || cairo_surface_status (surface)) {
439                 cairo_test_log (ctx, "Failed to generate surface: %s.%s\n",
440                                 target->name,
441                                 format);
442                 free (base_name);
443                 free (test_name);
444                 ret = CAIRO_TEST_FAILURE;
445                 continue;
446             }
447
448             cairo_test_log (ctx,
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);
452             fflush (stdout);
453
454             if (target->force_fallbacks != NULL)
455                 target->force_fallbacks (surface, ppi[n].x, ppi[n].y);
456             cr = cairo_create (surface);
457 #if SET_TOLERANCE
458             cairo_set_tolerance (cr, 3.0);
459 #endif
460
461             cairo_surface_set_device_offset (surface, 25, 25);
462
463             cairo_save (cr); {
464                 cairo_set_source_rgb (cr, 1, 1, 1);
465                 cairo_paint (cr);
466             } cairo_restore (cr);
467
468             /* First draw the top half in a conventional way. */
469             cairo_save (cr); {
470                 cairo_rectangle (cr, 0, 0, SIZE, SIZE / 2.0);
471                 cairo_clip (cr);
472
473                 draw (cr, SIZE, SIZE);
474             } cairo_restore (cr);
475
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). */
479             cairo_save (cr); {
480                 cairo_rectangle (cr, 0, SIZE / 2.0, SIZE, SIZE / 2.0);
481                 cairo_clip (cr);
482
483                 cairo_push_group (cr); {
484                     draw (cr, SIZE, SIZE);
485                 } cairo_pop_group_to_source (cr);
486
487                 cairo_paint (cr);
488             } cairo_restore (cr);
489
490             status = cairo_status (cr);
491             cairo_destroy (cr);
492
493             pass = FALSE;
494             if (status) {
495                 cairo_test_log (ctx, "Error: Failed to create target surface: %s\n",
496                                 cairo_status_to_string (status));
497                 ret = CAIRO_TEST_FAILURE;
498             } else {
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;
502                 else
503                     pass = TRUE;
504             }
505             cairo_surface_destroy (surface);
506             if (target->cleanup)
507                 target->cleanup (closure);
508
509             free (base_name);
510             free (test_name);
511
512             if (pass) {
513                 printf ("PASS\n");
514             } else {
515                 printf ("FAIL\n");
516             }
517             fflush (stdout);
518         }
519     }
520
521     return ret;
522 }
523
524 CAIRO_TEST (fallback_resolution,
525             "Check handling of fallback resolutions",
526             "fallback", /* keywords */
527             NULL, /* requirements */
528             0, 0,
529             preamble, NULL)