2 * Copyright © 2008 Adrian Johnson
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 * Author: Adrian Johnson <ajohnson@redneon.com>
27 #include "cairo-test.h"
32 #include <cairo-pdf.h>
34 /* This test checks that the mime data is correctly used by the PDF
35 * surface when embedding images..
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"
43 static cairo_test_status_t
44 read_file (const cairo_test_context_t *ctx,
51 fp = fopen (file, "rb");
55 /* try again with srcdir */
56 snprintf (filename, sizeof (filename),
57 "%s/%s", ctx->srcdir, file);
58 fp = fopen (filename, "rb");
63 return CAIRO_TEST_NO_MEMORY;
65 return CAIRO_TEST_FAILURE;
69 fseek (fp, 0, SEEK_END);
71 fseek (fp, 0, SEEK_SET);
72 *data = malloc (*len);
74 return CAIRO_TEST_NO_MEMORY;
76 if (fread(*data, *len, 1, fp) != 1)
77 return CAIRO_TEST_FAILURE;
80 return CAIRO_TEST_SUCCESS;
83 static cairo_test_status_t
84 preamble (cairo_test_context_t *ctx)
86 const char *filename = "pdf-mime-data.out.pdf";
87 cairo_surface_t *image;
88 cairo_surface_t *surface;
90 cairo_status_t status, status2;
91 cairo_test_status_t test_status;
94 unsigned char *out_data;
95 unsigned int len, out_len;
99 if (! cairo_test_is_target_enabled (ctx, "pdf"))
100 return CAIRO_TEST_UNTESTED;
102 image = cairo_image_surface_create_from_png (IMAGE_FILE ".png");
103 test_status = read_file (ctx, IMAGE_FILE ".jpg", &data, &len);
105 cairo_test_log (ctx, "Could not read input jpeg file %s\n", IMAGE_FILE ".jpg");
109 cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JPEG,
112 width = cairo_image_surface_get_width (image);
113 height = cairo_image_surface_get_height (image);
115 surface = cairo_pdf_surface_create (filename, width + 20, height + 20);
116 cr = cairo_create (surface);
117 cairo_translate (cr, 10, 10);
118 cairo_set_source_surface (cr, image, 0, 0);
120 status = cairo_status (cr);
122 cairo_surface_finish (surface);
123 status2 = cairo_surface_status (surface);
124 if (status != CAIRO_STATUS_SUCCESS)
126 cairo_surface_destroy (surface);
127 cairo_surface_destroy (image);
130 cairo_test_log (ctx, "Failed to create pdf surface for file %s: %s\n",
131 filename, cairo_status_to_string (status));
132 return CAIRO_TEST_FAILURE;
135 printf ("pdf-mime-data: Please check %s to ensure it looks/prints correctly.\n", filename);
137 sprintf (command, "pdfimages -j %s pdf-mime-data.out", filename);
138 exit_status = system (command);
140 cairo_test_log (ctx, "pdfimages failed with exit status %d\n", exit_status);
141 return CAIRO_TEST_FAILURE;
144 test_status = read_file (ctx, IMAGE_FILE ".jpg", &data, &len);
146 cairo_test_log (ctx, "Could not read input jpeg file %s\n", IMAGE_FILE ".jpg");
150 test_status = read_file (ctx, "pdf-mime-data.out-000.jpg", &out_data, &out_len);
154 "Could not read input jpeg file %s\n",
155 "pdf-mime-data.out-000.jpg");
159 if (len != out_len || memcmp(data, out_data, len) != 0) {
162 cairo_test_log (ctx, "output mime data does not match source mime data\n");
163 return CAIRO_TEST_FAILURE;
169 return CAIRO_TEST_SUCCESS;
172 CAIRO_TEST (pdf_mime_data,
173 "Check mime data correctly used by PDF surface",
174 "pdf, mime-data", /* keywords */
175 NULL, /* requirements */