1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2021 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 <function>get_cairo_path</function> method
30 * of the parent #AdgTrail class, avoiding the need of an
31 * #AdgTrailCallback. The path is constructed programmatically: keep
32 * in mind any method that modifies the path will invalidate the
33 * #cairo_path_t 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_close_path() a %CPML_MOVE primitive to
47 * the starting point of the segment is automatically added by cairo;
48 * 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"
65 #include "adg-model.h"
66 #include "adg-trail.h"
69 #include "adg-path-private.h"
72 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_path_parent_class)
73 #define _ADG_OLD_MODEL_CLASS ((AdgModelClass *) adg_path_parent_class)
75 #define REMAPPED(ptr, from, to) \
76 (ptr) == NULL ? NULL : \
77 (gpointer) ((guint8 *) (ptr) - (guint8 *) (from) + (guint8 *) (to))
80 G_DEFINE_TYPE_WITH_PRIVATE(AdgPath
, adg_path
, ADG_TYPE_TRAIL
)
83 static void _adg_finalize (GObject
*object
);
84 static void _adg_clear (AdgModel
*model
);
85 static void _adg_clear_parent (AdgModel
*model
);
86 static void _adg_changed (AdgModel
*model
);
87 static cairo_path_t
* _adg_get_cairo_path (AdgTrail
*trail
);
88 static cairo_path_t
* _adg_read_cairo_path (AdgPath
*path
);
89 static gint
_adg_primitive_length (CpmlPrimitiveType type
);
90 static void _adg_primitive_remap (CpmlPrimitive
*primitive
,
95 static void _adg_rescan (AdgPath
*path
);
96 static void _adg_append_primitive (AdgPath
*path
,
97 CpmlPrimitive
*primitive
);
98 static void _adg_clear_operation (AdgPath
*path
);
99 static gboolean
_adg_append_operation (AdgPath
*path
,
102 static void _adg_do_operation (AdgPath
*path
,
105 static void _adg_do_action (AdgPath
*path
,
107 CpmlPrimitive
*primitive
);
108 static void _adg_do_chamfer (AdgPath
*path
,
109 CpmlPrimitive
*current
);
110 static void _adg_do_fillet (AdgPath
*path
,
111 CpmlPrimitive
*current
);
112 static gboolean
_adg_is_convex (const CpmlPrimitive
116 static const gchar
* _adg_action_name (AdgAction action
);
117 static void _adg_get_named_pair (AdgModel
*model
,
121 static void _adg_dup_reverse_named_pairs
128 adg_path_class_init(AdgPathClass
*klass
)
130 GObjectClass
*gobject_class
;
131 AdgModelClass
*model_class
;
132 AdgTrailClass
*trail_class
;
134 gobject_class
= (GObjectClass
*) klass
;
135 model_class
= (AdgModelClass
*) klass
;
136 trail_class
= (AdgTrailClass
*) klass
;
138 gobject_class
->finalize
= _adg_finalize
;
140 model_class
->clear
= _adg_clear
;
141 model_class
->changed
= _adg_changed
;
143 trail_class
->get_cairo_path
= _adg_get_cairo_path
;
147 adg_path_init(AdgPath
*path
)
149 AdgPathPrivate
*data
= adg_path_get_instance_private(path
);
150 data
->cp_is_valid
= FALSE
;
153 data
->cairo
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
154 data
->cairo
.path
.data
= NULL
;
155 data
->cairo
.path
.num_data
= 0;
156 data
->cairo
.array
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
157 data
->last
.segment
= NULL
;
158 data
->last
.org
= NULL
;
159 data
->last
.data
= NULL
;
160 data
->over
.segment
= NULL
;
161 data
->over
.org
= NULL
;
162 data
->over
.data
= NULL
;
163 data
->operation
.action
= ADG_ACTION_NONE
;
167 _adg_finalize(GObject
*object
)
169 AdgPath
*path
= (AdgPath
*) object
;
170 AdgPathPrivate
*data
= adg_path_get_instance_private(path
);
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, <constant>NULL</constant> 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 <constant>NULL</constant> 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
);
222 data
= adg_path_get_instance_private(path
);
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
);
248 data
= adg_path_get_instance_private(path
);
249 return data
->cp_is_valid
;
253 * adg_path_last_primitive:
256 * Gets the last primitive appended to @path. The %CPML_MOVE type is
257 * not considered a full-fledged primitive, i.e. adg_path_move_to()
258 * or similar does not change the last primitive.
260 * The returned struct is owned by @path and should not be freed or
263 * Returns: (transfer none): a pointer to the last appended primitive or <constant>NULL</constant> on no last primitive or on errors.
267 const CpmlPrimitive
*
268 adg_path_last_primitive(AdgPath
*path
)
270 AdgPathPrivate
*data
;
272 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
274 data
= adg_path_get_instance_private(path
);
276 /* Directly return NULL instead of returning an undefined primitive */
277 if (data
->last
.org
== NULL
|| data
->last
.data
== NULL
)
284 * adg_path_over_primitive:
287 * Gets the primitive before the last one appended to @path. The
288 * "over" term comes from forth, where the <emphasis>OVER</emphasis>
289 * operator works on the stack in the same way as
290 * adg_path_over_primitive() works on @path. The %CPML_MOVE type is
291 * not considered a full-fledged primitive, i.e. adg_path_move_to()
292 * or similar does not change the over primitive.
294 * The returned struct is owned by @path and should not be freed or
297 * Returns: (transfer none): a pointer to the primitive before the last appended one or <constant>NULL</constant> on errors.
301 const CpmlPrimitive
*
302 adg_path_over_primitive(AdgPath
*path
)
304 AdgPathPrivate
*data
;
306 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
308 data
= adg_path_get_instance_private(path
);
310 /* Directly return NULL instead of returning an undefined primitive */
311 if (data
->over
.org
== NULL
|| data
->over
.data
== NULL
)
320 * @type: (type CpmlPrimitiveType): a #cairo_data_type_t value
321 * @...: point data, specified as #CpmlPair pointers
323 * Generic method to append a primitive to @path. The number of #CpmlPair
324 * pointers to pass as @Varargs depends on @type: %CPML_CLOSE does not
325 * require any pair, %CPML_MOVE and %CPML_LINE require one pair,
326 * %CPML_ARC two pairs, %CPML_CURVE three pairs and so on.
328 * All the needed pairs must be not <constant>NULL</constant> pointers,
329 * otherwise the function will fail. The pairs in excess, if any, are ignored.
334 adg_path_append(AdgPath
*path
, gint type
, ...)
338 va_start(var_args
, type
);
339 adg_path_append_valist(path
, (CpmlPrimitiveType
) type
, var_args
);
344 * adg_path_append_valist:
346 * @type: a #cairo_data_type_t value
347 * @var_args: point data, specified as #CpmlPair pointers
349 * va_list version of adg_path_append().
354 adg_path_append_valist(AdgPath
*path
, CpmlPrimitiveType type
, va_list var_args
)
357 const CpmlPair
*pair
;
360 length
= _adg_primitive_length(type
);
364 array
= g_ptr_array_sized_new(4);
366 pair
= va_arg(var_args
, const CpmlPair
*);
368 g_ptr_array_free(array
, TRUE
);
369 g_return_if_reached();
372 g_ptr_array_add(array
, (gpointer
) pair
);
375 /* The array must be NULL terminated */
376 g_ptr_array_add(array
, NULL
);
378 adg_path_append_array(path
, type
, (const CpmlPair
**) array
->pdata
);
380 g_ptr_array_free(array
, TRUE
);
384 * adg_path_append_array: (rename-to adg_path_append)
386 * @type: a #cairo_data_type_t value
387 * @pairs: (array zero-terminated=1) (element-type Cpml.Pair) (transfer none): point data, specified as a <constant>NULL</constant> terminated array of #CpmlPair pointers.
389 * A bindingable version of adg_path_append() that uses a
390 * <constant>NULL</constant> terminated array of pairs instead of variable
391 * argument list and friends.
393 * Furthermore, because of the list is <constant>NULL</constant> terminated,
394 * an arbitrary number of pairs can be passed in @pairs. This allows to embed
395 * in a primitive element more data pairs than requested, something impossible
396 * to do with adg_path_append() and adg_path_append_valist().
401 adg_path_append_array(AdgPath
*path
, CpmlPrimitiveType type
,
402 const CpmlPair
**pairs
)
406 const CpmlPair
**pair
;
407 cairo_path_data_t path_data
;
409 g_return_if_fail(ADG_IS_PATH(path
));
410 g_return_if_fail(pairs
!= NULL
);
412 length
= _adg_primitive_length(type
);
416 array
= g_array_new(FALSE
, FALSE
, sizeof(path_data
));
417 for (pair
= pairs
; *pair
!= NULL
; ++ pair
) {
418 cpml_pair_to_cairo(*pair
, &path_data
);
419 g_array_append_val(array
, path_data
);
422 if (array
->len
< length
- 1) {
423 /* Not enough pairs have been provided */
424 g_warning(_("%s: null pair caught while parsing arguments"), G_STRLOC
);
426 AdgPathPrivate
*data
= adg_path_get_instance_private(path
);
427 CpmlPrimitive primitive
;
428 cairo_path_data_t org
;
430 /* Save a copy of the current point as primitive origin */
431 cpml_pair_to_cairo(&data
->cp
, &org
);
433 /* Prepend the cairo header */
434 path_data
.header
.type
= type
;
435 path_data
.header
.length
= array
->len
+ 1;
436 g_array_prepend_val(array
, path_data
);
438 /* Append a new primitive to @path */
439 primitive
.segment
= NULL
;
440 primitive
.org
= &org
;
441 primitive
.data
= (cairo_path_data_t
*) array
->data
;
442 _adg_append_primitive(path
, &primitive
);
445 g_array_free(array
, TRUE
);
450 * adg_path_append_primitive:
452 * @primitive: the #CpmlPrimitive to append
454 * Appends @primitive to @path. The primitive to add is considered the
455 * continuation of the current path so the <structfield>org</structfield>
456 * component of @primitive is not used. Anyway the current point is
457 * checked against it: they must be equal or the function will fail
458 * without further processing.
463 adg_path_append_primitive(AdgPath
*path
, const CpmlPrimitive
*primitive
)
465 AdgPathPrivate
*data
;
466 CpmlPrimitive
*primitive_dup
;
468 g_return_if_fail(ADG_IS_PATH(path
));
469 g_return_if_fail(primitive
!= NULL
);
470 g_return_if_fail(primitive
->org
!= NULL
);
471 g_return_if_fail(primitive
->data
!= NULL
);
473 data
= adg_path_get_instance_private(path
);
475 g_return_if_fail(primitive
->org
->point
.x
== data
->cp
.x
&&
476 primitive
->org
->point
.y
== data
->cp
.y
);
478 /* The primitive data could be modified by pending operations:
480 primitive_dup
= cpml_primitive_deep_dup(primitive
);
482 _adg_append_primitive(path
, primitive_dup
);
484 g_free(primitive_dup
);
488 * adg_path_append_segment:
490 * @segment: the #CpmlSegment to append
492 * Appends @segment to @path.
497 adg_path_append_segment(AdgPath
*path
, const CpmlSegment
*segment
)
499 g_return_if_fail(ADG_IS_PATH(path
));
500 g_return_if_fail(segment
!= NULL
);
502 if (segment
->num_data
> 0) {
503 AdgPathPrivate
*data
;
505 g_return_if_fail(segment
->data
!= NULL
);
507 data
= adg_path_get_instance_private(path
);
509 _adg_clear_parent((AdgModel
*) path
);
510 data
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
511 segment
->data
, segment
->num_data
);
517 * adg_path_append_cairo_path:
519 * @cairo_path: the #cairo_path_t path to append
521 * Appends a whole #cairo_path_t to @path.
526 adg_path_append_cairo_path(AdgPath
*path
, const cairo_path_t
*cairo_path
)
528 AdgPathPrivate
*data
;
530 g_return_if_fail(ADG_IS_PATH(path
));
531 g_return_if_fail(cairo_path
!= NULL
);
533 data
= adg_path_get_instance_private(path
);
535 _adg_clear_parent((AdgModel
*) path
);
536 data
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
538 cairo_path
->num_data
);
543 * adg_path_append_trail:
545 * @trail: an #AdgTrail instance
547 * Appends the content of @trail to @path. It is similar to
548 * adg_path_append_cairo_path() but it also appends to @path the named pairs
549 * eventually defined in @trail.
554 adg_path_append_trail(AdgPath
*path
, AdgTrail
*trail
)
557 AdgNamedPair
*named_pair
;
559 g_return_if_fail(ADG_IS_PATH(path
));
560 g_return_if_fail(ADG_IS_TRAIL(trail
));
562 adg_path_append_cairo_path(path
, adg_trail_get_cairo_path(trail
));
564 /* Populate named_pairs with all the named pairs of trail */
566 adg_model_foreach_named_pair((AdgModel
*)trail
,
567 _adg_get_named_pair
, &named_pairs
);
569 /* Readd the pairs to path */
570 while (named_pairs
) {
571 named_pair
= (AdgNamedPair
*) named_pairs
->data
;
572 adg_model_set_named_pair((AdgModel
*) path
,
573 named_pair
->name
, &named_pair
->pair
);
574 named_pairs
= g_slist_delete_link(named_pairs
, named_pairs
);
579 * adg_path_remove_primitive:
582 * Removes the last primitive from @path.
587 adg_path_remove_primitive(AdgPath
*path
)
589 AdgPathPrivate
*data
;
590 const CpmlPrimitive
*over
;
593 g_return_if_fail(ADG_IS_PATH(path
));
595 data
= adg_path_get_instance_private(path
);
596 over
= adg_path_over_primitive(path
);
599 cairo_path_data_t
*end
= over
->data
+ over
->data
->header
.length
;
600 len
= end
- (cairo_path_data_t
*) (data
->cairo
.array
)->data
;
605 /* Resize the data array */
606 g_array_set_size(data
->cairo
.array
, len
);
608 /* Rescan path to compute the new over and last primitives */
609 _adg_clear_parent((AdgModel
*) path
);
616 * @pair: the destination coordinates
618 * Begins a new segment. After this call the current point will be @pair.
623 adg_path_move_to(AdgPath
*path
, const CpmlPair
*pair
)
625 adg_path_append(path
, CPML_MOVE
, pair
);
629 * adg_path_move_to_explicit:
631 * @x: the new x coordinate
632 * @y: the new y coordinate
634 * Convenient function to call adg_path_move_to() using explicit
635 * coordinates instead of #CpmlPair.
640 adg_path_move_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
647 adg_path_append(path
, CPML_MOVE
, &p
);
653 * @pair: the destination coordinates
655 * Adds a line to @path from the current point to @pair. After this
656 * call the current point will be @pair.
658 * If @path has no current point before this call, this function will
659 * trigger a warning without other effect.
664 adg_path_line_to(AdgPath
*path
, const CpmlPair
*pair
)
666 adg_path_append(path
, CPML_LINE
, pair
);
670 * adg_path_line_to_explicit:
672 * @x: the new x coordinate
673 * @y: the new y coordinate
675 * Convenient function to call adg_path_line_to() using explicit
676 * coordinates instead of #CpmlPair.
681 adg_path_line_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
688 adg_path_append(path
, CPML_LINE
, &p
);
694 * @through: an arbitrary point on the arc
695 * @pair: the destination coordinates
697 * Adds an arc to the path from the current point to @pair, passing
698 * through @through. After this call the current point will be @pair.
700 * If @path has no current point before this call, this function will
701 * trigger a warning without other effect.
706 adg_path_arc_to(AdgPath
*path
, const CpmlPair
*through
, const CpmlPair
*pair
)
708 adg_path_append(path
, CPML_ARC
, through
, pair
);
712 * adg_path_arc_to_explicit:
714 * @x1: the x coordinate of an intermediate point
715 * @y1: the y coordinate of an intermediate point
716 * @x2: the x coordinate of the end of the arc
717 * @y2: the y coordinate of the end of the arc
719 * Convenient function to call adg_path_arc_to() using explicit
720 * coordinates instead of #CpmlPair.
725 adg_path_arc_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
726 gdouble x2
, gdouble y2
)
735 adg_path_append(path
, CPML_ARC
, &p
[0], &p
[1]);
741 * @control1: the first control point of the curve
742 * @control2: the second control point of the curve
743 * @pair: the destination coordinates
745 * Adds a cubic Bézier curve to the path from the current point to
746 * position @pair, using @control1 and @control2 as control points.
747 * After this call the current point will be @pair.
749 * If @path has no current point before this call, this function will
750 * trigger a warning without other effect.
755 adg_path_curve_to(AdgPath
*path
, const CpmlPair
*control1
,
756 const CpmlPair
*control2
, const CpmlPair
*pair
)
758 adg_path_append(path
, CPML_CURVE
, control1
, control2
, pair
);
762 * adg_path_curve_to_explicit:
764 * @x1: the x coordinate of the first control point
765 * @y1: the y coordinate of the first control point
766 * @x2: the x coordinate of the second control point
767 * @y2: the y coordinate of the second control point
768 * @x3: the x coordinate of the end of the curve
769 * @y3: the y coordinate of the end of the curve
771 * Convenient function to call adg_path_curve_to() using explicit
772 * coordinates instead of #CpmlPair.
777 adg_path_curve_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
778 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
789 adg_path_append(path
, CPML_CURVE
, &p
[0], &p
[1], &p
[2]);
796 * Adds a line segment to the path from the current point to the
797 * beginning of the current segment, (the most recent point passed
798 * to an adg_path_move_to()), and closes this segment.
799 * After this call the current point will be unset.
801 * The behavior of adg_path_close() is distinct from simply calling
802 * adg_path_line_to() with the coordinates of the segment starting
803 * point. When a closed segment is stroked, there are no caps on the
804 * ends. Instead, there is a line join connecting the final and
805 * initial primitive of the segment.
807 * If @path has no current point before this call, this function will
808 * trigger a warning without other effect.
813 adg_path_close(AdgPath
*path
)
815 adg_path_append(path
, CPML_CLOSE
);
821 * @center: coordinates of the center of the arc
822 * @r: the radius of the arc
823 * @start: the start angle, in radians
824 * @end: the end angle, in radians
826 * A more usual way to add an arc to @path. After this call, the current
827 * point will be the computed end point of the arc. The arc will be
828 * rendered in increasing angle, accordling to @start and @end. This means
829 * if @start is less than @end, the arc will be rendered in clockwise
830 * direction (accordling to the default cairo coordinate system) while if
831 * @start is greather than @end, the arc will be rendered in couterclockwise
834 * By explicitely setting the whole arc data, the start point could be
835 * different from the current point. In this case, if @path has no
836 * current point before the call a %CPML_MOVE to the start point of
837 * the arc will be automatically prepended to the arc. If @path has a
838 * current point, a %CPML_LINE to the start point of the arc will be
839 * used instead of the "move to" primitive.
844 adg_path_arc(AdgPath
*path
, const CpmlPair
*center
, gdouble r
,
845 gdouble start
, gdouble end
)
847 AdgPathPrivate
*data
;
850 g_return_if_fail(ADG_IS_PATH(path
));
851 g_return_if_fail(center
!= NULL
);
853 data
= adg_path_get_instance_private(path
);
855 cpml_vector_from_angle(&p
[0], start
);
856 cpml_vector_from_angle(&p
[1], (start
+end
) / 2);
857 cpml_vector_from_angle(&p
[2], end
);
859 cpml_vector_set_length(&p
[0], r
);
860 cpml_vector_set_length(&p
[1], r
);
861 cpml_vector_set_length(&p
[2], r
);
870 if (!data
->cp_is_valid
)
871 adg_path_append(path
, CPML_MOVE
, &p
[0]);
872 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
873 adg_path_append(path
, CPML_LINE
, &p
[0]);
875 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
879 * adg_path_arc_explicit:
881 * @xc: x position of the center of the arc
882 * @yc: y position of the center of the arc
883 * @r: the radius of the arc
884 * @start: the start angle, in radians
885 * @end: the end angle, in radians
887 * Convenient function to call adg_path_arc() using explicit
888 * coordinates instead of #CpmlPair.
893 adg_path_arc_explicit(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
894 gdouble start
, gdouble end
)
901 adg_path_arc(path
, ¢er
, r
, start
, end
);
907 * @delta1: the distance from the intersection point of the current primitive
908 * @delta2: the distance from the intersection point of the next primitive
910 * A binary action that generates a chamfer between two primitives.
911 * The first primitive involved is the current primitive, the second will
912 * be the next primitive appended to @path after this call. The second
913 * primitive is required: if the chamfer operation is not properly
914 * terminated (by not providing the second primitive), any API accessing
915 * the path in reading mode will raise a warning.
917 * An exception is a chamfer after a %CPML_CLOSE primitive. In this case,
918 * the second primitive is not required: the current close path is used
919 * as first operand while the first primitive of the current segment is
920 * used as second operand.
922 * The chamfer operation requires two lengths: @delta1 specifies the
923 * "quantity" to trim on the first primitive while @delta2 is the same
924 * applied on the second primitive. The term "quantity" means the length
925 * of the portion to cut out from the original primitive (that is the
926 * primitive as would be without the chamfer).
931 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
933 g_return_if_fail(ADG_IS_PATH(path
));
935 if (!_adg_append_operation(path
, ADG_ACTION_CHAMFER
, delta1
, delta2
))
942 * @radius: the radius of the fillet
944 * A binary action that joins to primitives with an arc.
945 * The first primitive involved is the current primitive, the second will
946 * be the next primitive appended to @path after this call. The second
947 * primitive is required: if the fillet operation is not properly
948 * terminated (by not providing the second primitive), any API accessing
949 * the path in reading mode will raise a warning.
951 * An exception is a fillet after a %CPML_CLOSE primitive. In this case,
952 * the second primitive is not required: the current close path is used
953 * as first operand while the first primitive of the current segment is
954 * used as second operand.
959 adg_path_fillet(AdgPath
*path
, gdouble radius
)
961 g_return_if_fail(ADG_IS_PATH(path
));
963 if (!_adg_append_operation(path
, ADG_ACTION_FILLET
, radius
))
971 * Joins all the segments of @path. After the call there will be only one
974 * This operation is roughly equivalent to converting embedded %CPML_MOVE
975 * primitives into %CPML_LINE ones.
980 adg_path_join(AdgPath
*path
)
982 cairo_path_t
*cairo_path
;
983 cairo_path_data_t
*data
;
986 g_return_if_fail(ADG_IS_PATH(path
));
988 cairo_path
= _adg_read_cairo_path(path
);
990 data
= cairo_path
->data
;
992 while (data
- cairo_path
->data
< cairo_path
->num_data
) {
993 if (data
->header
.type
!= CPML_MOVE
) {
995 } else if (pen_down
) {
996 data
->header
.type
= CPML_LINE
;
998 data
+= data
->header
.length
;
1004 * @path: an #AdgPath
1005 * @vector: (allow-none): the slope of the axis
1007 * Reflects the first segment or @path around the axis passing
1008 * through (0, 0) and with a @vector slope. The internal segment
1009 * is duplicated and the proper transformation (computed from
1010 * @vector) to mirror the segment is applied on all its points.
1011 * The result is then reversed with cpml_segment_reverse() and
1012 * appended to the original path with adg_path_append_segment().
1014 * For convenience, if @vector is <constant>NULL</constant> the
1015 * path is reversed around the x axis <constant>(y = 0)</constant>.
1020 adg_path_reflect(AdgPath
*path
, const CpmlVector
*vector
)
1024 cairo_matrix_t matrix
;
1025 CpmlSegment segment
, *dup_segment
;
1028 g_return_if_fail(ADG_IS_PATH(path
));
1029 g_return_if_fail(vector
== NULL
|| vector
->x
!= 0 || vector
->y
!= 0);
1031 model
= (AdgModel
*) path
;
1032 trail
= (AdgTrail
*) path
;
1034 if (vector
== NULL
) {
1035 cairo_matrix_init_scale(&matrix
, 1, -1);
1038 gdouble cos2angle
, sin2angle
;
1040 cpml_pair_copy(&slope
, vector
);
1041 cpml_vector_set_length(&slope
, 1);
1043 if (slope
.x
== 0 && slope
.y
== 0) {
1044 g_warning(_("%s: the axis of the reflection is not known"),
1049 sin2angle
= 2. * vector
->x
* vector
->y
;
1050 cos2angle
= 2. * vector
->x
* vector
->x
- 1;
1052 cairo_matrix_init(&matrix
, cos2angle
, sin2angle
,
1053 sin2angle
, -cos2angle
, 0, 0);
1056 for (n
= adg_trail_n_segments(trail
); n
> 0; --n
) {
1057 adg_trail_put_segment(trail
, n
, &segment
);
1059 /* No need to reverse an empty segment */
1060 if (segment
.num_data
== 0 || segment
.num_data
== 0)
1063 dup_segment
= cpml_segment_deep_dup(&segment
);
1064 if (dup_segment
== NULL
)
1067 cpml_segment_reverse(dup_segment
);
1068 cpml_segment_transform(dup_segment
, &matrix
);
1069 dup_segment
->data
[0].header
.type
= CPML_MOVE
;
1071 adg_path_append_segment(path
, dup_segment
);
1073 g_free(dup_segment
);
1076 _adg_dup_reverse_named_pairs(model
, &matrix
);
1080 * adg_path_reflect_explicit:
1081 * @path: an #AdgPath
1082 * @x: the vector x component
1083 * @y: the vector y component
1085 * Convenient function to call adg_path_reflect() using explicit
1086 * vector components instead of #CpmlVector.
1091 adg_path_reflect_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
1098 adg_path_reflect(path
, &vector
);
1103 _adg_clear(AdgModel
*model
)
1105 AdgPath
*path
= (AdgPath
*) model
;
1106 AdgPathPrivate
*data
= adg_path_get_instance_private(path
);
1108 g_array_set_size(data
->cairo
.array
, 0);
1109 _adg_clear_operation(path
);
1110 _adg_clear_parent(model
);
1114 _adg_clear_parent(AdgModel
*model
)
1116 if (_ADG_OLD_MODEL_CLASS
->clear
)
1117 _ADG_OLD_MODEL_CLASS
->clear(model
);
1121 _adg_changed(AdgModel
*model
)
1123 _adg_clear_parent(model
);
1125 if (_ADG_OLD_MODEL_CLASS
->changed
)
1126 _ADG_OLD_MODEL_CLASS
->changed(model
);
1129 static cairo_path_t
*
1130 _adg_get_cairo_path(AdgTrail
*trail
)
1132 _adg_clear_parent((AdgModel
*) trail
);
1133 return _adg_read_cairo_path((AdgPath
*) trail
);
1136 static cairo_path_t
*
1137 _adg_read_cairo_path(AdgPath
*path
)
1139 AdgPathPrivate
*data
= adg_path_get_instance_private(path
);
1140 cairo_path_t
*cairo_path
= &data
->cairo
.path
;
1141 GArray
*array
= data
->cairo
.array
;
1143 /* Always regenerate the cairo_path_t as it is a trivial operation */
1144 cairo_path
->status
= CAIRO_STATUS_SUCCESS
;
1145 cairo_path
->data
= (cairo_path_data_t
*) array
->data
;
1146 cairo_path
->num_data
= array
->len
;
1152 _adg_primitive_length(CpmlPrimitiveType type
)
1163 return cpml_primitive_type_get_n_points(type
);
1168 _adg_primitive_remap(CpmlPrimitive
*primitive
, gpointer to
,
1169 const CpmlPrimitive
*old
, gconstpointer from
)
1171 primitive
->org
= REMAPPED(old
->org
, from
, to
);
1172 primitive
->segment
= REMAPPED(old
->segment
, from
, to
);
1173 primitive
->data
= REMAPPED(old
->data
, from
, to
);
1177 _adg_rescan(AdgPath
*path
)
1179 AdgPathPrivate
*data
= adg_path_get_instance_private(path
);
1180 CpmlPrimitive
*last
= &data
->last
;
1181 CpmlPrimitive
*over
= &data
->over
;
1182 CpmlSegment segment
;
1183 CpmlPrimitive current
;
1185 last
->segment
= NULL
;
1188 over
->segment
= NULL
;
1192 /* When no data is present, just bail out */
1193 if (! cpml_segment_from_cairo(&segment
, _adg_read_cairo_path(path
))) {
1194 data
->cp_is_valid
= FALSE
;
1199 cpml_primitive_from_segment(¤t
, &segment
);
1201 cpml_primitive_copy(over
, last
);
1202 cpml_primitive_copy(last
, ¤t
);
1203 } while (cpml_primitive_next(¤t
));
1204 } while (cpml_segment_next(&segment
));
1206 /* Save the last point in the current point */
1207 data
->cp_is_valid
= last
->data
&& last
->data
->header
.type
!= CPML_CLOSE
;
1208 if (data
->cp_is_valid
) {
1209 CpmlPrimitiveType type
= last
->data
->header
.type
;
1210 size_t n
= type
== CPML_MOVE
? 1 : cpml_primitive_type_get_n_points(type
) - 1;
1211 cpml_pair_from_cairo(&data
->cp
, &last
->data
[n
]);
1216 _adg_append_primitive(AdgPath
*path
, CpmlPrimitive
*current
)
1218 AdgPathPrivate
*data
= adg_path_get_instance_private(path
);
1219 cairo_path_data_t
*path_data
= current
->data
;
1220 int length
= path_data
->header
.length
;
1221 CpmlPrimitiveType type
= path_data
->header
.type
;
1222 gconstpointer old_data
;
1225 /* Execute any pending operation */
1226 _adg_do_operation(path
, path_data
);
1228 /* Append the path data to the internal path array */
1229 old_data
= (data
->cairo
.array
)->data
;
1230 data
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
1232 new_data
= (data
->cairo
.array
)->data
;
1234 /* Set path data to point to the recently appended cairo_path_data_t
1235 * primitive: the first struct is the header */
1236 path_data
= (cairo_path_data_t
*) new_data
+
1237 (data
->cairo
.array
)->len
- length
;
1239 if (type
== CPML_MOVE
) {
1240 /* Remap last and over, but do not change their content */
1241 _adg_primitive_remap(&data
->last
, new_data
, &data
->last
, old_data
);
1242 _adg_primitive_remap(&data
->over
, new_data
, &data
->over
, old_data
);
1244 /* Store the last primitive into over */
1245 _adg_primitive_remap(&data
->over
, new_data
, &data
->last
, old_data
);
1247 /* Set the last primitive for subsequent binary operations */
1248 /* TODO: the assumption path_data - 1 is the last point is not true
1249 * e.g. when there are embedded data in primitives */
1250 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
1251 data
->last
.segment
= NULL
;
1252 data
->last
.data
= path_data
;
1255 data
->cp_is_valid
= type
!= CPML_CLOSE
;
1256 if (data
->cp_is_valid
) {
1257 /* Save the last point in the current point */
1258 size_t n
= type
== CPML_MOVE
? 1 : cpml_primitive_type_get_n_points(type
) - 1;
1259 cpml_pair_from_cairo(&data
->cp
, &path_data
[n
]);
1262 /* Invalidate cairo_path: should be recomputed */
1263 _adg_clear_parent((AdgModel
*) path
);
1267 _adg_clear_operation(AdgPath
*path
)
1269 AdgPathPrivate
*data
= adg_path_get_instance_private(path
);
1270 AdgOperation
*operation
= &data
->operation
;
1272 if (operation
->action
!= ADG_ACTION_NONE
) {
1273 g_warning(_("%s: a '%s' operation is still active while clearing the path"),
1274 G_STRLOC
, _adg_action_name(operation
->action
));
1275 operation
->action
= ADG_ACTION_NONE
;
1278 data
->cp_is_valid
= FALSE
;
1279 data
->last
.data
= NULL
;
1280 data
->over
.data
= NULL
;
1284 _adg_append_operation(AdgPath
*path
, gint action
, ...)
1286 AdgPathPrivate
*data
= adg_path_get_instance_private(path
);
1287 AdgAction real_action
= (AdgAction
) action
;
1288 AdgOperation
*operation
;
1291 if (data
->last
.data
== NULL
) {
1292 g_warning(_("%s: requested a '%s' operation on a path without current primitive"),
1293 G_STRLOC
, _adg_action_name(real_action
));
1297 operation
= &data
->operation
;
1298 if (operation
->action
!= ADG_ACTION_NONE
) {
1299 g_warning(_("%s: requested a '%s' operation while a '%s' operation was active"),
1300 G_STRLOC
, _adg_action_name(real_action
),
1301 _adg_action_name(operation
->action
));
1302 /* XXX: http://dev.entidi.com/p/adg/issues/50/ */
1306 va_start(var_args
, action
);
1308 switch (real_action
) {
1310 case ADG_ACTION_CHAMFER
:
1311 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
1312 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
1315 case ADG_ACTION_FILLET
:
1316 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
1319 case ADG_ACTION_NONE
:
1324 g_warning(_("%s: %d path operation not recognized"), G_STRLOC
, real_action
);
1329 operation
->action
= real_action
;
1332 if (data
->last
.data
[0].header
.type
== CPML_CLOSE
) {
1333 /* Special case: an action with the close primitive should
1334 * be resolved now by changing the close primitive to a
1335 * line-to and using it as second operand and use the first
1336 * primitive of the current segment as first operand */
1338 cairo_path_data_t
*path_data
;
1339 CpmlSegment segment
;
1340 CpmlPrimitive current
;
1342 length
= data
->cairo
.array
->len
;
1344 /* Ensure the close path primitive is not the only data */
1345 g_return_val_if_fail(length
> 1, FALSE
);
1347 /* Allocate one more item once for all to accept the
1348 * conversion from a close to line-to primitive */
1349 data
->cairo
.array
= g_array_set_size(data
->cairo
.array
, length
+ 1);
1350 path_data
= (cairo_path_data_t
*) data
->cairo
.array
->data
;
1351 --data
->cairo
.array
->len
;
1353 /* Set segment and current (the first primitive of segment) */
1354 cpml_segment_from_cairo(&segment
, _adg_read_cairo_path(path
));
1355 while (cpml_segment_next(&segment
))
1357 cpml_primitive_from_segment(¤t
, &segment
);
1359 /* Convert close path to a line-to primitive */
1360 ++data
->cairo
.array
->len
;
1361 path_data
[length
- 1].header
.type
= CPML_LINE
;
1362 path_data
[length
- 1].header
.length
= 2;
1363 path_data
[length
] = *current
.org
;
1365 data
->last
.segment
= &segment
;
1366 data
->last
.org
= &path_data
[length
- 2];
1367 data
->last
.data
= &path_data
[length
- 1];
1369 _adg_do_action(path
, real_action
, ¤t
);
1376 _adg_do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
1378 AdgPathPrivate
*data
= adg_path_get_instance_private(path
);
1379 AdgAction action
= data
->operation
.action
;
1380 CpmlSegment segment
;
1381 CpmlPrimitive current
;
1382 cairo_path_data_t current_org
;
1384 cpml_segment_from_cairo(&segment
, _adg_read_cairo_path(path
));
1386 /* Construct the current primitive, that is the primitive to be
1387 * mixed with the last primitive with the specified operation.
1388 * Its org is a copy of the end point of the last primitive: it can be
1389 * modified without affecting anything else. It is expected the operation
1390 * functions will add to @path the primitives required but NOT to add
1391 * @current, as this one will be inserted automatically. */
1392 current
.segment
= &segment
;
1393 current
.org
= ¤t_org
;
1394 current
.data
= path_data
;
1395 cpml_pair_to_cairo(&data
->cp
, ¤t_org
);
1397 _adg_do_action(path
, action
, ¤t
);
1401 _adg_do_action(AdgPath
*path
, AdgAction action
, CpmlPrimitive
*primitive
)
1404 case ADG_ACTION_NONE
:
1406 case ADG_ACTION_CHAMFER
:
1407 _adg_do_chamfer(path
, primitive
);
1409 case ADG_ACTION_FILLET
:
1410 _adg_do_fillet(path
, primitive
);
1413 g_return_if_reached();
1418 _adg_do_chamfer(AdgPath
*path
, CpmlPrimitive
*current
)
1420 AdgPathPrivate
*data
= adg_path_get_instance_private(path
);
1421 CpmlPrimitive
*last
= &data
->last
;
1422 gdouble delta1
= data
->operation
.data
.chamfer
.delta1
;
1423 gdouble len1
= cpml_primitive_get_length(last
);
1424 gdouble delta2
, len2
;
1427 if (delta1
>= len1
) {
1428 g_warning(_("%s: first chamfer delta of %lf is greather than the available %lf length"),
1429 G_STRLOC
, delta1
, len1
);
1433 delta2
= data
->operation
.data
.chamfer
.delta2
;
1434 len2
= cpml_primitive_get_length(current
);
1436 if (delta2
>= len2
) {
1437 g_warning(_("%s: second chamfer delta of %lf is greather than the available %lf length"),
1438 G_STRLOC
, delta1
, len1
);
1442 /* Change the end point of the last primitive */
1443 cpml_primitive_put_pair_at(last
, 1. - delta1
/ len1
, &pair
);
1444 cpml_primitive_set_point(last
, -1, &pair
);
1446 /* Change the start point of the current primitive */
1447 cpml_primitive_put_pair_at(current
, delta2
/ len2
, &pair
);
1448 cpml_primitive_set_point(current
, 0, &pair
);
1450 /* Add the chamfer line */
1451 data
->operation
.action
= ADG_ACTION_NONE
;
1452 adg_path_append(path
, CPML_LINE
, &pair
);
1456 _adg_do_fillet(AdgPath
*path
, CpmlPrimitive
*current
)
1458 AdgPathPrivate
*data
= adg_path_get_instance_private(path
);
1459 CpmlPrimitive
*last
= &data
->last
;
1460 CpmlPrimitive
*current_dup
= cpml_primitive_deep_dup(current
);
1461 CpmlPrimitive
*last_dup
= cpml_primitive_deep_dup(last
);
1462 gdouble radius
= data
->operation
.data
.fillet
.radius
;
1463 gdouble offset
= _adg_is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1465 CpmlPair center
, vector
, p
[3];
1467 /* Find the center of the fillet from the intersection between
1468 * the last and current primitives offseted by radius */
1469 cpml_primitive_offset(current_dup
, offset
);
1470 cpml_primitive_offset(last_dup
, offset
);
1471 if (cpml_primitive_put_intersections(current_dup
, last_dup
, 1, ¢er
) == 0) {
1472 g_warning(_("%s: fillet with radius of %lf is not applicable here"),
1474 g_free(current_dup
);
1479 /* Compute the start point of the fillet */
1480 pos
= cpml_primitive_get_closest_pos(last_dup
, ¢er
);
1481 cpml_primitive_put_vector_at(last_dup
, pos
, &vector
);
1482 cpml_vector_set_length(&vector
, offset
);
1483 cpml_vector_normal(&vector
);
1484 p
[0].x
= center
.x
- vector
.x
;
1485 p
[0].y
= center
.y
- vector
.y
;
1487 /* Compute the mid point of the fillet */
1488 cpml_pair_from_cairo(&vector
, current
->org
);
1489 vector
.x
-= center
.x
;
1490 vector
.y
-= center
.y
;
1491 cpml_vector_set_length(&vector
, radius
);
1492 p
[1].x
= center
.x
+ vector
.x
;
1493 p
[1].y
= center
.y
+ vector
.y
;
1495 /* Compute the end point of the fillet */
1496 pos
= cpml_primitive_get_closest_pos(current_dup
, ¢er
);
1497 cpml_primitive_put_vector_at(current_dup
, pos
, &vector
);
1498 cpml_vector_set_length(&vector
, offset
);
1499 cpml_vector_normal(&vector
);
1500 p
[2].x
= center
.x
- vector
.x
;
1501 p
[2].y
= center
.y
- vector
.y
;
1503 g_free(current_dup
);
1506 /* Change the end point of the last primitive */
1507 cpml_primitive_set_point(last
, -1, &p
[0]);
1509 /* Change the start point of the current primitive */
1510 cpml_primitive_set_point(current
, 0, &p
[2]);
1512 /* Add the fillet arc */
1513 data
->operation
.action
= ADG_ACTION_NONE
;
1514 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
1518 _adg_is_convex(const CpmlPrimitive
*primitive1
, const CpmlPrimitive
*primitive2
)
1521 gdouble angle1
, angle2
;
1523 cpml_primitive_put_vector_at(primitive1
, -1, &v1
);
1524 cpml_primitive_put_vector_at(primitive2
, 0, &v2
);
1526 /* Probably there is a smarter way to get this without trygonometry */
1527 angle1
= cpml_vector_angle(&v1
);
1528 angle2
= cpml_vector_angle(&v2
);
1530 if (angle1
> angle2
)
1533 return angle2
-angle1
> G_PI
;
1536 static const gchar
*
1537 _adg_action_name(AdgAction action
)
1540 case ADG_ACTION_NONE
:
1542 case ADG_ACTION_CHAMFER
:
1544 case ADG_ACTION_FILLET
:
1552 _adg_get_named_pair(AdgModel
*model
, const gchar
*name
,
1553 CpmlPair
*pair
, gpointer user_data
)
1555 GSList
**named_pairs
;
1556 AdgNamedPair
*named_pair
;
1558 named_pairs
= user_data
;
1560 named_pair
= g_new(AdgNamedPair
, 1);
1561 named_pair
->name
= name
;
1562 named_pair
->pair
= *pair
;
1564 *named_pairs
= g_slist_prepend(*named_pairs
, named_pair
);
1568 _adg_dup_reverse_named_pairs(AdgModel
*model
, const cairo_matrix_t
*matrix
)
1570 AdgNamedPair
*old_named_pair
;
1571 AdgNamedPair named_pair
;
1572 GSList
*named_pairs
;
1574 /* Populate named_pairs with all the named pairs of model */
1576 adg_model_foreach_named_pair(model
, _adg_get_named_pair
, &named_pairs
);
1578 /* Readd the pairs applying the reversing transformation matrix to
1579 * their coordinates and prepending a "-" to their name */
1580 while (named_pairs
) {
1581 old_named_pair
= named_pairs
->data
;
1583 named_pair
.name
= g_strdup_printf("-%s", old_named_pair
->name
);
1584 named_pair
.pair
= old_named_pair
->pair
;
1585 cpml_pair_transform(&named_pair
.pair
, matrix
);
1587 adg_model_set_named_pair(model
, named_pair
.name
, &named_pair
.pair
);
1589 g_free((gpointer
) named_pair
.name
);
1590 named_pairs
= g_slist_delete_link(named_pairs
, named_pairs
);