1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
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.
56 * All fields are private and should not be used directly.
57 * Use its public methods instead.
63 #include "adg-internal.h"
66 #include "adg-model.h"
67 #include "adg-trail.h"
70 #include "adg-path-private.h"
73 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_path_parent_class)
74 #define _ADG_OLD_MODEL_CLASS ((AdgModelClass *) adg_path_parent_class)
77 G_DEFINE_TYPE(AdgPath
, adg_path
, ADG_TYPE_TRAIL
)
80 static void _adg_finalize (GObject
*object
);
81 static void _adg_clear (AdgModel
*model
);
82 static void _adg_clear_parent (AdgModel
*model
);
83 static void _adg_changed (AdgModel
*model
);
84 static CpmlPath
* _adg_get_cpml_path (AdgTrail
*trail
);
85 static CpmlPath
* _adg_read_cpml_path (AdgPath
*path
);
86 static gint
_adg_primitive_length (CpmlPrimitiveType type
);
87 static void _adg_append_primitive (AdgPath
*path
,
88 AdgPrimitive
*primitive
);
89 static void _adg_clear_operation (AdgPath
*path
);
90 static gboolean
_adg_append_operation (AdgPath
*path
,
93 static void _adg_do_operation (AdgPath
*path
,
96 static void _adg_do_action (AdgPath
*path
,
98 AdgPrimitive
*primitive
);
99 static void _adg_do_chamfer (AdgPath
*path
,
100 AdgPrimitive
*current
);
101 static void _adg_do_fillet (AdgPath
*path
,
102 AdgPrimitive
*current
);
103 static gboolean
_adg_is_convex (const AdgPrimitive
107 static const gchar
* _adg_action_name (AdgAction action
);
108 static void _adg_get_named_pair (AdgModel
*model
,
112 static void _adg_dup_reverse_named_pairs
114 const AdgMatrix
*matrix
);
118 adg_path_class_init(AdgPathClass
*klass
)
120 GObjectClass
*gobject_class
;
121 AdgModelClass
*model_class
;
122 AdgTrailClass
*trail_class
;
124 gobject_class
= (GObjectClass
*) klass
;
125 model_class
= (AdgModelClass
*) klass
;
126 trail_class
= (AdgTrailClass
*) klass
;
128 g_type_class_add_private(klass
, sizeof(AdgPathPrivate
));
130 gobject_class
->finalize
= _adg_finalize
;
132 model_class
->clear
= _adg_clear
;
133 model_class
->changed
= _adg_changed
;
135 trail_class
->get_cpml_path
= _adg_get_cpml_path
;
139 adg_path_init(AdgPath
*path
)
141 AdgPathPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(path
, ADG_TYPE_PATH
,
144 data
->cp_is_valid
= FALSE
;
145 data
->cpml
.array
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
146 data
->operation
.action
= ADG_ACTION_NONE
;
152 _adg_finalize(GObject
*object
)
155 AdgPathPrivate
*data
;
157 path
= (AdgPath
*) object
;
160 g_array_free(data
->cpml
.array
, TRUE
);
161 _adg_clear_operation(path
);
163 if (_ADG_OLD_OBJECT_CLASS
->finalize
)
164 _ADG_OLD_OBJECT_CLASS
->finalize(object
);
171 * Creates a new path model. The path should be constructed
172 * programmatically by using the methods provided by #AdgPath.
174 * Returns: the newly created path model
181 return g_object_new(ADG_TYPE_PATH
, NULL
);
185 * adg_path_get_current_point:
188 * Gets the current point of @path, which is conceptually the
189 * final point reached by the path so far.
191 * If there is no defined current point, %NULL is returned.
192 * It is possible to check this in advance with
193 * adg_path_has_current_point().
195 * Most #AdgPath methods alter the current point and most of them
196 * expect a current point to be defined otherwise will fail triggering
197 * a warning. Check the description of every method for specific details.
199 * Returns: the current point or %NULL on no current point set or errors
204 adg_path_get_current_point(AdgPath
*path
)
206 AdgPathPrivate
*data
;
208 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
212 if (!data
->cp_is_valid
)
219 * adg_path_has_current_point:
222 * Returns whether a current point is defined on @path.
223 * See adg_path_get_current_point() for details on the current point.
225 * Returns: whether a current point is defined
230 adg_path_has_current_point(AdgPath
*path
)
232 AdgPathPrivate
*data
;
234 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
238 return data
->cp_is_valid
;
242 * adg_path_last_primitive:
245 * Gets the last primitive appended to @path. The returned struct
246 * is owned by @path and should not be freed or modified.
248 * Returns: a pointer to the last appended primitive or %NULL on errors
253 adg_path_last_primitive(AdgPath
*path
)
255 AdgPathPrivate
*data
;
257 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
265 * adg_path_over_primitive:
268 * Gets the primitive before the last one appended to @path. The
269 * "over" term comes from forth, where the %OVER operator works
270 * on the stack in the same way as adg_path_over_primitive() works
271 * on @path. The returned struct is owned by @path and should not
272 * be freed or modified.
274 * Returns: a pointer to the primitive before the last appended one
280 adg_path_over_primitive(AdgPath
*path
)
282 AdgPathPrivate
*data
;
284 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
294 * @type: a #cairo_data_type_t value
295 * @...: point data, specified as #AdgPair pointers
297 * Generic method to append a primitive to @path. The number of #AdgPair
298 * pointers to pass as @Varargs depends on @type: #CPML_CLOSE does not
299 * require any pair, #CPML_MOVE and #CPML_LINE require one pair,
300 * #CPML_ARC two pairs, #CPML_CURVE three pairs and so on.
302 * All the needed pairs must be not %NULL pointers, otherwise the function
303 * will fail. The pairs in excess, if any, will be ignored.
308 adg_path_append(AdgPath
*path
, CpmlPrimitiveType type
, ...)
312 va_start(var_args
, type
);
313 adg_path_append_valist(path
, type
, var_args
);
318 * adg_path_append_valist:
320 * @type: a #cairo_data_type_t value
321 * @var_args: point data, specified as #AdgPair pointers
323 * va_list version of adg_path_append().
328 adg_path_append_valist(AdgPath
*path
, CpmlPrimitiveType type
, va_list var_args
)
334 length
= _adg_primitive_length(type
);
338 array
= g_array_new(TRUE
, FALSE
, sizeof(pair
));
340 pair
= va_arg(var_args
, AdgPair
*);
341 g_array_append_val(array
, pair
);
344 adg_path_append_array(path
, type
, (const AdgPair
**) array
->data
);
345 g_array_free(array
, TRUE
);
349 * adg_path_append_array:
351 * @type: a #cairo_data_type_t value
352 * @pairs: (array zero-terminated=1) (element-type Adg.Pair) (transfer none): point data, specified as a %NULL terminated array of #AdgPair pointers
354 * A bindingable version of adg_path_append() that uses a %NULL terminated
355 * array of pairs instead of variable argument list and friends.
357 * Furthermore, because of the list is %NULL terminated, an arbitrary
358 * number of pairs can be passed in @pairs. This allows to embed in a
359 * primitive element more data pairs than requested, something impossible
360 * to do with adg_path_append() and adg_path_append_valist().
362 * Rename to: adg_path_append
366 adg_path_append_array(AdgPath
*path
, CpmlPrimitiveType type
,
367 const AdgPair
**pairs
)
371 const AdgPair
**pair
;
372 cairo_path_data_t path_data
;
374 g_return_if_fail(ADG_IS_PATH(path
));
375 g_return_if_fail(pairs
!= NULL
);
377 length
= _adg_primitive_length(type
);
381 array
= g_array_new(FALSE
, FALSE
, sizeof(path_data
));
382 for (pair
= pairs
; *pair
!= NULL
; ++ pair
) {
383 cpml_pair_to_cairo(*pair
, &path_data
);
384 g_array_append_val(array
, path_data
);
387 if (array
->len
< length
- 1) {
388 /* Not enough pairs have been provided */
389 g_warning(_("%s: null pair caught while parsing arguments"), G_STRLOC
);
391 AdgPathPrivate
*data
;
392 AdgPrimitive primitive
;
393 cairo_path_data_t org
;
395 /* Save a copy of the current point as primitive origin */
397 cpml_pair_to_cairo(&data
->cp
, &org
);
399 /* Prepend the cairo header */
400 path_data
.header
.type
= type
;
401 path_data
.header
.length
= array
->len
+ 1;
402 g_array_prepend_val(array
, path_data
);
404 /* Append a new primitive to @path */
405 primitive
.segment
= NULL
;
406 primitive
.org
= &org
;
407 primitive
.data
= (cairo_path_data_t
*) array
->data
;
408 _adg_append_primitive(path
, &primitive
);
411 g_array_free(array
, TRUE
);
416 * adg_path_append_primitive:
418 * @primitive: the #AdgPrimitive to append
420 * Appends @primitive to @path. The primitive to add is considered the
421 * continuation of the current path so the <structfield>org</structfield>
422 * component of @primitive is not used. Anyway the current point is
423 * checked against it: they must be equal or the function will fail
424 * without further processing.
429 adg_path_append_primitive(AdgPath
*path
, const AdgPrimitive
*primitive
)
431 AdgPathPrivate
*data
;
432 AdgPrimitive
*primitive_dup
;
434 g_return_if_fail(ADG_IS_PATH(path
));
435 g_return_if_fail(primitive
!= NULL
);
436 g_return_if_fail(primitive
->org
!= NULL
);
440 g_return_if_fail(primitive
->org
->point
.x
== data
->cp
.x
&&
441 primitive
->org
->point
.y
== data
->cp
.y
);
443 /* The primitive data could be modified by pending operations:
445 primitive_dup
= adg_primitive_deep_dup(primitive
);
447 _adg_append_primitive(path
, primitive_dup
);
449 g_free(primitive_dup
);
453 * adg_path_append_segment:
455 * @segment: the #AdgSegment to append
457 * Appends @segment to @path.
462 adg_path_append_segment(AdgPath
*path
, const AdgSegment
*segment
)
464 AdgPathPrivate
*data
;
466 g_return_if_fail(ADG_IS_PATH(path
));
467 g_return_if_fail(segment
!= NULL
);
471 _adg_clear_parent((AdgModel
*) path
);
472 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
473 segment
->data
, segment
->num_data
);
477 * adg_path_append_cpml_path:
479 * @cpml_path: the #cairo_path_t path to append
481 * Appends a whole #CpmlPath to @path. #CpmlPath is a superset of
482 * #cairo_path_t, so this function can be feeded with both.
487 adg_path_append_cpml_path(AdgPath
*path
, const CpmlPath
*cpml_path
)
489 AdgPathPrivate
*data
;
491 g_return_if_fail(ADG_IS_PATH(path
));
492 g_return_if_fail(cpml_path
!= NULL
);
496 _adg_clear_parent((AdgModel
*) path
);
497 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
499 cpml_path
->num_data
);
505 * @pair: the destination coordinates
507 * Begins a new segment. After this call the current point will be @pair.
512 adg_path_move_to(AdgPath
*path
, const AdgPair
*pair
)
514 adg_path_append(path
, CPML_MOVE
, pair
);
518 * adg_path_move_to_explicit:
520 * @x: the new x coordinate
521 * @y: the new y coordinate
523 * Convenient function to call adg_path_move_to() using explicit
524 * coordinates instead of #AdgPair.
529 adg_path_move_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
536 adg_path_append(path
, CPML_MOVE
, &p
);
542 * @pair: the destination coordinates
544 * Adds a line to @path from the current point to @pair. After this
545 * 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.
553 adg_path_line_to(AdgPath
*path
, const AdgPair
*pair
)
555 adg_path_append(path
, CPML_LINE
, pair
);
559 * adg_path_line_to_explicit:
561 * @x: the new x coordinate
562 * @y: the new y coordinate
564 * Convenient function to call adg_path_line_to() using explicit
565 * coordinates instead of #AdgPair.
570 adg_path_line_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
577 adg_path_append(path
, CPML_LINE
, &p
);
583 * @throught: an arbitrary point on the arc
584 * @pair: the destination coordinates
586 * Adds an arc to the path from the current point to @pair, passing
587 * throught @throught. After this call the current point will be @pair.
589 * If @path has no current point before this call, this function will
590 * trigger a warning without other effect.
595 adg_path_arc_to(AdgPath
*path
, const AdgPair
*throught
, const AdgPair
*pair
)
597 adg_path_append(path
, CPML_ARC
, throught
, pair
);
601 * adg_path_arc_to_explicit:
603 * @x1: the x coordinate of an intermediate point
604 * @y1: the y coordinate of an intermediate point
605 * @x2: the x coordinate of the end of the arc
606 * @y2: the y coordinate of the end of the arc
608 * Convenient function to call adg_path_arc_to() using explicit
609 * coordinates instead of #AdgPair.
614 adg_path_arc_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
615 gdouble x2
, gdouble y2
)
624 adg_path_append(path
, CPML_ARC
, &p
[0], &p
[1]);
630 * @control1: the first control point of the curve
631 * @control2: the second control point of the curve
632 * @pair: the destination coordinates
634 * Adds a cubic Bézier curve to the path from the current point to
635 * position @pair, using @control1 and @control2 as control points.
636 * After this call the current point will be @pair.
638 * If @path has no current point before this call, this function will
639 * trigger a warning without other effect.
644 adg_path_curve_to(AdgPath
*path
, const AdgPair
*control1
,
645 const AdgPair
*control2
, const AdgPair
*pair
)
647 adg_path_append(path
, CPML_CURVE
, control1
, control2
, pair
);
651 * adg_path_curve_to_explicit:
653 * @x1: the x coordinate of the first control point
654 * @y1: the y coordinate of the first control point
655 * @x2: the x coordinate of the second control point
656 * @y2: the y coordinate of the second control point
657 * @x3: the x coordinate of the end of the curve
658 * @y3: the y coordinate of the end of the curve
660 * Convenient function to call adg_path_curve_to() using explicit
661 * coordinates instead of #AdgPair.
666 adg_path_curve_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
667 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
678 adg_path_append(path
, CPML_CURVE
, &p
[0], &p
[1], &p
[2]);
685 * Adds a line segment to the path from the current point to the
686 * beginning of the current segment, (the most recent point passed
687 * to an adg_path_move_to()), and closes this segment.
688 * After this call the current point will be unset.
690 * The behavior of adg_path_close() is distinct from simply calling
691 * adg_line_to() with the coordinates of the segment starting point.
692 * When a closed segment is stroked, there are no caps on the ends.
693 * Instead, there is a line join connecting the final and initial
694 * primitive of the segment.
696 * If @path has no current point before this call, this function will
697 * trigger a warning without other effect.
702 adg_path_close(AdgPath
*path
)
704 adg_path_append(path
, CPML_CLOSE
);
710 * @center: coordinates of the center of the arc
711 * @r: the radius of the arc
712 * @start: the start angle, in radians
713 * @end: the end angle, in radians
715 * A more usual way to add an arc to @path. After this call, the current
716 * point will be the computed end point of the arc. The arc will be
717 * rendered in increasing angle, accordling to @start and @end. This means
718 * if @start is less than @end, the arc will be rendered in clockwise
719 * direction (accordling to the default cairo coordinate system) while if
720 * @start is greather than @end, the arc will be rendered in couterclockwise
723 * By explicitely setting the whole arc data, the start point could be
724 * different from the current point. In this case, if @path has no
725 * current point before the call a #CPML_MOVE to the start point of
726 * the arc will be automatically prepended to the arc. If @path has a
727 * current point, a #CPML_LINE to the start point of the arc will be
728 * used instead of the "move to" primitive.
733 adg_path_arc(AdgPath
*path
, const AdgPair
*center
, gdouble r
,
734 gdouble start
, gdouble end
)
736 AdgPathPrivate
*data
;
739 g_return_if_fail(ADG_IS_PATH(path
));
740 g_return_if_fail(center
!= NULL
);
743 cpml_vector_from_angle(&p
[0], start
);
744 cpml_vector_from_angle(&p
[1], (end
-start
) / 2);
745 cpml_vector_from_angle(&p
[2], end
);
747 cpml_vector_set_length(&p
[0], r
);
748 cpml_vector_set_length(&p
[1], r
);
749 cpml_vector_set_length(&p
[2], r
);
758 if (!data
->cp_is_valid
)
759 adg_path_append(path
, CPML_MOVE
, &p
[0]);
760 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
761 adg_path_append(path
, CPML_LINE
, &p
[0]);
763 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
767 * adg_path_arc_explicit:
769 * @xc: x position of the center of the arc
770 * @yc: y position of the center of the arc
771 * @r: the radius of the arc
772 * @start: the start angle, in radians
773 * @end: the end angle, in radians
775 * Convenient function to call adg_path_arc() using explicit
776 * coordinates instead of #AdgPair.
781 adg_path_arc_explicit(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
782 gdouble start
, gdouble end
)
789 adg_path_arc(path
, ¢er
, r
, start
, end
);
795 * @delta1: the distance from the intersection point of the current primitive
796 * @delta2: the distance from the intersection point of the next primitive
798 * A binary action that generates a chamfer between two primitives.
799 * The first primitive involved is the current primitive, the second will
800 * be the next primitive appended to @path after this call. The second
801 * primitive is required: if the chamfer operation is not properly
802 * terminated (by not providing the second primitive), any API accessing
803 * the path in reading mode will raise a warning.
805 * An exception is a chamfer after a #CPML_CLOSE primitive. In this case,
806 * the second primitive is not required: the current close path is used
807 * as first operand while the first primitive of the current segment is
808 * used as second operand.
810 * The chamfer operation requires two lengths: @delta1 specifies the
811 * "quantity" to trim on the first primitive while @delta2 is the same
812 * applied on the second primitive. The term "quantity" means the length
813 * of the portion to cut out from the original primitive (that is the
814 * primitive as would be without the chamfer).
819 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
821 g_return_if_fail(ADG_IS_PATH(path
));
823 if (!_adg_append_operation(path
, ADG_ACTION_CHAMFER
, delta1
, delta2
))
830 * @radius: the radius of the fillet
832 * A binary action that joins to primitives with an arc.
833 * The first primitive involved is the current primitive, the second will
834 * be the next primitive appended to @path after this call. The second
835 * primitive is required: if the fillet operation is not properly
836 * terminated (by not providing the second primitive), any API accessing
837 * the path in reading mode will raise a warning.
839 * An exception is a fillet after a #CPML_CLOSE primitive. In this case,
840 * the second primitive is not required: the current close path is used
841 * as first operand while the first primitive of the current segment is
842 * used as second operand.
847 adg_path_fillet(AdgPath
*path
, gdouble radius
)
849 g_return_if_fail(ADG_IS_PATH(path
));
851 if (!_adg_append_operation(path
, ADG_ACTION_FILLET
, radius
))
858 * @vector: the slope of the axis
860 * Reflects the first segment or @path around the axis passing
861 * throught (0, 0) and with a @vector slope. The internal segment
862 * is duplicated and the proper transformation (computed from
863 * @vector) to mirror the segment is applied on all its points.
864 * The result is then reversed with cpml_segment_reverse() and
865 * appended to the original path with adg_path_append_segment().
867 * For convenience, if @vector is %NULL the path is reversed
868 * around the x axis (y=0).
873 adg_path_reflect(AdgPath
*path
, const CpmlVector
*vector
)
877 AdgSegment segment
, *dup_segment
;
879 g_return_if_fail(ADG_IS_PATH(path
));
880 g_return_if_fail(vector
== NULL
|| vector
->x
!= 0 || vector
->y
!= 0);
882 model
= (AdgModel
*) path
;
884 if (vector
== NULL
) {
885 cairo_matrix_init_scale(&matrix
, 1, -1);
888 gdouble cos2angle
, sin2angle
;
890 cpml_pair_copy(&slope
, vector
);
891 cpml_vector_set_length(&slope
, 1);
893 if (slope
.x
== 0 && slope
.y
== 0) {
894 g_warning(_("%s: the axis of the reflection is not known"),
899 sin2angle
= 2. * vector
->x
* vector
->y
;
900 cos2angle
= 2. * vector
->x
* vector
->x
- 1;
902 cairo_matrix_init(&matrix
, cos2angle
, sin2angle
,
903 sin2angle
, -cos2angle
, 0, 0);
906 if (!adg_trail_put_segment((AdgTrail
*) path
, 1, &segment
))
909 /* No need to reverse an empty segment */
910 if (segment
.num_data
== 0 || segment
.num_data
== 0)
913 dup_segment
= adg_segment_deep_dup(&segment
);
914 if (dup_segment
== NULL
)
917 cpml_segment_reverse(dup_segment
);
918 cpml_segment_transform(dup_segment
, &matrix
);
919 dup_segment
->data
[0].header
.type
= CPML_LINE
;
921 adg_path_append_segment(path
, dup_segment
);
925 _adg_dup_reverse_named_pairs(model
, &matrix
);
929 * adg_path_reflect_explicit:
931 * @x: the vector x component
932 * @y: the vector y component
934 * Convenient function to call adg_path_reflect() using explicit
935 * vector components instead of #CpmlVector.
940 adg_path_reflect_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
947 adg_path_reflect(path
, &vector
);
952 _adg_clear(AdgModel
*model
)
955 AdgPathPrivate
*data
;
957 path
= (AdgPath
*) model
;
960 g_array_set_size(data
->cpml
.array
, 0);
961 _adg_clear_operation(path
);
962 _adg_clear_parent(model
);
966 _adg_clear_parent(AdgModel
*model
)
968 if (_ADG_OLD_MODEL_CLASS
->clear
)
969 _ADG_OLD_MODEL_CLASS
->clear(model
);
973 _adg_changed(AdgModel
*model
)
975 _adg_clear_parent(model
);
977 if (_ADG_OLD_MODEL_CLASS
->changed
)
978 _ADG_OLD_MODEL_CLASS
->changed(model
);
982 _adg_get_cpml_path(AdgTrail
*trail
)
984 _adg_clear_parent((AdgModel
*) trail
);
985 return _adg_read_cpml_path((AdgPath
*) trail
);
989 _adg_read_cpml_path(AdgPath
*path
)
991 AdgPathPrivate
*data
= path
->data
;
993 /* Always regenerate the CpmlPath as it is a trivial operation */
994 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
995 data
->cpml
.path
.data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
;
996 data
->cpml
.path
.num_data
= (data
->cpml
.array
)->len
;
998 return &data
->cpml
.path
;
1002 _adg_primitive_length(CpmlPrimitiveType type
)
1004 if (type
== CPML_CLOSE
)
1006 else if (type
== CPML_MOVE
)
1009 return cpml_primitive_type_get_n_points(type
);
1013 _adg_append_primitive(AdgPath
*path
, AdgPrimitive
*current
)
1015 AdgPathPrivate
*data
;
1016 cairo_path_data_t
*path_data
;
1020 path_data
= current
->data
;
1021 length
= path_data
[0].header
.length
;
1023 /* Execute any pending operation */
1024 _adg_do_operation(path
, path_data
);
1026 /* Append the path data to the internal path array */
1027 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
1030 /* Set path data to point to the recently appended cairo_path_data_t
1031 * primitive: the first struct is the header */
1032 path_data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
+
1033 (data
->cpml
.array
)->len
- length
;
1035 /* Store the over primitive */
1036 memcpy(&data
->over
, &data
->last
, sizeof(AdgPrimitive
));
1038 /* Set the last primitive for subsequent binary operations */
1039 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
1040 data
->last
.segment
= NULL
;
1041 data
->last
.data
= path_data
;
1043 /* Save the last point as the current point, if applicable */
1044 data
->cp_is_valid
= length
> 1;
1046 cpml_pair_from_cairo(&data
->cp
, &path_data
[length
-1]);
1048 /* Invalidate cairo_path: should be recomputed */
1049 _adg_clear_parent((AdgModel
*) path
);
1053 _adg_clear_operation(AdgPath
*path
)
1055 AdgPathPrivate
*data
;
1056 AdgOperation
*operation
;
1059 operation
= &data
->operation
;
1061 if (operation
->action
!= ADG_ACTION_NONE
) {
1062 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
1063 G_STRLOC
, _adg_action_name(operation
->action
));
1064 operation
->action
= ADG_ACTION_NONE
;
1067 data
->cp_is_valid
= FALSE
;
1068 data
->last
.data
= NULL
;
1069 data
->over
.data
= NULL
;
1073 _adg_append_operation(AdgPath
*path
, AdgAction action
, ...)
1075 AdgPathPrivate
*data
;
1076 AdgOperation
*operation
;
1081 if (data
->last
.data
== NULL
) {
1082 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
1083 G_STRLOC
, _adg_action_name(action
));
1087 operation
= &data
->operation
;
1088 if (operation
->action
!= ADG_ACTION_NONE
) {
1089 g_warning(_("%s: requested a `%s' operation while a `%s' operation was active"),
1090 G_STRLOC
, _adg_action_name(action
),
1091 _adg_action_name(operation
->action
));
1092 /* XXX: http://dev.entidi.com/p/adg/issues/50/ */
1096 va_start(var_args
, action
);
1100 case ADG_ACTION_CHAMFER
:
1101 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
1102 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
1105 case ADG_ACTION_FILLET
:
1106 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
1109 case ADG_ACTION_NONE
:
1114 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC
, action
);
1119 operation
->action
= action
;
1122 if (data
->last
.data
[0].header
.type
== CAIRO_PATH_CLOSE_PATH
) {
1123 /* Special case: an action with the close primitive should
1124 * be resolved now by changing the close primitive to a
1125 * line-to and using it as second operand and use the first
1126 * primitive of the current segment as first operand */
1128 cairo_path_data_t
*path_data
;
1129 CpmlSegment segment
;
1130 CpmlPrimitive current
;
1132 length
= data
->cpml
.array
->len
;
1134 /* Ensure the close path primitive is not the only data */
1135 g_return_val_if_fail(length
> 1, FALSE
);
1137 /* Allocate one more item once for all to accept the
1138 * conversion from a close to line-to primitive */
1139 data
->cpml
.array
= g_array_set_size(data
->cpml
.array
, length
+ 1);
1140 path_data
= (cairo_path_data_t
*) data
->cpml
.array
->data
;
1141 --data
->cpml
.array
->len
;
1143 /* Set segment and current (the first primitive of segment) */
1144 cpml_segment_from_cairo(&segment
, _adg_read_cpml_path(path
));
1145 while (cpml_segment_next(&segment
))
1147 cpml_primitive_from_segment(¤t
, &segment
);
1149 /* Convert close path to a line-to primitive */
1150 ++data
->cpml
.array
->len
;
1151 path_data
[length
- 1].header
.type
= CPML_LINE
;
1152 path_data
[length
- 1].header
.length
= 2;
1153 path_data
[length
] = *current
.org
;
1155 data
->last
.segment
= &segment
;
1156 data
->last
.org
= &path_data
[length
- 2];
1157 data
->last
.data
= &path_data
[length
- 1];
1159 _adg_do_action(path
, action
, ¤t
);
1167 _adg_do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
1169 AdgPathPrivate
*data
;
1172 AdgPrimitive current
;
1173 cairo_path_data_t current_org
;
1176 action
= data
->operation
.action
;
1177 cpml_segment_from_cairo(&segment
, _adg_read_cpml_path(path
));
1179 /* Construct the current primitive, that is the primitive to be
1180 * mixed with the last primitive with the specified operation.
1181 * Its org is a copy of the end point of the last primitive: it can be
1182 * modified without affecting anything else. It is expected the operation
1183 * functions will add to @path the primitives required but NOT to add
1184 * @current, as this one will be inserted automatically. */
1185 current
.segment
= &segment
;
1186 current
.org
= ¤t_org
;
1187 current
.data
= path_data
;
1188 cpml_pair_to_cairo(&data
->cp
, ¤t_org
);
1190 _adg_do_action(path
, action
, ¤t
);
1194 _adg_do_action(AdgPath
*path
, AdgAction action
, AdgPrimitive
*primitive
)
1197 case ADG_ACTION_NONE
:
1199 case ADG_ACTION_CHAMFER
:
1200 _adg_do_chamfer(path
, primitive
);
1202 case ADG_ACTION_FILLET
:
1203 _adg_do_fillet(path
, primitive
);
1206 g_return_if_reached();
1211 _adg_do_chamfer(AdgPath
*path
, AdgPrimitive
*current
)
1213 AdgPathPrivate
*data
;
1215 gdouble delta1
, delta2
;
1221 delta1
= data
->operation
.data
.chamfer
.delta1
;
1222 len1
= cpml_primitive_get_length(last
);
1224 if (delta1
>= len1
) {
1225 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1226 G_STRLOC
, delta1
, len1
);
1230 delta2
= data
->operation
.data
.chamfer
.delta2
;
1231 len2
= cpml_primitive_get_length(current
);
1233 if (delta2
>= len2
) {
1234 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1235 G_STRLOC
, delta1
, len1
);
1239 /* Change the end point of the last primitive */
1240 cpml_primitive_put_pair_at(last
, 1. - delta1
/ len1
, &pair
);
1241 cpml_primitive_set_point(last
, -1, &pair
);
1243 /* Change the start point of the current primitive */
1244 cpml_primitive_put_pair_at(current
, delta2
/ len2
, &pair
);
1245 cpml_primitive_set_point(current
, 0, &pair
);
1247 /* Add the chamfer line */
1248 data
->operation
.action
= ADG_ACTION_NONE
;
1249 adg_path_append(path
, CPML_LINE
, &pair
);
1253 _adg_do_fillet(AdgPath
*path
, AdgPrimitive
*current
)
1255 AdgPathPrivate
*data
;
1256 AdgPrimitive
*last
, *current_dup
, *last_dup
;
1257 gdouble radius
, offset
, pos
;
1258 AdgPair center
, vector
, p
[3];
1262 current_dup
= adg_primitive_deep_dup(current
);
1263 last_dup
= adg_primitive_deep_dup(last
);
1264 radius
= data
->operation
.data
.fillet
.radius
;
1265 offset
= _adg_is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1267 /* Find the center of the fillet from the intersection between
1268 * the last and current primitives offseted by radius */
1269 cpml_primitive_offset(current_dup
, offset
);
1270 cpml_primitive_offset(last_dup
, offset
);
1271 if (cpml_primitive_put_intersections(current_dup
, last_dup
, 1, ¢er
) == 0) {
1272 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1274 g_free(current_dup
);
1279 /* Compute the start point of the fillet */
1280 pos
= cpml_primitive_get_closest_pos(last_dup
, ¢er
);
1281 cpml_primitive_put_vector_at(last_dup
, pos
, &vector
);
1282 cpml_vector_set_length(&vector
, offset
);
1283 cpml_vector_normal(&vector
);
1284 p
[0].x
= center
.x
- vector
.x
;
1285 p
[0].y
= center
.y
- vector
.y
;
1287 /* Compute the mid point of the fillet */
1288 cpml_pair_from_cairo(&vector
, current
->org
);
1289 vector
.x
-= center
.x
;
1290 vector
.y
-= center
.y
;
1291 cpml_vector_set_length(&vector
, radius
);
1292 p
[1].x
= center
.x
+ vector
.x
;
1293 p
[1].y
= center
.y
+ vector
.y
;
1295 /* Compute the end point of the fillet */
1296 pos
= cpml_primitive_get_closest_pos(current_dup
, ¢er
);
1297 cpml_primitive_put_vector_at(current_dup
, pos
, &vector
);
1298 cpml_vector_set_length(&vector
, offset
);
1299 cpml_vector_normal(&vector
);
1300 p
[2].x
= center
.x
- vector
.x
;
1301 p
[2].y
= center
.y
- vector
.y
;
1303 g_free(current_dup
);
1306 /* Change the end point of the last primitive */
1307 cpml_primitive_set_point(last
, -1, &p
[0]);
1309 /* Change the start point of the current primitive */
1310 cpml_primitive_set_point(current
, 0, &p
[2]);
1312 /* Add the fillet arc */
1313 data
->operation
.action
= ADG_ACTION_NONE
;
1314 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
1318 _adg_is_convex(const AdgPrimitive
*primitive1
, const AdgPrimitive
*primitive2
)
1321 gdouble angle1
, angle2
;
1323 cpml_primitive_put_vector_at(primitive1
, -1, &v1
);
1324 cpml_primitive_put_vector_at(primitive2
, 0, &v2
);
1326 /* Probably there is a smarter way to get this without trygonometry */
1327 angle1
= cpml_vector_angle(&v1
);
1328 angle2
= cpml_vector_angle(&v2
);
1330 if (angle1
> angle2
)
1333 return angle2
-angle1
> G_PI
;
1336 static const gchar
*
1337 _adg_action_name(AdgAction action
)
1340 case ADG_ACTION_NONE
:
1342 case ADG_ACTION_CHAMFER
:
1344 case ADG_ACTION_FILLET
:
1352 _adg_get_named_pair(AdgModel
*model
, const gchar
*name
,
1353 AdgPair
*pair
, gpointer user_data
)
1355 GSList
**named_pairs
;
1356 AdgNamedPair
*named_pair
;
1358 named_pairs
= user_data
;
1360 named_pair
= g_new(AdgNamedPair
, 1);
1361 named_pair
->name
= name
;
1362 named_pair
->pair
= *pair
;
1364 *named_pairs
= g_slist_prepend(*named_pairs
, named_pair
);
1368 _adg_dup_reverse_named_pairs(AdgModel
*model
, const AdgMatrix
*matrix
)
1370 AdgNamedPair
*old_named_pair
;
1371 AdgNamedPair named_pair
;
1372 GSList
*named_pairs
;
1374 /* Populate named_pairs with all the named pairs of model */
1376 adg_model_foreach_named_pair(model
, _adg_get_named_pair
, &named_pairs
);
1378 /* Readd the pairs applying the reversing transformation matrix to
1379 * their coordinates and prepending a "-" to their name */
1380 while (named_pairs
) {
1381 old_named_pair
= named_pairs
->data
;
1383 named_pair
.name
= g_strdup_printf("-%s", old_named_pair
->name
);
1384 named_pair
.pair
= old_named_pair
->pair
;
1385 cpml_pair_transform(&named_pair
.pair
, matrix
);
1387 adg_model_set_named_pair(model
, named_pair
.name
, &named_pair
.pair
);
1389 g_free((gpointer
) named_pair
.name
);
1390 named_pairs
= g_slist_delete_link(named_pairs
, named_pairs
);