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
#include "config.h"
29

            
30
#include <stdio.h>
31
#include <stdlib.h>
32
#include <cairo.h>
33

            
34
#if CAIRO_HAS_PDF_SURFACE
35
#include <cairo-pdf.h>
36
#endif
37

            
38
#ifdef HAVE_UNISTD_H
39
#include <unistd.h>
40
#include <errno.h>
41
#endif
42
#if HAVE_SYS_STAT_H
43
#include <sys/stat.h>
44
#endif
45

            
46
#include "cairo-test.h"
47
#include "buffer-diff.h"
48

            
49
/* This test exists to test cairo_surface_set_fallback_resolution
50
 *
51
 * <behdad> one more thing.
52
 *          if you can somehow incorporate cairo_show_page stuff in the
53
 *          test suite.  such that fallback-resolution can actually be
54
 *          automated..
55
 *          if we could get a callback on surface when that function is
56
 *          called, we could do cool stuff like making other backends
57
 *          draw a long strip of images, one for each page...
58
 */
59

            
60
#define INCHES_TO_POINTS(in) ((in) * 72.0)
61
#define SIZE INCHES_TO_POINTS(2)
62

            
63
/* cairo_set_tolerance() is not respected by the PS/PDF backends currently */
64
#define SET_TOLERANCE 0
65

            
66
#define GENERATE_REFERENCE 0
67

            
68
static void
69
draw (cairo_t *cr, double width, double height)
70
{
71
    const char *text = "cairo";
72
    cairo_text_extents_t extents;
73
    const double dash[2] = { 8, 16 };
74
    cairo_pattern_t *pattern;
75

            
76
    cairo_save (cr);
77

            
78
    cairo_new_path (cr);
79

            
80
    cairo_set_line_width (cr, .05 * SIZE / 2.0);
81

            
82
    cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
83
	       0.875 * SIZE / 2.0,
84
	       0, 2.0 * M_PI);
85
    cairo_stroke (cr);
86

            
87
    /* use dashes to demonstrate bugs:
88
     *  https://bugs.freedesktop.org/show_bug.cgi?id=9189
89
     *  https://bugs.freedesktop.org/show_bug.cgi?id=17223
90
     */
91
    cairo_save (cr);
92
    cairo_set_dash (cr, dash, 2, 0);
93
    cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
94
	       0.75 * SIZE / 2.0,
95
	       0, 2.0 * M_PI);
96
    cairo_stroke (cr);
97
    cairo_restore (cr);
98

            
99
    cairo_save (cr);
100
    cairo_rectangle (cr, 0, 0, SIZE/2, SIZE);
101
    cairo_clip (cr);
102
    cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
103
	       0.6 * SIZE / 2.0,
104
	       0, 2.0 * M_PI);
105
    cairo_fill (cr);
106
    cairo_restore (cr);
107

            
108
    /* use a pattern to exercise bug:
109
     *   https://bugs.launchpad.net/inkscape/+bug/234546
110
     */
111
    cairo_save (cr);
112
    cairo_rectangle (cr, SIZE/2, 0, SIZE/2, SIZE);
113
    cairo_clip (cr);
114
    pattern = cairo_pattern_create_linear (SIZE/2, 0, SIZE, 0);
115
    cairo_pattern_add_color_stop_rgba (pattern, 0, 0, 0, 0, 1.);
116
    cairo_pattern_add_color_stop_rgba (pattern, 1, 0, 0, 0, 0.);
117
    cairo_set_source (cr, pattern);
118
    cairo_pattern_destroy (pattern);
119
    cairo_arc (cr, SIZE / 2.0, SIZE / 2.0,
120
	       0.6 * SIZE / 2.0,
121
	       0, 2.0 * M_PI);
122
    cairo_fill (cr);
123
    cairo_restore (cr);
124

            
125
    cairo_set_source_rgb (cr, 1, 1, 1); /* white */
126
    cairo_set_font_size (cr, .25 * SIZE / 2.0);
127
    cairo_text_extents (cr, text, &extents);
128
    cairo_move_to (cr, (SIZE-extents.width)/2.0-extents.x_bearing,
129
		       (SIZE-extents.height)/2.0-extents.y_bearing);
130
    cairo_show_text (cr, text);
131

            
132
    cairo_restore (cr);
