1
/*
2
 * Copyright © 2006 Red Hat, Inc.
3
 * Copyright © 2009 Adrian Johnson
4
 * Copyright © 2008 Chris Wilson
5
 *
6
 * Permission to use, copy, modify, distribute, and sell this software
7
 * and its documentation for any purpose is hereby granted without
8
 * fee, provided that the above copyright notice appear in all copies
9
 * and that both that copyright notice and this permission notice
10
 * appear in supporting documentation, and that the name of
11
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
12
 * distribution of the software without specific, written prior
13
 * permission. Red Hat, Inc. makes no representations about the
14
 * suitability of this software for any purpose.  It is provided "as
15
 * is" without express or implied warranty.
16
 *
17
 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
18
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
20
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
21
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
22
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
23
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
 *
25
 * Author: Carl D. Worth <cworth@cworth.org>
26
 *         Adrian Johnson <ajohnson@redneon.com>
27
 *         Chris Wilson <chris@chris-wilson.co.uk>
28
 */
29

            
30
#include "config.h"
31

            
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <math.h>
35
#include <cairo.h>
36
#include <cairo-ps.h>
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
/* Test EPS output.
50
 */
51

            
52
#define WIDTH 595
53
#define HEIGHT 842
54

            
55
/* Reference Bounding Box */
56
#define LLX  95
57
#define LLY 687
58
#define URX 155
59
#define URY 747
60

            
61
static void
62
_xunlink (const cairo_test_context_t *ctx, const char *pathname)
63
{
64
    if (unlink (pathname) < 0 && errno != ENOENT) {
65
	cairo_test_log (ctx, "Error: Cannot remove %s: %s\n",
66
			pathname, strerror (errno));
67
	exit (1);
68
    }
69
}
70

            
71
static cairo_bool_t
72
check_result (cairo_test_context_t *ctx,
73
	      const cairo_boilerplate_target_t *target,
74
	      const char *test_name,
75
	      const char *base_name,
76
	      cairo_surface_t *surface)
77
{
78
    const char *format;
79
    char *ref_name;
80
    char *png_name;
81
    char *diff_name;
82
    cairo_surface_t *test_image, *ref_image, *diff_image;
83
    buffer_diff_result_t result;
84
    cairo_status_t status;
85
    cairo_bool_t ret;
86

            
87
    /* XXX log target, OUTPUT, REFERENCE, DIFFERENCE for index.html */
88

            
89
    if (target->finish_surface != NULL) {
90
	status = target->finish_surface (surface);
91
	if (status) {
92
	    cairo_test_log (ctx, "Error: Failed to finish surface: %s\n",
93
		    cairo_status_to_string (status));
94
	    cairo_surface_destroy (surface);
95
	    return FALSE;
96
	}
97
    }
98

            
99
    xasprintf (&png_name,  "%s.out.png", base_name);
100
    xasprintf (&diff_name, "%s.diff.png", base_name);
101

            
102
    test_image = target->get_image_surface (surface, 0, WIDTH, HEIGHT);
103
    if (cairo_surface_status (test_image)) {
104
	cairo_test_log (ctx, "Error: Failed to extract page: %s\n",
105
		        cairo_status_to_string (cairo_surface_status (test_image)));
106
	cairo_surface_destroy (test_image);
107
	free (png_name);
108
	free (diff_name);
109
	return FALSE;
110
    }
111

            
112
    _xunlink (ctx, png_name);
113
    status = cairo_surface_write_to_png (test_image, png_name);
114
    if (status) {
115
	cairo_test_log (ctx, "Error: Failed to write output image: %s\n",
116
		cairo_status_to_string (status));
117
	cairo_surface_destroy (test_image);
118
	free (png_name);
119
	free (diff_name);
120
	return FALSE;
121
    }
122

            
123
    format = cairo_boilerplate_content_name (target->content);
124
    ref_name = cairo_test_reference_filename (ctx,
125
					      base_name,
126
					      test_name,
127
					      target->name,
128
					      target->basename,
129
					      format,
130
					      CAIRO_TEST_REF_SUFFIX,
131
					      CAIRO_TEST_PNG_EXTENSION);
132
    if (ref_name == NULL) {
133
	cairo_test_log (ctx, "Error: Cannot find reference image for %s\n",
134
		        base_name);
135
	cairo_surface_destroy (test_image);
136
	free (png_name);
137
	free (diff_name);
138
	return FALSE;
139
    }
140

            
141
    ref_image = cairo_test_get_reference_image (ctx, ref_name,
142
	    target->content == CAIRO_TEST_CONTENT_COLOR_ALPHA_FLATTENED);
143
    if (cairo_surface_status (ref_image)) {
144
	cairo_test_log (ctx, "Error: Cannot open reference image for %s: %s\n",
145
		        ref_name,
146
		cairo_status_to_string (cairo_surface_status (ref_image)));
147
	cairo_surface_destroy (ref_image);
148
	cairo_surface_destroy (test_image);
149
	free (png_name);
150
	free (diff_name);
151
	free (ref_name);
152
	return FALSE;
153
    }
154

            
155
    diff_image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
156
	    WIDTH, HEIGHT);
