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 : return CAIRO_STATUS_SUCCESS;
135 : }
136 : }
137 :
138 : static cairo_status_t
139 0 : _cairo_path_bounder_close_path (void *closure)
140 : {
141 0 : return CAIRO_STATUS_SUCCESS;
142 : }
143 :
144 : /* This computes the extents of all the points in the path, not those of
145 : * the damage area (i.e it does not consider winding and it only inspects
146 : * the control points of the curves, not the flattened path).
147 : */
148 : void
149 0 : _cairo_path_fixed_approximate_clip_extents (const cairo_path_fixed_t *path,
150 : cairo_rectangle_int_t *extents)
151 : {
152 0 : if (path->extents.p1.x < path->extents.p2.x) {
153 0 : _cairo_box_round_to_rectangle (&path->extents, extents);
154 : } else {
155 0 : extents->x = extents->y = 0;
156 0 : extents->width = extents->height = 0;
157 : }
158 0 : }
159 :
160 : /* A slightly better approximation than above - we actually decompose the
161 : * Bezier, but we continue to ignore winding.
162 : */
163 : void
164 0 : _cairo_path_fixed_approximate_fill_extents (const cairo_path_fixed_t *path,
165 : cairo_rectangle_int_t *extents)
166 : {
167 : cairo_path_bounder_t bounder;
168 : cairo_status_t status;
169 :
170 0 : if (! path->has_curve_to) {
171 0 : bounder.extents = path->extents;
172 0 : bounder.has_point = path->extents.p1.x < path->extents.p2.x;
173 : } else {
174 0 : _cairo_path_bounder_init (&bounder);
175 :
176 0 : status = _cairo_path_fixed_interpret (path, CAIRO_DIRECTION_FORWARD,
177 : _cairo_path_bounder_move_to,
178 : _cairo_path_bounder_line_to,
179 : _cairo_path_bounder_curve_to,
180 : _cairo_path_bounder_close_path,
181 : &bounder);
182 0 : assert (status == CAIRO_STATUS_SUCCESS);
183 : }
184 :
185 0 : if (bounder.has_point) {
186 0 : _cairo_box_round_to_rectangle (&bounder.extents, extents);
187 : } else {
188 0 : extents->x = extents->y = 0;
189 0 : extents->width = extents->height = 0;
190 : }
191 0 : }
192 :
193 : void
194 0 : _cairo_path_fixed_fill_extents (const cairo_path_fixed_t *path,
195 : cairo_fill_rule_t fill_rule,
196 : double tolerance,
197 : cairo_rectangle_int_t *extents)
198 : {
199 : cairo_path_bounder_t bounder;
200 : cairo_status_t status;
201 :
202 0 : if (! path->has_curve_to) {
203 0 : bounder.extents = path->extents;
204 0 : bounder.has_point = path->extents.p1.x < path->extents.p2.x;
205 : } else {
206 0 : _cairo_path_bounder_init (&bounder);
207 :
208 0 : status = _cairo_path_fixed_interpret_flat (path, CAIRO_DIRECTION_FORWARD,
209 : _cairo_path_bounder_move_to,
210 : _cairo_path_bounder_line_to,
211 : _cairo_path_bounder_close_path,
212 : &bounder, tolerance);
213 0 : assert (status == CAIRO_STATUS_SUCCESS);
214 : }
215 :
216 0 : if (bounder.has_point) {
217 0 : _cairo_box_round_to_rectangle (&bounder.extents, extents);
218 : } else {
219 0 : extents->x = extents->y = 0;
220 0 : extents->width = extents->height = 0;
221 : }
222 0 : }
223 :
224 : /* Adjusts the fill extents (above) by the device-space pen. */
225 : void
226 0 : _cairo_path_fixed_approximate_stroke_extents (const cairo_path_fixed_t *path,
227 : const cairo_stroke_style_t *style,
228 : const cairo_matrix_t *ctm,
229 : cairo_rectangle_int_t *extents)
230 : {
231 : cairo_path_bounder_t bounder;
232 : cairo_status_t status;
233 :
234 0 : if (! path->has_curve_to) {
235 0 : bounder.extents = path->extents;
236 :
237 : /* include trailing move-to for degenerate segments */
238 0 : if (path->has_last_move_point) {
239 0 : const cairo_point_t *point = &path->last_move_point;
240 :
241 0 : if (point->x < bounder.extents.p1.x)
242 0 : bounder.extents.p1.x = point->x;
243 0 : if (point->y < bounder.extents.p1.y)
244 0 : bounder.extents.p1.y = point->y;
245 :
246 0 : if (point->x > bounder.extents.p2.x)
247 0 : bounder.extents.p2.x = point->x;
248 0 : if (point->y > bounder.extents.p2.y)
249 0 : bounder.extents.p2.y = point->y;
250 : }
251 :
252 0 : bounder.has_point = bounder.extents.p1.x <= bounder.extents.p2.x;
253 0 : bounder.has_initial_point = FALSE;
254 : } else {
255 0 : _cairo_path_bounder_init (&bounder);
256 :
257 0 : status = _cairo_path_fixed_interpret (path, CAIRO_DIRECTION_FORWARD,
258 : _cairo_path_bounder_move_to,
259 : _cairo_path_bounder_line_to,
260 : _cairo_path_bounder_curve_to,
261 : _cairo_path_bounder_close_path,
262 : &bounder);
263 0 : assert (status == CAIRO_STATUS_SUCCESS);
264 : }
265 :
266 0 : if (bounder.has_point) {
267 : double dx, dy;
268 :
269 0 : _cairo_stroke_style_max_distance_from_path (style, ctm, &dx, &dy);
270 :
271 0 : bounder.extents.p1.x -= _cairo_fixed_from_double (dx);
272 0 : bounder.extents.p2.x += _cairo_fixed_from_double (dx);
273 0 : bounder.extents.p1.y -= _cairo_fixed_from_double (dy);
274 0 : bounder.extents.p2.y += _cairo_fixed_from_double (dy);
275 :
276 0 : _cairo_box_round_to_rectangle (&bounder.extents, extents);
277 0 : } else if (bounder.has_initial_point) {
278 : double dx, dy;
279 :
280 : /* accommodate capping of degenerate paths */
281 :
282 0 : _cairo_stroke_style_max_distance_from_path (style, ctm, &dx, &dy);
283 :
284 0 : bounder.extents.p1.x = bounder.current_point.x - _cairo_fixed_from_double (dx);
285 0 : bounder.extents.p2.x = bounder.current_point.x + _cairo_fixed_from_double (dx);
286 0 : bounder.extents.p1.y = bounder.current_point.y - _cairo_fixed_from_double (dy);
287 0 : bounder.extents.p2.y = bounder.current_point.y + _cairo_fixed_from_double (dy);
288 :
289 0 : _cairo_box_round_to_rectangle (&bounder.extents, extents);
290 : } else {
291 0 : extents->x = extents->y = 0;
292 0 : extents->width = extents->height = 0;
293 : }
294 0 : }
295 :
296 : cairo_status_t
297 0 : _cairo_path_fixed_stroke_extents (const cairo_path_fixed_t *path,
298 : const cairo_stroke_style_t *stroke_style,
299 : const cairo_matrix_t *ctm,
300 : const cairo_matrix_t *ctm_inverse,
301 : double tolerance,
302 : cairo_rectangle_int_t *extents)
303 : {
304 : cairo_traps_t traps;
305 : cairo_box_t bbox;
306 : cairo_status_t status;
307 :
308 0 : _cairo_traps_init (&traps);
309 :
310 0 : status = _cairo_path_fixed_stroke_to_traps (path,
311 : stroke_style,
312 : ctm,
313 : ctm_inverse,
314 : tolerance,
315 : &traps);
316 :
317 0 : _cairo_traps_extents (&traps, &bbox);
318 0 : _cairo_traps_fini (&traps);
319 :
320 0 : _cairo_box_round_to_rectangle (&bbox, extents);
321 :
322 0 : return status;
323 : }
324 :
325 : cairo_bool_t
326 0 : _cairo_path_fixed_extents (const cairo_path_fixed_t *path,
327 : cairo_box_t *box)
328 : {
329 : cairo_path_bounder_t bounder;
330 : cairo_status_t status;
331 :
332 0 : if (! path->has_curve_to) {
333 0 : *box = path->extents;
334 0 : return path->extents.p1.x <= path->extents.p2.x;
335 : }
336 :
337 0 : _cairo_path_bounder_init (&bounder);
338 :
339 0 : status = _cairo_path_fixed_interpret (path, CAIRO_DIRECTION_FORWARD,
340 : _cairo_path_bounder_move_to,
341 : _cairo_path_bounder_line_to,
342 : _cairo_path_bounder_curve_to,
343 : _cairo_path_bounder_close_path,
344 : &bounder);
345 0 : assert (status == CAIRO_STATUS_SUCCESS);
346 :
347 0 : *box = bounder.extents;
348 0 : return bounder.has_point;
349 : }
|