1
/*
2
 * Copyright © 2005 Red Hat, Inc.
3
 * Copyright © 2006 Red Hat, Inc.
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software
6
 * and its documentation for any purpose is hereby granted without
7
 * fee, provided that the above copyright notice appear in all copies
8
 * and that both that copyright notice and this permission notice
9
 * appear in supporting documentation, and that the name of
10
 * Red Hat, Inc. not be used in advertising or publicity pertaining to
11
 * distribution of the software without specific, written prior
12
 * permission. Red Hat, Inc. makes no representations about the
13
 * suitability of this software for any purpose.  It is provided "as
14
 * is" without express or implied warranty.
15
 *
16
 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18
 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
19
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
 *
24
 * Author: Carl D. Worth <cworth@cworth.org>
25
 */
26

            
27
#include "cairo-test.h"
28

            
29
#define PAD 2
30
#define SIZE 10
31

            
32
typedef void (*path_func_t) (cairo_t *cr);
33

            
34
static void
35
3
rectangle (cairo_t *cr)
36
{
37
3
    cairo_rectangle (cr, PAD, PAD, SIZE, SIZE);
38
3
}
39

            
40
static void
41
3
circle (cairo_t *cr)
42
{
43
3
    cairo_arc (cr,
44
	       PAD + SIZE / 2, PAD + SIZE / 2,
45
	       SIZE / 2,
46
	       0, 2 * M_PI);
47
3
}
48

            
49
/* Given a path-generating function and two possibly translucent
50
 * patterns, fill and stroke the path with the patterns (to an
51
 * offscreen group), then blend the result into the destination.
52
 */
53
static void
54
6
fill_and_stroke (cairo_t		*cr,
55
		 path_func_t		 path_func,
56
		 cairo_pattern_t	*fill_pattern,
57
		 cairo_pattern_t	*stroke_pattern)
58
{
59
6
    cairo_push_group (cr);
60
    {
61
6
	(path_func) (cr);
62
6
	cairo_set_source (cr, fill_pattern);
63
6
	cairo_fill_preserve (cr);
64

            
65
	/* Use DEST_OUT to subtract stroke from fill. */
66
6
	cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
67
6
	cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OUT);
68
6
	cairo_stroke_preserve (cr);
69

            
70
	/* Then use ADD to draw the stroke without a seam. */
71
6
	cairo_set_source (cr, stroke_pattern);
72
6
	cairo_set_operator (cr, CAIRO_OPERATOR_ADD);
73
6
	cairo_stroke (cr);
74
    }
75
6
    cairo_pop_group_to_source (cr);
76
6
    cairo_paint (cr);
77
6
}
78

            
79
static cairo_test_status_t
80
3
draw (cairo_t *cr, int width, int height)
81
{
82
    cairo_pattern_t *blue;
83
    cairo_pattern_t *red;
84

            
85
3
    blue = cairo_pattern_create_rgba (0.0, 0.0, 1.0, 0.8);
86
3
    red = cairo_pattern_create_rgba (1.0, 0.0, 0.0, 0.2);
87

            
88
3
    cairo_test_paint_checkered (cr);
89

            
90
3
    fill_and_stroke (cr, rectangle, blue, red);
91

            
92
3
    cairo_translate (cr, SIZE + 2 * PAD, 0);
93

            
94
3
    fill_and_stroke (cr, circle, red, blue);
95

            
96
3
    cairo_pattern_destroy (blue);
97
3
    cairo_pattern_destroy (red);
98

            
99
3
    return CAIRO_TEST_SUCCESS;
100
}
101

            
102
1
CAIRO_TEST (fill_and_stroke_alpha_add,
103
	    "Use a group to fill/stroke a path (each with different alpha) using DEST_OUT and ADD to combine",
104
	    "fill-and-stroke, fill, stroke", /* keywords */
105
	    NULL, /* requirements */
106
	    2 * SIZE + 4 * PAD, SIZE + 2 * PAD,
107
	    NULL, draw)