1 /*
2  * Copyright © 2005 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software
5  * and its documentation for any purpose is hereby granted without
6  * fee, provided that the above copyright notice appear in all copies
7  * and that both that copyright notice and this permission notice
8  * appear in supporting documentation, and that the name of
9  * Red Hat, Inc. not be used in advertising or publicity pertaining to
10  * distribution of the software without specific, written prior
11  * permission. Red Hat, Inc. makes no representations about the
12  * suitability of this software for any purpose.  It is provided "as
13  * is" without express or implied warranty.
14  *
15  * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
18  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
21  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Author: Carl D. Worth <cworth@cworth.org>
24  */
25 
26 #include "cairo-test.h"
27 
28 #include <stdio.h>
29 
30 #include <cairo.h>
31 
32 #if CAIRO_HAS_PS_SURFACE
33 #include <cairo-ps.h>
34 #endif
35 
36 #if CAIRO_HAS_PDF_SURFACE
37 #include <cairo-pdf.h>
38 #endif
39 
40 /* The PostScript and PDF backends are now integrated into the main
41  * test suite, so we are getting good verification of most things
42  * there.
43  *
44  * One thing that isn't supported there yet is multi-page output. So,
45  * for now we have this one-off test. There's no automatic
46  * verififcation here yet, but you can manually view the output to
47  * make sure it looks happy.
48  */
49 
50 #define WIDTH_IN_INCHES  3
51 #define HEIGHT_IN_INCHES 3
52 #define WIDTH_IN_POINTS  (WIDTH_IN_INCHES  * 72.0)
53 #define HEIGHT_IN_POINTS (HEIGHT_IN_INCHES * 72.0)
54 #define BASENAME         "multi-page.out"
55 
56 static void
draw_smiley(cairo_t * cr,double width,double height,double smile_ratio)57 draw_smiley (cairo_t *cr, double width, double height, double smile_ratio)
58 {
59 #define STROKE_WIDTH .04
60     double size;
61 
62     double theta = M_PI / 4 * smile_ratio;
63     double dx = sqrt (0.005) * cos (theta);
64     double dy = sqrt (0.005) * sin (theta);
65 
66     cairo_save (cr);
67 
68     if (width > height)
69 	size = height;
70     else
71 	size = width;
72 
73     cairo_translate (cr, (width - size) / 2.0, (height - size) / 2.0);
74     cairo_scale (cr, size, size);
75 
76     /* Fill face */
77     cairo_arc (cr, 0.5, 0.5, 0.5 - STROKE_WIDTH, 0, 2 * M_PI);
78     cairo_set_source_rgb (cr, 1, 1, 0);
79     cairo_fill_preserve (cr);
80 
81     cairo_set_source_rgb (cr, 0, 0, 0);
82 
83     /* Stroke face */
84     cairo_set_line_width (cr, STROKE_WIDTH / 2.0);
85     cairo_stroke (cr);
86 
87     /* Eyes */
88     cairo_set_line_width (cr, STROKE_WIDTH);
89     cairo_arc (cr, 0.3, 0.4, STROKE_WIDTH, 0, 2 * M_PI);
90     cairo_fill (cr);
91     cairo_arc (cr, 0.7, 0.4, STROKE_WIDTH, 0, 2 * M_PI);
92     cairo_fill (cr);
93 
94     /* Mouth */
95     cairo_move_to (cr,
96 		   0.35 - dx, 0.75 - dy);
97     cairo_curve_to (cr,
98 		    0.35 + dx, 0.75 + dy,
99 		    0.65 - dx, 0.75 + dy,
100 		    0.65 + dx, 0.75 - dy);
101     cairo_stroke (cr);
102 
103     cairo_restore (cr);
104 }
105 
106 static void
draw_some_pages(cairo_surface_t * surface)107 draw_some_pages (cairo_surface_t *surface)
108 {
109     cairo_t *cr;
110     int i;
111 
112     cr = cairo_create (surface);
113 
114 #define NUM_FRAMES 5
115     for (i=0; i < NUM_FRAMES; i++) {
116 	draw_smiley (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS,
117 	             (double) i / (NUM_FRAMES - 1));
118 
119 	/* Duplicate the last frame onto another page. (This is just a
120 	 * way to sneak cairo_copy_page into the test).
121 	 */
122 	if (i == (NUM_FRAMES - 1))
123 	    cairo_copy_page (cr);
124 
125 	cairo_show_page (cr);
126     }
127 
128     cairo_destroy (cr);
129 }
130 
131 static cairo_test_status_t
preamble(cairo_test_context_t * ctx)132 preamble (cairo_test_context_t *ctx)
133 {
134     cairo_surface_t *surface;
135     cairo_status_t status;
136     char *filename;
137     cairo_test_status_t result = CAIRO_TEST_UNTESTED;
138     const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
139 
140 #if CAIRO_HAS_PS_SURFACE
141     if (cairo_test_is_target_enabled (ctx, "ps2") ||
142         cairo_test_is_target_enabled (ctx, "ps3"))
143     {
144 	if (result == CAIRO_TEST_UNTESTED)
145 	    result = CAIRO_TEST_SUCCESS;
146 
147 	xasprintf (&filename, "%s/%s", path, BASENAME ".ps");
148 	surface = cairo_ps_surface_create (filename,
149 					   WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
150 	status = cairo_surface_status (surface);
151 	if (status) {
152 	    cairo_test_log (ctx, "Failed to create ps surface for file %s: %s\n",
153 			    filename, cairo_status_to_string (status));
154 	    result = CAIRO_TEST_FAILURE;
155 	}
156 
157 	draw_some_pages (surface);
158 
159 	cairo_surface_destroy (surface);
160 
161 	printf ("multi-page: Please check %s to ensure it looks happy.\n", filename);
162 	free (filename);
163     }
164 #endif
165 
166 #if CAIRO_HAS_PDF_SURFACE
167     if (cairo_test_is_target_enabled (ctx, "pdf")) {
168 	if (result == CAIRO_TEST_UNTESTED)
169 	    result = CAIRO_TEST_SUCCESS;
170 
171 	xasprintf (&filename, "%s/%s", path, BASENAME ".pdf");
172 	surface = cairo_pdf_surface_create (filename,
173 					    WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
174 	status = cairo_surface_status (surface);
175 	if (status) {
176 	    cairo_test_log (ctx, "Failed to create pdf surface for file %s: %s\n",
177 			    filename, cairo_status_to_string (status));
178 	    result = CAIRO_TEST_FAILURE;
179 	}
180 
181 	draw_some_pages (surface);
182 
183 	cairo_surface_destroy (surface);
184 
185 	printf ("multi-page: Please check %s to ensure it looks happy.\n", filename);
186 	free (filename);
187     }
188 #endif
189 
190     return result;
191 }
192 
193 CAIRO_TEST (multi_page,
194 	    "Check the paginated surfaces handle multiple pages.",
195 	    "paginated", /* keywords */
196 	    "target=vector", /* requirements */
197 	    0, 0,
198 	    preamble, NULL)
199