summaryrefslogtreecommitdiff
path: root/test/hairline.c
blob: 78fdca4a673c0a9dd32c3c31a19483585884a81e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright © 2021 Rick Yorgason
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "cairo-test.h"

/**
 * draw:
 * @cr: a #cairo_t
 * @width: Width of the test image
 * @height: Height of the test image
 * @scale_width: Percentage to scale the width
 * @scale_height: Percentage to scale the height
 * @fit_to_scale: Whether or not to adjust the image to fit in the scale parameters
 * regardless of the scale parameters
 * @correct_scale: Tests if the hairlines render correctly regardless of 
 * whether or not the scale is set "correctly", as per
 * https://cairographics.org/cookbook/ellipses/ 
 */
static cairo_test_status_t
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)
{
	cairo_matrix_t save_matrix;
	double fit_width = fit_to_scale ? scale_width : 1.0;
	double fit_height = fit_to_scale ? scale_height : 1.0;
	double fit_max = MAX(fit_width, fit_height);
	double dash[] = {3.0};

	if (cairo_get_hairline (cr) == TRUE) {
		return CAIRO_TEST_ERROR;
	}

	/* Clear background */
	cairo_set_source_rgb (cr, 1, 1, 1);
	cairo_paint (cr);

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

	/* Hairline sample */
	if (correct_scale) {
		cairo_get_matrix (cr, &save_matrix);
	}
	cairo_scale (cr, scale_width, scale_height);

	cairo_set_hairline (cr, TRUE);
	if (cairo_get_hairline (cr) == FALSE) {
		return CAIRO_TEST_ERROR;
	}

	cairo_move_to (cr, 0, 0);
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
	cairo_move_to (cr, width/fit_width/2, 0);
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
	cairo_move_to (cr, 0, height/fit_height/2);
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
	cairo_move_to (cr, width/fit_width/4, 0);
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
	cairo_arc (cr, width/fit_width/2, height/fit_height/2, width/fit_max/4, M_PI*0.5, M_PI*1.0);

	if (correct_scale) {
		cairo_set_matrix (cr, &save_matrix);
	}
	cairo_stroke (cr);

	/* Dashed sample */
	if (correct_scale) {
		cairo_get_matrix (cr, &save_matrix);
		cairo_scale (cr, scale_width, scale_height);
	}
	cairo_set_dash (cr, dash, 1, 0);
	cairo_arc (cr, width/fit_width/2, height/fit_height/2, width/fit_max/4, M_PI*1.0, M_PI*1.5);
	if (correct_scale) {
		cairo_set_matrix (cr, &save_matrix);
	}
	cairo_stroke (cr);

	/* Control sample */
	if (correct_scale) {
		cairo_get_matrix (cr, &save_matrix);
		cairo_scale (cr, scale_width, scale_height);
	}

	cairo_set_line_width (cr, 3.0);
	cairo_set_hairline (cr, FALSE);
	if (cairo_get_hairline (cr) == TRUE) {
		return CAIRO_TEST_ERROR;
	}

	cairo_set_dash (cr, 0, 0, 0);

	cairo_move_to (cr, width/fit_width, height/fit_height);
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
	cairo_move_to (cr, width/fit_width/2, height/fit_height);
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
	cairo_move_to (cr, width/fit_width, height/fit_height/2);
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
	cairo_move_to (cr, width/fit_width*0.75, height/fit_height);
	cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
	cairo_arc (cr, width/fit_width/2, height/fit_height/2, width/fit_max/4, M_PI*1.5, M_PI*2.0);

	if (correct_scale) {
		cairo_set_matrix (cr, &save_matrix);
	}
	cairo_stroke (cr);

	/* Dashed sample */
	if (correct_scale) {
		cairo_get_matrix (cr, &save_matrix);
		cairo_scale (cr, scale_width, scale_height);
	}
	cairo_set_dash (cr, dash, 1, 0);
	cairo_arc (cr, width/fit_width/2, height/fit_height/2, width/fit_max/4, 0, M_PI*0.5);
	if (correct_scale) {
		cairo_set_matrix (cr, &save_matrix);
	}
	cairo_stroke (cr);

	return CAIRO_TEST_SUCCESS;
}

static cairo_test_status_t
draw_typical (cairo_t *cr, int width, int height)
{
	return draw (cr, width, height, 1.0, 1.0, TRUE, TRUE);
}

static cairo_test_status_t
draw_scaled (cairo_t *cr, int width, int height)
{
	return draw (cr, width, height, 0.5, 0.5, FALSE, TRUE);
}

static cairo_test_status_t
draw_anisotropic (cairo_t *cr, int width, int height)
{
	return draw (cr, width, height, 2.0, 5.0, TRUE, TRUE);
}

static cairo_test_status_t
draw_anisotropic_incorrect (cairo_t *cr, int width, int height)
{
	return draw (cr, width, height, 2.0, 5.0, TRUE, FALSE);
}

CAIRO_TEST (hairline,
		    "Tests hairlines are drawn at a single pixel width",
			"path, stroke, hairline", /* keywords */
			NULL, /* requirements */
			49, 49,
			NULL, draw_typical)

CAIRO_TEST (hairline_big,
		    "Tests hairlines are drawn at a single pixel width",
			"path, stroke, hairline", /* keywords */
			NULL, /* requirements */
			99, 99,
			NULL, draw_typical)

CAIRO_TEST (hairline_scaled,
		    "Tests hairlines are drawn at a single pixel width",
			"path, stroke, hairline", /* keywords */
			NULL, /* requirements */
			99, 99,
			NULL, draw_scaled)

CAIRO_TEST (hairline_anisotropic,
		    "Tests hairlines with a really lopsided scale parameter",
			"path, stroke, hairline", /* keywords */
			NULL, /* requirements */
			99, 99,
			NULL, draw_anisotropic)

CAIRO_TEST (hairline_anisotropic_incorrect,
		    "Tests hairlines with a really lopsided scale parameter",
			"path, stroke, hairline", /* keywords */
			NULL, /* requirements */
			99, 99,
			NULL, draw_anisotropic_incorrect)