133
}
134

            
135
static void
136
_xunlink (const cairo_test_context_t *ctx, const char *pathname)
137
{
138
    if (unlink (pathname) < 0 && errno != ENOENT) {
139
	cairo_test_log (ctx, "Error: Cannot remove %s: %s\n",
140
			pathname, strerror (errno));
141
	exit (1);
142
    }
143
}
144

            
145
static cairo_bool_t
146
check_result (cairo_test_context_t *ctx,
147
	      const cairo_boilerplate_target_t *target,
148
	      const char *test_name,
149
	      const char *base_name,
150
	      cairo_surface_t *surface)
151
{
152
    const char *format;
153
    char *ref_name;
154
    char *png_name;
155
    char *diff_name;
156
    cairo_surface_t *test_image, *ref_image, *diff_image;
157
    buffer_diff_result_t result;
158
    cairo_status_t status;
159
    cairo_bool_t ret;
160

            
161
    /* XXX log target, OUTPUT, REFERENCE, DIFFERENCE for index.html */
162

            
163
    if (target->finish_surface != NULL) {
164
	status = target->finish_surface (surface);
165
	if (status) {
166
	    cairo_test_log (ctx, "Error: Failed to finish surface: %s\n",
167
		    cairo_status_to_string (status));
168
	    cairo_surface_destroy (surface);
169
	    return FALSE;
170
	}
171
    }
172

            
173
    xasprintf (&png_name,  "%s.out.png", base_name);
174
    xasprintf (&diff_name, "%s.diff.png", base_name);
175

            
176
    test_image = target->get_image_surface (surface, 0, SIZE, SIZE);
177
    if (cairo_surface_status (test_image)) {
178
	cairo_test_log (ctx, "Error: Failed to extract page: %s\n",
179
		        cairo_status_to_string (cairo_surface_status (test_image)));
180
	cairo_surface_destroy (test_image);
181
	free (png_name);
182
	free (diff_name);
183
	return FALSE;
184
    }
185

            
186
    _xunlink (ctx, png_name);
187
    status = cairo_surface_write_to_png (test_image, png_name);
188
    if (status) {
189
	cairo_test_log (ctx, "Error: Failed to write output image: %s\n",
190
		cairo_status_to_string (status));
191
	cairo_surface_destroy (test_image);
192
	free (png_name);
193
	free (diff_name);
194
	return FALSE;
195
    }
196

            
197
    format = cairo_boilerplate_content_name (target->content);
198
    ref_name = cairo_test_reference_filename (ctx,
199
					      base_name,
200
					      test_name,
201
					      target->name,
202
					      target->basename,
203
					      format,
204
					      CAIRO_TEST_REF_SUFFIX,
205
					      CAIRO_TEST_PNG_EXTENSION);
206
    if (ref_name == NULL) {
207
	cairo_test_log (ctx, "Error: Cannot find reference image for %s\n",
208
		        base_name);
209
	cairo_surface_destroy (test_image);
210
	free (png_name);
211
	free (diff_name);
212
	return FALSE;
213
    }
214

            
215

            
216
    ref_image = cairo_test_get_reference_image (ctx, ref_name,
217
	    target->content == CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED);
218
    if (cairo_surface_status (ref_image)) {
219
	cairo_test_log (ctx, "Error: Cannot open reference image for %s: %s\n",
220
		        ref_name,
221
		cairo_status_to_string (cairo_surface_status (ref_image)));
222
	cairo_surface_destroy (ref_image);
223
	cairo_surface_destroy (test_image);
224
	free (png_name);
225
	free (diff_name);
226
	free (ref_name);
227
	return FALSE;
228
    }
229

            
230
    diff_image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
231
	    SIZE, SIZE);
232

            
233
    ret = TRUE;
234
    status = image_diff (ctx,
235
	    test_image, ref_image, diff_image,
236
	    &result);
237
    _xunlink (ctx, diff_name);
238
    if (status) {
239
	cairo_test_log (ctx, "Error: Failed to compare images: %s\n",
240
			cairo_status_to_string (status));
241
	ret = FALSE;
242
    } else if (image_diff_is_failure (&result, target->error_tolerance))
243
    {
244
	ret = FALSE;
245

            
246
	status = cairo_surface_write_to_png (diff_image, diff_name);
247
	if (status) {
248
	    cairo_test_log (ctx, "Error: Failed to write differences image: %s\n",
249
		    cairo_status_to_string (status));
250
	}
251
    }
252

            
253
    cairo_surface_destroy (test_image);
254
    cairo_surface_destroy (diff_image);
255
    free (png_name);
256
    free (diff_name);
257
    free (ref_name);
258

            
259
    return ret;
260
}
261

            
262
#if GENERATE_REFERENCE
263
static void
264
generate_reference (double ppi_x, double ppi_y, const char *filename)
265
{
266
    cairo_surface_t *surface, *target;
267
    cairo_t *cr;
268
    cairo_status_t status;
269

            
270
    surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
271
	                                  SIZE*ppi_x/72, SIZE*ppi_y/72);
