adg: use cairo_matrix_t instead of AdgMatrix
[adg.git] / src / adg / adg-path.c
blob9c91e1455778414cc83b1cd8a7dfe0adfa42ca53
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012 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 #CpmlPath: this class
26 * implements methods to create the path and provides additional
27 * operations specific to technical drawings.
29 * #AdgPath overrides the get_cpml_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 #CpmlPath
33 * returned by adg_trail_get_cpml_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"
64 #include <string.h>
66 #include "adg-model.h"
67 #include "adg-trail.h"
69 #include "adg-path.h"
70 #include "adg-path-private.h"
73 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_path_parent_class)
74 #define _ADG_OLD_MODEL_CLASS ((AdgModelClass *) adg_path_parent_class)
77 G_DEFINE_TYPE(AdgPath, adg_path, ADG_TYPE_TRAIL)
80 static void _adg_finalize (GObject *object);
81 static void _adg_clear (AdgModel *model);
82 static void _adg_clear_parent (AdgModel *model);
83 static void _adg_changed (AdgModel *model);
84 static CpmlPath * _adg_get_cpml_path (AdgTrail *trail);
85 static CpmlPath * _adg_read_cpml_path (AdgPath *path);
86 static gint _adg_primitive_length (CpmlPrimitiveType type);
87 static void _adg_append_primitive (AdgPath *path,
88 CpmlPrimitive *primitive);
89 static void _adg_clear_operation (AdgPath *path);
90 static gboolean _adg_append_operation (AdgPath *path,
91 AdgAction action,
92 ...);
93 static void _adg_do_operation (AdgPath *path,
94 cairo_path_data_t
95 *path_data);
96 static void _adg_do_action (AdgPath *path,
97 AdgAction action,
98 CpmlPrimitive *primitive);
99 static void _adg_do_chamfer (AdgPath *path,
100 CpmlPrimitive *current);
101 static void _adg_do_fillet (AdgPath *path,
102 CpmlPrimitive *current);
103 static gboolean _adg_is_convex (const CpmlPrimitive
104 *primitive1,
105 const CpmlPrimitive
106 *primitive2);
107 static const gchar * _adg_action_name (AdgAction action);
108 static void _adg_get_named_pair (AdgModel *model,
109 const gchar *name,
110 CpmlPair *pair,
111 gpointer user_data);
112 static void _adg_dup_reverse_named_pairs
113 (AdgModel *model,
114 const cairo_matrix_t
115 *matrix);
118 static void
119 adg_path_class_init(AdgPathClass *klass)
121 GObjectClass *gobject_class;
122 AdgModelClass *model_class;
123 AdgTrailClass *trail_class;
125 gobject_class = (GObjectClass *) klass;
126 model_class = (AdgModelClass *) klass;
127 trail_class = (AdgTrailClass *) klass;
129 g_type_class_add_private(klass, sizeof(AdgPathPrivate));
131 gobject_class->finalize = _adg_finalize;
133 model_class->clear = _adg_clear;
134 model_class->changed = _adg_changed;
136 trail_class->get_cpml_path = _adg_get_cpml_path;
139 static void
140 adg_path_init(AdgPath *path)
142 AdgPathPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(path, ADG_TYPE_PATH,
143 AdgPathPrivate);
145 data->cp_is_valid = FALSE;
146 data->cpml.array = g_array_new(FALSE, FALSE, sizeof(cairo_path_data_t));
147 data->operation.action = ADG_ACTION_NONE;
149 path->data = data;
152 static void
153 _adg_finalize(GObject *object)
155 AdgPath *path;
156 AdgPathPrivate *data;
158 path = (AdgPath *) object;
159 data = path->data;
161 g_array_free(data->cpml.array, TRUE);
162 _adg_clear_operation(path);
164 if (_ADG_OLD_OBJECT_CLASS->finalize)
165 _ADG_OLD_OBJECT_CLASS->finalize(object);
170 * adg_path_new:
172 * Creates a new path model. The path should be constructed
173 * programmatically by using the methods provided by #AdgPath.
175 * Returns: the newly created path model
177 * Since: 1.0
179 AdgPath *
180 adg_path_new(void)
182 return g_object_new(ADG_TYPE_PATH, NULL);
186 * adg_path_get_current_point:
187 * @path: an #AdgPath
189 * Gets the current point of @path, which is conceptually the
190 * final point reached by the path so far.
192 * If there is no defined current point, %NULL is returned.
193 * It is possible to check this in advance with
194 * adg_path_has_current_point().
196 * Most #AdgPath methods alter the current point and most of them
197 * expect a current point to be defined otherwise will fail triggering
198 * a warning. Check the description of every method for specific details.
200 * Returns: the current point or %NULL on no current point set or errors
202 * Since: 1.0
204 const CpmlPair *
205 adg_path_get_current_point(AdgPath *path)
207 AdgPathPrivate *data;
209 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
211 data = path->data;
213 if (!data->cp_is_valid)
214 return NULL;
216 return &data->cp;
220 * adg_path_has_current_point:
221 * @path: an #AdgPath
223 * Returns whether a current point is defined on @path.
224 * See adg_path_get_current_point() for details on the current point.
226 * Returns: whether a current point is defined
228 * Since: 1.0
230 gboolean
231 adg_path_has_current_point(AdgPath *path)
233 AdgPathPrivate *data;
235 g_return_val_if_fail(ADG_IS_PATH(path), FALSE);
237 data = path->data;
239 return data->cp_is_valid;
243 * adg_path_last_primitive:
244 * @path: an #AdgPath
246 * Gets the last primitive appended to @path. The returned struct
247 * is owned by @path and should not be freed or modified.
249 * Returns: a pointer to the last appended primitive or %NULL on errors
251 * Since: 1.0
253 const CpmlPrimitive *
254 adg_path_last_primitive(AdgPath *path)
256 AdgPathPrivate *data;
258 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
260 data = path->data;
262 return &data->last;
266 * adg_path_over_primitive:
267 * @path: an #AdgPath
269 * Gets the primitive before the last one appended to @path. The
270 * "over" term comes from forth, where the %OVER operator works
271 * on the stack in the same way as adg_path_over_primitive() works
272 * on @path. The returned struct is owned by @path and should not
273 * be freed or modified.
275 * Returns: a pointer to the primitive before the last appended one
276 * or %NULL on errors
278 * Since: 1.0
280 const CpmlPrimitive *
281 adg_path_over_primitive(AdgPath *path)
283 AdgPathPrivate *data;
285 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
287 data = path->data;
289 return &data->over;
293 * adg_path_append:
294 * @path: an #AdgPath
295 * @type: a #cairo_data_type_t value
296 * @...: point data, specified as #CpmlPair pointers
298 * Generic method to append a primitive to @path. The number of #CpmlPair
299 * pointers to pass as @Varargs depends on @type: #CPML_CLOSE does not
300 * require any pair, #CPML_MOVE and #CPML_LINE require one pair,
301 * #CPML_ARC two pairs, #CPML_CURVE three pairs and so on.
303 * All the needed pairs must be not %NULL pointers, otherwise the function
304 * will fail. The pairs in excess, if any, will be ignored.
306 * Since: 1.0
308 void
309 adg_path_append(AdgPath *path, CpmlPrimitiveType type, ...)
311 va_list var_args;
313 va_start(var_args, type);
314 adg_path_append_valist(path, type, var_args);
315 va_end(var_args);
319 * adg_path_append_valist:
320 * @path: an #AdgPath
321 * @type: a #cairo_data_type_t value
322 * @var_args: point data, specified as #CpmlPair pointers
324 * va_list version of adg_path_append().
326 * Since: 1.0
328 void
329 adg_path_append_valist(AdgPath *path, CpmlPrimitiveType type, va_list var_args)
331 GArray *array;
332 CpmlPair *pair;
333 gint length;
335 length = _adg_primitive_length(type);
336 if (length == 0)
337 return;
339 array = g_array_new(TRUE, FALSE, sizeof(pair));
340 while (-- length) {
341 pair = va_arg(var_args, CpmlPair *);
342 g_array_append_val(array, pair);
345 adg_path_append_array(path, type, (const CpmlPair **) array->data);
346 g_array_free(array, TRUE);
350 * adg_path_append_array:
351 * @path: an #AdgPath
352 * @type: a #cairo_data_type_t value
353 * @pairs: (array zero-terminated=1) (element-type Adg.Pair) (transfer none): point data, specified as a %NULL terminated array of #CpmlPair pointers
355 * A bindingable version of adg_path_append() that uses a %NULL terminated
356 * array of pairs instead of variable argument list and friends.
358 * Furthermore, because of the list is %NULL terminated, an arbitrary
359 * number of pairs can be passed in @pairs. This allows to embed in a
360 * primitive element more data pairs than requested, something impossible
361 * to do with adg_path_append() and adg_path_append_valist().
363 * Rename to: adg_path_append
364 * Since: 1.0
366 void
367 adg_path_append_array(AdgPath *path, CpmlPrimitiveType type,
368 const CpmlPair **pairs)
370 gint length;
371 GArray *array;
372 const CpmlPair **pair;
373 cairo_path_data_t path_data;
375 g_return_if_fail(ADG_IS_PATH(path));
376 g_return_if_fail(pairs != NULL);
378 length = _adg_primitive_length(type);
379 if (length == 0)
380 return;
382 array = g_array_new(FALSE, FALSE, sizeof(path_data));
383 for (pair = pairs; *pair != NULL; ++ pair) {
384 cpml_pair_to_cairo(*pair, &path_data);
385 g_array_append_val(array, path_data);
388 if (array->len < length - 1) {
389 /* Not enough pairs have been provided */
390 g_warning(_("%s: null pair caught while parsing arguments"), G_STRLOC);
391 } else {
392 AdgPathPrivate *data;
393 CpmlPrimitive primitive;
394 cairo_path_data_t org;
396 /* Save a copy of the current point as primitive origin */
397 data = path->data;
398 cpml_pair_to_cairo(&data->cp, &org);
400 /* Prepend the cairo header */
401 path_data.header.type = type;
402 path_data.header.length = array->len + 1;
403 g_array_prepend_val(array, path_data);
405 /* Append a new primitive to @path */
406 primitive.segment = NULL;
407 primitive.org = &org;
408 primitive.data = (cairo_path_data_t *) array->data;
409 _adg_append_primitive(path, &primitive);
412 g_array_free(array, TRUE);
417 * adg_path_append_primitive:
418 * @path: an #AdgPath
419 * @primitive: the #CpmlPrimitive to append
421 * Appends @primitive to @path. The primitive to add is considered the
422 * continuation of the current path so the <structfield>org</structfield>
423 * component of @primitive is not used. Anyway the current point is
424 * checked against it: they must be equal or the function will fail
425 * without further processing.
427 * Since: 1.0
429 void
430 adg_path_append_primitive(AdgPath *path, const CpmlPrimitive *primitive)
432 AdgPathPrivate *data;
433 CpmlPrimitive *primitive_dup;
435 g_return_if_fail(ADG_IS_PATH(path));
436 g_return_if_fail(primitive != NULL);
437 g_return_if_fail(primitive->org != NULL);
439 data = path->data;
441 g_return_if_fail(primitive->org->point.x == data->cp.x &&
442 primitive->org->point.y == data->cp.y);
444 /* The primitive data could be modified by pending operations:
445 * work on a copy */
446 primitive_dup = cpml_primitive_deep_dup(primitive);
448 _adg_append_primitive(path, primitive_dup);
450 g_free(primitive_dup);
454 * adg_path_append_segment:
455 * @path: an #AdgPath
456 * @segment: the #CpmlSegment to append
458 * Appends @segment to @path.
460 * Since: 1.0
462 void
463 adg_path_append_segment(AdgPath *path, const CpmlSegment *segment)
465 AdgPathPrivate *data;
467 g_return_if_fail(ADG_IS_PATH(path));
468 g_return_if_fail(segment != NULL);
470 data = path->data;
472 _adg_clear_parent((AdgModel *) path);
473 data->cpml.array = g_array_append_vals(data->cpml.array,
474 segment->data, segment->num_data);
478 * adg_path_append_cpml_path:
479 * @path: an #AdgPath
480 * @cpml_path: the #cairo_path_t path to append
482 * Appends a whole #CpmlPath to @path. #CpmlPath is a superset of
483 * #cairo_path_t, so this function can be feeded with both.
485 * Since: 1.0
487 void
488 adg_path_append_cpml_path(AdgPath *path, const CpmlPath *cpml_path)
490 AdgPathPrivate *data;
492 g_return_if_fail(ADG_IS_PATH(path));
493 g_return_if_fail(cpml_path != NULL);
495 data = path->data;
497 _adg_clear_parent((AdgModel *) path);
498 data->cpml.array = g_array_append_vals(data->cpml.array,
499 cpml_path->data,
500 cpml_path->num_data);
504 * adg_path_move_to:
505 * @path: an #AdgPath
506 * @pair: the destination coordinates
508 * Begins a new segment. After this call the current point will be @pair.
510 * Since: 1.0
512 void
513 adg_path_move_to(AdgPath *path, const CpmlPair *pair)
515 adg_path_append(path, CPML_MOVE, pair);
519 * adg_path_move_to_explicit:
520 * @path: an #AdgPath
521 * @x: the new x coordinate
522 * @y: the new y coordinate
524 * Convenient function to call adg_path_move_to() using explicit
525 * coordinates instead of #CpmlPair.
527 * Since: 1.0
529 void
530 adg_path_move_to_explicit(AdgPath *path, gdouble x, gdouble y)
532 CpmlPair p;
534 p.x = x;
535 p.y = y;
537 adg_path_append(path, CPML_MOVE, &p);
541 * adg_path_line_to:
542 * @path: an #AdgPath
543 * @pair: the destination coordinates
545 * Adds a line to @path from the current point to @pair. After this
546 * call the current point will be @pair.
548 * If @path has no current point before this call, this function will
549 * trigger a warning without other effect.
551 * Since: 1.0
553 void
554 adg_path_line_to(AdgPath *path, const CpmlPair *pair)
556 adg_path_append(path, CPML_LINE, pair);
560 * adg_path_line_to_explicit:
561 * @path: an #AdgPath
562 * @x: the new x coordinate
563 * @y: the new y coordinate
565 * Convenient function to call adg_path_line_to() using explicit
566 * coordinates instead of #CpmlPair.
568 * Since: 1.0
570 void
571 adg_path_line_to_explicit(AdgPath *path, gdouble x, gdouble y)
573 CpmlPair p;
575 p.x = x;
576 p.y = y;
578 adg_path_append(path, CPML_LINE, &p);
582 * adg_path_arc_to:
583 * @path: an #AdgPath
584 * @throught: an arbitrary point on the arc
585 * @pair: the destination coordinates
587 * Adds an arc to the path from the current point to @pair, passing
588 * throught @throught. After this call the current point will be @pair.
590 * If @path has no current point before this call, this function will
591 * trigger a warning without other effect.
593 * Since: 1.0
595 void
596 adg_path_arc_to(AdgPath *path, const CpmlPair *throught, const CpmlPair *pair)
598 adg_path_append(path, CPML_ARC, throught, pair);
602 * adg_path_arc_to_explicit:
603 * @path: an #AdgPath
604 * @x1: the x coordinate of an intermediate point
605 * @y1: the y coordinate of an intermediate point
606 * @x2: the x coordinate of the end of the arc
607 * @y2: the y coordinate of the end of the arc
609 * Convenient function to call adg_path_arc_to() using explicit
610 * coordinates instead of #CpmlPair.
612 * Since: 1.0
614 void
615 adg_path_arc_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
616 gdouble x2, gdouble y2)
618 CpmlPair p[2];
620 p[0].x = x1;
621 p[0].y = y1;
622 p[1].x = x2;
623 p[1].y = y2;
625 adg_path_append(path, CPML_ARC, &p[0], &p[1]);
629 * adg_path_curve_to:
630 * @path: an #AdgPath
631 * @control1: the first control point of the curve
632 * @control2: the second control point of the curve
633 * @pair: the destination coordinates
635 * Adds a cubic Bézier curve to the path from the current point to
636 * position @pair, using @control1 and @control2 as control points.
637 * After this call the current point will be @pair.
639 * If @path has no current point before this call, this function will
640 * trigger a warning without other effect.
642 * Since: 1.0
644 void
645 adg_path_curve_to(AdgPath *path, const CpmlPair *control1,
646 const CpmlPair *control2, const CpmlPair *pair)
648 adg_path_append(path, CPML_CURVE, control1, control2, pair);
652 * adg_path_curve_to_explicit:
653 * @path: an #AdgPath
654 * @x1: the x coordinate of the first control point
655 * @y1: the y coordinate of the first control point
656 * @x2: the x coordinate of the second control point
657 * @y2: the y coordinate of the second control point
658 * @x3: the x coordinate of the end of the curve
659 * @y3: the y coordinate of the end of the curve
661 * Convenient function to call adg_path_curve_to() using explicit
662 * coordinates instead of #CpmlPair.
664 * Since: 1.0
666 void
667 adg_path_curve_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
668 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
670 CpmlPair p[3];
672 p[0].x = x1;
673 p[0].y = y1;
674 p[1].x = x2;
675 p[1].y = y2;
676 p[2].x = x3;
677 p[2].y = y3;
679 adg_path_append(path, CPML_CURVE, &p[0], &p[1], &p[2]);
683 * adg_path_close:
684 * @path: an #AdgPath
686 * Adds a line segment to the path from the current point to the
687 * beginning of the current segment, (the most recent point passed
688 * to an adg_path_move_to()), and closes this segment.
689 * After this call the current point will be unset.
691 * The behavior of adg_path_close() is distinct from simply calling
692 * adg_line_to() with the coordinates of the segment starting point.
693 * When a closed segment is stroked, there are no caps on the ends.
694 * Instead, there is a line join connecting the final and initial
695 * primitive of the segment.
697 * If @path has no current point before this call, this function will
698 * trigger a warning without other effect.
700 * Since: 1.0
702 void
703 adg_path_close(AdgPath *path)
705 adg_path_append(path, CPML_CLOSE);
709 * adg_path_arc:
710 * @path: an #AdgPath
711 * @center: coordinates of the center of the arc
712 * @r: the radius of the arc
713 * @start: the start angle, in radians
714 * @end: the end angle, in radians
716 * A more usual way to add an arc to @path. After this call, the current
717 * point will be the computed end point of the arc. The arc will be
718 * rendered in increasing angle, accordling to @start and @end. This means
719 * if @start is less than @end, the arc will be rendered in clockwise
720 * direction (accordling to the default cairo coordinate system) while if
721 * @start is greather than @end, the arc will be rendered in couterclockwise
722 * direction.
724 * By explicitely setting the whole arc data, the start point could be
725 * different from the current point. In this case, if @path has no
726 * current point before the call a #CPML_MOVE to the start point of
727 * the arc will be automatically prepended to the arc. If @path has a
728 * current point, a #CPML_LINE to the start point of the arc will be
729 * used instead of the "move to" primitive.
731 * Since: 1.0
733 void
734 adg_path_arc(AdgPath *path, const CpmlPair *center, gdouble r,
735 gdouble start, gdouble end)
737 AdgPathPrivate *data;
738 CpmlPair p[3];
740 g_return_if_fail(ADG_IS_PATH(path));
741 g_return_if_fail(center != NULL);
743 data = path->data;
744 cpml_vector_from_angle(&p[0], start);
745 cpml_vector_from_angle(&p[1], (end-start) / 2);
746 cpml_vector_from_angle(&p[2], end);
748 cpml_vector_set_length(&p[0], r);
749 cpml_vector_set_length(&p[1], r);
750 cpml_vector_set_length(&p[2], r);
752 p[0].x += center->x;
753 p[0].y += center->y;
754 p[1].x += center->x;
755 p[1].y += center->y;
756 p[2].x += center->x;
757 p[2].y += center->y;
759 if (!data->cp_is_valid)
760 adg_path_append(path, CPML_MOVE, &p[0]);
761 else if (p[0].x != data->cp.x || p[0].y != data->cp.y)
762 adg_path_append(path, CPML_LINE, &p[0]);
764 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
768 * adg_path_arc_explicit:
769 * @path: an #AdgPath
770 * @xc: x position of the center of the arc
771 * @yc: y position of the center of the arc
772 * @r: the radius of the arc
773 * @start: the start angle, in radians
774 * @end: the end angle, in radians
776 * Convenient function to call adg_path_arc() using explicit
777 * coordinates instead of #CpmlPair.
779 * Since: 1.0
781 void
782 adg_path_arc_explicit(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
783 gdouble start, gdouble end)
785 CpmlPair center;
787 center.x = xc;
788 center.y = yc;
790 adg_path_arc(path, &center, r, start, end);
794 * adg_path_chamfer:
795 * @path: an #AdgPath
796 * @delta1: the distance from the intersection point of the current primitive
797 * @delta2: the distance from the intersection point of the next primitive
799 * A binary action that generates a chamfer between two primitives.
800 * The first primitive involved is the current primitive, the second will
801 * be the next primitive appended to @path after this call. The second
802 * primitive is required: if the chamfer operation is not properly
803 * terminated (by not providing the second primitive), any API accessing
804 * the path in reading mode will raise a warning.
806 * An exception is a chamfer after a #CPML_CLOSE primitive. In this case,
807 * the second primitive is not required: the current close path is used
808 * as first operand while the first primitive of the current segment is
809 * used as second operand.
811 * The chamfer operation requires two lengths: @delta1 specifies the
812 * "quantity" to trim on the first primitive while @delta2 is the same
813 * applied on the second primitive. The term "quantity" means the length
814 * of the portion to cut out from the original primitive (that is the
815 * primitive as would be without the chamfer).
817 * Since: 1.0
819 void
820 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
822 g_return_if_fail(ADG_IS_PATH(path));
824 if (!_adg_append_operation(path, ADG_ACTION_CHAMFER, delta1, delta2))
825 return;
829 * adg_path_fillet:
830 * @path: an #AdgPath
831 * @radius: the radius of the fillet
833 * A binary action that joins to primitives with an arc.
834 * The first primitive involved is the current primitive, the second will
835 * be the next primitive appended to @path after this call. The second
836 * primitive is required: if the fillet operation is not properly
837 * terminated (by not providing the second primitive), any API accessing
838 * the path in reading mode will raise a warning.
840 * An exception is a fillet after a #CPML_CLOSE primitive. In this case,
841 * the second primitive is not required: the current close path is used
842 * as first operand while the first primitive of the current segment is
843 * used as second operand.
845 * Since: 1.0
847 void
848 adg_path_fillet(AdgPath *path, gdouble radius)
850 g_return_if_fail(ADG_IS_PATH(path));
852 if (!_adg_append_operation(path, ADG_ACTION_FILLET, radius))
853 return;
857 * adg_path_reflect:
858 * @path: an #AdgPath
859 * @vector: the slope of the axis
861 * Reflects the first segment or @path around the axis passing
862 * throught (0, 0) and with a @vector slope. The internal segment
863 * is duplicated and the proper transformation (computed from
864 * @vector) to mirror the segment is applied on all its points.
865 * The result is then reversed with cpml_segment_reverse() and
866 * appended to the original path with adg_path_append_segment().
868 * For convenience, if @vector is %NULL the path is reversed
869 * around the x axis (y=0).
871 * Since: 1.0
873 void
874 adg_path_reflect(AdgPath *path, const CpmlVector *vector)
876 AdgModel *model;
877 cairo_matrix_t matrix;
878 CpmlSegment segment, *dup_segment;
880 g_return_if_fail(ADG_IS_PATH(path));
881 g_return_if_fail(vector == NULL || vector->x != 0 || vector->y != 0);
883 model = (AdgModel *) path;
885 if (vector == NULL) {
886 cairo_matrix_init_scale(&matrix, 1, -1);
887 } else {
888 CpmlVector slope;
889 gdouble cos2angle, sin2angle;
891 cpml_pair_copy(&slope, vector);
892 cpml_vector_set_length(&slope, 1);
894 if (slope.x == 0 && slope.y == 0) {
895 g_warning(_("%s: the axis of the reflection is not known"),
896 G_STRLOC);
897 return;
900 sin2angle = 2. * vector->x * vector->y;
901 cos2angle = 2. * vector->x * vector->x - 1;
903 cairo_matrix_init(&matrix, cos2angle, sin2angle,
904 sin2angle, -cos2angle, 0, 0);
907 if (!adg_trail_put_segment((AdgTrail *) path, 1, &segment))
908 return;
910 /* No need to reverse an empty segment */
911 if (segment.num_data == 0 || segment.num_data == 0)
912 return;
914 dup_segment = cpml_segment_deep_dup(&segment);
915 if (dup_segment == NULL)
916 return;
918 cpml_segment_reverse(dup_segment);
919 cpml_segment_transform(dup_segment, &matrix);
920 dup_segment->data[0].header.type = CPML_LINE;
922 adg_path_append_segment(path, dup_segment);
924 g_free(dup_segment);
926 _adg_dup_reverse_named_pairs(model, &matrix);
930 * adg_path_reflect_explicit:
931 * @path: an #AdgPath
932 * @x: the vector x component
933 * @y: the vector y component
935 * Convenient function to call adg_path_reflect() using explicit
936 * vector components instead of #CpmlVector.
938 * Since: 1.0
940 void
941 adg_path_reflect_explicit(AdgPath *path, gdouble x, gdouble y)
943 CpmlVector vector;
945 vector.x = x;
946 vector.y = y;
948 adg_path_reflect(path, &vector);
952 static void
953 _adg_clear(AdgModel *model)
955 AdgPath *path;
956 AdgPathPrivate *data;
958 path = (AdgPath *) model;
959 data = path->data;
961 g_array_set_size(data->cpml.array, 0);
962 _adg_clear_operation(path);
963 _adg_clear_parent(model);
966 static void
967 _adg_clear_parent(AdgModel *model)
969 if (_ADG_OLD_MODEL_CLASS->clear)
970 _ADG_OLD_MODEL_CLASS->clear(model);
973 static void
974 _adg_changed(AdgModel *model)
976 _adg_clear_parent(model);
978 if (_ADG_OLD_MODEL_CLASS->changed)
979 _ADG_OLD_MODEL_CLASS->changed(model);
982 static CpmlPath *
983 _adg_get_cpml_path(AdgTrail *trail)
985 _adg_clear_parent((AdgModel *) trail);
986 return _adg_read_cpml_path((AdgPath *) trail);
989 static CpmlPath *
990 _adg_read_cpml_path(AdgPath *path)
992 AdgPathPrivate *data = path->data;
994 /* Always regenerate the CpmlPath as it is a trivial operation */
995 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
996 data->cpml.path.data = (cairo_path_data_t *) (data->cpml.array)->data;
997 data->cpml.path.num_data = (data->cpml.array)->len;
999 return &data->cpml.path;
1002 static gint
1003 _adg_primitive_length(CpmlPrimitiveType type)
1005 if (type == CPML_CLOSE)
1006 return 1;
1007 else if (type == CPML_MOVE)
1008 return 2;
1010 return cpml_primitive_type_get_n_points(type);
1013 static void
1014 _adg_append_primitive(AdgPath *path, CpmlPrimitive *current)
1016 AdgPathPrivate *data;
1017 cairo_path_data_t *path_data;
1018 int length;
1020 data = path->data;
1021 path_data = current->data;
1022 length = path_data[0].header.length;
1024 /* Execute any pending operation */
1025 _adg_do_operation(path, path_data);
1027 /* Append the path data to the internal path array */
1028 data->cpml.array = g_array_append_vals(data->cpml.array,
1029 path_data, length);
1031 /* Set path data to point to the recently appended cairo_path_data_t
1032 * primitive: the first struct is the header */
1033 path_data = (cairo_path_data_t *) (data->cpml.array)->data +
1034 (data->cpml.array)->len - length;
1036 /* Store the over primitive */
1037 memcpy(&data->over, &data->last, sizeof(CpmlPrimitive));
1039 /* Set the last primitive for subsequent binary operations */
1040 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
1041 data->last.segment = NULL;
1042 data->last.data = path_data;
1044 /* Save the last point as the current point, if applicable */
1045 data->cp_is_valid = length > 1;
1046 if (length > 1)
1047 cpml_pair_from_cairo(&data->cp, &path_data[length-1]);
1049 /* Invalidate cairo_path: should be recomputed */
1050 _adg_clear_parent((AdgModel *) path);
1053 static void
1054 _adg_clear_operation(AdgPath *path)
1056 AdgPathPrivate *data;
1057 AdgOperation *operation;
1059 data = path->data;
1060 operation = &data->operation;
1062 if (operation->action != ADG_ACTION_NONE) {
1063 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
1064 G_STRLOC, _adg_action_name(operation->action));
1065 operation->action = ADG_ACTION_NONE;
1068 data->cp_is_valid = FALSE;
1069 data->last.data = NULL;
1070 data->over.data = NULL;
1073 static gboolean
1074 _adg_append_operation(AdgPath *path, AdgAction action, ...)
1076 AdgPathPrivate *data;
1077 AdgOperation *operation;
1078 va_list var_args;
1080 data = path->data;
1082 if (data->last.data == NULL) {
1083 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
1084 G_STRLOC, _adg_action_name(action));
1085 return FALSE;
1088 operation = &data->operation;
1089 if (operation->action != ADG_ACTION_NONE) {
1090 g_warning(_("%s: requested a `%s' operation while a `%s' operation was active"),
1091 G_STRLOC, _adg_action_name(action),
1092 _adg_action_name(operation->action));
1093 /* XXX: http://dev.entidi.com/p/adg/issues/50/ */
1094 return FALSE;
1097 va_start(var_args, action);
1099 switch (action) {
1101 case ADG_ACTION_CHAMFER:
1102 operation->data.chamfer.delta1 = va_arg(var_args, double);
1103 operation->data.chamfer.delta2 = va_arg(var_args, double);
1104 break;
1106 case ADG_ACTION_FILLET:
1107 operation->data.fillet.radius = va_arg(var_args, double);
1108 break;
1110 case ADG_ACTION_NONE:
1111 va_end(var_args);
1112 return TRUE;
1114 default:
1115 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC, action);
1116 va_end(var_args);
1117 return FALSE;
1120 operation->action = action;
1121 va_end(var_args);
1123 if (data->last.data[0].header.type == CAIRO_PATH_CLOSE_PATH) {
1124 /* Special case: an action with the close primitive should
1125 * be resolved now by changing the close primitive to a
1126 * line-to and using it as second operand and use the first
1127 * primitive of the current segment as first operand */
1128 guint length;
1129 cairo_path_data_t *path_data;
1130 CpmlSegment segment;
1131 CpmlPrimitive current;
1133 length = data->cpml.array->len;
1135 /* Ensure the close path primitive is not the only data */
1136 g_return_val_if_fail(length > 1, FALSE);
1138 /* Allocate one more item once for all to accept the
1139 * conversion from a close to line-to primitive */
1140 data->cpml.array = g_array_set_size(data->cpml.array, length + 1);
1141 path_data = (cairo_path_data_t *) data->cpml.array->data;
1142 --data->cpml.array->len;
1144 /* Set segment and current (the first primitive of segment) */
1145 cpml_segment_from_cairo(&segment, _adg_read_cpml_path(path));
1146 while (cpml_segment_next(&segment))
1148 cpml_primitive_from_segment(&current, &segment);
1150 /* Convert close path to a line-to primitive */
1151 ++data->cpml.array->len;
1152 path_data[length - 1].header.type = CPML_LINE;
1153 path_data[length - 1].header.length = 2;
1154 path_data[length] = *current.org;
1156 data->last.segment = &segment;
1157 data->last.org = &path_data[length - 2];
1158 data->last.data = &path_data[length - 1];
1160 _adg_do_action(path, action, &current);
1164 return TRUE;
1167 static void
1168 _adg_do_operation(AdgPath *path, cairo_path_data_t *path_data)
1170 AdgPathPrivate *data;
1171 AdgAction action;
1172 CpmlSegment segment;
1173 CpmlPrimitive current;
1174 cairo_path_data_t current_org;
1176 data = path->data;
1177 action = data->operation.action;
1178 cpml_segment_from_cairo(&segment, _adg_read_cpml_path(path));
1180 /* Construct the current primitive, that is the primitive to be
1181 * mixed with the last primitive with the specified operation.
1182 * Its org is a copy of the end point of the last primitive: it can be
1183 * modified without affecting anything else. It is expected the operation
1184 * functions will add to @path the primitives required but NOT to add
1185 * @current, as this one will be inserted automatically. */
1186 current.segment = &segment;
1187 current.org = &current_org;
1188 current.data = path_data;
1189 cpml_pair_to_cairo(&data->cp, &current_org);
1191 _adg_do_action(path, action, &current);
1194 static void
1195 _adg_do_action(AdgPath *path, AdgAction action, CpmlPrimitive *primitive)
1197 switch (action) {
1198 case ADG_ACTION_NONE:
1199 return;
1200 case ADG_ACTION_CHAMFER:
1201 _adg_do_chamfer(path, primitive);
1202 break;
1203 case ADG_ACTION_FILLET:
1204 _adg_do_fillet(path, primitive);
1205 break;
1206 default:
1207 g_return_if_reached();
1211 static void
1212 _adg_do_chamfer(AdgPath *path, CpmlPrimitive *current)
1214 AdgPathPrivate *data;
1215 CpmlPrimitive *last;
1216 gdouble delta1, delta2;
1217 gdouble len1, len2;
1218 CpmlPair pair;
1220 data = path->data;
1221 last = &data->last;
1222 delta1 = data->operation.data.chamfer.delta1;
1223 len1 = cpml_primitive_get_length(last);
1225 if (delta1 >= len1) {
1226 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1227 G_STRLOC, delta1, len1);
1228 return;
1231 delta2 = data->operation.data.chamfer.delta2;
1232 len2 = cpml_primitive_get_length(current);
1234 if (delta2 >= len2) {
1235 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1236 G_STRLOC, delta1, len1);
1237 return;
1240 /* Change the end point of the last primitive */
1241 cpml_primitive_put_pair_at(last, 1. - delta1 / len1, &pair);
1242 cpml_primitive_set_point(last, -1, &pair);
1244 /* Change the start point of the current primitive */
1245 cpml_primitive_put_pair_at(current, delta2 / len2, &pair);
1246 cpml_primitive_set_point(current, 0, &pair);
1248 /* Add the chamfer line */
1249 data->operation.action = ADG_ACTION_NONE;
1250 adg_path_append(path, CPML_LINE, &pair);
1253 static void
1254 _adg_do_fillet(AdgPath *path, CpmlPrimitive *current)
1256 AdgPathPrivate *data;
1257 CpmlPrimitive *last, *current_dup, *last_dup;
1258 gdouble radius, offset, pos;
1259 CpmlPair center, vector, p[3];
1261 data = path->data;
1262 last = &data->last;
1263 current_dup = cpml_primitive_deep_dup(current);
1264 last_dup = cpml_primitive_deep_dup(last);
1265 radius = data->operation.data.fillet.radius;
1266 offset = _adg_is_convex(last_dup, current_dup) ? -radius : radius;
1268 /* Find the center of the fillet from the intersection between
1269 * the last and current primitives offseted by radius */
1270 cpml_primitive_offset(current_dup, offset);
1271 cpml_primitive_offset(last_dup, offset);
1272 if (cpml_primitive_put_intersections(current_dup, last_dup, 1, &center) == 0) {
1273 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1274 G_STRLOC, radius);
1275 g_free(current_dup);
1276 g_free(last_dup);
1277 return;
1280 /* Compute the start point of the fillet */
1281 pos = cpml_primitive_get_closest_pos(last_dup, &center);
1282 cpml_primitive_put_vector_at(last_dup, pos, &vector);
1283 cpml_vector_set_length(&vector, offset);
1284 cpml_vector_normal(&vector);
1285 p[0].x = center.x - vector.x;
1286 p[0].y = center.y - vector.y;
1288 /* Compute the mid point of the fillet */
1289 cpml_pair_from_cairo(&vector, current->org);
1290 vector.x -= center.x;
1291 vector.y -= center.y;
1292 cpml_vector_set_length(&vector, radius);
1293 p[1].x = center.x + vector.x;
1294 p[1].y = center.y + vector.y;
1296 /* Compute the end point of the fillet */
1297 pos = cpml_primitive_get_closest_pos(current_dup, &center);
1298 cpml_primitive_put_vector_at(current_dup, pos, &vector);
1299 cpml_vector_set_length(&vector, offset);
1300 cpml_vector_normal(&vector);
1301 p[2].x = center.x - vector.x;
1302 p[2].y = center.y - vector.y;
1304 g_free(current_dup);
1305 g_free(last_dup);
1307 /* Change the end point of the last primitive */
1308 cpml_primitive_set_point(last, -1, &p[0]);
1310 /* Change the start point of the current primitive */
1311 cpml_primitive_set_point(current, 0, &p[2]);
1313 /* Add the fillet arc */
1314 data->operation.action = ADG_ACTION_NONE;
1315 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
1318 static gboolean
1319 _adg_is_convex(const CpmlPrimitive *primitive1, const CpmlPrimitive *primitive2)
1321 CpmlVector v1, v2;
1322 gdouble angle1, angle2;
1324 cpml_primitive_put_vector_at(primitive1, -1, &v1);
1325 cpml_primitive_put_vector_at(primitive2, 0, &v2);
1327 /* Probably there is a smarter way to get this without trygonometry */
1328 angle1 = cpml_vector_angle(&v1);
1329 angle2 = cpml_vector_angle(&v2);
1331 if (angle1 > angle2)
1332 angle1 -= G_PI*2;
1334 return angle2-angle1 > G_PI;
1337 static const gchar *
1338 _adg_action_name(AdgAction action)
1340 switch (action) {
1341 case ADG_ACTION_NONE:
1342 return "NULL";
1343 case ADG_ACTION_CHAMFER:
1344 return "CHAMFER";
1345 case ADG_ACTION_FILLET:
1346 return "FILLET";
1349 return "undefined";
1352 static void
1353 _adg_get_named_pair(AdgModel *model, const gchar *name,
1354 CpmlPair *pair, gpointer user_data)
1356 GSList **named_pairs;
1357 AdgNamedPair *named_pair;
1359 named_pairs = user_data;
1361 named_pair = g_new(AdgNamedPair, 1);
1362 named_pair->name = name;
1363 named_pair->pair = *pair;
1365 *named_pairs = g_slist_prepend(*named_pairs, named_pair);
1368 static void
1369 _adg_dup_reverse_named_pairs(AdgModel *model, const cairo_matrix_t *matrix)
1371 AdgNamedPair *old_named_pair;
1372 AdgNamedPair named_pair;
1373 GSList *named_pairs;
1375 /* Populate named_pairs with all the named pairs of model */
1376 named_pairs = NULL;
1377 adg_model_foreach_named_pair(model, _adg_get_named_pair, &named_pairs);
1379 /* Readd the pairs applying the reversing transformation matrix to
1380 * their coordinates and prepending a "-" to their name */
1381 while (named_pairs) {
1382 old_named_pair = named_pairs->data;
1384 named_pair.name = g_strdup_printf("-%s", old_named_pair->name);
1385 named_pair.pair = old_named_pair->pair;
1386 cpml_pair_transform(&named_pair.pair, matrix);
1388 adg_model_set_named_pair(model, named_pair.name, &named_pair.pair);
1390 g_free((gpointer) named_pair.name);
1391 named_pairs = g_slist_delete_link(named_pairs, named_pairs);