[CpmlPrimitive] Do not return anything from cpml_primitive_copy()
[adg.git] / adg / adg-path.c
blob775060ba264a08464311e9cfb0edeff787cd289e
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 #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-path.h"
61 #include "adg-path-private.h"
62 #include "adg-primitive.h"
63 #include <math.h>
65 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_path_parent_class)
66 #define PARENT_MODEL_CLASS ((AdgModelClass *) adg_path_parent_class)
69 static void finalize (GObject *object);
70 static void clear (AdgModel *model);
71 static void clear_parent (AdgModel *model);
72 static void changed (AdgModel *model);
73 static CpmlPath * get_cpml_path (AdgTrail *trail);
74 static CpmlPath * read_cpml_path (AdgPath *path);
75 static void append_primitive (AdgPath *path,
76 AdgPrimitive *primitive);
77 static gint needed_pairs (CpmlPrimitiveType type);
78 static void clear_operation (AdgPath *path);
79 static gboolean append_operation (AdgPath *path,
80 AdgAction action,
81 ...);
82 static void do_operation (AdgPath *path,
83 cairo_path_data_t
84 *path_data);
85 static void do_action (AdgPath *path,
86 AdgAction action,
87 AdgPrimitive *primitive);
88 static void do_chamfer (AdgPath *path,
89 AdgPrimitive *current);
90 static void do_fillet (AdgPath *path,
91 AdgPrimitive *current);
92 static gboolean is_convex (const AdgPrimitive
93 *primitive1,
94 const AdgPrimitive
95 *primitive2);
96 static void dup_reversed_pair (const gchar *name,
97 AdgPair *pair,
98 gpointer user_data);
99 static const gchar * action_name (AdgAction action);
102 G_DEFINE_TYPE(AdgPath, adg_path, ADG_TYPE_TRAIL);
105 static void
106 adg_path_class_init(AdgPathClass *klass)
108 GObjectClass *gobject_class;
109 AdgModelClass *model_class;
110 AdgTrailClass *trail_class;
112 gobject_class = (GObjectClass *) klass;
113 model_class = (AdgModelClass *) klass;
114 trail_class = (AdgTrailClass *) klass;
116 g_type_class_add_private(klass, sizeof(AdgPathPrivate));
118 gobject_class->finalize = finalize;
120 model_class->clear = clear;
121 model_class->changed = changed;
123 trail_class->get_cpml_path = get_cpml_path;
126 static void
127 adg_path_init(AdgPath *path)
129 AdgPathPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(path, ADG_TYPE_PATH,
130 AdgPathPrivate);
132 data->cp_is_valid = FALSE;
133 data->cpml.array = g_array_new(FALSE, FALSE, sizeof(cairo_path_data_t));
134 data->operation.action = ADG_ACTION_NONE;
136 path->data = data;
139 static void
140 finalize(GObject *object)
142 AdgPath *path;
143 AdgPathPrivate *data;
145 path = (AdgPath *) object;
146 data = path->data;
148 g_array_free(data->cpml.array, TRUE);
149 clear_operation(path);
151 if (PARENT_OBJECT_CLASS->finalize)
152 PARENT_OBJECT_CLASS->finalize(object);
157 * adg_path_new:
159 * Creates a new path model. The path should be constructed
160 * programmatically by using the methods provided by #AdgPath.
162 * Returns: the newly created path model
164 AdgPath *
165 adg_path_new(void)
167 return g_object_new(ADG_TYPE_PATH, NULL);
171 * adg_path_get_current_point:
172 * @path: an #AdgPath
174 * Gets the current point of @path, which is conceptually the
175 * final point reached by the path so far.
177 * If there is no defined current point, %NULL is returned.
178 * It is possible to check this in advance with
179 * adg_path_has_current_point().
181 * Most #AdgPath methods alter the current point and most of them
182 * expect a current point to be defined otherwise will fail triggering
183 * a warning. Check the description of every method for specific details.
185 * Returns: the current point or %NULL on no current point set or errors
187 const AdgPair *
188 adg_path_get_current_point(AdgPath *path)
190 AdgPathPrivate *data;
192 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
194 data = path->data;
196 if (!data->cp_is_valid)
197 return NULL;
199 return &data->cp;
203 * adg_path_has_current_point:
204 * @path: an #AdgPath
206 * Returns whether a current point is defined on @path.
207 * See adg_path_get_current_point() for details on the current point.
209 * Returns: whether a current point is defined
211 gboolean
212 adg_path_has_current_point(AdgPath *path)
214 AdgPathPrivate *data;
216 g_return_val_if_fail(ADG_IS_PATH(path), FALSE);
218 data = path->data;
220 return data->cp_is_valid;
224 * adg_path_last_primitive:
225 * @path: an #AdgPath
227 * Gets the last primitive appended to @path. The returned struct
228 * is owned by @path and should not be freed or modified.
230 * Returns: a pointer to the last appended primitive or %NULL on errors
232 const AdgPrimitive *
233 adg_path_last_primitive(AdgPath *path)
235 AdgPathPrivate *data;
237 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
239 data = path->data;
241 return &data->last;
245 * adg_path_over_primitive:
246 * @path: an #AdgPath
248 * Gets the primitive before the last one appended to @path. The
249 * "over" term comes from forth, where the %OVER operator works
250 * on the stack in the same way as adg_path_over_primitive() works
251 * on @path. The returned struct is owned by @path and should not
252 * be freed or modified.
254 * Returns: a pointer to the primitive before the last appended one
255 * or %NULL on errors
257 const AdgPrimitive *
258 adg_path_over_primitive(AdgPath *path)
260 AdgPathPrivate *data;
262 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
264 data = path->data;
266 return &data->over;
270 * adg_path_append:
271 * @path: an #AdgPath
272 * @type: a #cairo_data_type_t value
273 * @...: point data, specified as #AdgPair pointers
275 * Generic method to append a primitive to @path. The number of #AdgPair
276 * structs depends on @type: there is no way with this function to
277 * reserve more cairo_path_data_t structs than what is needed by the
278 * primitive.
280 * This function accepts also the special #CPML_ARC primitive.
282 * If @path has no current point while the requested primitive needs it,
283 * a warning message will be triggered without other effect.
285 void
286 adg_path_append(AdgPath *path, CpmlPrimitiveType type, ...)
288 va_list var_args;
290 va_start(var_args, type);
291 adg_path_append_valist(path, type, var_args);
292 va_end(var_args);
296 * adg_path_append_valist:
297 * @path: an #AdgPath
298 * @type: a #cairo_data_type_t value
299 * @var_args: point data, specified as #AdgPair pointers
301 * va_list version of adg_path_append().
303 void
304 adg_path_append_valist(AdgPath *path, CpmlPrimitiveType type, va_list var_args)
306 AdgPathPrivate *data;
307 AdgPrimitive primitive;
308 gint length, cnt;
309 cairo_path_data_t org;
310 cairo_path_data_t *path_data;
311 const AdgPair *pair;
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 pair = va_arg(var_args, AdgPair *);
332 if (pair == NULL) {
333 g_free(primitive.data);
334 g_warning(_("%s: null pair caught while parsing arguments"),
335 G_STRLOC);
336 return;
339 ++ path_data;
340 cpml_pair_to_cairo(pair, path_data);
343 /* Terminate the creation of the temporary primitive */
344 primitive.segment = NULL;
346 /* Append this primitive to @path */
347 append_primitive(path, &primitive);
349 g_free(primitive.data);
353 * adg_path_append_primitive:
354 * @path: an #AdgPath
355 * @primitive: the #AdgPrimitive to append
357 * Appends @primitive to @path. The primitive to add is considered the
358 * continuation of the current path so the <structfield>org</structfield>
359 * component of @primitive is not used. Anyway the current point is
360 * checked against it: they must be equal or the function will fail
361 * without further processing.
363 void
364 adg_path_append_primitive(AdgPath *path, const AdgPrimitive *primitive)
366 AdgPathPrivate *data;
367 AdgPrimitive *primitive_dup;
369 g_return_if_fail(ADG_IS_PATH(path));
370 g_return_if_fail(primitive != NULL);
371 g_return_if_fail(primitive->org != NULL);
373 data = path->data;
375 g_return_if_fail(primitive->org->point.x == data->cp.x &&
376 primitive->org->point.y == data->cp.y);
378 /* The primitive data could be modified by pending operations:
379 * work on a copy */
380 primitive_dup = adg_primitive_deep_dup(primitive);
382 append_primitive(path, primitive_dup);
384 g_free(primitive_dup);
388 * adg_path_append_segment:
389 * @path: an #AdgPath
390 * @segment: the #AdgSegment to append
392 * Appends @segment to @path.
394 void
395 adg_path_append_segment(AdgPath *path, const AdgSegment *segment)
397 AdgPathPrivate *data;
399 g_return_if_fail(ADG_IS_PATH(path));
400 g_return_if_fail(segment != NULL);
402 data = path->data;
404 clear_parent((AdgModel *) path);
405 data->cpml.array = g_array_append_vals(data->cpml.array,
406 segment->data, segment->num_data);
410 * adg_path_append_cpml_path:
411 * @path: an #AdgPath
412 * @cpml_path: the #cairo_path_t path to append
414 * Appends a whole #CpmlPath to @path. #CpmlPath is a superset of
415 * #cairo_path_t, so this function can be feeded with both.
417 void
418 adg_path_append_cpml_path(AdgPath *path, const CpmlPath *cpml_path)
420 AdgPathPrivate *data;
422 g_return_if_fail(ADG_IS_PATH(path));
423 g_return_if_fail(cpml_path != NULL);
425 data = path->data;
427 clear_parent((AdgModel *) path);
428 data->cpml.array = g_array_append_vals(data->cpml.array,
429 cpml_path->data,
430 cpml_path->num_data);
434 * adg_path_move_to:
435 * @path: an #AdgPath
436 * @pair: the destination coordinates
438 * Begins a new segment. After this call the current point will be @pair.
440 void
441 adg_path_move_to(AdgPath *path, const AdgPair *pair)
443 adg_path_append(path, CPML_MOVE, pair);
447 * adg_path_move_to_explicit:
448 * @path: an #AdgPath
449 * @x: the new x coordinate
450 * @y: the new y coordinate
452 * Convenient function to call adg_path_move_to() using explicit
453 * coordinates instead of #AdgPair.
455 void
456 adg_path_move_to_explicit(AdgPath *path, gdouble x, gdouble y)
458 AdgPair p;
460 p.x = x;
461 p.y = y;
463 adg_path_append(path, CPML_MOVE, &p);
467 * adg_path_line_to:
468 * @path: an #AdgPath
469 * @pair: the destination coordinates
471 * Adds a line to @path from the current point to @pair. After this
472 * call the current point will be @pair.
474 * If @path has no current point before this call, this function will
475 * trigger a warning without other effect.
477 void
478 adg_path_line_to(AdgPath *path, const AdgPair *pair)
480 adg_path_append(path, CPML_LINE, pair);
484 * adg_path_line_to_explicit:
485 * @path: an #AdgPath
486 * @x: the new x coordinate
487 * @y: the new y coordinate
489 * Convenient function to call adg_path_line_to() using explicit
490 * coordinates instead of #AdgPair.
492 void
493 adg_path_line_to_explicit(AdgPath *path, gdouble x, gdouble y)
495 AdgPair p;
497 p.x = x;
498 p.y = y;
500 adg_path_append(path, CPML_LINE, &p);
504 * adg_path_arc_to:
505 * @path: an #AdgPath
506 * @throught: an arbitrary point on the arc
507 * @pair: the destination coordinates
509 * Adds an arc to the path from the current point to @pair, passing
510 * throught @throught. After this call the current point will be @pair.
512 * If @path has no current point before this call, this function will
513 * trigger a warning without other effect.
515 void
516 adg_path_arc_to(AdgPath *path, const AdgPair *throught, const AdgPair *pair)
518 adg_path_append(path, CPML_ARC, throught, pair);
522 * adg_path_arc_to_explicit:
523 * @path: an #AdgPath
524 * @x1: the x coordinate of an intermediate point
525 * @y1: the y coordinate of an intermediate point
526 * @x2: the x coordinate of the end of the arc
527 * @y2: the y coordinate of the end of the arc
529 * Convenient function to call adg_path_arc_to() using explicit
530 * coordinates instead of #AdgPair.
532 void
533 adg_path_arc_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
534 gdouble x2, gdouble y2)
536 AdgPair p[2];
538 p[0].x = x1;
539 p[0].y = y1;
540 p[1].x = x2;
541 p[1].y = y2;
543 adg_path_append(path, CPML_ARC, &p[0], &p[1]);
547 * adg_path_curve_to:
548 * @path: an #AdgPath
549 * @control1: the first control point of the curve
550 * @control2: the second control point of the curve
551 * @pair: the destination coordinates
553 * Adds a cubic Bézier curve to the path from the current point to
554 * position @pair, using @control1 and @control2 as control points.
555 * After this call the current point will be @pair.
557 * If @path has no current point before this call, this function will
558 * trigger a warning without other effect.
560 void
561 adg_path_curve_to(AdgPath *path, const AdgPair *control1,
562 const AdgPair *control2, const AdgPair *pair)
564 adg_path_append(path, CPML_CURVE, control1, control2, pair);
568 * adg_path_curve_to_explicit:
569 * @path: an #AdgPath
570 * @x1: the x coordinate of the first control point
571 * @y1: the y coordinate of the first control point
572 * @x2: the x coordinate of the second control point
573 * @y2: the y coordinate of the second control point
574 * @x3: the x coordinate of the end of the curve
575 * @y3: the y coordinate of the end of the curve
577 * Convenient function to call adg_path_curve_to() using explicit
578 * coordinates instead of #AdgPair.
580 void
581 adg_path_curve_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
582 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
584 AdgPair p[3];
586 p[0].x = x1;
587 p[0].y = y1;
588 p[1].x = x2;
589 p[1].y = y2;
590 p[2].x = x3;
591 p[2].y = y3;
593 adg_path_append(path, CPML_CURVE, &p[0], &p[1], &p[2]);
597 * adg_path_close:
598 * @path: an #AdgPath
600 * Adds a line segment to the path from the current point to the
601 * beginning of the current segment, (the most recent point passed
602 * to an adg_path_move_to()), and closes this segment.
603 * After this call the current point will be unset.
605 * The behavior of adg_path_close() is distinct from simply calling
606 * adg_line_to() with the coordinates of the segment starting point.
607 * When a closed segment is stroked, there are no caps on the ends.
608 * Instead, there is a line join connecting the final and initial
609 * primitive of the segment.
611 * If @path has no current point before this call, this function will
612 * trigger a warning without other effect.
614 void
615 adg_path_close(AdgPath *path)
617 adg_path_append(path, CPML_CLOSE);
621 * adg_path_arc:
622 * @path: an #AdgPath
623 * @center: coordinates of the center of the arc
624 * @r: the radius of the arc
625 * @start: the start angle, in radians
626 * @end: the end angle, in radians
628 * A more usual way to add an arc to @path. After this call, the current
629 * point will be the computed end point of the arc. The arc will be
630 * rendered in increasing angle, accordling to @start and @end. This means
631 * if @start is less than @end, the arc will be rendered in clockwise
632 * direction (accordling to the default cairo coordinate system) while if
633 * @start is greather than @end, the arc will be rendered in couterclockwise
634 * direction.
636 * By explicitely setting the whole arc data, the start point could be
637 * different from the current point. In this case, if @path has no
638 * current point before the call a #CPML_MOVE to the start point of
639 * the arc will be automatically prepended to the arc. If @path has a
640 * current point, a #CPML_LINE to the start point of the arc will be
641 * used instead of the "move to" primitive.
643 void
644 adg_path_arc(AdgPath *path, const AdgPair *center, gdouble r,
645 gdouble start, gdouble end)
647 AdgPathPrivate *data;
648 AdgPair p[3];
650 g_return_if_fail(ADG_IS_PATH(path));
651 g_return_if_fail(center != NULL);
653 data = path->data;
654 cpml_vector_from_angle(&p[0], start);
655 cpml_vector_from_angle(&p[1], (end-start) / 2);
656 cpml_vector_from_angle(&p[2], end);
658 cpml_vector_set_length(&p[0], r);
659 cpml_vector_set_length(&p[1], r);
660 cpml_vector_set_length(&p[2], r);
662 cpml_pair_add(&p[0], center);
663 cpml_pair_add(&p[1], center);
664 cpml_pair_add(&p[2], center);
666 if (!data->cp_is_valid)
667 adg_path_append(path, CPML_MOVE, &p[0]);
668 else if (p[0].x != data->cp.x || p[0].y != data->cp.y)
669 adg_path_append(path, CPML_LINE, &p[0]);
671 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
675 * adg_path_arc_explicit:
676 * @path: an #AdgPath
677 * @xc: x position of the center of the arc
678 * @yc: y position of the center of the arc
679 * @r: the radius of the arc
680 * @start: the start angle, in radians
681 * @end: the end angle, in radians
683 * Convenient function to call adg_path_arc() using explicit
684 * coordinates instead of #AdgPair.
686 void
687 adg_path_arc_explicit(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
688 gdouble start, gdouble end)
690 AdgPair center;
692 center.x = xc;
693 center.y = yc;
695 adg_path_arc(path, &center, r, start, end);
699 * adg_path_chamfer
700 * @path: an #AdgPath
701 * @delta1: the distance from the intersection point of the current primitive
702 * @delta2: the distance from the intersection point of the next primitive
704 * A binary action that generates a chamfer between two primitives.
705 * The first primitive involved is the current primitive, the second will
706 * be the next primitive appended to @path after this call. The second
707 * primitive is required: if the chamfer operation is not properly
708 * terminated (by not providing the second primitive), any API accessing
709 * the path in reading mode will raise a warning.
711 * An exception is a chamfer after a #CPML_CLOSE primitive. In this case,
712 * the second primitive is not required: the current close path is used
713 * as first operand while the first primitive of the current segment is
714 * used as second operand.
716 * The chamfer operation requires two lengths: @delta1 specifies the
717 * "quantity" to trim on the first primitive while @delta2 is the same
718 * applied on the second primitive. The term "quantity" means the length
719 * of the portion to cut out from the original primitive (that is the
720 * primitive as would be without the chamfer).
722 void
723 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
725 g_return_if_fail(ADG_IS_PATH(path));
727 if (!append_operation(path, ADG_ACTION_CHAMFER, delta1, delta2))
728 return;
732 * adg_path_fillet:
733 * @path: an #AdgPath
734 * @radius: the radius of the fillet
736 * A binary action that joins to primitives with an arc.
737 * The first primitive involved is the current primitive, the second will
738 * be the next primitive appended to @path after this call. The second
739 * primitive is required: if the fillet operation is not properly
740 * terminated (by not providing the second primitive), any API accessing
741 * the path in reading mode will raise a warning.
743 * An exception is a fillet after a #CPML_CLOSE primitive. In this case,
744 * the second primitive is not required: the current close path is used
745 * as first operand while the first primitive of the current segment is
746 * used as second operand.
748 void
749 adg_path_fillet(AdgPath *path, gdouble radius)
751 g_return_if_fail(ADG_IS_PATH(path));
753 if (!append_operation(path, ADG_ACTION_FILLET, radius))
754 return;
758 * adg_path_reflect:
759 * @path: an #AdgPath
760 * @vector: the slope of the axis
762 * Reflects the first segment or @path around the axis passing
763 * throught (0, 0) and with a @vector slope. The internal segment
764 * is duplicated and the proper transformation (computed from
765 * @vector) to mirror the segment is applied on all its points.
766 * The result is then reversed with cpml_segment_reverse() and
767 * appended to the original path with adg_path_append_segment().
769 * For convenience, if @vector is %NULL the path is reversed
770 * around the x axis (y=0).
772 void
773 adg_path_reflect(AdgPath *path, const CpmlVector *vector)
775 AdgNamedPairData data;
776 AdgSegment segment, *dup_segment;
778 g_return_if_fail(ADG_IS_PATH(path));
779 g_return_if_fail(vector == NULL || vector->x != 0 || vector->y != 0);
781 data.model = (AdgModel *) path;
783 if (vector == NULL) {
784 cairo_matrix_init_scale(&data.matrix, 1, -1);
785 } else {
786 CpmlVector slope;
787 gdouble cos2angle, sin2angle;
789 cpml_pair_copy(&slope, vector);
790 cpml_vector_set_length(&slope, 1);
792 if (slope.x == 0 && slope.y == 0) {
793 g_warning(_("%s: the axis of the reflection is not known"),
794 G_STRLOC);
795 return;
798 sin2angle = 2. * vector->x * vector->y;
799 cos2angle = 2. * vector->x * vector->x - 1;
801 cairo_matrix_init(&data.matrix, cos2angle, sin2angle,
802 sin2angle, -cos2angle, 0, 0);
805 if (!adg_trail_put_segment((AdgTrail *) path, 1, &segment))
806 return;
808 /* No need to reverse an empty segment */
809 if (segment.num_data == 0 || segment.num_data == 0)
810 return;
812 dup_segment = adg_segment_deep_dup(&segment);
813 if (dup_segment == NULL)
814 return;
816 cpml_segment_reverse(dup_segment);
817 cpml_segment_transform(dup_segment, &data.matrix);
818 dup_segment->data[0].header.type = CPML_LINE;
820 adg_path_append_segment(path, dup_segment);
822 g_free(dup_segment);
824 adg_model_foreach_named_pair(data.model, dup_reversed_pair, &data);
828 static void
829 clear(AdgModel *model)
831 AdgPath *path;
832 AdgPathPrivate *data;
834 path = (AdgPath *) model;
835 data = path->data;
837 g_array_set_size(data->cpml.array, 0);
838 clear_operation(path);
839 clear_parent(model);
842 static void
843 clear_parent(AdgModel *model)
845 if (PARENT_MODEL_CLASS->clear)
846 PARENT_MODEL_CLASS->clear(model);
849 static void
850 changed(AdgModel *model)
852 clear_parent(model);
854 if (PARENT_MODEL_CLASS->changed)
855 PARENT_MODEL_CLASS->changed(model);
858 static CpmlPath *
859 get_cpml_path(AdgTrail *trail)
861 clear_parent((AdgModel *) trail);
862 return read_cpml_path((AdgPath *) trail);
865 static CpmlPath *
866 read_cpml_path(AdgPath *path)
868 AdgPathPrivate *data = path->data;
870 /* Always regenerate the CpmlPath as it is a trivial operation */
871 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
872 data->cpml.path.data = (cairo_path_data_t *) (data->cpml.array)->data;
873 data->cpml.path.num_data = (data->cpml.array)->len;
875 return &data->cpml.path;
878 static void
879 append_primitive(AdgPath *path, AdgPrimitive *current)
881 AdgPathPrivate *data;
882 cairo_path_data_t *path_data;
883 int length;
885 data = path->data;
886 path_data = current->data;
887 length = path_data[0].header.length;
889 /* Execute any pending operation */
890 do_operation(path, path_data);
892 /* Append the path data to the internal path array */
893 data->cpml.array = g_array_append_vals(data->cpml.array,
894 path_data, length);
896 /* Set path data to point to the recently appended cairo_path_data_t
897 * primitive: the first struct is the header */
898 path_data = (cairo_path_data_t *) (data->cpml.array)->data +
899 (data->cpml.array)->len - length;
901 /* Store the over primitive */
902 memcpy(&data->over, &data->last, sizeof(AdgPrimitive));
904 /* Set the last primitive for subsequent binary operations */
905 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
906 data->last.segment = NULL;
907 data->last.data = path_data;
909 /* Save the last point as the current point, if applicable */
910 data->cp_is_valid = length > 1;
911 if (length > 1)
912 cpml_pair_from_cairo(&data->cp, &path_data[length-1]);
914 /* Invalidate cairo_path: should be recomputed */
915 clear_parent((AdgModel *) path);
918 static gint
919 needed_pairs(CpmlPrimitiveType type)
921 switch (type) {
922 case CPML_CLOSE:
923 return 1;
924 case CPML_MOVE:
925 return 2;
926 case CPML_LINE:
927 return 2;
928 case CPML_ARC:
929 return 3;
930 case CPML_CURVE:
931 return 4;
932 default:
933 g_return_val_if_reached(0);
936 return 0;
939 static void
940 clear_operation(AdgPath *path)
942 AdgPathPrivate *data;
943 AdgOperation *operation;
945 data = path->data;
946 operation = &data->operation;
948 if (operation->action != ADG_ACTION_NONE) {
949 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
950 G_STRLOC, action_name(operation->action));
951 operation->action = ADG_ACTION_NONE;
955 static gboolean
956 append_operation(AdgPath *path, AdgAction action, ...)
958 AdgPathPrivate *data;
959 AdgOperation *operation;
960 va_list var_args;
962 data = path->data;
964 if (data->last.data == NULL) {
965 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
966 G_STRLOC, action_name(action));
967 return FALSE;
970 operation = &data->operation;
971 if (operation->action != ADG_ACTION_NONE) {
972 g_warning(_("%s: requested a `%s' operation while a `%s' operation is active"),
973 G_STRLOC, action_name(action), action_name(operation->action));
974 ADG_MESSAGE("TODO: this is a rude simplification, as a lot of "
975 "actions could be chained up. As an example, a fillet "
976 "followed by a polar chamfer is quite common.");
977 return FALSE;
980 va_start(var_args, action);
982 switch (action) {
984 case ADG_ACTION_CHAMFER:
985 operation->data.chamfer.delta1 = va_arg(var_args, double);
986 operation->data.chamfer.delta2 = va_arg(var_args, double);
987 break;
989 case ADG_ACTION_FILLET:
990 operation->data.fillet.radius = va_arg(var_args, double);
991 break;
993 case ADG_ACTION_NONE:
994 va_end(var_args);
995 return TRUE;
997 default:
998 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC, action);
999 va_end(var_args);
1000 return FALSE;
1003 operation->action = action;
1004 va_end(var_args);
1006 if (data->last.data[0].header.type == CPML_CLOSE) {
1007 /* Special case: an action with the close primitive should
1008 * be resolved now by changing the close primitive to a
1009 * line-to and using it as second operand and use the first
1010 * primitive of the current segment as first operand */
1011 guint length;
1012 cairo_path_data_t *path_data;
1013 CpmlSegment segment;
1014 CpmlPrimitive current;
1016 length = data->cpml.array->len;
1018 /* Ensure the close path primitive is not the only data */
1019 g_return_val_if_fail(length > 1, FALSE);
1021 /* Allocate one more item once for all to accept the
1022 * conversion from a close to line-to primitive */
1023 data->cpml.array = g_array_set_size(data->cpml.array, length + 1);
1024 path_data = (cairo_path_data_t *) data->cpml.array->data;
1025 --data->cpml.array->len;
1027 /* Set segment and current (the first primitive of segment) */
1028 cpml_segment_from_cairo(&segment, read_cpml_path(path));
1029 while (cpml_segment_next(&segment))
1031 cpml_primitive_from_segment(&current, &segment);
1033 /* Convert close path to a line-to primitive */
1034 ++data->cpml.array->len;
1035 path_data[length - 1].header.type = CPML_LINE;
1036 path_data[length - 1].header.length = 2;
1037 path_data[length] = *current.org;
1039 data->last.segment = &segment;
1040 data->last.org = &path_data[length - 2];
1041 data->last.data = &path_data[length - 1];
1043 do_action(path, action, &current);
1047 return TRUE;
1050 static void
1051 do_operation(AdgPath *path, cairo_path_data_t *path_data)
1053 AdgPathPrivate *data;
1054 AdgAction action;
1055 AdgSegment segment;
1056 AdgPrimitive current;
1057 cairo_path_data_t current_org;
1059 data = path->data;
1060 action = data->operation.action;
1061 cpml_segment_from_cairo(&segment, read_cpml_path(path));
1063 /* Construct the current primitive, that is the primitive to be
1064 * mixed with the last primitive with the specified operation.
1065 * Its org is a copy of the end point of the last primitive: it can be
1066 * modified without affecting anything else. It is expected the operation
1067 * functions will add to @path the primitives required but NOT to add
1068 * @current, as this one will be inserted automatically. */
1069 current.segment = &segment;
1070 current.org = &current_org;
1071 current.data = path_data;
1072 cpml_pair_to_cairo(&data->cp, &current_org);
1074 do_action(path, action, &current);
1077 static void
1078 do_action(AdgPath *path, AdgAction action, AdgPrimitive *primitive)
1080 switch (action) {
1081 case ADG_ACTION_NONE:
1082 return;
1083 case ADG_ACTION_CHAMFER:
1084 do_chamfer(path, primitive);
1085 break;
1086 case ADG_ACTION_FILLET:
1087 do_fillet(path, primitive);
1088 break;
1089 default:
1090 g_return_if_reached();
1094 static void
1095 do_chamfer(AdgPath *path, AdgPrimitive *current)
1097 AdgPathPrivate *data;
1098 AdgPrimitive *last;
1099 gdouble delta1, delta2;
1100 gdouble len1, len2;
1101 AdgPair pair;
1103 data = path->data;
1104 last = &data->last;
1105 delta1 = data->operation.data.chamfer.delta1;
1106 len1 = cpml_primitive_get_length(last);
1108 if (delta1 >= len1) {
1109 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1110 G_STRLOC, delta1, len1);
1111 return;
1114 delta2 = data->operation.data.chamfer.delta2;
1115 len2 = cpml_primitive_get_length(current);
1117 if (delta2 >= len2) {
1118 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1119 G_STRLOC, delta1, len1);
1120 return;
1123 /* Change the end point of the last primitive */
1124 cpml_primitive_put_pair_at(last, 1. - delta1 / len1, &pair);
1125 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(last, -1));
1127 /* Change the start point of the current primitive */
1128 cpml_primitive_put_pair_at(current, delta2 / len2, &pair);
1129 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(current, 0));
1131 /* Add the chamfer line */
1132 data->operation.action = ADG_ACTION_NONE;
1133 adg_path_append(path, CPML_LINE, &pair);
1136 static void
1137 do_fillet(AdgPath *path, AdgPrimitive *current)
1139 AdgPathPrivate *data;
1140 AdgPrimitive *last, *current_dup, *last_dup;
1141 gdouble radius, offset, pos;
1142 AdgPair center, vector, p[3];
1144 data = path->data;
1145 last = &data->last;
1146 current_dup = adg_primitive_deep_dup(current);
1147 last_dup = adg_primitive_deep_dup(last);
1148 radius = data->operation.data.fillet.radius;
1149 offset = is_convex(last_dup, current_dup) ? -radius : radius;
1151 /* Find the center of the fillet from the intersection between
1152 * the last and current primitives offseted by radius */
1153 cpml_primitive_offset(current_dup, offset);
1154 cpml_primitive_offset(last_dup, offset);
1155 if (cpml_primitive_put_intersections(current_dup, last_dup, 1, &center) == 0) {
1156 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1157 G_STRLOC, radius);
1158 g_free(current_dup);
1159 g_free(last_dup);
1160 return;
1163 /* Compute the start point of the fillet */
1164 pos = cpml_primitive_get_closest_pos(last_dup, &center);
1165 cpml_primitive_put_vector_at(last_dup, pos, &vector);
1166 cpml_vector_set_length(&vector, offset);
1167 cpml_vector_normal(&vector);
1168 cpml_pair_copy(&p[0], &center);
1169 cpml_pair_sub(&p[0], &vector);
1171 /* Compute the mid point of the fillet */
1172 cpml_pair_from_cairo(&vector, current->org);
1173 cpml_pair_sub(&vector, &center);
1174 cpml_vector_set_length(&vector, radius);
1175 cpml_pair_copy(&p[1], &center);
1176 cpml_pair_add(&p[1], &vector);
1178 /* Compute the end point of the fillet */
1179 pos = cpml_primitive_get_closest_pos(current_dup, &center);
1180 cpml_primitive_put_vector_at(current_dup, pos, &vector);
1181 cpml_vector_set_length(&vector, offset);
1182 cpml_vector_normal(&vector);
1183 cpml_pair_copy(&p[2], &center);
1184 cpml_pair_sub(&p[2], &vector);
1186 g_free(current_dup);
1187 g_free(last_dup);
1189 /* Change the end point of the last primitive */
1190 cpml_pair_to_cairo(&p[0], cpml_primitive_get_point(last, -1));
1192 /* Change the start point of the current primitive */
1193 cpml_pair_to_cairo(&p[2], cpml_primitive_get_point(current, 0));
1195 /* Add the fillet arc */
1196 data->operation.action = ADG_ACTION_NONE;
1197 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
1200 static gboolean
1201 is_convex(const AdgPrimitive *primitive1, const AdgPrimitive *primitive2)
1203 CpmlVector v1, v2;
1204 gdouble angle1, angle2;
1206 cpml_primitive_put_vector_at(primitive1, -1, &v1);
1207 cpml_primitive_put_vector_at(primitive2, 0, &v2);
1209 /* Probably there is a smarter way to get this without trygonometry */
1210 angle1 = cpml_vector_angle(&v1);
1211 angle2 = cpml_vector_angle(&v2);
1213 if (angle1 > angle2)
1214 angle1 -= M_PI*2;
1216 return angle2-angle1 > M_PI;
1219 static void
1220 dup_reversed_pair(const gchar *name, AdgPair *pair, gpointer user_data)
1222 AdgNamedPairData *data;
1223 gchar *new_name;
1224 AdgPair new_pair;
1226 data = (AdgNamedPairData *) user_data;
1227 new_name = g_strdup_printf("-%s", name);
1228 cpml_pair_copy(&new_pair, pair);
1230 cpml_pair_transform(&new_pair, &data->matrix);
1231 adg_model_set_named_pair(data->model, new_name, &new_pair);
1233 g_free(new_name);
1236 static const gchar *
1237 action_name(AdgAction action)
1239 switch (action) {
1240 case ADG_ACTION_NONE:
1241 return "NULL";
1242 case ADG_ACTION_CHAMFER:
1243 return "CHAMFER";
1244 case ADG_ACTION_FILLET:
1245 return "FILLET";
1248 return "undefined";