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 void _adg_append_primitive (AdgPath
*path
,
87 AdgPrimitive
*primitive
);
88 static void _adg_clear_operation (AdgPath
*path
);
89 static gboolean
_adg_append_operation (AdgPath
*path
,
92 static void _adg_do_operation (AdgPath
*path
,
95 static void _adg_do_action (AdgPath
*path
,
97 AdgPrimitive
*primitive
);
98 static void _adg_do_chamfer (AdgPath
*path
,
99 AdgPrimitive
*current
);
100 static void _adg_do_fillet (AdgPath
*path
,
101 AdgPrimitive
*current
);
102 static gboolean
_adg_is_convex (const AdgPrimitive
106 static const gchar
* _adg_action_name (AdgAction action
);
107 static void _adg_get_named_pair (AdgModel
*model
,
111 static void _adg_dup_reverse_named_pairs
113 const AdgMatrix
*matrix
);
117 adg_path_class_init(AdgPathClass
*klass
)
119 GObjectClass
*gobject_class
;
120 AdgModelClass
*model_class
;
121 AdgTrailClass
*trail_class
;
123 gobject_class
= (GObjectClass
*) klass
;
124 model_class
= (AdgModelClass
*) klass
;
125 trail_class
= (AdgTrailClass
*) klass
;
127 g_type_class_add_private(klass
, sizeof(AdgPathPrivate
));
129 gobject_class
->finalize
= _adg_finalize
;
131 model_class
->clear
= _adg_clear
;
132 model_class
->changed
= _adg_changed
;
134 trail_class
->get_cpml_path
= _adg_get_cpml_path
;
138 adg_path_init(AdgPath
*path
)
140 AdgPathPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(path
, ADG_TYPE_PATH
,
143 data
->cp_is_valid
= FALSE
;
144 data
->cpml
.array
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
145 data
->operation
.action
= ADG_ACTION_NONE
;
151 _adg_finalize(GObject
*object
)
154 AdgPathPrivate
*data
;
156 path
= (AdgPath
*) object
;
159 g_array_free(data
->cpml
.array
, TRUE
);
160 _adg_clear_operation(path
);
162 if (_ADG_OLD_OBJECT_CLASS
->finalize
)
163 _ADG_OLD_OBJECT_CLASS
->finalize(object
);
170 * Creates a new path model. The path should be constructed
171 * programmatically by using the methods provided by #AdgPath.
173 * Returns: the newly created path model
180 return g_object_new(ADG_TYPE_PATH
, NULL
);
184 * adg_path_get_current_point:
187 * Gets the current point of @path, which is conceptually the
188 * final point reached by the path so far.
190 * If there is no defined current point, %NULL is returned.
191 * It is possible to check this in advance with
192 * adg_path_has_current_point().
194 * Most #AdgPath methods alter the current point and most of them
195 * expect a current point to be defined otherwise will fail triggering
196 * a warning. Check the description of every method for specific details.
198 * Returns: the current point or %NULL on no current point set or errors
203 adg_path_get_current_point(AdgPath
*path
)
205 AdgPathPrivate
*data
;
207 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
211 if (!data
->cp_is_valid
)
218 * adg_path_has_current_point:
221 * Returns whether a current point is defined on @path.
222 * See adg_path_get_current_point() for details on the current point.
224 * Returns: whether a current point is defined
229 adg_path_has_current_point(AdgPath
*path
)
231 AdgPathPrivate
*data
;
233 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
237 return data
->cp_is_valid
;
241 * adg_path_last_primitive:
244 * Gets the last primitive appended to @path. The returned struct
245 * is owned by @path and should not be freed or modified.
247 * Returns: a pointer to the last appended primitive or %NULL on errors
252 adg_path_last_primitive(AdgPath
*path
)
254 AdgPathPrivate
*data
;
256 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
264 * adg_path_over_primitive:
267 * Gets the primitive before the last one appended to @path. The
268 * "over" term comes from forth, where the %OVER operator works
269 * on the stack in the same way as adg_path_over_primitive() works
270 * on @path. The returned struct is owned by @path and should not
271 * be freed or modified.
273 * Returns: a pointer to the primitive before the last appended one
279 adg_path_over_primitive(AdgPath
*path
)
281 AdgPathPrivate
*data
;
283 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
293 * @type: a #cairo_data_type_t value
294 * @Varargs: point data, specified as #AdgPair pointers
296 * Generic method to append a primitive to @path. The number of #AdgPair
297 * structs depends on @type: there is no way with this function to
298 * reserve more cairo_path_data_t structs than what is needed by the
301 * This function accepts also the special #CPML_ARC primitive.
303 * If @path has no current point while the requested primitive needs it,
304 * a warning message will be triggered without other effect.
309 adg_path_append(AdgPath
*path
, CpmlPrimitiveType type
, ...)
313 va_start(var_args
, type
);
314 adg_path_append_valist(path
, type
, var_args
);
319 * adg_path_append_valist:
321 * @type: a #cairo_data_type_t value
322 * @var_args: point data, specified as #AdgPair pointers
324 * va_list version of adg_path_append().
329 adg_path_append_valist(AdgPath
*path
, CpmlPrimitiveType type
, va_list var_args
)
331 AdgPathPrivate
*data
;
332 AdgPrimitive primitive
;
334 cairo_path_data_t org
;
335 cairo_path_data_t
*path_data
;
338 g_return_if_fail(ADG_IS_PATH(path
));
342 if (type
== CPML_CLOSE
)
344 else if (type
== CPML_MOVE
)
347 length
= cpml_primitive_type_get_n_points(type
);
352 /* Set a copy of the current point as the primitive origin */
353 cpml_pair_to_cairo(&data
->cp
, &org
);
354 primitive
.org
= &org
;
356 /* Build the cairo_path_data_t array */
357 primitive
.data
= path_data
= g_new(cairo_path_data_t
, length
);
359 path_data
->header
.type
= type
;
360 path_data
->header
.length
= length
;
362 for (cnt
= 1; cnt
< length
; ++ cnt
) {
363 pair
= va_arg(var_args
, AdgPair
*);
365 g_free(primitive
.data
);
366 g_warning(_("%s: null pair caught while parsing arguments"),
372 cpml_pair_to_cairo(pair
, path_data
);
375 /* Terminate the creation of the temporary primitive */
376 primitive
.segment
= NULL
;
378 /* Append this primitive to @path */
379 _adg_append_primitive(path
, &primitive
);
381 g_free(primitive
.data
);
385 * adg_path_append_primitive:
387 * @primitive: the #AdgPrimitive to append
389 * Appends @primitive to @path. The primitive to add is considered the
390 * continuation of the current path so the <structfield>org</structfield>
391 * component of @primitive is not used. Anyway the current point is
392 * checked against it: they must be equal or the function will fail
393 * without further processing.
398 adg_path_append_primitive(AdgPath
*path
, const AdgPrimitive
*primitive
)
400 AdgPathPrivate
*data
;
401 AdgPrimitive
*primitive_dup
;
403 g_return_if_fail(ADG_IS_PATH(path
));
404 g_return_if_fail(primitive
!= NULL
);
405 g_return_if_fail(primitive
->org
!= NULL
);
409 g_return_if_fail(primitive
->org
->point
.x
== data
->cp
.x
&&
410 primitive
->org
->point
.y
== data
->cp
.y
);
412 /* The primitive data could be modified by pending operations:
414 primitive_dup
= adg_primitive_deep_dup(primitive
);
416 _adg_append_primitive(path
, primitive_dup
);
418 g_free(primitive_dup
);
422 * adg_path_append_segment:
424 * @segment: the #AdgSegment to append
426 * Appends @segment to @path.
431 adg_path_append_segment(AdgPath
*path
, const AdgSegment
*segment
)
433 AdgPathPrivate
*data
;
435 g_return_if_fail(ADG_IS_PATH(path
));
436 g_return_if_fail(segment
!= NULL
);
440 _adg_clear_parent((AdgModel
*) path
);
441 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
442 segment
->data
, segment
->num_data
);
446 * adg_path_append_cpml_path:
448 * @cpml_path: the #cairo_path_t path to append
450 * Appends a whole #CpmlPath to @path. #CpmlPath is a superset of
451 * #cairo_path_t, so this function can be feeded with both.
456 adg_path_append_cpml_path(AdgPath
*path
, const CpmlPath
*cpml_path
)
458 AdgPathPrivate
*data
;
460 g_return_if_fail(ADG_IS_PATH(path
));
461 g_return_if_fail(cpml_path
!= NULL
);
465 _adg_clear_parent((AdgModel
*) path
);
466 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
468 cpml_path
->num_data
);
474 * @pair: the destination coordinates
476 * Begins a new segment. After this call the current point will be @pair.
481 adg_path_move_to(AdgPath
*path
, const AdgPair
*pair
)
483 adg_path_append(path
, CPML_MOVE
, pair
);
487 * adg_path_move_to_explicit:
489 * @x: the new x coordinate
490 * @y: the new y coordinate
492 * Convenient function to call adg_path_move_to() using explicit
493 * coordinates instead of #AdgPair.
495 * Rename to: adg_path_move_to
500 adg_path_move_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
507 adg_path_append(path
, CPML_MOVE
, &p
);
513 * @pair: the destination coordinates
515 * Adds a line to @path from the current point to @pair. After this
516 * call the current point will be @pair.
518 * If @path has no current point before this call, this function will
519 * trigger a warning without other effect.
524 adg_path_line_to(AdgPath
*path
, const AdgPair
*pair
)
526 adg_path_append(path
, CPML_LINE
, pair
);
530 * adg_path_line_to_explicit:
532 * @x: the new x coordinate
533 * @y: the new y coordinate
535 * Convenient function to call adg_path_line_to() using explicit
536 * coordinates instead of #AdgPair.
538 * Rename to: adg_path_line_to
543 adg_path_line_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
550 adg_path_append(path
, CPML_LINE
, &p
);
556 * @throught: an arbitrary point on the arc
557 * @pair: the destination coordinates
559 * Adds an arc to the path from the current point to @pair, passing
560 * throught @throught. After this call the current point will be @pair.
562 * If @path has no current point before this call, this function will
563 * trigger a warning without other effect.
568 adg_path_arc_to(AdgPath
*path
, const AdgPair
*throught
, const AdgPair
*pair
)
570 adg_path_append(path
, CPML_ARC
, throught
, pair
);
574 * adg_path_arc_to_explicit:
576 * @x1: the x coordinate of an intermediate point
577 * @y1: the y coordinate of an intermediate point
578 * @x2: the x coordinate of the end of the arc
579 * @y2: the y coordinate of the end of the arc
581 * Convenient function to call adg_path_arc_to() using explicit
582 * coordinates instead of #AdgPair.
584 * Rename to: adg_path_arc_to
589 adg_path_arc_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
590 gdouble x2
, gdouble y2
)
599 adg_path_append(path
, CPML_ARC
, &p
[0], &p
[1]);
605 * @control1: the first control point of the curve
606 * @control2: the second control point of the curve
607 * @pair: the destination coordinates
609 * Adds a cubic Bézier curve to the path from the current point to
610 * position @pair, using @control1 and @control2 as control points.
611 * After this call the current point will be @pair.
613 * If @path has no current point before this call, this function will
614 * trigger a warning without other effect.
619 adg_path_curve_to(AdgPath
*path
, const AdgPair
*control1
,
620 const AdgPair
*control2
, const AdgPair
*pair
)
622 adg_path_append(path
, CPML_CURVE
, control1
, control2
, pair
);
626 * adg_path_curve_to_explicit:
628 * @x1: the x coordinate of the first control point
629 * @y1: the y coordinate of the first control point
630 * @x2: the x coordinate of the second control point
631 * @y2: the y coordinate of the second control point
632 * @x3: the x coordinate of the end of the curve
633 * @y3: the y coordinate of the end of the curve
635 * Convenient function to call adg_path_curve_to() using explicit
636 * coordinates instead of #AdgPair.
638 * Rename to: adg_path_curve_to
643 adg_path_curve_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
644 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
655 adg_path_append(path
, CPML_CURVE
, &p
[0], &p
[1], &p
[2]);
662 * Adds a line segment to the path from the current point to the
663 * beginning of the current segment, (the most recent point passed
664 * to an adg_path_move_to()), and closes this segment.
665 * After this call the current point will be unset.
667 * The behavior of adg_path_close() is distinct from simply calling
668 * adg_line_to() with the coordinates of the segment starting point.
669 * When a closed segment is stroked, there are no caps on the ends.
670 * Instead, there is a line join connecting the final and initial
671 * primitive of the segment.
673 * If @path has no current point before this call, this function will
674 * trigger a warning without other effect.
679 adg_path_close(AdgPath
*path
)
681 adg_path_append(path
, CPML_CLOSE
);
687 * @center: coordinates of the center of the arc
688 * @r: the radius of the arc
689 * @start: the start angle, in radians
690 * @end: the end angle, in radians
692 * A more usual way to add an arc to @path. After this call, the current
693 * point will be the computed end point of the arc. The arc will be
694 * rendered in increasing angle, accordling to @start and @end. This means
695 * if @start is less than @end, the arc will be rendered in clockwise
696 * direction (accordling to the default cairo coordinate system) while if
697 * @start is greather than @end, the arc will be rendered in couterclockwise
700 * By explicitely setting the whole arc data, the start point could be
701 * different from the current point. In this case, if @path has no
702 * current point before the call a #CPML_MOVE to the start point of
703 * the arc will be automatically prepended to the arc. If @path has a
704 * current point, a #CPML_LINE to the start point of the arc will be
705 * used instead of the "move to" primitive.
710 adg_path_arc(AdgPath
*path
, const AdgPair
*center
, gdouble r
,
711 gdouble start
, gdouble end
)
713 AdgPathPrivate
*data
;
716 g_return_if_fail(ADG_IS_PATH(path
));
717 g_return_if_fail(center
!= NULL
);
720 cpml_vector_from_angle(&p
[0], start
);
721 cpml_vector_from_angle(&p
[1], (end
-start
) / 2);
722 cpml_vector_from_angle(&p
[2], end
);
724 cpml_vector_set_length(&p
[0], r
);
725 cpml_vector_set_length(&p
[1], r
);
726 cpml_vector_set_length(&p
[2], r
);
735 if (!data
->cp_is_valid
)
736 adg_path_append(path
, CPML_MOVE
, &p
[0]);
737 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
738 adg_path_append(path
, CPML_LINE
, &p
[0]);
740 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
744 * adg_path_arc_explicit:
746 * @xc: x position of the center of the arc
747 * @yc: y position of the center of the arc
748 * @r: the radius of the arc
749 * @start: the start angle, in radians
750 * @end: the end angle, in radians
752 * Convenient function to call adg_path_arc() using explicit
753 * coordinates instead of #AdgPair.
755 * Rename to: adg_path_arc
760 adg_path_arc_explicit(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
761 gdouble start
, gdouble end
)
768 adg_path_arc(path
, ¢er
, r
, start
, end
);
774 * @delta1: the distance from the intersection point of the current primitive
775 * @delta2: the distance from the intersection point of the next primitive
777 * A binary action that generates a chamfer between two primitives.
778 * The first primitive involved is the current primitive, the second will
779 * be the next primitive appended to @path after this call. The second
780 * primitive is required: if the chamfer operation is not properly
781 * terminated (by not providing the second primitive), any API accessing
782 * the path in reading mode will raise a warning.
784 * An exception is a chamfer after a #CPML_CLOSE primitive. In this case,
785 * the second primitive is not required: the current close path is used
786 * as first operand while the first primitive of the current segment is
787 * used as second operand.
789 * The chamfer operation requires two lengths: @delta1 specifies the
790 * "quantity" to trim on the first primitive while @delta2 is the same
791 * applied on the second primitive. The term "quantity" means the length
792 * of the portion to cut out from the original primitive (that is the
793 * primitive as would be without the chamfer).
798 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
800 g_return_if_fail(ADG_IS_PATH(path
));
802 if (!_adg_append_operation(path
, ADG_ACTION_CHAMFER
, delta1
, delta2
))
809 * @radius: the radius of the fillet
811 * A binary action that joins to primitives with an arc.
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 fillet 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 fillet 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.
826 adg_path_fillet(AdgPath
*path
, gdouble radius
)
828 g_return_if_fail(ADG_IS_PATH(path
));
830 if (!_adg_append_operation(path
, ADG_ACTION_FILLET
, radius
))
837 * @vector: the slope of the axis
839 * Reflects the first segment or @path around the axis passing
840 * throught (0, 0) and with a @vector slope. The internal segment
841 * is duplicated and the proper transformation (computed from
842 * @vector) to mirror the segment is applied on all its points.
843 * The result is then reversed with cpml_segment_reverse() and
844 * appended to the original path with adg_path_append_segment().
846 * For convenience, if @vector is %NULL the path is reversed
847 * around the x axis (y=0).
852 adg_path_reflect(AdgPath
*path
, const CpmlVector
*vector
)
856 AdgSegment segment
, *dup_segment
;
858 g_return_if_fail(ADG_IS_PATH(path
));
859 g_return_if_fail(vector
== NULL
|| vector
->x
!= 0 || vector
->y
!= 0);
861 model
= (AdgModel
*) path
;
863 if (vector
== NULL
) {
864 cairo_matrix_init_scale(&matrix
, 1, -1);
867 gdouble cos2angle
, sin2angle
;
869 cpml_pair_copy(&slope
, vector
);
870 cpml_vector_set_length(&slope
, 1);
872 if (slope
.x
== 0 && slope
.y
== 0) {
873 g_warning(_("%s: the axis of the reflection is not known"),
878 sin2angle
= 2. * vector
->x
* vector
->y
;
879 cos2angle
= 2. * vector
->x
* vector
->x
- 1;
881 cairo_matrix_init(&matrix
, cos2angle
, sin2angle
,
882 sin2angle
, -cos2angle
, 0, 0);
885 if (!adg_trail_put_segment((AdgTrail
*) path
, 1, &segment
))
888 /* No need to reverse an empty segment */
889 if (segment
.num_data
== 0 || segment
.num_data
== 0)
892 dup_segment
= adg_segment_deep_dup(&segment
);
893 if (dup_segment
== NULL
)
896 cpml_segment_reverse(dup_segment
);
897 cpml_segment_transform(dup_segment
, &matrix
);
898 dup_segment
->data
[0].header
.type
= CPML_LINE
;
900 adg_path_append_segment(path
, dup_segment
);
904 _adg_dup_reverse_named_pairs(model
, &matrix
);
908 * adg_path_reflect_explicit:
910 * @x: the vector x component
911 * @y: the vector y component
913 * Convenient function to call adg_path_reflect() using explicit
914 * vector components instead of #CpmlVector.
916 * Rename to: adg_path_reflect
921 adg_path_reflect_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
928 adg_path_reflect(path
, &vector
);
933 _adg_clear(AdgModel
*model
)
936 AdgPathPrivate
*data
;
938 path
= (AdgPath
*) model
;
941 g_array_set_size(data
->cpml
.array
, 0);
942 _adg_clear_operation(path
);
943 _adg_clear_parent(model
);
947 _adg_clear_parent(AdgModel
*model
)
949 if (_ADG_OLD_MODEL_CLASS
->clear
)
950 _ADG_OLD_MODEL_CLASS
->clear(model
);
954 _adg_changed(AdgModel
*model
)
956 _adg_clear_parent(model
);
958 if (_ADG_OLD_MODEL_CLASS
->changed
)
959 _ADG_OLD_MODEL_CLASS
->changed(model
);
963 _adg_get_cpml_path(AdgTrail
*trail
)
965 _adg_clear_parent((AdgModel
*) trail
);
966 return _adg_read_cpml_path((AdgPath
*) trail
);
970 _adg_read_cpml_path(AdgPath
*path
)
972 AdgPathPrivate
*data
= path
->data
;
974 /* Always regenerate the CpmlPath as it is a trivial operation */
975 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
976 data
->cpml
.path
.data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
;
977 data
->cpml
.path
.num_data
= (data
->cpml
.array
)->len
;
979 return &data
->cpml
.path
;
983 _adg_append_primitive(AdgPath
*path
, AdgPrimitive
*current
)
985 AdgPathPrivate
*data
;
986 cairo_path_data_t
*path_data
;
990 path_data
= current
->data
;
991 length
= path_data
[0].header
.length
;
993 /* Execute any pending operation */
994 _adg_do_operation(path
, path_data
);
996 /* Append the path data to the internal path array */
997 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
1000 /* Set path data to point to the recently appended cairo_path_data_t
1001 * primitive: the first struct is the header */
1002 path_data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
+
1003 (data
->cpml
.array
)->len
- length
;
1005 /* Store the over primitive */
1006 memcpy(&data
->over
, &data
->last
, sizeof(AdgPrimitive
));
1008 /* Set the last primitive for subsequent binary operations */
1009 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
1010 data
->last
.segment
= NULL
;
1011 data
->last
.data
= path_data
;
1013 /* Save the last point as the current point, if applicable */
1014 data
->cp_is_valid
= length
> 1;
1016 cpml_pair_from_cairo(&data
->cp
, &path_data
[length
-1]);
1018 /* Invalidate cairo_path: should be recomputed */
1019 _adg_clear_parent((AdgModel
*) path
);
1023 _adg_clear_operation(AdgPath
*path
)
1025 AdgPathPrivate
*data
;
1026 AdgOperation
*operation
;
1029 operation
= &data
->operation
;
1031 if (operation
->action
!= ADG_ACTION_NONE
) {
1032 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
1033 G_STRLOC
, _adg_action_name(operation
->action
));
1034 operation
->action
= ADG_ACTION_NONE
;
1037 data
->cp_is_valid
= FALSE
;
1038 data
->last
.data
= NULL
;
1039 data
->over
.data
= NULL
;
1043 _adg_append_operation(AdgPath
*path
, AdgAction action
, ...)
1045 AdgPathPrivate
*data
;
1046 AdgOperation
*operation
;
1051 if (data
->last
.data
== NULL
) {
1052 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
1053 G_STRLOC
, _adg_action_name(action
));
1057 operation
= &data
->operation
;
1058 if (operation
->action
!= ADG_ACTION_NONE
) {
1059 g_warning(_("%s: requested a `%s' operation while a `%s' operation was active"),
1060 G_STRLOC
, _adg_action_name(action
),
1061 _adg_action_name(operation
->action
));
1062 /* XXX: http://dev.entidi.com/p/adg/issues/50/ */
1066 va_start(var_args
, action
);
1070 case ADG_ACTION_CHAMFER
:
1071 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
1072 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
1075 case ADG_ACTION_FILLET
:
1076 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
1079 case ADG_ACTION_NONE
:
1084 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC
, action
);
1089 operation
->action
= action
;
1092 if (data
->last
.data
[0].header
.type
== CAIRO_PATH_CLOSE_PATH
) {
1093 /* Special case: an action with the close primitive should
1094 * be resolved now by changing the close primitive to a
1095 * line-to and using it as second operand and use the first
1096 * primitive of the current segment as first operand */
1098 cairo_path_data_t
*path_data
;
1099 CpmlSegment segment
;
1100 CpmlPrimitive current
;
1102 length
= data
->cpml
.array
->len
;
1104 /* Ensure the close path primitive is not the only data */
1105 g_return_val_if_fail(length
> 1, FALSE
);
1107 /* Allocate one more item once for all to accept the
1108 * conversion from a close to line-to primitive */
1109 data
->cpml
.array
= g_array_set_size(data
->cpml
.array
, length
+ 1);
1110 path_data
= (cairo_path_data_t
*) data
->cpml
.array
->data
;
1111 --data
->cpml
.array
->len
;
1113 /* Set segment and current (the first primitive of segment) */
1114 cpml_segment_from_cairo(&segment
, _adg_read_cpml_path(path
));
1115 while (cpml_segment_next(&segment
))
1117 cpml_primitive_from_segment(¤t
, &segment
);
1119 /* Convert close path to a line-to primitive */
1120 ++data
->cpml
.array
->len
;
1121 path_data
[length
- 1].header
.type
= CPML_LINE
;
1122 path_data
[length
- 1].header
.length
= 2;
1123 path_data
[length
] = *current
.org
;
1125 data
->last
.segment
= &segment
;
1126 data
->last
.org
= &path_data
[length
- 2];
1127 data
->last
.data
= &path_data
[length
- 1];
1129 _adg_do_action(path
, action
, ¤t
);
1137 _adg_do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
1139 AdgPathPrivate
*data
;
1142 AdgPrimitive current
;
1143 cairo_path_data_t current_org
;
1146 action
= data
->operation
.action
;
1147 cpml_segment_from_cairo(&segment
, _adg_read_cpml_path(path
));
1149 /* Construct the current primitive, that is the primitive to be
1150 * mixed with the last primitive with the specified operation.
1151 * Its org is a copy of the end point of the last primitive: it can be
1152 * modified without affecting anything else. It is expected the operation
1153 * functions will add to @path the primitives required but NOT to add
1154 * @current, as this one will be inserted automatically. */
1155 current
.segment
= &segment
;
1156 current
.org
= ¤t_org
;
1157 current
.data
= path_data
;
1158 cpml_pair_to_cairo(&data
->cp
, ¤t_org
);
1160 _adg_do_action(path
, action
, ¤t
);
1164 _adg_do_action(AdgPath
*path
, AdgAction action
, AdgPrimitive
*primitive
)
1167 case ADG_ACTION_NONE
:
1169 case ADG_ACTION_CHAMFER
:
1170 _adg_do_chamfer(path
, primitive
);
1172 case ADG_ACTION_FILLET
:
1173 _adg_do_fillet(path
, primitive
);
1176 g_return_if_reached();
1181 _adg_do_chamfer(AdgPath
*path
, AdgPrimitive
*current
)
1183 AdgPathPrivate
*data
;
1185 gdouble delta1
, delta2
;
1191 delta1
= data
->operation
.data
.chamfer
.delta1
;
1192 len1
= cpml_primitive_get_length(last
);
1194 if (delta1
>= len1
) {
1195 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1196 G_STRLOC
, delta1
, len1
);
1200 delta2
= data
->operation
.data
.chamfer
.delta2
;
1201 len2
= cpml_primitive_get_length(current
);
1203 if (delta2
>= len2
) {
1204 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1205 G_STRLOC
, delta1
, len1
);
1209 /* Change the end point of the last primitive */
1210 cpml_primitive_put_pair_at(last
, 1. - delta1
/ len1
, &pair
);
1211 cpml_primitive_set_point(last
, -1, &pair
);
1213 /* Change the start point of the current primitive */
1214 cpml_primitive_put_pair_at(current
, delta2
/ len2
, &pair
);
1215 cpml_primitive_set_point(current
, 0, &pair
);
1217 /* Add the chamfer line */
1218 data
->operation
.action
= ADG_ACTION_NONE
;
1219 adg_path_append(path
, CPML_LINE
, &pair
);
1223 _adg_do_fillet(AdgPath
*path
, AdgPrimitive
*current
)
1225 AdgPathPrivate
*data
;
1226 AdgPrimitive
*last
, *current_dup
, *last_dup
;
1227 gdouble radius
, offset
, pos
;
1228 AdgPair center
, vector
, p
[3];
1232 current_dup
= adg_primitive_deep_dup(current
);
1233 last_dup
= adg_primitive_deep_dup(last
);
1234 radius
= data
->operation
.data
.fillet
.radius
;
1235 offset
= _adg_is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1237 /* Find the center of the fillet from the intersection between
1238 * the last and current primitives offseted by radius */
1239 cpml_primitive_offset(current_dup
, offset
);
1240 cpml_primitive_offset(last_dup
, offset
);
1241 if (cpml_primitive_put_intersections(current_dup
, last_dup
, 1, ¢er
) == 0) {
1242 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1244 g_free(current_dup
);
1249 /* Compute the start point of the fillet */
1250 pos
= cpml_primitive_get_closest_pos(last_dup
, ¢er
);
1251 cpml_primitive_put_vector_at(last_dup
, pos
, &vector
);
1252 cpml_vector_set_length(&vector
, offset
);
1253 cpml_vector_normal(&vector
);
1254 p
[0].x
= center
.x
- vector
.x
;
1255 p
[0].y
= center
.y
- vector
.y
;
1257 /* Compute the mid point of the fillet */
1258 cpml_pair_from_cairo(&vector
, current
->org
);
1259 vector
.x
-= center
.x
;
1260 vector
.y
-= center
.y
;
1261 cpml_vector_set_length(&vector
, radius
);
1262 p
[1].x
= center
.x
+ vector
.x
;
1263 p
[1].y
= center
.y
+ vector
.y
;
1265 /* Compute the end point of the fillet */
1266 pos
= cpml_primitive_get_closest_pos(current_dup
, ¢er
);
1267 cpml_primitive_put_vector_at(current_dup
, pos
, &vector
);
1268 cpml_vector_set_length(&vector
, offset
);
1269 cpml_vector_normal(&vector
);
1270 p
[2].x
= center
.x
- vector
.x
;
1271 p
[2].y
= center
.y
- vector
.y
;
1273 g_free(current_dup
);
1276 /* Change the end point of the last primitive */
1277 cpml_primitive_set_point(last
, -1, &p
[0]);
1279 /* Change the start point of the current primitive */
1280 cpml_primitive_set_point(current
, 0, &p
[2]);
1282 /* Add the fillet arc */
1283 data
->operation
.action
= ADG_ACTION_NONE
;
1284 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
1288 _adg_is_convex(const AdgPrimitive
*primitive1
, const AdgPrimitive
*primitive2
)
1291 gdouble angle1
, angle2
;
1293 cpml_primitive_put_vector_at(primitive1
, -1, &v1
);
1294 cpml_primitive_put_vector_at(primitive2
, 0, &v2
);
1296 /* Probably there is a smarter way to get this without trygonometry */
1297 angle1
= cpml_vector_angle(&v1
);
1298 angle2
= cpml_vector_angle(&v2
);
1300 if (angle1
> angle2
)
1303 return angle2
-angle1
> G_PI
;
1306 static const gchar
*
1307 _adg_action_name(AdgAction action
)
1310 case ADG_ACTION_NONE
:
1312 case ADG_ACTION_CHAMFER
:
1314 case ADG_ACTION_FILLET
:
1322 _adg_get_named_pair(AdgModel
*model
, const gchar
*name
,
1323 AdgPair
*pair
, gpointer user_data
)
1325 GSList
**named_pairs
;
1326 AdgNamedPair
*named_pair
;
1328 named_pairs
= user_data
;
1330 named_pair
= g_new(AdgNamedPair
, 1);
1331 named_pair
->name
= name
;
1332 named_pair
->pair
= *pair
;
1334 *named_pairs
= g_slist_prepend(*named_pairs
, named_pair
);
1338 _adg_dup_reverse_named_pairs(AdgModel
*model
, const AdgMatrix
*matrix
)
1340 AdgNamedPair
*old_named_pair
;
1341 AdgNamedPair named_pair
;
1342 GSList
*named_pairs
;
1344 /* Populate named_pairs with all the named pairs of model */
1346 adg_model_foreach_named_pair(model
, _adg_get_named_pair
, &named_pairs
);
1348 /* Readd the pairs applying the reversing transformation matrix to
1349 * their coordinates and prepending a "-" to their name */
1350 while (named_pairs
) {
1351 old_named_pair
= named_pairs
->data
;
1353 named_pair
.name
= g_strdup_printf("-%s", old_named_pair
->name
);
1354 named_pair
.pair
= old_named_pair
->pair
;
1355 cpml_pair_transform(&named_pair
.pair
, matrix
);
1357 adg_model_set_named_pair(model
, named_pair
.name
, &named_pair
.pair
);
1359 g_free((gpointer
) named_pair
.name
);
1360 named_pairs
= g_slist_delete_link(named_pairs
, named_pairs
);