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 "cairo-test.h"
27

            
28
#include <stdio.h>
29

            
30
#include <cairo-svg.h>
31

            
32
/* Test SVG clipping */
33

            
34
#define WIDTH_IN_POINTS 600
35
#define HEIGHT_IN_POINTS 600
36
#define BASENAME "svg-clip.out"
37

            
38
static void
39
test_clip (cairo_t *cr, double width, double height)
40
{
41
    cairo_t *cr2;
42

            
43
    /* Basic test; set a square clip and draw a circle to be clipped
44
     * against it.*/
45

            
46
    cairo_rectangle (cr, 100, 100, 400, 400);
47
    cairo_clip (cr);
48
    cairo_arc (cr, 300, 300, 210, 0, 2 * M_PI);
49
    cairo_set_source_rgb (cr, 1, 0, 0);
50
    cairo_fill (cr);
51

            
52
    /* Add a plus shaped clip path to the square clip and draw a big
53
     * green square to test the new clip path. */
54

            
55
    cairo_save (cr);
56

            
57
    cairo_rectangle (cr, 250, 100, 100, 400);
58
    cairo_rectangle (cr, 100, 250, 400, 100);
59
    cairo_clip (cr);
60

            
61
    cairo_rectangle (cr, 0, 0, 600, 600);
62
    cairo_set_source_rgb (cr, 0, 1, 0);
63
    cairo_fill (cr);
64

            
65
    cairo_restore (cr);
66

            
67
    /* Set a bezier shape in addition to the rectangle clip set before
68
     * the cairo_save() to verify that we successfully removed the
69
     * plus shaped clip path and can set a new clip.*/
70

            
71
    cairo_move_to (cr, 600, 0);
72
    cairo_curve_to (cr, 300, 600, 0, 300, 600, 0);
73
    cairo_clip (cr);
74

            
75
    cairo_rectangle (cr, 0, 0, 600, 600);
76
    cairo_set_source_rgb (cr, 0, 0, 1);
77
    cairo_fill (cr);
78

            
79
    /* Create a new context for this surface to test overlapped
80
     * drawing from two contexts */
81
    cr2 = cairo_create (cairo_get_group_target (cr));
82

            
83
    /* Using the new context, draw a black vertical line, which should
84
     * appear unclipped on top of everything drawn so far. */
85
    cairo_move_to (cr2, 110, 0);
86
    cairo_line_to (cr2, 110, 600);
87
    cairo_stroke (cr2);
88

            
89
    /* Using the first context, draw another black vertical line.
90
     * This line should be clipped against the bezier clipping path set
91
     * earlier. */
92
    cairo_set_source_rgb (cr, 0, 0, 0);
93
    cairo_move_to (cr, 400, 0);
94
    cairo_line_to (cr, 400, 600);
95
    cairo_stroke (cr);
96

            
97
    cairo_destroy (cr2);
98

            
99
    /* Test reset clip.  Draw a transparent black circle over
100
     * everything.  Specifically, make sure the circle extends outside
101
     * the square clip set at the top of this function.  */
102
    cairo_reset_clip (cr);
103
    cairo_arc (cr, 300, 300, 220, 0, 2 * M_PI);
104
    cairo_set_source_rgba (cr, 0, 0, 0, 0.2);
105
    cairo_fill (cr);
106
}
107

            
108
static cairo_test_status_t
109
1
preamble (cairo_test_context_t *ctx)
110
{
111
    cairo_t *cr;
112
    cairo_surface_t *surface;
113
    char *filename;
114
1
    const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
115

            
116
2
    if (! cairo_test_is_target_enabled (ctx, "svg11") &&
117
1
	! cairo_test_is_target_enabled (ctx, "svg12"))
118
    {
119
1
	return CAIRO_TEST_UNTESTED;
120
    }
121

            
122
    xasprintf (&filename, "%s/%s.svg", path, BASENAME);
123
    surface = cairo_svg_surface_create (filename,
124
					WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
125
    if (cairo_surface_status (surface)) {
126
	cairo_test_log (ctx,
127
			"Failed to create svg surface for file %s: %s\n",
128
			filename, cairo_status_to_string (cairo_surface_status (surface)));
129
	free (filename);
130
	return CAIRO_TEST_FAILURE;
131
    }
132

            
133
    cr = cairo_create (surface);
134

            
135
    test_clip (cr, WIDTH_IN_POINTS, HEIGHT_IN_POINTS);
136
    cairo_show_page (cr);
137

            
138
    cairo_destroy (cr);
139
    cairo_surface_destroy (surface);
140

            
141
    printf ("svg-clip: Please check %s to make sure it looks happy.\n",
142
	    filename);
143
    free (filename);
144
    return CAIRO_TEST_SUCCESS;
145
}
146

            
147
1
CAIRO_TEST (svg_clip,
148
	    "Test SVG clipping",
149
	    "svg, clip", /* keywords */
150
	    NULL, /* requirements */
151
	    0, 0,
152
	    preamble, NULL)