035b236050c749318e478e5dd25911f8f63d35a7
[framework/graphics/cairo.git] / test / svg-clip.c
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: Kristian Høgsberg <krh@redhat.com>
24  */
25
26 #include <stdio.h>
27
28 #include <cairo-svg.h>
29 #include "cairo-test.h"
30
31 /* Test SVG clipping */
32
33 #define WIDTH_IN_POINTS 600
34 #define HEIGHT_IN_POINTS 600
35
36 static void
37 test_clip (cairo_t *cr, double width, double height)
38 {
39     cairo_t *cr2;
40
41     /* Basic test; set a square clip and draw a circle to be clipped
42      * against it.*/
43
44     cairo_rectangle (cr, 100, 100, 400, 400);
45     cairo_clip (cr);
46     cairo_arc (cr, 300, 300, 210, 0, 2 * M_PI);
47     cairo_set_source_rgb (cr, 1, 0, 0);
48     cairo_fill (cr);
49
50     /* Add a plus shaped clip path to the square clip and draw a big
51      * green square to test the new clip path. */
52
53     cairo_save (cr);
54
55     cairo_rectangle (cr, 250, 100, 100, 400);
56     cairo_rectangle (cr, 100, 250, 400, 100);
57     cairo_clip (cr);
58
59     cairo_rectangle (cr, 0, 0, 600, 600);
60     cairo_set_source_rgb (cr, 0, 1, 0);
61     cairo_fill (cr);
62
63     cairo_restore (cr);
64
65     /* Set a bezier shape in addition to the rectangle clip set before
66      * the cairo_save() to verify that we successfully removed the
67      * plus shaped clip path and can set a new clip.*/
68
69     cairo_move_to (cr, 600, 0);
70     cairo_curve_to (cr, 300, 600, 0, 300, 600, 0);
71     cairo_clip (cr);
72
73     cairo_rectangle (cr, 0, 0, 600, 600);
74     cairo_set_source_rgb (cr, 0, 0, 1);
75     cairo_fill (cr);
76
77     /* Create a new context for this surface to test overlapped
78      * drawing from two contexts */
79     cr2 = cairo_create (cairo_get_group_target (cr));
80
81     /* Using the new context, draw a black vertical line, which should
82      * appear unclipped on top of everything drawn so far. */
83     cairo_move_to (cr2, 110, 0);
84     cairo_line_to (cr2, 110, 600);
85     cairo_stroke (cr2);
86
87     /* Using the first context, draw another black vertical line.
88      * This line should be clipped agaist the bezier clipping path set
89      * earlier. */
90     cairo_set_source_rgb (cr, 0, 0, 0);
91     cairo_move_to (cr, 400, 0);
92     cairo_line_to (cr, 400, 600);
93     cairo_stroke (cr);
94
95     cairo_destroy (cr2);
96
97     /* Test reset clip.  Draw a transparent black circle over
98      * everything.  Specifically, make sure the circle extends outside
99      * the square clip set at the top of this function.  */
100     cairo_reset_clip (cr);
101     cairo_arc (cr, 300, 300, 220, 0, 2 * M_PI);
102     cairo_set_source_rgba (cr, 0, 0, 0, 0.2);
103     cairo_fill (cr);
104 }
105
106 static cairo_test_status_t
107 preamble (cairo_test_context_t *ctx)
108 {
109     cairo_t *cr;
110     const char *filename = "svg-clip.out.svg";
111     cairo_surface_t *surface;
112
113     if (! cairo_test_is_target_enabled (ctx, "svg11") &&
114         ! cairo_test_is_target_enabled (ctx, "svg12"))
115     {
116         return CAIRO_TEST_UNTESTED;
117     }
118
119     surface = cairo_svg_surface_create (filename,
120                                         WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
121     if (cairo_surface_status (surface)) {
122         cairo_test_log (ctx,
123                         "Failed to create svg surface for file %s: %s\n",
124                         filename, cairo_status_to_string (cairo_surface_status (surface)));
125         return CAIRO_TEST_FAILURE;
126     }
127
128     cr = cairo_create (surface);
129
130     test_clip (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
131     cairo_show_page (cr);
132
133     cairo_destroy (cr);
134     cairo_surface_destroy (surface);
135
136     printf ("svg-clip: Please check %s to make sure it looks happy.\n",
137             filename);
138     return CAIRO_TEST_SUCCESS;
139 }
140
141 CAIRO_TEST (svg_clip,
142             "Test SVG clipping",
143             "svg, clip", /* keywords */
144             NULL, /* requirements */
145             0, 0,
146             preamble, NULL)