1
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2
/*
3
 * Copyright 2009 Andrea Canciani
4
 *
5
 * Permission is hereby granted, free of charge, to any person
6
 * obtaining a copy of this software and associated documentation
7
 * files (the "Software"), to deal in the Software without
8
 * restriction, including without limitation the rights to use, copy,
9
 * modify, merge, publish, distribute, sublicense, and/or sell copies
10
 * of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 *
25
 * Author: Andrea Canciani <ranma42@gmail.com>
26
 */
27

            
28
#include "cairo-test.h"
29

            
30
#define N_OPERATORS (CAIRO_OPERATOR_SATURATE + 1)
31
#define HEIGHT 16
32
#define WIDTH 16
33
#define PAD 3
34

            
35
static cairo_pattern_t*
36
6
_create_pattern (cairo_surface_t *target, cairo_content_t content, int width, int height)
37
{
38
    cairo_pattern_t *pattern;
39
    cairo_surface_t *surface;
40
    cairo_t *cr;
41

            
42
6
    surface = cairo_surface_create_similar (target, content, width, height);
43
6
    cr = cairo_create (surface);
44
6
    cairo_surface_destroy (surface);
45

            
46
6
    cairo_set_source_rgb (cr, 1, 0, 0);
47
6
    cairo_arc (cr, 0.5 * width, 0.5 * height, 0.45 * height, -M_PI / 4, 3 * M_PI / 4);
48
6
    cairo_fill (cr);
49

            
50
6
    pattern = cairo_pattern_create_for_surface (cairo_get_target (cr));
51
6
    cairo_destroy (cr);
52

            
53
6
    return pattern;
54
}
55

            
56
static cairo_test_status_t
57
3
draw (cairo_t *cr, int width, int height)
58
{
59
    cairo_pattern_t *alpha_pattern, *color_alpha_pattern, *pattern;
60
    unsigned int n, i;
61

            
62
3
    alpha_pattern = _create_pattern (cairo_get_target (cr),
63
				     CAIRO_CONTENT_ALPHA,
64
				     0.9 * WIDTH, 0.9 * HEIGHT);
65
3
    color_alpha_pattern = _create_pattern (cairo_get_target (cr),
66
					   CAIRO_CONTENT_COLOR_ALPHA,
67
					   0.9 * WIDTH, 0.9 * HEIGHT);
68

            
69
3
    pattern = cairo_pattern_create_linear (WIDTH, 0, 0, HEIGHT);
70
3
    cairo_pattern_add_color_stop_rgba (pattern, 0.2, 0, 0, 1, 1);
71
3
    cairo_pattern_add_color_stop_rgba (pattern, 0.8, 0, 0, 1, 0);
72

            
73
3
    cairo_translate (cr, PAD, PAD);
74

            
75
45
    for (n = 0; n < N_OPERATORS; n++) {
76
42
	cairo_save (cr);
77
210
	for (i = 0; i < 4; i++) {
78
168
	    cairo_reset_clip (cr);
79
168
	    cairo_rectangle (cr, 0, 0, WIDTH, HEIGHT);
80
168
	    cairo_clip (cr);
81

            
82
168
	    cairo_set_source (cr, pattern);
83
168
	    cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
84
168
	    if (i & 2) {
85
84
	        cairo_paint (cr);
86
	    } else {
87
84
	        cairo_rectangle (cr, WIDTH/2, HEIGHT/2, WIDTH, HEIGHT);
88
84
		cairo_fill (cr);
89
	    }
90

            
91
168
	    cairo_set_source (cr, i & 1 ? alpha_pattern : color_alpha_pattern);
92
168
	    cairo_set_operator (cr, n);
93
168
	    if (i & 2) {
94
84
	        cairo_paint (cr);
95
	    } else {
96
84
	        cairo_rectangle (cr, WIDTH/2, HEIGHT/2, WIDTH, HEIGHT);
97
84
		cairo_fill (cr);
98
	    }
99

            
100
168
	    cairo_translate (cr, 0, HEIGHT+PAD);
101
	}
102
42
	cairo_restore (cr);
103

            
104
42
	cairo_translate (cr, WIDTH+PAD, 0);
105
    }
106

            
107
3
    cairo_pattern_destroy (pattern);
108
3
    cairo_pattern_destroy (alpha_pattern);
109
3
    cairo_pattern_destroy (color_alpha_pattern);
110

            
111
3
    return CAIRO_TEST_SUCCESS;
112
}
113

            
114
1
CAIRO_TEST (surface_pattern_operator,
115
	    "Tests alpha-only and alpha-color sources with all operators",
116
	    "surface, pattern, operator", /* keywords */
117
	    NULL, /* requirements */
118
	    (WIDTH+PAD) * N_OPERATORS + PAD, 4*HEIGHT + 5*PAD,
119
	    NULL, draw)