Viewport clipping now works with both GL and Gallium.
[cairo/gpu.git] / test / multi-page.c
blob0b73902681a079a8fded563fb564956c84abcce6
1 /*
2 * Copyright © 2005 Red Hat, Inc.
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.
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.
23 * Author: Carl D. Worth <cworth@cworth.org>
26 #include <stdio.h>
28 #include <cairo.h>
30 #if CAIRO_HAS_PS_SURFACE
31 #include <cairo-ps.h>
32 #endif
34 #if CAIRO_HAS_PDF_SURFACE
35 #include <cairo-pdf.h>
36 #endif
38 #include "cairo-test.h"
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.
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.
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)
55 static void
56 draw_smiley (cairo_t *cr, double width, double height, double smile_ratio)
58 #define STROKE_WIDTH .04
59 double size;
61 double theta = M_PI / 4 * smile_ratio;
62 double dx = sqrt (0.005) * cos (theta);
63 double dy = sqrt (0.005) * sin (theta);
65 cairo_save (cr);
67 if (width > height)
68 size = height;
69 else
70 size = width;
72 cairo_translate (cr, (width - size) / 2.0, (height - size) / 2.0);
73 cairo_scale (cr, size, size);
75 /* Fill face */
76 cairo_arc (cr, 0.5, 0.5, 0.5 - STROKE_WIDTH, 0, 2 * M_PI);
77 cairo_set_source_rgb (cr, 1, 1, 0);
78 cairo_fill_preserve (cr);
80 cairo_set_source_rgb (cr, 0, 0, 0);
82 /* Stroke face */
83 cairo_set_line_width (cr, STROKE_WIDTH / 2.0);
84 cairo_stroke (cr);
86 /* Eyes */
87 cairo_set_line_width (cr, STROKE_WIDTH);
88 cairo_arc (cr, 0.3, 0.4, STROKE_WIDTH, 0, 2 * M_PI);
89 cairo_fill (cr);
90 cairo_arc (cr, 0.7, 0.4, STROKE_WIDTH, 0, 2 * M_PI);
91 cairo_fill (cr);
93 /* Mouth */
94 cairo_move_to (cr,
95 0.35 - dx, 0.75 - dy);
96 cairo_curve_to (cr,
97 0.35 + dx, 0.75 + dy,
98 0.65 - dx, 0.75 + dy,
99 0.65 + dx, 0.75 - dy);
100 cairo_stroke (cr);
102 cairo_restore (cr);
105 static void
106 draw_some_pages (cairo_surface_t *surface)
108 cairo_t *cr;
109 int i;
111 cr = cairo_create (surface);
113 #define NUM_FRAMES 5
114 for (i=0; i < NUM_FRAMES; i++) {
115 draw_smiley (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS,
116 (double) i / (NUM_FRAMES - 1));
118 /* Duplicate the last frame onto another page. (This is just a
119 * way to sneak cairo_copy_page into the test).
121 if (i == (NUM_FRAMES - 1))
122 cairo_copy_page (cr);
124 cairo_show_page (cr);
127 cairo_destroy (cr);
130 static cairo_test_status_t
131 preamble (cairo_test_context_t *ctx)
133 cairo_surface_t *surface;
134 cairo_status_t status;
135 const char *filename;
136 cairo_test_status_t result = CAIRO_TEST_UNTESTED;
138 #if CAIRO_HAS_PS_SURFACE
139 if (cairo_test_is_target_enabled (ctx, "ps2") ||
140 cairo_test_is_target_enabled (ctx, "ps3"))
142 if (result == CAIRO_TEST_UNTESTED)
143 result = CAIRO_TEST_SUCCESS;
145 filename = "multi-page.out.ps";
146 surface = cairo_ps_surface_create (filename,
147 WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
148 status = cairo_surface_status (surface);
149 if (status) {
150 cairo_test_log (ctx, "Failed to create ps surface for file %s: %s\n",
151 filename, cairo_status_to_string (status));
152 result = CAIRO_TEST_FAILURE;
155 draw_some_pages (surface);
157 cairo_surface_destroy (surface);
159 printf ("multi-page: Please check %s to ensure it looks happy.\n", filename);
161 #endif
163 #if CAIRO_HAS_PDF_SURFACE
164 if (cairo_test_is_target_enabled (ctx, "pdf")) {
165 if (result == CAIRO_TEST_UNTESTED)
166 result = CAIRO_TEST_SUCCESS;
168 filename = "multi-page.out.pdf";
169 surface = cairo_pdf_surface_create (filename,
170 WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
171 status = cairo_surface_status (surface);
172 if (status) {
173 cairo_test_log (ctx, "Failed to create pdf surface for file %s: %s\n",
174 filename, cairo_status_to_string (status));
175 result = CAIRO_TEST_FAILURE;
178 draw_some_pages (surface);
180 cairo_surface_destroy (surface);
182 printf ("multi-page: Please check %s to ensure it looks happy.\n", filename);
184 #endif
186 return result;
189 CAIRO_TEST (multi_page,
190 "Check the paginated surfaces handle multiple pages.",
191 "paginated", /* keywords */
192 "target=vector", /* requirements */
193 0, 0,
194 preamble, NULL)