fe998bb047811480a1e449315477e6e5cc96df43
[framework/graphics/cairo.git] / test / rectilinear-dash.c
1 /*
2  * Copyright © 2006 Red Hat, Inc.
3  * Copyright © 2008 Chris Wilson
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: Carl D. Worth <cworth@cworth.org>
26  *         Chris Wilson <chris@chris-wilson.co.uk>
27  *
28  * Based on the original test/rectilinear-stroke.c by Carl D. Worth.
29  */
30
31 #include "cairo-test.h"
32
33 #define SIZE 50
34
35 static void
36 draw_dashes (cairo_t *cr)
37 {
38     const double dash_square[4] = {4, 2, 2, 2};
39     const double dash_butt[4] = {5, 1, 3, 1};
40
41     cairo_save (cr);
42
43     cairo_set_dash (cr, dash_square, 4, 0);
44
45     cairo_set_line_width (cr, 1.0);
46     cairo_translate (cr, 1, 1);
47
48     /* Draw everything first with square caps. */
49     cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
50
51     /* Draw horizontal and vertical segments, each in both
52      * directions. */
53     cairo_move_to     (cr,  4.5,  0.5);
54     cairo_rel_line_to (cr,  2.0,  0.0);
55
56     cairo_move_to     (cr, 10.5,  4.5);
57     cairo_rel_line_to (cr,  0.0,  2.0);
58
59     cairo_move_to     (cr,  6.5, 10.5);
60     cairo_rel_line_to (cr, -2.0,  0.0);
61
62     cairo_move_to     (cr,  0.5,  6.5);
63     cairo_rel_line_to (cr,  0.0, -2.0);
64
65     /* Draw right angle turns in four directions. */
66     cairo_move_to     (cr,  0.5,  2.5);
67     cairo_rel_line_to (cr,  0.0, -2.0);
68     cairo_rel_line_to (cr,  2.0,  0.0);
69
70     cairo_move_to     (cr,  8.5,  0.5);
71     cairo_rel_line_to (cr,  2.0,  0.0);
72     cairo_rel_line_to (cr,  0.0,  2.0);
73
74     cairo_move_to     (cr, 10.5,  8.5);
75     cairo_rel_line_to (cr,  0.0,  2.0);
76     cairo_rel_line_to (cr, -2.0,  0.0);
77
78     cairo_move_to     (cr,  2.5, 10.5);
79     cairo_rel_line_to (cr, -2.0,  0.0);
80     cairo_rel_line_to (cr,  0.0, -2.0);
81
82     cairo_stroke (cr);
83
84     /* Draw a closed-path rectangle */
85     cairo_rectangle (cr, 0.5, 12.5, 10.0, 10.0);
86     cairo_set_dash (cr, dash_square, 4, 2);
87     cairo_stroke (cr);
88
89     cairo_translate (cr, 12, 0);
90
91     /* Now draw the same results, but with butt caps. */
92     cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
93     cairo_set_dash (cr, dash_butt, 4, 0.0);
94
95     /* Draw horizontal and vertical segments, each in both
96      * directions. */
97     cairo_move_to     (cr,  4.0,  0.5);
98     cairo_rel_line_to (cr,  3.0,  0.0);
99
100     cairo_move_to     (cr, 10.5,  4.0);
101     cairo_rel_line_to (cr,  0.0,  3.0);
102
103     cairo_move_to     (cr,  7.0, 10.5);
104     cairo_rel_line_to (cr, -3.0,  0.0);
105
106     cairo_move_to     (cr,  0.5,  7.0);
107     cairo_rel_line_to (cr,  0.0, -3.0);
108
109     /* Draw right angle turns in four directions. */
110     cairo_move_to     (cr,  0.5,  3.0);
111     cairo_rel_line_to (cr,  0.0, -2.5);
112     cairo_rel_line_to (cr,  2.5,  0.0);
113
114     cairo_move_to     (cr,  8.0,  0.5);
115     cairo_rel_line_to (cr,  2.5,  0.0);
116     cairo_rel_line_to (cr,  0.0,  2.5);
117
118     cairo_move_to     (cr, 10.5,  8.0);
119     cairo_rel_line_to (cr,  0.0,  2.5);
120     cairo_rel_line_to (cr, -2.5,  0.0);
121
122     cairo_move_to     (cr,  3.0, 10.5);
123     cairo_rel_line_to (cr, -2.5,  0.0);
124     cairo_rel_line_to (cr,  0.0, -2.5);
125
126     cairo_stroke (cr);
127
128     /* Draw a closed-path rectangle */
129     cairo_set_dash (cr, dash_butt, 4, 2.5);
130     cairo_rectangle (cr, 0.5, 12.5, 10.0, 10.0);
131     cairo_stroke (cr);
132
133     cairo_restore (cr);
134 }
135
136 static cairo_test_status_t
137 draw (cairo_t *cr, int width, int height)
138 {
139     /* Paint background white, then draw in black. */
140     cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
141     cairo_paint (cr);
142
143     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
144     draw_dashes (cr);
145
146     cairo_save (cr);
147     cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
148     cairo_translate (cr, 0, height);
149     cairo_scale (cr, 1, -1);
150     draw_dashes (cr);
151     cairo_restore (cr);
152
153     cairo_save (cr);
154     cairo_set_source_rgb (cr, 0.0, 1.0, 0.0);
155     cairo_translate (cr, width, 0);
156     cairo_scale (cr, -1, 1);
157     draw_dashes (cr);
158     cairo_restore (cr);
159
160     cairo_save (cr);
161     cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
162     cairo_translate (cr, width, height);
163     cairo_scale (cr, -1, -1);
164     draw_dashes (cr);
165     cairo_restore (cr);
166
167     return CAIRO_TEST_SUCCESS;
168 }
169
170 CAIRO_TEST (rectilinear_dash,
171             "Test dashed rectilinear stroke operations (covering only whole pixels)",
172             "stroke dash", /* keywords */
173             NULL, /* requirements */
174             SIZE, SIZE,
175             NULL, draw)
176