272
    cr = cairo_create (surface);
273
    cairo_surface_destroy (surface);
274

            
275
    /* As we wish to mimic a PDF surface, copy across the default font options
276
     * from the PDF backend.
277
     */
278
    {
279
	cairo_surface_t *pdf;
280
	cairo_font_options_t *options;
281

            
282
	options = cairo_font_options_create ();
283

            
284
#if CAIRO_HAS_PDF_SURFACE
285
	pdf = cairo_pdf_surface_create ("tmp.pdf", 1, 1);
286
	cairo_surface_get_font_options (pdf, options);
287
	cairo_surface_destroy (pdf);
288
#endif
289

            
290
	cairo_set_font_options (cr, options);
291
	cairo_font_options_destroy (options);
292
    }
293

            
294
#if SET_TOLERANCE
295
    cairo_set_tolerance (cr, 3.0);
296
#endif
297

            
298
    cairo_save (cr); {
299
	cairo_set_source_rgb (cr, 1, 1, 1);
300
	cairo_paint (cr);
301
    } cairo_restore (cr);
302

            
303
    cairo_scale (cr, ppi_x/72., ppi_y/72.);
304
    draw (cr, SIZE, SIZE);
305

            
306
    surface = cairo_surface_reference (cairo_get_target (cr));
307
    cairo_destroy (cr);
308

            
309
    target = cairo_image_surface_create (CAIRO_FORMAT_RGB24, SIZE, SIZE);
310
    cr = cairo_create (target);
311
    cairo_scale (cr, 72./ppi_x, 72./ppi_y);
312
    cairo_set_source_surface (cr, surface, 0, 0);
313
    cairo_paint (cr);
314

            
315
    status = cairo_surface_write_to_png (cairo_get_target (cr), filename);
316
    cairo_destroy (cr);
317

            
318
    if (status) {
319
	fprintf (stderr, "Failed to generate reference image '%s': %s\n",
320
		 filename, cairo_status_to_string (status));
321
	exit (1);
322
    }
