Changes.
[cairo/gpu.git] / src / cairo-path-fill.c
blob9569c9e908cea4f474706274099377cc14207ec8
1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2002 University of Southern California
5 * This library is free software; you can redistribute it and/or
6 * modify it either under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation
8 * (the "LGPL") or, at your option, under the terms of the Mozilla
9 * Public License Version 1.1 (the "MPL"). If you do not alter this
10 * notice, a recipient may use your version of this file under either
11 * the MPL or the LGPL.
13 * You should have received a copy of the LGPL along with this library
14 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * You should have received a copy of the MPL along with this library
17 * in the file COPYING-MPL-1.1
19 * The contents of this file are subject to the Mozilla Public License
20 * Version 1.1 (the "License"); you may not use this file except in
21 * compliance with the License. You may obtain a copy of the License at
22 * http://www.mozilla.org/MPL/
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26 * the specific language governing rights and limitations.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is University of Southern
31 * California.
33 * Contributor(s):
34 * Carl D. Worth <cworth@cworth.org>
37 #include "cairoint.h"
38 #include "cairo-path-fixed-private.h"
40 typedef struct cairo_filler {
41 double tolerance;
42 cairo_traps_t *traps;
44 cairo_point_t current_point;
46 cairo_polygon_t polygon;
47 } cairo_filler_t;
49 static void
50 _cairo_filler_init (cairo_filler_t *filler, double tolerance, cairo_traps_t *traps)
52 filler->tolerance = tolerance;
53 filler->traps = traps;
55 filler->current_point.x = 0;
56 filler->current_point.y = 0;
58 _cairo_polygon_init (&filler->polygon);
61 static void
62 _cairo_filler_fini (cairo_filler_t *filler)
64 _cairo_polygon_fini (&filler->polygon);
67 static cairo_status_t
68 _cairo_filler_move_to (void *closure,
69 const cairo_point_t *point)
71 cairo_filler_t *filler = closure;
72 cairo_polygon_t *polygon = &filler->polygon;
74 _cairo_polygon_close (polygon);
75 _cairo_polygon_move_to (polygon, point);
77 filler->current_point = *point;
79 return _cairo_polygon_status (&filler->polygon);
82 static cairo_status_t
83 _cairo_filler_line_to (void *closure,
84 const cairo_point_t *point)
86 cairo_filler_t *filler = closure;
87 cairo_polygon_t *polygon = &filler->polygon;
89 _cairo_polygon_line_to (polygon, point);
91 filler->current_point = *point;
93 return _cairo_polygon_status (&filler->polygon);
96 static cairo_status_t
97 _cairo_filler_curve_to (void *closure,
98 const cairo_point_t *b,
99 const cairo_point_t *c,
100 const cairo_point_t *d)
102 cairo_filler_t *filler = closure;
103 cairo_spline_t spline;
105 if (! _cairo_spline_init (&spline,
106 _cairo_filler_line_to,
107 filler,
108 &filler->current_point, b, c, d))
110 return CAIRO_STATUS_SUCCESS;
113 return _cairo_spline_decompose (&spline, filler->tolerance);
116 static cairo_status_t
117 _cairo_filler_close_path (void *closure)
119 cairo_filler_t *filler = closure;
120 cairo_polygon_t *polygon = &filler->polygon;
122 _cairo_polygon_close (polygon);
124 return _cairo_polygon_status (polygon);
127 static cairo_int_status_t
128 _cairo_path_fixed_fill_rectangle (cairo_path_fixed_t *path,
129 cairo_fill_rule_t fill_rule,
130 cairo_traps_t *traps);
132 cairo_status_t
133 _cairo_path_fixed_fill_to_traps (cairo_path_fixed_t *path,
134 cairo_fill_rule_t fill_rule,
135 double tolerance,
136 cairo_traps_t *traps)
138 cairo_status_t status = CAIRO_STATUS_SUCCESS;
139 cairo_filler_t filler;
141 /* Before we do anything else, we use a special-case filler for
142 * a device-axis aligned rectangle if possible. */
143 status = _cairo_path_fixed_fill_rectangle (path, fill_rule, traps);
144 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
145 return status;
147 _cairo_filler_init (&filler, tolerance, traps);
149 status = _cairo_path_fixed_interpret (path,
150 CAIRO_DIRECTION_FORWARD,
151 _cairo_filler_move_to,
152 _cairo_filler_line_to,
153 _cairo_filler_curve_to,
154 _cairo_filler_close_path,
155 &filler);
156 if (unlikely (status))
157 goto BAIL;
159 _cairo_polygon_close (&filler.polygon);
160 status = _cairo_polygon_status (&filler.polygon);
161 if (unlikely (status))
162 goto BAIL;
164 status = _cairo_bentley_ottmann_tessellate_polygon (filler.traps,
165 &filler.polygon,
166 fill_rule);
167 if (unlikely (status))
168 goto BAIL;
170 BAIL:
171 _cairo_filler_fini (&filler);
173 return status;
176 /* This special-case filler supports only a path that describes a
177 * device-axis aligned rectangle. It exists to avoid the overhead of
178 * the general tessellator when drawing very common rectangles.
180 * If the path described anything but a device-axis aligned rectangle,
181 * this function will return %CAIRO_INT_STATUS_UNSUPPORTED.
183 static cairo_int_status_t
184 _cairo_path_fixed_fill_rectangle (cairo_path_fixed_t *path,
185 cairo_fill_rule_t fill_rule,
186 cairo_traps_t *traps)
188 cairo_box_t box;
190 if (_cairo_path_fixed_is_box (path, &box)) {
191 if (box.p1.x > box.p2.x) {
192 cairo_fixed_t t;
194 t = box.p1.x;
195 box.p1.x = box.p2.x;
196 box.p2.x = t;
199 if (box.p1.y > box.p2.y) {
200 cairo_fixed_t t;
202 t = box.p1.y;
203 box.p1.y = box.p2.y;
204 box.p2.y = t;
207 return _cairo_traps_tessellate_rectangle (traps, &box.p1, &box.p2);
208 } else if (fill_rule == CAIRO_FILL_RULE_WINDING) {
209 cairo_path_fixed_iter_t iter;
210 int last_cw = -1;
212 /* Support a series of rectangles as can be expected to describe a
213 * GdkRegion clip region during exposes.
215 _cairo_path_fixed_iter_init (&iter, path);
216 while (_cairo_path_fixed_iter_is_fill_box (&iter, &box)) {
217 cairo_status_t status;
218 int cw = 0;
220 if (box.p1.x > box.p2.x) {
221 cairo_fixed_t t;
223 t = box.p1.x;
224 box.p1.x = box.p2.x;
225 box.p2.x = t;
227 cw = ! cw;
230 if (box.p1.y > box.p2.y) {
231 cairo_fixed_t t;
233 t = box.p1.y;
234 box.p1.y = box.p2.y;
235 box.p2.y = t;
237 cw = ! cw;
240 if (last_cw < 0) {
241 last_cw = cw;
242 } else if (last_cw != cw) {
243 _cairo_traps_clear (traps);
244 return CAIRO_INT_STATUS_UNSUPPORTED;
247 status = _cairo_traps_tessellate_rectangle (traps,
248 &box.p1, &box.p2);
249 if (unlikely (status))
250 return status;
252 if (_cairo_path_fixed_iter_at_end (&iter))
253 return CAIRO_STATUS_SUCCESS;
255 _cairo_traps_clear (traps);
258 return CAIRO_INT_STATUS_UNSUPPORTED;