[ADG] Moved include dependencies from .h to .c
[adg.git] / src / adg / adg-path.c
bloba0079ae422c3d788274989829643ae10fde541b1
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011 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.
49 **/
51 /**
52 * AdgPath:
54 * All fields are private and should not be used directly.
55 * Use its public methods instead.
56 **/
59 #include "adg-internal.h"
60 #include "adg-model.h"
61 #include "adg-trail.h"
62 #include <string.h>
63 #include <math.h>
65 #include "adg-path.h"
66 #include "adg-path-private.h"
69 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_path_parent_class)
70 #define _ADG_OLD_MODEL_CLASS ((AdgModelClass *) adg_path_parent_class)
73 G_DEFINE_TYPE(AdgPath, adg_path, ADG_TYPE_TRAIL);
76 static void _adg_finalize (GObject *object);
77 static void _adg_clear (AdgModel *model);
78 static void _adg_clear_parent (AdgModel *model);
79 static void _adg_changed (AdgModel *model);
80 static CpmlPath * _adg_get_cpml_path (AdgTrail *trail);
81 static CpmlPath * _adg_read_cpml_path (AdgPath *path);
82 static void _adg_append_primitive (AdgPath *path,
83 AdgPrimitive *primitive);
84 static void _adg_clear_operation (AdgPath *path);
85 static gboolean _adg_append_operation (AdgPath *path,
86 AdgAction action,
87 ...);
88 static void _adg_do_operation (AdgPath *path,
89 cairo_path_data_t
90 *path_data);
91 static void _adg_do_action (AdgPath *path,
92 AdgAction action,
93 AdgPrimitive *primitive);
94 static void _adg_do_chamfer (AdgPath *path,
95 AdgPrimitive *current);
96 static void _adg_do_fillet (AdgPath *path,
97 AdgPrimitive *current);
98 static gboolean _adg_is_convex (const AdgPrimitive
99 *primitive1,
100 const AdgPrimitive
101 *primitive2);
102 static const gchar * _adg_action_name (AdgAction action);
103 static void _adg_get_named_pair (AdgModel *model,
104 const gchar *name,
105 AdgPair *pair,
106 gpointer user_data);
107 static void _adg_dup_reverse_named_pairs
108 (AdgModel *model,
109 const AdgMatrix*matrix);
112 static void
113 adg_path_class_init(AdgPathClass *klass)
115 GObjectClass *gobject_class;
116 AdgModelClass *model_class;
117 AdgTrailClass *trail_class;
119 gobject_class = (GObjectClass *) klass;
120 model_class = (AdgModelClass *) klass;
121 trail_class = (AdgTrailClass *) klass;
123 g_type_class_add_private(klass, sizeof(AdgPathPrivate));
125 gobject_class->finalize = _adg_finalize;
127 model_class->clear = _adg_clear;
128 model_class->changed = _adg_changed;
130 trail_class->get_cpml_path = _adg_get_cpml_path;
133 static void
134 adg_path_init(AdgPath *path)
136 AdgPathPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(path, ADG_TYPE_PATH,
137 AdgPathPrivate);
139 data->cp_is_valid = FALSE;
140 data->cpml.array = g_array_new(FALSE, FALSE, sizeof(cairo_path_data_t));
141 data->operation.action = ADG_ACTION_NONE;
143 path->data = data;
146 static void
147 _adg_finalize(GObject *object)
149 AdgPath *path;
150 AdgPathPrivate *data;
152 path = (AdgPath *) object;
153 data = path->data;
155 g_array_free(data->cpml.array, TRUE);
156 _adg_clear_operation(path);
158 if (_ADG_OLD_OBJECT_CLASS->finalize)
159 _ADG_OLD_OBJECT_CLASS->finalize(object);
164 * adg_path_new:
166 * Creates a new path model. The path should be constructed
167 * programmatically by using the methods provided by #AdgPath.
169 * Returns: the newly created path model
171 AdgPath *
172 adg_path_new(void)
174 return g_object_new(ADG_TYPE_PATH, NULL);
178 * adg_path_get_current_point:
179 * @path: an #AdgPath
181 * Gets the current point of @path, which is conceptually the
182 * final point reached by the path so far.
184 * If there is no defined current point, %NULL is returned.
185 * It is possible to check this in advance with
186 * adg_path_has_current_point().
188 * Most #AdgPath methods alter the current point and most of them
189 * expect a current point to be defined otherwise will fail triggering
190 * a warning. Check the description of every method for specific details.
192 * Returns: the current point or %NULL on no current point set or errors
194 const AdgPair *
195 adg_path_get_current_point(AdgPath *path)
197 AdgPathPrivate *data;
199 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
201 data = path->data;
203 if (!data->cp_is_valid)
204 return NULL;
206 return &data->cp;
210 * adg_path_has_current_point:
211 * @path: an #AdgPath
213 * Returns whether a current point is defined on @path.
214 * See adg_path_get_current_point() for details on the current point.
216 * Returns: whether a current point is defined
218 gboolean
219 adg_path_has_current_point(AdgPath *path)
221 AdgPathPrivate *data;
223 g_return_val_if_fail(ADG_IS_PATH(path), FALSE);
225 data = path->data;
227 return data->cp_is_valid;
231 * adg_path_last_primitive:
232 * @path: an #AdgPath
234 * Gets the last primitive appended to @path. The returned struct
235 * is owned by @path and should not be freed or modified.
237 * Returns: a pointer to the last appended primitive or %NULL on errors
239 const AdgPrimitive *
240 adg_path_last_primitive(AdgPath *path)
242 AdgPathPrivate *data;
244 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
246 data = path->data;
248 return &data->last;
252 * adg_path_over_primitive:
253 * @path: an #AdgPath
255 * Gets the primitive before the last one appended to @path. The
256 * "over" term comes from forth, where the %OVER operator works
257 * on the stack in the same way as adg_path_over_primitive() works
258 * on @path. The returned struct is owned by @path and should not
259 * be freed or modified.
261 * Returns: a pointer to the primitive before the last appended one
262 * or %NULL on errors
264 const AdgPrimitive *
265 adg_path_over_primitive(AdgPath *path)
267 AdgPathPrivate *data;
269 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
271 data = path->data;
273 return &data->over;
277 * adg_path_append:
278 * @path: an #AdgPath
279 * @type: a #cairo_data_type_t value
280 * @Varargs: point data, specified as #AdgPair pointers
282 * Generic method to append a primitive to @path. The number of #AdgPair
283 * structs depends on @type: there is no way with this function to
284 * reserve more cairo_path_data_t structs than what is needed by the
285 * primitive.
287 * This function accepts also the special #CPML_ARC primitive.
289 * If @path has no current point while the requested primitive needs it,
290 * a warning message will be triggered without other effect.
292 void
293 adg_path_append(AdgPath *path, CpmlPrimitiveType type, ...)
295 va_list var_args;
297 va_start(var_args, type);
298 adg_path_append_valist(path, type, var_args);
299 va_end(var_args);
303 * adg_path_append_valist:
304 * @path: an #AdgPath
305 * @type: a #cairo_data_type_t value
306 * @var_args: point data, specified as #AdgPair pointers
308 * va_list version of adg_path_append().
310 void
311 adg_path_append_valist(AdgPath *path, CpmlPrimitiveType type, va_list var_args)
313 AdgPathPrivate *data;
314 AdgPrimitive primitive;
315 gint length, cnt;
316 cairo_path_data_t org;
317 cairo_path_data_t *path_data;
318 const AdgPair *pair;
320 g_return_if_fail(ADG_IS_PATH(path));
322 data = path->data;
324 if (type == CPML_CLOSE)
325 length = 1;
326 else if (type == CPML_MOVE)
327 length = 2;
328 else
329 length = cpml_primitive_type_get_n_points(type);
331 if (length == 0)
332 return;
334 /* Set a copy of the current point as the primitive origin */
335 cpml_pair_to_cairo(&data->cp, &org);
336 primitive.org = &org;
338 /* Build the cairo_path_data_t array */
339 primitive.data = path_data = g_new(cairo_path_data_t, length);
341 path_data->header.type = type;
342 path_data->header.length = length;
344 for (cnt = 1; cnt < length; ++ cnt) {
345 pair = va_arg(var_args, AdgPair *);
346 if (pair == NULL) {
347 g_free(primitive.data);
348 g_warning(_("%s: null pair caught while parsing arguments"),
349 G_STRLOC);
350 return;
353 ++ path_data;
354 cpml_pair_to_cairo(pair, path_data);
357 /* Terminate the creation of the temporary primitive */
358 primitive.segment = NULL;
360 /* Append this primitive to @path */
361 _adg_append_primitive(path, &primitive);
363 g_free(primitive.data);
367 * adg_path_append_primitive:
368 * @path: an #AdgPath
369 * @primitive: the #AdgPrimitive to append
371 * Appends @primitive to @path. The primitive to add is considered the
372 * continuation of the current path so the <structfield>org</structfield>
373 * component of @primitive is not used. Anyway the current point is
374 * checked against it: they must be equal or the function will fail
375 * without further processing.
377 void
378 adg_path_append_primitive(AdgPath *path, const AdgPrimitive *primitive)
380 AdgPathPrivate *data;
381 AdgPrimitive *primitive_dup;
383 g_return_if_fail(ADG_IS_PATH(path));
384 g_return_if_fail(primitive != NULL);
385 g_return_if_fail(primitive->org != NULL);
387 data = path->data;
389 g_return_if_fail(primitive->org->point.x == data->cp.x &&
390 primitive->org->point.y == data->cp.y);
392 /* The primitive data could be modified by pending operations:
393 * work on a copy */
394 primitive_dup = adg_primitive_deep_dup(primitive);
396 _adg_append_primitive(path, primitive_dup);
398 g_free(primitive_dup);
402 * adg_path_append_segment:
403 * @path: an #AdgPath
404 * @segment: the #AdgSegment to append
406 * Appends @segment to @path.
408 void
409 adg_path_append_segment(AdgPath *path, const AdgSegment *segment)
411 AdgPathPrivate *data;
413 g_return_if_fail(ADG_IS_PATH(path));
414 g_return_if_fail(segment != NULL);
416 data = path->data;
418 _adg_clear_parent((AdgModel *) path);
419 data->cpml.array = g_array_append_vals(data->cpml.array,
420 segment->data, segment->num_data);
424 * adg_path_append_cpml_path:
425 * @path: an #AdgPath
426 * @cpml_path: the #cairo_path_t path to append
428 * Appends a whole #CpmlPath to @path. #CpmlPath is a superset of
429 * #cairo_path_t, so this function can be feeded with both.
431 void
432 adg_path_append_cpml_path(AdgPath *path, const CpmlPath *cpml_path)
434 AdgPathPrivate *data;
436 g_return_if_fail(ADG_IS_PATH(path));
437 g_return_if_fail(cpml_path != NULL);
439 data = path->data;
441 _adg_clear_parent((AdgModel *) path);
442 data->cpml.array = g_array_append_vals(data->cpml.array,
443 cpml_path->data,
444 cpml_path->num_data);
448 * adg_path_move_to:
449 * @path: an #AdgPath
450 * @pair: the destination coordinates
452 * Begins a new segment. After this call the current point will be @pair.
454 void
455 adg_path_move_to(AdgPath *path, const AdgPair *pair)
457 adg_path_append(path, CPML_MOVE, pair);
461 * adg_path_move_to_explicit:
462 * @path: an #AdgPath
463 * @x: the new x coordinate
464 * @y: the new y coordinate
466 * Convenient function to call adg_path_move_to() using explicit
467 * coordinates instead of #AdgPair.
469 void
470 adg_path_move_to_explicit(AdgPath *path, gdouble x, gdouble y)
472 AdgPair p;
474 p.x = x;
475 p.y = y;
477 adg_path_append(path, CPML_MOVE, &p);
481 * adg_path_line_to:
482 * @path: an #AdgPath
483 * @pair: the destination coordinates
485 * Adds a line to @path from the current point to @pair. After this
486 * call the current point will be @pair.
488 * If @path has no current point before this call, this function will
489 * trigger a warning without other effect.
491 void
492 adg_path_line_to(AdgPath *path, const AdgPair *pair)
494 adg_path_append(path, CPML_LINE, pair);
498 * adg_path_line_to_explicit:
499 * @path: an #AdgPath
500 * @x: the new x coordinate
501 * @y: the new y coordinate
503 * Convenient function to call adg_path_line_to() using explicit
504 * coordinates instead of #AdgPair.
506 void
507 adg_path_line_to_explicit(AdgPath *path, gdouble x, gdouble y)
509 AdgPair p;
511 p.x = x;
512 p.y = y;
514 adg_path_append(path, CPML_LINE, &p);
518 * adg_path_arc_to:
519 * @path: an #AdgPath
520 * @throught: an arbitrary point on the arc
521 * @pair: the destination coordinates
523 * Adds an arc to the path from the current point to @pair, passing
524 * throught @throught. After this call the current point will be @pair.
526 * If @path has no current point before this call, this function will
527 * trigger a warning without other effect.
529 void
530 adg_path_arc_to(AdgPath *path, const AdgPair *throught, const AdgPair *pair)
532 adg_path_append(path, CPML_ARC, throught, pair);
536 * adg_path_arc_to_explicit:
537 * @path: an #AdgPath
538 * @x1: the x coordinate of an intermediate point
539 * @y1: the y coordinate of an intermediate point
540 * @x2: the x coordinate of the end of the arc
541 * @y2: the y coordinate of the end of the arc
543 * Convenient function to call adg_path_arc_to() using explicit
544 * coordinates instead of #AdgPair.
546 void
547 adg_path_arc_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
548 gdouble x2, gdouble y2)
550 AdgPair p[2];
552 p[0].x = x1;
553 p[0].y = y1;
554 p[1].x = x2;
555 p[1].y = y2;
557 adg_path_append(path, CPML_ARC, &p[0], &p[1]);
561 * adg_path_curve_to:
562 * @path: an #AdgPath
563 * @control1: the first control point of the curve
564 * @control2: the second control point of the curve
565 * @pair: the destination coordinates
567 * Adds a cubic Bézier curve to the path from the current point to
568 * position @pair, using @control1 and @control2 as control points.
569 * After this call the current point will be @pair.
571 * If @path has no current point before this call, this function will
572 * trigger a warning without other effect.
574 void
575 adg_path_curve_to(AdgPath *path, const AdgPair *control1,
576 const AdgPair *control2, const AdgPair *pair)
578 adg_path_append(path, CPML_CURVE, control1, control2, pair);
582 * adg_path_curve_to_explicit:
583 * @path: an #AdgPath
584 * @x1: the x coordinate of the first control point
585 * @y1: the y coordinate of the first control point
586 * @x2: the x coordinate of the second control point
587 * @y2: the y coordinate of the second control point
588 * @x3: the x coordinate of the end of the curve
589 * @y3: the y coordinate of the end of the curve
591 * Convenient function to call adg_path_curve_to() using explicit
592 * coordinates instead of #AdgPair.
594 void
595 adg_path_curve_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
596 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
598 AdgPair p[3];
600 p[0].x = x1;
601 p[0].y = y1;
602 p[1].x = x2;
603 p[1].y = y2;
604 p[2].x = x3;
605 p[2].y = y3;
607 adg_path_append(path, CPML_CURVE, &p[0], &p[1], &p[2]);
611 * adg_path_close:
612 * @path: an #AdgPath
614 * Adds a line segment to the path from the current point to the
615 * beginning of the current segment, (the most recent point passed
616 * to an adg_path_move_to()), and closes this segment.
617 * After this call the current point will be unset.
619 * The behavior of adg_path_close() is distinct from simply calling
620 * adg_line_to() with the coordinates of the segment starting point.
621 * When a closed segment is stroked, there are no caps on the ends.
622 * Instead, there is a line join connecting the final and initial
623 * primitive of the segment.
625 * If @path has no current point before this call, this function will
626 * trigger a warning without other effect.
628 void
629 adg_path_close(AdgPath *path)
631 adg_path_append(path, CPML_CLOSE);
635 * adg_path_arc:
636 * @path: an #AdgPath
637 * @center: coordinates of the center of the arc
638 * @r: the radius of the arc
639 * @start: the start angle, in radians
640 * @end: the end angle, in radians
642 * A more usual way to add an arc to @path. After this call, the current
643 * point will be the computed end point of the arc. The arc will be
644 * rendered in increasing angle, accordling to @start and @end. This means
645 * if @start is less than @end, the arc will be rendered in clockwise
646 * direction (accordling to the default cairo coordinate system) while if
647 * @start is greather than @end, the arc will be rendered in couterclockwise
648 * direction.
650 * By explicitely setting the whole arc data, the start point could be
651 * different from the current point. In this case, if @path has no
652 * current point before the call a #CPML_MOVE to the start point of
653 * the arc will be automatically prepended to the arc. If @path has a
654 * current point, a #CPML_LINE to the start point of the arc will be
655 * used instead of the "move to" primitive.
657 void
658 adg_path_arc(AdgPath *path, const AdgPair *center, gdouble r,
659 gdouble start, gdouble end)
661 AdgPathPrivate *data;
662 AdgPair p[3];
664 g_return_if_fail(ADG_IS_PATH(path));
665 g_return_if_fail(center != NULL);
667 data = path->data;
668 cpml_vector_from_angle(&p[0], start);
669 cpml_vector_from_angle(&p[1], (end-start) / 2);
670 cpml_vector_from_angle(&p[2], end);
672 cpml_vector_set_length(&p[0], r);
673 cpml_vector_set_length(&p[1], r);
674 cpml_vector_set_length(&p[2], r);
676 p[0].x += center->x;
677 p[0].y += center->y;
678 p[1].x += center->x;
679 p[1].y += center->y;
680 p[2].x += center->x;
681 p[2].y += center->y;
683 if (!data->cp_is_valid)
684 adg_path_append(path, CPML_MOVE, &p[0]);
685 else if (p[0].x != data->cp.x || p[0].y != data->cp.y)
686 adg_path_append(path, CPML_LINE, &p[0]);
688 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
692 * adg_path_arc_explicit:
693 * @path: an #AdgPath
694 * @xc: x position of the center of the arc
695 * @yc: y position of the center of the arc
696 * @r: the radius of the arc
697 * @start: the start angle, in radians
698 * @end: the end angle, in radians
700 * Convenient function to call adg_path_arc() using explicit
701 * coordinates instead of #AdgPair.
703 void
704 adg_path_arc_explicit(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
705 gdouble start, gdouble end)
707 AdgPair center;
709 center.x = xc;
710 center.y = yc;
712 adg_path_arc(path, &center, r, start, end);
716 * adg_path_chamfer
717 * @path: an #AdgPath
718 * @delta1: the distance from the intersection point of the current primitive
719 * @delta2: the distance from the intersection point of the next primitive
721 * A binary action that generates a chamfer between two primitives.
722 * The first primitive involved is the current primitive, the second will
723 * be the next primitive appended to @path after this call. The second
724 * primitive is required: if the chamfer operation is not properly
725 * terminated (by not providing the second primitive), any API accessing
726 * the path in reading mode will raise a warning.
728 * An exception is a chamfer after a #CPML_CLOSE primitive. In this case,
729 * the second primitive is not required: the current close path is used
730 * as first operand while the first primitive of the current segment is
731 * used as second operand.
733 * The chamfer operation requires two lengths: @delta1 specifies the
734 * "quantity" to trim on the first primitive while @delta2 is the same
735 * applied on the second primitive. The term "quantity" means the length
736 * of the portion to cut out from the original primitive (that is the
737 * primitive as would be without the chamfer).
739 void
740 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
742 g_return_if_fail(ADG_IS_PATH(path));
744 if (!_adg_append_operation(path, ADG_ACTION_CHAMFER, delta1, delta2))
745 return;
749 * adg_path_fillet:
750 * @path: an #AdgPath
751 * @radius: the radius of the fillet
753 * A binary action that joins to primitives with an arc.
754 * The first primitive involved is the current primitive, the second will
755 * be the next primitive appended to @path after this call. The second
756 * primitive is required: if the fillet operation is not properly
757 * terminated (by not providing the second primitive), any API accessing
758 * the path in reading mode will raise a warning.
760 * An exception is a fillet after a #CPML_CLOSE primitive. In this case,
761 * the second primitive is not required: the current close path is used
762 * as first operand while the first primitive of the current segment is
763 * used as second operand.
765 void
766 adg_path_fillet(AdgPath *path, gdouble radius)
768 g_return_if_fail(ADG_IS_PATH(path));
770 if (!_adg_append_operation(path, ADG_ACTION_FILLET, radius))
771 return;
775 * adg_path_reflect:
776 * @path: an #AdgPath
777 * @vector: the slope of the axis
779 * Reflects the first segment or @path around the axis passing
780 * throught (0, 0) and with a @vector slope. The internal segment
781 * is duplicated and the proper transformation (computed from
782 * @vector) to mirror the segment is applied on all its points.
783 * The result is then reversed with cpml_segment_reverse() and
784 * appended to the original path with adg_path_append_segment().
786 * For convenience, if @vector is %NULL the path is reversed
787 * around the x axis (y=0).
789 void
790 adg_path_reflect(AdgPath *path, const CpmlVector *vector)
792 AdgModel *model;
793 AdgMatrix matrix;
794 AdgSegment segment, *dup_segment;
796 g_return_if_fail(ADG_IS_PATH(path));
797 g_return_if_fail(vector == NULL || vector->x != 0 || vector->y != 0);
799 model = (AdgModel *) path;
801 if (vector == NULL) {
802 cairo_matrix_init_scale(&matrix, 1, -1);
803 } else {
804 CpmlVector slope;
805 gdouble cos2angle, sin2angle;
807 cpml_pair_copy(&slope, vector);
808 cpml_vector_set_length(&slope, 1);
810 if (slope.x == 0 && slope.y == 0) {
811 g_warning(_("%s: the axis of the reflection is not known"),
812 G_STRLOC);
813 return;
816 sin2angle = 2. * vector->x * vector->y;
817 cos2angle = 2. * vector->x * vector->x - 1;
819 cairo_matrix_init(&matrix, cos2angle, sin2angle,
820 sin2angle, -cos2angle, 0, 0);
823 if (!adg_trail_put_segment((AdgTrail *) path, 1, &segment))
824 return;
826 /* No need to reverse an empty segment */
827 if (segment.num_data == 0 || segment.num_data == 0)
828 return;
830 dup_segment = adg_segment_deep_dup(&segment);
831 if (dup_segment == NULL)
832 return;
834 cpml_segment_reverse(dup_segment);
835 cpml_segment_transform(dup_segment, &matrix);
836 dup_segment->data[0].header.type = CPML_LINE;
838 adg_path_append_segment(path, dup_segment);
840 g_free(dup_segment);
842 _adg_dup_reverse_named_pairs(model, &matrix);
846 static void
847 _adg_clear(AdgModel *model)
849 AdgPath *path;
850 AdgPathPrivate *data;
852 path = (AdgPath *) model;
853 data = path->data;
855 g_array_set_size(data->cpml.array, 0);
856 _adg_clear_operation(path);
857 _adg_clear_parent(model);
860 static void
861 _adg_clear_parent(AdgModel *model)
863 if (_ADG_OLD_MODEL_CLASS->clear)
864 _ADG_OLD_MODEL_CLASS->clear(model);
867 static void
868 _adg_changed(AdgModel *model)
870 _adg_clear_parent(model);
872 if (_ADG_OLD_MODEL_CLASS->changed)
873 _ADG_OLD_MODEL_CLASS->changed(model);
876 static CpmlPath *
877 _adg_get_cpml_path(AdgTrail *trail)
879 _adg_clear_parent((AdgModel *) trail);
880 return _adg_read_cpml_path((AdgPath *) trail);
883 static CpmlPath *
884 _adg_read_cpml_path(AdgPath *path)
886 AdgPathPrivate *data = path->data;
888 /* Always regenerate the CpmlPath as it is a trivial operation */
889 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
890 data->cpml.path.data = (cairo_path_data_t *) (data->cpml.array)->data;
891 data->cpml.path.num_data = (data->cpml.array)->len;
893 return &data->cpml.path;
896 static void
897 _adg_append_primitive(AdgPath *path, AdgPrimitive *current)
899 AdgPathPrivate *data;
900 cairo_path_data_t *path_data;
901 int length;
903 data = path->data;
904 path_data = current->data;
905 length = path_data[0].header.length;
907 /* Execute any pending operation */
908 _adg_do_operation(path, path_data);
910 /* Append the path data to the internal path array */
911 data->cpml.array = g_array_append_vals(data->cpml.array,
912 path_data, length);
914 /* Set path data to point to the recently appended cairo_path_data_t
915 * primitive: the first struct is the header */
916 path_data = (cairo_path_data_t *) (data->cpml.array)->data +
917 (data->cpml.array)->len - length;
919 /* Store the over primitive */
920 memcpy(&data->over, &data->last, sizeof(AdgPrimitive));
922 /* Set the last primitive for subsequent binary operations */
923 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
924 data->last.segment = NULL;
925 data->last.data = path_data;
927 /* Save the last point as the current point, if applicable */
928 data->cp_is_valid = length > 1;
929 if (length > 1)
930 cpml_pair_from_cairo(&data->cp, &path_data[length-1]);
932 /* Invalidate cairo_path: should be recomputed */
933 _adg_clear_parent((AdgModel *) path);
936 static void
937 _adg_clear_operation(AdgPath *path)
939 AdgPathPrivate *data;
940 AdgOperation *operation;
942 data = path->data;
943 operation = &data->operation;
945 if (operation->action != ADG_ACTION_NONE) {
946 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
947 G_STRLOC, _adg_action_name(operation->action));
948 operation->action = ADG_ACTION_NONE;
951 data->cp_is_valid = FALSE;
952 data->last.data = NULL;
953 data->over.data = NULL;
956 static gboolean
957 _adg_append_operation(AdgPath *path, AdgAction action, ...)
959 AdgPathPrivate *data;
960 AdgOperation *operation;
961 va_list var_args;
963 data = path->data;
965 if (data->last.data == NULL) {
966 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
967 G_STRLOC, _adg_action_name(action));
968 return FALSE;
971 operation = &data->operation;
972 if (operation->action != ADG_ACTION_NONE) {
973 g_warning(_("%s: requested a `%s' operation while a `%s' operation is active"),
974 G_STRLOC, _adg_action_name(action),
975 _adg_action_name(operation->action));
976 /* TODO: this is a rude simplification, as a lot of actions
977 * could be chained up. As an example, a fillet followed by
978 * a polar chamfer is quite common.
980 return FALSE;
983 va_start(var_args, action);
985 switch (action) {
987 case ADG_ACTION_CHAMFER:
988 operation->data.chamfer.delta1 = va_arg(var_args, double);
989 operation->data.chamfer.delta2 = va_arg(var_args, double);
990 break;
992 case ADG_ACTION_FILLET:
993 operation->data.fillet.radius = va_arg(var_args, double);
994 break;
996 case ADG_ACTION_NONE:
997 va_end(var_args);
998 return TRUE;
1000 default:
1001 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC, action);
1002 va_end(var_args);
1003 return FALSE;
1006 operation->action = action;
1007 va_end(var_args);
1009 if (data->last.data[0].header.type == CAIRO_PATH_CLOSE_PATH) {
1010 /* Special case: an action with the close primitive should
1011 * be resolved now by changing the close primitive to a
1012 * line-to and using it as second operand and use the first
1013 * primitive of the current segment as first operand */
1014 guint length;
1015 cairo_path_data_t *path_data;
1016 CpmlSegment segment;
1017 CpmlPrimitive current;
1019 length = data->cpml.array->len;
1021 /* Ensure the close path primitive is not the only data */
1022 g_return_val_if_fail(length > 1, FALSE);
1024 /* Allocate one more item once for all to accept the
1025 * conversion from a close to line-to primitive */
1026 data->cpml.array = g_array_set_size(data->cpml.array, length + 1);
1027 path_data = (cairo_path_data_t *) data->cpml.array->data;
1028 --data->cpml.array->len;
1030 /* Set segment and current (the first primitive of segment) */
1031 cpml_segment_from_cairo(&segment, _adg_read_cpml_path(path));
1032 while (cpml_segment_next(&segment))
1034 cpml_primitive_from_segment(&current, &segment);
1036 /* Convert close path to a line-to primitive */
1037 ++data->cpml.array->len;
1038 path_data[length - 1].header.type = CPML_LINE;
1039 path_data[length - 1].header.length = 2;
1040 path_data[length] = *current.org;
1042 data->last.segment = &segment;
1043 data->last.org = &path_data[length - 2];
1044 data->last.data = &path_data[length - 1];
1046 _adg_do_action(path, action, &current);
1050 return TRUE;
1053 static void
1054 _adg_do_operation(AdgPath *path, cairo_path_data_t *path_data)
1056 AdgPathPrivate *data;
1057 AdgAction action;
1058 AdgSegment segment;
1059 AdgPrimitive current;
1060 cairo_path_data_t current_org;
1062 data = path->data;
1063 action = data->operation.action;
1064 cpml_segment_from_cairo(&segment, _adg_read_cpml_path(path));
1066 /* Construct the current primitive, that is the primitive to be
1067 * mixed with the last primitive with the specified operation.
1068 * Its org is a copy of the end point of the last primitive: it can be
1069 * modified without affecting anything else. It is expected the operation
1070 * functions will add to @path the primitives required but NOT to add
1071 * @current, as this one will be inserted automatically. */
1072 current.segment = &segment;
1073 current.org = &current_org;
1074 current.data = path_data;
1075 cpml_pair_to_cairo(&data->cp, &current_org);
1077 _adg_do_action(path, action, &current);
1080 static void
1081 _adg_do_action(AdgPath *path, AdgAction action, AdgPrimitive *primitive)
1083 switch (action) {
1084 case ADG_ACTION_NONE:
1085 return;
1086 case ADG_ACTION_CHAMFER:
1087 _adg_do_chamfer(path, primitive);
1088 break;
1089 case ADG_ACTION_FILLET:
1090 _adg_do_fillet(path, primitive);
1091 break;
1092 default:
1093 g_return_if_reached();
1097 static void
1098 _adg_do_chamfer(AdgPath *path, AdgPrimitive *current)
1100 AdgPathPrivate *data;
1101 AdgPrimitive *last;
1102 gdouble delta1, delta2;
1103 gdouble len1, len2;
1104 AdgPair pair;
1106 data = path->data;
1107 last = &data->last;
1108 delta1 = data->operation.data.chamfer.delta1;
1109 len1 = cpml_primitive_get_length(last);
1111 if (delta1 >= len1) {
1112 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1113 G_STRLOC, delta1, len1);
1114 return;
1117 delta2 = data->operation.data.chamfer.delta2;
1118 len2 = cpml_primitive_get_length(current);
1120 if (delta2 >= len2) {
1121 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1122 G_STRLOC, delta1, len1);
1123 return;
1126 /* Change the end point of the last primitive */
1127 cpml_primitive_put_pair_at(last, 1. - delta1 / len1, &pair);
1128 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(last, -1));
1130 /* Change the start point of the current primitive */
1131 cpml_primitive_put_pair_at(current, delta2 / len2, &pair);
1132 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(current, 0));
1134 /* Add the chamfer line */
1135 data->operation.action = ADG_ACTION_NONE;
1136 adg_path_append(path, CPML_LINE, &pair);
1139 static void
1140 _adg_do_fillet(AdgPath *path, AdgPrimitive *current)
1142 AdgPathPrivate *data;
1143 AdgPrimitive *last, *current_dup, *last_dup;
1144 gdouble radius, offset, pos;
1145 AdgPair center, vector, p[3];
1147 data = path->data;
1148 last = &data->last;
1149 current_dup = adg_primitive_deep_dup(current);
1150 last_dup = adg_primitive_deep_dup(last);
1151 radius = data->operation.data.fillet.radius;
1152 offset = _adg_is_convex(last_dup, current_dup) ? -radius : radius;
1154 /* Find the center of the fillet from the intersection between
1155 * the last and current primitives offseted by radius */
1156 cpml_primitive_offset(current_dup, offset);
1157 cpml_primitive_offset(last_dup, offset);
1158 if (cpml_primitive_put_intersections(current_dup, last_dup, 1, &center) == 0) {
1159 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1160 G_STRLOC, radius);
1161 g_free(current_dup);
1162 g_free(last_dup);
1163 return;
1166 /* Compute the start point of the fillet */
1167 pos = cpml_primitive_get_closest_pos(last_dup, &center);
1168 cpml_primitive_put_vector_at(last_dup, pos, &vector);
1169 cpml_vector_set_length(&vector, offset);
1170 cpml_vector_normal(&vector);
1171 p[0].x = center.x - vector.x;
1172 p[0].y = center.y - vector.y;
1174 /* Compute the mid point of the fillet */
1175 cpml_pair_from_cairo(&vector, current->org);
1176 vector.x -= center.x;
1177 vector.y -= center.y;
1178 cpml_vector_set_length(&vector, radius);
1179 p[1].x = center.x + vector.x;
1180 p[1].y = center.y + vector.y;
1182 /* Compute the end point of the fillet */
1183 pos = cpml_primitive_get_closest_pos(current_dup, &center);
1184 cpml_primitive_put_vector_at(current_dup, pos, &vector);
1185 cpml_vector_set_length(&vector, offset);
1186 cpml_vector_normal(&vector);
1187 p[2].x = center.x - vector.x;
1188 p[2].y = center.y - vector.y;
1190 g_free(current_dup);
1191 g_free(last_dup);
1193 /* Change the end point of the last primitive */
1194 cpml_pair_to_cairo(&p[0], cpml_primitive_get_point(last, -1));
1196 /* Change the start point of the current primitive */
1197 cpml_pair_to_cairo(&p[2], cpml_primitive_get_point(current, 0));
1199 /* Add the fillet arc */
1200 data->operation.action = ADG_ACTION_NONE;
1201 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
1204 static gboolean
1205 _adg_is_convex(const AdgPrimitive *primitive1, const AdgPrimitive *primitive2)
1207 CpmlVector v1, v2;
1208 gdouble angle1, angle2;
1210 cpml_primitive_put_vector_at(primitive1, -1, &v1);
1211 cpml_primitive_put_vector_at(primitive2, 0, &v2);
1213 /* Probably there is a smarter way to get this without trygonometry */
1214 angle1 = cpml_vector_angle(&v1);
1215 angle2 = cpml_vector_angle(&v2);
1217 if (angle1 > angle2)
1218 angle1 -= M_PI*2;
1220 return angle2-angle1 > M_PI;
1223 static const gchar *
1224 _adg_action_name(AdgAction action)
1226 switch (action) {
1227 case ADG_ACTION_NONE:
1228 return "NULL";
1229 case ADG_ACTION_CHAMFER:
1230 return "CHAMFER";
1231 case ADG_ACTION_FILLET:
1232 return "FILLET";
1235 return "undefined";
1238 static void
1239 _adg_get_named_pair(AdgModel *model, const gchar *name,
1240 AdgPair *pair, gpointer user_data)
1242 GSList **named_pairs;
1243 AdgNamedPair *named_pair;
1245 named_pairs = user_data;
1247 named_pair = g_new(AdgNamedPair, 1);
1248 named_pair->name = name;
1249 named_pair->pair = *pair;
1251 *named_pairs = g_slist_prepend(*named_pairs, named_pair);
1254 static void
1255 _adg_dup_reverse_named_pairs(AdgModel *model, const AdgMatrix *matrix)
1257 AdgNamedPair *old_named_pair;
1258 AdgNamedPair named_pair;
1259 GSList *named_pairs;
1261 /* Populate named_pairs with all the named pairs of model */
1262 named_pairs = NULL;
1263 adg_model_foreach_named_pair(model, _adg_get_named_pair, &named_pairs);
1265 /* Readd the pairs applying the reversing transformation matrix to
1266 * their coordinates and prepending a "-" to their name */
1267 while (named_pairs) {
1268 old_named_pair = named_pairs->data;
1270 named_pair.name = g_strdup_printf("-%s", old_named_pair->name);
1271 named_pair.pair = old_named_pair->pair;
1272 cpml_pair_transform(&named_pair.pair, matrix);
1274 adg_model_set_named_pair(model, named_pair.name, &named_pair.pair);
1276 g_free((gpointer) named_pair.name);
1277 named_pairs = g_slist_delete_link(named_pairs, named_pairs);