23a55ff63e5184401f1ea7c41b24fa78ea613668
[framework/graphics/cairo.git] / test / clip-mixed-antialias.c
1 /*
2  * Copyright © 2011 Intel Corporation
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: Chris Wilson <chris@chris-wilson.co.uk>
25  */
26
27 #include "cairo-test.h"
28
29 #define WIDTH   200
30 #define HEIGHT  200
31
32 /* This is an example from the wild, as silly as it is
33 n 52 0 429 709 rectangle
34 clip+
35 //ANTIALIAS_NONE set-antialias
36 n 8 0 m 952 0 l 956.417969 0 960 3.582031 960 8 c 960 1807 l 960 1811.417969 956.417969 1815 952 1815 c 8 1815 l 3.582031 1815 0 1811.417969 0 1807 c 0 8 l 0 3.582031 3.582031 0 8 0 c h
37 clip+
38 //EVEN_ODD set-fill-rule
39 n 0 0 m 480 0 l 480 708 l 0 708 l h 8 1 m 952 1 l 955.867188 1 959 4.132812 959 8 c 959 1807 l 959 1810.867188 955.867188 1814 952 1814 c 8 1814 l 4.132812 1814 1 1810.867188 1 1807 c 1 8 l 1 4.132812 4.132812 1 8 1 c h
40 clip+
41 //WINDING set-fill-rule
42 //ANTIALIAS_DEFAULT set-antialias
43 n 960 0 m 52.5 907.5 l 52.5 1815 l 960 1815 l h
44 clip+
45 //ANTIALIAS_NONE set-antialias
46 n 960 0 m 52.5 0 l 52.5 907.5 l 960 1815 l h
47 clip+
48 */
49
50 static void background (cairo_t *cr)
51 {
52     cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
53     cairo_set_source_rgb (cr, 1,1,1);
54     cairo_paint (cr);
55     cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
56 }
57
58 static void clip_0 (cairo_t *cr)
59 {
60     cairo_rectangle (cr, 5, 5, 190, 190);
61     cairo_clip (cr);
62 }
63
64 static void clip_1 (cairo_t *cr)
65 {
66     cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
67
68     cairo_arc (cr, 100, 100, 125, 0, 2*M_PI);
69     cairo_clip (cr);
70
71     cairo_set_antialias (cr, CAIRO_ANTIALIAS_DEFAULT);
72 }
73
74 static void
75 rounded_rectangle (cairo_t *cr, int x, int y, int w, int h, int r)
76 {
77     cairo_new_sub_path (cr);
78     cairo_arc (cr, x + r, y + r, r, M_PI, 3 * M_PI / 2);
79     cairo_arc (cr, x + w - r, y + r, r, 3 *M_PI / 2, 2 * M_PI);
80     cairo_arc (cr, x + w - r, y + h - r, r, 0, M_PI / 2);
81     cairo_arc (cr, x + r, y + h - r, r, M_PI / 2, M_PI);
82     cairo_close_path (cr);
83 }
84
85 static void clip_2 (cairo_t *cr)
86 {
87     cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
88
89     rounded_rectangle (cr, 50, 50, 100, 100, 15);
90     rounded_rectangle (cr, 60, 60, 80, 80, 5);
91     cairo_clip (cr);
92
93     cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING);
94 }
95
96 static void clip_3 (cairo_t *cr)
97 {
98     cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
99
100     cairo_rectangle (cr, 40.25, 60.25, 120, 80);
101     cairo_rectangle (cr, 60.25, 40.25, 80, 120);
102     cairo_clip (cr);
103
104     cairo_set_antialias (cr, CAIRO_ANTIALIAS_DEFAULT);
105 }
106
107 static cairo_test_status_t
108 draw (cairo_t *cr, int width, int height)
109 {
110     background (cr);
111
112     clip_0 (cr);
113     clip_1 (cr);
114     clip_2 (cr);
115     clip_3 (cr);
116
117     cairo_set_source_rgb (cr, 0, 0, 0);
118     cairo_paint (cr);
119
120     return CAIRO_TEST_SUCCESS;
121 }
122
123 CAIRO_TEST (clip_mixed_antialias,
124             "Test drawing through through an mixture of clips",
125             "clip", /* keywords */
126             "target=raster", /* requirements */
127             WIDTH, HEIGHT,
128             NULL, draw)