157

            
158
    ret = TRUE;
159
    status = image_diff (ctx,
160
	    test_image, ref_image, diff_image,
161
	    &result);
162
    _xunlink (ctx, diff_name);
163
    if (status) {
164
	cairo_test_log (ctx, "Error: Failed to compare images: %s\n",
165
			cairo_status_to_string (status));
166
	ret = FALSE;
167
    } else if (image_diff_is_failure (&result, target->error_tolerance))
168
    {
169
	ret = FALSE;
170

            
171
	status = cairo_surface_write_to_png (diff_image, diff_name);
172
	if (status) {
173
	    cairo_test_log (ctx, "Error: Failed to write differences image: %s\n",
174
		    cairo_status_to_string (status));
175
	}
176
    }
177

            
178
    cairo_surface_destroy (test_image);
179
    cairo_surface_destroy (diff_image);
180
    free (png_name);
181
    free (diff_name);
182
    free (ref_name);
183

            
184
    return ret;
185
}
186

            
187

            
188
#define DOCUMENT_BBOX  "%%BoundingBox:"
189
#define PAGE_BBOX      "%%PageBoundingBox:"
190

            
191
static cairo_bool_t
192
check_bbox (cairo_test_context_t *ctx,
193
	    const char *base_name)
194
{
195
    char *filename;
196
    FILE *f;
197
    char buf[256];
198
    cairo_bool_t bbox_pass, page_bbox_pass;
199
    int llx, lly, urx, ury;
200
    int ret;
201

            
202
    xasprintf (&filename,  "%s.out.ps", base_name);
203
    f = fopen (filename, "r");
204
    if (!f) {
205
	cairo_test_log (ctx, "Error: Cannot open EPS output: %s\n",
206
		        base_name);
207
	free (filename);
208
	return FALSE;
209
    }
210

            
211
    bbox_pass = FALSE;
212
    page_bbox_pass = FALSE;
213
    while (!feof(f)) {
214
	if (fgets (buf, sizeof(buf), f) == (char *)EOF) {
215
	    cairo_test_log (ctx, "Error: Unexpected EOF in %s\n",
216
			    filename);
217
	    break;
218
	}
219

            
220
	if (strncmp (buf, DOCUMENT_BBOX, strlen (DOCUMENT_BBOX)) == 0) {
221
	    ret = sscanf (buf+strlen (DOCUMENT_BBOX), "%d %d %d %d", &llx, &lly, &urx, &ury);
222
	    if (ret == 4 && llx == LLX && lly == LLY && urx == URX && ury == URY)
223
		bbox_pass = TRUE;
224
	}
225

            
226
	if (strncmp (buf, PAGE_BBOX, strlen (PAGE_BBOX)) == 0) {
227
	    ret = sscanf (buf+strlen (PAGE_BBOX), "%d %d %d %d", &llx, &lly, &urx, &ury);
228
	    if (ret == 4 && llx == LLX && lly == LLY && urx == URX && ury == URY)
229
		page_bbox_pass = TRUE;
230
	}
231
    }
232
    fclose (f);
233

            
234
    if (!bbox_pass || !page_bbox_pass) {
235
	cairo_test_log (ctx, "Error: EPS Bounding Box does not match reference Bounding Box\n");
236
	return FALSE;
237
    }
238

            
239
    free (filename);
240

            
241
    return TRUE;
242
}
243

            
244
static cairo_test_status_t
245
1
preamble (cairo_test_context_t *ctx)
246
{
247
    cairo_t *cr;
248
1
    cairo_test_status_t ret = CAIRO_TEST_UNTESTED;
249
    unsigned int i;
250
1
    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
251

            
252
4
    for (i = 0; i < ctx->num_targets; i++) {
253
3
	const cairo_boilerplate_target_t *target = ctx->targets_to_test[i];
254
3
	cairo_surface_t *surface = NULL;
255
	char *base_name;
256
	void *closure;
257
	const char *format;
258
	cairo_status_t status;
259
	cairo_bool_t pass;
260
	char *test_name;
261

            
262
3
	if (! cairo_test_is_target_enabled (ctx, target->name))
263
3
	    continue;
264

            
265
3
	format = cairo_boilerplate_content_name (target->content);
266
3
	xasprintf (&test_name, "ps-eps");
267
3
	xasprintf (&base_name, "%s/ps-eps.%s.%s",
268
3
		   path, target->name, format);
269

            
270
3
	surface = (target->create_surface) (base_name,
271
3
					    target->content,
272
					    WIDTH, HEIGHT,
273
					    WIDTH, HEIGHT,
274
					    CAIRO_BOILERPLATE_MODE_TEST,
275
					    &closure);
276

            
277
3
	if (surface == NULL) {
278
	    free (base_name);
279
	    free (test_name);
280
	    continue;
281
	}
282

            
283
3
	cairo_ps_surface_set_eps (surface, TRUE);
284
3
	if (!cairo_ps_surface_get_eps (surface)) {
285
3
	    cairo_surface_destroy (surface);
286
3
	    if (target->cleanup)
287
		target->cleanup (closure);
288

            
289
3
	    free (base_name);
290
3
	    free (test_name);
291
3
	    continue;
292
	}
293
	
294
	cairo_test_log (ctx,
295
			"Testing ps-eps with %s target\n",
296
			target->name);
297
	printf ("%s:\t", base_name);
298
	fflush (stdout);
299

            
300
	cairo_surface_set_device_offset (surface, 25, 25);
301
	cr = cairo_create (surface);
302

            
303
	cairo_new_sub_path (cr);
304
	cairo_arc (cr, 100, 100, 25, 0, 2*M_PI);
305
	cairo_set_line_width (cr, 10);
306
	cairo_stroke (cr);
307

            
308
	cairo_show_page (cr);
309

            
310
	status = cairo_status (cr);
311
	cairo_destroy (cr);
312

            
313
	if (status) {
314
	    cairo_test_log (ctx, "Error: Failed to create target surface: %s\n",
315
			    cairo_status_to_string (status));
316
	    pass = FALSE;
317
	} else {
318
	    pass = TRUE;
319
	    /* extract the image and compare it to our reference */
320
	    if (! check_result (ctx, target, test_name, base_name, surface))
321
		pass = FALSE;
322

            
323
	    /* check the bounding box of the EPS file and compare it to our reference */
324
	    if (! check_bbox (ctx, base_name))
325
		pass = FALSE;
326
	}
327
	cairo_surface_destroy (surface);
328
	if (target->cleanup)
329
	    target->cleanup (closure);
330

            
331
	free (base_name);
332
	free (test_name);
333

            
334
	if (pass) {
335
	    printf ("PASS\n");
336
	    ret = CAIRO_TEST_SUCCESS;
337
	} else {
338
	    printf ("FAIL\n");
339
	    ret = CAIRO_TEST_FAILURE;
340
	}
341
	fflush (stdout);
342
    }
343

            
344
1
    return ret;
345
}
346

            
347
1
CAIRO_TEST (ps_eps,
348
	    "Check EPS output from PS surface",
349
	    "ps, api", /* keywords */
350
	    NULL, /* requirements */
351
	    0, 0,
352
	    preamble, NULL)