adg: refactor AdgPath code for calculating primitive length
[adg.git] / src / adg / adg-path.c
blobfc8933cb32a7a73e33054319a0ea0050ef0b41f5
1 /* ADG - Automatic Drawing Generation
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: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) ((to) + ((gchar *) (ptr) - (gchar *) (from)))
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 GArray *array;
367 CpmlPair *pair;
368 gint length;
370 length = _adg_primitive_length(type);
371 if (length == 0)
372 return;
374 array = g_array_new(TRUE, FALSE, sizeof(pair));
375 while (-- length) {
376 pair = va_arg(var_args, CpmlPair *);
377 if (pair == NULL) {
378 g_array_free(array, TRUE);
379 g_return_if_reached();
380 return;
382 g_array_append_val(array, pair);
385 adg_path_append_array(path, type, (const CpmlPair **) array->data);
386 g_array_free(array, TRUE);
390 * adg_path_append_array: (rename-to adg_path_append)
391 * @path: an #AdgPath
392 * @type: a #cairo_data_type_t value
393 * @pairs: (array zero-terminated=1) (element-type Cpml.Pair) (transfer none): point data, specified as a <constant>NULL</constant> terminated array of #CpmlPair pointers.
395 * A bindingable version of adg_path_append() that uses a
396 * <constant>NULL</constant> terminated array of pairs instead of variable
397 * argument list and friends.
399 * Furthermore, because of the list is <constant>NULL</constant> terminated,
400 * an arbitrary number of pairs can be passed in @pairs. This allows to embed
401 * in a primitive element more data pairs than requested, something impossible
402 * to do with adg_path_append() and adg_path_append_valist().
404 * Since: 1.0
406 void
407 adg_path_append_array(AdgPath *path, CpmlPrimitiveType type,
408 const CpmlPair **pairs)
410 gint length;
411 GArray *array;
412 const CpmlPair **pair;
413 cairo_path_data_t path_data;
415 g_return_if_fail(ADG_IS_PATH(path));
416 g_return_if_fail(pairs != NULL);
418 length = _adg_primitive_length(type);
419 if (length == 0)
420 return;
422 array = g_array_new(FALSE, FALSE, sizeof(path_data));
423 for (pair = pairs; *pair != NULL; ++ pair) {
424 cpml_pair_to_cairo(*pair, &path_data);
425 g_array_append_val(array, path_data);
428 if (array->len < length - 1) {
429 /* Not enough pairs have been provided */
430 g_warning(_("%s: null pair caught while parsing arguments"), G_STRLOC);
431 } else {
432 AdgPathPrivate *data;
433 CpmlPrimitive primitive;
434 cairo_path_data_t org;
436 /* Save a copy of the current point as primitive origin */
437 data = path->data;
438 cpml_pair_to_cairo(&data->cp, &org);
440 /* Prepend the cairo header */
441 path_data.header.type = type;
442 path_data.header.length = array->len + 1;
443 g_array_prepend_val(array, path_data);
445 /* Append a new primitive to @path */
446 primitive.segment = NULL;
447 primitive.org = &org;
448 primitive.data = (cairo_path_data_t *) array->data;
449 _adg_append_primitive(path, &primitive);
452 g_array_free(array, TRUE);
457 * adg_path_append_primitive:
458 * @path: an #AdgPath
459 * @primitive: the #CpmlPrimitive to append
461 * Appends @primitive to @path. The primitive to add is considered the
462 * continuation of the current path so the <structfield>org</structfield>
463 * component of @primitive is not used. Anyway the current point is
464 * checked against it: they must be equal or the function will fail
465 * without further processing.
467 * Since: 1.0
469 void
470 adg_path_append_primitive(AdgPath *path, const CpmlPrimitive *primitive)
472 AdgPathPrivate *data;
473 CpmlPrimitive *primitive_dup;
475 g_return_if_fail(ADG_IS_PATH(path));
476 g_return_if_fail(primitive != NULL);
477 g_return_if_fail(primitive->org != NULL);
478 g_return_if_fail(primitive->data != NULL);
480 data = path->data;
482 g_return_if_fail(primitive->org->point.x == data->cp.x &&
483 primitive->org->point.y == data->cp.y);
485 /* The primitive data could be modified by pending operations:
486 * work on a copy */
487 primitive_dup = cpml_primitive_deep_dup(primitive);
489 _adg_append_primitive(path, primitive_dup);
491 g_free(primitive_dup);
495 * adg_path_append_segment:
496 * @path: an #AdgPath
497 * @segment: the #CpmlSegment to append
499 * Appends @segment to @path.
501 * Since: 1.0
503 void
504 adg_path_append_segment(AdgPath *path, const CpmlSegment *segment)
506 g_return_if_fail(ADG_IS_PATH(path));
507 g_return_if_fail(segment != NULL);
509 if (segment->num_data > 0) {
510 AdgPathPrivate *data;
512 g_return_if_fail(segment->data != NULL);
514 data = path->data;
516 _adg_clear_parent((AdgModel *) path);
517 data->cairo.array = g_array_append_vals(data->cairo.array,
518 segment->data, segment->num_data);
519 _adg_rescan(path);
524 * adg_path_append_cairo_path:
525 * @path: an #AdgPath
526 * @cairo_path: (type gpointer): the #cairo_path_t path to append
528 * Appends a whole #cairo_path_t to @path.
530 * Since: 1.0
532 void
533 adg_path_append_cairo_path(AdgPath *path, const cairo_path_t *cairo_path)
535 AdgPathPrivate *data;
537 g_return_if_fail(ADG_IS_PATH(path));
538 g_return_if_fail(cairo_path != NULL);
540 data = path->data;
542 _adg_clear_parent((AdgModel *) path);
543 data->cairo.array = g_array_append_vals(data->cairo.array,
544 cairo_path->data,
545 cairo_path->num_data);
546 _adg_rescan(path);
550 * adg_path_move_to:
551 * @path: an #AdgPath
552 * @pair: the destination coordinates
554 * Begins a new segment. After this call the current point will be @pair.
556 * Since: 1.0
558 void
559 adg_path_move_to(AdgPath *path, const CpmlPair *pair)
561 adg_path_append(path, CPML_MOVE, pair);
565 * adg_path_move_to_explicit:
566 * @path: an #AdgPath
567 * @x: the new x coordinate
568 * @y: the new y coordinate
570 * Convenient function to call adg_path_move_to() using explicit
571 * coordinates instead of #CpmlPair.
573 * Since: 1.0
575 void
576 adg_path_move_to_explicit(AdgPath *path, gdouble x, gdouble y)
578 CpmlPair p;
580 p.x = x;
581 p.y = y;
583 adg_path_append(path, CPML_MOVE, &p);
587 * adg_path_line_to:
588 * @path: an #AdgPath
589 * @pair: the destination coordinates
591 * Adds a line to @path from the current point to @pair. After this
592 * call the current point will be @pair.
594 * If @path has no current point before this call, this function will
595 * trigger a warning without other effect.
597 * Since: 1.0
599 void
600 adg_path_line_to(AdgPath *path, const CpmlPair *pair)
602 adg_path_append(path, CPML_LINE, pair);
606 * adg_path_line_to_explicit:
607 * @path: an #AdgPath
608 * @x: the new x coordinate
609 * @y: the new y coordinate
611 * Convenient function to call adg_path_line_to() using explicit
612 * coordinates instead of #CpmlPair.
614 * Since: 1.0
616 void
617 adg_path_line_to_explicit(AdgPath *path, gdouble x, gdouble y)
619 CpmlPair p;
621 p.x = x;
622 p.y = y;
624 adg_path_append(path, CPML_LINE, &p);
628 * adg_path_arc_to:
629 * @path: an #AdgPath
630 * @throught: an arbitrary point on the arc
631 * @pair: the destination coordinates
633 * Adds an arc to the path from the current point to @pair, passing
634 * throught @throught. After this call the current point will be @pair.
636 * If @path has no current point before this call, this function will
637 * trigger a warning without other effect.
639 * Since: 1.0
641 void
642 adg_path_arc_to(AdgPath *path, const CpmlPair *throught, const CpmlPair *pair)
644 adg_path_append(path, CPML_ARC, throught, pair);
648 * adg_path_arc_to_explicit:
649 * @path: an #AdgPath
650 * @x1: the x coordinate of an intermediate point
651 * @y1: the y coordinate of an intermediate point
652 * @x2: the x coordinate of the end of the arc
653 * @y2: the y coordinate of the end of the arc
655 * Convenient function to call adg_path_arc_to() using explicit
656 * coordinates instead of #CpmlPair.
658 * Since: 1.0
660 void
661 adg_path_arc_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
662 gdouble x2, gdouble y2)
664 CpmlPair p[2];
666 p[0].x = x1;
667 p[0].y = y1;
668 p[1].x = x2;
669 p[1].y = y2;
671 adg_path_append(path, CPML_ARC, &p[0], &p[1]);
675 * adg_path_curve_to:
676 * @path: an #AdgPath
677 * @control1: the first control point of the curve
678 * @control2: the second control point of the curve
679 * @pair: the destination coordinates
681 * Adds a cubic Bézier curve to the path from the current point to
682 * position @pair, using @control1 and @control2 as control points.
683 * After this call the current point will be @pair.
685 * If @path has no current point before this call, this function will
686 * trigger a warning without other effect.
688 * Since: 1.0
690 void
691 adg_path_curve_to(AdgPath *path, const CpmlPair *control1,
692 const CpmlPair *control2, const CpmlPair *pair)
694 adg_path_append(path, CPML_CURVE, control1, control2, pair);
698 * adg_path_curve_to_explicit:
699 * @path: an #AdgPath
700 * @x1: the x coordinate of the first control point
701 * @y1: the y coordinate of the first control point
702 * @x2: the x coordinate of the second control point
703 * @y2: the y coordinate of the second control point
704 * @x3: the x coordinate of the end of the curve
705 * @y3: the y coordinate of the end of the curve
707 * Convenient function to call adg_path_curve_to() using explicit
708 * coordinates instead of #CpmlPair.
710 * Since: 1.0
712 void
713 adg_path_curve_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
714 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
716 CpmlPair p[3];
718 p[0].x = x1;
719 p[0].y = y1;
720 p[1].x = x2;
721 p[1].y = y2;
722 p[2].x = x3;
723 p[2].y = y3;
725 adg_path_append(path, CPML_CURVE, &p[0], &p[1], &p[2]);
729 * adg_path_close:
730 * @path: an #AdgPath
732 * Adds a line segment to the path from the current point to the
733 * beginning of the current segment, (the most recent point passed
734 * to an adg_path_move_to()), and closes this segment.
735 * After this call the current point will be unset.
737 * The behavior of adg_path_close() is distinct from simply calling
738 * adg_path_line_to() with the coordinates of the segment starting
739 * point. When a closed segment is stroked, there are no caps on the
740 * ends. Instead, there is a line join connecting the final and
741 * initial primitive of the segment.
743 * If @path has no current point before this call, this function will
744 * trigger a warning without other effect.
746 * Since: 1.0
748 void
749 adg_path_close(AdgPath *path)
751 adg_path_append(path, CPML_CLOSE);
755 * adg_path_arc:
756 * @path: an #AdgPath
757 * @center: coordinates of the center of the arc
758 * @r: the radius of the arc
759 * @start: the start angle, in radians
760 * @end: the end angle, in radians
762 * A more usual way to add an arc to @path. After this call, the current
763 * point will be the computed end point of the arc. The arc will be
764 * rendered in increasing angle, accordling to @start and @end. This means
765 * if @start is less than @end, the arc will be rendered in clockwise
766 * direction (accordling to the default cairo coordinate system) while if
767 * @start is greather than @end, the arc will be rendered in couterclockwise
768 * direction.
770 * By explicitely setting the whole arc data, the start point could be
771 * different from the current point. In this case, if @path has no
772 * current point before the call a %CPML_MOVE to the start point of
773 * the arc will be automatically prepended to the arc. If @path has a
774 * current point, a %CPML_LINE to the start point of the arc will be
775 * used instead of the "move to" primitive.
777 * Since: 1.0
779 void
780 adg_path_arc(AdgPath *path, const CpmlPair *center, gdouble r,
781 gdouble start, gdouble end)
783 AdgPathPrivate *data;
784 CpmlPair p[3];
786 g_return_if_fail(ADG_IS_PATH(path));
787 g_return_if_fail(center != NULL);
789 data = path->data;
790 cpml_vector_from_angle(&p[0], start);
791 cpml_vector_from_angle(&p[1], (end-start) / 2);
792 cpml_vector_from_angle(&p[2], end);
794 cpml_vector_set_length(&p[0], r);
795 cpml_vector_set_length(&p[1], r);
796 cpml_vector_set_length(&p[2], r);
798 p[0].x += center->x;
799 p[0].y += center->y;
800 p[1].x += center->x;
801 p[1].y += center->y;
802 p[2].x += center->x;
803 p[2].y += center->y;
805 if (!data->cp_is_valid)
806 adg_path_append(path, CPML_MOVE, &p[0]);
807 else if (p[0].x != data->cp.x || p[0].y != data->cp.y)
808 adg_path_append(path, CPML_LINE, &p[0]);
810 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
814 * adg_path_arc_explicit:
815 * @path: an #AdgPath
816 * @xc: x position of the center of the arc
817 * @yc: y position of the center of the arc
818 * @r: the radius of the arc
819 * @start: the start angle, in radians
820 * @end: the end angle, in radians
822 * Convenient function to call adg_path_arc() using explicit
823 * coordinates instead of #CpmlPair.
825 * Since: 1.0
827 void
828 adg_path_arc_explicit(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
829 gdouble start, gdouble end)
831 CpmlPair center;
833 center.x = xc;
834 center.y = yc;
836 adg_path_arc(path, &center, r, start, end);
840 * adg_path_chamfer:
841 * @path: an #AdgPath
842 * @delta1: the distance from the intersection point of the current primitive
843 * @delta2: the distance from the intersection point of the next primitive
845 * A binary action that generates a chamfer between two primitives.
846 * The first primitive involved is the current primitive, the second will
847 * be the next primitive appended to @path after this call. The second
848 * primitive is required: if the chamfer operation is not properly
849 * terminated (by not providing the second primitive), any API accessing
850 * the path in reading mode will raise a warning.
852 * An exception is a chamfer after a %CPML_CLOSE primitive. In this case,
853 * the second primitive is not required: the current close path is used
854 * as first operand while the first primitive of the current segment is
855 * used as second operand.
857 * The chamfer operation requires two lengths: @delta1 specifies the
858 * "quantity" to trim on the first primitive while @delta2 is the same
859 * applied on the second primitive. The term "quantity" means the length
860 * of the portion to cut out from the original primitive (that is the
861 * primitive as would be without the chamfer).
863 * Since: 1.0
865 void
866 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
868 g_return_if_fail(ADG_IS_PATH(path));
870 if (!_adg_append_operation(path, ADG_ACTION_CHAMFER, delta1, delta2))
871 return;
875 * adg_path_fillet:
876 * @path: an #AdgPath
877 * @radius: the radius of the fillet
879 * A binary action that joins to primitives with an arc.
880 * The first primitive involved is the current primitive, the second will
881 * be the next primitive appended to @path after this call. The second
882 * primitive is required: if the fillet operation is not properly
883 * terminated (by not providing the second primitive), any API accessing
884 * the path in reading mode will raise a warning.
886 * An exception is a fillet after a %CPML_CLOSE primitive. In this case,
887 * the second primitive is not required: the current close path is used
888 * as first operand while the first primitive of the current segment is
889 * used as second operand.
891 * Since: 1.0
893 void
894 adg_path_fillet(AdgPath *path, gdouble radius)
896 g_return_if_fail(ADG_IS_PATH(path));
898 if (!_adg_append_operation(path, ADG_ACTION_FILLET, radius))
899 return;
903 * adg_path_reflect:
904 * @path: an #AdgPath
905 * @vector: (allow-none): the slope of the axis
907 * Reflects the first segment or @path around the axis passing
908 * throught (0, 0) and with a @vector slope. The internal segment
909 * is duplicated and the proper transformation (computed from
910 * @vector) to mirror the segment is applied on all its points.
911 * The result is then reversed with cpml_segment_reverse() and
912 * appended to the original path with adg_path_append_segment().
914 * For convenience, if @vector is <constant>NULL</constant> the
915 * path is reversed around the x axis <constant>(y = 0)</constant>.
917 * Since: 1.0
919 void
920 adg_path_reflect(AdgPath *path, const CpmlVector *vector)
922 AdgModel *model;
923 cairo_matrix_t matrix;
924 CpmlSegment segment, *dup_segment;
926 g_return_if_fail(ADG_IS_PATH(path));
927 g_return_if_fail(vector == NULL || vector->x != 0 || vector->y != 0);
929 model = (AdgModel *) path;
931 if (vector == NULL) {
932 cairo_matrix_init_scale(&matrix, 1, -1);
933 } else {
934 CpmlVector slope;
935 gdouble cos2angle, sin2angle;
937 cpml_pair_copy(&slope, vector);
938 cpml_vector_set_length(&slope, 1);
940 if (slope.x == 0 && slope.y == 0) {
941 g_warning(_("%s: the axis of the reflection is not known"),
942 G_STRLOC);
943 return;
946 sin2angle = 2. * vector->x * vector->y;
947 cos2angle = 2. * vector->x * vector->x - 1;
949 cairo_matrix_init(&matrix, cos2angle, sin2angle,
950 sin2angle, -cos2angle, 0, 0);
953 if (!adg_trail_put_segment((AdgTrail *) path, 1, &segment))
954 return;
956 /* No need to reverse an empty segment */
957 if (segment.num_data == 0 || segment.num_data == 0)
958 return;
960 dup_segment = cpml_segment_deep_dup(&segment);
961 if (dup_segment == NULL)
962 return;
964 cpml_segment_reverse(dup_segment);
965 cpml_segment_transform(dup_segment, &matrix);
966 dup_segment->data[0].header.type = CPML_LINE;
968 adg_path_append_segment(path, dup_segment);
970 g_free(dup_segment);
972 _adg_dup_reverse_named_pairs(model, &matrix);
976 * adg_path_reflect_explicit:
977 * @path: an #AdgPath
978 * @x: the vector x component
979 * @y: the vector y component
981 * Convenient function to call adg_path_reflect() using explicit
982 * vector components instead of #CpmlVector.
984 * Since: 1.0
986 void
987 adg_path_reflect_explicit(AdgPath *path, gdouble x, gdouble y)
989 CpmlVector vector;
991 vector.x = x;
992 vector.y = y;
994 adg_path_reflect(path, &vector);
998 static void
999 _adg_clear(AdgModel *model)
1001 AdgPath *path;
1002 AdgPathPrivate *data;
1004 path = (AdgPath *) model;
1005 data = path->data;
1007 g_array_set_size(data->cairo.array, 0);
1008 _adg_clear_operation(path);
1009 _adg_clear_parent(model);
1012 static void
1013 _adg_clear_parent(AdgModel *model)
1015 if (_ADG_OLD_MODEL_CLASS->clear)
1016 _ADG_OLD_MODEL_CLASS->clear(model);
1019 static void
1020 _adg_changed(AdgModel *model)
1022 _adg_clear_parent(model);
1024 if (_ADG_OLD_MODEL_CLASS->changed)
1025 _ADG_OLD_MODEL_CLASS->changed(model);
1028 static cairo_path_t *
1029 _adg_get_cairo_path(AdgTrail *trail)
1031 _adg_clear_parent((AdgModel *) trail);
1032 return _adg_read_cairo_path((AdgPath *) trail);
1035 static cairo_path_t *
1036 _adg_read_cairo_path(AdgPath *path)
1038 AdgPathPrivate *data = path->data;
1039 cairo_path_t *cairo_path = &data->cairo.path;
1040 GArray *array = data->cairo.array;
1042 /* Always regenerate the cairo_path_t as it is a trivial operation */
1043 cairo_path->status = CAIRO_STATUS_SUCCESS;
1044 cairo_path->data = (cairo_path_data_t *) array->data;
1045 cairo_path->num_data = array->len;
1047 return cairo_path;
1050 static gint
1051 _adg_primitive_length(CpmlPrimitiveType type)
1053 switch (type) {
1055 case CPML_CLOSE:
1056 return 1;
1058 case CPML_MOVE:
1059 return 2;
1061 default:
1062 return cpml_primitive_type_get_n_points(type);
1066 static void
1067 _adg_primitive_remap(CpmlPrimitive *primitive, gpointer to,
1068 const CpmlPrimitive *old, gconstpointer from)
1070 primitive->org = REMAPPED(old->org, from, to);
1071 primitive->segment = REMAPPED(old->segment, from, to);
1072 primitive->data = REMAPPED(old->data, from, to);
1075 static void
1076 _adg_rescan(AdgPath *path)
1078 AdgPathPrivate *data;
1079 CpmlSegment segment;
1080 CpmlPrimitive current, *last, *over;
1082 data = path->data;
1083 last = &data->last;
1084 over = &data->over;
1086 last->segment = NULL;
1087 last->org = NULL;
1088 last->data = NULL;
1089 over->segment = NULL;
1090 over->org = NULL;
1091 over->data = NULL;
1093 /* When no data is present, just bail out */
1094 if (! cpml_segment_from_cairo(&segment, _adg_read_cairo_path(path)))
1095 return;
1097 do {
1098 cpml_primitive_from_segment(&current, &segment);
1099 do {
1100 cpml_primitive_copy(over, last);
1101 cpml_primitive_copy(last, &current);
1102 } while (cpml_primitive_next(&current));
1103 } while (cpml_segment_next(&segment));
1106 static void
1107 _adg_append_primitive(AdgPath *path, CpmlPrimitive *current)
1109 AdgPathPrivate *data;
1110 cairo_path_data_t *path_data;
1111 CpmlPrimitiveType type;
1112 int length;
1113 gconstpointer old_data;
1114 gpointer new_data;
1116 data = path->data;
1117 path_data = current->data;
1118 length = path_data->header.length;
1119 type = path_data->header.type;
1121 /* Execute any pending operation */
1122 _adg_do_operation(path, path_data);
1124 /* Append the path data to the internal path array */
1125 old_data = (data->cairo.array)->data;
1126 data->cairo.array = g_array_append_vals(data->cairo.array,
1127 path_data, length);
1128 new_data = (data->cairo.array)->data;
1130 /* Set path data to point to the recently appended cairo_path_data_t
1131 * primitive: the first struct is the header */
1132 path_data = (cairo_path_data_t *) new_data +
1133 (data->cairo.array)->len - length;
1135 if (type == CPML_MOVE) {
1136 /* Remap last and over, but do not change their content */
1137 _adg_primitive_remap(&data->last, new_data, &data->last, old_data);
1138 _adg_primitive_remap(&data->over, new_data, &data->over, old_data);
1139 } else {
1140 /* Store the last primitive into over */
1141 _adg_primitive_remap(&data->over, new_data, &data->last, old_data);
1143 /* Set the last primitive for subsequent binary operations */
1144 /* TODO: the assumption path_data - 1 is the last point is not true
1145 * e.g. when there are embeeded data in primitives */
1146 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
1147 data->last.segment = NULL;
1148 data->last.data = path_data;
1151 data->cp_is_valid = type != CPML_CLOSE;
1152 if (data->cp_is_valid) {
1153 /* Save the last point in the current point */
1154 size_t n = type == CPML_MOVE ? 1 : cpml_primitive_type_get_n_points(type) - 1;
1155 cpml_pair_from_cairo(&data->cp, &path_data[n]);
1158 /* Invalidate cairo_path: should be recomputed */
1159 _adg_clear_parent((AdgModel *) path);
1162 static void
1163 _adg_clear_operation(AdgPath *path)
1165 AdgPathPrivate *data;
1166 AdgOperation *operation;
1168 data = path->data;
1169 operation = &data->operation;
1171 if (operation->action != ADG_ACTION_NONE) {
1172 g_warning(_("%s: a '%s' operation is still active while clearing the path"),
1173 G_STRLOC, _adg_action_name(operation->action));
1174 operation->action = ADG_ACTION_NONE;
1177 data->cp_is_valid = FALSE;
1178 data->last.data = NULL;
1179 data->over.data = NULL;
1182 static gboolean
1183 _adg_append_operation(AdgPath *path, gint action, ...)
1185 AdgPathPrivate *data;
1186 AdgOperation *operation;
1187 AdgAction real_action;
1188 va_list var_args;
1190 real_action = (AdgAction) action;
1191 data = path->data;
1193 if (data->last.data == NULL) {
1194 g_warning(_("%s: requested a '%s' operation on a path without current primitive"),
1195 G_STRLOC, _adg_action_name(real_action));
1196 return FALSE;
1199 operation = &data->operation;
1200 if (operation->action != ADG_ACTION_NONE) {
1201 g_warning(_("%s: requested a '%s' operation while a '%s' operation was active"),
1202 G_STRLOC, _adg_action_name(real_action),
1203 _adg_action_name(operation->action));
1204 /* XXX: http://dev.entidi.com/p/adg/issues/50/ */
1205 return FALSE;
1208 va_start(var_args, action);
1210 switch (real_action) {
1212 case ADG_ACTION_CHAMFER:
1213 operation->data.chamfer.delta1 = va_arg(var_args, double);
1214 operation->data.chamfer.delta2 = va_arg(var_args, double);
1215 break;
1217 case ADG_ACTION_FILLET:
1218 operation->data.fillet.radius = va_arg(var_args, double);
1219 break;
1221 case ADG_ACTION_NONE:
1222 va_end(var_args);
1223 return TRUE;
1225 default:
1226 g_warning(_("%s: %d path operation not recognized"), G_STRLOC, real_action);
1227 va_end(var_args);
1228 return FALSE;
1231 operation->action = real_action;
1232 va_end(var_args);
1234 if (data->last.data[0].header.type == CPML_CLOSE) {
1235 /* Special case: an action with the close primitive should
1236 * be resolved now by changing the close primitive to a
1237 * line-to and using it as second operand and use the first
1238 * primitive of the current segment as first operand */
1239 guint length;
1240 cairo_path_data_t *path_data;
1241 CpmlSegment segment;
1242 CpmlPrimitive current;
1244 length = data->cairo.array->len;
1246 /* Ensure the close path primitive is not the only data */
1247 g_return_val_if_fail(length > 1, FALSE);
1249 /* Allocate one more item once for all to accept the
1250 * conversion from a close to line-to primitive */
1251 data->cairo.array = g_array_set_size(data->cairo.array, length + 1);
1252 path_data = (cairo_path_data_t *) data->cairo.array->data;
1253 --data->cairo.array->len;
1255 /* Set segment and current (the first primitive of segment) */
1256 cpml_segment_from_cairo(&segment, _adg_read_cairo_path(path));
1257 while (cpml_segment_next(&segment))
1259 cpml_primitive_from_segment(&current, &segment);
1261 /* Convert close path to a line-to primitive */
1262 ++data->cairo.array->len;
1263 path_data[length - 1].header.type = CPML_LINE;
1264 path_data[length - 1].header.length = 2;
1265 path_data[length] = *current.org;
1267 data->last.segment = &segment;
1268 data->last.org = &path_data[length - 2];
1269 data->last.data = &path_data[length - 1];
1271 _adg_do_action(path, real_action, &current);
1274 return TRUE;
1277 static void
1278 _adg_do_operation(AdgPath *path, cairo_path_data_t *path_data)
1280 AdgPathPrivate *data;
1281 AdgAction action;
1282 CpmlSegment segment;
1283 CpmlPrimitive current;
1284 cairo_path_data_t current_org;
1286 data = path->data;
1287 action = data->operation.action;
1288 cpml_segment_from_cairo(&segment, _adg_read_cairo_path(path));
1290 /* Construct the current primitive, that is the primitive to be
1291 * mixed with the last primitive with the specified operation.
1292 * Its org is a copy of the end point of the last primitive: it can be
1293 * modified without affecting anything else. It is expected the operation
1294 * functions will add to @path the primitives required but NOT to add
1295 * @current, as this one will be inserted automatically. */
1296 current.segment = &segment;
1297 current.org = &current_org;
1298 current.data = path_data;
1299 cpml_pair_to_cairo(&data->cp, &current_org);
1301 _adg_do_action(path, action, &current);
1304 static void
1305 _adg_do_action(AdgPath *path, AdgAction action, CpmlPrimitive *primitive)
1307 switch (action) {
1308 case ADG_ACTION_NONE:
1309 return;
1310 case ADG_ACTION_CHAMFER:
1311 _adg_do_chamfer(path, primitive);
1312 break;
1313 case ADG_ACTION_FILLET:
1314 _adg_do_fillet(path, primitive);
1315 break;
1316 default:
1317 g_return_if_reached();
1321 static void
1322 _adg_do_chamfer(AdgPath *path, CpmlPrimitive *current)
1324 AdgPathPrivate *data;
1325 CpmlPrimitive *last;
1326 gdouble delta1, delta2;
1327 gdouble len1, len2;
1328 CpmlPair pair;
1330 data = path->data;
1331 last = &data->last;
1332 delta1 = data->operation.data.chamfer.delta1;
1333 len1 = cpml_primitive_get_length(last);
1335 if (delta1 >= len1) {
1336 g_warning(_("%s: first chamfer delta of %lf is greather than the available %lf length"),
1337 G_STRLOC, delta1, len1);
1338 return;
1341 delta2 = data->operation.data.chamfer.delta2;
1342 len2 = cpml_primitive_get_length(current);
1344 if (delta2 >= len2) {
1345 g_warning(_("%s: second chamfer delta of %lf is greather than the available %lf length"),
1346 G_STRLOC, delta1, len1);
1347 return;
1350 /* Change the end point of the last primitive */
1351 cpml_primitive_put_pair_at(last, 1. - delta1 / len1, &pair);
1352 cpml_primitive_set_point(last, -1, &pair);
1354 /* Change the start point of the current primitive */
1355 cpml_primitive_put_pair_at(current, delta2 / len2, &pair);
1356 cpml_primitive_set_point(current, 0, &pair);
1358 /* Add the chamfer line */
1359 data->operation.action = ADG_ACTION_NONE;
1360 adg_path_append(path, CPML_LINE, &pair);
1363 static void
1364 _adg_do_fillet(AdgPath *path, CpmlPrimitive *current)
1366 AdgPathPrivate *data;
1367 CpmlPrimitive *last, *current_dup, *last_dup;
1368 gdouble radius, offset, pos;
1369 CpmlPair center, vector, p[3];
1371 data = path->data;
1372 last = &data->last;
1373 current_dup = cpml_primitive_deep_dup(current);
1374 last_dup = cpml_primitive_deep_dup(last);
1375 radius = data->operation.data.fillet.radius;
1376 offset = _adg_is_convex(last_dup, current_dup) ? -radius : radius;
1378 /* Find the center of the fillet from the intersection between
1379 * the last and current primitives offseted by radius */
1380 cpml_primitive_offset(current_dup, offset);
1381 cpml_primitive_offset(last_dup, offset);
1382 if (cpml_primitive_put_intersections(current_dup, last_dup, 1, &center) == 0) {
1383 g_warning(_("%s: fillet with radius of %lf is not applicable here"),
1384 G_STRLOC, radius);
1385 g_free(current_dup);
1386 g_free(last_dup);
1387 return;
1390 /* Compute the start point of the fillet */
1391 pos = cpml_primitive_get_closest_pos(last_dup, &center);
1392 cpml_primitive_put_vector_at(last_dup, pos, &vector);
1393 cpml_vector_set_length(&vector, offset);
1394 cpml_vector_normal(&vector);
1395 p[0].x = center.x - vector.x;
1396 p[0].y = center.y - vector.y;
1398 /* Compute the mid point of the fillet */
1399 cpml_pair_from_cairo(&vector, current->org);
1400 vector.x -= center.x;
1401 vector.y -= center.y;
1402 cpml_vector_set_length(&vector, radius);
1403 p[1].x = center.x + vector.x;
1404 p[1].y = center.y + vector.y;
1406 /* Compute the end point of the fillet */
1407 pos = cpml_primitive_get_closest_pos(current_dup, &center);
1408 cpml_primitive_put_vector_at(current_dup, pos, &vector);
1409 cpml_vector_set_length(&vector, offset);
1410 cpml_vector_normal(&vector);
1411 p[2].x = center.x - vector.x;
1412 p[2].y = center.y - vector.y;
1414 g_free(current_dup);
1415 g_free(last_dup);
1417 /* Change the end point of the last primitive */
1418 cpml_primitive_set_point(last, -1, &p[0]);
1420 /* Change the start point of the current primitive */
1421 cpml_primitive_set_point(current, 0, &p[2]);
1423 /* Add the fillet arc */
1424 data->operation.action = ADG_ACTION_NONE;
1425 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
1428 static gboolean
1429 _adg_is_convex(const CpmlPrimitive *primitive1, const CpmlPrimitive *primitive2)
1431 CpmlVector v1, v2;
1432 gdouble angle1, angle2;
1434 cpml_primitive_put_vector_at(primitive1, -1, &v1);
1435 cpml_primitive_put_vector_at(primitive2, 0, &v2);
1437 /* Probably there is a smarter way to get this without trygonometry */
1438 angle1 = cpml_vector_angle(&v1);
1439 angle2 = cpml_vector_angle(&v2);
1441 if (angle1 > angle2)
1442 angle1 -= G_PI*2;
1444 return angle2-angle1 > G_PI;
1447 static const gchar *
1448 _adg_action_name(AdgAction action)
1450 switch (action) {
1451 case ADG_ACTION_NONE:
1452 return "NULL";
1453 case ADG_ACTION_CHAMFER:
1454 return "CHAMFER";
1455 case ADG_ACTION_FILLET:
1456 return "FILLET";
1459 return "undefined";
1462 static void
1463 _adg_get_named_pair(AdgModel *model, const gchar *name,
1464 CpmlPair *pair, gpointer user_data)
1466 GSList **named_pairs;
1467 AdgNamedPair *named_pair;
1469 named_pairs = user_data;
1471 named_pair = g_new(AdgNamedPair, 1);
1472 named_pair->name = name;
1473 named_pair->pair = *pair;
1475 *named_pairs = g_slist_prepend(*named_pairs, named_pair);
1478 static void
1479 _adg_dup_reverse_named_pairs(AdgModel *model, const cairo_matrix_t *matrix)
1481 AdgNamedPair *old_named_pair;
1482 AdgNamedPair named_pair;
1483 GSList *named_pairs;
1485 /* Populate named_pairs with all the named pairs of model */
1486 named_pairs = NULL;
1487 adg_model_foreach_named_pair(model, _adg_get_named_pair, &named_pairs);
1489 /* Readd the pairs applying the reversing transformation matrix to
1490 * their coordinates and prepending a "-" to their name */
1491 while (named_pairs) {
1492 old_named_pair = named_pairs->data;
1494 named_pair.name = g_strdup_printf("-%s", old_named_pair->name);
1495 named_pair.pair = old_named_pair->pair;
1496 cpml_pair_transform(&named_pair.pair, matrix);
1498 adg_model_set_named_pair(model, named_pair.name, &named_pair.pair);
1500 g_free((gpointer) named_pair.name);
1501 named_pairs = g_slist_delete_link(named_pairs, named_pairs);