adg: do not force wrong type on adg_path_append_cairo_path
[adg.git] / src / adg / adg-path.c
blobe9b709e35276ff4ade9382a9d3d1bbf5780c3aec
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2017 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:adg-path
23 * @short_description: The basic model representing a generic path
25 * The #AdgPath model represents a virtual #cairo_path_t: this class
26 * implements methods to create the path and provides additional
27 * operations specific to technical drawings.
29 * #AdgPath overrides the <function>get_cairo_path</function> method
30 * of the parent #AdgTrail class, avoiding the need of an
31 * #AdgTrailCallback. The path is constructed programmatically: keep
32 * in mind any method that modifies the path will invalidate the
33 * #cairo_path_t returned by adg_trail_get_cairo_path().
35 * Although some of the provided methods are clearly based on the
36 * original cairo path manipulation API, their behavior could be
37 * sligthly different. This is intentional, because the ADG provides
38 * additional path manipulation algorithms, sometime quite complex,
39 * and a more restrictive filter on the path quality is required.
40 * Also, the ADG is designed to be used by technicians while cairo
41 * targets a broader range of developers.
43 * As an example, following the rule of the less surprise, some
44 * cairo functions guess the current point when it is not defined,
45 * while the #AdgPath methods trigger a warning without other effect.
46 * Furthermore, after cairo_close_path() a %CPML_MOVE primitive to
47 * the starting point of the segment is automatically added by cairo;
48 * in ADG, after an adg_path_close() the current point is unset.
50 * Since: 1.0
51 **/
53 /**
54 * AdgPath:
56 * All fields are private and should not be used directly.
57 * Use its public methods instead.
59 * Since: 1.0
60 **/
63 #include "adg-internal.h"
65 #include "adg-model.h"
66 #include "adg-trail.h"
68 #include "adg-path.h"
69 #include "adg-path-private.h"
72 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_path_parent_class)
73 #define _ADG_OLD_MODEL_CLASS ((AdgModelClass *) adg_path_parent_class)
75 #define REMAPPED(ptr, from, to) \
76 (ptr) == NULL ? NULL : \
77 (gpointer) ((guint8 *) (ptr) - (guint8 *) (from) + (guint8 *) (to))
80 G_DEFINE_TYPE(AdgPath, adg_path, ADG_TYPE_TRAIL)
83 static void _adg_finalize (GObject *object);
84 static void _adg_clear (AdgModel *model);
85 static void _adg_clear_parent (AdgModel *model);
86 static void _adg_changed (AdgModel *model);
87 static cairo_path_t * _adg_get_cairo_path (AdgTrail *trail);
88 static cairo_path_t * _adg_read_cairo_path (AdgPath *path);
89 static gint _adg_primitive_length (CpmlPrimitiveType type);
90 static void _adg_primitive_remap (CpmlPrimitive *primitive,
91 gpointer to,
92 const CpmlPrimitive
93 *old,
94 gconstpointer from);
95 static void _adg_rescan (AdgPath *path);
96 static void _adg_append_primitive (AdgPath *path,
97 CpmlPrimitive *primitive);
98 static void _adg_clear_operation (AdgPath *path);
99 static gboolean _adg_append_operation (AdgPath *path,
100 gint action,
101 ...);
102 static void _adg_do_operation (AdgPath *path,
103 cairo_path_data_t
104 *path_data);
105 static void _adg_do_action (AdgPath *path,
106 AdgAction action,
107 CpmlPrimitive *primitive);
108 static void _adg_do_chamfer (AdgPath *path,
109 CpmlPrimitive *current);
110 static void _adg_do_fillet (AdgPath *path,
111 CpmlPrimitive *current);
112 static gboolean _adg_is_convex (const CpmlPrimitive
113 *primitive1,
114 const CpmlPrimitive
115 *primitive2);
116 static const gchar * _adg_action_name (AdgAction action);
117 static void _adg_get_named_pair (AdgModel *model,
118 const gchar *name,
119 CpmlPair *pair,
120 gpointer user_data);
121 static void _adg_dup_reverse_named_pairs
122 (AdgModel *model,
123 const cairo_matrix_t
124 *matrix);
127 static void
128 adg_path_class_init(AdgPathClass *klass)
130 GObjectClass *gobject_class;
131 AdgModelClass *model_class;
132 AdgTrailClass *trail_class;
134 gobject_class = (GObjectClass *) klass;
135 model_class = (AdgModelClass *) klass;
136 trail_class = (AdgTrailClass *) klass;
138 g_type_class_add_private(klass, sizeof(AdgPathPrivate));
140 gobject_class->finalize = _adg_finalize;
142 model_class->clear = _adg_clear;
143 model_class->changed = _adg_changed;
145 trail_class->get_cairo_path = _adg_get_cairo_path;
148 static void
149 adg_path_init(AdgPath *path)
151 AdgPathPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(path, ADG_TYPE_PATH,
152 AdgPathPrivate);
154 data->cp_is_valid = FALSE;
155 data->cp.x = 0;
156 data->cp.y = 0;
157 data->cairo.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
158 data->cairo.path.data = NULL;
159 data->cairo.path.num_data = 0;
160 data->cairo.array = g_array_new(FALSE, FALSE, sizeof(cairo_path_data_t));
161 data->last.segment = NULL;
162 data->last.org = NULL;
163 data->last.data = NULL;
164 data->over.segment = NULL;
165 data->over.org = NULL;
166 data->over.data = NULL;
167 data->operation.action = ADG_ACTION_NONE;
169 path->data = data;
172 static void
173 _adg_finalize(GObject *object)
175 AdgPath *path;
176 AdgPathPrivate *data;
178 path = (AdgPath *) object;
179 data = path->data;
181 g_array_free(data->cairo.array, TRUE);
182 _adg_clear_operation(path);
184 if (_ADG_OLD_OBJECT_CLASS->finalize)
185 _ADG_OLD_OBJECT_CLASS->finalize(object);
190 * adg_path_new:
192 * Creates a new path model. The path should be constructed
193 * programmatically by using the methods provided by #AdgPath.
195 * Returns: the newly created path model
197 * Since: 1.0
199 AdgPath *
200 adg_path_new(void)
202 return g_object_new(ADG_TYPE_PATH, NULL);
206 * adg_path_get_current_point:
207 * @path: an #AdgPath
209 * Gets the current point of @path, which is conceptually the
210 * final point reached by the path so far.
212 * If there is no defined current point, <constant>NULL</constant> is returned.
213 * It is possible to check this in advance with
214 * adg_path_has_current_point().
216 * Most #AdgPath methods alter the current point and most of them
217 * expect a current point to be defined otherwise will fail triggering
218 * a warning. Check the description of every method for specific details.
220 * Returns: (transfer none): the current point or <constant>NULL</constant> on no current point set or errors.
222 * Since: 1.0
224 const CpmlPair *
225 adg_path_get_current_point(AdgPath *path)
227 AdgPathPrivate *data;
229 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
231 data = path->data;
233 if (!data->cp_is_valid)
234 return NULL;
236 return &data->cp;
240 * adg_path_has_current_point:
241 * @path: an #AdgPath
243 * Returns whether a current point is defined on @path.
244 * See adg_path_get_current_point() for details on the current point.
246 * Returns: whether a current point is defined
248 * Since: 1.0
250 gboolean
251 adg_path_has_current_point(AdgPath *path)
253 AdgPathPrivate *data;
255 g_return_val_if_fail(ADG_IS_PATH(path), FALSE);
257 data = path->data;
259 return data->cp_is_valid;
263 * adg_path_last_primitive:
264 * @path: an #AdgPath
266 * Gets the last primitive appended to @path. The #CPML_MOVE type is
267 * not considered a full-fledged primitive, i.e. adg_path_move_to()
268 * or similar does not change the last primitive.
270 * The returned struct is owned by @path and should not be freed or
271 * modified.
273 * Returns: (transfer none): a pointer to the last appended primitive or <constant>NULL</constant> on no last primitive or on errors.
275 * Since: 1.0
277 const CpmlPrimitive *
278 adg_path_last_primitive(AdgPath *path)
280 AdgPathPrivate *data;
282 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
284 data = path->data;
286 /* Directly return NULL instead of returning an undefined primitive */
287 if (data->last.org == NULL || data->last.data == NULL)
288 return NULL;
290 return &data->last;
294 * adg_path_over_primitive:
295 * @path: an #AdgPath
297 * Gets the primitive before the last one appended to @path. The
298 * "over" term comes from forth, where the <emphasis>OVER</emphasis>
299 * operator works on the stack in the same way as
300 * adg_path_over_primitive() works on @path. The #CPML_MOVE type is
301 * not considered a full-fledged primitive, i.e. adg_path_move_to()
302 * or similar does not change the over primitive.
304 * The returned struct is owned by @path and should not be freed or
305 * modified.
307 * Returns: (transfer none): a pointer to the primitive before the last appended one or <constant>NULL</constant> on errors.
309 * Since: 1.0
311 const CpmlPrimitive *
312 adg_path_over_primitive(AdgPath *path)
314 AdgPathPrivate *data;
316 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
318 data = path->data;
320 /* Directly return NULL instead of returning an undefined primitive */
321 if (data->over.org == NULL || data->over.data == NULL)
322 return NULL;
324 return &data->over;
328 * adg_path_append:
329 * @path: an #AdgPath
330 * @type: (type CpmlPrimitiveType): a #cairo_data_type_t value
331 * @...: point data, specified as #CpmlPair pointers
333 * Generic method to append a primitive to @path. The number of #CpmlPair
334 * pointers to pass as @Varargs depends on @type: %CPML_CLOSE does not
335 * require any pair, %CPML_MOVE and %CPML_LINE require one pair,
336 * %CPML_ARC two pairs, %CPML_CURVE three pairs and so on.
338 * All the needed pairs must be not <constant>NULL</constant> pointers,
339 * otherwise the function will fail. The pairs in excess, if any, are ignored.
341 * Since: 1.0
343 void
344 adg_path_append(AdgPath *path, gint type, ...)
346 va_list var_args;
348 va_start(var_args, type);
349 adg_path_append_valist(path, (CpmlPrimitiveType) type, var_args);
350 va_end(var_args);
354 * adg_path_append_valist:
355 * @path: an #AdgPath
356 * @type: a #cairo_data_type_t value
357 * @var_args: point data, specified as #CpmlPair pointers
359 * va_list version of adg_path_append().
361 * Since: 1.0
363 void
364 adg_path_append_valist(AdgPath *path, CpmlPrimitiveType type, va_list var_args)
366 GPtrArray *array;
367 const CpmlPair *pair;
368 gint length;
370 length = _adg_primitive_length(type);
371 if (length == 0)
372 return;
374 array = g_ptr_array_sized_new(4);
375 while (-- length) {
376 pair = va_arg(var_args, const CpmlPair *);
377 if (pair == NULL) {
378 g_ptr_array_free(array, TRUE);
379 g_return_if_reached();
380 return;
382 g_ptr_array_add(array, (gpointer) pair);
385 /* The array must be NULL terminated */
386 g_ptr_array_add(array, NULL);
388 adg_path_append_array(path, type, (const CpmlPair **) array->pdata);
390 g_ptr_array_free(array, TRUE);
394 * adg_path_append_array: (rename-to adg_path_append)
395 * @path: an #AdgPath
396 * @type: a #cairo_data_type_t value
397 * @pairs: (array zero-terminated=1) (element-type Cpml.Pair) (transfer none): point data, specified as a <constant>NULL</constant> terminated array of #CpmlPair pointers.
399 * A bindingable version of adg_path_append() that uses a
400 * <constant>NULL</constant> terminated array of pairs instead of variable
401 * argument list and friends.
403 * Furthermore, because of the list is <constant>NULL</constant> terminated,
404 * an arbitrary number of pairs can be passed in @pairs. This allows to embed
405 * in a primitive element more data pairs than requested, something impossible
406 * to do with adg_path_append() and adg_path_append_valist().
408 * Since: 1.0
410 void
411 adg_path_append_array(AdgPath *path, CpmlPrimitiveType type,
412 const CpmlPair **pairs)
414 gint length;
415 GArray *array;
416 const CpmlPair **pair;
417 cairo_path_data_t path_data;
419 g_return_if_fail(ADG_IS_PATH(path));
420 g_return_if_fail(pairs != NULL);
422 length = _adg_primitive_length(type);
423 if (length == 0)
424 return;
426 array = g_array_new(FALSE, FALSE, sizeof(path_data));
427 for (pair = pairs; *pair != NULL; ++ pair) {
428 cpml_pair_to_cairo(*pair, &path_data);
429 g_array_append_val(array, path_data);
432 if (array->len < length - 1) {
433 /* Not enough pairs have been provided */
434 g_warning(_("%s: null pair caught while parsing arguments"), G_STRLOC);
435 } else {
436 AdgPathPrivate *data;
437 CpmlPrimitive primitive;
438 cairo_path_data_t org;
440 /* Save a copy of the current point as primitive origin */
441 data = path->data;
442 cpml_pair_to_cairo(&data->cp, &org);
444 /* Prepend the cairo header */
445 path_data.header.type = type;
446 path_data.header.length = array->len + 1;
447 g_array_prepend_val(array, path_data);
449 /* Append a new primitive to @path */
450 primitive.segment = NULL;
451 primitive.org = &org;
452 primitive.data = (cairo_path_data_t *) array->data;
453 _adg_append_primitive(path, &primitive);
456 g_array_free(array, TRUE);
461 * adg_path_append_primitive:
462 * @path: an #AdgPath
463 * @primitive: the #CpmlPrimitive to append
465 * Appends @primitive to @path. The primitive to add is considered the
466 * continuation of the current path so the <structfield>org</structfield>
467 * component of @primitive is not used. Anyway the current point is
468 * checked against it: they must be equal or the function will fail
469 * without further processing.
471 * Since: 1.0
473 void
474 adg_path_append_primitive(AdgPath *path, const CpmlPrimitive *primitive)
476 AdgPathPrivate *data;
477 CpmlPrimitive *primitive_dup;
479 g_return_if_fail(ADG_IS_PATH(path));
480 g_return_if_fail(primitive != NULL);
481 g_return_if_fail(primitive->org != NULL);
482 g_return_if_fail(primitive->data != NULL);
484 data = path->data;
486 g_return_if_fail(primitive->org->point.x == data->cp.x &&
487 primitive->org->point.y == data->cp.y);
489 /* The primitive data could be modified by pending operations:
490 * work on a copy */
491 primitive_dup = cpml_primitive_deep_dup(primitive);
493 _adg_append_primitive(path, primitive_dup);
495 g_free(primitive_dup);
499 * adg_path_append_segment:
500 * @path: an #AdgPath
501 * @segment: the #CpmlSegment to append
503 * Appends @segment to @path.
505 * Since: 1.0
507 void
508 adg_path_append_segment(AdgPath *path, const CpmlSegment *segment)
510 g_return_if_fail(ADG_IS_PATH(path));
511 g_return_if_fail(segment != NULL);
513 if (segment->num_data > 0) {
514 AdgPathPrivate *data;
516 g_return_if_fail(segment->data != NULL);
518 data = path->data;
520 _adg_clear_parent((AdgModel *) path);
521 data->cairo.array = g_array_append_vals(data->cairo.array,
522 segment->data, segment->num_data);
523 _adg_rescan(path);
528 * adg_path_append_cairo_path:
529 * @path: an #AdgPath
530 * @cairo_path: the #cairo_path_t path to append
532 * Appends a whole #cairo_path_t to @path.
534 * Since: 1.0
536 void
537 adg_path_append_cairo_path(AdgPath *path, const cairo_path_t *cairo_path)
539 AdgPathPrivate *data;
541 g_return_if_fail(ADG_IS_PATH(path));
542 g_return_if_fail(cairo_path != NULL);
544 data = path->data;
546 _adg_clear_parent((AdgModel *) path);
547 data->cairo.array = g_array_append_vals(data->cairo.array,
548 cairo_path->data,
549 cairo_path->num_data);
550 _adg_rescan(path);
554 * adg_path_move_to:
555 * @path: an #AdgPath
556 * @pair: the destination coordinates
558 * Begins a new segment. After this call the current point will be @pair.
560 * Since: 1.0
562 void
563 adg_path_move_to(AdgPath *path, const CpmlPair *pair)
565 adg_path_append(path, CPML_MOVE, pair);
569 * adg_path_move_to_explicit:
570 * @path: an #AdgPath
571 * @x: the new x coordinate
572 * @y: the new y coordinate
574 * Convenient function to call adg_path_move_to() using explicit
575 * coordinates instead of #CpmlPair.
577 * Since: 1.0
579 void
580 adg_path_move_to_explicit(AdgPath *path, gdouble x, gdouble y)
582 CpmlPair p;
584 p.x = x;
585 p.y = y;
587 adg_path_append(path, CPML_MOVE, &p);
591 * adg_path_line_to:
592 * @path: an #AdgPath
593 * @pair: the destination coordinates
595 * Adds a line to @path from the current point to @pair. After this
596 * call the current point will be @pair.
598 * If @path has no current point before this call, this function will
599 * trigger a warning without other effect.
601 * Since: 1.0
603 void
604 adg_path_line_to(AdgPath *path, const CpmlPair *pair)
606 adg_path_append(path, CPML_LINE, pair);
610 * adg_path_line_to_explicit:
611 * @path: an #AdgPath
612 * @x: the new x coordinate
613 * @y: the new y coordinate
615 * Convenient function to call adg_path_line_to() using explicit
616 * coordinates instead of #CpmlPair.
618 * Since: 1.0
620 void
621 adg_path_line_to_explicit(AdgPath *path, gdouble x, gdouble y)
623 CpmlPair p;
625 p.x = x;
626 p.y = y;
628 adg_path_append(path, CPML_LINE, &p);
632 * adg_path_arc_to:
633 * @path: an #AdgPath
634 * @throught: an arbitrary point on the arc
635 * @pair: the destination coordinates
637 * Adds an arc to the path from the current point to @pair, passing
638 * throught @throught. After this call the current point will be @pair.
640 * If @path has no current point before this call, this function will
641 * trigger a warning without other effect.
643 * Since: 1.0
645 void
646 adg_path_arc_to(AdgPath *path, const CpmlPair *throught, const CpmlPair *pair)
648 adg_path_append(path, CPML_ARC, throught, pair);
652 * adg_path_arc_to_explicit:
653 * @path: an #AdgPath
654 * @x1: the x coordinate of an intermediate point
655 * @y1: the y coordinate of an intermediate point
656 * @x2: the x coordinate of the end of the arc
657 * @y2: the y coordinate of the end of the arc
659 * Convenient function to call adg_path_arc_to() using explicit
660 * coordinates instead of #CpmlPair.
662 * Since: 1.0
664 void
665 adg_path_arc_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
666 gdouble x2, gdouble y2)
668 CpmlPair p[2];
670 p[0].x = x1;
671 p[0].y = y1;
672 p[1].x = x2;
673 p[1].y = y2;
675 adg_path_append(path, CPML_ARC, &p[0], &p[1]);
679 * adg_path_curve_to:
680 * @path: an #AdgPath
681 * @control1: the first control point of the curve
682 * @control2: the second control point of the curve
683 * @pair: the destination coordinates
685 * Adds a cubic Bézier curve to the path from the current point to
686 * position @pair, using @control1 and @control2 as control points.
687 * After this call the current point will be @pair.
689 * If @path has no current point before this call, this function will
690 * trigger a warning without other effect.
692 * Since: 1.0
694 void
695 adg_path_curve_to(AdgPath *path, const CpmlPair *control1,
696 const CpmlPair *control2, const CpmlPair *pair)
698 adg_path_append(path, CPML_CURVE, control1, control2, pair);
702 * adg_path_curve_to_explicit:
703 * @path: an #AdgPath
704 * @x1: the x coordinate of the first control point
705 * @y1: the y coordinate of the first control point
706 * @x2: the x coordinate of the second control point
707 * @y2: the y coordinate of the second control point
708 * @x3: the x coordinate of the end of the curve
709 * @y3: the y coordinate of the end of the curve
711 * Convenient function to call adg_path_curve_to() using explicit
712 * coordinates instead of #CpmlPair.
714 * Since: 1.0
716 void
717 adg_path_curve_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
718 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
720 CpmlPair p[3];
722 p[0].x = x1;
723 p[0].y = y1;
724 p[1].x = x2;
725 p[1].y = y2;
726 p[2].x = x3;
727 p[2].y = y3;
729 adg_path_append(path, CPML_CURVE, &p[0], &p[1], &p[2]);
733 * adg_path_close:
734 * @path: an #AdgPath
736 * Adds a line segment to the path from the current point to the
737 * beginning of the current segment, (the most recent point passed
738 * to an adg_path_move_to()), and closes this segment.
739 * After this call the current point will be unset.
741 * The behavior of adg_path_close() is distinct from simply calling
742 * adg_path_line_to() with the coordinates of the segment starting
743 * point. When a closed segment is stroked, there are no caps on the
744 * ends. Instead, there is a line join connecting the final and
745 * initial primitive of the segment.
747 * If @path has no current point before this call, this function will
748 * trigger a warning without other effect.
750 * Since: 1.0
752 void
753 adg_path_close(AdgPath *path)
755 adg_path_append(path, CPML_CLOSE);
759 * adg_path_arc:
760 * @path: an #AdgPath
761 * @center: coordinates of the center of the arc
762 * @r: the radius of the arc
763 * @start: the start angle, in radians
764 * @end: the end angle, in radians
766 * A more usual way to add an arc to @path. After this call, the current
767 * point will be the computed end point of the arc. The arc will be
768 * rendered in increasing angle, accordling to @start and @end. This means
769 * if @start is less than @end, the arc will be rendered in clockwise
770 * direction (accordling to the default cairo coordinate system) while if
771 * @start is greather than @end, the arc will be rendered in couterclockwise
772 * direction.
774 * By explicitely setting the whole arc data, the start point could be
775 * different from the current point. In this case, if @path has no
776 * current point before the call a %CPML_MOVE to the start point of
777 * the arc will be automatically prepended to the arc. If @path has a
778 * current point, a %CPML_LINE to the start point of the arc will be
779 * used instead of the "move to" primitive.
781 * Since: 1.0
783 void
784 adg_path_arc(AdgPath *path, const CpmlPair *center, gdouble r,
785 gdouble start, gdouble end)
787 AdgPathPrivate *data;
788 CpmlPair p[3];
790 g_return_if_fail(ADG_IS_PATH(path));
791 g_return_if_fail(center != NULL);
793 data = path->data;
794 cpml_vector_from_angle(&p[0], start);
795 cpml_vector_from_angle(&p[1], (end-start) / 2);
796 cpml_vector_from_angle(&p[2], end);
798 cpml_vector_set_length(&p[0], r);
799 cpml_vector_set_length(&p[1], r);
800 cpml_vector_set_length(&p[2], r);
802 p[0].x += center->x;
803 p[0].y += center->y;
804 p[1].x += center->x;
805 p[1].y += center->y;
806 p[2].x += center->x;
807 p[2].y += center->y;
809 if (!data->cp_is_valid)
810 adg_path_append(path, CPML_MOVE, &p[0]);
811 else if (p[0].x != data->cp.x || p[0].y != data->cp.y)
812 adg_path_append(path, CPML_LINE, &p[0]);
814 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
818 * adg_path_arc_explicit:
819 * @path: an #AdgPath
820 * @xc: x position of the center of the arc
821 * @yc: y position of the center of the arc
822 * @r: the radius of the arc
823 * @start: the start angle, in radians
824 * @end: the end angle, in radians
826 * Convenient function to call adg_path_arc() using explicit
827 * coordinates instead of #CpmlPair.
829 * Since: 1.0
831 void
832 adg_path_arc_explicit(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
833 gdouble start, gdouble end)
835 CpmlPair center;
837 center.x = xc;
838 center.y = yc;
840 adg_path_arc(path, &center, r, start, end);
844 * adg_path_chamfer:
845 * @path: an #AdgPath
846 * @delta1: the distance from the intersection point of the current primitive
847 * @delta2: the distance from the intersection point of the next primitive
849 * A binary action that generates a chamfer between two primitives.
850 * The first primitive involved is the current primitive, the second will
851 * be the next primitive appended to @path after this call. The second
852 * primitive is required: if the chamfer operation is not properly
853 * terminated (by not providing the second primitive), any API accessing
854 * the path in reading mode will raise a warning.
856 * An exception is a chamfer after a %CPML_CLOSE primitive. In this case,
857 * the second primitive is not required: the current close path is used
858 * as first operand while the first primitive of the current segment is
859 * used as second operand.
861 * The chamfer operation requires two lengths: @delta1 specifies the
862 * "quantity" to trim on the first primitive while @delta2 is the same
863 * applied on the second primitive. The term "quantity" means the length
864 * of the portion to cut out from the original primitive (that is the
865 * primitive as would be without the chamfer).
867 * Since: 1.0
869 void
870 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
872 g_return_if_fail(ADG_IS_PATH(path));
874 if (!_adg_append_operation(path, ADG_ACTION_CHAMFER, delta1, delta2))
875 return;
879 * adg_path_fillet:
880 * @path: an #AdgPath
881 * @radius: the radius of the fillet
883 * A binary action that joins to primitives with an arc.
884 * The first primitive involved is the current primitive, the second will
885 * be the next primitive appended to @path after this call. The second
886 * primitive is required: if the fillet operation is not properly
887 * terminated (by not providing the second primitive), any API accessing
888 * the path in reading mode will raise a warning.
890 * An exception is a fillet after a %CPML_CLOSE primitive. In this case,
891 * the second primitive is not required: the current close path is used
892 * as first operand while the first primitive of the current segment is
893 * used as second operand.
895 * Since: 1.0
897 void
898 adg_path_fillet(AdgPath *path, gdouble radius)
900 g_return_if_fail(ADG_IS_PATH(path));
902 if (!_adg_append_operation(path, ADG_ACTION_FILLET, radius))
903 return;
907 * adg_path_reflect:
908 * @path: an #AdgPath
909 * @vector: (allow-none): the slope of the axis
911 * Reflects the first segment or @path around the axis passing
912 * throught (0, 0) and with a @vector slope. The internal segment
913 * is duplicated and the proper transformation (computed from
914 * @vector) to mirror the segment is applied on all its points.
915 * The result is then reversed with cpml_segment_reverse() and
916 * appended to the original path with adg_path_append_segment().
918 * For convenience, if @vector is <constant>NULL</constant> the
919 * path is reversed around the x axis <constant>(y = 0)</constant>.
921 * Since: 1.0
923 void
924 adg_path_reflect(AdgPath *path, const CpmlVector *vector)
926 AdgModel *model;
927 cairo_matrix_t matrix;
928 CpmlSegment segment, *dup_segment;
930 g_return_if_fail(ADG_IS_PATH(path));
931 g_return_if_fail(vector == NULL || vector->x != 0 || vector->y != 0);
933 model = (AdgModel *) path;
935 if (vector == NULL) {
936 cairo_matrix_init_scale(&matrix, 1, -1);
937 } else {
938 CpmlVector slope;
939 gdouble cos2angle, sin2angle;
941 cpml_pair_copy(&slope, vector);
942 cpml_vector_set_length(&slope, 1);
944 if (slope.x == 0 && slope.y == 0) {
945 g_warning(_("%s: the axis of the reflection is not known"),
946 G_STRLOC);
947 return;
950 sin2angle = 2. * vector->x * vector->y;
951 cos2angle = 2. * vector->x * vector->x - 1;
953 cairo_matrix_init(&matrix, cos2angle, sin2angle,
954 sin2angle, -cos2angle, 0, 0);
957 if (!adg_trail_put_segment((AdgTrail *) path, 1, &segment))
958 return;
960 /* No need to reverse an empty segment */
961 if (segment.num_data == 0 || segment.num_data == 0)
962 return;
964 dup_segment = cpml_segment_deep_dup(&segment);
965 if (dup_segment == NULL)
966 return;
968 cpml_segment_reverse(dup_segment);
969 cpml_segment_transform(dup_segment, &matrix);
970 dup_segment->data[0].header.type = CPML_LINE;
972 adg_path_append_segment(path, dup_segment);
974 g_free(dup_segment);
976 _adg_dup_reverse_named_pairs(model, &matrix);
980 * adg_path_reflect_explicit:
981 * @path: an #AdgPath
982 * @x: the vector x component
983 * @y: the vector y component
985 * Convenient function to call adg_path_reflect() using explicit
986 * vector components instead of #CpmlVector.
988 * Since: 1.0
990 void
991 adg_path_reflect_explicit(AdgPath *path, gdouble x, gdouble y)
993 CpmlVector vector;
995 vector.x = x;
996 vector.y = y;
998 adg_path_reflect(path, &vector);
1002 static void
1003 _adg_clear(AdgModel *model)
1005 AdgPath *path;
1006 AdgPathPrivate *data;
1008 path = (AdgPath *) model;
1009 data = path->data;
1011 g_array_set_size(data->cairo.array, 0);
1012 _adg_clear_operation(path);
1013 _adg_clear_parent(model);
1016 static void
1017 _adg_clear_parent(AdgModel *model)
1019 if (_ADG_OLD_MODEL_CLASS->clear)
1020 _ADG_OLD_MODEL_CLASS->clear(model);
1023 static void
1024 _adg_changed(AdgModel *model)
1026 _adg_clear_parent(model);
1028 if (_ADG_OLD_MODEL_CLASS->changed)
1029 _ADG_OLD_MODEL_CLASS->changed(model);
1032 static cairo_path_t *
1033 _adg_get_cairo_path(AdgTrail *trail)
1035 _adg_clear_parent((AdgModel *) trail);
1036 return _adg_read_cairo_path((AdgPath *) trail);
1039 static cairo_path_t *
1040 _adg_read_cairo_path(AdgPath *path)
1042 AdgPathPrivate *data = path->data;
1043 cairo_path_t *cairo_path = &data->cairo.path;
1044 GArray *array = data->cairo.array;
1046 /* Always regenerate the cairo_path_t as it is a trivial operation */
1047 cairo_path->status = CAIRO_STATUS_SUCCESS;
1048 cairo_path->data = (cairo_path_data_t *) array->data;
1049 cairo_path->num_data = array->len;
1051 return cairo_path;
1054 static gint
1055 _adg_primitive_length(CpmlPrimitiveType type)
1057 switch (type) {
1059 case CPML_CLOSE:
1060 return 1;
1062 case CPML_MOVE:
1063 return 2;
1065 default:
1066 return cpml_primitive_type_get_n_points(type);
1070 static void
1071 _adg_primitive_remap(CpmlPrimitive *primitive, gpointer to,
1072 const CpmlPrimitive *old, gconstpointer from)
1074 primitive->org = REMAPPED(old->org, from, to);
1075 primitive->segment = REMAPPED(old->segment, from, to);
1076 primitive->data = REMAPPED(old->data, from, to);
1079 static void
1080 _adg_rescan(AdgPath *path)
1082 AdgPathPrivate *data;
1083 CpmlSegment segment;
1084 CpmlPrimitive current, *last, *over;
1086 data = path->data;
1087 last = &data->last;
1088 over = &data->over;
1090 last->segment = NULL;
1091 last->org = NULL;
1092 last->data = NULL;
1093 over->segment = NULL;
1094 over->org = NULL;
1095 over->data = NULL;
1097 /* When no data is present, just bail out */
1098 if (! cpml_segment_from_cairo(&segment, _adg_read_cairo_path(path)))
1099 return;
1101 do {
1102 cpml_primitive_from_segment(&current, &segment);
1103 do {
1104 cpml_primitive_copy(over, last);
1105 cpml_primitive_copy(last, &current);
1106 } while (cpml_primitive_next(&current));
1107 } while (cpml_segment_next(&segment));
1110 static void
1111 _adg_append_primitive(AdgPath *path, CpmlPrimitive *current)
1113 AdgPathPrivate *data;
1114 cairo_path_data_t *path_data;
1115 CpmlPrimitiveType type;
1116 int length;
1117 gconstpointer old_data;
1118 gpointer new_data;
1120 data = path->data;
1121 path_data = current->data;
1122 length = path_data->header.length;
1123 type = path_data->header.type;
1125 /* Execute any pending operation */
1126 _adg_do_operation(path, path_data);
1128 /* Append the path data to the internal path array */
1129 old_data = (data->cairo.array)->data;
1130 data->cairo.array = g_array_append_vals(data->cairo.array,
1131 path_data, length);
1132 new_data = (data->cairo.array)->data;
1134 /* Set path data to point to the recently appended cairo_path_data_t
1135 * primitive: the first struct is the header */
1136 path_data = (cairo_path_data_t *) new_data +
1137 (data->cairo.array)->len - length;
1139 if (type == CPML_MOVE) {
1140 /* Remap last and over, but do not change their content */
1141 _adg_primitive_remap(&data->last, new_data, &data->last, old_data);
1142 _adg_primitive_remap(&data->over, new_data, &data->over, old_data);
1143 } else {
1144 /* Store the last primitive into over */
1145 _adg_primitive_remap(&data->over, new_data, &data->last, old_data);
1147 /* Set the last primitive for subsequent binary operations */
1148 /* TODO: the assumption path_data - 1 is the last point is not true
1149 * e.g. when there are embeeded data in primitives */
1150 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
1151 data->last.segment = NULL;
1152 data->last.data = path_data;
1155 data->cp_is_valid = type != CPML_CLOSE;
1156 if (data->cp_is_valid) {
1157 /* Save the last point in the current point */
1158 size_t n = type == CPML_MOVE ? 1 : cpml_primitive_type_get_n_points(type) - 1;
1159 cpml_pair_from_cairo(&data->cp, &path_data[n]);
1162 /* Invalidate cairo_path: should be recomputed */
1163 _adg_clear_parent((AdgModel *) path);
1166 static void
1167 _adg_clear_operation(AdgPath *path)
1169 AdgPathPrivate *data;
1170 AdgOperation *operation;
1172 data = path->data;
1173 operation = &data->operation;
1175 if (operation->action != ADG_ACTION_NONE) {
1176 g_warning(_("%s: a '%s' operation is still active while clearing the path"),
1177 G_STRLOC, _adg_action_name(operation->action));
1178 operation->action = ADG_ACTION_NONE;
1181 data->cp_is_valid = FALSE;
1182 data->last.data = NULL;
1183 data->over.data = NULL;
1186 static gboolean
1187 _adg_append_operation(AdgPath *path, gint action, ...)
1189 AdgPathPrivate *data;
1190 AdgOperation *operation;
1191 AdgAction real_action;
1192 va_list var_args;
1194 real_action = (AdgAction) action;
1195 data = path->data;
1197 if (data->last.data == NULL) {
1198 g_warning(_("%s: requested a '%s' operation on a path without current primitive"),
1199 G_STRLOC, _adg_action_name(real_action));
1200 return FALSE;
1203 operation = &data->operation;
1204 if (operation->action != ADG_ACTION_NONE) {
1205 g_warning(_("%s: requested a '%s' operation while a '%s' operation was active"),
1206 G_STRLOC, _adg_action_name(real_action),
1207 _adg_action_name(operation->action));
1208 /* XXX: http://dev.entidi.com/p/adg/issues/50/ */
1209 return FALSE;
1212 va_start(var_args, action);
1214 switch (real_action) {
1216 case ADG_ACTION_CHAMFER:
1217 operation->data.chamfer.delta1 = va_arg(var_args, double);
1218 operation->data.chamfer.delta2 = va_arg(var_args, double);
1219 break;
1221 case ADG_ACTION_FILLET:
1222 operation->data.fillet.radius = va_arg(var_args, double);
1223 break;
1225 case ADG_ACTION_NONE:
1226 va_end(var_args);
1227 return TRUE;
1229 default:
1230 g_warning(_("%s: %d path operation not recognized"), G_STRLOC, real_action);
1231 va_end(var_args);
1232 return FALSE;
1235 operation->action = real_action;
1236 va_end(var_args);
1238 if (data->last.data[0].header.type == CPML_CLOSE) {
1239 /* Special case: an action with the close primitive should
1240 * be resolved now by changing the close primitive to a
1241 * line-to and using it as second operand and use the first
1242 * primitive of the current segment as first operand */
1243 guint length;
1244 cairo_path_data_t *path_data;
1245 CpmlSegment segment;
1246 CpmlPrimitive current;
1248 length = data->cairo.array->len;
1250 /* Ensure the close path primitive is not the only data */
1251 g_return_val_if_fail(length > 1, FALSE);
1253 /* Allocate one more item once for all to accept the
1254 * conversion from a close to line-to primitive */
1255 data->cairo.array = g_array_set_size(data->cairo.array, length + 1);
1256 path_data = (cairo_path_data_t *) data->cairo.array->data;
1257 --data->cairo.array->len;
1259 /* Set segment and current (the first primitive of segment) */
1260 cpml_segment_from_cairo(&segment, _adg_read_cairo_path(path));
1261 while (cpml_segment_next(&segment))
1263 cpml_primitive_from_segment(&current, &segment);
1265 /* Convert close path to a line-to primitive */
1266 ++data->cairo.array->len;
1267 path_data[length - 1].header.type = CPML_LINE;
1268 path_data[length - 1].header.length = 2;
1269 path_data[length] = *current.org;
1271 data->last.segment = &segment;
1272 data->last.org = &path_data[length - 2];
1273 data->last.data = &path_data[length - 1];
1275 _adg_do_action(path, real_action, &current);
1278 return TRUE;
1281 static void
1282 _adg_do_operation(AdgPath *path, cairo_path_data_t *path_data)
1284 AdgPathPrivate *data;
1285 AdgAction action;
1286 CpmlSegment segment;
1287 CpmlPrimitive current;
1288 cairo_path_data_t current_org;
1290 data = path->data;
1291 action = data->operation.action;
1292 cpml_segment_from_cairo(&segment, _adg_read_cairo_path(path));
1294 /* Construct the current primitive, that is the primitive to be
1295 * mixed with the last primitive with the specified operation.
1296 * Its org is a copy of the end point of the last primitive: it can be
1297 * modified without affecting anything else. It is expected the operation
1298 * functions will add to @path the primitives required but NOT to add
1299 * @current, as this one will be inserted automatically. */
1300 current.segment = &segment;
1301 current.org = &current_org;
1302 current.data = path_data;
1303 cpml_pair_to_cairo(&data->cp, &current_org);
1305 _adg_do_action(path, action, &current);
1308 static void
1309 _adg_do_action(AdgPath *path, AdgAction action, CpmlPrimitive *primitive)
1311 switch (action) {
1312 case ADG_ACTION_NONE:
1313 return;
1314 case ADG_ACTION_CHAMFER:
1315 _adg_do_chamfer(path, primitive);
1316 break;
1317 case ADG_ACTION_FILLET:
1318 _adg_do_fillet(path, primitive);
1319 break;
1320 default:
1321 g_return_if_reached();
1325 static void
1326 _adg_do_chamfer(AdgPath *path, CpmlPrimitive *current)
1328 AdgPathPrivate *data;
1329 CpmlPrimitive *last;
1330 gdouble delta1, delta2;
1331 gdouble len1, len2;
1332 CpmlPair pair;
1334 data = path->data;
1335 last = &data->last;
1336 delta1 = data->operation.data.chamfer.delta1;
1337 len1 = cpml_primitive_get_length(last);
1339 if (delta1 >= len1) {
1340 g_warning(_("%s: first chamfer delta of %lf is greather than the available %lf length"),
1341 G_STRLOC, delta1, len1);
1342 return;
1345 delta2 = data->operation.data.chamfer.delta2;
1346 len2 = cpml_primitive_get_length(current);
1348 if (delta2 >= len2) {
1349 g_warning(_("%s: second chamfer delta of %lf is greather than the available %lf length"),
1350 G_STRLOC, delta1, len1);
1351 return;
1354 /* Change the end point of the last primitive */
1355 cpml_primitive_put_pair_at(last, 1. - delta1 / len1, &pair);
1356 cpml_primitive_set_point(last, -1, &pair);
1358 /* Change the start point of the current primitive */
1359 cpml_primitive_put_pair_at(current, delta2 / len2, &pair);
1360 cpml_primitive_set_point(current, 0, &pair);
1362 /* Add the chamfer line */
1363 data->operation.action = ADG_ACTION_NONE;
1364 adg_path_append(path, CPML_LINE, &pair);
1367 static void
1368 _adg_do_fillet(AdgPath *path, CpmlPrimitive *current)
1370 AdgPathPrivate *data;
1371 CpmlPrimitive *last, *current_dup, *last_dup;
1372 gdouble radius, offset, pos;
1373 CpmlPair center, vector, p[3];
1375 data = path->data;
1376 last = &data->last;
1377 current_dup = cpml_primitive_deep_dup(current);
1378 last_dup = cpml_primitive_deep_dup(last);
1379 radius = data->operation.data.fillet.radius;
1380 offset = _adg_is_convex(last_dup, current_dup) ? -radius : radius;
1382 /* Find the center of the fillet from the intersection between
1383 * the last and current primitives offseted by radius */
1384 cpml_primitive_offset(current_dup, offset);
1385 cpml_primitive_offset(last_dup, offset);
1386 if (cpml_primitive_put_intersections(current_dup, last_dup, 1, &center) == 0) {
1387 g_warning(_("%s: fillet with radius of %lf is not applicable here"),
1388 G_STRLOC, radius);
1389 g_free(current_dup);
1390 g_free(last_dup);
1391 return;
1394 /* Compute the start point of the fillet */
1395 pos = cpml_primitive_get_closest_pos(last_dup, &center);
1396 cpml_primitive_put_vector_at(last_dup, pos, &vector);
1397 cpml_vector_set_length(&vector, offset);
1398 cpml_vector_normal(&vector);
1399 p[0].x = center.x - vector.x;
1400 p[0].y = center.y - vector.y;
1402 /* Compute the mid point of the fillet */
1403 cpml_pair_from_cairo(&vector, current->org);
1404 vector.x -= center.x;
1405 vector.y -= center.y;
1406 cpml_vector_set_length(&vector, radius);
1407 p[1].x = center.x + vector.x;
1408 p[1].y = center.y + vector.y;
1410 /* Compute the end point of the fillet */
1411 pos = cpml_primitive_get_closest_pos(current_dup, &center);
1412 cpml_primitive_put_vector_at(current_dup, pos, &vector);
1413 cpml_vector_set_length(&vector, offset);
1414 cpml_vector_normal(&vector);
1415 p[2].x = center.x - vector.x;
1416 p[2].y = center.y - vector.y;
1418 g_free(current_dup);
1419 g_free(last_dup);
1421 /* Change the end point of the last primitive */
1422 cpml_primitive_set_point(last, -1, &p[0]);
1424 /* Change the start point of the current primitive */
1425 cpml_primitive_set_point(current, 0, &p[2]);
1427 /* Add the fillet arc */
1428 data->operation.action = ADG_ACTION_NONE;
1429 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
1432 static gboolean
1433 _adg_is_convex(const CpmlPrimitive *primitive1, const CpmlPrimitive *primitive2)
1435 CpmlVector v1, v2;
1436 gdouble angle1, angle2;
1438 cpml_primitive_put_vector_at(primitive1, -1, &v1);
1439 cpml_primitive_put_vector_at(primitive2, 0, &v2);
1441 /* Probably there is a smarter way to get this without trygonometry */
1442 angle1 = cpml_vector_angle(&v1);
1443 angle2 = cpml_vector_angle(&v2);
1445 if (angle1 > angle2)
1446 angle1 -= G_PI*2;
1448 return angle2-angle1 > G_PI;
1451 static const gchar *
1452 _adg_action_name(AdgAction action)
1454 switch (action) {
1455 case ADG_ACTION_NONE:
1456 return "NULL";
1457 case ADG_ACTION_CHAMFER:
1458 return "CHAMFER";
1459 case ADG_ACTION_FILLET:
1460 return "FILLET";
1463 return "undefined";
1466 static void
1467 _adg_get_named_pair(AdgModel *model, const gchar *name,
1468 CpmlPair *pair, gpointer user_data)
1470 GSList **named_pairs;
1471 AdgNamedPair *named_pair;
1473 named_pairs = user_data;
1475 named_pair = g_new(AdgNamedPair, 1);
1476 named_pair->name = name;
1477 named_pair->pair = *pair;
1479 *named_pairs = g_slist_prepend(*named_pairs, named_pair);
1482 static void
1483 _adg_dup_reverse_named_pairs(AdgModel *model, const cairo_matrix_t *matrix)
1485 AdgNamedPair *old_named_pair;
1486 AdgNamedPair named_pair;
1487 GSList *named_pairs;
1489 /* Populate named_pairs with all the named pairs of model */
1490 named_pairs = NULL;
1491 adg_model_foreach_named_pair(model, _adg_get_named_pair, &named_pairs);
1493 /* Readd the pairs applying the reversing transformation matrix to
1494 * their coordinates and prepending a "-" to their name */
1495 while (named_pairs) {
1496 old_named_pair = named_pairs->data;
1498 named_pair.name = g_strdup_printf("-%s", old_named_pair->name);
1499 named_pair.pair = old_named_pair->pair;
1500 cpml_pair_transform(&named_pair.pair, matrix);
1502 adg_model_set_named_pair(model, named_pair.name, &named_pair.pair);
1504 g_free((gpointer) named_pair.name);
1505 named_pairs = g_slist_delete_link(named_pairs, named_pairs);