/work/workdir/UnpackedTarball/cairo/src/cairo-path.c
Line | Count | Source |
1 | | /* cairo - a vector graphics library with display and print output |
2 | | * |
3 | | * Copyright © 2005 Red Hat, Inc. |
4 | | * Copyright © 2006 Red Hat, Inc. |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it either under the terms of the GNU Lesser General Public |
8 | | * License version 2.1 as published by the Free Software Foundation |
9 | | * (the "LGPL") or, at your option, under the terms of the Mozilla |
10 | | * Public License Version 1.1 (the "MPL"). If you do not alter this |
11 | | * notice, a recipient may use your version of this file under either |
12 | | * the MPL or the LGPL. |
13 | | * |
14 | | * You should have received a copy of the LGPL along with this library |
15 | | * in the file COPYING-LGPL-2.1; if not, write to the Free Software |
16 | | * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA |
17 | | * You should have received a copy of the MPL along with this library |
18 | | * in the file COPYING-MPL-1.1 |
19 | | * |
20 | | * The contents of this file are subject to the Mozilla Public License |
21 | | * Version 1.1 (the "License"); you may not use this file except in |
22 | | * compliance with the License. You may obtain a copy of the License at |
23 | | * http://www.mozilla.org/MPL/ |
24 | | * |
25 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY |
26 | | * OF ANY KIND, either express or implied. See the LGPL or the MPL for |
27 | | * the specific language governing rights and limitations. |
28 | | * |
29 | | * The Original Code is the cairo graphics library. |
30 | | * |
31 | | * The Initial Developer of the Original Code is Red Hat, Inc. |
32 | | * |
33 | | * Contributor(s): |
34 | | * Carl D. Worth <cworth@redhat.com> |
35 | | */ |
36 | | |
37 | | #include "cairoint.h" |
38 | | |
39 | | #include "cairo-private.h" |
40 | | #include "cairo-backend-private.h" |
41 | | #include "cairo-error-private.h" |
42 | | #include "cairo-path-private.h" |
43 | | #include "cairo-path-fixed-private.h" |
44 | | |
45 | | /** |
46 | | * SECTION:cairo-paths |
47 | | * @Title: Paths |
48 | | * @Short_Description: Creating paths and manipulating path data |
49 | | * |
50 | | * Paths are the most basic drawing tools and are primarily used to implicitly |
51 | | * generate simple masks. |
52 | | **/ |
53 | | |
54 | | static const cairo_path_t _cairo_path_nil = { CAIRO_STATUS_NO_MEMORY, NULL, 0 }; |
55 | | |
56 | | /* Closure for path interpretation. */ |
57 | | typedef struct cairo_path_count { |
58 | | int count; |
59 | | } cpc_t; |
60 | | |
61 | | static cairo_status_t |
62 | | _cpc_move_to (void *closure, |
63 | | const cairo_point_t *point) |
64 | 1.10k | { |
65 | 1.10k | cpc_t *cpc = closure; |
66 | | |
67 | 1.10k | cpc->count += 2; |
68 | | |
69 | 1.10k | return CAIRO_STATUS_SUCCESS; |
70 | 1.10k | } |
71 | | |
72 | | static cairo_status_t |
73 | | _cpc_line_to (void *closure, |
74 | | const cairo_point_t *point) |
75 | 1.70k | { |
76 | 1.70k | cpc_t *cpc = closure; |
77 | | |
78 | 1.70k | cpc->count += 2; |
79 | | |
80 | 1.70k | return CAIRO_STATUS_SUCCESS; |
81 | 1.70k | } |
82 | | |
83 | | static cairo_status_t |
84 | | _cpc_curve_to (void *closure, |
85 | | const cairo_point_t *p1, |
86 | | const cairo_point_t *p2, |
87 | | const cairo_point_t *p3) |
88 | 342 | { |
89 | 342 | cpc_t *cpc = closure; |
90 | | |
91 | 342 | cpc->count += 4; |
92 | | |
93 | 342 | return CAIRO_STATUS_SUCCESS; |
94 | 342 | } |
95 | | |
96 | | static cairo_status_t |
97 | | _cpc_close_path (void *closure) |
98 | 551 | { |
99 | 551 | cpc_t *cpc = closure; |
100 | | |
101 | 551 | cpc->count += 1; |
102 | | |
103 | 551 | return CAIRO_STATUS_SUCCESS; |
104 | 551 | } |
105 | | |
106 | | static int |
107 | | _cairo_path_count (cairo_path_t *path, |
108 | | cairo_path_fixed_t *path_fixed, |
109 | | double tolerance, |
110 | | cairo_bool_t flatten) |
111 | 551 | { |
112 | 551 | cairo_status_t status; |
113 | 551 | cpc_t cpc; |
114 | | |
115 | 551 | cpc.count = 0; |
116 | | |
117 | 551 | if (flatten) { |
118 | 0 | status = _cairo_path_fixed_interpret_flat (path_fixed, |
119 | 0 | _cpc_move_to, |
120 | 0 | _cpc_line_to, |
121 | 0 | _cpc_close_path, |
122 | 0 | &cpc, |
123 | 0 | tolerance); |
124 | 551 | } else { |
125 | 551 | status = _cairo_path_fixed_interpret (path_fixed, |
126 | 551 | _cpc_move_to, |
127 | 551 | _cpc_line_to, |
128 | 551 | _cpc_curve_to, |
129 | 551 | _cpc_close_path, |
130 | 551 | &cpc); |
131 | 551 | } |
132 | | |
133 | 551 | if (unlikely (status)) |
134 | 0 | return -1; |
135 | | |
136 | 551 | return cpc.count; |
137 | 551 | } |
138 | | |
139 | | /* Closure for path interpretation. */ |
140 | | typedef struct cairo_path_populate { |
141 | | cairo_path_data_t *data; |
142 | | cairo_t *cr; |
143 | | } cpp_t; |
144 | | |
145 | | static cairo_status_t |
146 | | _cpp_move_to (void *closure, |
147 | | const cairo_point_t *point) |
148 | 1.10k | { |
149 | 1.10k | cpp_t *cpp = closure; |
150 | 1.10k | cairo_path_data_t *data = cpp->data; |
151 | 1.10k | double x, y; |
152 | | |
153 | 1.10k | x = _cairo_fixed_to_double (point->x); |
154 | 1.10k | y = _cairo_fixed_to_double (point->y); |
155 | | |
156 | 1.10k | _cairo_backend_to_user (cpp->cr, &x, &y); |
157 | | |
158 | 1.10k | data->header.type = CAIRO_PATH_MOVE_TO; |
159 | 1.10k | data->header.length = 2; |
160 | | |
161 | | /* We index from 1 to leave room for data->header */ |
162 | 1.10k | data[1].point.x = x; |
163 | 1.10k | data[1].point.y = y; |
164 | | |
165 | 1.10k | cpp->data += data->header.length; |
166 | | |
167 | 1.10k | return CAIRO_STATUS_SUCCESS; |
168 | 1.10k | } |
169 | | |
170 | | static cairo_status_t |
171 | | _cpp_line_to (void *closure, |
172 | | const cairo_point_t *point) |
173 | 1.70k | { |
174 | 1.70k | cpp_t *cpp = closure; |
175 | 1.70k | cairo_path_data_t *data = cpp->data; |
176 | 1.70k | double x, y; |
177 | | |
178 | 1.70k | x = _cairo_fixed_to_double (point->x); |
179 | 1.70k | y = _cairo_fixed_to_double (point->y); |
180 | | |
181 | 1.70k | _cairo_backend_to_user (cpp->cr, &x, &y); |
182 | | |
183 | 1.70k | data->header.type = CAIRO_PATH_LINE_TO; |
184 | 1.70k | data->header.length = 2; |
185 | | |
186 | | /* We index from 1 to leave room for data->header */ |
187 | 1.70k | data[1].point.x = x; |
188 | 1.70k | data[1].point.y = y; |
189 | | |
190 | 1.70k | cpp->data += data->header.length; |
191 | | |
192 | 1.70k | return CAIRO_STATUS_SUCCESS; |
193 | 1.70k | } |
194 | | |
195 | | static cairo_status_t |
196 | | _cpp_curve_to (void *closure, |
197 | | const cairo_point_t *p1, |
198 | | const cairo_point_t *p2, |
199 | | const cairo_point_t *p3) |
200 | 342 | { |
201 | 342 | cpp_t *cpp = closure; |
202 | 342 | cairo_path_data_t *data = cpp->data; |
203 | 342 | double x1, y1; |
204 | 342 | double x2, y2; |
205 | 342 | double x3, y3; |
206 | | |
207 | 342 | x1 = _cairo_fixed_to_double (p1->x); |
208 | 342 | y1 = _cairo_fixed_to_double (p1->y); |
209 | 342 | _cairo_backend_to_user (cpp->cr, &x1, &y1); |
210 | | |
211 | 342 | x2 = _cairo_fixed_to_double (p2->x); |
212 | 342 | y2 = _cairo_fixed_to_double (p2->y); |
213 | 342 | _cairo_backend_to_user (cpp->cr, &x2, &y2); |
214 | | |
215 | 342 | x3 = _cairo_fixed_to_double (p3->x); |
216 | 342 | y3 = _cairo_fixed_to_double (p3->y); |
217 | 342 | _cairo_backend_to_user (cpp->cr, &x3, &y3); |
218 | | |
219 | 342 | data->header.type = CAIRO_PATH_CURVE_TO; |
220 | 342 | data->header.length = 4; |
221 | | |
222 | | /* We index from 1 to leave room for data->header */ |
223 | 342 | data[1].point.x = x1; |
224 | 342 | data[1].point.y = y1; |
225 | | |
226 | 342 | data[2].point.x = x2; |
227 | 342 | data[2].point.y = y2; |
228 | | |
229 | 342 | data[3].point.x = x3; |
230 | 342 | data[3].point.y = y3; |
231 | | |
232 | 342 | cpp->data += data->header.length; |
233 | | |
234 | 342 | return CAIRO_STATUS_SUCCESS; |
235 | 342 | } |
236 | | |
237 | | static cairo_status_t |
238 | | _cpp_close_path (void *closure) |
239 | 551 | { |
240 | 551 | cpp_t *cpp = closure; |
241 | 551 | cairo_path_data_t *data = cpp->data; |
242 | | |
243 | 551 | data->header.type = CAIRO_PATH_CLOSE_PATH; |
244 | 551 | data->header.length = 1; |
245 | | |
246 | 551 | cpp->data += data->header.length; |
247 | | |
248 | 551 | return CAIRO_STATUS_SUCCESS; |
249 | 551 | } |
250 | | |
251 | | static cairo_status_t |
252 | | _cairo_path_populate (cairo_path_t *path, |
253 | | cairo_path_fixed_t *path_fixed, |
254 | | cairo_t *cr, |
255 | | cairo_bool_t flatten) |
256 | 551 | { |
257 | 551 | cairo_status_t status; |
258 | 551 | cpp_t cpp; |
259 | | |
260 | 551 | cpp.data = path->data; |
261 | 551 | cpp.cr = cr; |
262 | | |
263 | 551 | if (flatten) { |
264 | 0 | status = _cairo_path_fixed_interpret_flat (path_fixed, |
265 | 0 | _cpp_move_to, |
266 | 0 | _cpp_line_to, |
267 | 0 | _cpp_close_path, |
268 | 0 | &cpp, |
269 | 0 | cairo_get_tolerance (cr)); |
270 | 551 | } else { |
271 | 551 | status = _cairo_path_fixed_interpret (path_fixed, |
272 | 551 | _cpp_move_to, |
273 | 551 | _cpp_line_to, |
274 | 551 | _cpp_curve_to, |
275 | 551 | _cpp_close_path, |
276 | 551 | &cpp); |
277 | 551 | } |
278 | | |
279 | 551 | if (unlikely (status)) |
280 | 0 | return status; |
281 | | |
282 | | /* Sanity check the count */ |
283 | 551 | assert (cpp.data - path->data == path->num_data); |
284 | | |
285 | 551 | return CAIRO_STATUS_SUCCESS; |
286 | 551 | } |
287 | | |
288 | | cairo_path_t * |
289 | | _cairo_path_create_in_error (cairo_status_t status) |
290 | 0 | { |
291 | 0 | cairo_path_t *path; |
292 | | |
293 | | /* special case NO_MEMORY so as to avoid allocations */ |
294 | 0 | if (status == CAIRO_STATUS_NO_MEMORY) |
295 | 0 | return (cairo_path_t*) &_cairo_path_nil; |
296 | | |
297 | 0 | path = _cairo_calloc (sizeof (cairo_path_t)); |
298 | 0 | if (unlikely (path == NULL)) { |
299 | 0 | _cairo_error_throw (CAIRO_STATUS_NO_MEMORY); |
300 | 0 | return (cairo_path_t*) &_cairo_path_nil; |
301 | 0 | } |
302 | | |
303 | 0 | path->num_data = 0; |
304 | 0 | path->data = NULL; |
305 | 0 | path->status = status; |
306 | |
|
307 | 0 | return path; |
308 | 0 | } |
309 | | |
310 | | static cairo_path_t * |
311 | | _cairo_path_create_internal (cairo_path_fixed_t *path_fixed, |
312 | | cairo_t *cr, |
313 | | cairo_bool_t flatten) |
314 | 551 | { |
315 | 551 | cairo_path_t *path; |
316 | | |
317 | 551 | path = _cairo_calloc (sizeof (cairo_path_t)); |
318 | 551 | if (unlikely (path == NULL)) { |
319 | 0 | _cairo_error_throw (CAIRO_STATUS_NO_MEMORY); |
320 | 0 | return (cairo_path_t*) &_cairo_path_nil; |
321 | 0 | } |
322 | | |
323 | 551 | path->num_data = _cairo_path_count (path, path_fixed, |
324 | 551 | cairo_get_tolerance (cr), |
325 | 551 | flatten); |
326 | 551 | if (path->num_data < 0) { |
327 | 0 | free (path); |
328 | 0 | return (cairo_path_t*) &_cairo_path_nil; |
329 | 0 | } |
330 | | |
331 | 551 | if (path->num_data) { |
332 | 551 | path->data = _cairo_malloc_ab (path->num_data, |
333 | 551 | sizeof (cairo_path_data_t)); |
334 | 551 | if (unlikely (path->data == NULL)) { |
335 | 0 | free (path); |
336 | 0 | _cairo_error_throw (CAIRO_STATUS_NO_MEMORY); |
337 | 0 | return (cairo_path_t*) &_cairo_path_nil; |
338 | 0 | } |
339 | | |
340 | 551 | path->status = _cairo_path_populate (path, path_fixed, cr, flatten); |
341 | 551 | } else { |
342 | 0 | path->data = NULL; |
343 | 0 | path->status = CAIRO_STATUS_SUCCESS; |
344 | 0 | } |
345 | | |
346 | 551 | return path; |
347 | 551 | } |
348 | | |
349 | | /** |
350 | | * cairo_path_destroy: |
351 | | * @path: a path previously returned by either cairo_copy_path() or |
352 | | * cairo_copy_path_flat(). |
353 | | * |
354 | | * Immediately releases all memory associated with @path. After a call |
355 | | * to cairo_path_destroy() the @path pointer is no longer valid and |
356 | | * should not be used further. |
357 | | * |
358 | | * Note: cairo_path_destroy() should only be called with a |
359 | | * pointer to a #cairo_path_t returned by a cairo function. Any path |
360 | | * that is created manually (ie. outside of cairo) should be destroyed |
361 | | * manually as well. |
362 | | * |
363 | | * Since: 1.0 |
364 | | **/ |
365 | | void |
366 | | cairo_path_destroy (cairo_path_t *path) |
367 | 551 | { |
368 | 551 | if (path == NULL || path == &_cairo_path_nil) |
369 | 0 | return; |
370 | | |
371 | 551 | free (path->data); |
372 | | |
373 | 551 | free (path); |
374 | 551 | } |
375 | | |
376 | | /** |
377 | | * _cairo_path_create: |
378 | | * @path: a fixed-point, device-space path to be converted and copied |
379 | | * @cr: the current graphics context |
380 | | * |
381 | | * Creates a user-space #cairo_path_t copy of the given device-space |
382 | | * @path. The @cr parameter provides the inverse CTM for the |
383 | | * conversion. |
384 | | * |
385 | | * Return value: the new copy of the path. If there is insufficient |
386 | | * memory a pointer to a special static nil #cairo_path_t will be |
387 | | * returned instead with status==%CAIRO_STATUS_NO_MEMORY and |
388 | | * data==%NULL. |
389 | | **/ |
390 | | cairo_path_t * |
391 | | _cairo_path_create (cairo_path_fixed_t *path, |
392 | | cairo_t *cr) |
393 | 551 | { |
394 | 551 | return _cairo_path_create_internal (path, cr, FALSE); |
395 | 551 | } |
396 | | |
397 | | /** |
398 | | * _cairo_path_create_flat: |
399 | | * @path: a fixed-point, device-space path to be flattened, converted and copied |
400 | | * @cr: the current graphics context |
401 | | * |
402 | | * Creates a flattened, user-space #cairo_path_t copy of the given |
403 | | * device-space @path. The @cr parameter provide the inverse CTM |
404 | | * for the conversion, as well as the tolerance value to control the |
405 | | * accuracy of the flattening. |
406 | | * |
407 | | * Return value: the flattened copy of the path. If there is insufficient |
408 | | * memory a pointer to a special static nil #cairo_path_t will be |
409 | | * returned instead with status==%CAIRO_STATUS_NO_MEMORY and |
410 | | * data==%NULL. |
411 | | **/ |
412 | | cairo_path_t * |
413 | | _cairo_path_create_flat (cairo_path_fixed_t *path, |
414 | | cairo_t *cr) |
415 | 0 | { |
416 | 0 | return _cairo_path_create_internal (path, cr, TRUE); |
417 | 0 | } |
418 | | |
419 | | /** |
420 | | * _cairo_path_append_to_context: |
421 | | * @path: the path data to be appended |
422 | | * @cr: a cairo context |
423 | | * |
424 | | * Append @path to the current path within @cr. |
425 | | * |
426 | | * Return value: %CAIRO_STATUS_INVALID_PATH_DATA if the data in @path |
427 | | * is invalid, and %CAIRO_STATUS_SUCCESS otherwise. |
428 | | **/ |
429 | | cairo_status_t |
430 | | _cairo_path_append_to_context (const cairo_path_t *path, |
431 | | cairo_t *cr) |
432 | 551 | { |
433 | 551 | const cairo_path_data_t *p, *end; |
434 | | |
435 | 551 | end = &path->data[path->num_data]; |
436 | 4.24k | for (p = &path->data[0]; p < end; p += p->header.length) { |
437 | 3.69k | switch (p->header.type) { |
438 | 1.10k | case CAIRO_PATH_MOVE_TO: |
439 | 1.10k | if (unlikely (p->header.length < 2)) |
440 | 0 | return _cairo_error (CAIRO_STATUS_INVALID_PATH_DATA); |
441 | | |
442 | 1.10k | cairo_move_to (cr, p[1].point.x, p[1].point.y); |
443 | 1.10k | break; |
444 | | |
445 | 1.70k | case CAIRO_PATH_LINE_TO: |
446 | 1.70k | if (unlikely (p->header.length < 2)) |
447 | 0 | return _cairo_error (CAIRO_STATUS_INVALID_PATH_DATA); |
448 | | |
449 | 1.70k | cairo_line_to (cr, p[1].point.x, p[1].point.y); |
450 | 1.70k | break; |
451 | | |
452 | 342 | case CAIRO_PATH_CURVE_TO: |
453 | 342 | if (unlikely (p->header.length < 4)) |
454 | 0 | return _cairo_error (CAIRO_STATUS_INVALID_PATH_DATA); |
455 | | |
456 | 342 | cairo_curve_to (cr, |
457 | 342 | p[1].point.x, p[1].point.y, |
458 | 342 | p[2].point.x, p[2].point.y, |
459 | 342 | p[3].point.x, p[3].point.y); |
460 | 342 | break; |
461 | | |
462 | 551 | case CAIRO_PATH_CLOSE_PATH: |
463 | 551 | if (unlikely (p->header.length < 1)) |
464 | 0 | return _cairo_error (CAIRO_STATUS_INVALID_PATH_DATA); |
465 | | |
466 | 551 | cairo_close_path (cr); |
467 | 551 | break; |
468 | | |
469 | 0 | default: |
470 | 0 | return _cairo_error (CAIRO_STATUS_INVALID_PATH_DATA); |
471 | 3.69k | } |
472 | | |
473 | 3.69k | if (unlikely (cr->status)) |
474 | 0 | return cr->status; |
475 | 3.69k | } |
476 | | |
477 | 551 | return CAIRO_STATUS_SUCCESS; |
478 | 551 | } |