Tizen 2.0 Release
[framework/graphics/cairo.git] / test / pdf-mime-data.c
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
43 static cairo_test_status_t
44 read_file (const cairo_test_context_t *ctx,
45            const char *file,
46            unsigned char **data,
47            unsigned int *len)
48 {
49     FILE *fp;
50
51     fp = fopen (file, "rb");
52     if (file == NULL) {
53         char filename[4096];
54
55         /* try again with srcdir */
56         snprintf (filename, sizeof (filename),
57                   "%s/%s", ctx->srcdir, file);
58         fp = fopen (filename, "rb");
59     }
60     if (fp == NULL) {
61         switch (errno) {
62         case ENOMEM:
63             return CAIRO_TEST_NO_MEMORY;
64         default:
65             return CAIRO_TEST_FAILURE;
66         }
67     }
68
69     fseek (fp, 0, SEEK_END);
70     *len = ftell(fp);
71     fseek (fp, 0, SEEK_SET);
72     *data = malloc (*len);
73     if (*data == NULL)
74         return CAIRO_TEST_NO_MEMORY;
75
76     if (fread(*data, *len, 1, fp) != 1)
77         return CAIRO_TEST_FAILURE;
78
79     fclose(fp);
80     return CAIRO_TEST_SUCCESS;
81 }
82
83 static cairo_test_status_t
84 preamble (cairo_test_context_t *ctx)
85 {
86     const char *filename = "pdf-mime-data.out.pdf";
87     cairo_surface_t *image;
88     cairo_surface_t *surface;
89     cairo_t *cr;
90     cairo_status_t status, status2;
91     cairo_test_status_t test_status;
92     int width, height;
93     unsigned char *data;
94     unsigned char *out_data;
95     unsigned int len, out_len;
96     char command[4096];
97     int exit_status;
98
99     if (! cairo_test_is_target_enabled (ctx, "pdf"))
100         return CAIRO_TEST_UNTESTED;
101
102     image = cairo_image_surface_create_from_png (IMAGE_FILE ".png");
103     test_status = read_file (ctx, IMAGE_FILE ".jpg", &data, &len);
104     if (test_status) {
105         cairo_test_log (ctx, "Could not read input jpeg file %s\n", IMAGE_FILE ".jpg");
106         return test_status;
107     }
108
109     cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JPEG,
110                                  data, len,
111                                  free, data);
112     width = cairo_image_surface_get_width (image);
113     height = cairo_image_surface_get_height (image);
114
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);
119     cairo_paint (cr);
120     status = cairo_status (cr);
121     cairo_destroy (cr);
122     cairo_surface_finish (surface);
123     status2 = cairo_surface_status (surface);
124     if (status != CAIRO_STATUS_SUCCESS)
125         status = status2;
126     cairo_surface_destroy (surface);
127     cairo_surface_destroy (image);
128
129     if (status) {
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;
133     }
134
135     printf ("pdf-mime-data: Please check %s to ensure it looks/prints correctly.\n", filename);
136
137     sprintf (command, "pdfimages -j %s pdf-mime-data.out", filename);
138     exit_status = system (command);
139     if (exit_status) {
140         cairo_test_log (ctx, "pdfimages failed with exit status %d\n", exit_status);
141         return CAIRO_TEST_FAILURE;
142     }
143
144     test_status = read_file (ctx, IMAGE_FILE ".jpg", &data, &len);
145     if (test_status) {
146         cairo_test_log (ctx, "Could not read input jpeg file %s\n", IMAGE_FILE ".jpg");
147         return test_status;
148     }
149
150     test_status = read_file (ctx, "pdf-mime-data.out-000.jpg", &out_data, &out_len);
151     if (test_status) {
152         free (data);
153         cairo_test_log (ctx,
154                         "Could not read input jpeg file %s\n",
155                         "pdf-mime-data.out-000.jpg");
156         return test_status;
157     }
158
159     if (len != out_len || memcmp(data, out_data, len) != 0) {
160         free (data);
161         free (out_data);
162         cairo_test_log (ctx, "output mime data does not match source mime data\n");
163         return CAIRO_TEST_FAILURE;
164     }
165
166     free (data);
167     free (out_data);
168
169     return CAIRO_TEST_SUCCESS;
170 }
171
172 CAIRO_TEST (pdf_mime_data,
173             "Check mime data correctly used by PDF surface",
174             "pdf, mime-data", /* keywords */
175             NULL, /* requirements */
176             0, 0,
177             preamble, NULL)