1
/*
2
 * Copyright © 2021 Rick Yorgason
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

            
25
#include "cairo-test.h"
26

            
27
/**
28
 * draw:
29
 * @cr: a #cairo_t
30
 * @width: Width of the test image
31
 * @height: Height of the test image
32
 * @scale_width: Percentage to scale the width
33
 * @scale_height: Percentage to scale the height
34
 * @fit_to_scale: Whether or not to adjust the image to fit in the scale parameters
35
 * regardless of the scale parameters
36
 * @correct_scale: Tests if the hairlines render correctly regardless of 
37
 * whether or not the scale is set "correctly", as per
38
 * https://cairographics.org/cookbook/ellipses/ 
39
 */
40
static cairo_test_status_t
41
15
draw (cairo_t *cr, int width, int height, double scale_width, double scale_height, cairo_bool_t fit_to_scale, cairo_bool_t correct_scale)
42
{
43
	cairo_matrix_t save_matrix;
44
15
	double fit_width = fit_to_scale ? scale_width : 1.0;
45
15
	double fit_height = fit_to_scale ? scale_height : 1.0;
46
15
	double fit_max = MAX(fit_width, fit_height);
47
15
	double dash[] = {3.0};
48

            
49
15
	if (cairo_get_hairline (cr) == TRUE) {
50
		return CAIRO_TEST_ERROR;
51
	}
52

            
53
	/* Clear background */
54
15
	cairo_set_source_rgb (cr, 1, 1, 1);
55
15
	cairo_paint (cr);
56

            
57
15
	cairo_set_source_rgb (cr, 0, 0, 0);
58
15
	cairo_set_line_width (cr, 100.0); /* If everything is working right, this value should never get used */
59

            
60
	/* Hairline sample */
61
15
	if (correct_scale) {
62
12
		cairo_get_matrix (cr, &save_matrix);
63
	}
64
15
	cairo_scale (cr, scale_width, scale_height);
65

            
66
15
	cairo_set_hairline (cr, TRUE);
67
15
	if (cairo_get_hairline (cr) == FALSE) {
68
		return CAIRO_TEST_ERROR;
69
	}
70

            
71
15
	cairo_move_to (cr, 0, 0);
72
15
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
73
15
	cairo_move_to (cr, width/fit_width/2, 0);
74
15
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
75
15
	cairo_move_to (cr, 0, height/fit_height/2);
76
15
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
77
15
	cairo_move_to (cr, width/fit_width/4, 0);
78
15
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
79
15
	cairo_arc (cr, width/fit_width/2, height/fit_height/2, width/fit_max/4, M_PI*0.5, M_PI*1.0);
80

            
81
15
	if (correct_scale) {
82
12
		cairo_set_matrix (cr, &save_matrix);
83
	}
84
15
	cairo_stroke (cr);
85

            
86
	/* Dashed sample */
87
15
	if (correct_scale) {
88
12
		cairo_get_matrix (cr, &save_matrix);
89
12
		cairo_scale (cr, scale_width, scale_height);
90
	}
91
15
	cairo_set_dash (cr, dash, 1, 0);
92
15
	cairo_arc (cr, width/fit_width/2, height/fit_height/2, width/fit_max/4, M_PI*1.0, M_PI*1.5);
93
15
	if (correct_scale) {
94
12
		cairo_set_matrix (cr, &save_matrix);
95
	}
96
15
	cairo_stroke (cr);
97

            
98
	/* Control sample */
99
15
	if (correct_scale) {
100
12
		cairo_get_matrix (cr, &save_matrix);
101
12
		cairo_scale (cr, scale_width, scale_height);
102
	}
103

            
104
15
	cairo_set_line_width (cr, 3.0);
105
15
	cairo_set_hairline (cr, FALSE);
106
15
	if (cairo_get_hairline (cr) == TRUE) {
107
		return CAIRO_TEST_ERROR;
108
	}
109

            
110
15
	cairo_set_dash (cr, 0, 0, 0);
111

            
112
15
	cairo_move_to (cr, width/fit_width, height/fit_height);
113
15
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
114
15
	cairo_move_to (cr, width/fit_width/2, height/fit_height);
115
15
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
116
15
	cairo_move_to (cr, width/fit_width, height/fit_height/2);
117
15
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
118
15
	cairo_move_to (cr, width/fit_width*0.75, height/fit_height);
119
15
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
120
15
	cairo_arc (cr, width/fit_width/2, height/fit_height/2, width/fit_max/4, M_PI*1.5, M_PI*2.0);
121

            
122
15
	if (correct_scale) {
123
12
		cairo_set_matrix (cr, &save_matrix);
124
	}
125
15
	cairo_stroke (cr);
126

            
127
	/* Dashed sample */
128
15
	if (correct_scale) {
129
12
		cairo_get_matrix (cr, &save_matrix);
130
12
		cairo_scale (cr, scale_width, scale_height);
131
	}
132
15
	cairo_set_dash (cr, dash, 1, 0);
133
15
	cairo_arc (cr, width/fit_width/2, height/fit_height/2, width/fit_max/4, 0, M_PI*0.5);
134
15
	if (correct_scale) {
135
12
		cairo_set_matrix (cr, &save_matrix);
136
	}
137
15
	cairo_stroke (cr);
138

            
139
15
	return CAIRO_TEST_SUCCESS;
140
}
141

            
142
static cairo_test_status_t
143
6
draw_typical (cairo_t *cr, int width, int height)
144
{
145
6
	return draw (cr, width, height, 1.0, 1.0, TRUE, TRUE);
146
}
147

            
148
static cairo_test_status_t
149
3
draw_scaled (cairo_t *cr, int width, int height)
150
{
151
3
	return draw (cr, width, height, 0.5, 0.5, FALSE, TRUE);
152
}
153

            
154
static cairo_test_status_t
155
3
draw_anisotropic (cairo_t *cr, int width, int height)
156
{
157
3
	return draw (cr, width, height, 2.0, 5.0, TRUE, TRUE);
158
}
159

            
160
static cairo_test_status_t
161
3
draw_anisotropic_incorrect (cairo_t *cr, int width, int height)
162
{
163
3
	return draw (cr, width, height, 2.0, 5.0, TRUE, FALSE);
164
}
165

            
166
1
CAIRO_TEST (hairline,
167
		    "Tests hairlines are drawn at a single pixel width",
168
			"path, stroke, hairline", /* keywords */
169
			NULL, /* requirements */
170
			49, 49,
171
			NULL, draw_typical)
172

            
173
1
CAIRO_TEST (hairline_big,
174
		    "Tests hairlines are drawn at a single pixel width",
175
			"path, stroke, hairline", /* keywords */
176
			NULL, /* requirements */
177
			99, 99,
178
			NULL, draw_typical)
179

            
180
1
CAIRO_TEST (hairline_scaled,
181
		    "Tests hairlines are drawn at a single pixel width",
182
			"path, stroke, hairline", /* keywords */
183
			NULL, /* requirements */
184
			99, 99,
185
			NULL, draw_scaled)
186

            
187
1
CAIRO_TEST (hairline_anisotropic,
188
		    "Tests hairlines with a really lopsided scale parameter",
189
			"path, stroke, hairline", /* keywords */
190
			NULL, /* requirements */
191
			99, 99,
192
			NULL, draw_anisotropic)
193

            
194
1
CAIRO_TEST (hairline_anisotropic_incorrect,
195
		    "Tests hairlines with a really lopsided scale parameter",
196
			"path, stroke, hairline", /* keywords */
197
			NULL, /* requirements */
198
			99, 99,
199
			NULL, draw_anisotropic_incorrect)