Line data Source code
1 : /* cairo - a vector graphics library with display and print output
2 : *
3 : * Copyright © 2003 University of Southern California
4 : *
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.
12 : *
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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16 : * You should have received a copy of the MPL along with this library
17 : * in the file COPYING-MPL-1.1
18 : *
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/
23 : *
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.
27 : *
28 : * The Original Code is the cairo graphics library.
29 : *
30 : * The Initial Developer of the Original Code is University of Southern
31 : * California.
32 : *
33 : * Contributor(s):
34 : * Carl D. Worth <cworth@cworth.org>
35 : */
36 :
37 : #include "cairoint.h"
38 : #include "cairo-path-fixed-private.h"
39 :
40 : typedef struct cairo_path_bounder {
41 : cairo_point_t current_point;
42 : cairo_bool_t has_initial_point;
43 : cairo_bool_t has_point;
44 :
45 : cairo_box_t extents;
46 : } cairo_path_bounder_t;
47 :
48 : static void
49 0 : _cairo_path_bounder_init (cairo_path_bounder_t *bounder)
50 : {
51 0 : bounder->has_initial_point = FALSE;
52 0 : bounder->has_point = FALSE;
53 0 : }
54 :
55 : static void
56 0 : _cairo_path_bounder_add_point (cairo_path_bounder_t *bounder,
57 : const cairo_point_t *point)
58 : {
59 0 : if (bounder->has_point) {
60 0 : if (point->x < bounder->extents.p1.x)
61 0 : bounder->extents.p1.x = point->x;
62 :
63 0 : if (point->y < bounder->extents.p1.y)
64 0 : bounder->extents.p1.y = point->y;
65 :
66 0 : if (point->x > bounder->extents.p2.x)
67 0 : bounder->extents.p2.x = point->x;
68 :
69 0 : if (point->y > bounder->extents.p2.y)
70 0 : bounder->extents.p2.y = point->y;
71 : } else {
72 0 : bounder->extents.p1.x = point->x;
73 0 : bounder->extents.p1.y = point->y;
74 0 : bounder->extents.p2.x = point->x;
75 0 : bounder->extents.p2.y = point->y;
76 0 : bounder->has_point = TRUE;
77 : }
78 0 : }
79 :
80 : static cairo_status_t
81 0 : _cairo_path_bounder_move_to (void *closure,
82 : const cairo_point_t *point)
83 : {
84 0 : cairo_path_bounder_t *bounder = closure;
85 :
86 0 : bounder->current_point = *point;
87 0 : bounder->has_initial_point = TRUE;
88 :
89 0 : return CAIRO_STATUS_SUCCESS;
90 : }
91 :
92 : static cairo_status_t
93 0 : _cairo_path_bounder_line_to (void *closure,
94 : const cairo_point_t *point)
95 : {
96 0 : cairo_path_bounder_t *bounder = closure;
97 :
98 0 : if (bounder->has_initial_point) {
99 0 : _cairo_path_bounder_add_point (bounder, &bounder->current_point);
100 0 : bounder->has_initial_point = FALSE;
101 : }
102 :
103 0 : _cairo_path_bounder_add_point (bounder, point);
104 0 : bounder->current_point = *point;
105 :
106 0 : return CAIRO_STATUS_SUCCESS;
107 : }
108 :
109 : static cairo_status_t
110 0 : _cairo_path_bounder_curve_to (void *closure,
111 : const cairo_point_t *b,
112 : const cairo_point_t *c,
113 : const cairo_point_t *d)
114 : {
115 0 : cairo_path_bounder_t *bounder = closure;
116 :
117 : /* If the bbox of the control points is entirely inside, then we
118 : * do not need to further evaluate the spline.
119 : */
120 0 : if (! bounder->has_point ||
121 0 : b->x < bounder->extents.p1.x || b->x > bounder->extents.p2.x ||
122 0 : b->y < bounder->extents.p1.y || b->y > bounder->extents.p2.y ||
123 0 : c->x < bounder->extents.p1.x || c->x > bounder->extents.p2.x ||
124 0 : c->y < bounder->extents.p1.y || c->y > bounder->extents.p2.y ||
125 0 : d->x < bounder->extents.p1.x || d->x > bounder->extents.p2.x ||
126 0 : d->y < bounder->extents.p1.y || d->y > bounder->extents.p2.y)
127 : {
128 0 : return _cairo_spline_bound (_cairo_path_bounder_line_to, bounder,
129 0 : &bounder->current_point, b, c, d);
130 : }
131 : else
132 : {
133 : /* All control points are within the current extents. */
134 0 : bounder->current_point = *d;
135 0 : return CAIRO_STATUS_SUCCESS;
136 : }
137 : }
138 :
139 : static cairo_status_t
140 0 : _cairo_path_bounder_close_path (void *closure)
141 : {
142 0 : return CAIRO_STATUS_SUCCESS;
143 : }
144 :
145 : /* This computes the extents of all the points in the path, not those of
146 : * the damage area (i.e it does not consider winding and it only inspects
147 : * the control points of the curves, not the flattened path).
148 : */
149 : void
150 0 : _cairo_path_fixed_approximate_clip_extents (const cairo_path_fixed_t *path,
151 : cairo_rectangle_int_t *extents)
152 : {
153 0 : if (path->extents.p1.x < path->extents.p2.x) {
154 0 : _cairo_box_round_to_rectangle (&path->extents, extents);
155 : } else {
156 0 : extents->x = extents->y = 0;
157 0 : extents->width = extents->height = 0;
158 : }
159 0 : }
160 :
161 : /* A slightly better approximation than above - we actually decompose the
162 : * Bezier, but we continue to ignore winding.
163 : */
164 : void
165 0 : _cairo_path_fixed_approximate_fill_extents (const cairo_path_fixed_t *path,
166 : cairo_rectangle_int_t *extents)
167 : {
168 : cairo_path_bounder_t bounder;
169 : cairo_status_t status;
170 :
171 0 : if (! path->has_curve_to) {
172 0 : bounder.extents = path->extents;
173 0 : bounder.has_point = path->extents.p1.x < path->extents.p2.x;
174 : } else {
175 0 : _cairo_path_bounder_init (&bounder);
176 :
177 0 : status = _cairo_path_fixed_interpret (path, CAIRO_DIRECTION_FORWARD,
178 : _cairo_path_bounder_move_to,
179 : _cairo_path_bounder_line_to,
180 : _cairo_path_bounder_curve_to,
181 : _cairo_path_bounder_close_path,
182 : &bounder);
183 0 : assert (status == CAIRO_STATUS_SUCCESS);
184 : }
185 :
186 0 : if (bounder.has_point) {
187 0 : _cairo_box_round_to_rectangle (&bounder.extents, extents);
188 : } else {
189 0 : extents->x = extents->y = 0;
190 0 : extents->width = extents->height = 0;
191 : }
192 0 : }
193 :
194 : void
195 0 : _cairo_path_fixed_fill_extents (const cairo_path_fixed_t *path,
196 : cairo_fill_rule_t fill_rule,
197 : double tolerance,
198 : cairo_rectangle_int_t *extents)
199 : {
200 : cairo_path_bounder_t bounder;
201 : cairo_status_t status;
202 :
203 0 : if (! path->has_curve_to) {
204 0 : bounder.extents = path->extents;
205 0 : bounder.has_point = path->extents.p1.x < path->extents.p2.x;
206 : } else {
207 0 : _cairo_path_bounder_init (&bounder);
208 :
209 0 : status = _cairo_path_fixed_interpret_flat (path, CAIRO_DIRECTION_FORWARD,
210 : _cairo_path_bounder_move_to,
211 : _cairo_path_bounder_line_to,
212 : _cairo_path_bounder_close_path,
213 : &bounder, tolerance);
214 0 : assert (status == CAIRO_STATUS_SUCCESS);
215 : }
216 :
217 0 : if (bounder.has_point) {
218 0 : _cairo_box_round_to_rectangle (&bounder.extents, extents);
219 : } else {
220 0 : extents->x = extents->y = 0;
221 0 : extents->width = extents->height = 0;
222 : }
223 0 : }
224 :
225 : /* Adjusts the fill extents (above) by the device-space pen. */
226 : void
227 0 : _cairo_path_fixed_approximate_stroke_extents (const cairo_path_fixed_t *path,
228 : const cairo_stroke_style_t *style,
229 : const cairo_matrix_t *ctm,
230 : cairo_rectangle_int_t *extents)
231 : {
232 : cairo_path_bounder_t bounder;
233 : cairo_status_t status;
234 :
235 0 : if (! path->has_curve_to) {
236 0 : bounder.extents = path->extents;
237 :
238 : /* include trailing move-to for degenerate segments */
239 0 : if (path->has_last_move_point) {
240 0 : const cairo_point_t *point = &path->last_move_point;
241 :
242 0 : if (point->x < bounder.extents.p1.x)
243 0 : bounder.extents.p1.x = point->x;
244 0 : if (point->y < bounder.extents.p1.y)
245 0 : bounder.extents.p1.y = point->y;
246 :
247 0 : if (point->x > bounder.extents.p2.x)
248 0 : bounder.extents.p2.x = point->x;
249 0 : if (point->y > bounder.extents.p2.y)
250 0 : bounder.extents.p2.y = point->y;
251 : }
252 :
253 0 : bounder.has_point = bounder.extents.p1.x <= bounder.extents.p2.x;
254 0 : bounder.has_initial_point = FALSE;
255 : } else {
256 0 : _cairo_path_bounder_init (&bounder);
257 :
258 0 : status = _cairo_path_fixed_interpret (path, CAIRO_DIRECTION_FORWARD,
259 : _cairo_path_bounder_move_to,
260 : _cairo_path_bounder_line_to,
261 : _cairo_path_bounder_curve_to,
262 : _cairo_path_bounder_close_path,
263 : &bounder);
264 0 : assert (status == CAIRO_STATUS_SUCCESS);
265 : }
266 :
267 0 : if (bounder.has_point) {
268 : double dx, dy;
269 :
270 0 : _cairo_stroke_style_max_distance_from_path (style, ctm, &dx, &dy);
271 :
272 0 : bounder.extents.p1.x -= _cairo_fixed_from_double (dx);
273 0 : bounder.extents.p2.x += _cairo_fixed_from_double (dx);
274 0 : bounder.extents.p1.y -= _cairo_fixed_from_double (dy);
275 0 : bounder.extents.p2.y += _cairo_fixed_from_double (dy);
276 :
277 0 : _cairo_box_round_to_rectangle (&bounder.extents, extents);
278 0 : } else if (bounder.has_initial_point) {
279 : double dx, dy;
280 :
281 : /* accommodate capping of degenerate paths */
282 :
283 0 : _cairo_stroke_style_max_distance_from_path (style, ctm, &dx, &dy);
284 :
285 0 : bounder.extents.p1.x = bounder.current_point.x - _cairo_fixed_from_double (dx);
286 0 : bounder.extents.p2.x = bounder.current_point.x + _cairo_fixed_from_double (dx);
287 0 : bounder.extents.p1.y = bounder.current_point.y - _cairo_fixed_from_double (dy);
288 0 : bounder.extents.p2.y = bounder.current_point.y + _cairo_fixed_from_double (dy);
289 :
290 0 : _cairo_box_round_to_rectangle (&bounder.extents, extents);
291 : } else {
292 0 : extents->x = extents->y = 0;
293 0 : extents->width = extents->height = 0;
294 : }
295 0 : }
296 :
297 : cairo_status_t
298 0 : _cairo_path_fixed_stroke_extents (const cairo_path_fixed_t *path,
299 : const cairo_stroke_style_t *stroke_style,
300 : const cairo_matrix_t *ctm,
301 : const cairo_matrix_t *ctm_inverse,
302 : double tolerance,
303 : cairo_rectangle_int_t *extents)
304 : {
305 : cairo_traps_t traps;
306 : cairo_box_t bbox;
307 : cairo_status_t status;
308 :
309 0 : _cairo_traps_init (&traps);
310 :
311 0 : status = _cairo_path_fixed_stroke_to_traps (path,
312 : stroke_style,
313 : ctm,
314 : ctm_inverse,
315 : tolerance,
316 : &traps);
317 :
318 0 : _cairo_traps_extents (&traps, &bbox);
319 0 : _cairo_traps_fini (&traps);
320 :
321 0 : _cairo_box_round_to_rectangle (&bbox, extents);
322 :
323 0 : return status;
324 : }
325 :
326 : cairo_bool_t
327 0 : _cairo_path_fixed_extents (const cairo_path_fixed_t *path,
328 : cairo_box_t *box)
329 : {
330 : cairo_path_bounder_t bounder;
331 : cairo_status_t status;
332 :
333 0 : if (! path->has_curve_to) {
334 0 : *box = path->extents;
335 0 : return path->extents.p1.x <= path->extents.p2.x;
336 : }
337 :
338 0 : _cairo_path_bounder_init (&bounder);
339 :
340 0 : status = _cairo_path_fixed_interpret (path, CAIRO_DIRECTION_FORWARD,
341 : _cairo_path_bounder_move_to,
342 : _cairo_path_bounder_line_to,
343 : _cairo_path_bounder_curve_to,
344 : _cairo_path_bounder_close_path,
345 : &bounder);
346 0 : assert (status == CAIRO_STATUS_SUCCESS);
347 :
348 0 : *box = bounder.extents;
349 0 : return bounder.has_point;
350 : }
|