[build] Updated build system and i18n
[adg.git] / adg / adg-path.c
blobd7ce09fd841f16f2e257ee071f158cfdedf2d6cf
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 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 %CAIRO_PATH_MOVE_TO
47 * primitive to the starting point of the segment is automatically
48 * added by cairo; in ADG, after an adg_path_close() the current
49 * point is simply unset.
50 **/
52 /**
53 * AdgPath:
55 * All fields are private and should not be used directly.
56 * Use its public methods instead.
57 **/
60 #include "adg-internal.h"
61 #include "adg-path.h"
62 #include "adg-path-private.h"
63 #include "adg-primitive.h"
64 #include <math.h>
66 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_path_parent_class)
67 #define PARENT_MODEL_CLASS ((AdgModelClass *) adg_path_parent_class)
70 static void finalize (GObject *object);
71 static void clear (AdgModel *model);
72 static void clear_parent (AdgModel *model);
73 static void changed (AdgModel *model);
74 static CpmlPath * get_cpml_path (AdgTrail *trail);
75 static CpmlPath * read_cpml_path (AdgPath *path);
76 static void append_primitive (AdgPath *path,
77 AdgPrimitive *primitive);
78 static gint needed_pairs (CpmlPrimitiveType type);
79 static void clear_operation (AdgPath *path);
80 static gboolean append_operation (AdgPath *path,
81 AdgAction action,
82 ...);
83 static void do_operation (AdgPath *path,
84 cairo_path_data_t
85 *path_data);
86 static void do_action (AdgPath *path,
87 AdgAction action,
88 AdgPrimitive *primitive);
89 static void do_chamfer (AdgPath *path,
90 AdgPrimitive *current);
91 static void do_fillet (AdgPath *path,
92 AdgPrimitive *current);
93 static gboolean is_convex (const AdgPrimitive
94 *primitive1,
95 const AdgPrimitive
96 *primitive2);
97 static void dup_reversed_pair (const gchar *name,
98 AdgPair *pair,
99 gpointer user_data);
100 static const gchar * action_name (AdgAction action);
103 G_DEFINE_TYPE(AdgPath, adg_path, ADG_TYPE_TRAIL);
106 static void
107 adg_path_class_init(AdgPathClass *klass)
109 GObjectClass *gobject_class;
110 AdgModelClass *model_class;
111 AdgTrailClass *trail_class;
113 gobject_class = (GObjectClass *) klass;
114 model_class = (AdgModelClass *) klass;
115 trail_class = (AdgTrailClass *) klass;
117 g_type_class_add_private(klass, sizeof(AdgPathPrivate));
119 gobject_class->finalize = finalize;
121 model_class->clear = clear;
122 model_class->changed = changed;
124 trail_class->get_cpml_path = get_cpml_path;
127 static void
128 adg_path_init(AdgPath *path)
130 AdgPathPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(path, ADG_TYPE_PATH,
131 AdgPathPrivate);
133 data->cp_is_valid = FALSE;
134 data->cpml.array = g_array_new(FALSE, FALSE, sizeof(cairo_path_data_t));
135 data->operation.action = ADG_ACTION_NONE;
137 path->data = data;
140 static void
141 finalize(GObject *object)
143 AdgPath *path;
144 AdgPathPrivate *data;
146 path = (AdgPath *) object;
147 data = path->data;
149 g_array_free(data->cpml.array, TRUE);
150 clear_operation(path);
152 if (PARENT_OBJECT_CLASS->finalize)
153 PARENT_OBJECT_CLASS->finalize(object);
158 * adg_path_new:
160 * Creates a new path model. The path should be constructed
161 * programmatically by using the methods provided by #AdgPath.
163 * Returns: the newly created path model
165 AdgPath *
166 adg_path_new(void)
168 return g_object_new(ADG_TYPE_PATH, NULL);
172 * adg_path_get_current_point:
173 * @path: an #AdgPath
175 * Gets the current point of @path, which is conceptually the
176 * final point reached by the path so far.
178 * If there is no defined current point, %NULL is returned.
179 * It is possible to check this in advance with
180 * adg_path_has_current_point().
182 * Most #AdgPath methods alter the current point and most of them
183 * expect a current point to be defined otherwise will fail triggering
184 * a warning. Check the description of every method for specific details.
186 * Returns: the current point or %NULL on no current point set or errors
188 const AdgPair *
189 adg_path_get_current_point(AdgPath *path)
191 AdgPathPrivate *data;
193 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
195 data = path->data;
197 if (!data->cp_is_valid)
198 return NULL;
200 return &data->cp;
204 * adg_path_has_current_point:
205 * @path: an #AdgPath
207 * Returns whether a current point is defined on @path.
208 * See adg_path_get_current_point() for details on the current point.
210 * Returns: whether a current point is defined
212 gboolean
213 adg_path_has_current_point(AdgPath *path)
215 AdgPathPrivate *data;
217 g_return_val_if_fail(ADG_IS_PATH(path), FALSE);
219 data = path->data;
221 return data->cp_is_valid;
225 * adg_path_last_primitive:
226 * @path: an #AdgPath
228 * Gets the last primitive appended to @path. The returned struct
229 * is owned by @path and should not be freed or modified.
231 * Returns: a pointer to the last appended primitive or %NULL on errors
233 const AdgPrimitive *
234 adg_path_last_primitive(AdgPath *path)
236 AdgPathPrivate *data;
238 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
240 data = path->data;
242 return &data->last;
246 * adg_path_over_primitive:
247 * @path: an #AdgPath
249 * Gets the primitive before the last one appended to @path. The
250 * "over" term comes from forth, where the %OVER operator works
251 * on the stack in the same way as adg_path_over_primitive() works
252 * on @path. The returned struct is owned by @path and should not
253 * be freed or modified.
255 * Returns: a pointer to the primitive before the last appended one
256 * or %NULL on errors
258 const AdgPrimitive *
259 adg_path_over_primitive(AdgPath *path)
261 AdgPathPrivate *data;
263 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
265 data = path->data;
267 return &data->over;
271 * adg_path_append:
272 * @path: an #AdgPath
273 * @type: a #cairo_data_type_t value
274 * @...: point data, specified as #AdgPair pointers
276 * Generic method to append a primitive to @path. The number of #AdgPair
277 * structs depends on @type: there is no way with this function to
278 * reserve more cairo_path_data_t structs than what is needed by the
279 * primitive.
281 * This function accepts also the special %CAIRO_PATH_ARC_TO primitive.
283 * If @path has no current point while the requested primitive needs it,
284 * a warning message will be triggered without other effect.
286 void
287 adg_path_append(AdgPath *path, CpmlPrimitiveType type, ...)
289 va_list var_args;
291 va_start(var_args, type);
292 adg_path_append_valist(path, type, var_args);
293 va_end(var_args);
297 * adg_path_append_valist:
298 * @path: an #AdgPath
299 * @type: a #cairo_data_type_t value
300 * @var_args: point data, specified as #AdgPair pointers
302 * va_list version of adg_path_append().
304 void
305 adg_path_append_valist(AdgPath *path, CpmlPrimitiveType type, va_list var_args)
307 AdgPathPrivate *data;
308 AdgPrimitive primitive;
309 gint length, cnt;
310 cairo_path_data_t org;
311 cairo_path_data_t *path_data;
313 g_return_if_fail(ADG_IS_PATH(path));
315 data = path->data;
316 length = needed_pairs(type);
317 if (length == 0)
318 return;
320 /* Set a copy of the current point as the primitive origin */
321 cpml_pair_to_cairo(&data->cp, &org);
322 primitive.org = &org;
324 /* Build the cairo_path_data_t array */
325 primitive.data = path_data = g_new(cairo_path_data_t, length);
327 path_data->header.type = type;
328 path_data->header.length = length;
330 for (cnt = 1; cnt < length; ++ cnt) {
331 ++ path_data;
332 cpml_pair_to_cairo(va_arg(var_args, AdgPair *), path_data);
335 /* Terminate the creation of the temporary primitive */
336 primitive.segment = NULL;
338 /* Append this primitive to @path */
339 append_primitive(path, &primitive);
341 g_free(primitive.data);
345 * adg_path_append_primitive:
346 * @path: an #AdgPath
347 * @primitive: the #AdgPrimitive to append
349 * Appends @primitive to @path. The primitive to add is considered the
350 * continuation of the current path so the <structfield>org</structfield>
351 * component of @primitive is not used. Anyway the current poins is
352 * checked against it: they must be equal or the function will fail
353 * without further processing.
355 void
356 adg_path_append_primitive(AdgPath *path, const AdgPrimitive *primitive)
358 AdgPathPrivate *data;
359 AdgPrimitive *primitive_dup;
361 g_return_if_fail(ADG_IS_PATH(path));
362 g_return_if_fail(primitive != NULL);
364 data = path->data;
366 g_return_if_fail(primitive->org->point.x == data->cp.x &&
367 primitive->org->point.y == data->cp.y);
369 /* The primitive data could be modified by pending operations:
370 * work on a copy */
371 primitive_dup = adg_primitive_deep_dup(primitive);
373 append_primitive(path, primitive_dup);
375 g_free(primitive_dup);
379 * adg_path_append_segment:
380 * @path: an #AdgPath
381 * @segment: the #AdgSegment to append
383 * Appends @segment to @path.
385 void
386 adg_path_append_segment(AdgPath *path, const AdgSegment *segment)
388 AdgPathPrivate *data;
390 g_return_if_fail(ADG_IS_PATH(path));
391 g_return_if_fail(segment != NULL);
393 data = path->data;
395 clear_parent((AdgModel *) path);
396 data->cpml.array = g_array_append_vals(data->cpml.array,
397 segment->data, segment->num_data);
401 * adg_path_append_cpml_path:
402 * @path: an #AdgPath
403 * @cpml_path: the #cairo_path_t path to append
405 * Appends a whole #CpmlPath to @path. #CpmlPath is a superset of
406 * #cairo_path_t, so this function can be feeded with both.
408 void
409 adg_path_append_cpml_path(AdgPath *path, const CpmlPath *cpml_path)
411 AdgPathPrivate *data;
413 g_return_if_fail(ADG_IS_PATH(path));
415 data = path->data;
417 clear_parent((AdgModel *) path);
418 data->cpml.array = g_array_append_vals(data->cpml.array,
419 cpml_path->data,
420 cpml_path->num_data);
424 * adg_path_move_to:
425 * @path: an #AdgPath
426 * @pair: the destination coordinates
428 * Begins a new segment. After this call the current point will be @pair.
430 void
431 adg_path_move_to(AdgPath *path, const AdgPair *pair)
433 adg_path_append(path, CAIRO_PATH_MOVE_TO, pair);
437 * adg_path_move_to_explicit:
438 * @path: an #AdgPath
439 * @x: the new x coordinate
440 * @y: the new y coordinate
442 * Convenient function to call adg_path_move_to() using explicit
443 * coordinates instead of #AdgPair.
445 void
446 adg_path_move_to_explicit(AdgPath *path, gdouble x, gdouble y)
448 AdgPair p;
450 p.x = x;
451 p.y = y;
453 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p);
457 * adg_path_line_to:
458 * @path: an #AdgPath
459 * @pair: the destination coordinates
461 * Adds a line to @path from the current point to @pair. After this
462 * call the current point will be @pair.
464 * If @path has no current point before this call, this function will
465 * trigger a warning without other effect.
467 void
468 adg_path_line_to(AdgPath *path, const AdgPair *pair)
470 adg_path_append(path, CAIRO_PATH_LINE_TO, pair);
474 * adg_path_line_to_explicit:
475 * @path: an #AdgPath
476 * @x: the new x coordinate
477 * @y: the new y coordinate
479 * Convenient function to call adg_path_line_to() using explicit
480 * coordinates instead of #AdgPair.
482 void
483 adg_path_line_to_explicit(AdgPath *path, gdouble x, gdouble y)
485 AdgPair p;
487 p.x = x;
488 p.y = y;
490 adg_path_append(path, CAIRO_PATH_LINE_TO, &p);
494 * adg_path_arc_to:
495 * @path: an #AdgPath
496 * @throught: an arbitrary point on the arc
497 * @pair: the destination coordinates
499 * Adds an arc to the path from the current point to @pair, passing
500 * throught @throught. After this call the current point will be @pair.
502 * If @path has no current point before this call, this function will
503 * trigger a warning without other effect.
505 void
506 adg_path_arc_to(AdgPath *path, const AdgPair *throught, const AdgPair *pair)
508 adg_path_append(path, CAIRO_PATH_ARC_TO, throught, pair);
512 * adg_path_arc_to_explicit:
513 * @path: an #AdgPath
514 * @x1: the x coordinate of an intermediate point
515 * @y1: the y coordinate of an intermediate point
516 * @x2: the x coordinate of the end of the arc
517 * @y2: the y coordinate of the end of the arc
519 * Convenient function to call adg_path_arc_to() using explicit
520 * coordinates instead of #AdgPair.
522 void
523 adg_path_arc_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
524 gdouble x2, gdouble y2)
526 AdgPair p[2];
528 p[0].x = x1;
529 p[0].y = y1;
530 p[1].x = x2;
531 p[1].y = y2;
533 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[0], &p[1]);
537 * adg_path_curve_to:
538 * @path: an #AdgPath
539 * @control1: the first control point of the curve
540 * @control2: the second control point of the curve
541 * @pair: the destination coordinates
543 * Adds a cubic Bézier curve to the path from the current point to
544 * position @pair, using @control1 and @control2 as control points.
545 * After this call the current point will be @pair.
547 * If @path has no current point before this call, this function will
548 * trigger a warning without other effect.
550 void
551 adg_path_curve_to(AdgPath *path, const AdgPair *control1,
552 const AdgPair *control2, const AdgPair *pair)
554 adg_path_append(path, CAIRO_PATH_CURVE_TO, control1, control2, pair);
558 * adg_path_curve_to_explicit:
559 * @path: an #AdgPath
560 * @x1: the x coordinate of the first control point
561 * @y1: the y coordinate of the first control point
562 * @x2: the x coordinate of the second control point
563 * @y2: the y coordinate of the second control point
564 * @x3: the x coordinate of the end of the curve
565 * @y3: the y coordinate of the end of the curve
567 * Convenient function to call adg_path_curve_to() using explicit
568 * coordinates instead of #AdgPair.
570 void
571 adg_path_curve_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
572 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
574 AdgPair p[3];
576 p[0].x = x1;
577 p[0].y = y1;
578 p[1].x = x2;
579 p[1].y = y2;
580 p[2].x = x3;
581 p[2].y = y3;
583 adg_path_append(path, CAIRO_PATH_CURVE_TO, &p[0], &p[1], &p[2]);
587 * adg_path_close:
588 * @path: an #AdgPath
590 * Adds a line segment to the path from the current point to the
591 * beginning of the current segment, (the most recent point passed
592 * to an adg_path_move_to()), and closes this segment.
593 * After this call the current point will be unset.
595 * The behavior of adg_path_close() is distinct from simply calling
596 * adg_line_to() with the coordinates of the segment starting point.
597 * When a closed segment is stroked, there are no caps on the ends.
598 * Instead, there is a line join connecting the final and initial
599 * primitive of the segment.
601 * If @path has no current point before this call, this function will
602 * trigger a warning without other effect.
604 void
605 adg_path_close(AdgPath *path)
607 adg_path_append(path, CAIRO_PATH_CLOSE_PATH);
611 * adg_path_arc:
612 * @path: an #AdgPath
613 * @center: coordinates of the center of the arc
614 * @r: the radius of the arc
615 * @start: the start angle, in radians
616 * @end: the end angle, in radians
618 * A more usual way to add an arc to @path. After this call, the current
619 * point will be the computed end point of the arc. The arc will be
620 * rendered in increasing angle, accordling to @start and @end. This means
621 * if @start is less than @end, the arc will be rendered in clockwise
622 * direction (accordling to the default cairo coordinate system) while if
623 * @start is greather than @end, the arc will be rendered in couterclockwise
624 * direction.
626 * By explicitely setting the whole arc data, the start point could be
627 * different from the current point. In this case, if @path has no
628 * current point before the call a %CAIRO_PATH_MOVE_TO to the start
629 * point of the arc will be automatically prepended to the arc.
630 * If @path has a current point, a %CAIRO_PATH_LINE_TO to the start
631 * point of the arc will be used instead of the "move to" primitive.
633 void
634 adg_path_arc(AdgPath *path, const AdgPair *center, gdouble r,
635 gdouble start, gdouble end)
637 AdgPathPrivate *data;
638 AdgPair p[3];
640 g_return_if_fail(ADG_IS_PATH(path));
642 data = path->data;
643 cpml_vector_from_angle(&p[0], start);
644 cpml_vector_from_angle(&p[1], (end-start) / 2);
645 cpml_vector_from_angle(&p[2], end);
647 cpml_vector_set_length(&p[0], r);
648 cpml_vector_set_length(&p[1], r);
649 cpml_vector_set_length(&p[2], r);
651 cpml_pair_add(&p[0], center);
652 cpml_pair_add(&p[1], center);
653 cpml_pair_add(&p[2], center);
655 if (!data->cp_is_valid)
656 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p[0]);
657 else if (p[0].x != data->cp.x || p[0].y != data->cp.y)
658 adg_path_append(path, CAIRO_PATH_LINE_TO, &p[0]);
660 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[1], &p[2]);
664 * adg_path_arc_explicit:
665 * @path: an #AdgPath
666 * @xc: x position of the center of the arc
667 * @yc: y position of the center of the arc
668 * @r: the radius of the arc
669 * @start: the start angle, in radians
670 * @end: the end angle, in radians
672 * Convenient function to call adg_path_arc() using explicit
673 * coordinates instead of #AdgPair.
675 void
676 adg_path_arc_explicit(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
677 gdouble start, gdouble end)
679 AdgPair center;
681 center.x = xc;
682 center.y = yc;
684 adg_path_arc(path, &center, r, start, end);
688 * adg_path_chamfer
689 * @path: an #AdgPath
690 * @delta1: the distance from the intersection point of the current primitive
691 * @delta2: the distance from the intersection point of the next primitive
693 * A binary action that generates a chamfer between two primitives.
694 * The first primitive involved is the current primitive, the second will
695 * be the next primitive appended to @path after this call. The second
696 * primitive is required: if the chamfer operation is not properly
697 * terminated (by not providing the second primitive), any API accessing
698 * the path in reading mode will raise a warning.
700 * An exception is a chamfer after a %CAIRO_PATH_CLOSE_PATH primitive.
701 * In this case the second primitive is not required: the current close
702 * path is used as first operand while the first primitive of the
703 * current segment is used as second operand.
705 * The chamfer operation requires two lengths: @delta1 specifies the
706 * "quantity" to trim on the first primitive while @delta2 is the same
707 * applied on the second primitive. The term "quantity" means the length
708 * of the portion to cut out from the original primitive (that is the
709 * primitive as would be without the chamfer).
711 void
712 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
714 g_return_if_fail(ADG_IS_PATH(path));
716 if (!append_operation(path, ADG_ACTION_CHAMFER, delta1, delta2))
717 return;
721 * adg_path_fillet:
722 * @path: an #AdgPath
723 * @radius: the radius of the fillet
725 * A binary action that joins to primitives with an arc.
726 * The first primitive involved is the current primitive, the second will
727 * be the next primitive appended to @path after this call. The second
728 * primitive is required: if the fillet operation is not properly
729 * terminated (by not providing the second primitive), any API accessing
730 * the path in reading mode will raise a warning.
732 * An exception is a fillet after a %CAIRO_PATH_CLOSE_PATH primitive.
733 * In this case the second primitive is not required: the current close
734 * path is used as first operand while the first primitive of the
735 * current segment is used as second operand.
737 void
738 adg_path_fillet(AdgPath *path, gdouble radius)
740 g_return_if_fail(ADG_IS_PATH(path));
742 if (!append_operation(path, ADG_ACTION_FILLET, radius))
743 return;
747 * adg_path_reflect:
748 * @path: an #AdgPath
749 * @vector: the slope of the axis
751 * Reflects the first segment or @path around the axis passing
752 * throught (0, 0) and with a @vector slope. The internal segment
753 * is duplicated and the proper transformation (computed from
754 * @vector) to mirror the segment is applied on all its points.
755 * The result is then reversed with cpml_segment_reverse() and
756 * appended to the original path with adg_path_append_segment().
758 void
759 adg_path_reflect(AdgPath *path, const CpmlVector *vector)
761 AdgNamedPairData data;
762 AdgSegment segment, *dup_segment;
764 g_return_if_fail(ADG_IS_PATH(path));
766 data.model = (AdgModel *) path;
768 if (vector == NULL) {
769 cairo_matrix_init_scale(&data.matrix, 1, -1);
770 } else {
771 CpmlVector slope;
772 gdouble cos2angle, sin2angle;
774 cpml_pair_copy(&slope, vector);
775 cpml_vector_set_length(&slope, 1);
777 if (slope.x == 0 && slope.y == 0) {
778 g_warning(_("%s: the axis of the reflection is not known"),
779 G_STRLOC);
780 return;
783 sin2angle = 2. * vector->x * vector->y;
784 cos2angle = 2. * vector->x * vector->x - 1;
786 cairo_matrix_init(&data.matrix, cos2angle, sin2angle,
787 sin2angle, -cos2angle, 0, 0);
790 adg_trail_put_segment((AdgTrail *) path, 1, &segment);
791 dup_segment = adg_segment_deep_dup(&segment);
793 cpml_segment_reverse(dup_segment);
794 cpml_segment_transform(dup_segment, &data.matrix);
795 dup_segment->data[0].header.type = CAIRO_PATH_LINE_TO;
797 adg_path_append_segment(path, dup_segment);
799 g_free(dup_segment);
801 adg_model_foreach_named_pair(data.model, dup_reversed_pair, &data);
805 static void
806 clear(AdgModel *model)
808 AdgPath *path;
809 AdgPathPrivate *data;
811 path = (AdgPath *) model;
812 data = path->data;
814 g_array_set_size(data->cpml.array, 0);
815 clear_operation(path);
816 clear_parent(model);
819 static void
820 clear_parent(AdgModel *model)
822 if (PARENT_MODEL_CLASS->clear)
823 PARENT_MODEL_CLASS->clear(model);
826 static void
827 changed(AdgModel *model)
829 clear_parent(model);
831 if (PARENT_MODEL_CLASS->changed)
832 PARENT_MODEL_CLASS->changed(model);
835 static CpmlPath *
836 get_cpml_path(AdgTrail *trail)
838 clear_parent((AdgModel *) trail);
839 return read_cpml_path((AdgPath *) trail);
842 static CpmlPath *
843 read_cpml_path(AdgPath *path)
845 AdgPathPrivate *data = path->data;
847 /* Always regenerate the CpmlPath as it is a trivial operation */
848 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
849 data->cpml.path.data = (cairo_path_data_t *) (data->cpml.array)->data;
850 data->cpml.path.num_data = (data->cpml.array)->len;
852 return &data->cpml.path;
855 static void
856 append_primitive(AdgPath *path, AdgPrimitive *current)
858 AdgPathPrivate *data;
859 cairo_path_data_t *path_data;
860 int length;
862 data = path->data;
863 path_data = current->data;
864 length = path_data[0].header.length;
866 /* Execute any pending operation */
867 do_operation(path, path_data);
869 /* Append the path data to the internal path array */
870 data->cpml.array = g_array_append_vals(data->cpml.array,
871 path_data, length);
873 /* Set path data to point to the recently appended cairo_path_data_t
874 * primitive: the first struct is the header */
875 path_data = (cairo_path_data_t *) (data->cpml.array)->data +
876 (data->cpml.array)->len - length;
878 /* Store the over primitive */
879 memcpy(&data->over, &data->last, sizeof(AdgPrimitive));
881 /* Set the last primitive for subsequent binary operations */
882 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
883 data->last.segment = NULL;
884 data->last.data = path_data;
886 /* Save the last point as the current point, if applicable */
887 data->cp_is_valid = length > 1;
888 if (length > 1)
889 cpml_pair_from_cairo(&data->cp, &path_data[length-1]);
891 /* Invalidate cairo_path: should be recomputed */
892 clear_parent((AdgModel *) path);
895 static gint
896 needed_pairs(CpmlPrimitiveType type)
898 switch (type) {
899 case CAIRO_PATH_CLOSE_PATH:
900 return 1;
901 case CAIRO_PATH_MOVE_TO:
902 return 2;
903 case CAIRO_PATH_LINE_TO:
904 return 2;
905 case CAIRO_PATH_ARC_TO:
906 return 3;
907 case CAIRO_PATH_CURVE_TO:
908 return 4;
909 default:
910 g_return_val_if_reached(0);
913 return 0;
916 static void
917 clear_operation(AdgPath *path)
919 AdgPathPrivate *data;
920 AdgOperation *operation;
922 data = path->data;
923 operation = &data->operation;
925 if (operation->action != ADG_ACTION_NONE) {
926 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
927 G_STRLOC, action_name(operation->action));
928 operation->action = ADG_ACTION_NONE;
932 static gboolean
933 append_operation(AdgPath *path, AdgAction action, ...)
935 AdgPathPrivate *data;
936 AdgOperation *operation;
937 va_list var_args;
939 data = path->data;
941 if (data->last.data == NULL) {
942 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
943 G_STRLOC, action_name(action));
944 return FALSE;
947 operation = &data->operation;
948 if (operation->action != ADG_ACTION_NONE) {
949 g_warning(_("%s: requested a `%s' operation while a `%s' operation is active"),
950 G_STRLOC, action_name(action), action_name(operation->action));
951 ADG_MESSAGE("TODO: this is a rude simplification, as a lot of "
952 "actions could be chained up. As an example, a fillet "
953 "followed by a polar chamfer is quite common.");
954 return FALSE;
957 va_start(var_args, action);
959 switch (action) {
961 case ADG_ACTION_CHAMFER:
962 operation->data.chamfer.delta1 = va_arg(var_args, double);
963 operation->data.chamfer.delta2 = va_arg(var_args, double);
964 break;
966 case ADG_ACTION_FILLET:
967 operation->data.fillet.radius = va_arg(var_args, double);
968 break;
970 case ADG_ACTION_NONE:
971 va_end(var_args);
972 return TRUE;
974 default:
975 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC, action);
976 va_end(var_args);
977 return FALSE;
980 operation->action = action;
981 va_end(var_args);
983 if (data->last.data[0].header.type == CAIRO_PATH_CLOSE_PATH) {
984 /* Special case: an action with the close primitive should
985 * be resolved now by changing the close primitive to a
986 * line-to and using it as second operand and use the first
987 * primitive of the current segment as first operand */
988 guint length;
989 cairo_path_data_t *path_data;
990 CpmlSegment segment;
991 CpmlPrimitive current;
993 length = data->cpml.array->len;
995 /* Ensure the close path primitive is not the only data */
996 g_return_val_if_fail(length > 1, FALSE);
998 /* Allocate one more item once for all to accept the
999 * conversion from a close to line-to primitive */
1000 data->cpml.array = g_array_set_size(data->cpml.array, length + 1);
1001 path_data = (cairo_path_data_t *) data->cpml.array->data;
1002 --data->cpml.array->len;
1004 /* Set segment and current (the first primitive of segment) */
1005 cpml_segment_from_cairo(&segment, read_cpml_path(path));
1006 while (cpml_segment_next(&segment))
1008 cpml_primitive_from_segment(&current, &segment);
1010 /* Convert close path to a line-to primitive */
1011 ++data->cpml.array->len;
1012 path_data[length - 1].header.type = CAIRO_PATH_LINE_TO;
1013 path_data[length - 1].header.length = 2;
1014 path_data[length] = *current.org;
1016 data->last.segment = &segment;
1017 data->last.org = &path_data[length - 2];
1018 data->last.data = &path_data[length - 1];
1020 do_action(path, action, &current);
1024 return TRUE;
1027 static void
1028 do_operation(AdgPath *path, cairo_path_data_t *path_data)
1030 AdgPathPrivate *data;
1031 AdgAction action;
1032 AdgSegment segment;
1033 AdgPrimitive current;
1034 cairo_path_data_t current_org;
1036 data = path->data;
1037 action = data->operation.action;
1038 cpml_segment_from_cairo(&segment, read_cpml_path(path));
1040 /* Construct the current primitive, that is the primitive to be
1041 * mixed with the last primitive with the specified operation.
1042 * Its org is a copy of the end point of the last primitive: it can be
1043 * modified without affecting anything else. It is expected the operation
1044 * functions will add to @path the primitives required but NOT to add
1045 * @current, as this one will be inserted automatically. */
1046 current.segment = &segment;
1047 current.org = &current_org;
1048 current.data = path_data;
1049 cpml_pair_to_cairo(&data->cp, &current_org);
1051 do_action(path, action, &current);
1054 static void
1055 do_action(AdgPath *path, AdgAction action, AdgPrimitive *primitive)
1057 switch (action) {
1058 case ADG_ACTION_NONE:
1059 return;
1060 case ADG_ACTION_CHAMFER:
1061 do_chamfer(path, primitive);
1062 break;
1063 case ADG_ACTION_FILLET:
1064 do_fillet(path, primitive);
1065 break;
1066 default:
1067 g_return_if_reached();
1071 static void
1072 do_chamfer(AdgPath *path, AdgPrimitive *current)
1074 AdgPathPrivate *data;
1075 AdgPrimitive *last;
1076 gdouble delta1, delta2;
1077 gdouble len1, len2;
1078 AdgPair pair;
1080 data = path->data;
1081 last = &data->last;
1082 delta1 = data->operation.data.chamfer.delta1;
1083 len1 = cpml_primitive_length(last);
1085 if (delta1 >= len1) {
1086 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1087 G_STRLOC, delta1, len1);
1088 return;
1091 delta2 = data->operation.data.chamfer.delta2;
1092 len2 = cpml_primitive_length(current);
1094 if (delta2 >= len2) {
1095 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1096 G_STRLOC, delta1, len1);
1097 return;
1100 /* Change the end point of the last primitive */
1101 cpml_primitive_pair_at(last, &pair, 1. - delta1 / len1);
1102 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(last, -1));
1104 /* Change the start point of the current primitive */
1105 cpml_primitive_pair_at(current, &pair, delta2 / len2);
1106 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(current, 0));
1108 /* Add the chamfer line */
1109 data->operation.action = ADG_ACTION_NONE;
1110 adg_path_append(path, CAIRO_PATH_LINE_TO, &pair);
1113 static void
1114 do_fillet(AdgPath *path, AdgPrimitive *current)
1116 AdgPathPrivate *data;
1117 AdgPrimitive *last, *current_dup, *last_dup;
1118 gdouble radius, offset, pos;
1119 AdgPair center, vector, p[3];
1121 data = path->data;
1122 last = &data->last;
1123 current_dup = adg_primitive_deep_dup(current);
1124 last_dup = adg_primitive_deep_dup(last);
1125 radius = data->operation.data.fillet.radius;
1126 offset = is_convex(last_dup, current_dup) ? -radius : radius;
1128 /* Find the center of the fillet from the intersection between
1129 * the last and current primitives offseted by radius */
1130 cpml_primitive_offset(current_dup, offset);
1131 cpml_primitive_offset(last_dup, offset);
1132 if (cpml_primitive_intersection(current_dup, last_dup,
1133 &center, 1) == 0) {
1134 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1135 G_STRLOC, radius);
1136 g_free(current_dup);
1137 g_free(last_dup);
1138 return;
1141 /* Compute the start point of the fillet */
1142 pos = cpml_primitive_near_pos(last_dup, &center);
1143 cpml_primitive_vector_at(last_dup, &vector, pos);
1144 cpml_vector_set_length(&vector, offset);
1145 cpml_vector_normal(&vector);
1146 cpml_pair_copy(&p[0], &center);
1147 cpml_pair_sub(&p[0], &vector);
1149 /* Compute the mid point of the fillet */
1150 cpml_pair_from_cairo(&vector, current->org);
1151 cpml_pair_sub(&vector, &center);
1152 cpml_vector_set_length(&vector, radius);
1153 cpml_pair_copy(&p[1], &center);
1154 cpml_pair_add(&p[1], &vector);
1156 /* Compute the end point of the fillet */
1157 pos = cpml_primitive_near_pos(current_dup, &center);
1158 cpml_primitive_vector_at(current_dup, &vector, pos);
1159 cpml_vector_set_length(&vector, offset);
1160 cpml_vector_normal(&vector);
1161 cpml_pair_copy(&p[2], &center);
1162 cpml_pair_sub(&p[2], &vector);
1164 g_free(current_dup);
1165 g_free(last_dup);
1167 /* Change the end point of the last primitive */
1168 cpml_pair_to_cairo(&p[0], cpml_primitive_get_point(last, -1));
1170 /* Change the start point of the current primitive */
1171 cpml_pair_to_cairo(&p[2], cpml_primitive_get_point(current, 0));
1173 /* Add the fillet arc */
1174 data->operation.action = ADG_ACTION_NONE;
1175 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[1], &p[2]);
1178 static gboolean
1179 is_convex(const AdgPrimitive *primitive1, const AdgPrimitive *primitive2)
1181 CpmlVector v1, v2;
1182 gdouble angle1, angle2;
1184 cpml_primitive_vector_at(primitive1, &v1, -1);
1185 cpml_primitive_vector_at(primitive2, &v2, 0);
1187 /* Probably there is a smarter way to get this without trygonometry */
1188 angle1 = cpml_vector_angle(&v1);
1189 angle2 = cpml_vector_angle(&v2);
1191 if (angle1 > angle2)
1192 angle1 -= M_PI*2;
1194 return angle2-angle1 > M_PI;
1197 static void
1198 dup_reversed_pair(const gchar *name, AdgPair *pair, gpointer user_data)
1200 AdgNamedPairData *data;
1201 gchar *new_name;
1202 AdgPair new_pair;
1204 data = (AdgNamedPairData *) user_data;
1205 new_name = g_strdup_printf("-%s", name);
1206 cpml_pair_copy(&new_pair, pair);
1208 cpml_pair_transform(&new_pair, &data->matrix);
1209 adg_model_set_named_pair(data->model, new_name, &new_pair);
1211 g_free(new_name);
1214 static const gchar *
1215 action_name(AdgAction action)
1217 switch (action) {
1218 case ADG_ACTION_NONE:
1219 return "NULL";
1220 case ADG_ACTION_CHAMFER:
1221 return "CHAMFER";
1222 case ADG_ACTION_FILLET:
1223 return "FILLET";
1226 return "undefined";