Upload Tizen2.0 source
[framework/graphics/cairo.git] / test / recording-surface-pattern.c
1 /*
2  * Copyright © 2007 Adrian Johnson
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * Author: Adrian Johnson <ajohnson@redneon.com>
25  */
26
27 #include "cairo-test.h"
28
29 #define PAT_WIDTH  120
30 #define PAT_HEIGHT 120
31 #define SIZE (PAT_WIDTH*2)
32 #define PAD 2
33 #define WIDTH (PAD + SIZE + PAD)
34 #define HEIGHT WIDTH
35
36
37 /* This test is designed to test painting a recording surface pattern with
38  * CAIRO_EXTEND_NONE and a non identity pattern matrix.
39  */
40 static cairo_pattern_t *create_pattern (cairo_t *target)
41 {
42     cairo_surface_t *surface;
43     cairo_pattern_t *pattern;
44     cairo_t *cr;
45
46     surface = cairo_surface_create_similar (cairo_get_group_target (target),
47                                             CAIRO_CONTENT_COLOR_ALPHA,
48                                             PAT_WIDTH, PAT_HEIGHT);
49     cr = cairo_create (surface);
50     cairo_surface_destroy (surface);
51
52     cairo_set_source_rgba (cr, 1, 0, 1, 0.5);
53     cairo_rectangle (cr, PAT_WIDTH/6.0, PAT_HEIGHT/6.0, PAT_WIDTH/4.0, PAT_HEIGHT/4.0);
54     cairo_fill (cr);
55
56     cairo_set_source_rgba (cr, 0, 1, 1, 0.5);
57     cairo_rectangle (cr, PAT_WIDTH/2.0, PAT_HEIGHT/2.0, PAT_WIDTH/4.0, PAT_HEIGHT/4.0);
58     cairo_fill (cr);
59
60     cairo_set_line_width (cr, 1);
61     cairo_move_to (cr, PAT_WIDTH/6.0, 0);
62     cairo_line_to (cr, 0, 0);
63     cairo_line_to (cr, 0, PAT_HEIGHT/6.0);
64     cairo_set_source_rgb (cr, 1, 0, 0);
65     cairo_stroke (cr);
66     cairo_move_to (cr, PAT_WIDTH/6.0, PAT_HEIGHT);
67     cairo_line_to (cr, 0, PAT_HEIGHT);
68     cairo_line_to (cr, 0, 5*PAT_HEIGHT/6.0);
69     cairo_set_source_rgb (cr, 0, 1, 0);
70     cairo_stroke (cr);
71     cairo_move_to (cr, 5*PAT_WIDTH/6.0, 0);
72     cairo_line_to (cr, PAT_WIDTH, 0);
73     cairo_line_to (cr, PAT_WIDTH, PAT_HEIGHT/6.0);
74     cairo_set_source_rgb (cr, 0, 0, 1);
75     cairo_stroke (cr);
76     cairo_move_to (cr, 5*PAT_WIDTH/6.0, PAT_HEIGHT);
77     cairo_line_to (cr, PAT_WIDTH, PAT_HEIGHT);
78     cairo_line_to (cr, PAT_WIDTH, 5*PAT_HEIGHT/6.0);
79     cairo_set_source_rgb (cr, 1, 1, 0);
80     cairo_stroke (cr);
81
82     cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
83     cairo_set_line_width (cr, PAT_WIDTH/10.0);
84
85     cairo_move_to (cr, 0,         PAT_HEIGHT/4.0);
86     cairo_line_to (cr, PAT_WIDTH, PAT_HEIGHT/4.0);
87     cairo_stroke (cr);
88
89     cairo_move_to (cr, PAT_WIDTH/4.0,         0);
90     cairo_line_to (cr, PAT_WIDTH/4.0, PAT_WIDTH);
91     cairo_stroke (cr);
92
93     pattern = cairo_pattern_create_for_surface (cairo_get_target (cr));
94     cairo_destroy (cr);
95
96     return pattern;
97 }
98
99 static cairo_test_status_t
100 over (cairo_t *cr, int width, int height)
101 {
102     cairo_pattern_t *pattern;
103     cairo_matrix_t   mat;
104
105     cairo_translate (cr, PAD, PAD);
106
107     pattern = create_pattern (cr);
108
109     cairo_matrix_init_identity (&mat);
110     cairo_matrix_scale (&mat, 2, 1.5);
111     cairo_matrix_rotate (&mat, 1);
112     cairo_matrix_translate (&mat, -PAT_WIDTH/4.0, -PAT_WIDTH/2.0);
113     cairo_pattern_set_matrix (pattern, &mat);
114     cairo_pattern_set_extend (pattern, CAIRO_EXTEND_NONE);
115
116     cairo_set_source (cr, pattern);
117     cairo_paint (cr);
118
119     cairo_pattern_destroy (pattern);
120
121     return CAIRO_TEST_SUCCESS;
122 }
123
124 static cairo_test_status_t
125 source (cairo_t *cr, int width, int height)
126 {
127     cairo_pattern_t *pattern;
128     cairo_matrix_t   mat;
129
130     cairo_translate (cr, PAD, PAD);
131
132     pattern = create_pattern (cr);
133
134     cairo_matrix_init_identity (&mat);
135     cairo_matrix_scale (&mat, 2, 1.5);
136     cairo_matrix_rotate (&mat, 1);
137     cairo_matrix_translate (&mat, -PAT_WIDTH/4.0, -PAT_WIDTH/2.0);
138     cairo_pattern_set_matrix (pattern, &mat);
139     cairo_pattern_set_extend (pattern, CAIRO_EXTEND_NONE);
140
141     cairo_set_source (cr, pattern);
142     cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
143     cairo_paint (cr);
144
145     cairo_pattern_destroy (pattern);
146
147     return CAIRO_TEST_SUCCESS;
148 }
149
150 CAIRO_TEST (recording_surface_over,
151             "Paint recording surface pattern with non identity pattern matrix",
152             "recording", /* keywords */
153             NULL, /* requirements */
154             WIDTH, HEIGHT,
155             NULL, over)
156
157 CAIRO_TEST (recording_surface_source,
158             "Paint recording surface pattern with non identity pattern matrix",
159             "recording", /* keywords */
160             NULL, /* requirements */
161             WIDTH, HEIGHT,
162             NULL, source)