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
24
draw_dashes (cairo_t *cr)
37
{
38
24
    const double dash_square[4] = {4, 2, 2, 2};
39
24
    const double dash_butt[4] = {5, 1, 3, 1};
40

            
41
24
    cairo_save (cr);
42

            
43
24
    cairo_set_dash (cr, dash_square, 4, 0);
44

            
45
24
    cairo_set_line_width (cr, 1.0);
46
24
    cairo_translate (cr, 1, 1);
47

            
48
    /* Draw everything first with square caps. */
49
24
    cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
50

            
51
    /* Draw horizontal and vertical segments, each in both
52
     * directions. */
53
24
    cairo_move_to     (cr,  4.5,  0.5);
54
24
    cairo_rel_line_to (cr,  2.0,  0.0);
55

            
56
24
    cairo_move_to     (cr, 10.5,  4.5);
57
24
    cairo_rel_line_to (cr,  0.0,  2.0);
58

            
59
24
    cairo_move_to     (cr,  6.5, 10.5);
60
24
    cairo_rel_line_to (cr, -2.0,  0.0);
61

            
62
24
    cairo_move_to     (cr,  0.5,  6.5);
63
24
    cairo_rel_line_to (cr,  0.0, -2.0);
64

            
65
    /* Draw right angle turns in four directions. */
66
24
    cairo_move_to     (cr,  0.5,  2.5);
67
24
    cairo_rel_line_to (cr,  0.0, -2.0);
68
24
    cairo_rel_line_to (cr,  2.0,  0.0);
69

            
70
24
    cairo_move_to     (cr,  8.5,  0.5);
71
24
    cairo_rel_line_to (cr,  2.0,  0.0);
72
24
    cairo_rel_line_to (cr,  0.0,  2.0);
73

            
74
24
    cairo_move_to     (cr, 10.5,  8.5);
75
24
    cairo_rel_line_to (cr,  0.0,  2.0);
76
24
    cairo_rel_line_to (cr, -2.0,  0.0);
77

            
78
24
    cairo_move_to     (cr,  2.5, 10.5);
79
24
    cairo_rel_line_to (cr, -2.0,  0.0);
80
24
    cairo_rel_line_to (cr,  0.0, -2.0);
81

            
82
24
    cairo_stroke (cr);
83

            
84
    /* Draw a closed-path rectangle */
85
24
    cairo_rectangle (cr, 0.5, 12.5, 10.0, 10.0);
86
24
    cairo_set_dash (cr, dash_square, 4, 2);
87
24
    cairo_stroke (cr);
88

            
89
24
    cairo_translate (cr, 12, 0);
90

            
91
    /* Now draw the same results, but with butt caps. */
92
24
    cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
93
24
    cairo_set_dash (cr, dash_butt, 4, 0.0);
94

            
95
    /* Draw horizontal and vertical segments, each in both
96
     * directions. */
97
24
    cairo_move_to     (cr,  4.0,  0.5);
98
24
    cairo_rel_line_to (cr,  3.0,  0.0);
99

            
100
24
    cairo_move_to     (cr, 10.5,  4.0);
101
24
    cairo_rel_line_to (cr,  0.0,  3.0);
102

            
103
24
    cairo_move_to     (cr,  7.0, 10.5);
104
24
    cairo_rel_line_to (cr, -3.0,  0.0);
105

            
106
24
    cairo_move_to     (cr,  0.5,  7.0);
107
24
    cairo_rel_line_to (cr,  0.0, -3.0);
108

            
109
    /* Draw right angle turns in four directions. */
110
24
    cairo_move_to     (cr,  0.5,  3.0);
111
24
    cairo_rel_line_to (cr,  0.0, -2.5);
112
24
    cairo_rel_line_to (cr,  2.5,  0.0);
113

            
114
24
    cairo_move_to     (cr,  8.0,  0.5);
115
24
    cairo_rel_line_to (cr,  2.5,  0.0);
116
24
    cairo_rel_line_to (cr,  0.0,  2.5);
117

            
118
24
    cairo_move_to     (cr, 10.5,  8.0);
119
24
    cairo_rel_line_to (cr,  0.0,  2.5);
120
24
    cairo_rel_line_to (cr, -2.5,  0.0);
121

            
122
24
    cairo_move_to     (cr,  3.0, 10.5);
123
24
    cairo_rel_line_to (cr, -2.5,  0.0);
124
24
    cairo_rel_line_to (cr,  0.0, -2.5);
125

            
126
24
    cairo_stroke (cr);
127

            
128
    /* Draw a closed-path rectangle */
129
24
    cairo_set_dash (cr, dash_butt, 4, 2.5);
130
24
    cairo_rectangle (cr, 0.5, 12.5, 10.0, 10.0);
131
24
    cairo_stroke (cr);
132

            
133
24
    cairo_restore (cr);
134
24
}
135

            
136
static cairo_test_status_t
137
6
dashes (cairo_t *cr)
138
{
139
    /* Paint background white, then draw in black. */
140
6
    cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
141
6
    cairo_paint (cr);
142

            
143
6
    cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
144
6
    draw_dashes (cr);
145

            
146
6
    cairo_save (cr);
147
6
    cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
148
6
    cairo_translate (cr, 0, SIZE);
149
6
    cairo_scale (cr, 1, -1);
150
6
    draw_dashes (cr);
151
6
    cairo_restore (cr);
152

            
153
6
    cairo_save (cr);
154
6
    cairo_set_source_rgb (cr, 0.0, 1.0, 0.0);
155
6
    cairo_translate (cr, SIZE, 0);
156
6
    cairo_scale (cr, -1, 1);
157
6
    draw_dashes (cr);
158
6
    cairo_restore (cr);
159

            
160
6
    cairo_save (cr);
161
6
    cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
162
6
    cairo_translate (cr, SIZE, SIZE);
163
6
    cairo_scale (cr, -1, -1);
164
6
    draw_dashes (cr);
165
6
    cairo_restore (cr);
166

            
167
6
    return CAIRO_TEST_SUCCESS;
168
}
169

            
170
static cairo_test_status_t
171
3
aligned (cairo_t *cr, int width, int height)
172
{
173
3
    cairo_scale (cr, 4, 2);
174
3
    return dashes(cr);
175
}
176

            
177
static cairo_test_status_t
178
3
unaligned (cairo_t *cr, int width, int height)
179
{
180
3
    cairo_scale (cr, 3.9, 1.9);
181
3
    return dashes(cr);
182
}
183

            
184
1
CAIRO_TEST (rectilinear_dash_scale,
185
	    "Test dashed rectilinear stroke operations (covering only whole pixels) after scaling",
186
	    "stroke, dash", /* keywords */
187
	    NULL, /* requirements */
188
	    4*SIZE, 2*SIZE,
189
	    NULL, aligned)
190

            
191
1
CAIRO_TEST (rectilinear_dash_scale_unaligned,
192
	    "Test dashed rectilinear stroke operations (covering partial pixels) after scaling",
193
	    "stroke, dash", /* keywords */
194
	    NULL, /* requirements */
195
	    4*SIZE, 2*SIZE,
196
	    NULL, unaligned)