Revert "cpml: protect class data from NULL"
[adg.git] / src / cpml / cpml-primitive.c
blob8e2362765ad7aa7371e43ded0ddd2ae825e82311
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-primitive
23 * @Section_Id:Primitive
24 * @title: CpmlPrimitive
25 * @short_description: Basic component of segments
27 * A primitive is an atomic geometric element found inside #CpmlSegment.
28 * The available primitives are the same defined by #cairo_path_data_type_t
29 * with the additional #CPML_ARC type (check #CpmlPrimitiveType
30 * for further information) and without #CPML_MOVE as it is not
31 * considered a primitive and it is managed in different way: the move-to
32 * primitives are only used to define the origin of a segment.
34 * Since: 1.0
35 **/
37 /**
38 * CpmlPrimitiveType:
39 * @CPML_MOVE: equivalent to %CAIRO_PATH_MOVE_TO
40 * @CPML_LINE: equivalent to %CAIRO_PATH_LINE_TO
41 * @CPML_CURVE: equivalent to %CAIRO_PATH_CURVE_TO
42 * @CPML_CLOSE: equivalent to %CAIRO_PATH_CLOSE_PATH
43 * @CPML_ARC: an arc representation at CPML level
45 * This is a type compatible with #cairo_path_data_type_t type. It is
46 * basically the same enum but it embodies an important difference:
47 * it can be used to specify the special #CPML_ARC primitive. Having
48 * two different types is a good way to make clear when a function
49 * expect or not embedded #CPML_ARC primitives.
51 * Arcs are used extensively in technical drawing: some operations are
52 * trivials with arcs and a nightmare with cubic Bézier curves. Actually,
53 * at least up to version 1.10.2, the cairo library does not support arc
54 * primitives natively and there is no plan they will be ever supported.
56 * The CPML library supports arc natively, converting them to curves
57 * when the #CpmlSegment is returned to the cairo context, for instance
58 * when using cpml_segment_to_cairo().
60 * Since: 1.0
61 **/
63 /**
64 * CpmlPrimitive:
65 * @segment: the source #CpmlSegment
66 * @org: a pointer to the first point of the primitive
67 * @data: the array of the path data, prepended by the header
69 * As for #CpmlSegment, also the primitive is unobtrusive. This
70 * means CpmlPrimitive does not include any coordinates but instead
71 * keeps pointers to the original segment (and, by transition, to
72 * the underlying #cairo_data_path_t struct).
74 * Since: 1.0
75 **/
78 #include "cpml-internal.h"
79 #include "cpml-extents.h"
80 #include "cpml-segment.h"
81 #include "cpml-primitive.h"
82 #include "cpml-primitive-private.h"
83 #include "cpml-arc.h"
84 #include "cpml-curve.h"
85 #include <string.h>
86 #include <stdio.h>
89 static const _CpmlPrimitiveClass *
90 _cpml_class_from_type (CpmlPrimitiveType type);
91 static const _CpmlPrimitiveClass *
92 _cpml_class_from_obj (const CpmlPrimitive *primitive);
93 static cairo_path_data_t *
94 _cpml_get_point (const CpmlPrimitive *primitive,
95 int n_point);
96 static void _cpml_dump_point (const cairo_path_data_t *path_data);
99 /**
100 * cpml_primitive_type_get_n_points:
101 * @type: a primitive type
103 * Gets the number of points required to identify the @type primitive.
105 * Returns: the number of points or 0 on errors
107 * Since: 1.0
109 size_t
110 cpml_primitive_type_get_n_points(CpmlPrimitiveType type)
112 const _CpmlPrimitiveClass *class_data = _cpml_class_from_type(type);
114 if (class_data == NULL)
115 return 0;
117 return class_data->n_points;
121 * cpml_primitive_from_segment:
122 * @primitive: the destination #CpmlPrimitive struct
123 * @segment: the source segment
125 * Initializes @primitive to the first primitive of @segment.
127 * Since: 1.0
129 void
130 cpml_primitive_from_segment(CpmlPrimitive *primitive, CpmlSegment *segment)
132 primitive->segment = segment;
134 if (segment != NULL) {
135 /* The first element of a CpmlSegment is always a CPML_MOVE,
136 * as ensured by cpml_segment_from_cairo() and by the browsing APIs,
137 * so the origin is in the second data item */
138 primitive->org = segment->data + 1;
140 /* Also, the segment APIs ensure that @segment is prepended by
141 * only one CPML_MOVE */
142 primitive->data = segment->data + segment->data->header.length;
143 } else {
144 primitive->org = NULL;
145 primitive->data = NULL;
150 * cpml_primitive_copy:
151 * @primitive: the destination #CpmlPrimitive
152 * @src: the source #CpmlPrimitive
154 * Copies @src in @primitive. This is a shallow copy: the internal fields
155 * of @primitive refer to the same memory as the original @src primitive.
157 * Since: 1.0
159 void
160 cpml_primitive_copy(CpmlPrimitive *primitive, const CpmlPrimitive *src)
162 if (src != NULL) {
163 memcpy(primitive, src, sizeof(CpmlPrimitive));
168 * cpml_primitive_reset:
169 * @primitive: a #CpmlPrimitive
171 * Resets @primitive so it refers to the first primitive of the
172 * source segment.
174 * Since: 1.0
176 void
177 cpml_primitive_reset(CpmlPrimitive *primitive)
179 cpml_primitive_from_segment(primitive, primitive->segment);
183 * cpml_primitive_next:
184 * @primitive: a #CpmlPrimitive
186 * Changes @primitive so it refers to the next primitive on the
187 * source segment. If there are no more primitives, @primitive is
188 * not changed and 0 is returned.
190 * Returns: (type boolean): 1 on success, 0 if no next primitive found or errors.
192 * Since: 1.0
195 cpml_primitive_next(CpmlPrimitive *primitive)
197 cairo_path_data_t *new_data;
198 const cairo_path_data_t *end_data;
200 new_data = primitive->data + primitive->data->header.length;
201 end_data = primitive->segment->data + primitive->segment->num_data;
203 if (new_data >= end_data)
204 return 0;
206 primitive->org = _cpml_get_point(primitive, -1);
207 primitive->data = new_data;
209 return 1;
213 * cpml_primitive_get_n_points:
214 * @primitive: a #CpmlPrimitive
216 * Gets the number of points required to identify @primitive.
217 * It is similar to cpml_primitive_type_get_n_points() but using
218 * a @primitive instance instead of a type.
220 * Returns: the number of points or 0 on errors.
222 * <!-- Virtual: n_points -->
224 * Since: 1.0
226 size_t
227 cpml_primitive_get_n_points(const CpmlPrimitive *primitive)
229 return cpml_primitive_type_get_n_points(primitive->data->header.type);
233 * cpml_primitive_get_length:
234 * @primitive: a #CpmlPrimitive
236 * Abstracts the length() family functions by providing a common
237 * way to access the underlying primitive-specific implementation.
238 * The function returns the length of @primitive.
240 * Returns: the requested length or 0 on errors
242 * <!-- Virtual: get_length -->
244 * Since: 1.0
246 double
247 cpml_primitive_get_length(const CpmlPrimitive *primitive)
249 const _CpmlPrimitiveClass *class_data = _cpml_class_from_obj(primitive);
251 if (class_data == NULL || class_data->get_length == NULL)
252 return 0;
254 return class_data->get_length(primitive);
258 * cpml_primitive_put_extents:
259 * @primitive: a #CpmlPrimitive
260 * @extents: (out caller-allocates): where to store the extents
262 * Abstracts the extents() family functions by providing a common
263 * way to access the underlying primitive-specific implementation.
265 * This function stores in @extents the bounding box of @primitive.
267 * On errors, that is if the extents cannot be calculated for some
268 * reason, this function does nothing.
270 * <!-- Virtual: put_extents -->
272 * Since: 1.0
274 void
275 cpml_primitive_put_extents(const CpmlPrimitive *primitive,
276 CpmlExtents *extents)
278 const _CpmlPrimitiveClass *class_data = _cpml_class_from_obj(primitive);
280 if (class_data == NULL || class_data->put_extents == NULL)
281 return;
283 class_data->put_extents(primitive, extents);
287 * cpml_primitive_set_point:
288 * @primitive: a #CpmlPrimitive
289 * @n_point: the index of the point to retrieve
290 * @pair: (allow-none): the source #CpmlPair
292 * Sets the specified @n_point of @primitive to @pair. The @n_point
293 * index starts from 0: if @n_point is 0, the start point (the origin)
294 * is changed, 1 for the second point and so on. If @n_point is
295 * negative, it is considered as a negative index from the end, so
296 * that -1 is the end point, -2 the point before the end point and
297 * so on.
299 * #CPML_CLOSE is managed in a special way: if @n_point
300 * is -1 or 1 and @primitive is a close-path, this function cycles
301 * the source #CpmlSegment and returns the first point. This is
302 * needed because requesting the end point (or the second point)
303 * of a close path is a valid operation and must returns the origin
304 * of the segment.
306 * Returns: (type gboolean): 1 if the point to be set is existent, 0 otherwise.
308 * Since: 1.0
311 cpml_primitive_set_point(CpmlPrimitive *primitive,
312 int n_point, const CpmlPair *pair)
314 cairo_path_data_t *point = _cpml_get_point(primitive, n_point);
316 if (point == NULL)
317 return 0;
319 if (pair != NULL)
320 cpml_pair_to_cairo(pair, point);
322 return 1;
326 * cpml_primitive_put_point:
327 * @primitive: a #CpmlPrimitive
328 * @n_point: the index of the point to retrieve
329 * @pair: (out caller-allocates) (allow-none): the destination #CpmlPair
331 * Gets the specified @n_point from @primitive and stores it into
332 * @pair. The @n_point index is subject to the same rules explained
333 * in the cpml_primitive_set_point() function.
335 * Returns: (type gboolean): 1 if the point is found, 0 otherwise.
337 * Since: 1.0
340 cpml_primitive_put_point(const CpmlPrimitive *primitive,
341 int n_point, CpmlPair *pair)
343 const cairo_path_data_t *point = _cpml_get_point(primitive, n_point);
345 if (point == NULL)
346 return 0;
348 if (pair != NULL)
349 cpml_pair_from_cairo(pair, point);
351 return 1;
355 * cpml_primitive_put_pair_at:
356 * @primitive: a #CpmlPrimitive
357 * @pos: the position value
358 * @pair: (out caller-allocates): the destination #CpmlPair
360 * Abstracts the <function>put_pair_at</function> family functions by
361 * providing a common way to access the underlying primitive-specific
362 * implementation.
364 * It gets the coordinates of the point lying on @primitive
365 * at position @pos. @pos is an homogeneous factor where 0 is the
366 * start point, 1 the end point, 0.5 the mid point and so on.
367 * @pos can be less than 0 or greater than 1, in which case the
368 * coordinates of @pair are interpolated.
370 * On errors, that is if the coordinates cannot be calculated for
371 * some reason, this function does nothing.
373 * <!-- Virtual: put_pair_at -->
375 * Since: 1.0
377 void
378 cpml_primitive_put_pair_at(const CpmlPrimitive *primitive,
379 double pos, CpmlPair *pair)
381 const _CpmlPrimitiveClass *class_data = _cpml_class_from_obj(primitive);
383 if (class_data == NULL || class_data->put_pair_at == NULL)
384 return;
386 class_data->put_pair_at(primitive, pos, pair);
390 * cpml_primitive_put_vector_at:
391 * @primitive: a #CpmlPrimitive
392 * @pos: the position value
393 * @vector: (out caller-allocates): the destination #CpmlVector
395 * Abstracts the <function>put_vector_at</function> family functions by
396 * providing a common way to access the underlying primitive-specific
397 * implementation.
399 * It gets the steepness of the point at position @pos on @primitive.
400 * @pos is an homogeneous factor where 0 is the start point, 1 the
401 * end point, 0.5 the mid point and so on.
402 * @pos can be less than 0 or greater than 1, in which case the
403 * coordinates of @pair are interpolated.
405 * On errors, that is if the steepness cannot be calculated for
406 * some reason, this function does nothing.
408 * <!-- Virtual: put_vector_at -->
410 * Since: 1.0
412 void
413 cpml_primitive_put_vector_at(const CpmlPrimitive *primitive,
414 double pos, CpmlVector *vector)
416 const _CpmlPrimitiveClass *class_data = _cpml_class_from_obj(primitive);
418 if (class_data == NULL || class_data->put_vector_at == NULL)
419 return;
421 class_data->put_vector_at(primitive, pos, vector);
425 * cpml_primitive_is_inside:
426 * @primitive: a #CpmlPrimitive
427 * @pair: the coordinates of the subject point
429 * Checks if @pair is inside the bounding box of @primitive. This
430 * can be useful e.g. to verify if an intersection point is a real
431 * intersection or an hypothetical one.
433 * Returns: 1 if @pair is inside the bounding box of @primitive, 0 otherwise.
435 * Since: 1.0
438 cpml_primitive_is_inside(const CpmlPrimitive *primitive, const CpmlPair *pair)
440 CpmlExtents extents = { 0 };
441 cpml_primitive_put_extents(primitive, &extents);
442 return cpml_extents_pair_is_inside(&extents, pair);
446 * cpml_primitive_get_closest_pos:
447 * @primitive: a #CpmlPrimitive
448 * @pair: the coordinates of the subject point
450 * Returns the pos value of the point on @primitive nearest to @pair.
451 * The returned value is always clamped between 0 and 1.
453 * Returns: the requested pos value between 0 and 1, or -1 on errors.
455 * <!-- Virtual: get_closest_pos -->
457 * Since: 1.0
459 double
460 cpml_primitive_get_closest_pos(const CpmlPrimitive *primitive,
461 const CpmlPair *pair)
463 const _CpmlPrimitiveClass *class_data;
465 if (pair == NULL)
466 return -1;
468 class_data = _cpml_class_from_obj(primitive);
469 if (class_data == NULL || class_data->get_closest_pos == NULL)
470 return -1;
472 return class_data->get_closest_pos(primitive, pair);
476 * cpml_primitive_put_intersections:
477 * @primitive: the first #CpmlPrimitive
478 * @primitive2: the second #CpmlPrimitive
479 * @n_dest: maximum number of intersections to return
480 * @dest: (out caller-allocates) (array length=n_dest): the destination buffer that can contain @n_dest #CpmlPair
482 * Finds the intersection points between the given primitives and
483 * returns the result in @dest. The size of @dest should be enough
484 * to store @n_dest #CpmlPair. The maximum number of intersections
485 * is dependent on the type of the primitive involved in the
486 * operation. If there are at least one Bézier curve involved, up to
487 * 4 intersections could be returned. Otherwise, if there is an arc
488 * the intersections will be 2 at maximum. For line primitives, there
489 * is only 1 point (or 0 if the lines are parallel).
491 * Also hypothetical intersections are returned, that is intersections
492 * made by extending the primitives outside their bounds. This means e.g.
493 * two lines always return one intersection if they are not parallel. To
494 * discriminate real intersections you should check the returned points
495 * with cpml_primitive_is_inside(), for example:
497 * <informalexample><programlisting language="C">
498 * if (cpml_primitive_put_intersections(line1, line2, 1, &pair) == 0) {
499 * // line1 and line2 are parallels
500 * } else if (cpml_primitive_is_inside(line1, &pair) &&
501 * cpml_primitive_is_inside(line2, &pair)) {
502 * // This is a real intersection
503 * } else {
504 * // This is an hypothetical intersection
506 * </programlisting></informalexample>
508 * <note>
509 * <para>
510 * The convention used by the CPML library is that a primitive should
511 * implement only the intersection algorithms with lower degree
512 * primitives. This is required to avoid code duplication: intersection
513 * between arc and Bézier curves must be implemented by #CPML_CURVE and
514 * intersection between lines and arcs must be implemented by #CPML_ARC.
515 * cpml_primitive_put_intersections() will take care of swapping the
516 * arguments if they are not properly ordered.
517 * </para>
518 * </note>
520 * Returns: the number of intersection points found or 0 if the
521 * primitives do not intersect or on errors.
523 * <!-- Virtual: put_intersections -->
525 * Since: 1.0
527 size_t
528 cpml_primitive_put_intersections(const CpmlPrimitive *primitive,
529 const CpmlPrimitive *primitive2,
530 size_t n_dest, CpmlPair *dest)
532 const _CpmlPrimitiveClass *class_data;
533 size_t n_points, n_points2;
535 if (primitive2 == NULL || dest == NULL)
536 return 0;
538 class_data = _cpml_class_from_obj(primitive);
539 if (class_data == NULL)
540 return 0;
542 n_points = cpml_primitive_get_n_points(primitive);
543 n_points2 = cpml_primitive_get_n_points(primitive2);
545 /* Check if the primitives can intersect */
546 if (n_points == 0 || n_points2 == 0)
547 return 0;
549 /* Primitives reordering: the first must be the more complex one */
550 if (n_points < n_points2) {
551 return cpml_primitive_put_intersections(primitive2, primitive,
552 n_dest, dest);
555 /* Check if put_intersections is implemented */
556 if (class_data->put_intersections == NULL)
557 return 0;
559 return class_data->put_intersections(primitive, primitive2, n_dest, dest);
563 * cpml_primitive_put_intersections_with_segment:
564 * @primitive: a #CpmlPrimitive
565 * @segment: a #CpmlSegment
566 * @n_dest: maximum number of intersections to return
567 * @dest: (out caller-allocates) (array length=n_dest): the destination buffer that can contain @n_dest #CpmlPair
569 * Computes the intersections between @segment and @primitive by
570 * sequentially scanning the primitives in @segment and looking
571 * for their intersections with @primitive.
573 * If the intersections are more than @n_dest, only the first
574 * @n_dest pairs are stored.
576 * Returns: the number of intersection points found or 0 if the
577 * items do not intersect or on errors.
579 * Since: 1.0
581 size_t
582 cpml_primitive_put_intersections_with_segment(const CpmlPrimitive *primitive,
583 const CpmlSegment *segment,
584 size_t n_dest, CpmlPair *dest)
586 CpmlPrimitive portion;
587 size_t found;
589 if (segment == NULL || dest == NULL)
590 return 0;
592 cpml_primitive_from_segment(&portion, (CpmlSegment *) segment);
593 found = 0;
595 while (found < n_dest) {
596 found += cpml_primitive_put_intersections(&portion, primitive,
597 n_dest-found, dest+found);
598 if (!cpml_primitive_next(&portion))
599 break;
602 return found;
606 * cpml_primitive_offset:
607 * @primitive: a #CpmlPrimitive
608 * @offset: distance for the computed offset primitive
610 * Given a primitive, computes the same (or approximated) parallel
611 * primitive distant @offset from the original one and returns
612 * the result by changing @primitive.
614 * On errors, that is if the offset primitive cannot be calculated
615 * for some reason, this function does nothing.
617 * <!-- Virtual: offset -->
619 * Since: 1.0
621 void
622 cpml_primitive_offset(CpmlPrimitive *primitive, double offset)
624 const _CpmlPrimitiveClass *class_data = _cpml_class_from_obj(primitive);
626 if (class_data == NULL || class_data->offset == NULL)
627 return;
629 class_data->offset(primitive, offset);
633 * cpml_primitive_join:
634 * @primitive: the first #CpmlPrimitive
635 * @primitive2: (inout) (transfer none): the second #CpmlPrimitive
637 * Joins two primitive modifying the end point of @primitive and the
638 * start point of @primitive2 so that the resulting points will overlap.
640 * <important>
641 * <title>TODO</title>
642 * <itemizedlist>
643 * <listitem>Actually, the join is done by extending the end vector
644 * of @primitive and the start vector of @primitive2 and
645 * interpolating the intersection: this means no primitive
646 * dependent code is needed. Anyway, it is likely to change
647 * in the future because this approach is quite naive when
648 * curves are involved.</listitem>
649 * </itemizedlist>
650 * </important>
652 * Returns: (type boolean): 1 on success, 0 if the primitives cannot be joint.
654 * <!-- Virtual: join -->
656 * Since: 1.0
659 cpml_primitive_join(CpmlPrimitive *primitive, CpmlPrimitive *primitive2)
661 cairo_path_data_t *end1, *start2;
662 CpmlPrimitive line1, line2;
663 cairo_path_data_t data1[2], data2[2];
664 CpmlPair joint;
666 end1 = _cpml_get_point(primitive, -1);
667 start2 = _cpml_get_point(primitive2, 0);
669 /* Check if the primitives are already connected */
670 if (end1->point.x == start2->point.x && end1->point.y == start2->point.y)
671 return 1;
673 line1.org = _cpml_get_point(primitive, -2);
674 line1.data = data1;
675 data1[0].header.type = CPML_LINE;
676 data1[1] = *end1;
678 line2.org = start2;
679 line2.data = data2;
680 data2[0].header.type = CPML_LINE;
681 data2[1] = *_cpml_get_point(primitive2, 1);
683 if (!cpml_primitive_put_intersections(&line1, &line2, 1, &joint))
684 return 0;
686 cpml_pair_to_cairo(&joint, end1);
687 cpml_pair_to_cairo(&joint, start2);
689 return 1;
693 * cpml_primitive_to_cairo:
694 * @primitive: a #CpmlPrimitive
695 * @cr: (inout) (transfer none): the destination cairo context
697 * Renders a single @primitive to the @cr cairo context.
698 * As a special case, if the primitive is a #CPML_CLOSE, an
699 * equivalent line is rendered, because a close path left alone
700 * is not renderable.
702 * Also a #CPML_ARC primitive is treated specially, as it is not
703 * natively supported by cairo and has its own rendering API.
705 * Since: 1.0
707 void
708 cpml_primitive_to_cairo(const CpmlPrimitive *primitive, cairo_t *cr)
710 CpmlPrimitiveType type;
711 cairo_path_t path;
712 cairo_path_data_t *path_data;
714 if (primitive == NULL || cr == NULL)
715 return;
717 cairo_move_to(cr, primitive->org->point.x, primitive->org->point.y);
719 type = primitive->data->header.type;
721 if (type == CPML_CLOSE) {
722 path_data = _cpml_get_point(primitive, -1);
723 cairo_line_to(cr, path_data->point.x, path_data->point.y);
724 } else if (type == CPML_ARC) {
725 cpml_arc_to_cairo(primitive, cr);
726 } else {
727 path.status = CAIRO_STATUS_SUCCESS;
728 path.data = primitive->data;
729 path.num_data = primitive->data->header.length;
730 cairo_append_path(cr, &path);
735 * cpml_primitive_dump:
736 * @primitive: a #CpmlPrimitive
737 * @org_also: whether to output also the origin coordinates
739 * Dumps info on the specified @primitive to stdout: useful for
740 * debugging purposes. If @org_also is 1, a #CPML_MOVE to the
741 * origin is prepended to the data otherwise the
742 * <structfield>org</structfield> field is not used.
744 * Since: 1.0
746 void
747 cpml_primitive_dump(const CpmlPrimitive *primitive, int org_also)
749 const cairo_path_data_t *data;
750 int type;
751 const _CpmlPrimitiveClass *class_data;
752 size_t n, n_points;
754 if (primitive == NULL) {
755 printf("NULL primitive\n");
756 return;
759 data = primitive->data;
760 type = data->header.type;
761 class_data = _cpml_class_from_type(type);
763 if (class_data == NULL) {
764 printf("Unknown primitive type (%d)\n", type);
765 return;
768 /* Dump the origin, if requested */
769 if (org_also) {
770 printf("move to ");
771 _cpml_dump_point(primitive->org);
772 printf("\n");
775 printf("%s ", class_data->name);
777 n_points = cpml_primitive_get_n_points(primitive);
778 for (n = 1; n < n_points; ++n)
779 _cpml_dump_point(_cpml_get_point(primitive, n));
781 printf("\n");
785 static const _CpmlPrimitiveClass *
786 _cpml_class_from_type(CpmlPrimitiveType type)
788 if (type == CPML_LINE)
789 return _cpml_line_get_class();
790 else if (type == CPML_ARC)
791 return _cpml_arc_get_class();
792 else if (type == CPML_CURVE)
793 return _cpml_curve_get_class();
794 else if (type == CPML_CLOSE)
795 return _cpml_close_get_class();
797 return NULL;
800 static const _CpmlPrimitiveClass *
801 _cpml_class_from_obj(const CpmlPrimitive *primitive)
803 return _cpml_class_from_type(primitive->data->header.type);
807 * _cpml_get_point:
808 * @primitive: a #CpmlPrimitive
809 * @n_point: the index of the point to retrieve
811 * Gets the specified @n_point from @primitive. The index starts
812 * at 0: if @n_point is 0, the start point (the origin) is
813 * returned, 1 for the second point and so on. If @n_point is
814 * negative, it is considered as a negative index from the end,
815 * so that -1 is the end point, -2 the point before the end point
816 * and so on.
818 * #CPML_CLOSE is managed in a special way: if @n_point
819 * is -1 or 1 and @primitive is a close-path, this function cycles
820 * the source #CpmlSegment and returns the first point. This is
821 * needed because requesting the end point (or the second point)
822 * of a close path is a valid operation and must returns the start
823 * of the segment.
825 * Returns: a pointer to the requested point (in cairo format)
826 * or <constant>NULL</constant> if the point is outside
827 * the valid range.
829 * Since: 1.0
831 static cairo_path_data_t *
832 _cpml_get_point(const CpmlPrimitive *primitive, int n_point)
834 size_t n_points;
836 /* For a start point request, simply return the origin
837 * without further checking */
838 if (n_point == 0)
839 return primitive->org;
841 /* The CPML_CLOSE special case */
842 if (primitive->data->header.type == CAIRO_PATH_CLOSE_PATH &&
843 (n_point == 1 || n_point == -1))
844 return &primitive->segment->data[1];
846 n_points = cpml_primitive_get_n_points(primitive);
847 if (n_points == 0)
848 return NULL;
850 /* If n_point is negative, consider it as a negative index from the end */
851 if (n_point < 0)
852 n_point = n_points + n_point;
854 /* Out of range condition */
855 if (n_point < 0 || n_point >= n_points)
856 return NULL;
858 return n_point == 0 ? primitive->org : &primitive->data[n_point];
861 static void
862 _cpml_dump_point(const cairo_path_data_t *path_data)
864 printf("(%g %g) ", path_data->point.x, path_data->point.y);