build: depends on cairo-gobject if introspection is enabled
[adg.git] / src / adg / adg-path.c
blob80a501c02aac7668f76627b7004865a8cdb3697b
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 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 get_cairo_path() method of the parent
30 * #AdgTrail class, avoiding the need of an #AdgTrailCallback.
31 * The path is constructed programmaticaly: keep in mind any
32 * method that modifies the path will invalidate the #cairo_path_t
33 * 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_path_close_path() a #CPML_MOVE primitive
47 * to the starting point of the segment is automatically added by
48 * cairo; 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_append_primitive (AdgPath *path,
96 CpmlPrimitive *primitive);
97 static void _adg_clear_operation (AdgPath *path);
98 static gboolean _adg_append_operation (AdgPath *path,
99 AdgAction action,
100 ...);
101 static void _adg_do_operation (AdgPath *path,
102 cairo_path_data_t
103 *path_data);
104 static void _adg_do_action (AdgPath *path,
105 AdgAction action,
106 CpmlPrimitive *primitive);
107 static void _adg_do_chamfer (AdgPath *path,
108 CpmlPrimitive *current);
109 static void _adg_do_fillet (AdgPath *path,
110 CpmlPrimitive *current);
111 static gboolean _adg_is_convex (const CpmlPrimitive
112 *primitive1,
113 const CpmlPrimitive
114 *primitive2);
115 static const gchar * _adg_action_name (AdgAction action);
116 static void _adg_get_named_pair (AdgModel *model,
117 const gchar *name,
118 CpmlPair *pair,
119 gpointer user_data);
120 static void _adg_dup_reverse_named_pairs
121 (AdgModel *model,
122 const cairo_matrix_t
123 *matrix);
126 static void
127 adg_path_class_init(AdgPathClass *klass)
129 GObjectClass *gobject_class;
130 AdgModelClass *model_class;
131 AdgTrailClass *trail_class;
133 gobject_class = (GObjectClass *) klass;
134 model_class = (AdgModelClass *) klass;
135 trail_class = (AdgTrailClass *) klass;
137 g_type_class_add_private(klass, sizeof(AdgPathPrivate));
139 gobject_class->finalize = _adg_finalize;
141 model_class->clear = _adg_clear;
142 model_class->changed = _adg_changed;
144 trail_class->get_cairo_path = _adg_get_cairo_path;
147 static void
148 adg_path_init(AdgPath *path)
150 AdgPathPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(path, ADG_TYPE_PATH,
151 AdgPathPrivate);
153 data->cp_is_valid = FALSE;
154 data->cp.x = 0;
155 data->cp.y = 0;
156 data->cairo.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
157 data->cairo.path.data = NULL;
158 data->cairo.path.num_data = 0;
159 data->cairo.array = g_array_new(FALSE, FALSE, sizeof(cairo_path_data_t));
160 data->last.segment = NULL;
161 data->last.org = NULL;
162 data->last.data = NULL;
163 data->over.segment = NULL;
164 data->over.org = NULL;
165 data->over.data = NULL;
166 data->operation.action = ADG_ACTION_NONE;
168 path->data = data;
171 static void
172 _adg_finalize(GObject *object)
174 AdgPath *path;
175 AdgPathPrivate *data;
177 path = (AdgPath *) object;
178 data = path->data;
180 g_array_free(data->cairo.array, TRUE);
181 _adg_clear_operation(path);
183 if (_ADG_OLD_OBJECT_CLASS->finalize)
184 _ADG_OLD_OBJECT_CLASS->finalize(object);
189 * adg_path_new:
191 * Creates a new path model. The path should be constructed
192 * programmatically by using the methods provided by #AdgPath.
194 * Returns: the newly created path model
196 * Since: 1.0
198 AdgPath *
199 adg_path_new(void)
201 return g_object_new(ADG_TYPE_PATH, NULL);
205 * adg_path_get_current_point:
206 * @path: an #AdgPath
208 * Gets the current point of @path, which is conceptually the
209 * final point reached by the path so far.
211 * If there is no defined current point, %NULL is returned.
212 * It is possible to check this in advance with
213 * adg_path_has_current_point().
215 * Most #AdgPath methods alter the current point and most of them
216 * expect a current point to be defined otherwise will fail triggering
217 * a warning. Check the description of every method for specific details.
219 * Returns: (transfer none): the current point or %NULL on no current point set or errors
221 * Since: 1.0
223 const CpmlPair *
224 adg_path_get_current_point(AdgPath *path)
226 AdgPathPrivate *data;
228 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
230 data = path->data;
232 if (!data->cp_is_valid)
233 return NULL;
235 return &data->cp;
239 * adg_path_has_current_point:
240 * @path: an #AdgPath
242 * Returns whether a current point is defined on @path.
243 * See adg_path_get_current_point() for details on the current point.
245 * Returns: whether a current point is defined
247 * Since: 1.0
249 gboolean
250 adg_path_has_current_point(AdgPath *path)
252 AdgPathPrivate *data;
254 g_return_val_if_fail(ADG_IS_PATH(path), FALSE);
256 data = path->data;
258 return data->cp_is_valid;
262 * adg_path_last_primitive:
263 * @path: an #AdgPath
265 * Gets the last primitive appended to @path. The returned struct
266 * is owned by @path and should not be freed or modified.
268 * Returns: (transfer none): a pointer to the last appended primitive or %NULL on errors
270 * Since: 1.0
272 const CpmlPrimitive *
273 adg_path_last_primitive(AdgPath *path)
275 AdgPathPrivate *data;
277 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
279 data = path->data;
281 return &data->last;
285 * adg_path_over_primitive:
286 * @path: an #AdgPath
288 * Gets the primitive before the last one appended to @path. The
289 * "over" term comes from forth, where the %OVER operator works
290 * on the stack in the same way as adg_path_over_primitive() works
291 * on @path. The returned struct is owned by @path and should not
292 * be freed or modified.
294 * Returns: (transfer none): a pointer to the primitive before the last appended one or %NULL on errors
296 * Since: 1.0
298 const CpmlPrimitive *
299 adg_path_over_primitive(AdgPath *path)
301 AdgPathPrivate *data;
303 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
305 data = path->data;
307 return &data->over;
311 * adg_path_append:
312 * @path: an #AdgPath
313 * @type: a #cairo_data_type_t value
314 * @...: point data, specified as #CpmlPair pointers
316 * Generic method to append a primitive to @path. The number of #CpmlPair
317 * pointers to pass as @Varargs depends on @type: #CPML_CLOSE does not
318 * require any pair, #CPML_MOVE and #CPML_LINE require one pair,
319 * #CPML_ARC two pairs, #CPML_CURVE three pairs and so on.
321 * All the needed pairs must be not %NULL pointers, otherwise the function
322 * will fail. The pairs in excess, if any, will be ignored.
324 * Since: 1.0
326 void
327 adg_path_append(AdgPath *path, CpmlPrimitiveType type, ...)
329 va_list var_args;
331 va_start(var_args, type);
332 adg_path_append_valist(path, type, var_args);
333 va_end(var_args);
337 * adg_path_append_valist:
338 * @path: an #AdgPath
339 * @type: a #cairo_data_type_t value
340 * @var_args: point data, specified as #CpmlPair pointers
342 * va_list version of adg_path_append().
344 * Since: 1.0
346 void
347 adg_path_append_valist(AdgPath *path, CpmlPrimitiveType type, va_list var_args)
349 GArray *array;
350 CpmlPair *pair;
351 gint length;
353 length = _adg_primitive_length(type);
354 if (length == 0)
355 return;
357 array = g_array_new(TRUE, FALSE, sizeof(pair));
358 while (-- length) {
359 pair = va_arg(var_args, CpmlPair *);
360 g_array_append_val(array, pair);
363 adg_path_append_array(path, type, (const CpmlPair **) array->data);
364 g_array_free(array, TRUE);
368 * adg_path_append_array:
369 * @path: an #AdgPath
370 * @type: a #cairo_data_type_t value
371 * @pairs: (array zero-terminated=1) (element-type Cpml.Pair) (transfer none): point data, specified as a %NULL terminated array of #CpmlPair pointers
373 * A bindingable version of adg_path_append() that uses a %NULL terminated
374 * array of pairs instead of variable argument list and friends.
376 * Furthermore, because of the list is %NULL terminated, an arbitrary
377 * number of pairs can be passed in @pairs. This allows to embed in a
378 * primitive element more data pairs than requested, something impossible
379 * to do with adg_path_append() and adg_path_append_valist().
381 * Rename to: adg_path_append
382 * Since: 1.0
384 void
385 adg_path_append_array(AdgPath *path, CpmlPrimitiveType type,
386 const CpmlPair **pairs)
388 gint length;
389 GArray *array;
390 const CpmlPair **pair;
391 cairo_path_data_t path_data;
393 g_return_if_fail(ADG_IS_PATH(path));
394 g_return_if_fail(pairs != NULL);
396 length = _adg_primitive_length(type);
397 if (length == 0)
398 return;
400 array = g_array_new(FALSE, FALSE, sizeof(path_data));
401 for (pair = pairs; *pair != NULL; ++ pair) {
402 cpml_pair_to_cairo(*pair, &path_data);
403 g_array_append_val(array, path_data);
406 if (array->len < length - 1) {
407 /* Not enough pairs have been provided */
408 g_warning(_("%s: null pair caught while parsing arguments"), G_STRLOC);
409 } else {
410 AdgPathPrivate *data;
411 CpmlPrimitive primitive;
412 cairo_path_data_t org;
414 /* Save a copy of the current point as primitive origin */
415 data = path->data;
416 cpml_pair_to_cairo(&data->cp, &org);
418 /* Prepend the cairo header */
419 path_data.header.type = type;
420 path_data.header.length = array->len + 1;
421 g_array_prepend_val(array, path_data);
423 /* Append a new primitive to @path */
424 primitive.segment = NULL;
425 primitive.org = &org;
426 primitive.data = (cairo_path_data_t *) array->data;
427 _adg_append_primitive(path, &primitive);
430 g_array_free(array, TRUE);
435 * adg_path_append_primitive:
436 * @path: an #AdgPath
437 * @primitive: the #CpmlPrimitive to append
439 * Appends @primitive to @path. The primitive to add is considered the
440 * continuation of the current path so the <structfield>org</structfield>
441 * component of @primitive is not used. Anyway the current point is
442 * checked against it: they must be equal or the function will fail
443 * without further processing.
445 * Since: 1.0
447 void
448 adg_path_append_primitive(AdgPath *path, const CpmlPrimitive *primitive)
450 AdgPathPrivate *data;
451 CpmlPrimitive *primitive_dup;
453 g_return_if_fail(ADG_IS_PATH(path));
454 g_return_if_fail(primitive != NULL);
455 g_return_if_fail(primitive->org != NULL);
457 data = path->data;
459 g_return_if_fail(primitive->org->point.x == data->cp.x &&
460 primitive->org->point.y == data->cp.y);
462 /* The primitive data could be modified by pending operations:
463 * work on a copy */
464 primitive_dup = cpml_primitive_deep_dup(primitive);
466 _adg_append_primitive(path, primitive_dup);
468 g_free(primitive_dup);
472 * adg_path_append_segment:
473 * @path: an #AdgPath
474 * @segment: the #CpmlSegment to append
476 * Appends @segment to @path.
478 * Since: 1.0
480 void
481 adg_path_append_segment(AdgPath *path, const CpmlSegment *segment)
483 AdgPathPrivate *data;
485 g_return_if_fail(ADG_IS_PATH(path));
486 g_return_if_fail(segment != NULL);
488 data = path->data;
490 _adg_clear_parent((AdgModel *) path);
491 data->cairo.array = g_array_append_vals(data->cairo.array,
492 segment->data, segment->num_data);
496 * adg_path_append_cairo_path:
497 * @path: an #AdgPath
498 * @cairo_path: (type gpointer): the #cairo_path_t path to append
500 * Appends a whole #cairo_path_t to @path.
502 * Since: 1.0
504 void
505 adg_path_append_cairo_path(AdgPath *path, const cairo_path_t *cairo_path)
507 AdgPathPrivate *data;
509 g_return_if_fail(ADG_IS_PATH(path));
510 g_return_if_fail(cairo_path != NULL);
512 data = path->data;
514 _adg_clear_parent((AdgModel *) path);
515 data->cairo.array = g_array_append_vals(data->cairo.array,
516 cairo_path->data,
517 cairo_path->num_data);
521 * adg_path_move_to:
522 * @path: an #AdgPath
523 * @pair: the destination coordinates
525 * Begins a new segment. After this call the current point will be @pair.
527 * Since: 1.0
529 void
530 adg_path_move_to(AdgPath *path, const CpmlPair *pair)
532 adg_path_append(path, CPML_MOVE, pair);
536 * adg_path_move_to_explicit:
537 * @path: an #AdgPath
538 * @x: the new x coordinate
539 * @y: the new y coordinate
541 * Convenient function to call adg_path_move_to() using explicit
542 * coordinates instead of #CpmlPair.
544 * Since: 1.0
546 void
547 adg_path_move_to_explicit(AdgPath *path, gdouble x, gdouble y)
549 CpmlPair p;
551 p.x = x;
552 p.y = y;
554 adg_path_append(path, CPML_MOVE, &p);
558 * adg_path_line_to:
559 * @path: an #AdgPath
560 * @pair: the destination coordinates
562 * Adds a line to @path from the current point to @pair. After this
563 * call the current point will be @pair.
565 * If @path has no current point before this call, this function will
566 * trigger a warning without other effect.
568 * Since: 1.0
570 void
571 adg_path_line_to(AdgPath *path, const CpmlPair *pair)
573 adg_path_append(path, CPML_LINE, pair);
577 * adg_path_line_to_explicit:
578 * @path: an #AdgPath
579 * @x: the new x coordinate
580 * @y: the new y coordinate
582 * Convenient function to call adg_path_line_to() using explicit
583 * coordinates instead of #CpmlPair.
585 * Since: 1.0
587 void
588 adg_path_line_to_explicit(AdgPath *path, gdouble x, gdouble y)
590 CpmlPair p;
592 p.x = x;
593 p.y = y;
595 adg_path_append(path, CPML_LINE, &p);
599 * adg_path_arc_to:
600 * @path: an #AdgPath
601 * @throught: an arbitrary point on the arc
602 * @pair: the destination coordinates
604 * Adds an arc to the path from the current point to @pair, passing
605 * throught @throught. After this call the current point will be @pair.
607 * If @path has no current point before this call, this function will
608 * trigger a warning without other effect.
610 * Since: 1.0
612 void
613 adg_path_arc_to(AdgPath *path, const CpmlPair *throught, const CpmlPair *pair)
615 adg_path_append(path, CPML_ARC, throught, pair);
619 * adg_path_arc_to_explicit:
620 * @path: an #AdgPath
621 * @x1: the x coordinate of an intermediate point
622 * @y1: the y coordinate of an intermediate point
623 * @x2: the x coordinate of the end of the arc
624 * @y2: the y coordinate of the end of the arc
626 * Convenient function to call adg_path_arc_to() using explicit
627 * coordinates instead of #CpmlPair.
629 * Since: 1.0
631 void
632 adg_path_arc_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
633 gdouble x2, gdouble y2)
635 CpmlPair p[2];
637 p[0].x = x1;
638 p[0].y = y1;
639 p[1].x = x2;
640 p[1].y = y2;
642 adg_path_append(path, CPML_ARC, &p[0], &p[1]);
646 * adg_path_curve_to:
647 * @path: an #AdgPath
648 * @control1: the first control point of the curve
649 * @control2: the second control point of the curve
650 * @pair: the destination coordinates
652 * Adds a cubic Bézier curve to the path from the current point to
653 * position @pair, using @control1 and @control2 as control points.
654 * After this call the current point will be @pair.
656 * If @path has no current point before this call, this function will
657 * trigger a warning without other effect.
659 * Since: 1.0
661 void
662 adg_path_curve_to(AdgPath *path, const CpmlPair *control1,
663 const CpmlPair *control2, const CpmlPair *pair)
665 adg_path_append(path, CPML_CURVE, control1, control2, pair);
669 * adg_path_curve_to_explicit:
670 * @path: an #AdgPath
671 * @x1: the x coordinate of the first control point
672 * @y1: the y coordinate of the first control point
673 * @x2: the x coordinate of the second control point
674 * @y2: the y coordinate of the second control point
675 * @x3: the x coordinate of the end of the curve
676 * @y3: the y coordinate of the end of the curve
678 * Convenient function to call adg_path_curve_to() using explicit
679 * coordinates instead of #CpmlPair.
681 * Since: 1.0
683 void
684 adg_path_curve_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
685 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
687 CpmlPair p[3];
689 p[0].x = x1;
690 p[0].y = y1;
691 p[1].x = x2;
692 p[1].y = y2;
693 p[2].x = x3;
694 p[2].y = y3;
696 adg_path_append(path, CPML_CURVE, &p[0], &p[1], &p[2]);
700 * adg_path_close:
701 * @path: an #AdgPath
703 * Adds a line segment to the path from the current point to the
704 * beginning of the current segment, (the most recent point passed
705 * to an adg_path_move_to()), and closes this segment.
706 * After this call the current point will be unset.
708 * The behavior of adg_path_close() is distinct from simply calling
709 * adg_line_to() with the coordinates of the segment starting point.
710 * When a closed segment is stroked, there are no caps on the ends.
711 * Instead, there is a line join connecting the final and initial
712 * primitive of the segment.
714 * If @path has no current point before this call, this function will
715 * trigger a warning without other effect.
717 * Since: 1.0
719 void
720 adg_path_close(AdgPath *path)
722 adg_path_append(path, CPML_CLOSE);
726 * adg_path_arc:
727 * @path: an #AdgPath
728 * @center: coordinates of the center of the arc
729 * @r: the radius of the arc
730 * @start: the start angle, in radians
731 * @end: the end angle, in radians
733 * A more usual way to add an arc to @path. After this call, the current
734 * point will be the computed end point of the arc. The arc will be
735 * rendered in increasing angle, accordling to @start and @end. This means
736 * if @start is less than @end, the arc will be rendered in clockwise
737 * direction (accordling to the default cairo coordinate system) while if
738 * @start is greather than @end, the arc will be rendered in couterclockwise
739 * direction.
741 * By explicitely setting the whole arc data, the start point could be
742 * different from the current point. In this case, if @path has no
743 * current point before the call a #CPML_MOVE to the start point of
744 * the arc will be automatically prepended to the arc. If @path has a
745 * current point, a #CPML_LINE to the start point of the arc will be
746 * used instead of the "move to" primitive.
748 * Since: 1.0
750 void
751 adg_path_arc(AdgPath *path, const CpmlPair *center, gdouble r,
752 gdouble start, gdouble end)
754 AdgPathPrivate *data;
755 CpmlPair p[3];
757 g_return_if_fail(ADG_IS_PATH(path));
758 g_return_if_fail(center != NULL);
760 data = path->data;
761 cpml_vector_from_angle(&p[0], start);
762 cpml_vector_from_angle(&p[1], (end-start) / 2);
763 cpml_vector_from_angle(&p[2], end);
765 cpml_vector_set_length(&p[0], r);
766 cpml_vector_set_length(&p[1], r);
767 cpml_vector_set_length(&p[2], r);
769 p[0].x += center->x;
770 p[0].y += center->y;
771 p[1].x += center->x;
772 p[1].y += center->y;
773 p[2].x += center->x;
774 p[2].y += center->y;
776 if (!data->cp_is_valid)
777 adg_path_append(path, CPML_MOVE, &p[0]);
778 else if (p[0].x != data->cp.x || p[0].y != data->cp.y)
779 adg_path_append(path, CPML_LINE, &p[0]);
781 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
785 * adg_path_arc_explicit:
786 * @path: an #AdgPath
787 * @xc: x position of the center of the arc
788 * @yc: y position of the center of the arc
789 * @r: the radius of the arc
790 * @start: the start angle, in radians
791 * @end: the end angle, in radians
793 * Convenient function to call adg_path_arc() using explicit
794 * coordinates instead of #CpmlPair.
796 * Since: 1.0
798 void
799 adg_path_arc_explicit(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
800 gdouble start, gdouble end)
802 CpmlPair center;
804 center.x = xc;
805 center.y = yc;
807 adg_path_arc(path, &center, r, start, end);
811 * adg_path_chamfer:
812 * @path: an #AdgPath
813 * @delta1: the distance from the intersection point of the current primitive
814 * @delta2: the distance from the intersection point of the next primitive
816 * A binary action that generates a chamfer between two primitives.
817 * The first primitive involved is the current primitive, the second will
818 * be the next primitive appended to @path after this call. The second
819 * primitive is required: if the chamfer operation is not properly
820 * terminated (by not providing the second primitive), any API accessing
821 * the path in reading mode will raise a warning.
823 * An exception is a chamfer after a #CPML_CLOSE primitive. In this case,
824 * the second primitive is not required: the current close path is used
825 * as first operand while the first primitive of the current segment is
826 * used as second operand.
828 * The chamfer operation requires two lengths: @delta1 specifies the
829 * "quantity" to trim on the first primitive while @delta2 is the same
830 * applied on the second primitive. The term "quantity" means the length
831 * of the portion to cut out from the original primitive (that is the
832 * primitive as would be without the chamfer).
834 * Since: 1.0
836 void
837 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
839 g_return_if_fail(ADG_IS_PATH(path));
841 if (!_adg_append_operation(path, ADG_ACTION_CHAMFER, delta1, delta2))
842 return;
846 * adg_path_fillet:
847 * @path: an #AdgPath
848 * @radius: the radius of the fillet
850 * A binary action that joins to primitives with an arc.
851 * The first primitive involved is the current primitive, the second will
852 * be the next primitive appended to @path after this call. The second
853 * primitive is required: if the fillet operation is not properly
854 * terminated (by not providing the second primitive), any API accessing
855 * the path in reading mode will raise a warning.
857 * An exception is a fillet after a #CPML_CLOSE primitive. In this case,
858 * the second primitive is not required: the current close path is used
859 * as first operand while the first primitive of the current segment is
860 * used as second operand.
862 * Since: 1.0
864 void
865 adg_path_fillet(AdgPath *path, gdouble radius)
867 g_return_if_fail(ADG_IS_PATH(path));
869 if (!_adg_append_operation(path, ADG_ACTION_FILLET, radius))
870 return;
874 * adg_path_reflect:
875 * @path: an #AdgPath
876 * @vector: (allow-none): the slope of the axis
878 * Reflects the first segment or @path around the axis passing
879 * throught (0, 0) and with a @vector slope. The internal segment
880 * is duplicated and the proper transformation (computed from
881 * @vector) to mirror the segment is applied on all its points.
882 * The result is then reversed with cpml_segment_reverse() and
883 * appended to the original path with adg_path_append_segment().
885 * For convenience, if @vector is %NULL the path is reversed
886 * around the x axis (y=0).
888 * Since: 1.0
890 void
891 adg_path_reflect(AdgPath *path, const CpmlVector *vector)
893 AdgModel *model;
894 cairo_matrix_t matrix;
895 CpmlSegment segment, *dup_segment;
897 g_return_if_fail(ADG_IS_PATH(path));
898 g_return_if_fail(vector == NULL || vector->x != 0 || vector->y != 0);
900 model = (AdgModel *) path;
902 if (vector == NULL) {
903 cairo_matrix_init_scale(&matrix, 1, -1);
904 } else {
905 CpmlVector slope;
906 gdouble cos2angle, sin2angle;
908 cpml_pair_copy(&slope, vector);
909 cpml_vector_set_length(&slope, 1);
911 if (slope.x == 0 && slope.y == 0) {
912 g_warning(_("%s: the axis of the reflection is not known"),
913 G_STRLOC);
914 return;
917 sin2angle = 2. * vector->x * vector->y;
918 cos2angle = 2. * vector->x * vector->x - 1;
920 cairo_matrix_init(&matrix, cos2angle, sin2angle,
921 sin2angle, -cos2angle, 0, 0);
924 if (!adg_trail_put_segment((AdgTrail *) path, 1, &segment))
925 return;
927 /* No need to reverse an empty segment */
928 if (segment.num_data == 0 || segment.num_data == 0)
929 return;
931 dup_segment = cpml_segment_deep_dup(&segment);
932 if (dup_segment == NULL)
933 return;
935 cpml_segment_reverse(dup_segment);
936 cpml_segment_transform(dup_segment, &matrix);
937 dup_segment->data[0].header.type = CPML_LINE;
939 adg_path_append_segment(path, dup_segment);
941 g_free(dup_segment);
943 _adg_dup_reverse_named_pairs(model, &matrix);
947 * adg_path_reflect_explicit:
948 * @path: an #AdgPath
949 * @x: the vector x component
950 * @y: the vector y component
952 * Convenient function to call adg_path_reflect() using explicit
953 * vector components instead of #CpmlVector.
955 * Since: 1.0
957 void
958 adg_path_reflect_explicit(AdgPath *path, gdouble x, gdouble y)
960 CpmlVector vector;
962 vector.x = x;
963 vector.y = y;
965 adg_path_reflect(path, &vector);
969 static void
970 _adg_clear(AdgModel *model)
972 AdgPath *path;
973 AdgPathPrivate *data;
975 path = (AdgPath *) model;
976 data = path->data;
978 g_array_set_size(data->cairo.array, 0);
979 _adg_clear_operation(path);
980 _adg_clear_parent(model);
983 static void
984 _adg_clear_parent(AdgModel *model)
986 if (_ADG_OLD_MODEL_CLASS->clear)
987 _ADG_OLD_MODEL_CLASS->clear(model);
990 static void
991 _adg_changed(AdgModel *model)
993 _adg_clear_parent(model);
995 if (_ADG_OLD_MODEL_CLASS->changed)
996 _ADG_OLD_MODEL_CLASS->changed(model);
999 static cairo_path_t *
1000 _adg_get_cairo_path(AdgTrail *trail)
1002 _adg_clear_parent((AdgModel *) trail);
1003 return _adg_read_cairo_path((AdgPath *) trail);
1006 static cairo_path_t *
1007 _adg_read_cairo_path(AdgPath *path)
1009 AdgPathPrivate *data = path->data;
1010 cairo_path_t *cairo_path = &data->cairo.path;
1011 GArray *array = data->cairo.array;
1013 /* Always regenerate the cairo_path_t as it is a trivial operation */
1014 cairo_path->status = CAIRO_STATUS_SUCCESS;
1015 cairo_path->data = (cairo_path_data_t *) array->data;
1016 cairo_path->num_data = array->len;
1018 return cairo_path;
1021 static gint
1022 _adg_primitive_length(CpmlPrimitiveType type)
1024 if (type == CPML_CLOSE)
1025 return 1;
1026 else if (type == CPML_MOVE)
1027 return 2;
1029 return cpml_primitive_type_get_n_points(type);
1032 static void
1033 _adg_primitive_remap(CpmlPrimitive *primitive, gpointer to,
1034 const CpmlPrimitive *old, gconstpointer from)
1036 primitive->org = REMAPPED(old->org, from, to);
1037 primitive->segment = REMAPPED(old->segment, from, to);
1038 primitive->data = REMAPPED(old->data, from, to);
1041 static void
1042 _adg_append_primitive(AdgPath *path, CpmlPrimitive *current)
1044 AdgPathPrivate *data;
1045 cairo_path_data_t *path_data;
1046 int length;
1047 gconstpointer old_data;
1049 data = path->data;
1050 path_data = current->data;
1051 length = path_data[0].header.length;
1053 /* Execute any pending operation */
1054 _adg_do_operation(path, path_data);
1056 /* Append the path data to the internal path array */
1057 old_data = (data->cairo.array)->data;
1058 data->cairo.array = g_array_append_vals(data->cairo.array,
1059 path_data, length);
1061 /* Set path data to point to the recently appended cairo_path_data_t
1062 * primitive: the first struct is the header */
1063 path_data = (cairo_path_data_t *) (data->cairo.array)->data +
1064 (data->cairo.array)->len - length;
1066 /* Store the last primitive into over */
1067 _adg_primitive_remap(&data->over, (data->cairo.array)->data,
1068 &data->last, old_data);
1070 /* Set the last primitive for subsequent binary operations */
1071 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
1072 data->last.segment = NULL;
1073 data->last.data = path_data;
1075 /* Save the last point as the current point, if applicable */
1076 data->cp_is_valid = length > 1;
1077 if (length > 1)
1078 cpml_pair_from_cairo(&data->cp, &path_data[length-1]);
1080 /* Invalidate cairo_path: should be recomputed */
1081 _adg_clear_parent((AdgModel *) path);
1084 static void
1085 _adg_clear_operation(AdgPath *path)
1087 AdgPathPrivate *data;
1088 AdgOperation *operation;
1090 data = path->data;
1091 operation = &data->operation;
1093 if (operation->action != ADG_ACTION_NONE) {
1094 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
1095 G_STRLOC, _adg_action_name(operation->action));
1096 operation->action = ADG_ACTION_NONE;
1099 data->cp_is_valid = FALSE;
1100 data->last.data = NULL;
1101 data->over.data = NULL;
1104 static gboolean
1105 _adg_append_operation(AdgPath *path, AdgAction action, ...)
1107 AdgPathPrivate *data;
1108 AdgOperation *operation;
1109 va_list var_args;
1111 data = path->data;
1113 if (data->last.data == NULL) {
1114 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
1115 G_STRLOC, _adg_action_name(action));
1116 return FALSE;
1119 operation = &data->operation;
1120 if (operation->action != ADG_ACTION_NONE) {
1121 g_warning(_("%s: requested a `%s' operation while a `%s' operation was active"),
1122 G_STRLOC, _adg_action_name(action),
1123 _adg_action_name(operation->action));
1124 /* XXX: http://dev.entidi.com/p/adg/issues/50/ */
1125 return FALSE;
1128 va_start(var_args, action);
1130 switch (action) {
1132 case ADG_ACTION_CHAMFER:
1133 operation->data.chamfer.delta1 = va_arg(var_args, double);
1134 operation->data.chamfer.delta2 = va_arg(var_args, double);
1135 break;
1137 case ADG_ACTION_FILLET:
1138 operation->data.fillet.radius = va_arg(var_args, double);
1139 break;
1141 case ADG_ACTION_NONE:
1142 va_end(var_args);
1143 return TRUE;
1145 default:
1146 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC, action);
1147 va_end(var_args);
1148 return FALSE;
1151 operation->action = action;
1152 va_end(var_args);
1154 if (data->last.data[0].header.type == CAIRO_PATH_CLOSE_PATH) {
1155 /* Special case: an action with the close primitive should
1156 * be resolved now by changing the close primitive to a
1157 * line-to and using it as second operand and use the first
1158 * primitive of the current segment as first operand */
1159 guint length;
1160 cairo_path_data_t *path_data;
1161 CpmlSegment segment;
1162 CpmlPrimitive current;
1164 length = data->cairo.array->len;
1166 /* Ensure the close path primitive is not the only data */
1167 g_return_val_if_fail(length > 1, FALSE);
1169 /* Allocate one more item once for all to accept the
1170 * conversion from a close to line-to primitive */
1171 data->cairo.array = g_array_set_size(data->cairo.array, length + 1);
1172 path_data = (cairo_path_data_t *) data->cairo.array->data;
1173 --data->cairo.array->len;
1175 /* Set segment and current (the first primitive of segment) */
1176 cpml_segment_from_cairo(&segment, _adg_read_cairo_path(path));
1177 while (cpml_segment_next(&segment))
1179 cpml_primitive_from_segment(&current, &segment);
1181 /* Convert close path to a line-to primitive */
1182 ++data->cairo.array->len;
1183 path_data[length - 1].header.type = CPML_LINE;
1184 path_data[length - 1].header.length = 2;
1185 path_data[length] = *current.org;
1187 data->last.segment = &segment;
1188 data->last.org = &path_data[length - 2];
1189 data->last.data = &path_data[length - 1];
1191 _adg_do_action(path, action, &current);
1194 return TRUE;
1197 static void
1198 _adg_do_operation(AdgPath *path, cairo_path_data_t *path_data)
1200 AdgPathPrivate *data;
1201 AdgAction action;
1202 CpmlSegment segment;
1203 CpmlPrimitive current;
1204 cairo_path_data_t current_org;
1206 data = path->data;
1207 action = data->operation.action;
1208 cpml_segment_from_cairo(&segment, _adg_read_cairo_path(path));
1210 /* Construct the current primitive, that is the primitive to be
1211 * mixed with the last primitive with the specified operation.
1212 * Its org is a copy of the end point of the last primitive: it can be
1213 * modified without affecting anything else. It is expected the operation
1214 * functions will add to @path the primitives required but NOT to add
1215 * @current, as this one will be inserted automatically. */
1216 current.segment = &segment;
1217 current.org = &current_org;
1218 current.data = path_data;
1219 cpml_pair_to_cairo(&data->cp, &current_org);
1221 _adg_do_action(path, action, &current);
1224 static void
1225 _adg_do_action(AdgPath *path, AdgAction action, CpmlPrimitive *primitive)
1227 switch (action) {
1228 case ADG_ACTION_NONE:
1229 return;
1230 case ADG_ACTION_CHAMFER:
1231 _adg_do_chamfer(path, primitive);
1232 break;
1233 case ADG_ACTION_FILLET:
1234 _adg_do_fillet(path, primitive);
1235 break;
1236 default:
1237 g_return_if_reached();
1241 static void
1242 _adg_do_chamfer(AdgPath *path, CpmlPrimitive *current)
1244 AdgPathPrivate *data;
1245 CpmlPrimitive *last;
1246 gdouble delta1, delta2;
1247 gdouble len1, len2;
1248 CpmlPair pair;
1250 data = path->data;
1251 last = &data->last;
1252 delta1 = data->operation.data.chamfer.delta1;
1253 len1 = cpml_primitive_get_length(last);
1255 if (delta1 >= len1) {
1256 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1257 G_STRLOC, delta1, len1);
1258 return;
1261 delta2 = data->operation.data.chamfer.delta2;
1262 len2 = cpml_primitive_get_length(current);
1264 if (delta2 >= len2) {
1265 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1266 G_STRLOC, delta1, len1);
1267 return;
1270 /* Change the end point of the last primitive */
1271 cpml_primitive_put_pair_at(last, 1. - delta1 / len1, &pair);
1272 cpml_primitive_set_point(last, -1, &pair);
1274 /* Change the start point of the current primitive */
1275 cpml_primitive_put_pair_at(current, delta2 / len2, &pair);
1276 cpml_primitive_set_point(current, 0, &pair);
1278 /* Add the chamfer line */
1279 data->operation.action = ADG_ACTION_NONE;
1280 adg_path_append(path, CPML_LINE, &pair);
1283 static void
1284 _adg_do_fillet(AdgPath *path, CpmlPrimitive *current)
1286 AdgPathPrivate *data;
1287 CpmlPrimitive *last, *current_dup, *last_dup;
1288 gdouble radius, offset, pos;
1289 CpmlPair center, vector, p[3];
1291 data = path->data;
1292 last = &data->last;
1293 current_dup = cpml_primitive_deep_dup(current);
1294 last_dup = cpml_primitive_deep_dup(last);
1295 radius = data->operation.data.fillet.radius;
1296 offset = _adg_is_convex(last_dup, current_dup) ? -radius : radius;
1298 /* Find the center of the fillet from the intersection between
1299 * the last and current primitives offseted by radius */
1300 cpml_primitive_offset(current_dup, offset);
1301 cpml_primitive_offset(last_dup, offset);
1302 if (cpml_primitive_put_intersections(current_dup, last_dup, 1, &center) == 0) {
1303 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1304 G_STRLOC, radius);
1305 g_free(current_dup);
1306 g_free(last_dup);
1307 return;
1310 /* Compute the start point of the fillet */
1311 pos = cpml_primitive_get_closest_pos(last_dup, &center);
1312 cpml_primitive_put_vector_at(last_dup, pos, &vector);
1313 cpml_vector_set_length(&vector, offset);
1314 cpml_vector_normal(&vector);
1315 p[0].x = center.x - vector.x;
1316 p[0].y = center.y - vector.y;
1318 /* Compute the mid point of the fillet */
1319 cpml_pair_from_cairo(&vector, current->org);
1320 vector.x -= center.x;
1321 vector.y -= center.y;
1322 cpml_vector_set_length(&vector, radius);
1323 p[1].x = center.x + vector.x;
1324 p[1].y = center.y + vector.y;
1326 /* Compute the end point of the fillet */
1327 pos = cpml_primitive_get_closest_pos(current_dup, &center);
1328 cpml_primitive_put_vector_at(current_dup, pos, &vector);
1329 cpml_vector_set_length(&vector, offset);
1330 cpml_vector_normal(&vector);
1331 p[2].x = center.x - vector.x;
1332 p[2].y = center.y - vector.y;
1334 g_free(current_dup);
1335 g_free(last_dup);
1337 /* Change the end point of the last primitive */
1338 cpml_primitive_set_point(last, -1, &p[0]);
1340 /* Change the start point of the current primitive */
1341 cpml_primitive_set_point(current, 0, &p[2]);
1343 /* Add the fillet arc */
1344 data->operation.action = ADG_ACTION_NONE;
1345 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
1348 static gboolean
1349 _adg_is_convex(const CpmlPrimitive *primitive1, const CpmlPrimitive *primitive2)
1351 CpmlVector v1, v2;
1352 gdouble angle1, angle2;
1354 cpml_primitive_put_vector_at(primitive1, -1, &v1);
1355 cpml_primitive_put_vector_at(primitive2, 0, &v2);
1357 /* Probably there is a smarter way to get this without trygonometry */
1358 angle1 = cpml_vector_angle(&v1);
1359 angle2 = cpml_vector_angle(&v2);
1361 if (angle1 > angle2)
1362 angle1 -= G_PI*2;
1364 return angle2-angle1 > G_PI;
1367 static const gchar *
1368 _adg_action_name(AdgAction action)
1370 switch (action) {
1371 case ADG_ACTION_NONE:
1372 return "NULL";
1373 case ADG_ACTION_CHAMFER:
1374 return "CHAMFER";
1375 case ADG_ACTION_FILLET:
1376 return "FILLET";
1379 return "undefined";
1382 static void
1383 _adg_get_named_pair(AdgModel *model, const gchar *name,
1384 CpmlPair *pair, gpointer user_data)
1386 GSList **named_pairs;
1387 AdgNamedPair *named_pair;
1389 named_pairs = user_data;
1391 named_pair = g_new(AdgNamedPair, 1);
1392 named_pair->name = name;
1393 named_pair->pair = *pair;
1395 *named_pairs = g_slist_prepend(*named_pairs, named_pair);
1398 static void
1399 _adg_dup_reverse_named_pairs(AdgModel *model, const cairo_matrix_t *matrix)
1401 AdgNamedPair *old_named_pair;
1402 AdgNamedPair named_pair;
1403 GSList *named_pairs;
1405 /* Populate named_pairs with all the named pairs of model */
1406 named_pairs = NULL;
1407 adg_model_foreach_named_pair(model, _adg_get_named_pair, &named_pairs);
1409 /* Readd the pairs applying the reversing transformation matrix to
1410 * their coordinates and prepending a "-" to their name */
1411 while (named_pairs) {
1412 old_named_pair = named_pairs->data;
1414 named_pair.name = g_strdup_printf("-%s", old_named_pair->name);
1415 named_pair.pair = old_named_pair->pair;
1416 cpml_pair_transform(&named_pair.pair, matrix);
1418 adg_model_set_named_pair(model, named_pair.name, &named_pair.pair);
1420 g_free((gpointer) named_pair.name);
1421 named_pairs = g_slist_delete_link(named_pairs, named_pairs);