1
/*
2
 * Copyright © 2008 Adrian Johnson
3
 *
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use, copy,
8
 * modify, merge, publish, distribute, sublicense, and/or sell copies
9
 * of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 *
24
 * Author: Adrian Johnson <ajohnson@redneon.com>
25
 */
26

            
27
#include "cairo-test.h"
28

            
29
#include <stdio.h>
30
#include <errno.h>
31
#include <cairo.h>
32
#include <cairo-pdf.h>
33

            
34
/* This test checks that the mime data is correctly used by the PDF
35
 * surface when embedding images..
36
 */
37

            
38
/* Both a *.png and *.jpg file with this name is required since we
39
 * are not using a jpeg library */
40
#define IMAGE_FILE "romedalen"
41

            
42
#define BASENAME "pdf-mime-data.out"
43

            
44
static cairo_test_status_t
45
read_file (const cairo_test_context_t *ctx,
46
	   const char *file,
47
	   unsigned char **data,
48
	   unsigned int *len)
49
{
50
    FILE *fp;
51

            
52
    fp = fopen (file, "rb");
53
    if (fp == NULL) {
54
	char filename[4096];
55

            
56
	/* try again with srcdir */
57
	snprintf (filename, sizeof (filename),
58
		  "%s/%s", ctx->srcdir, file);
59
	fp = fopen (filename, "rb");
60
    }
61
    if (fp == NULL) {
62
	switch (errno) {
63
	case ENOMEM:
64
	    cairo_test_log (ctx, "Could not create file handle for %s due to \
65
				lack of memory\n", file);
66
	    return CAIRO_TEST_NO_MEMORY;
67
	default:
68
	    cairo_test_log (ctx, "Could not get the file handle for %s\n", file);
69
	    return CAIRO_TEST_FAILURE;
70
	}
71
    }
72

            
73
    fseek (fp, 0, SEEK_END);
74
    *len = ftell(fp);
75
    fseek (fp, 0, SEEK_SET);
76
    *data = malloc (*len);
77
    if (*data == NULL) {
78
	fclose(fp);
79
	cairo_test_log (ctx, "Could not allocate memory for buffer to read \
80
				from file %s\n", file);
81
	return CAIRO_TEST_NO_MEMORY;
82
    }
83

            
84
    if (fread(*data, *len, 1, fp) != 1) {
85
	free (*data);
86
	fclose(fp);
87
	cairo_test_log (ctx, "Could not read data from file %s\n", file);
88
	return CAIRO_TEST_FAILURE;
89
    }
90

            
91
    fclose(fp);
92
    return CAIRO_TEST_SUCCESS;
93
}
94

            
95
static cairo_test_status_t
96
1
preamble (cairo_test_context_t *ctx)
97
{
98
    cairo_surface_t *image;
99
    cairo_surface_t *surface;
100
    cairo_t *cr;
101
    cairo_status_t status, status2;
102
    cairo_test_status_t test_status;
103
    int width, height;
104
    unsigned char *data;
105
    unsigned char *out_data;
106
    unsigned int len, out_len;
107
    char command[4096];
108
    int exit_status;
109
    char *filename;
110
1
    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
111

            
112
1
    if (! cairo_test_is_target_enabled (ctx, "pdf"))
113
1
	return CAIRO_TEST_UNTESTED;
114

            
115
    exit_status = system ("command -v pdfimages");
116
    if (exit_status) {
117
	cairo_test_log (ctx, "pdfimages not available\n");
118
	return CAIRO_TEST_UNTESTED;
119
    }
120

            
121
    image = cairo_test_create_surface_from_png (ctx, IMAGE_FILE ".png");
122
    test_status = read_file (ctx, IMAGE_FILE ".jpg", &data, &len);
123
    if (test_status) {
124
	return test_status;
125
    }
126

            
127
    cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JPEG,
128
				 data, len,
129
				 free, data);
130
    width = cairo_image_surface_get_width (image);
131
    height = cairo_image_surface_get_height (image);
132

            
133
    xasprintf (&filename, "%s/%s.pdf", path, BASENAME);
134
    surface = cairo_pdf_surface_create (filename, width + 20, height + 20);
135
    cr = cairo_create (surface);
136
    cairo_translate (cr, 10, 10);
137
    cairo_set_source_surface (cr, image, 0, 0);
138
    cairo_paint (cr);
139
    status = cairo_status (cr);
140
    cairo_destroy (cr);
141
    cairo_surface_finish (surface);
142
    status2 = cairo_surface_status (surface);
143
    if (status == CAIRO_STATUS_SUCCESS)
144
	status = status2;
145
    cairo_surface_destroy (surface);
146
    cairo_surface_destroy (image);
147

            
148
    if (status) {
149
	cairo_test_log (ctx, "Failed to create pdf surface for file %s: %s\n",
150
			filename, cairo_status_to_string (status));
151
        free (filename);
152
	return CAIRO_TEST_FAILURE;
153
    }
154

            
155
    printf ("pdf-mime-data: Please check %s to ensure it looks/prints correctly.\n", filename);
156

            
157
    sprintf (command, "pdfimages -j %s %s", filename, CAIRO_TEST_OUTPUT_DIR "/" BASENAME);
158
    exit_status = system (command);
159
    free (filename);
160
    if (exit_status) {
161
	cairo_test_log (ctx, "pdfimages failed with exit status %d\n", exit_status);
162
	return CAIRO_TEST_FAILURE;
163
    }
164

            
165
    test_status = read_file (ctx, IMAGE_FILE ".jpg", &data, &len);
166
    if (test_status) {
167
	return test_status;
168
    }
169

            
170
    test_status = read_file (ctx, CAIRO_TEST_OUTPUT_DIR "/" BASENAME "-000.jpg", &out_data, &out_len);
171
    if (test_status) {
172
	return test_status;
173
    }
174

            
175
    if (len != out_len || memcmp(data, out_data, len) != 0) {
176
	free (data);
177
	free (out_data);
178
	cairo_test_log (ctx, "output mime data does not match source mime data\n");
179
	return CAIRO_TEST_FAILURE;
180
    }
181

            
182
    free (data);
183
    free (out_data);
184

            
185
    return CAIRO_TEST_SUCCESS;
186
}
187

            
188
1
CAIRO_TEST (pdf_mime_data,
189
	    "Check mime data correctly used by PDF surface",
190
	    "pdf, mime-data", /* keywords */
191
	    NULL, /* requirements */
192
	    0, 0,
193
	    preamble, NULL)