summaryrefslogtreecommitdiff
path: root/test/pdf-mime-data.c
blob: 00cde9dfb22ec057222751f485408e2c4bc4020a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright © 2008 Adrian Johnson
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Author: Adrian Johnson <ajohnson@redneon.com>
 */

#include "cairo-test.h"

#include <stdio.h>
#include <errno.h>
#include <cairo.h>
#include <cairo-pdf.h>

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

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


static cairo_test_status_t
read_file (const cairo_test_context_t *ctx,
	   const char *file,
	   unsigned char **data,
	   unsigned int *len)
{
    FILE *fp;

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

	/* try again with srcdir */
	snprintf (filename, sizeof (filename),
		  "%s/%s", ctx->srcdir, file);
	fp = fopen (filename, "rb");
    }
    if (fp == NULL) {
	switch (errno) {
	case ENOMEM:
	    return CAIRO_TEST_NO_MEMORY;
	default:
	    return CAIRO_TEST_FAILURE;
	}
    }

    fseek (fp, 0, SEEK_END);
    *len = ftell(fp);
    fseek (fp, 0, SEEK_SET);
    *data = malloc (*len);
    if (*data == NULL)
	return CAIRO_TEST_NO_MEMORY;

    if (fread(*data, *len, 1, fp) != 1)
	return CAIRO_TEST_FAILURE;

    fclose(fp);
    return CAIRO_TEST_SUCCESS;
}

static cairo_test_status_t
preamble (cairo_test_context_t *ctx)
{
    const char *filename = "pdf-mime-data.out.pdf";
    cairo_surface_t *image;
    cairo_surface_t *surface;
    cairo_t *cr;
    cairo_status_t status, status2;
    cairo_test_status_t test_status;
    int width, height;
    unsigned char *data;
    unsigned char *out_data;
    unsigned int len, out_len;
    char command[4096];
    int exit_status;

    if (! cairo_test_is_target_enabled (ctx, "pdf"))
	return CAIRO_TEST_UNTESTED;

    image = cairo_image_surface_create_from_png (IMAGE_FILE ".png");
    test_status = read_file (ctx, IMAGE_FILE ".jpg", &data, &len);
    if (test_status) {
	cairo_test_log (ctx, "Could not read input jpeg file %s\n", IMAGE_FILE ".jpg");
	return test_status;
    }

    cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JPEG,
				 data, len,
				 free, data);
    width = cairo_image_surface_get_width (image);
    height = cairo_image_surface_get_height (image);

    surface = cairo_pdf_surface_create (filename, width + 20, height + 20);
    cr = cairo_create (surface);
    cairo_translate (cr, 10, 10);
    cairo_set_source_surface (cr, image, 0, 0);
    cairo_paint (cr);
    status = cairo_status (cr);
    cairo_destroy (cr);
    cairo_surface_finish (surface);
    status2 = cairo_surface_status (surface);
    if (status != CAIRO_STATUS_SUCCESS)
	status = status2;
    cairo_surface_destroy (surface);
    cairo_surface_destroy (image);

    if (status) {
	cairo_test_log (ctx, "Failed to create pdf surface for file %s: %s\n",
			filename, cairo_status_to_string (status));
	return CAIRO_TEST_FAILURE;
    }

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

    sprintf (command, "pdfimages -j %s pdf-mime-data.out", filename);
    exit_status = system (command);
    if (exit_status) {
	cairo_test_log (ctx, "pdfimages failed with exit status %d\n", exit_status);
	return CAIRO_TEST_FAILURE;
    }

    test_status = read_file (ctx, IMAGE_FILE ".jpg", &data, &len);
    if (test_status) {
	cairo_test_log (ctx, "Could not read input jpeg file %s\n", IMAGE_FILE ".jpg");
	return test_status;
    }

    test_status = read_file (ctx, "pdf-mime-data.out-000.jpg", &out_data, &out_len);
    if (test_status) {
	free (data);
	cairo_test_log (ctx,
			"Could not read input jpeg file %s\n",
			"pdf-mime-data.out-000.jpg");
	return test_status;
    }

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

    free (data);
    free (out_data);

    return CAIRO_TEST_SUCCESS;
}

CAIRO_TEST (pdf_mime_data,
	    "Check mime data correctly used by PDF surface",
	    "pdf, mime-data", /* keywords */
	    NULL, /* requirements */
	    0, 0,
	    preamble, NULL)