323
}
324
#endif
325

            
326
/* TODO: Split each ppi case out to its own CAIRO_TEST() test case */
327
static cairo_test_status_t
328
1
preamble (cairo_test_context_t *ctx)
329
{
330
    cairo_t *cr;
331
1
    cairo_test_status_t ret = CAIRO_TEST_UNTESTED;
332
    struct {
333
	double x, y;
334
1
    } ppi[] = {
335
	{ 576, 576 },
336
	{ 576, 72 },
337

            
338
	{ 288, 288 },
339
	{ 288, 72 },
340

            
341
	{ 144, 144 },
342
	{ 144, 72 },
343

            
344
	{ 72, 576 },
345
	{ 72, 288 },
346
	{ 72, 144 },
347
	{ 72, 72 },
348
    };
349
    unsigned int i;
350
    int n, num_ppi;
351
1
    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
352

            
353
1
    num_ppi = ARRAY_LENGTH (ppi);
354

            
355
#if GENERATE_REFERENCE
356
    for (n = 0; n < num_ppi; n++) {
357
	char *ref_name;
358
	xasprintf (&ref_name, "reference/fallback-resolution.ppi%gx%g.ref.png",
359
		   ppi[n].x, ppi[n].y);
360
	generate_reference (ppi[n].x, ppi[n].y, ref_name);
361
	free (ref_name);
362
    }
363
#endif
364

            
365
4
    for (i = 0; i < ctx->num_targets; i++) {
366
3
	const cairo_boilerplate_target_t *target = ctx->targets_to_test[i];
367
3
	cairo_surface_t *surface = NULL;
368
	char *base_name;
369
	void *closure;
370
	const char *format;
371
	cairo_status_t status;
372

            
373
3
	if (! target->is_vector)
374
3
	    continue;
375

            
376
	if (! cairo_test_is_target_enabled (ctx, target->name))
377
	    continue;
378

            
379
	format = cairo_boilerplate_content_name (target->content);
380
	xasprintf (&base_name, "%s/fallback-resolution.%s.%s",
381
		   path, target->name,
382
		   format);
383

            
384
	surface = (target->create_surface) (base_name,
385
					    target->content,
386
					    SIZE, SIZE,
387
					    SIZE, SIZE,
388
					    CAIRO_BOILERPLATE_MODE_TEST,
389
					    &closure);
390

            
391
	if (surface == NULL) {
392
	    free (base_name);
393
	    continue;
394
	}
395

            
396
	if (ret == CAIRO_TEST_UNTESTED)
397
	    ret = CAIRO_TEST_SUCCESS;
398

            
399
	cairo_surface_destroy (surface);
400
	if (target->cleanup)
401
	    target->cleanup (closure);
402
	free (base_name);
403

            
404
	/* we need to recreate the surface for each resolution as we include
405
	 * SVG in testing which does not support the paginated interface.
406
	 */
407
	for (n = 0; n < num_ppi; n++) {
408
	    char *test_name;
409
	    cairo_bool_t pass;
410

            
411
	    xasprintf (&test_name, "fallback-resolution.ppi%gx%g",
412
		       ppi[n].x, ppi[n].y);
413
	    xasprintf (&base_name, "%s/%s.%s.%s",
414
		       path, test_name,
415
		       target->name,
416
		       format);
417

            
418
	    surface = (target->create_surface) (base_name,
419
						target->content,
420
						SIZE + 25, SIZE + 25,
421
						SIZE + 25, SIZE + 25,
422
						CAIRO_BOILERPLATE_MODE_TEST,
423
						&closure);
424
	    if (surface == NULL || cairo_surface_status (surface)) {
425
		cairo_test_log (ctx, "Failed to generate surface: %s.%s\n",
426
				target->name,
427
				format);
428
		free (base_name);
429
		free (test_name);
430
		ret = CAIRO_TEST_FAILURE;
431
		continue;
432
	    }
433

            
434
	    cairo_test_log (ctx,
435
			    "Testing fallback-resolution %gx%g with %s target\n",
436
			    ppi[n].x, ppi[n].y, target->name);
437
	    printf ("%s:\t", base_name);
438
	    fflush (stdout);
439

            
440
	    if (target->force_fallbacks != NULL)
441
		target->force_fallbacks (surface, ppi[n].x, ppi[n].y);
442
	    cr = cairo_create (surface);
443
#if SET_TOLERANCE
444
	    cairo_set_tolerance (cr, 3.0);
445
#endif
446

            
447
	    cairo_surface_set_device_offset (surface, 25, 25);
448

            
449
	    cairo_save (cr); {
450
		cairo_set_source_rgb (cr, 1, 1, 1);
451
		cairo_paint (cr);
452
	    } cairo_restore (cr);
453

            
454
	    /* First draw the top half in a conventional way. */
455
	    cairo_save (cr); {
456
		cairo_rectangle (cr, 0, 0, SIZE, SIZE / 2.0);
457
		cairo_clip (cr);
458

            
459
		draw (cr, SIZE, SIZE);
460
	    } cairo_restore (cr);
461

            
462
	    /* Then draw the bottom half in a separate group,
463
	     * (exposing a bug in 1.6.4 with the group not being
464
	     * rendered with the correct fallback resolution). */
465
	    cairo_save (cr); {
466
		cairo_rectangle (cr, 0, SIZE / 2.0, SIZE, SIZE / 2.0);
467
		cairo_clip (cr);
468

            
469
		cairo_push_group (cr); {
470
		    draw (cr, SIZE, SIZE);
471
		} cairo_pop_group_to_source (cr);
472

            
473
		cairo_paint (cr);
474
	    } cairo_restore (cr);
475

            
476
	    status = cairo_status (cr);
477
	    cairo_destroy (cr);
478

            
479
	    pass = FALSE;
480
	    if (status) {
481
		cairo_test_log (ctx, "Error: Failed to create target surface: %s\n",
482
				cairo_status_to_string (status));
483
		ret = CAIRO_TEST_FAILURE;
484
	    } else {
485
		/* extract the image and compare it to our reference */
486
		if (! check_result (ctx, target, test_name, base_name, surface))
487
		    ret = CAIRO_TEST_FAILURE;
488
		else
489
		    pass = TRUE;
490
	    }
491
	    cairo_surface_destroy (surface);
492
	    if (target->cleanup)
493
		target->cleanup (closure);
494

            
495
	    free (base_name);
496
	    free (test_name);
497

            
498
	    if (pass) {
499
		printf ("PASS\n");
500
	    } else {
501
		printf ("FAIL\n");
502
	    }
503
	    fflush (stdout);
504
	}
505
    }
506

            
507
1
    return ret;
508
}
509

            
510
1
CAIRO_TEST (fallback_resolution,
511
	    "Check handling of fallback resolutions",
512
	    "fallback", /* keywords */
513
	    NULL, /* requirements */
514
	    0, 0,
515
	    preamble, NULL)