1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 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 #cairo_path_t: this class
26 * implements methods to create the path and provides additional
27 * operations specific to technical drawings.
29 * #AdgPath overrides the get_cairo_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 #cairo_path_t
33 * returned by adg_trail_get_cairo_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 cairo_path_t
* _adg_get_cairo_path (AdgTrail
*trail
);
85 static cairo_path_t
* _adg_read_cairo_path (AdgPath
*path
);
86 static gint
_adg_primitive_length (CpmlPrimitiveType type
);
87 static void _adg_append_primitive (AdgPath
*path
,
88 CpmlPrimitive
*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 CpmlPrimitive
*primitive
);
99 static void _adg_do_chamfer (AdgPath
*path
,
100 CpmlPrimitive
*current
);
101 static void _adg_do_fillet (AdgPath
*path
,
102 CpmlPrimitive
*current
);
103 static gboolean
_adg_is_convex (const CpmlPrimitive
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
119 adg_path_class_init(AdgPathClass
*klass
)
121 GObjectClass
*gobject_class
;
122 AdgModelClass
*model_class
;
123 AdgTrailClass
*trail_class
;
125 gobject_class
= (GObjectClass
*) klass
;
126 model_class
= (AdgModelClass
*) klass
;
127 trail_class
= (AdgTrailClass
*) klass
;
129 g_type_class_add_private(klass
, sizeof(AdgPathPrivate
));
131 gobject_class
->finalize
= _adg_finalize
;
133 model_class
->clear
= _adg_clear
;
134 model_class
->changed
= _adg_changed
;
136 trail_class
->get_cairo_path
= _adg_get_cairo_path
;
140 adg_path_init(AdgPath
*path
)
142 AdgPathPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(path
, ADG_TYPE_PATH
,
145 data
->cp_is_valid
= FALSE
;
146 data
->cairo
.array
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
147 data
->operation
.action
= ADG_ACTION_NONE
;
153 _adg_finalize(GObject
*object
)
156 AdgPathPrivate
*data
;
158 path
= (AdgPath
*) object
;
161 g_array_free(data
->cairo
.array
, TRUE
);
162 _adg_clear_operation(path
);
164 if (_ADG_OLD_OBJECT_CLASS
->finalize
)
165 _ADG_OLD_OBJECT_CLASS
->finalize(object
);
172 * Creates a new path model. The path should be constructed
173 * programmatically by using the methods provided by #AdgPath.
175 * Returns: the newly created path model
182 return g_object_new(ADG_TYPE_PATH
, NULL
);
186 * adg_path_get_current_point:
189 * Gets the current point of @path, which is conceptually the
190 * final point reached by the path so far.
192 * If there is no defined current point, %NULL is returned.
193 * It is possible to check this in advance with
194 * adg_path_has_current_point().
196 * Most #AdgPath methods alter the current point and most of them
197 * expect a current point to be defined otherwise will fail triggering
198 * a warning. Check the description of every method for specific details.
200 * Returns: (transfer none): the current point or %NULL on no current point set or errors
205 adg_path_get_current_point(AdgPath
*path
)
207 AdgPathPrivate
*data
;
209 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
213 if (!data
->cp_is_valid
)
220 * adg_path_has_current_point:
223 * Returns whether a current point is defined on @path.
224 * See adg_path_get_current_point() for details on the current point.
226 * Returns: whether a current point is defined
231 adg_path_has_current_point(AdgPath
*path
)
233 AdgPathPrivate
*data
;
235 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
239 return data
->cp_is_valid
;
243 * adg_path_last_primitive:
246 * Gets the last primitive appended to @path. The returned struct
247 * is owned by @path and should not be freed or modified.
249 * Returns: (transfer none): a pointer to the last appended primitive or %NULL on errors
253 const CpmlPrimitive
*
254 adg_path_last_primitive(AdgPath
*path
)
256 AdgPathPrivate
*data
;
258 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
266 * adg_path_over_primitive:
269 * Gets the primitive before the last one appended to @path. The
270 * "over" term comes from forth, where the %OVER operator works
271 * on the stack in the same way as adg_path_over_primitive() works
272 * on @path. The returned struct is owned by @path and should not
273 * be freed or modified.
275 * Returns: (transfer none): a pointer to the primitive before the last appended one or %NULL on errors
279 const CpmlPrimitive
*
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 #CpmlPair pointers
297 * Generic method to append a primitive to @path. The number of #CpmlPair
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 #CpmlPair 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
, CpmlPair
*);
341 g_array_append_val(array
, pair
);
344 adg_path_append_array(path
, type
, (const CpmlPair
**) 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 #CpmlPair 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 CpmlPair
**pairs
)
371 const CpmlPair
**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 CpmlPrimitive 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 #CpmlPrimitive 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 CpmlPrimitive
*primitive
)
431 AdgPathPrivate
*data
;
432 CpmlPrimitive
*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
= cpml_primitive_deep_dup(primitive
);
447 _adg_append_primitive(path
, primitive_dup
);
449 g_free(primitive_dup
);
453 * adg_path_append_segment:
455 * @segment: the #CpmlSegment to append
457 * Appends @segment to @path.
462 adg_path_append_segment(AdgPath
*path
, const CpmlSegment
*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
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
473 segment
->data
, segment
->num_data
);
477 * adg_path_append_cairo_path:
479 * @cairo_path: (type gpointer): the #cairo_path_t path to append
481 * Appends a whole #cairo_path_t to @path.
486 adg_path_append_cairo_path(AdgPath
*path
, const cairo_path_t
*cairo_path
)
488 AdgPathPrivate
*data
;
490 g_return_if_fail(ADG_IS_PATH(path
));
491 g_return_if_fail(cairo_path
!= NULL
);
495 _adg_clear_parent((AdgModel
*) path
);
496 data
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
498 cairo_path
->num_data
);
504 * @pair: the destination coordinates
506 * Begins a new segment. After this call the current point will be @pair.
511 adg_path_move_to(AdgPath
*path
, const CpmlPair
*pair
)
513 adg_path_append(path
, CPML_MOVE
, pair
);
517 * adg_path_move_to_explicit:
519 * @x: the new x coordinate
520 * @y: the new y coordinate
522 * Convenient function to call adg_path_move_to() using explicit
523 * coordinates instead of #CpmlPair.
528 adg_path_move_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
535 adg_path_append(path
, CPML_MOVE
, &p
);
541 * @pair: the destination coordinates
543 * Adds a line to @path from the current point to @pair. After this
544 * call the current point will be @pair.
546 * If @path has no current point before this call, this function will
547 * trigger a warning without other effect.
552 adg_path_line_to(AdgPath
*path
, const CpmlPair
*pair
)
554 adg_path_append(path
, CPML_LINE
, pair
);
558 * adg_path_line_to_explicit:
560 * @x: the new x coordinate
561 * @y: the new y coordinate
563 * Convenient function to call adg_path_line_to() using explicit
564 * coordinates instead of #CpmlPair.
569 adg_path_line_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
576 adg_path_append(path
, CPML_LINE
, &p
);
582 * @throught: an arbitrary point on the arc
583 * @pair: the destination coordinates
585 * Adds an arc to the path from the current point to @pair, passing
586 * throught @throught. After this call the current point will be @pair.
588 * If @path has no current point before this call, this function will
589 * trigger a warning without other effect.
594 adg_path_arc_to(AdgPath
*path
, const CpmlPair
*throught
, const CpmlPair
*pair
)
596 adg_path_append(path
, CPML_ARC
, throught
, pair
);
600 * adg_path_arc_to_explicit:
602 * @x1: the x coordinate of an intermediate point
603 * @y1: the y coordinate of an intermediate point
604 * @x2: the x coordinate of the end of the arc
605 * @y2: the y coordinate of the end of the arc
607 * Convenient function to call adg_path_arc_to() using explicit
608 * coordinates instead of #CpmlPair.
613 adg_path_arc_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
614 gdouble x2
, gdouble y2
)
623 adg_path_append(path
, CPML_ARC
, &p
[0], &p
[1]);
629 * @control1: the first control point of the curve
630 * @control2: the second control point of the curve
631 * @pair: the destination coordinates
633 * Adds a cubic Bézier curve to the path from the current point to
634 * position @pair, using @control1 and @control2 as control points.
635 * After this call the current point will be @pair.
637 * If @path has no current point before this call, this function will
638 * trigger a warning without other effect.
643 adg_path_curve_to(AdgPath
*path
, const CpmlPair
*control1
,
644 const CpmlPair
*control2
, const CpmlPair
*pair
)
646 adg_path_append(path
, CPML_CURVE
, control1
, control2
, pair
);
650 * adg_path_curve_to_explicit:
652 * @x1: the x coordinate of the first control point
653 * @y1: the y coordinate of the first control point
654 * @x2: the x coordinate of the second control point
655 * @y2: the y coordinate of the second control point
656 * @x3: the x coordinate of the end of the curve
657 * @y3: the y coordinate of the end of the curve
659 * Convenient function to call adg_path_curve_to() using explicit
660 * coordinates instead of #CpmlPair.
665 adg_path_curve_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
666 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
677 adg_path_append(path
, CPML_CURVE
, &p
[0], &p
[1], &p
[2]);
684 * Adds a line segment to the path from the current point to the
685 * beginning of the current segment, (the most recent point passed
686 * to an adg_path_move_to()), and closes this segment.
687 * After this call the current point will be unset.
689 * The behavior of adg_path_close() is distinct from simply calling
690 * adg_line_to() with the coordinates of the segment starting point.
691 * When a closed segment is stroked, there are no caps on the ends.
692 * Instead, there is a line join connecting the final and initial
693 * primitive of the segment.
695 * If @path has no current point before this call, this function will
696 * trigger a warning without other effect.
701 adg_path_close(AdgPath
*path
)
703 adg_path_append(path
, CPML_CLOSE
);
709 * @center: coordinates of the center of the arc
710 * @r: the radius of the arc
711 * @start: the start angle, in radians
712 * @end: the end angle, in radians
714 * A more usual way to add an arc to @path. After this call, the current
715 * point will be the computed end point of the arc. The arc will be
716 * rendered in increasing angle, accordling to @start and @end. This means
717 * if @start is less than @end, the arc will be rendered in clockwise
718 * direction (accordling to the default cairo coordinate system) while if
719 * @start is greather than @end, the arc will be rendered in couterclockwise
722 * By explicitely setting the whole arc data, the start point could be
723 * different from the current point. In this case, if @path has no
724 * current point before the call a #CPML_MOVE to the start point of
725 * the arc will be automatically prepended to the arc. If @path has a
726 * current point, a #CPML_LINE to the start point of the arc will be
727 * used instead of the "move to" primitive.
732 adg_path_arc(AdgPath
*path
, const CpmlPair
*center
, gdouble r
,
733 gdouble start
, gdouble end
)
735 AdgPathPrivate
*data
;
738 g_return_if_fail(ADG_IS_PATH(path
));
739 g_return_if_fail(center
!= NULL
);
742 cpml_vector_from_angle(&p
[0], start
);
743 cpml_vector_from_angle(&p
[1], (end
-start
) / 2);
744 cpml_vector_from_angle(&p
[2], end
);
746 cpml_vector_set_length(&p
[0], r
);
747 cpml_vector_set_length(&p
[1], r
);
748 cpml_vector_set_length(&p
[2], r
);
757 if (!data
->cp_is_valid
)
758 adg_path_append(path
, CPML_MOVE
, &p
[0]);
759 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
760 adg_path_append(path
, CPML_LINE
, &p
[0]);
762 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
766 * adg_path_arc_explicit:
768 * @xc: x position of the center of the arc
769 * @yc: y position of the center of the arc
770 * @r: the radius of the arc
771 * @start: the start angle, in radians
772 * @end: the end angle, in radians
774 * Convenient function to call adg_path_arc() using explicit
775 * coordinates instead of #CpmlPair.
780 adg_path_arc_explicit(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
781 gdouble start
, gdouble end
)
788 adg_path_arc(path
, ¢er
, r
, start
, end
);
794 * @delta1: the distance from the intersection point of the current primitive
795 * @delta2: the distance from the intersection point of the next primitive
797 * A binary action that generates a chamfer between two primitives.
798 * The first primitive involved is the current primitive, the second will
799 * be the next primitive appended to @path after this call. The second
800 * primitive is required: if the chamfer operation is not properly
801 * terminated (by not providing the second primitive), any API accessing
802 * the path in reading mode will raise a warning.
804 * An exception is a chamfer after a #CPML_CLOSE primitive. In this case,
805 * the second primitive is not required: the current close path is used
806 * as first operand while the first primitive of the current segment is
807 * used as second operand.
809 * The chamfer operation requires two lengths: @delta1 specifies the
810 * "quantity" to trim on the first primitive while @delta2 is the same
811 * applied on the second primitive. The term "quantity" means the length
812 * of the portion to cut out from the original primitive (that is the
813 * primitive as would be without the chamfer).
818 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
820 g_return_if_fail(ADG_IS_PATH(path
));
822 if (!_adg_append_operation(path
, ADG_ACTION_CHAMFER
, delta1
, delta2
))
829 * @radius: the radius of the fillet
831 * A binary action that joins to primitives with an arc.
832 * The first primitive involved is the current primitive, the second will
833 * be the next primitive appended to @path after this call. The second
834 * primitive is required: if the fillet operation is not properly
835 * terminated (by not providing the second primitive), any API accessing
836 * the path in reading mode will raise a warning.
838 * An exception is a fillet after a #CPML_CLOSE primitive. In this case,
839 * the second primitive is not required: the current close path is used
840 * as first operand while the first primitive of the current segment is
841 * used as second operand.
846 adg_path_fillet(AdgPath
*path
, gdouble radius
)
848 g_return_if_fail(ADG_IS_PATH(path
));
850 if (!_adg_append_operation(path
, ADG_ACTION_FILLET
, radius
))
857 * @vector: the slope of the axis
859 * Reflects the first segment or @path around the axis passing
860 * throught (0, 0) and with a @vector slope. The internal segment
861 * is duplicated and the proper transformation (computed from
862 * @vector) to mirror the segment is applied on all its points.
863 * The result is then reversed with cpml_segment_reverse() and
864 * appended to the original path with adg_path_append_segment().
866 * For convenience, if @vector is %NULL the path is reversed
867 * around the x axis (y=0).
872 adg_path_reflect(AdgPath
*path
, const CpmlVector
*vector
)
875 cairo_matrix_t matrix
;
876 CpmlSegment segment
, *dup_segment
;
878 g_return_if_fail(ADG_IS_PATH(path
));
879 g_return_if_fail(vector
== NULL
|| vector
->x
!= 0 || vector
->y
!= 0);
881 model
= (AdgModel
*) path
;
883 if (vector
== NULL
) {
884 cairo_matrix_init_scale(&matrix
, 1, -1);
887 gdouble cos2angle
, sin2angle
;
889 cpml_pair_copy(&slope
, vector
);
890 cpml_vector_set_length(&slope
, 1);
892 if (slope
.x
== 0 && slope
.y
== 0) {
893 g_warning(_("%s: the axis of the reflection is not known"),
898 sin2angle
= 2. * vector
->x
* vector
->y
;
899 cos2angle
= 2. * vector
->x
* vector
->x
- 1;
901 cairo_matrix_init(&matrix
, cos2angle
, sin2angle
,
902 sin2angle
, -cos2angle
, 0, 0);
905 if (!adg_trail_put_segment((AdgTrail
*) path
, 1, &segment
))
908 /* No need to reverse an empty segment */
909 if (segment
.num_data
== 0 || segment
.num_data
== 0)
912 dup_segment
= cpml_segment_deep_dup(&segment
);
913 if (dup_segment
== NULL
)
916 cpml_segment_reverse(dup_segment
);
917 cpml_segment_transform(dup_segment
, &matrix
);
918 dup_segment
->data
[0].header
.type
= CPML_LINE
;
920 adg_path_append_segment(path
, dup_segment
);
924 _adg_dup_reverse_named_pairs(model
, &matrix
);
928 * adg_path_reflect_explicit:
930 * @x: the vector x component
931 * @y: the vector y component
933 * Convenient function to call adg_path_reflect() using explicit
934 * vector components instead of #CpmlVector.
939 adg_path_reflect_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
946 adg_path_reflect(path
, &vector
);
951 _adg_clear(AdgModel
*model
)
954 AdgPathPrivate
*data
;
956 path
= (AdgPath
*) model
;
959 g_array_set_size(data
->cairo
.array
, 0);
960 _adg_clear_operation(path
);
961 _adg_clear_parent(model
);
965 _adg_clear_parent(AdgModel
*model
)
967 if (_ADG_OLD_MODEL_CLASS
->clear
)
968 _ADG_OLD_MODEL_CLASS
->clear(model
);
972 _adg_changed(AdgModel
*model
)
974 _adg_clear_parent(model
);
976 if (_ADG_OLD_MODEL_CLASS
->changed
)
977 _ADG_OLD_MODEL_CLASS
->changed(model
);
980 static cairo_path_t
*
981 _adg_get_cairo_path(AdgTrail
*trail
)
983 _adg_clear_parent((AdgModel
*) trail
);
984 return _adg_read_cairo_path((AdgPath
*) trail
);
987 static cairo_path_t
*
988 _adg_read_cairo_path(AdgPath
*path
)
990 AdgPathPrivate
*data
= path
->data
;
991 cairo_path_t
*cairo_path
= &data
->cairo
.path
;
992 GArray
*array
= data
->cairo
.array
;
994 /* Always regenerate the cairo_path_t as it is a trivial operation */
995 cairo_path
->status
= CAIRO_STATUS_SUCCESS
;
996 cairo_path
->data
= (cairo_path_data_t
*) array
->data
;
997 cairo_path
->num_data
= array
->len
;
1003 _adg_primitive_length(CpmlPrimitiveType type
)
1005 if (type
== CPML_CLOSE
)
1007 else if (type
== CPML_MOVE
)
1010 return cpml_primitive_type_get_n_points(type
);
1014 _adg_append_primitive(AdgPath
*path
, CpmlPrimitive
*current
)
1016 AdgPathPrivate
*data
;
1017 cairo_path_data_t
*path_data
;
1021 path_data
= current
->data
;
1022 length
= path_data
[0].header
.length
;
1024 /* Execute any pending operation */
1025 _adg_do_operation(path
, path_data
);
1027 /* Append the path data to the internal path array */
1028 data
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
1031 /* Set path data to point to the recently appended cairo_path_data_t
1032 * primitive: the first struct is the header */
1033 path_data
= (cairo_path_data_t
*) (data
->cairo
.array
)->data
+
1034 (data
->cairo
.array
)->len
- length
;
1036 /* Store the over primitive */
1037 memcpy(&data
->over
, &data
->last
, sizeof(CpmlPrimitive
));
1039 /* Set the last primitive for subsequent binary operations */
1040 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
1041 data
->last
.segment
= NULL
;
1042 data
->last
.data
= path_data
;
1044 /* Save the last point as the current point, if applicable */
1045 data
->cp_is_valid
= length
> 1;
1047 cpml_pair_from_cairo(&data
->cp
, &path_data
[length
-1]);
1049 /* Invalidate cairo_path: should be recomputed */
1050 _adg_clear_parent((AdgModel
*) path
);
1054 _adg_clear_operation(AdgPath
*path
)
1056 AdgPathPrivate
*data
;
1057 AdgOperation
*operation
;
1060 operation
= &data
->operation
;
1062 if (operation
->action
!= ADG_ACTION_NONE
) {
1063 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
1064 G_STRLOC
, _adg_action_name(operation
->action
));
1065 operation
->action
= ADG_ACTION_NONE
;
1068 data
->cp_is_valid
= FALSE
;
1069 data
->last
.data
= NULL
;
1070 data
->over
.data
= NULL
;
1074 _adg_append_operation(AdgPath
*path
, AdgAction action
, ...)
1076 AdgPathPrivate
*data
;
1077 AdgOperation
*operation
;
1082 if (data
->last
.data
== NULL
) {
1083 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
1084 G_STRLOC
, _adg_action_name(action
));
1088 operation
= &data
->operation
;
1089 if (operation
->action
!= ADG_ACTION_NONE
) {
1090 g_warning(_("%s: requested a `%s' operation while a `%s' operation was active"),
1091 G_STRLOC
, _adg_action_name(action
),
1092 _adg_action_name(operation
->action
));
1093 /* XXX: http://dev.entidi.com/p/adg/issues/50/ */
1097 va_start(var_args
, action
);
1101 case ADG_ACTION_CHAMFER
:
1102 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
1103 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
1106 case ADG_ACTION_FILLET
:
1107 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
1110 case ADG_ACTION_NONE
:
1115 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC
, action
);
1120 operation
->action
= action
;
1123 if (data
->last
.data
[0].header
.type
== CAIRO_PATH_CLOSE_PATH
) {
1124 /* Special case: an action with the close primitive should
1125 * be resolved now by changing the close primitive to a
1126 * line-to and using it as second operand and use the first
1127 * primitive of the current segment as first operand */
1129 cairo_path_data_t
*path_data
;
1130 CpmlSegment segment
;
1131 CpmlPrimitive current
;
1133 length
= data
->cairo
.array
->len
;
1135 /* Ensure the close path primitive is not the only data */
1136 g_return_val_if_fail(length
> 1, FALSE
);
1138 /* Allocate one more item once for all to accept the
1139 * conversion from a close to line-to primitive */
1140 data
->cairo
.array
= g_array_set_size(data
->cairo
.array
, length
+ 1);
1141 path_data
= (cairo_path_data_t
*) data
->cairo
.array
->data
;
1142 --data
->cairo
.array
->len
;
1144 /* Set segment and current (the first primitive of segment) */
1145 cpml_segment_from_cairo(&segment
, _adg_read_cairo_path(path
));
1146 while (cpml_segment_next(&segment
))
1148 cpml_primitive_from_segment(¤t
, &segment
);
1150 /* Convert close path to a line-to primitive */
1151 ++data
->cairo
.array
->len
;
1152 path_data
[length
- 1].header
.type
= CPML_LINE
;
1153 path_data
[length
- 1].header
.length
= 2;
1154 path_data
[length
] = *current
.org
;
1156 data
->last
.segment
= &segment
;
1157 data
->last
.org
= &path_data
[length
- 2];
1158 data
->last
.data
= &path_data
[length
- 1];
1160 _adg_do_action(path
, action
, ¤t
);
1167 _adg_do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
1169 AdgPathPrivate
*data
;
1171 CpmlSegment segment
;
1172 CpmlPrimitive current
;
1173 cairo_path_data_t current_org
;
1176 action
= data
->operation
.action
;
1177 cpml_segment_from_cairo(&segment
, _adg_read_cairo_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
, CpmlPrimitive
*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
, CpmlPrimitive
*current
)
1213 AdgPathPrivate
*data
;
1214 CpmlPrimitive
*last
;
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
, CpmlPrimitive
*current
)
1255 AdgPathPrivate
*data
;
1256 CpmlPrimitive
*last
, *current_dup
, *last_dup
;
1257 gdouble radius
, offset
, pos
;
1258 CpmlPair center
, vector
, p
[3];
1262 current_dup
= cpml_primitive_deep_dup(current
);
1263 last_dup
= cpml_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 CpmlPrimitive
*primitive1
, const CpmlPrimitive
*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 CpmlPair
*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 cairo_matrix_t
*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
);