1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
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 * @Varargs: 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().
325 * Rename to: adg_path_append()
330 adg_path_append_valist(AdgPath
*path
, CpmlPrimitiveType type
, va_list var_args
)
336 length
= _adg_primitive_length(type
);
340 array
= g_array_new(TRUE
, FALSE
, sizeof(pair
));
342 pair
= va_arg(var_args
, AdgPair
*);
343 g_array_append_val(array
, pair
);
346 adg_path_append_array(path
, type
, (const AdgPair
**) array
->data
);
347 g_array_free(array
, TRUE
);
351 * adg_path_append_array:
353 * @type: a #cairo_data_type_t value
354 * @pairs: (array zero-terminated=1) (element-type Adg.Pair): point data, specified as a %NULL terminated array of #AdgPair pointers
356 * A bindingable version of adg_path_append() that uses a %NULL terminated
357 * array of pairs instead of variable argument list and friends.
359 * Furthermore, because of the list is %NULL terminated, an arbitrary
360 * number of pairs can be passed in @pairs. This allows to embed in a
361 * primitive element more data pairs than requested, something impossible
362 * to do with adg_path_append() and adg_path_append_valist().
364 * Rename to: adg_path_append()
369 adg_path_append_array(AdgPath
*path
, CpmlPrimitiveType type
,
370 const AdgPair
**pairs
)
374 const AdgPair
**pair
;
375 cairo_path_data_t path_data
;
377 g_return_if_fail(ADG_IS_PATH(path
));
378 g_return_if_fail(pairs
!= NULL
);
380 length
= _adg_primitive_length(type
);
384 array
= g_array_new(FALSE
, FALSE
, sizeof(path_data
));
385 for (pair
= pairs
; *pair
!= NULL
; ++ pair
) {
386 cpml_pair_to_cairo(*pair
, &path_data
);
387 g_array_append_val(array
, path_data
);
390 if (array
->len
< length
- 1) {
391 /* Not enough pairs have been provided */
392 g_warning(_("%s: null pair caught while parsing arguments"), G_STRLOC
);
394 AdgPathPrivate
*data
;
395 AdgPrimitive primitive
;
396 cairo_path_data_t org
;
398 /* Save a copy of the current point as primitive origin */
400 cpml_pair_to_cairo(&data
->cp
, &org
);
402 /* Prepend the cairo header */
403 path_data
.header
.type
= type
;
404 path_data
.header
.length
= array
->len
+ 1;
405 g_array_prepend_val(array
, path_data
);
407 /* Append a new primitive to @path */
408 primitive
.segment
= NULL
;
409 primitive
.org
= &org
;
410 primitive
.data
= (cairo_path_data_t
*) array
->data
;
411 _adg_append_primitive(path
, &primitive
);
414 g_array_free(array
, TRUE
);
419 * adg_path_append_primitive:
421 * @primitive: the #AdgPrimitive to append
423 * Appends @primitive to @path. The primitive to add is considered the
424 * continuation of the current path so the <structfield>org</structfield>
425 * component of @primitive is not used. Anyway the current point is
426 * checked against it: they must be equal or the function will fail
427 * without further processing.
432 adg_path_append_primitive(AdgPath
*path
, const AdgPrimitive
*primitive
)
434 AdgPathPrivate
*data
;
435 AdgPrimitive
*primitive_dup
;
437 g_return_if_fail(ADG_IS_PATH(path
));
438 g_return_if_fail(primitive
!= NULL
);
439 g_return_if_fail(primitive
->org
!= NULL
);
443 g_return_if_fail(primitive
->org
->point
.x
== data
->cp
.x
&&
444 primitive
->org
->point
.y
== data
->cp
.y
);
446 /* The primitive data could be modified by pending operations:
448 primitive_dup
= adg_primitive_deep_dup(primitive
);
450 _adg_append_primitive(path
, primitive_dup
);
452 g_free(primitive_dup
);
456 * adg_path_append_segment:
458 * @segment: the #AdgSegment to append
460 * Appends @segment to @path.
465 adg_path_append_segment(AdgPath
*path
, const AdgSegment
*segment
)
467 AdgPathPrivate
*data
;
469 g_return_if_fail(ADG_IS_PATH(path
));
470 g_return_if_fail(segment
!= NULL
);
474 _adg_clear_parent((AdgModel
*) path
);
475 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
476 segment
->data
, segment
->num_data
);
480 * adg_path_append_cpml_path:
482 * @cpml_path: the #cairo_path_t path to append
484 * Appends a whole #CpmlPath to @path. #CpmlPath is a superset of
485 * #cairo_path_t, so this function can be feeded with both.
490 adg_path_append_cpml_path(AdgPath
*path
, const CpmlPath
*cpml_path
)
492 AdgPathPrivate
*data
;
494 g_return_if_fail(ADG_IS_PATH(path
));
495 g_return_if_fail(cpml_path
!= NULL
);
499 _adg_clear_parent((AdgModel
*) path
);
500 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
502 cpml_path
->num_data
);
508 * @pair: the destination coordinates
510 * Begins a new segment. After this call the current point will be @pair.
515 adg_path_move_to(AdgPath
*path
, const AdgPair
*pair
)
517 adg_path_append(path
, CPML_MOVE
, pair
);
521 * adg_path_move_to_explicit:
523 * @x: the new x coordinate
524 * @y: the new y coordinate
526 * Convenient function to call adg_path_move_to() using explicit
527 * coordinates instead of #AdgPair.
529 * Rename to: adg_path_move_to
534 adg_path_move_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
541 adg_path_append(path
, CPML_MOVE
, &p
);
547 * @pair: the destination coordinates
549 * Adds a line to @path from the current point to @pair. After this
550 * call the current point will be @pair.
552 * If @path has no current point before this call, this function will
553 * trigger a warning without other effect.
558 adg_path_line_to(AdgPath
*path
, const AdgPair
*pair
)
560 adg_path_append(path
, CPML_LINE
, pair
);
564 * adg_path_line_to_explicit:
566 * @x: the new x coordinate
567 * @y: the new y coordinate
569 * Convenient function to call adg_path_line_to() using explicit
570 * coordinates instead of #AdgPair.
572 * Rename to: adg_path_line_to
577 adg_path_line_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
584 adg_path_append(path
, CPML_LINE
, &p
);
590 * @throught: an arbitrary point on the arc
591 * @pair: the destination coordinates
593 * Adds an arc to the path from the current point to @pair, passing
594 * throught @throught. After this call the current point will be @pair.
596 * If @path has no current point before this call, this function will
597 * trigger a warning without other effect.
602 adg_path_arc_to(AdgPath
*path
, const AdgPair
*throught
, const AdgPair
*pair
)
604 adg_path_append(path
, CPML_ARC
, throught
, pair
);
608 * adg_path_arc_to_explicit:
610 * @x1: the x coordinate of an intermediate point
611 * @y1: the y coordinate of an intermediate point
612 * @x2: the x coordinate of the end of the arc
613 * @y2: the y coordinate of the end of the arc
615 * Convenient function to call adg_path_arc_to() using explicit
616 * coordinates instead of #AdgPair.
618 * Rename to: adg_path_arc_to
623 adg_path_arc_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
624 gdouble x2
, gdouble y2
)
633 adg_path_append(path
, CPML_ARC
, &p
[0], &p
[1]);
639 * @control1: the first control point of the curve
640 * @control2: the second control point of the curve
641 * @pair: the destination coordinates
643 * Adds a cubic Bézier curve to the path from the current point to
644 * position @pair, using @control1 and @control2 as control points.
645 * After this call the current point will be @pair.
647 * If @path has no current point before this call, this function will
648 * trigger a warning without other effect.
653 adg_path_curve_to(AdgPath
*path
, const AdgPair
*control1
,
654 const AdgPair
*control2
, const AdgPair
*pair
)
656 adg_path_append(path
, CPML_CURVE
, control1
, control2
, pair
);
660 * adg_path_curve_to_explicit:
662 * @x1: the x coordinate of the first control point
663 * @y1: the y coordinate of the first control point
664 * @x2: the x coordinate of the second control point
665 * @y2: the y coordinate of the second control point
666 * @x3: the x coordinate of the end of the curve
667 * @y3: the y coordinate of the end of the curve
669 * Convenient function to call adg_path_curve_to() using explicit
670 * coordinates instead of #AdgPair.
672 * Rename to: adg_path_curve_to
677 adg_path_curve_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
678 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
689 adg_path_append(path
, CPML_CURVE
, &p
[0], &p
[1], &p
[2]);
696 * Adds a line segment to the path from the current point to the
697 * beginning of the current segment, (the most recent point passed
698 * to an adg_path_move_to()), and closes this segment.
699 * After this call the current point will be unset.
701 * The behavior of adg_path_close() is distinct from simply calling
702 * adg_line_to() with the coordinates of the segment starting point.
703 * When a closed segment is stroked, there are no caps on the ends.
704 * Instead, there is a line join connecting the final and initial
705 * primitive of the segment.
707 * If @path has no current point before this call, this function will
708 * trigger a warning without other effect.
713 adg_path_close(AdgPath
*path
)
715 adg_path_append(path
, CPML_CLOSE
);
721 * @center: coordinates of the center of the arc
722 * @r: the radius of the arc
723 * @start: the start angle, in radians
724 * @end: the end angle, in radians
726 * A more usual way to add an arc to @path. After this call, the current
727 * point will be the computed end point of the arc. The arc will be
728 * rendered in increasing angle, accordling to @start and @end. This means
729 * if @start is less than @end, the arc will be rendered in clockwise
730 * direction (accordling to the default cairo coordinate system) while if
731 * @start is greather than @end, the arc will be rendered in couterclockwise
734 * By explicitely setting the whole arc data, the start point could be
735 * different from the current point. In this case, if @path has no
736 * current point before the call a #CPML_MOVE to the start point of
737 * the arc will be automatically prepended to the arc. If @path has a
738 * current point, a #CPML_LINE to the start point of the arc will be
739 * used instead of the "move to" primitive.
744 adg_path_arc(AdgPath
*path
, const AdgPair
*center
, gdouble r
,
745 gdouble start
, gdouble end
)
747 AdgPathPrivate
*data
;
750 g_return_if_fail(ADG_IS_PATH(path
));
751 g_return_if_fail(center
!= NULL
);
754 cpml_vector_from_angle(&p
[0], start
);
755 cpml_vector_from_angle(&p
[1], (end
-start
) / 2);
756 cpml_vector_from_angle(&p
[2], end
);
758 cpml_vector_set_length(&p
[0], r
);
759 cpml_vector_set_length(&p
[1], r
);
760 cpml_vector_set_length(&p
[2], r
);
769 if (!data
->cp_is_valid
)
770 adg_path_append(path
, CPML_MOVE
, &p
[0]);
771 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
772 adg_path_append(path
, CPML_LINE
, &p
[0]);
774 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
778 * adg_path_arc_explicit:
780 * @xc: x position of the center of the arc
781 * @yc: y position of the center of the arc
782 * @r: the radius of the arc
783 * @start: the start angle, in radians
784 * @end: the end angle, in radians
786 * Convenient function to call adg_path_arc() using explicit
787 * coordinates instead of #AdgPair.
789 * Rename to: adg_path_arc
794 adg_path_arc_explicit(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
795 gdouble start
, gdouble end
)
802 adg_path_arc(path
, ¢er
, r
, start
, end
);
808 * @delta1: the distance from the intersection point of the current primitive
809 * @delta2: the distance from the intersection point of the next primitive
811 * A binary action that generates a chamfer between two primitives.
812 * The first primitive involved is the current primitive, the second will
813 * be the next primitive appended to @path after this call. The second
814 * primitive is required: if the chamfer operation is not properly
815 * terminated (by not providing the second primitive), any API accessing
816 * the path in reading mode will raise a warning.
818 * An exception is a chamfer after a #CPML_CLOSE primitive. In this case,
819 * the second primitive is not required: the current close path is used
820 * as first operand while the first primitive of the current segment is
821 * used as second operand.
823 * The chamfer operation requires two lengths: @delta1 specifies the
824 * "quantity" to trim on the first primitive while @delta2 is the same
825 * applied on the second primitive. The term "quantity" means the length
826 * of the portion to cut out from the original primitive (that is the
827 * primitive as would be without the chamfer).
832 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
834 g_return_if_fail(ADG_IS_PATH(path
));
836 if (!_adg_append_operation(path
, ADG_ACTION_CHAMFER
, delta1
, delta2
))
843 * @radius: the radius of the fillet
845 * A binary action that joins to primitives with an arc.
846 * The first primitive involved is the current primitive, the second will
847 * be the next primitive appended to @path after this call. The second
848 * primitive is required: if the fillet operation is not properly
849 * terminated (by not providing the second primitive), any API accessing
850 * the path in reading mode will raise a warning.
852 * An exception is a fillet after a #CPML_CLOSE primitive. In this case,
853 * the second primitive is not required: the current close path is used
854 * as first operand while the first primitive of the current segment is
855 * used as second operand.
860 adg_path_fillet(AdgPath
*path
, gdouble radius
)
862 g_return_if_fail(ADG_IS_PATH(path
));
864 if (!_adg_append_operation(path
, ADG_ACTION_FILLET
, radius
))
871 * @vector: the slope of the axis
873 * Reflects the first segment or @path around the axis passing
874 * throught (0, 0) and with a @vector slope. The internal segment
875 * is duplicated and the proper transformation (computed from
876 * @vector) to mirror the segment is applied on all its points.
877 * The result is then reversed with cpml_segment_reverse() and
878 * appended to the original path with adg_path_append_segment().
880 * For convenience, if @vector is %NULL the path is reversed
881 * around the x axis (y=0).
886 adg_path_reflect(AdgPath
*path
, const CpmlVector
*vector
)
890 AdgSegment segment
, *dup_segment
;
892 g_return_if_fail(ADG_IS_PATH(path
));
893 g_return_if_fail(vector
== NULL
|| vector
->x
!= 0 || vector
->y
!= 0);
895 model
= (AdgModel
*) path
;
897 if (vector
== NULL
) {
898 cairo_matrix_init_scale(&matrix
, 1, -1);
901 gdouble cos2angle
, sin2angle
;
903 cpml_pair_copy(&slope
, vector
);
904 cpml_vector_set_length(&slope
, 1);
906 if (slope
.x
== 0 && slope
.y
== 0) {
907 g_warning(_("%s: the axis of the reflection is not known"),
912 sin2angle
= 2. * vector
->x
* vector
->y
;
913 cos2angle
= 2. * vector
->x
* vector
->x
- 1;
915 cairo_matrix_init(&matrix
, cos2angle
, sin2angle
,
916 sin2angle
, -cos2angle
, 0, 0);
919 if (!adg_trail_put_segment((AdgTrail
*) path
, 1, &segment
))
922 /* No need to reverse an empty segment */
923 if (segment
.num_data
== 0 || segment
.num_data
== 0)
926 dup_segment
= adg_segment_deep_dup(&segment
);
927 if (dup_segment
== NULL
)
930 cpml_segment_reverse(dup_segment
);
931 cpml_segment_transform(dup_segment
, &matrix
);
932 dup_segment
->data
[0].header
.type
= CPML_LINE
;
934 adg_path_append_segment(path
, dup_segment
);
938 _adg_dup_reverse_named_pairs(model
, &matrix
);
942 * adg_path_reflect_explicit:
944 * @x: the vector x component
945 * @y: the vector y component
947 * Convenient function to call adg_path_reflect() using explicit
948 * vector components instead of #CpmlVector.
950 * Rename to: adg_path_reflect
955 adg_path_reflect_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
962 adg_path_reflect(path
, &vector
);
967 _adg_clear(AdgModel
*model
)
970 AdgPathPrivate
*data
;
972 path
= (AdgPath
*) model
;
975 g_array_set_size(data
->cpml
.array
, 0);
976 _adg_clear_operation(path
);
977 _adg_clear_parent(model
);
981 _adg_clear_parent(AdgModel
*model
)
983 if (_ADG_OLD_MODEL_CLASS
->clear
)
984 _ADG_OLD_MODEL_CLASS
->clear(model
);
988 _adg_changed(AdgModel
*model
)
990 _adg_clear_parent(model
);
992 if (_ADG_OLD_MODEL_CLASS
->changed
)
993 _ADG_OLD_MODEL_CLASS
->changed(model
);
997 _adg_get_cpml_path(AdgTrail
*trail
)
999 _adg_clear_parent((AdgModel
*) trail
);
1000 return _adg_read_cpml_path((AdgPath
*) trail
);
1004 _adg_read_cpml_path(AdgPath
*path
)
1006 AdgPathPrivate
*data
= path
->data
;
1008 /* Always regenerate the CpmlPath as it is a trivial operation */
1009 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
1010 data
->cpml
.path
.data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
;
1011 data
->cpml
.path
.num_data
= (data
->cpml
.array
)->len
;
1013 return &data
->cpml
.path
;
1017 _adg_primitive_length(CpmlPrimitiveType type
)
1019 if (type
== CPML_CLOSE
)
1021 else if (type
== CPML_MOVE
)
1024 return cpml_primitive_type_get_n_points(type
);
1028 _adg_append_primitive(AdgPath
*path
, AdgPrimitive
*current
)
1030 AdgPathPrivate
*data
;
1031 cairo_path_data_t
*path_data
;
1035 path_data
= current
->data
;
1036 length
= path_data
[0].header
.length
;
1038 /* Execute any pending operation */
1039 _adg_do_operation(path
, path_data
);
1041 /* Append the path data to the internal path array */
1042 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
1045 /* Set path data to point to the recently appended cairo_path_data_t
1046 * primitive: the first struct is the header */
1047 path_data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
+
1048 (data
->cpml
.array
)->len
- length
;
1050 /* Store the over primitive */
1051 memcpy(&data
->over
, &data
->last
, sizeof(AdgPrimitive
));
1053 /* Set the last primitive for subsequent binary operations */
1054 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
1055 data
->last
.segment
= NULL
;
1056 data
->last
.data
= path_data
;
1058 /* Save the last point as the current point, if applicable */
1059 data
->cp_is_valid
= length
> 1;
1061 cpml_pair_from_cairo(&data
->cp
, &path_data
[length
-1]);
1063 /* Invalidate cairo_path: should be recomputed */
1064 _adg_clear_parent((AdgModel
*) path
);
1068 _adg_clear_operation(AdgPath
*path
)
1070 AdgPathPrivate
*data
;
1071 AdgOperation
*operation
;
1074 operation
= &data
->operation
;
1076 if (operation
->action
!= ADG_ACTION_NONE
) {
1077 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
1078 G_STRLOC
, _adg_action_name(operation
->action
));
1079 operation
->action
= ADG_ACTION_NONE
;
1082 data
->cp_is_valid
= FALSE
;
1083 data
->last
.data
= NULL
;
1084 data
->over
.data
= NULL
;
1088 _adg_append_operation(AdgPath
*path
, AdgAction action
, ...)
1090 AdgPathPrivate
*data
;
1091 AdgOperation
*operation
;
1096 if (data
->last
.data
== NULL
) {
1097 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
1098 G_STRLOC
, _adg_action_name(action
));
1102 operation
= &data
->operation
;
1103 if (operation
->action
!= ADG_ACTION_NONE
) {
1104 g_warning(_("%s: requested a `%s' operation while a `%s' operation was active"),
1105 G_STRLOC
, _adg_action_name(action
),
1106 _adg_action_name(operation
->action
));
1107 /* XXX: http://dev.entidi.com/p/adg/issues/50/ */
1111 va_start(var_args
, action
);
1115 case ADG_ACTION_CHAMFER
:
1116 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
1117 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
1120 case ADG_ACTION_FILLET
:
1121 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
1124 case ADG_ACTION_NONE
:
1129 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC
, action
);
1134 operation
->action
= action
;
1137 if (data
->last
.data
[0].header
.type
== CAIRO_PATH_CLOSE_PATH
) {
1138 /* Special case: an action with the close primitive should
1139 * be resolved now by changing the close primitive to a
1140 * line-to and using it as second operand and use the first
1141 * primitive of the current segment as first operand */
1143 cairo_path_data_t
*path_data
;
1144 CpmlSegment segment
;
1145 CpmlPrimitive current
;
1147 length
= data
->cpml
.array
->len
;
1149 /* Ensure the close path primitive is not the only data */
1150 g_return_val_if_fail(length
> 1, FALSE
);
1152 /* Allocate one more item once for all to accept the
1153 * conversion from a close to line-to primitive */
1154 data
->cpml
.array
= g_array_set_size(data
->cpml
.array
, length
+ 1);
1155 path_data
= (cairo_path_data_t
*) data
->cpml
.array
->data
;
1156 --data
->cpml
.array
->len
;
1158 /* Set segment and current (the first primitive of segment) */
1159 cpml_segment_from_cairo(&segment
, _adg_read_cpml_path(path
));
1160 while (cpml_segment_next(&segment
))
1162 cpml_primitive_from_segment(¤t
, &segment
);
1164 /* Convert close path to a line-to primitive */
1165 ++data
->cpml
.array
->len
;
1166 path_data
[length
- 1].header
.type
= CPML_LINE
;
1167 path_data
[length
- 1].header
.length
= 2;
1168 path_data
[length
] = *current
.org
;
1170 data
->last
.segment
= &segment
;
1171 data
->last
.org
= &path_data
[length
- 2];
1172 data
->last
.data
= &path_data
[length
- 1];
1174 _adg_do_action(path
, action
, ¤t
);
1182 _adg_do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
1184 AdgPathPrivate
*data
;
1187 AdgPrimitive current
;
1188 cairo_path_data_t current_org
;
1191 action
= data
->operation
.action
;
1192 cpml_segment_from_cairo(&segment
, _adg_read_cpml_path(path
));
1194 /* Construct the current primitive, that is the primitive to be
1195 * mixed with the last primitive with the specified operation.
1196 * Its org is a copy of the end point of the last primitive: it can be
1197 * modified without affecting anything else. It is expected the operation
1198 * functions will add to @path the primitives required but NOT to add
1199 * @current, as this one will be inserted automatically. */
1200 current
.segment
= &segment
;
1201 current
.org
= ¤t_org
;
1202 current
.data
= path_data
;
1203 cpml_pair_to_cairo(&data
->cp
, ¤t_org
);
1205 _adg_do_action(path
, action
, ¤t
);
1209 _adg_do_action(AdgPath
*path
, AdgAction action
, AdgPrimitive
*primitive
)
1212 case ADG_ACTION_NONE
:
1214 case ADG_ACTION_CHAMFER
:
1215 _adg_do_chamfer(path
, primitive
);
1217 case ADG_ACTION_FILLET
:
1218 _adg_do_fillet(path
, primitive
);
1221 g_return_if_reached();
1226 _adg_do_chamfer(AdgPath
*path
, AdgPrimitive
*current
)
1228 AdgPathPrivate
*data
;
1230 gdouble delta1
, delta2
;
1236 delta1
= data
->operation
.data
.chamfer
.delta1
;
1237 len1
= cpml_primitive_get_length(last
);
1239 if (delta1
>= len1
) {
1240 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1241 G_STRLOC
, delta1
, len1
);
1245 delta2
= data
->operation
.data
.chamfer
.delta2
;
1246 len2
= cpml_primitive_get_length(current
);
1248 if (delta2
>= len2
) {
1249 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1250 G_STRLOC
, delta1
, len1
);
1254 /* Change the end point of the last primitive */
1255 cpml_primitive_put_pair_at(last
, 1. - delta1
/ len1
, &pair
);
1256 cpml_primitive_set_point(last
, -1, &pair
);
1258 /* Change the start point of the current primitive */
1259 cpml_primitive_put_pair_at(current
, delta2
/ len2
, &pair
);
1260 cpml_primitive_set_point(current
, 0, &pair
);
1262 /* Add the chamfer line */
1263 data
->operation
.action
= ADG_ACTION_NONE
;
1264 adg_path_append(path
, CPML_LINE
, &pair
);
1268 _adg_do_fillet(AdgPath
*path
, AdgPrimitive
*current
)
1270 AdgPathPrivate
*data
;
1271 AdgPrimitive
*last
, *current_dup
, *last_dup
;
1272 gdouble radius
, offset
, pos
;
1273 AdgPair center
, vector
, p
[3];
1277 current_dup
= adg_primitive_deep_dup(current
);
1278 last_dup
= adg_primitive_deep_dup(last
);
1279 radius
= data
->operation
.data
.fillet
.radius
;
1280 offset
= _adg_is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1282 /* Find the center of the fillet from the intersection between
1283 * the last and current primitives offseted by radius */
1284 cpml_primitive_offset(current_dup
, offset
);
1285 cpml_primitive_offset(last_dup
, offset
);
1286 if (cpml_primitive_put_intersections(current_dup
, last_dup
, 1, ¢er
) == 0) {
1287 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1289 g_free(current_dup
);
1294 /* Compute the start point of the fillet */
1295 pos
= cpml_primitive_get_closest_pos(last_dup
, ¢er
);
1296 cpml_primitive_put_vector_at(last_dup
, pos
, &vector
);
1297 cpml_vector_set_length(&vector
, offset
);
1298 cpml_vector_normal(&vector
);
1299 p
[0].x
= center
.x
- vector
.x
;
1300 p
[0].y
= center
.y
- vector
.y
;
1302 /* Compute the mid point of the fillet */
1303 cpml_pair_from_cairo(&vector
, current
->org
);
1304 vector
.x
-= center
.x
;
1305 vector
.y
-= center
.y
;
1306 cpml_vector_set_length(&vector
, radius
);
1307 p
[1].x
= center
.x
+ vector
.x
;
1308 p
[1].y
= center
.y
+ vector
.y
;
1310 /* Compute the end point of the fillet */
1311 pos
= cpml_primitive_get_closest_pos(current_dup
, ¢er
);
1312 cpml_primitive_put_vector_at(current_dup
, pos
, &vector
);
1313 cpml_vector_set_length(&vector
, offset
);
1314 cpml_vector_normal(&vector
);
1315 p
[2].x
= center
.x
- vector
.x
;
1316 p
[2].y
= center
.y
- vector
.y
;
1318 g_free(current_dup
);
1321 /* Change the end point of the last primitive */
1322 cpml_primitive_set_point(last
, -1, &p
[0]);
1324 /* Change the start point of the current primitive */
1325 cpml_primitive_set_point(current
, 0, &p
[2]);
1327 /* Add the fillet arc */
1328 data
->operation
.action
= ADG_ACTION_NONE
;
1329 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
1333 _adg_is_convex(const AdgPrimitive
*primitive1
, const AdgPrimitive
*primitive2
)
1336 gdouble angle1
, angle2
;
1338 cpml_primitive_put_vector_at(primitive1
, -1, &v1
);
1339 cpml_primitive_put_vector_at(primitive2
, 0, &v2
);
1341 /* Probably there is a smarter way to get this without trygonometry */
1342 angle1
= cpml_vector_angle(&v1
);
1343 angle2
= cpml_vector_angle(&v2
);
1345 if (angle1
> angle2
)
1348 return angle2
-angle1
> G_PI
;
1351 static const gchar
*
1352 _adg_action_name(AdgAction action
)
1355 case ADG_ACTION_NONE
:
1357 case ADG_ACTION_CHAMFER
:
1359 case ADG_ACTION_FILLET
:
1367 _adg_get_named_pair(AdgModel
*model
, const gchar
*name
,
1368 AdgPair
*pair
, gpointer user_data
)
1370 GSList
**named_pairs
;
1371 AdgNamedPair
*named_pair
;
1373 named_pairs
= user_data
;
1375 named_pair
= g_new(AdgNamedPair
, 1);
1376 named_pair
->name
= name
;
1377 named_pair
->pair
= *pair
;
1379 *named_pairs
= g_slist_prepend(*named_pairs
, named_pair
);
1383 _adg_dup_reverse_named_pairs(AdgModel
*model
, const AdgMatrix
*matrix
)
1385 AdgNamedPair
*old_named_pair
;
1386 AdgNamedPair named_pair
;
1387 GSList
*named_pairs
;
1389 /* Populate named_pairs with all the named pairs of model */
1391 adg_model_foreach_named_pair(model
, _adg_get_named_pair
, &named_pairs
);
1393 /* Readd the pairs applying the reversing transformation matrix to
1394 * their coordinates and prepending a "-" to their name */
1395 while (named_pairs
) {
1396 old_named_pair
= named_pairs
->data
;
1398 named_pair
.name
= g_strdup_printf("-%s", old_named_pair
->name
);
1399 named_pair
.pair
= old_named_pair
->pair
;
1400 cpml_pair_transform(&named_pair
.pair
, matrix
);
1402 adg_model_set_named_pair(model
, named_pair
.name
, &named_pair
.pair
);
1404 g_free((gpointer
) named_pair
.name
);
1405 named_pairs
= g_slist_delete_link(named_pairs
, named_pairs
);