Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1892 | serge | 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 |
||
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 | _cairo_path_bounder_init (cairo_path_bounder_t *bounder) |
||
50 | { |
||
51 | bounder->has_initial_point = FALSE; |
||
52 | bounder->has_point = FALSE; |
||
53 | } |
||
54 | |||
55 | static void |
||
56 | _cairo_path_bounder_add_point (cairo_path_bounder_t *bounder, |
||
57 | const cairo_point_t *point) |
||
58 | { |
||
59 | if (bounder->has_point) { |
||
60 | if (point->x < bounder->extents.p1.x) |
||
61 | bounder->extents.p1.x = point->x; |
||
62 | |||
63 | if (point->y < bounder->extents.p1.y) |
||
64 | bounder->extents.p1.y = point->y; |
||
65 | |||
66 | if (point->x > bounder->extents.p2.x) |
||
67 | bounder->extents.p2.x = point->x; |
||
68 | |||
69 | if (point->y > bounder->extents.p2.y) |
||
70 | bounder->extents.p2.y = point->y; |
||
71 | } else { |
||
72 | bounder->extents.p1.x = point->x; |
||
73 | bounder->extents.p1.y = point->y; |
||
74 | bounder->extents.p2.x = point->x; |
||
75 | bounder->extents.p2.y = point->y; |
||
76 | bounder->has_point = TRUE; |
||
77 | } |
||
78 | } |
||
79 | |||
80 | static cairo_status_t |
||
81 | _cairo_path_bounder_move_to (void *closure, |
||
82 | const cairo_point_t *point) |
||
83 | { |
||
84 | cairo_path_bounder_t *bounder = closure; |
||
85 | |||
86 | bounder->current_point = *point; |
||
87 | bounder->has_initial_point = TRUE; |
||
88 | |||
89 | return CAIRO_STATUS_SUCCESS; |
||
90 | } |
||
91 | |||
92 | static cairo_status_t |
||
93 | _cairo_path_bounder_line_to (void *closure, |
||
94 | const cairo_point_t *point) |
||
95 | { |
||
96 | cairo_path_bounder_t *bounder = closure; |
||
97 | |||
98 | if (bounder->has_initial_point) { |
||
99 | _cairo_path_bounder_add_point (bounder, &bounder->current_point); |
||
100 | bounder->has_initial_point = FALSE; |
||
101 | } |
||
102 | |||
103 | _cairo_path_bounder_add_point (bounder, point); |
||
104 | bounder->current_point = *point; |
||
105 | |||
106 | return CAIRO_STATUS_SUCCESS; |
||
107 | } |
||
108 | |||
109 | static cairo_status_t |
||
110 | _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 | 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 | if (! bounder->has_point || |
||
121 | b->x < bounder->extents.p1.x || b->x > bounder->extents.p2.x || |
||
122 | b->y < bounder->extents.p1.y || b->y > bounder->extents.p2.y || |
||
123 | c->x < bounder->extents.p1.x || c->x > bounder->extents.p2.x || |
||
124 | c->y < bounder->extents.p1.y || c->y > bounder->extents.p2.y || |
||
125 | d->x < bounder->extents.p1.x || d->x > bounder->extents.p2.x || |
||
126 | d->y < bounder->extents.p1.y || d->y > bounder->extents.p2.y) |
||
127 | { |
||
128 | return _cairo_spline_bound (_cairo_path_bounder_line_to, bounder, |
||
129 | &bounder->current_point, b, c, d); |
||
130 | } |
||
131 | else |
||
132 | { |
||
133 | /* All control points are within the current extents. */ |
||
134 | bounder->current_point = *d; |
||
135 | |||
136 | return CAIRO_STATUS_SUCCESS; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | static cairo_status_t |
||
141 | _cairo_path_bounder_close_path (void *closure) |
||
142 | { |
||
143 | return CAIRO_STATUS_SUCCESS; |
||
144 | } |
||
145 | |||
146 | /* This computes the extents of all the points in the path, not those of |
||
147 | * the damage area (i.e it does not consider winding and it only inspects |
||
148 | * the control points of the curves, not the flattened path). |
||
149 | */ |
||
150 | void |
||
151 | _cairo_path_fixed_approximate_clip_extents (const cairo_path_fixed_t *path, |
||
152 | cairo_rectangle_int_t *extents) |
||
153 | { |
||
154 | if (path->extents.p1.x < path->extents.p2.x) { |
||
155 | _cairo_box_round_to_rectangle (&path->extents, extents); |
||
156 | } else { |
||
157 | extents->x = extents->y = 0; |
||
158 | extents->width = extents->height = 0; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | /* A slightly better approximation than above - we actually decompose the |
||
163 | * Bezier, but we continue to ignore winding. |
||
164 | */ |
||
165 | void |
||
166 | _cairo_path_fixed_approximate_fill_extents (const cairo_path_fixed_t *path, |
||
167 | cairo_rectangle_int_t *extents) |
||
168 | { |
||
169 | cairo_path_bounder_t bounder; |
||
170 | cairo_status_t status; |
||
171 | |||
172 | if (! path->has_curve_to) { |
||
173 | bounder.extents = path->extents; |
||
174 | bounder.has_point = path->extents.p1.x < path->extents.p2.x; |
||
175 | } else { |
||
176 | _cairo_path_bounder_init (&bounder); |
||
177 | |||
178 | status = _cairo_path_fixed_interpret (path, CAIRO_DIRECTION_FORWARD, |
||
179 | _cairo_path_bounder_move_to, |
||
180 | _cairo_path_bounder_line_to, |
||
181 | _cairo_path_bounder_curve_to, |
||
182 | _cairo_path_bounder_close_path, |
||
183 | &bounder); |
||
184 | assert (status == CAIRO_STATUS_SUCCESS); |
||
185 | } |
||
186 | |||
187 | if (bounder.has_point) { |
||
188 | _cairo_box_round_to_rectangle (&bounder.extents, extents); |
||
189 | } else { |
||
190 | extents->x = extents->y = 0; |
||
191 | extents->width = extents->height = 0; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | void |
||
196 | _cairo_path_fixed_fill_extents (const cairo_path_fixed_t *path, |
||
197 | cairo_fill_rule_t fill_rule, |
||
198 | double tolerance, |
||
199 | cairo_rectangle_int_t *extents) |
||
200 | { |
||
201 | cairo_path_bounder_t bounder; |
||
202 | cairo_status_t status; |
||
203 | |||
204 | if (! path->has_curve_to) { |
||
205 | bounder.extents = path->extents; |
||
206 | bounder.has_point = path->extents.p1.x < path->extents.p2.x; |
||
207 | } else { |
||
208 | _cairo_path_bounder_init (&bounder); |
||
209 | |||
210 | status = _cairo_path_fixed_interpret_flat (path, CAIRO_DIRECTION_FORWARD, |
||
211 | _cairo_path_bounder_move_to, |
||
212 | _cairo_path_bounder_line_to, |
||
213 | _cairo_path_bounder_close_path, |
||
214 | &bounder, tolerance); |
||
215 | assert (status == CAIRO_STATUS_SUCCESS); |
||
216 | } |
||
217 | |||
218 | if (bounder.has_point) { |
||
219 | _cairo_box_round_to_rectangle (&bounder.extents, extents); |
||
220 | } else { |
||
221 | extents->x = extents->y = 0; |
||
222 | extents->width = extents->height = 0; |
||
223 | } |
||
224 | } |
||
225 | |||
226 | /* Adjusts the fill extents (above) by the device-space pen. */ |
||
227 | void |
||
228 | _cairo_path_fixed_approximate_stroke_extents (const cairo_path_fixed_t *path, |
||
229 | const cairo_stroke_style_t *style, |
||
230 | const cairo_matrix_t *ctm, |
||
231 | cairo_rectangle_int_t *extents) |
||
232 | { |
||
233 | cairo_path_bounder_t bounder; |
||
234 | cairo_status_t status; |
||
235 | |||
236 | if (! path->has_curve_to) { |
||
237 | bounder.extents = path->extents; |
||
238 | |||
239 | /* include trailing move-to for degenerate segments */ |
||
240 | if (path->has_last_move_point) { |
||
241 | const cairo_point_t *point = &path->last_move_point; |
||
242 | |||
243 | if (point->x < bounder.extents.p1.x) |
||
244 | bounder.extents.p1.x = point->x; |
||
245 | if (point->y < bounder.extents.p1.y) |
||
246 | bounder.extents.p1.y = point->y; |
||
247 | |||
248 | if (point->x > bounder.extents.p2.x) |
||
249 | bounder.extents.p2.x = point->x; |
||
250 | if (point->y > bounder.extents.p2.y) |
||
251 | bounder.extents.p2.y = point->y; |
||
252 | } |
||
253 | |||
254 | bounder.has_point = bounder.extents.p1.x <= bounder.extents.p2.x; |
||
255 | bounder.has_initial_point = FALSE; |
||
256 | } else { |
||
257 | _cairo_path_bounder_init (&bounder); |
||
258 | |||
259 | status = _cairo_path_fixed_interpret (path, CAIRO_DIRECTION_FORWARD, |
||
260 | _cairo_path_bounder_move_to, |
||
261 | _cairo_path_bounder_line_to, |
||
262 | _cairo_path_bounder_curve_to, |
||
263 | _cairo_path_bounder_close_path, |
||
264 | &bounder); |
||
265 | assert (status == CAIRO_STATUS_SUCCESS); |
||
266 | } |
||
267 | |||
268 | if (bounder.has_point) { |
||
269 | double dx, dy; |
||
270 | |||
271 | _cairo_stroke_style_max_distance_from_path (style, ctm, &dx, &dy); |
||
272 | |||
273 | bounder.extents.p1.x -= _cairo_fixed_from_double (dx); |
||
274 | bounder.extents.p2.x += _cairo_fixed_from_double (dx); |
||
275 | bounder.extents.p1.y -= _cairo_fixed_from_double (dy); |
||
276 | bounder.extents.p2.y += _cairo_fixed_from_double (dy); |
||
277 | |||
278 | _cairo_box_round_to_rectangle (&bounder.extents, extents); |
||
279 | } else if (bounder.has_initial_point) { |
||
280 | double dx, dy; |
||
281 | |||
282 | /* accommodate capping of degenerate paths */ |
||
283 | |||
284 | _cairo_stroke_style_max_distance_from_path (style, ctm, &dx, &dy); |
||
285 | |||
286 | bounder.extents.p1.x = bounder.current_point.x - _cairo_fixed_from_double (dx); |
||
287 | bounder.extents.p2.x = bounder.current_point.x + _cairo_fixed_from_double (dx); |
||
288 | bounder.extents.p1.y = bounder.current_point.y - _cairo_fixed_from_double (dy); |
||
289 | bounder.extents.p2.y = bounder.current_point.y + _cairo_fixed_from_double (dy); |
||
290 | |||
291 | _cairo_box_round_to_rectangle (&bounder.extents, extents); |
||
292 | } else { |
||
293 | extents->x = extents->y = 0; |
||
294 | extents->width = extents->height = 0; |
||
295 | } |
||
296 | } |
||
297 | |||
298 | cairo_status_t |
||
299 | _cairo_path_fixed_stroke_extents (const cairo_path_fixed_t *path, |
||
300 | const cairo_stroke_style_t *stroke_style, |
||
301 | const cairo_matrix_t *ctm, |
||
302 | const cairo_matrix_t *ctm_inverse, |
||
303 | double tolerance, |
||
304 | cairo_rectangle_int_t *extents) |
||
305 | { |
||
306 | cairo_traps_t traps; |
||
307 | cairo_box_t bbox; |
||
308 | cairo_status_t status; |
||
309 | |||
310 | _cairo_traps_init (&traps); |
||
311 | |||
312 | status = _cairo_path_fixed_stroke_to_traps (path, |
||
313 | stroke_style, |
||
314 | ctm, |
||
315 | ctm_inverse, |
||
316 | tolerance, |
||
317 | &traps); |
||
318 | |||
319 | _cairo_traps_extents (&traps, &bbox); |
||
320 | _cairo_traps_fini (&traps); |
||
321 | |||
322 | _cairo_box_round_to_rectangle (&bbox, extents); |
||
323 | |||
324 | return status; |
||
325 | } |
||
326 | |||
327 | cairo_bool_t |
||
328 | _cairo_path_fixed_extents (const cairo_path_fixed_t *path, |
||
329 | cairo_box_t *box) |
||
330 | { |
||
331 | cairo_path_bounder_t bounder; |
||
332 | cairo_status_t status; |
||
333 | |||
334 | if (! path->has_curve_to) { |
||
335 | *box = path->extents; |
||
336 | /* empty extents should still have an origin and should not |
||
337 | * be {0, 0, 0, 0} */ |
||
338 | return path->extents.p1.x <= path->extents.p2.x; |
||
339 | } |
||
340 | |||
341 | _cairo_path_bounder_init (&bounder); |
||
342 | |||
343 | status = _cairo_path_fixed_interpret (path, CAIRO_DIRECTION_FORWARD, |
||
344 | _cairo_path_bounder_move_to, |
||
345 | _cairo_path_bounder_line_to, |
||
346 | _cairo_path_bounder_curve_to, |
||
347 | _cairo_path_bounder_close_path, |
||
348 | &bounder); |
||
349 | assert (status == CAIRO_STATUS_SUCCESS); |
||
350 | |||
351 | *box = bounder.extents; |
||
352 | return bounder.has_point; |
||
353 | }=>=>>>>>>>>>>>>>> |