[check] Filter programlistings for check-doc-syntax.sh
[cairo/haiku.git] / test / line-width-scale.c
blobeada6c6b0e35b401a925ad720e2e6770dddec971
1 /*
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
31 * to the path itself.
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.
48 #define LINE_WIDTH 13
49 #define SPLINE 50.0
50 #define XSCALE 0.5
51 #define YSCALE 2.0
52 #define WIDTH (XSCALE * SPLINE * 6.0)
53 #define HEIGHT (YSCALE * SPLINE * 2.0)
55 static cairo_test_draw_function_t draw;
57 cairo_test_t test = {
58 "line-width-scale",
59 "Tests interaction of cairo_set_line_width with cairo_scale",
60 WIDTH, HEIGHT,
61 draw
64 static void
65 spline_path (cairo_t *cr)
67 cairo_save (cr);
69 cairo_move_to (cr,
70 - SPLINE, 0);
71 cairo_curve_to (cr,
72 - SPLINE / 4, - SPLINE,
73 SPLINE / 4, SPLINE,
74 SPLINE, 0);
76 cairo_restore (cr);
79 /* If we scale before setting the line width or creating the path,
80 * then obviously both will be scaled. */
81 static void
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);
86 spline_path (cr);
87 cairo_stroke (cr);
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.
96 static void
97 scale_path_and_line_width (cairo_t *cr)
99 cairo_save (cr);
101 cairo_scale (cr, XSCALE, YSCALE);
102 spline_path (cr);
104 cairo_restore (cr);
106 cairo_save (cr);
108 cairo_scale (cr, XSCALE, YSCALE);
109 cairo_set_line_width (cr, LINE_WIDTH);
110 cairo_stroke (cr);
112 cairo_restore (cr);
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
120 * path here.
122 static void
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);
127 spline_path (cr);
128 cairo_stroke (cr);
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
135 * is not.
137 static void
138 scale_path_not_line_width (cairo_t *cr)
140 cairo_save (cr);
142 cairo_scale (cr, XSCALE, YSCALE);
143 spline_path (cr);
145 cairo_restore (cr);
147 cairo_save (cr);
149 cairo_set_line_width (cr, LINE_WIDTH);
150 cairo_stroke (cr);
152 cairo_restore (cr);
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)
160 int i;
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 */
170 cairo_paint (cr);
171 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
173 for (i = 0; i < 4; i++) {
174 cairo_save (cr);
175 cairo_translate (cr,
176 WIDTH/4 + (i % 2) * WIDTH/2,
177 HEIGHT/4 + (i / 2) * HEIGHT/2);
178 (figures[i]) (cr);
179 cairo_restore (cr);
182 return CAIRO_TEST_SUCCESS;
186 main (void)
188 return cairo_test (&test);