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
;
148 data
->cairo
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
149 data
->cairo
.path
.data
= NULL
;
150 data
->cairo
.path
.num_data
= 0;
151 data
->cairo
.array
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
152 data
->last
.segment
= NULL
;
153 data
->last
.org
= NULL
;
154 data
->last
.data
= NULL
;
155 data
->over
.segment
= NULL
;
156 data
->over
.org
= NULL
;
157 data
->over
.data
= NULL
;
158 data
->operation
.action
= ADG_ACTION_NONE
;
164 _adg_finalize(GObject
*object
)
167 AdgPathPrivate
*data
;
169 path
= (AdgPath
*) object
;
172 g_array_free(data
->cairo
.array
, TRUE
);
173 _adg_clear_operation(path
);
175 if (_ADG_OLD_OBJECT_CLASS
->finalize
)
176 _ADG_OLD_OBJECT_CLASS
->finalize(object
);
183 * Creates a new path model. The path should be constructed
184 * programmatically by using the methods provided by #AdgPath.
186 * Returns: the newly created path model
193 return g_object_new(ADG_TYPE_PATH
, NULL
);
197 * adg_path_get_current_point:
200 * Gets the current point of @path, which is conceptually the
201 * final point reached by the path so far.
203 * If there is no defined current point, %NULL is returned.
204 * It is possible to check this in advance with
205 * adg_path_has_current_point().
207 * Most #AdgPath methods alter the current point and most of them
208 * expect a current point to be defined otherwise will fail triggering
209 * a warning. Check the description of every method for specific details.
211 * Returns: (transfer none): the current point or %NULL on no current point set or errors
216 adg_path_get_current_point(AdgPath
*path
)
218 AdgPathPrivate
*data
;
220 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
224 if (!data
->cp_is_valid
)
231 * adg_path_has_current_point:
234 * Returns whether a current point is defined on @path.
235 * See adg_path_get_current_point() for details on the current point.
237 * Returns: whether a current point is defined
242 adg_path_has_current_point(AdgPath
*path
)
244 AdgPathPrivate
*data
;
246 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
250 return data
->cp_is_valid
;
254 * adg_path_last_primitive:
257 * Gets the last primitive appended to @path. The returned struct
258 * is owned by @path and should not be freed or modified.
260 * Returns: (transfer none): a pointer to the last appended primitive or %NULL on errors
264 const CpmlPrimitive
*
265 adg_path_last_primitive(AdgPath
*path
)
267 AdgPathPrivate
*data
;
269 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
277 * adg_path_over_primitive:
280 * Gets the primitive before the last one appended to @path. The
281 * "over" term comes from forth, where the %OVER operator works
282 * on the stack in the same way as adg_path_over_primitive() works
283 * on @path. The returned struct is owned by @path and should not
284 * be freed or modified.
286 * Returns: (transfer none): a pointer to the primitive before the last appended one or %NULL on errors
290 const CpmlPrimitive
*
291 adg_path_over_primitive(AdgPath
*path
)
293 AdgPathPrivate
*data
;
295 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
305 * @type: a #cairo_data_type_t value
306 * @...: point data, specified as #CpmlPair pointers
308 * Generic method to append a primitive to @path. The number of #CpmlPair
309 * pointers to pass as @Varargs depends on @type: #CPML_CLOSE does not
310 * require any pair, #CPML_MOVE and #CPML_LINE require one pair,
311 * #CPML_ARC two pairs, #CPML_CURVE three pairs and so on.
313 * All the needed pairs must be not %NULL pointers, otherwise the function
314 * will fail. The pairs in excess, if any, will be ignored.
319 adg_path_append(AdgPath
*path
, CpmlPrimitiveType type
, ...)
323 va_start(var_args
, type
);
324 adg_path_append_valist(path
, type
, var_args
);
329 * adg_path_append_valist:
331 * @type: a #cairo_data_type_t value
332 * @var_args: point data, specified as #CpmlPair pointers
334 * va_list version of adg_path_append().
339 adg_path_append_valist(AdgPath
*path
, CpmlPrimitiveType type
, va_list var_args
)
345 length
= _adg_primitive_length(type
);
349 array
= g_array_new(TRUE
, FALSE
, sizeof(pair
));
351 pair
= va_arg(var_args
, CpmlPair
*);
352 g_array_append_val(array
, pair
);
355 adg_path_append_array(path
, type
, (const CpmlPair
**) array
->data
);
356 g_array_free(array
, TRUE
);
360 * adg_path_append_array:
362 * @type: a #cairo_data_type_t value
363 * @pairs: (array zero-terminated=1) (element-type Cpml.Pair) (transfer none): point data, specified as a %NULL terminated array of #CpmlPair pointers
365 * A bindingable version of adg_path_append() that uses a %NULL terminated
366 * array of pairs instead of variable argument list and friends.
368 * Furthermore, because of the list is %NULL terminated, an arbitrary
369 * number of pairs can be passed in @pairs. This allows to embed in a
370 * primitive element more data pairs than requested, something impossible
371 * to do with adg_path_append() and adg_path_append_valist().
373 * Rename to: adg_path_append
377 adg_path_append_array(AdgPath
*path
, CpmlPrimitiveType type
,
378 const CpmlPair
**pairs
)
382 const CpmlPair
**pair
;
383 cairo_path_data_t path_data
;
385 g_return_if_fail(ADG_IS_PATH(path
));
386 g_return_if_fail(pairs
!= NULL
);
388 length
= _adg_primitive_length(type
);
392 array
= g_array_new(FALSE
, FALSE
, sizeof(path_data
));
393 for (pair
= pairs
; *pair
!= NULL
; ++ pair
) {
394 cpml_pair_to_cairo(*pair
, &path_data
);
395 g_array_append_val(array
, path_data
);
398 if (array
->len
< length
- 1) {
399 /* Not enough pairs have been provided */
400 g_warning(_("%s: null pair caught while parsing arguments"), G_STRLOC
);
402 AdgPathPrivate
*data
;
403 CpmlPrimitive primitive
;
404 cairo_path_data_t org
;
406 /* Save a copy of the current point as primitive origin */
408 cpml_pair_to_cairo(&data
->cp
, &org
);
410 /* Prepend the cairo header */
411 path_data
.header
.type
= type
;
412 path_data
.header
.length
= array
->len
+ 1;
413 g_array_prepend_val(array
, path_data
);
415 /* Append a new primitive to @path */
416 primitive
.segment
= NULL
;
417 primitive
.org
= &org
;
418 primitive
.data
= (cairo_path_data_t
*) array
->data
;
419 _adg_append_primitive(path
, &primitive
);
422 g_array_free(array
, TRUE
);
427 * adg_path_append_primitive:
429 * @primitive: the #CpmlPrimitive to append
431 * Appends @primitive to @path. The primitive to add is considered the
432 * continuation of the current path so the <structfield>org</structfield>
433 * component of @primitive is not used. Anyway the current point is
434 * checked against it: they must be equal or the function will fail
435 * without further processing.
440 adg_path_append_primitive(AdgPath
*path
, const CpmlPrimitive
*primitive
)
442 AdgPathPrivate
*data
;
443 CpmlPrimitive
*primitive_dup
;
445 g_return_if_fail(ADG_IS_PATH(path
));
446 g_return_if_fail(primitive
!= NULL
);
447 g_return_if_fail(primitive
->org
!= NULL
);
451 g_return_if_fail(primitive
->org
->point
.x
== data
->cp
.x
&&
452 primitive
->org
->point
.y
== data
->cp
.y
);
454 /* The primitive data could be modified by pending operations:
456 primitive_dup
= cpml_primitive_deep_dup(primitive
);
458 _adg_append_primitive(path
, primitive_dup
);
460 g_free(primitive_dup
);
464 * adg_path_append_segment:
466 * @segment: the #CpmlSegment to append
468 * Appends @segment to @path.
473 adg_path_append_segment(AdgPath
*path
, const CpmlSegment
*segment
)
475 AdgPathPrivate
*data
;
477 g_return_if_fail(ADG_IS_PATH(path
));
478 g_return_if_fail(segment
!= NULL
);
482 _adg_clear_parent((AdgModel
*) path
);
483 data
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
484 segment
->data
, segment
->num_data
);
488 * adg_path_append_cairo_path:
490 * @cairo_path: (type gpointer): the #cairo_path_t path to append
492 * Appends a whole #cairo_path_t to @path.
497 adg_path_append_cairo_path(AdgPath
*path
, const cairo_path_t
*cairo_path
)
499 AdgPathPrivate
*data
;
501 g_return_if_fail(ADG_IS_PATH(path
));
502 g_return_if_fail(cairo_path
!= NULL
);
506 _adg_clear_parent((AdgModel
*) path
);
507 data
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
509 cairo_path
->num_data
);
515 * @pair: the destination coordinates
517 * Begins a new segment. After this call the current point will be @pair.
522 adg_path_move_to(AdgPath
*path
, const CpmlPair
*pair
)
524 adg_path_append(path
, CPML_MOVE
, pair
);
528 * adg_path_move_to_explicit:
530 * @x: the new x coordinate
531 * @y: the new y coordinate
533 * Convenient function to call adg_path_move_to() using explicit
534 * coordinates instead of #CpmlPair.
539 adg_path_move_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
546 adg_path_append(path
, CPML_MOVE
, &p
);
552 * @pair: the destination coordinates
554 * Adds a line to @path from the current point to @pair. After this
555 * call the current point will be @pair.
557 * If @path has no current point before this call, this function will
558 * trigger a warning without other effect.
563 adg_path_line_to(AdgPath
*path
, const CpmlPair
*pair
)
565 adg_path_append(path
, CPML_LINE
, pair
);
569 * adg_path_line_to_explicit:
571 * @x: the new x coordinate
572 * @y: the new y coordinate
574 * Convenient function to call adg_path_line_to() using explicit
575 * coordinates instead of #CpmlPair.
580 adg_path_line_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
587 adg_path_append(path
, CPML_LINE
, &p
);
593 * @throught: an arbitrary point on the arc
594 * @pair: the destination coordinates
596 * Adds an arc to the path from the current point to @pair, passing
597 * throught @throught. After this call the current point will be @pair.
599 * If @path has no current point before this call, this function will
600 * trigger a warning without other effect.
605 adg_path_arc_to(AdgPath
*path
, const CpmlPair
*throught
, const CpmlPair
*pair
)
607 adg_path_append(path
, CPML_ARC
, throught
, pair
);
611 * adg_path_arc_to_explicit:
613 * @x1: the x coordinate of an intermediate point
614 * @y1: the y coordinate of an intermediate point
615 * @x2: the x coordinate of the end of the arc
616 * @y2: the y coordinate of the end of the arc
618 * Convenient function to call adg_path_arc_to() using explicit
619 * coordinates instead of #CpmlPair.
624 adg_path_arc_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
625 gdouble x2
, gdouble y2
)
634 adg_path_append(path
, CPML_ARC
, &p
[0], &p
[1]);
640 * @control1: the first control point of the curve
641 * @control2: the second control point of the curve
642 * @pair: the destination coordinates
644 * Adds a cubic Bézier curve to the path from the current point to
645 * position @pair, using @control1 and @control2 as control points.
646 * After this call the current point will be @pair.
648 * If @path has no current point before this call, this function will
649 * trigger a warning without other effect.
654 adg_path_curve_to(AdgPath
*path
, const CpmlPair
*control1
,
655 const CpmlPair
*control2
, const CpmlPair
*pair
)
657 adg_path_append(path
, CPML_CURVE
, control1
, control2
, pair
);
661 * adg_path_curve_to_explicit:
663 * @x1: the x coordinate of the first control point
664 * @y1: the y coordinate of the first control point
665 * @x2: the x coordinate of the second control point
666 * @y2: the y coordinate of the second control point
667 * @x3: the x coordinate of the end of the curve
668 * @y3: the y coordinate of the end of the curve
670 * Convenient function to call adg_path_curve_to() using explicit
671 * coordinates instead of #CpmlPair.
676 adg_path_curve_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
677 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
688 adg_path_append(path
, CPML_CURVE
, &p
[0], &p
[1], &p
[2]);
695 * Adds a line segment to the path from the current point to the
696 * beginning of the current segment, (the most recent point passed
697 * to an adg_path_move_to()), and closes this segment.
698 * After this call the current point will be unset.
700 * The behavior of adg_path_close() is distinct from simply calling
701 * adg_line_to() with the coordinates of the segment starting point.
702 * When a closed segment is stroked, there are no caps on the ends.
703 * Instead, there is a line join connecting the final and initial
704 * primitive of the segment.
706 * If @path has no current point before this call, this function will
707 * trigger a warning without other effect.
712 adg_path_close(AdgPath
*path
)
714 adg_path_append(path
, CPML_CLOSE
);
720 * @center: coordinates of the center of the arc
721 * @r: the radius of the arc
722 * @start: the start angle, in radians
723 * @end: the end angle, in radians
725 * A more usual way to add an arc to @path. After this call, the current
726 * point will be the computed end point of the arc. The arc will be
727 * rendered in increasing angle, accordling to @start and @end. This means
728 * if @start is less than @end, the arc will be rendered in clockwise
729 * direction (accordling to the default cairo coordinate system) while if
730 * @start is greather than @end, the arc will be rendered in couterclockwise
733 * By explicitely setting the whole arc data, the start point could be
734 * different from the current point. In this case, if @path has no
735 * current point before the call a #CPML_MOVE to the start point of
736 * the arc will be automatically prepended to the arc. If @path has a
737 * current point, a #CPML_LINE to the start point of the arc will be
738 * used instead of the "move to" primitive.
743 adg_path_arc(AdgPath
*path
, const CpmlPair
*center
, gdouble r
,
744 gdouble start
, gdouble end
)
746 AdgPathPrivate
*data
;
749 g_return_if_fail(ADG_IS_PATH(path
));
750 g_return_if_fail(center
!= NULL
);
753 cpml_vector_from_angle(&p
[0], start
);
754 cpml_vector_from_angle(&p
[1], (end
-start
) / 2);
755 cpml_vector_from_angle(&p
[2], end
);
757 cpml_vector_set_length(&p
[0], r
);
758 cpml_vector_set_length(&p
[1], r
);
759 cpml_vector_set_length(&p
[2], r
);
768 if (!data
->cp_is_valid
)
769 adg_path_append(path
, CPML_MOVE
, &p
[0]);
770 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
771 adg_path_append(path
, CPML_LINE
, &p
[0]);
773 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
777 * adg_path_arc_explicit:
779 * @xc: x position of the center of the arc
780 * @yc: y position of the center of the arc
781 * @r: the radius of the arc
782 * @start: the start angle, in radians
783 * @end: the end angle, in radians
785 * Convenient function to call adg_path_arc() using explicit
786 * coordinates instead of #CpmlPair.
791 adg_path_arc_explicit(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
792 gdouble start
, gdouble end
)
799 adg_path_arc(path
, ¢er
, r
, start
, end
);
805 * @delta1: the distance from the intersection point of the current primitive
806 * @delta2: the distance from the intersection point of the next primitive
808 * A binary action that generates a chamfer between two primitives.
809 * The first primitive involved is the current primitive, the second will
810 * be the next primitive appended to @path after this call. The second
811 * primitive is required: if the chamfer operation is not properly
812 * terminated (by not providing the second primitive), any API accessing
813 * the path in reading mode will raise a warning.
815 * An exception is a chamfer after a #CPML_CLOSE primitive. In this case,
816 * the second primitive is not required: the current close path is used
817 * as first operand while the first primitive of the current segment is
818 * used as second operand.
820 * The chamfer operation requires two lengths: @delta1 specifies the
821 * "quantity" to trim on the first primitive while @delta2 is the same
822 * applied on the second primitive. The term "quantity" means the length
823 * of the portion to cut out from the original primitive (that is the
824 * primitive as would be without the chamfer).
829 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
831 g_return_if_fail(ADG_IS_PATH(path
));
833 if (!_adg_append_operation(path
, ADG_ACTION_CHAMFER
, delta1
, delta2
))
840 * @radius: the radius of the fillet
842 * A binary action that joins to primitives with an arc.
843 * The first primitive involved is the current primitive, the second will
844 * be the next primitive appended to @path after this call. The second
845 * primitive is required: if the fillet operation is not properly
846 * terminated (by not providing the second primitive), any API accessing
847 * the path in reading mode will raise a warning.
849 * An exception is a fillet after a #CPML_CLOSE primitive. In this case,
850 * the second primitive is not required: the current close path is used
851 * as first operand while the first primitive of the current segment is
852 * used as second operand.
857 adg_path_fillet(AdgPath
*path
, gdouble radius
)
859 g_return_if_fail(ADG_IS_PATH(path
));
861 if (!_adg_append_operation(path
, ADG_ACTION_FILLET
, radius
))
868 * @vector: the slope of the axis
870 * Reflects the first segment or @path around the axis passing
871 * throught (0, 0) and with a @vector slope. The internal segment
872 * is duplicated and the proper transformation (computed from
873 * @vector) to mirror the segment is applied on all its points.
874 * The result is then reversed with cpml_segment_reverse() and
875 * appended to the original path with adg_path_append_segment().
877 * For convenience, if @vector is %NULL the path is reversed
878 * around the x axis (y=0).
883 adg_path_reflect(AdgPath
*path
, const CpmlVector
*vector
)
886 cairo_matrix_t matrix
;
887 CpmlSegment segment
, *dup_segment
;
889 g_return_if_fail(ADG_IS_PATH(path
));
890 g_return_if_fail(vector
== NULL
|| vector
->x
!= 0 || vector
->y
!= 0);
892 model
= (AdgModel
*) path
;
894 if (vector
== NULL
) {
895 cairo_matrix_init_scale(&matrix
, 1, -1);
898 gdouble cos2angle
, sin2angle
;
900 cpml_pair_copy(&slope
, vector
);
901 cpml_vector_set_length(&slope
, 1);
903 if (slope
.x
== 0 && slope
.y
== 0) {
904 g_warning(_("%s: the axis of the reflection is not known"),
909 sin2angle
= 2. * vector
->x
* vector
->y
;
910 cos2angle
= 2. * vector
->x
* vector
->x
- 1;
912 cairo_matrix_init(&matrix
, cos2angle
, sin2angle
,
913 sin2angle
, -cos2angle
, 0, 0);
916 if (!adg_trail_put_segment((AdgTrail
*) path
, 1, &segment
))
919 /* No need to reverse an empty segment */
920 if (segment
.num_data
== 0 || segment
.num_data
== 0)
923 dup_segment
= cpml_segment_deep_dup(&segment
);
924 if (dup_segment
== NULL
)
927 cpml_segment_reverse(dup_segment
);
928 cpml_segment_transform(dup_segment
, &matrix
);
929 dup_segment
->data
[0].header
.type
= CPML_LINE
;
931 adg_path_append_segment(path
, dup_segment
);
935 _adg_dup_reverse_named_pairs(model
, &matrix
);
939 * adg_path_reflect_explicit:
941 * @x: the vector x component
942 * @y: the vector y component
944 * Convenient function to call adg_path_reflect() using explicit
945 * vector components instead of #CpmlVector.
950 adg_path_reflect_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
957 adg_path_reflect(path
, &vector
);
962 _adg_clear(AdgModel
*model
)
965 AdgPathPrivate
*data
;
967 path
= (AdgPath
*) model
;
970 g_array_set_size(data
->cairo
.array
, 0);
971 _adg_clear_operation(path
);
972 _adg_clear_parent(model
);
976 _adg_clear_parent(AdgModel
*model
)
978 if (_ADG_OLD_MODEL_CLASS
->clear
)
979 _ADG_OLD_MODEL_CLASS
->clear(model
);
983 _adg_changed(AdgModel
*model
)
985 _adg_clear_parent(model
);
987 if (_ADG_OLD_MODEL_CLASS
->changed
)
988 _ADG_OLD_MODEL_CLASS
->changed(model
);
991 static cairo_path_t
*
992 _adg_get_cairo_path(AdgTrail
*trail
)
994 _adg_clear_parent((AdgModel
*) trail
);
995 return _adg_read_cairo_path((AdgPath
*) trail
);
998 static cairo_path_t
*
999 _adg_read_cairo_path(AdgPath
*path
)
1001 AdgPathPrivate
*data
= path
->data
;
1002 cairo_path_t
*cairo_path
= &data
->cairo
.path
;
1003 GArray
*array
= data
->cairo
.array
;
1005 /* Always regenerate the cairo_path_t as it is a trivial operation */
1006 cairo_path
->status
= CAIRO_STATUS_SUCCESS
;
1007 cairo_path
->data
= (cairo_path_data_t
*) array
->data
;
1008 cairo_path
->num_data
= array
->len
;
1014 _adg_primitive_length(CpmlPrimitiveType type
)
1016 if (type
== CPML_CLOSE
)
1018 else if (type
== CPML_MOVE
)
1021 return cpml_primitive_type_get_n_points(type
);
1025 _adg_append_primitive(AdgPath
*path
, CpmlPrimitive
*current
)
1027 AdgPathPrivate
*data
;
1028 cairo_path_data_t
*path_data
;
1032 path_data
= current
->data
;
1033 length
= path_data
[0].header
.length
;
1035 /* Execute any pending operation */
1036 _adg_do_operation(path
, path_data
);
1038 /* Append the path data to the internal path array */
1039 data
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
1042 /* Set path data to point to the recently appended cairo_path_data_t
1043 * primitive: the first struct is the header */
1044 path_data
= (cairo_path_data_t
*) (data
->cairo
.array
)->data
+
1045 (data
->cairo
.array
)->len
- length
;
1047 /* Store the over primitive */
1048 memcpy(&data
->over
, &data
->last
, sizeof(CpmlPrimitive
));
1050 /* Set the last primitive for subsequent binary operations */
1051 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
1052 data
->last
.segment
= NULL
;
1053 data
->last
.data
= path_data
;
1055 /* Save the last point as the current point, if applicable */
1056 data
->cp_is_valid
= length
> 1;
1058 cpml_pair_from_cairo(&data
->cp
, &path_data
[length
-1]);
1060 /* Invalidate cairo_path: should be recomputed */
1061 _adg_clear_parent((AdgModel
*) path
);
1065 _adg_clear_operation(AdgPath
*path
)
1067 AdgPathPrivate
*data
;
1068 AdgOperation
*operation
;
1071 operation
= &data
->operation
;
1073 if (operation
->action
!= ADG_ACTION_NONE
) {
1074 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
1075 G_STRLOC
, _adg_action_name(operation
->action
));
1076 operation
->action
= ADG_ACTION_NONE
;
1079 data
->cp_is_valid
= FALSE
;
1080 data
->last
.data
= NULL
;
1081 data
->over
.data
= NULL
;
1085 _adg_append_operation(AdgPath
*path
, AdgAction action
, ...)
1087 AdgPathPrivate
*data
;
1088 AdgOperation
*operation
;
1093 if (data
->last
.data
== NULL
) {
1094 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
1095 G_STRLOC
, _adg_action_name(action
));
1099 operation
= &data
->operation
;
1100 if (operation
->action
!= ADG_ACTION_NONE
) {
1101 g_warning(_("%s: requested a `%s' operation while a `%s' operation was active"),
1102 G_STRLOC
, _adg_action_name(action
),
1103 _adg_action_name(operation
->action
));
1104 /* XXX: http://dev.entidi.com/p/adg/issues/50/ */
1108 va_start(var_args
, action
);
1112 case ADG_ACTION_CHAMFER
:
1113 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
1114 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
1117 case ADG_ACTION_FILLET
:
1118 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
1121 case ADG_ACTION_NONE
:
1126 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC
, action
);
1131 operation
->action
= action
;
1134 if (data
->last
.data
[0].header
.type
== CAIRO_PATH_CLOSE_PATH
) {
1135 /* Special case: an action with the close primitive should
1136 * be resolved now by changing the close primitive to a
1137 * line-to and using it as second operand and use the first
1138 * primitive of the current segment as first operand */
1140 cairo_path_data_t
*path_data
;
1141 CpmlSegment segment
;
1142 CpmlPrimitive current
;
1144 length
= data
->cairo
.array
->len
;
1146 /* Ensure the close path primitive is not the only data */
1147 g_return_val_if_fail(length
> 1, FALSE
);
1149 /* Allocate one more item once for all to accept the
1150 * conversion from a close to line-to primitive */
1151 data
->cairo
.array
= g_array_set_size(data
->cairo
.array
, length
+ 1);
1152 path_data
= (cairo_path_data_t
*) data
->cairo
.array
->data
;
1153 --data
->cairo
.array
->len
;
1155 /* Set segment and current (the first primitive of segment) */
1156 cpml_segment_from_cairo(&segment
, _adg_read_cairo_path(path
));
1157 while (cpml_segment_next(&segment
))
1159 cpml_primitive_from_segment(¤t
, &segment
);
1161 /* Convert close path to a line-to primitive */
1162 ++data
->cairo
.array
->len
;
1163 path_data
[length
- 1].header
.type
= CPML_LINE
;
1164 path_data
[length
- 1].header
.length
= 2;
1165 path_data
[length
] = *current
.org
;
1167 data
->last
.segment
= &segment
;
1168 data
->last
.org
= &path_data
[length
- 2];
1169 data
->last
.data
= &path_data
[length
- 1];
1171 _adg_do_action(path
, action
, ¤t
);
1178 _adg_do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
1180 AdgPathPrivate
*data
;
1182 CpmlSegment segment
;
1183 CpmlPrimitive current
;
1184 cairo_path_data_t current_org
;
1187 action
= data
->operation
.action
;
1188 cpml_segment_from_cairo(&segment
, _adg_read_cairo_path(path
));
1190 /* Construct the current primitive, that is the primitive to be
1191 * mixed with the last primitive with the specified operation.
1192 * Its org is a copy of the end point of the last primitive: it can be
1193 * modified without affecting anything else. It is expected the operation
1194 * functions will add to @path the primitives required but NOT to add
1195 * @current, as this one will be inserted automatically. */
1196 current
.segment
= &segment
;
1197 current
.org
= ¤t_org
;
1198 current
.data
= path_data
;
1199 cpml_pair_to_cairo(&data
->cp
, ¤t_org
);
1201 _adg_do_action(path
, action
, ¤t
);
1205 _adg_do_action(AdgPath
*path
, AdgAction action
, CpmlPrimitive
*primitive
)
1208 case ADG_ACTION_NONE
:
1210 case ADG_ACTION_CHAMFER
:
1211 _adg_do_chamfer(path
, primitive
);
1213 case ADG_ACTION_FILLET
:
1214 _adg_do_fillet(path
, primitive
);
1217 g_return_if_reached();
1222 _adg_do_chamfer(AdgPath
*path
, CpmlPrimitive
*current
)
1224 AdgPathPrivate
*data
;
1225 CpmlPrimitive
*last
;
1226 gdouble delta1
, delta2
;
1232 delta1
= data
->operation
.data
.chamfer
.delta1
;
1233 len1
= cpml_primitive_get_length(last
);
1235 if (delta1
>= len1
) {
1236 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1237 G_STRLOC
, delta1
, len1
);
1241 delta2
= data
->operation
.data
.chamfer
.delta2
;
1242 len2
= cpml_primitive_get_length(current
);
1244 if (delta2
>= len2
) {
1245 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1246 G_STRLOC
, delta1
, len1
);
1250 /* Change the end point of the last primitive */
1251 cpml_primitive_put_pair_at(last
, 1. - delta1
/ len1
, &pair
);
1252 cpml_primitive_set_point(last
, -1, &pair
);
1254 /* Change the start point of the current primitive */
1255 cpml_primitive_put_pair_at(current
, delta2
/ len2
, &pair
);
1256 cpml_primitive_set_point(current
, 0, &pair
);
1258 /* Add the chamfer line */
1259 data
->operation
.action
= ADG_ACTION_NONE
;
1260 adg_path_append(path
, CPML_LINE
, &pair
);
1264 _adg_do_fillet(AdgPath
*path
, CpmlPrimitive
*current
)
1266 AdgPathPrivate
*data
;
1267 CpmlPrimitive
*last
, *current_dup
, *last_dup
;
1268 gdouble radius
, offset
, pos
;
1269 CpmlPair center
, vector
, p
[3];
1273 current_dup
= cpml_primitive_deep_dup(current
);
1274 last_dup
= cpml_primitive_deep_dup(last
);
1275 radius
= data
->operation
.data
.fillet
.radius
;
1276 offset
= _adg_is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1278 /* Find the center of the fillet from the intersection between
1279 * the last and current primitives offseted by radius */
1280 cpml_primitive_offset(current_dup
, offset
);
1281 cpml_primitive_offset(last_dup
, offset
);
1282 if (cpml_primitive_put_intersections(current_dup
, last_dup
, 1, ¢er
) == 0) {
1283 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1285 g_free(current_dup
);
1290 /* Compute the start point of the fillet */
1291 pos
= cpml_primitive_get_closest_pos(last_dup
, ¢er
);
1292 cpml_primitive_put_vector_at(last_dup
, pos
, &vector
);
1293 cpml_vector_set_length(&vector
, offset
);
1294 cpml_vector_normal(&vector
);
1295 p
[0].x
= center
.x
- vector
.x
;
1296 p
[0].y
= center
.y
- vector
.y
;
1298 /* Compute the mid point of the fillet */
1299 cpml_pair_from_cairo(&vector
, current
->org
);
1300 vector
.x
-= center
.x
;
1301 vector
.y
-= center
.y
;
1302 cpml_vector_set_length(&vector
, radius
);
1303 p
[1].x
= center
.x
+ vector
.x
;
1304 p
[1].y
= center
.y
+ vector
.y
;
1306 /* Compute the end point of the fillet */
1307 pos
= cpml_primitive_get_closest_pos(current_dup
, ¢er
);
1308 cpml_primitive_put_vector_at(current_dup
, pos
, &vector
);
1309 cpml_vector_set_length(&vector
, offset
);
1310 cpml_vector_normal(&vector
);
1311 p
[2].x
= center
.x
- vector
.x
;
1312 p
[2].y
= center
.y
- vector
.y
;
1314 g_free(current_dup
);
1317 /* Change the end point of the last primitive */
1318 cpml_primitive_set_point(last
, -1, &p
[0]);
1320 /* Change the start point of the current primitive */
1321 cpml_primitive_set_point(current
, 0, &p
[2]);
1323 /* Add the fillet arc */
1324 data
->operation
.action
= ADG_ACTION_NONE
;
1325 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
1329 _adg_is_convex(const CpmlPrimitive
*primitive1
, const CpmlPrimitive
*primitive2
)
1332 gdouble angle1
, angle2
;
1334 cpml_primitive_put_vector_at(primitive1
, -1, &v1
);
1335 cpml_primitive_put_vector_at(primitive2
, 0, &v2
);
1337 /* Probably there is a smarter way to get this without trygonometry */
1338 angle1
= cpml_vector_angle(&v1
);
1339 angle2
= cpml_vector_angle(&v2
);
1341 if (angle1
> angle2
)
1344 return angle2
-angle1
> G_PI
;
1347 static const gchar
*
1348 _adg_action_name(AdgAction action
)
1351 case ADG_ACTION_NONE
:
1353 case ADG_ACTION_CHAMFER
:
1355 case ADG_ACTION_FILLET
:
1363 _adg_get_named_pair(AdgModel
*model
, const gchar
*name
,
1364 CpmlPair
*pair
, gpointer user_data
)
1366 GSList
**named_pairs
;
1367 AdgNamedPair
*named_pair
;
1369 named_pairs
= user_data
;
1371 named_pair
= g_new(AdgNamedPair
, 1);
1372 named_pair
->name
= name
;
1373 named_pair
->pair
= *pair
;
1375 *named_pairs
= g_slist_prepend(*named_pairs
, named_pair
);
1379 _adg_dup_reverse_named_pairs(AdgModel
*model
, const cairo_matrix_t
*matrix
)
1381 AdgNamedPair
*old_named_pair
;
1382 AdgNamedPair named_pair
;
1383 GSList
*named_pairs
;
1385 /* Populate named_pairs with all the named pairs of model */
1387 adg_model_foreach_named_pair(model
, _adg_get_named_pair
, &named_pairs
);
1389 /* Readd the pairs applying the reversing transformation matrix to
1390 * their coordinates and prepending a "-" to their name */
1391 while (named_pairs
) {
1392 old_named_pair
= named_pairs
->data
;
1394 named_pair
.name
= g_strdup_printf("-%s", old_named_pair
->name
);
1395 named_pair
.pair
= old_named_pair
->pair
;
1396 cpml_pair_transform(&named_pair
.pair
, matrix
);
1398 adg_model_set_named_pair(model
, named_pair
.name
, &named_pair
.pair
);
1400 g_free((gpointer
) named_pair
.name
);
1401 named_pairs
= g_slist_delete_link(named_pairs
, named_pairs
);