cpml: protect cpml_segment_to_cairo from NULL segment
[adg.git] / src / cpml / cpml-segment.c
blob434d715800b8f40868381bdbca399ddc1ca6ea68
1 /* CPML - Cairo Path Manipulation Library
2 * Copyright (C) 2007-2015 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:cpml-segment
23 * @Section_Id:Segment
24 * @title: CpmlSegment
25 * @short_description: Contiguous segment that can be a fragment
26 * or a whole cairo path
28 * A segment is a single contiguous line got from a cairo path. The
29 * CPML library relies on one assumption to let the data be independent
30 * from the current point (and thus from the cairo context): any segment
31 * MUST be preceded by at least one %CPML_MOVE primitive.
32 * This means a valid segment in cairo could be rejected by CPML.
34 * #CpmlSegment provides an unobtrusive way to access a cairo path.
35 * This means #CpmlSegment itsself does not hold any coordinates but
36 * instead a bunch of pointers to the original #cairo_path_t struct:
37 * modifying data throught this struct also changes the original path.
39 * Every #cairo_path_t struct can contain more than one segment: the CPML
40 * library provides iteration APIs to browse the segments of a path.
41 * Use cpml_segment_reset() to reset the iterator at the start of the
42 * cairo path (will point the first segment) and cpml_segment_next()
43 * to get the next segment. Getting the previous segment is not provided
44 * as the underlying cairo struct is not accessible in reverse order.
46 * When initialized, #CpmlSegment yet refers to the first segment so
47 * the initial reset is not required.
49 * Since: 1.0
50 **/
52 /**
53 * CpmlSegment:
54 * @path: the source #cairo_path_t struct
55 * @data: the data points of the segment; the first primitive
56 * will always be a %CPML_MOVE
57 * @num_data: size of @data
59 * This is an unobtrusive struct to identify a segment inside a
60 * cairo path. Unobtrusive means that the real coordinates are
61 * still stored in @path: CpmlSegment only provides a way to
62 * access them.
64 * Since: 1.0
65 **/
68 #include "cpml-internal.h"
69 #include "cpml-extents.h"
70 #include "cpml-segment.h"
71 #include "cpml-primitive.h"
72 #include "cpml-curve.h"
73 #include <string.h>
75 static int normalize (CpmlSegment *segment);
76 static int ensure_one_leading_move (CpmlSegment *segment);
77 static int reshape (CpmlSegment *segment);
80 /**
81 * cpml_segment_from_cairo:
82 * @segment: a #CpmlSegment
83 * @path: (type gpointer): the source #cairo_path_t
85 * Builds a CpmlSegment from a #cairo_path_t structure. This operation
86 * involves stripping the duplicate %CPML_MOVE primitives at the
87 * start of the path and setting <structfield>num_data</structfield>
88 * field to the end of the contiguous line, that is when another
89 * %CPML_MOVE primitive is found or at the end of the path.
90 * A pointer to the source cairo path is kept though.
92 * This function will fail if @path is null, empty or if its
93 * <structfield>status</structfield> member is not %CAIRO_STATUS_SUCCESS.
94 * Also, the first primitive must be a %CPML_MOVE, so no
95 * dependency on the cairo context is needed.
97 * Returns: (type gboolean): 1 if @segment has been succesfully computed, 0 on errors.
99 * Since: 1.0
102 cpml_segment_from_cairo(CpmlSegment *segment, cairo_path_t *path)
104 /* segment and path must be non-NULL and path must be valid */
105 if (path == NULL || segment == NULL ||
106 path->num_data == 0 || path->status != CAIRO_STATUS_SUCCESS)
107 return 0;
109 segment->path = path;
110 segment->data = path->data;
111 segment->num_data = path->num_data;
113 return normalize(segment);
117 * cpml_segment_copy:
118 * @segment: a #CpmlSegment
119 * @src: the source segment to copy
121 * Makes a shallow copy of @src into @segment.
123 * Since: 1.0
125 void
126 cpml_segment_copy(CpmlSegment *segment, const CpmlSegment *src)
128 memcpy(segment, src, sizeof(CpmlSegment));
132 * cpml_segment_reset:
133 * @segment: a #CpmlSegment
135 * Modifies @segment to point to the first segment of the source cairo path.
137 * Since: 1.0
139 void
140 cpml_segment_reset(CpmlSegment *segment)
142 segment->data = segment->path->data;
143 segment->num_data = segment->path->num_data;
144 normalize(segment);
148 * cpml_segment_next:
149 * @segment: a #CpmlSegment
151 * Modifies @segment to point to the next segment of the source cairo path.
153 * Returns: (type gboolean): 1 on success, 0 if no next segment found or errors.
155 * Since: 1.0
158 cpml_segment_next(CpmlSegment *segment)
160 cairo_path_data_t *new_data;
161 const cairo_path_data_t *end_data;
163 new_data = segment->data + segment->num_data;
164 end_data = segment->path->data + segment->path->num_data;
166 if (new_data >= end_data)
167 return 0;
169 segment->data = new_data;
170 segment->num_data = end_data - new_data;
172 return normalize(segment);
176 * cpml_segment_get_length:
177 * @segment: a #CpmlSegment
179 * Gets the whole length of @segment.
181 * Returns: the requested length
183 * Since: 1.0
185 double
186 cpml_segment_get_length(const CpmlSegment *segment)
188 CpmlPrimitive primitive;
189 double length;
191 if (segment == NULL)
192 return 0;
194 cpml_primitive_from_segment(&primitive, (CpmlSegment *) segment);
195 length = 0;
197 do {
198 length += cpml_primitive_get_length(&primitive);
199 } while (cpml_primitive_next(&primitive));
201 return length;
205 * cpml_segment_put_extents:
206 * @segment: a #CpmlSegment
207 * @extents: where to store the extents
209 * Gets the whole extents of @segment.
211 * Since: 1.0
213 void
214 cpml_segment_put_extents(const CpmlSegment *segment, CpmlExtents *extents)
216 CpmlPrimitive primitive;
217 CpmlExtents primitive_extents;
219 extents->is_defined = 0;
221 cpml_primitive_from_segment(&primitive, (CpmlSegment *) segment);
223 do {
224 cpml_primitive_put_extents(&primitive, &primitive_extents);
225 cpml_extents_add(extents, &primitive_extents);
226 } while (cpml_primitive_next(&primitive));
230 * cpml_segment_put_pair_at:
231 * @segment: a #CpmlSegment
232 * @pos: the position value
233 * @pair: the destination #CpmlPair
235 * Gets the coordinates of the point lying on @segment at position
236 * @pos. @pos is an homogeneous factor where 0 is the start point,
237 * 1 the end point, 0.5 the mid point and so on.
238 * The relation <constant>0 < @pos < 1</constant> should be satisfied,
239 * although some cases accept value outside this range.
241 * <important>
242 * <title>TODO</title>
243 * <itemizedlist>
244 * <listitem>The actual implementation returns only the start and end points,
245 * that is only when @pos is 0 or 1.</listitem>
246 * </itemizedlist>
247 * </important>
249 * Since: 1.0
251 void
252 cpml_segment_put_pair_at(const CpmlSegment *segment, double pos,
253 CpmlPair *pair)
255 CpmlPrimitive primitive;
257 cpml_primitive_from_segment(&primitive, (CpmlSegment *) segment);
259 /* Handle the common cases: start and end points */
260 if (pos == 0) {
261 cpml_primitive_put_pair_at(&primitive, 0, pair);
262 } if (pos == 1) {
263 while (cpml_primitive_next(&primitive))
265 cpml_primitive_put_pair_at(&primitive, 1, pair);
270 * cpml_segment_put_vector_at:
271 * @segment: a #CpmlSegment
272 * @pos: the position value
273 * @vector: the destination #CpmlVector
275 * Gets the steepness of the point lying on @segment at position
276 * @pos. @pos is an homogeneous factor where 0 is the start point,
277 * 1 the end point, 0.5 the mid point and so on.
278 * The relation <constant>0 < @pos < 1</constant> should be satisfied,
279 * although some cases accept value outside this range.
281 * <important>
282 * <title>TODO</title>
283 * <itemizedlist>
284 * <listitem>The actual implementation returns only the start and end
285 * steepness, that is only when @pos is 0 or 1.</listitem>
286 * </itemizedlist>
287 * </important>
289 * Since: 1.0
291 void
292 cpml_segment_put_vector_at(const CpmlSegment *segment, double pos,
293 CpmlVector *vector)
295 CpmlPrimitive primitive;
297 cpml_primitive_from_segment(&primitive, (CpmlSegment *) segment);
299 /* Handle the common cases: start and end points */
300 if (pos == 0) {
301 cpml_primitive_put_vector_at(&primitive, 0, vector);
302 return;
305 if (pos == 1) {
306 while (cpml_primitive_next(&primitive))
308 cpml_primitive_put_vector_at(&primitive, 1, vector);
309 return;
314 * cpml_segment_put_intersections:
315 * @segment: the first #CpmlSegment
316 * @segment2: the second #CpmlSegment
317 * @n_dest: maximum number of intersections to return
318 * @dest: the destination vector of #CpmlPair
320 * Computes the intersections between @segment and @segment2 and
321 * returns the found points in @dest. If the intersections are more
322 * than @n_dest, only the first @n_dest pairs are stored in @dest.
324 * To get the job done, the primitives of @segment are sequentially
325 * scanned for intersections with any primitive in @segment2. This
326 * means @segment has a higher precedence over @segment2.
328 * Returns: the number of intersections found
330 * Since: 1.0
332 size_t
333 cpml_segment_put_intersections(const CpmlSegment *segment,
334 const CpmlSegment *segment2,
335 size_t n_dest, CpmlPair *dest)
337 CpmlPrimitive portion;
338 size_t partial, total;
340 cpml_primitive_from_segment(&portion, (CpmlSegment *) segment);
341 total = 0;
343 do {
344 partial = cpml_primitive_put_intersections_with_segment(&portion,
345 segment2,
346 n_dest - total,
347 dest + total);
348 total += partial;
349 } while (total < n_dest && cpml_primitive_next(&portion));
351 return total;
355 * cpml_segment_offset:
356 * @segment: a #CpmlSegment
357 * @offset: the offset distance
359 * Offsets a segment of the specified amount, that is builds a "parallel"
360 * segment at the @offset distance from the original one and returns the
361 * result by replacing the original @segment.
363 * <important>
364 * <title>TODO</title>
365 * <itemizedlist>
366 * <listitem>Closed path are not yet managed: an elegant solution is not
367 * so obvious: use <function>cpml_close_offset</function> when
368 * will be available.</listitem>
369 * <listitem>Degenerated primitives, such as lines of length 0, are not
370 * managed properly.</listitem>
371 * </itemizedlist>
372 * </important>
374 * Since: 1.0
376 void
377 cpml_segment_offset(CpmlSegment *segment, double offset)
379 CpmlPrimitive primitive;
380 CpmlPrimitive last_primitive;
381 CpmlPair old_end;
382 cairo_path_data_t org, *old_org;
383 int first_cycle;
385 cpml_primitive_from_segment(&primitive, segment);
386 first_cycle = 1;
388 do {
389 if (! first_cycle) {
390 cpml_pair_to_cairo(&old_end, &org);
391 old_org = primitive.org;
392 primitive.org = &org;
395 cpml_primitive_put_point(&primitive, -1, &old_end);
396 cpml_primitive_offset(&primitive, offset);
398 if (! first_cycle) {
399 cpml_primitive_join(&last_primitive, &primitive);
400 primitive.org = old_org;
403 cpml_primitive_copy(&last_primitive, &primitive);
404 first_cycle = 0;
405 } while (cpml_primitive_next(&primitive));
409 * cpml_segment_transform:
410 * @segment: a #CpmlSegment
411 * @matrix: the matrix to be applied
413 * Applies @matrix on all the points of @segment.
415 * Since: 1.0
417 void
418 cpml_segment_transform(CpmlSegment *segment, const cairo_matrix_t *matrix)
420 CpmlPrimitive primitive;
421 cairo_path_data_t *data;
422 size_t n_points;
424 cpml_primitive_from_segment(&primitive, segment);
425 cairo_matrix_transform_point(matrix, &(primitive.org)->point.x,
426 &(primitive.org)->point.y);
428 do {
429 data = primitive.data;
430 if (data->header.type != CAIRO_PATH_CLOSE_PATH) {
431 n_points = cpml_primitive_get_n_points(&primitive);
433 while (--n_points > 0) {
434 ++data;
435 cairo_matrix_transform_point(matrix,
436 &data->point.x, &data->point.y);
439 } while (cpml_primitive_next(&primitive));
443 * cpml_segment_reverse:
444 * @segment: a #CpmlSegment
446 * Reverses @segment in-place. The resulting rendering will be the same,
447 * but with the primitives generated in reverse order.
449 * It is assumed that @segment has yet been sanitized, that is returned
450 * by some CPML API function or it is a path yet conforming to the
451 * segment rules described by the cpml_segment_from_cairo() function.
453 * Since: 1.0
455 void
456 cpml_segment_reverse(CpmlSegment *segment)
458 cairo_path_data_t *data, *dst_data;
459 size_t data_size;
460 double end_x, end_y;
461 int n, length;
462 size_t n_points, n_point;
463 const cairo_path_data_t *src_data;
465 data_size = sizeof(cairo_path_data_t) * segment->num_data;
466 data = malloc(data_size);
467 end_x = segment->data[1].point.x;
468 end_y = segment->data[1].point.y;
470 n = segment->data->header.length;
471 data->header.type = CAIRO_PATH_CLOSE_PATH;
472 data->header.length = n;
474 while (n < segment->num_data) {
475 src_data = segment->data + n;
476 n_points = cpml_primitive_type_get_n_points(src_data->header.type);
477 length = src_data->header.length;
478 n += length;
479 dst_data = data + segment->num_data - n + data->header.length;
480 dst_data->header.type = src_data->header.type;
481 dst_data->header.length = length;
483 for (n_point = 1; n_point < n_points; ++n_point) {
484 dst_data[n_points - n_point].point.x = end_x;
485 dst_data[n_points - n_point].point.y = end_y;
486 end_x = src_data[n_point].point.x;
487 end_y = src_data[n_point].point.y;
490 /* Copy also the embedded data, if any */
491 if (n_points < length) {
492 size_t size = (length - n_points) * sizeof(cairo_path_data_t);
493 memcpy(dst_data + n_points, src_data + n_points, size);
497 data[1].point.x = end_x;
498 data[1].point.y = end_y;
499 memcpy(segment->data, data, data_size);
501 free(data);
505 * cpml_segment_to_cairo:
506 * @segment: a #CpmlSegment
507 * @cr: the destination cairo context
509 * Appends the path of @segment to @cr. The segment is "flattened",
510 * that is %CPML_ARC primitives are approximated by one or more
511 * %CPML_CURVE using cpml_arc_to_cairo(). Check its documentation
512 * for further details.
514 * Since: 1.0
516 void
517 cpml_segment_to_cairo(const CpmlSegment *segment, cairo_t *cr)
519 CpmlPrimitive primitive;
521 if (segment == NULL || cr == NULL)
522 return;
524 cpml_primitive_from_segment(&primitive, (CpmlSegment *) segment);
526 do {
527 cpml_primitive_to_cairo(&primitive, cr);
528 } while (cpml_primitive_next(&primitive));
532 * cpml_segment_dump:
533 * @segment: a #CpmlSegment
535 * Dumps the specified @segment to stdout. Useful for debugging purposes.
537 * Since: 1.0
539 void
540 cpml_segment_dump(const CpmlSegment *segment)
542 CpmlPrimitive primitive;
543 int first_call = 1;
545 cpml_primitive_from_segment(&primitive, (CpmlSegment *) segment);
547 do {
548 cpml_primitive_dump(&primitive, first_call);
549 first_call = 0;
550 } while (cpml_primitive_next(&primitive));
555 * normalize:
556 * @segment: a #CpmlSegment
558 * Sanitizes @segment by calling ensure_one_leading_move() and reshape().
560 * Returns: 1 on success, 0 on no leading MOVE_TOs or on errors.
562 static int
563 normalize(CpmlSegment *segment)
565 if (!ensure_one_leading_move(segment))
566 return 0;
568 return reshape(segment);
572 * ensure_one_leading_move:
573 * @segment: a #CpmlSegment
575 * Strips the leading %CPML_MOVE primitives, updating the
576 * <structname>CpmlSegment</structname> structure accordingly.
577 * One, and only one, %CPML_MOVE primitive is left.
579 * Returns: 1 on success, 0 on no leading MOVE_TOs or on empty path
581 static int
582 ensure_one_leading_move(CpmlSegment *segment)
584 cairo_path_data_t *new_data;
585 int new_num_data, move_length;
587 /* Check for at least one move to */
588 if (segment->data->header.type != CAIRO_PATH_MOVE_TO)
589 return 0;
591 new_data = segment->data;
592 new_num_data = segment->num_data;
594 while (1) {
595 move_length = new_data->header.length;
597 /* Check for the end of cairo path data, that is when
598 * @segment is composed by only CAIRO_PATH_MOVE_TO */
599 if (new_num_data <= move_length)
600 return 0;
602 /* Check if this is the last CAIRO_PATH_MOVE_TO */
603 if (new_data[move_length].header.type != CAIRO_PATH_MOVE_TO)
604 break;
606 new_data += move_length;
607 new_num_data -= move_length;
610 segment->data = new_data;
611 segment->num_data = new_num_data;
613 return 1;
617 * reshape:
618 * @segment: a #CpmlSegment
620 * Looks for the segment termination, that is the end of the underlying
621 * cairo path or a %CPML_MOVE operation. <structfield>num_data</structfield>
622 * field is modified to properly point to the end of @segment.
623 * @segment must have only one leading %CPML_MOVE and it is supposed
624 * to be non-empty, conditions yet imposed by the ensure_one_leading_move().
626 * This function also checks that all the components of @segment
627 * are valid primitives.
629 * Returns: 1 on success, 0 on invalid primitive found.
631 static int
632 reshape(CpmlSegment *segment)
634 const cairo_path_data_t *data;
635 int trailing_data, new_num_data, length;
637 data = segment->data;
638 new_num_data = 0;
639 trailing_data = segment->num_data;
641 while (1) {
642 length = data->header.length;
643 new_num_data += length;
644 trailing_data -= length;
645 data += length;
647 /* Check for invalid data size */
648 if (trailing_data < 0)
649 return 0;
651 if (trailing_data == 0 || data->header.type == CAIRO_PATH_MOVE_TO)
652 break;
654 /* Ensure that all the components are valid primitives */
655 if (cpml_primitive_type_get_n_points(data->header.type) == 0)
656 return 0;
659 segment->num_data = new_num_data;
660 return 1;