2 * Copyright © 2006 Red Hat, Inc.
4 * Permission to use, copy, modify, distribute, and sell this software
5 * and its documentation for any purpose is hereby granted without
6 * fee, provided that the above copyright notice appear in all copies
7 * and that both that copyright notice and this permission notice
8 * appear in supporting documentation, and that the name of
9 * Red Hat, Inc. not be used in advertising or publicity pertaining to
10 * distribution of the software without specific, written prior
11 * permission. Red Hat, Inc. makes no representations about the
12 * suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
18 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
21 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Author: Carl D. Worth <cworth@cworth.org>
26 #include "cairo-test.h"
28 /* This test exercises the various interactions between
29 * cairo_set_line_width and cairo_scale. Specifically it shows how
30 * separate transformations can affect the pen for stroking compared
33 * This was inspired by an image by Maxim Shemanarev demonstrating the
34 * flexible-pipeline nature of his Antigrain Geometry project:
36 * http://antigrain.com/tips/line_alignment/conv_order.gif
38 * It also uncovered some behavior in cairo that I found surprising.
39 * Namely, cairo_set_line_width was not transforming the width
40 * according the the current CTM, but instead delaying that
41 * transformation until the time of cairo_stroke.
43 * This delayed behavior was released in cairo 1.0 so we're going to
44 * document this as the way cairo_set_line_width works rather than
45 * considering this a bug.
52 #define WIDTH (XSCALE * SPLINE * 6.0)
53 #define HEIGHT (YSCALE * SPLINE * 2.0)
55 static cairo_test_draw_function_t draw
;
59 "Tests interaction of cairo_set_line_width with cairo_scale",
65 spline_path (cairo_t
*cr
)
72 - SPLINE
/ 4, - SPLINE
,
79 /* If we scale before setting the line width or creating the path,
80 * then obviously both will be scaled. */
82 scale_then_set_line_width_and_stroke (cairo_t
*cr
)
84 cairo_scale (cr
, XSCALE
, YSCALE
);
85 cairo_set_line_width (cr
, LINE_WIDTH
);
90 /* This is used to verify the results of
91 * scale_then_set_line_width_and_stroke.
93 * It uses save/restore pairs to isolate the scaling of the path and
94 * line_width and ensures that both are scaled.
97 scale_path_and_line_width (cairo_t
*cr
)
101 cairo_scale (cr
, XSCALE
, YSCALE
);
108 cairo_scale (cr
, XSCALE
, YSCALE
);
109 cairo_set_line_width (cr
, LINE_WIDTH
);
115 /* This is the case that was surprising.
117 * Setting the line width before scaling doesn't change anything. The
118 * line width will be interpreted under the CTM in effect at the time
119 * of cairo_stroke, so the line width will be scaled as well as the
123 set_line_width_then_scale_and_stroke (cairo_t
*cr
)
125 cairo_set_line_width (cr
, LINE_WIDTH
);
126 cairo_scale (cr
, XSCALE
, YSCALE
);
131 /* Here then is the way to achieve the alternate result.
133 * This uses save/restore pairs to isolate the scaling of the path and
134 * line_width and ensures that the path is scaled while the line width
138 scale_path_not_line_width (cairo_t
*cr
)
142 cairo_scale (cr
, XSCALE
, YSCALE
);
149 cairo_set_line_width (cr
, LINE_WIDTH
);
155 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
157 static cairo_test_status_t
158 draw (cairo_t
*cr
, int width
, int height
)
161 typedef void (*figure_t
) (cairo_t
*cr
);
162 figure_t figures
[4] = {
163 scale_then_set_line_width_and_stroke
,
164 scale_path_and_line_width
,
165 set_line_width_then_scale_and_stroke
,
166 scale_path_not_line_width
169 cairo_set_source_rgb (cr
, 1.0, 1.0, 1.0); /* white */
171 cairo_set_source_rgb (cr
, 0.0, 0.0, 0.0); /* black */
173 for (i
= 0; i
< 4; i
++) {
176 WIDTH
/4 + (i
% 2) * WIDTH
/2,
177 HEIGHT
/4 + (i
/ 2) * HEIGHT
/2);
182 return CAIRO_TEST_SUCCESS
;
188 return cairo_test (&test
);