1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2017 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(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 g_type_class_add_private(klass
, sizeof(AdgPathPrivate
));
140 gobject_class
->finalize
= _adg_finalize
;
142 model_class
->clear
= _adg_clear
;
143 model_class
->changed
= _adg_changed
;
145 trail_class
->get_cairo_path
= _adg_get_cairo_path
;
149 adg_path_init(AdgPath
*path
)
151 AdgPathPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(path
, ADG_TYPE_PATH
,
154 data
->cp_is_valid
= FALSE
;
157 data
->cairo
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
158 data
->cairo
.path
.data
= NULL
;
159 data
->cairo
.path
.num_data
= 0;
160 data
->cairo
.array
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
161 data
->last
.segment
= NULL
;
162 data
->last
.org
= NULL
;
163 data
->last
.data
= NULL
;
164 data
->over
.segment
= NULL
;
165 data
->over
.org
= NULL
;
166 data
->over
.data
= NULL
;
167 data
->operation
.action
= ADG_ACTION_NONE
;
173 _adg_finalize(GObject
*object
)
176 AdgPathPrivate
*data
;
178 path
= (AdgPath
*) object
;
181 g_array_free(data
->cairo
.array
, TRUE
);
182 _adg_clear_operation(path
);
184 if (_ADG_OLD_OBJECT_CLASS
->finalize
)
185 _ADG_OLD_OBJECT_CLASS
->finalize(object
);
192 * Creates a new path model. The path should be constructed
193 * programmatically by using the methods provided by #AdgPath.
195 * Returns: the newly created path model
202 return g_object_new(ADG_TYPE_PATH
, NULL
);
206 * adg_path_get_current_point:
209 * Gets the current point of @path, which is conceptually the
210 * final point reached by the path so far.
212 * If there is no defined current point, <constant>NULL</constant> is returned.
213 * It is possible to check this in advance with
214 * adg_path_has_current_point().
216 * Most #AdgPath methods alter the current point and most of them
217 * expect a current point to be defined otherwise will fail triggering
218 * a warning. Check the description of every method for specific details.
220 * Returns: (transfer none): the current point or <constant>NULL</constant> on no current point set or errors.
225 adg_path_get_current_point(AdgPath
*path
)
227 AdgPathPrivate
*data
;
229 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
233 if (!data
->cp_is_valid
)
240 * adg_path_has_current_point:
243 * Returns whether a current point is defined on @path.
244 * See adg_path_get_current_point() for details on the current point.
246 * Returns: whether a current point is defined
251 adg_path_has_current_point(AdgPath
*path
)
253 AdgPathPrivate
*data
;
255 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
259 return data
->cp_is_valid
;
263 * adg_path_last_primitive:
266 * Gets the last primitive appended to @path. The #CPML_MOVE type is
267 * not considered a full-fledged primitive, i.e. adg_path_move_to()
268 * or similar does not change the last primitive.
270 * The returned struct is owned by @path and should not be freed or
273 * Returns: (transfer none): a pointer to the last appended primitive or <constant>NULL</constant> on no last primitive or on errors.
277 const CpmlPrimitive
*
278 adg_path_last_primitive(AdgPath
*path
)
280 AdgPathPrivate
*data
;
282 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
286 /* Directly return NULL instead of returning an undefined primitive */
287 if (data
->last
.org
== NULL
|| data
->last
.data
== NULL
)
294 * adg_path_over_primitive:
297 * Gets the primitive before the last one appended to @path. The
298 * "over" term comes from forth, where the <emphasis>OVER</emphasis>
299 * operator works on the stack in the same way as
300 * adg_path_over_primitive() works on @path. The #CPML_MOVE type is
301 * not considered a full-fledged primitive, i.e. adg_path_move_to()
302 * or similar does not change the over primitive.
304 * The returned struct is owned by @path and should not be freed or
307 * Returns: (transfer none): a pointer to the primitive before the last appended one or <constant>NULL</constant> on errors.
311 const CpmlPrimitive
*
312 adg_path_over_primitive(AdgPath
*path
)
314 AdgPathPrivate
*data
;
316 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
320 /* Directly return NULL instead of returning an undefined primitive */
321 if (data
->over
.org
== NULL
|| data
->over
.data
== NULL
)
330 * @type: (type CpmlPrimitiveType): a #cairo_data_type_t value
331 * @...: point data, specified as #CpmlPair pointers
333 * Generic method to append a primitive to @path. The number of #CpmlPair
334 * pointers to pass as @Varargs depends on @type: %CPML_CLOSE does not
335 * require any pair, %CPML_MOVE and %CPML_LINE require one pair,
336 * %CPML_ARC two pairs, %CPML_CURVE three pairs and so on.
338 * All the needed pairs must be not <constant>NULL</constant> pointers,
339 * otherwise the function will fail. The pairs in excess, if any, are ignored.
344 adg_path_append(AdgPath
*path
, gint type
, ...)
348 va_start(var_args
, type
);
349 adg_path_append_valist(path
, (CpmlPrimitiveType
) type
, var_args
);
354 * adg_path_append_valist:
356 * @type: a #cairo_data_type_t value
357 * @var_args: point data, specified as #CpmlPair pointers
359 * va_list version of adg_path_append().
364 adg_path_append_valist(AdgPath
*path
, CpmlPrimitiveType type
, va_list var_args
)
367 const CpmlPair
*pair
;
370 length
= _adg_primitive_length(type
);
374 array
= g_ptr_array_sized_new(4);
376 pair
= va_arg(var_args
, const CpmlPair
*);
378 g_ptr_array_free(array
, TRUE
);
379 g_return_if_reached();
382 g_ptr_array_add(array
, (gpointer
) pair
);
385 /* The array must be NULL terminated */
386 g_ptr_array_add(array
, NULL
);
388 adg_path_append_array(path
, type
, (const CpmlPair
**) array
->pdata
);
390 g_ptr_array_free(array
, TRUE
);
394 * adg_path_append_array: (rename-to adg_path_append)
396 * @type: a #cairo_data_type_t value
397 * @pairs: (array zero-terminated=1) (element-type Cpml.Pair) (transfer none): point data, specified as a <constant>NULL</constant> terminated array of #CpmlPair pointers.
399 * A bindingable version of adg_path_append() that uses a
400 * <constant>NULL</constant> terminated array of pairs instead of variable
401 * argument list and friends.
403 * Furthermore, because of the list is <constant>NULL</constant> terminated,
404 * an arbitrary number of pairs can be passed in @pairs. This allows to embed
405 * in a primitive element more data pairs than requested, something impossible
406 * to do with adg_path_append() and adg_path_append_valist().
411 adg_path_append_array(AdgPath
*path
, CpmlPrimitiveType type
,
412 const CpmlPair
**pairs
)
416 const CpmlPair
**pair
;
417 cairo_path_data_t path_data
;
419 g_return_if_fail(ADG_IS_PATH(path
));
420 g_return_if_fail(pairs
!= NULL
);
422 length
= _adg_primitive_length(type
);
426 array
= g_array_new(FALSE
, FALSE
, sizeof(path_data
));
427 for (pair
= pairs
; *pair
!= NULL
; ++ pair
) {
428 cpml_pair_to_cairo(*pair
, &path_data
);
429 g_array_append_val(array
, path_data
);
432 if (array
->len
< length
- 1) {
433 /* Not enough pairs have been provided */
434 g_warning(_("%s: null pair caught while parsing arguments"), G_STRLOC
);
436 AdgPathPrivate
*data
;
437 CpmlPrimitive primitive
;
438 cairo_path_data_t org
;
440 /* Save a copy of the current point as primitive origin */
442 cpml_pair_to_cairo(&data
->cp
, &org
);
444 /* Prepend the cairo header */
445 path_data
.header
.type
= type
;
446 path_data
.header
.length
= array
->len
+ 1;
447 g_array_prepend_val(array
, path_data
);
449 /* Append a new primitive to @path */
450 primitive
.segment
= NULL
;
451 primitive
.org
= &org
;
452 primitive
.data
= (cairo_path_data_t
*) array
->data
;
453 _adg_append_primitive(path
, &primitive
);
456 g_array_free(array
, TRUE
);
461 * adg_path_append_primitive:
463 * @primitive: the #CpmlPrimitive to append
465 * Appends @primitive to @path. The primitive to add is considered the
466 * continuation of the current path so the <structfield>org</structfield>
467 * component of @primitive is not used. Anyway the current point is
468 * checked against it: they must be equal or the function will fail
469 * without further processing.
474 adg_path_append_primitive(AdgPath
*path
, const CpmlPrimitive
*primitive
)
476 AdgPathPrivate
*data
;
477 CpmlPrimitive
*primitive_dup
;
479 g_return_if_fail(ADG_IS_PATH(path
));
480 g_return_if_fail(primitive
!= NULL
);
481 g_return_if_fail(primitive
->org
!= NULL
);
482 g_return_if_fail(primitive
->data
!= NULL
);
486 g_return_if_fail(primitive
->org
->point
.x
== data
->cp
.x
&&
487 primitive
->org
->point
.y
== data
->cp
.y
);
489 /* The primitive data could be modified by pending operations:
491 primitive_dup
= cpml_primitive_deep_dup(primitive
);
493 _adg_append_primitive(path
, primitive_dup
);
495 g_free(primitive_dup
);
499 * adg_path_append_segment:
501 * @segment: the #CpmlSegment to append
503 * Appends @segment to @path.
508 adg_path_append_segment(AdgPath
*path
, const CpmlSegment
*segment
)
510 g_return_if_fail(ADG_IS_PATH(path
));
511 g_return_if_fail(segment
!= NULL
);
513 if (segment
->num_data
> 0) {
514 AdgPathPrivate
*data
;
516 g_return_if_fail(segment
->data
!= NULL
);
520 _adg_clear_parent((AdgModel
*) path
);
521 data
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
522 segment
->data
, segment
->num_data
);
528 * adg_path_append_cairo_path:
530 * @cairo_path: the #cairo_path_t path to append
532 * Appends a whole #cairo_path_t to @path.
537 adg_path_append_cairo_path(AdgPath
*path
, const cairo_path_t
*cairo_path
)
539 AdgPathPrivate
*data
;
541 g_return_if_fail(ADG_IS_PATH(path
));
542 g_return_if_fail(cairo_path
!= NULL
);
546 _adg_clear_parent((AdgModel
*) path
);
547 data
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
549 cairo_path
->num_data
);
554 * adg_path_append_trail:
556 * @trail: an #AdgTrail instance
558 * Appends the content of @trail to @path. It is similar to
559 * adg_path_append_cairo_path() but it also appends to @path the named pairs
560 * eventually defined in @trail.
565 adg_path_append_trail(AdgPath
*path
, AdgTrail
*trail
)
568 AdgNamedPair
*named_pair
;
570 g_return_if_fail(ADG_IS_PATH(path
));
571 g_return_if_fail(ADG_IS_TRAIL(trail
));
573 adg_path_append_cairo_path(path
, adg_trail_get_cairo_path(trail
));
575 /* Populate named_pairs with all the named pairs of trail */
577 adg_model_foreach_named_pair((AdgModel
*)trail
,
578 _adg_get_named_pair
, &named_pairs
);
580 /* Readd the pairs to path */
581 while (named_pairs
) {
582 named_pair
= (AdgNamedPair
*) named_pairs
->data
;
583 adg_model_set_named_pair((AdgModel
*) path
,
584 named_pair
->name
, &named_pair
->pair
);
585 named_pairs
= g_slist_delete_link(named_pairs
, named_pairs
);
590 * adg_path_remove_primitive:
593 * Removes the last primitive from @path.
598 adg_path_remove_primitive(AdgPath
*path
)
600 const CpmlPrimitive
*over
;
603 g_return_if_fail(ADG_IS_PATH(path
));
605 AdgPathPrivate
*data
= path
->data
;
607 over
= adg_path_over_primitive(path
);
609 cairo_path_data_t
*end
= over
->data
+ over
->data
->header
.length
;
610 len
= end
- (cairo_path_data_t
*) (data
->cairo
.array
)->data
;
615 /* Resize the data array */
616 g_array_set_size(data
->cairo
.array
, len
);
618 /* Rescan path to compute the new over and last primitives */
619 _adg_clear_parent((AdgModel
*) path
);
626 * @pair: the destination coordinates
628 * Begins a new segment. After this call the current point will be @pair.
633 adg_path_move_to(AdgPath
*path
, const CpmlPair
*pair
)
635 adg_path_append(path
, CPML_MOVE
, pair
);
639 * adg_path_move_to_explicit:
641 * @x: the new x coordinate
642 * @y: the new y coordinate
644 * Convenient function to call adg_path_move_to() using explicit
645 * coordinates instead of #CpmlPair.
650 adg_path_move_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
657 adg_path_append(path
, CPML_MOVE
, &p
);
663 * @pair: the destination coordinates
665 * Adds a line to @path from the current point to @pair. After this
666 * call the current point will be @pair.
668 * If @path has no current point before this call, this function will
669 * trigger a warning without other effect.
674 adg_path_line_to(AdgPath
*path
, const CpmlPair
*pair
)
676 adg_path_append(path
, CPML_LINE
, pair
);
680 * adg_path_line_to_explicit:
682 * @x: the new x coordinate
683 * @y: the new y coordinate
685 * Convenient function to call adg_path_line_to() using explicit
686 * coordinates instead of #CpmlPair.
691 adg_path_line_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
698 adg_path_append(path
, CPML_LINE
, &p
);
704 * @throught: an arbitrary point on the arc
705 * @pair: the destination coordinates
707 * Adds an arc to the path from the current point to @pair, passing
708 * throught @throught. After this call the current point will be @pair.
710 * If @path has no current point before this call, this function will
711 * trigger a warning without other effect.
716 adg_path_arc_to(AdgPath
*path
, const CpmlPair
*throught
, const CpmlPair
*pair
)
718 adg_path_append(path
, CPML_ARC
, throught
, pair
);
722 * adg_path_arc_to_explicit:
724 * @x1: the x coordinate of an intermediate point
725 * @y1: the y coordinate of an intermediate point
726 * @x2: the x coordinate of the end of the arc
727 * @y2: the y coordinate of the end of the arc
729 * Convenient function to call adg_path_arc_to() using explicit
730 * coordinates instead of #CpmlPair.
735 adg_path_arc_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
736 gdouble x2
, gdouble y2
)
745 adg_path_append(path
, CPML_ARC
, &p
[0], &p
[1]);
751 * @control1: the first control point of the curve
752 * @control2: the second control point of the curve
753 * @pair: the destination coordinates
755 * Adds a cubic Bézier curve to the path from the current point to
756 * position @pair, using @control1 and @control2 as control points.
757 * After this call the current point will be @pair.
759 * If @path has no current point before this call, this function will
760 * trigger a warning without other effect.
765 adg_path_curve_to(AdgPath
*path
, const CpmlPair
*control1
,
766 const CpmlPair
*control2
, const CpmlPair
*pair
)
768 adg_path_append(path
, CPML_CURVE
, control1
, control2
, pair
);
772 * adg_path_curve_to_explicit:
774 * @x1: the x coordinate of the first control point
775 * @y1: the y coordinate of the first control point
776 * @x2: the x coordinate of the second control point
777 * @y2: the y coordinate of the second control point
778 * @x3: the x coordinate of the end of the curve
779 * @y3: the y coordinate of the end of the curve
781 * Convenient function to call adg_path_curve_to() using explicit
782 * coordinates instead of #CpmlPair.
787 adg_path_curve_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
788 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
799 adg_path_append(path
, CPML_CURVE
, &p
[0], &p
[1], &p
[2]);
806 * Adds a line segment to the path from the current point to the
807 * beginning of the current segment, (the most recent point passed
808 * to an adg_path_move_to()), and closes this segment.
809 * After this call the current point will be unset.
811 * The behavior of adg_path_close() is distinct from simply calling
812 * adg_path_line_to() with the coordinates of the segment starting
813 * point. When a closed segment is stroked, there are no caps on the
814 * ends. Instead, there is a line join connecting the final and
815 * initial primitive of the segment.
817 * If @path has no current point before this call, this function will
818 * trigger a warning without other effect.
823 adg_path_close(AdgPath
*path
)
825 adg_path_append(path
, CPML_CLOSE
);
831 * @center: coordinates of the center of the arc
832 * @r: the radius of the arc
833 * @start: the start angle, in radians
834 * @end: the end angle, in radians
836 * A more usual way to add an arc to @path. After this call, the current
837 * point will be the computed end point of the arc. The arc will be
838 * rendered in increasing angle, accordling to @start and @end. This means
839 * if @start is less than @end, the arc will be rendered in clockwise
840 * direction (accordling to the default cairo coordinate system) while if
841 * @start is greather than @end, the arc will be rendered in couterclockwise
844 * By explicitely setting the whole arc data, the start point could be
845 * different from the current point. In this case, if @path has no
846 * current point before the call a %CPML_MOVE to the start point of
847 * the arc will be automatically prepended to the arc. If @path has a
848 * current point, a %CPML_LINE to the start point of the arc will be
849 * used instead of the "move to" primitive.
854 adg_path_arc(AdgPath
*path
, const CpmlPair
*center
, gdouble r
,
855 gdouble start
, gdouble end
)
857 AdgPathPrivate
*data
;
860 g_return_if_fail(ADG_IS_PATH(path
));
861 g_return_if_fail(center
!= NULL
);
864 cpml_vector_from_angle(&p
[0], start
);
865 cpml_vector_from_angle(&p
[1], (start
+end
) / 2);
866 cpml_vector_from_angle(&p
[2], end
);
868 cpml_vector_set_length(&p
[0], r
);
869 cpml_vector_set_length(&p
[1], r
);
870 cpml_vector_set_length(&p
[2], r
);
879 if (!data
->cp_is_valid
)
880 adg_path_append(path
, CPML_MOVE
, &p
[0]);
881 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
882 adg_path_append(path
, CPML_LINE
, &p
[0]);
884 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
888 * adg_path_arc_explicit:
890 * @xc: x position of the center of the arc
891 * @yc: y position of the center of the arc
892 * @r: the radius of the arc
893 * @start: the start angle, in radians
894 * @end: the end angle, in radians
896 * Convenient function to call adg_path_arc() using explicit
897 * coordinates instead of #CpmlPair.
902 adg_path_arc_explicit(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
903 gdouble start
, gdouble end
)
910 adg_path_arc(path
, ¢er
, r
, start
, end
);
916 * @delta1: the distance from the intersection point of the current primitive
917 * @delta2: the distance from the intersection point of the next primitive
919 * A binary action that generates a chamfer between two primitives.
920 * The first primitive involved is the current primitive, the second will
921 * be the next primitive appended to @path after this call. The second
922 * primitive is required: if the chamfer operation is not properly
923 * terminated (by not providing the second primitive), any API accessing
924 * the path in reading mode will raise a warning.
926 * An exception is a chamfer after a %CPML_CLOSE primitive. In this case,
927 * the second primitive is not required: the current close path is used
928 * as first operand while the first primitive of the current segment is
929 * used as second operand.
931 * The chamfer operation requires two lengths: @delta1 specifies the
932 * "quantity" to trim on the first primitive while @delta2 is the same
933 * applied on the second primitive. The term "quantity" means the length
934 * of the portion to cut out from the original primitive (that is the
935 * primitive as would be without the chamfer).
940 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
942 g_return_if_fail(ADG_IS_PATH(path
));
944 if (!_adg_append_operation(path
, ADG_ACTION_CHAMFER
, delta1
, delta2
))
951 * @radius: the radius of the fillet
953 * A binary action that joins to primitives with an arc.
954 * The first primitive involved is the current primitive, the second will
955 * be the next primitive appended to @path after this call. The second
956 * primitive is required: if the fillet operation is not properly
957 * terminated (by not providing the second primitive), any API accessing
958 * the path in reading mode will raise a warning.
960 * An exception is a fillet after a %CPML_CLOSE primitive. In this case,
961 * the second primitive is not required: the current close path is used
962 * as first operand while the first primitive of the current segment is
963 * used as second operand.
968 adg_path_fillet(AdgPath
*path
, gdouble radius
)
970 g_return_if_fail(ADG_IS_PATH(path
));
972 if (!_adg_append_operation(path
, ADG_ACTION_FILLET
, radius
))
980 * Joins all the segments of @path. After the call there will be only one
983 * This operation is roughly equivalent to converting embedded %CPML_MOVE
984 * primitives into %CPML_LINE ones.
989 adg_path_join(AdgPath
*path
)
991 cairo_path_t
*cairo_path
;
992 cairo_path_data_t
*data
;
995 g_return_if_fail(ADG_IS_PATH(path
));
997 cairo_path
= _adg_read_cairo_path(path
);
999 data
= cairo_path
->data
;
1001 while (data
- cairo_path
->data
< cairo_path
->num_data
) {
1002 if (data
->header
.type
!= CPML_MOVE
) {
1004 } else if (pen_down
) {
1005 data
->header
.type
= CPML_LINE
;
1007 data
+= data
->header
.length
;
1013 * @path: an #AdgPath
1014 * @vector: (allow-none): the slope of the axis
1016 * Reflects the first segment or @path around the axis passing
1017 * throught (0, 0) and with a @vector slope. The internal segment
1018 * is duplicated and the proper transformation (computed from
1019 * @vector) to mirror the segment is applied on all its points.
1020 * The result is then reversed with cpml_segment_reverse() and
1021 * appended to the original path with adg_path_append_segment().
1023 * For convenience, if @vector is <constant>NULL</constant> the
1024 * path is reversed around the x axis <constant>(y = 0)</constant>.
1029 adg_path_reflect(AdgPath
*path
, const CpmlVector
*vector
)
1033 cairo_matrix_t matrix
;
1034 CpmlSegment segment
, *dup_segment
;
1037 g_return_if_fail(ADG_IS_PATH(path
));
1038 g_return_if_fail(vector
== NULL
|| vector
->x
!= 0 || vector
->y
!= 0);
1040 model
= (AdgModel
*) path
;
1041 trail
= (AdgTrail
*) path
;
1043 if (vector
== NULL
) {
1044 cairo_matrix_init_scale(&matrix
, 1, -1);
1047 gdouble cos2angle
, sin2angle
;
1049 cpml_pair_copy(&slope
, vector
);
1050 cpml_vector_set_length(&slope
, 1);
1052 if (slope
.x
== 0 && slope
.y
== 0) {
1053 g_warning(_("%s: the axis of the reflection is not known"),
1058 sin2angle
= 2. * vector
->x
* vector
->y
;
1059 cos2angle
= 2. * vector
->x
* vector
->x
- 1;
1061 cairo_matrix_init(&matrix
, cos2angle
, sin2angle
,
1062 sin2angle
, -cos2angle
, 0, 0);
1065 for (n
= adg_trail_n_segments(trail
); n
> 0; --n
) {
1066 adg_trail_put_segment(trail
, n
, &segment
);
1068 /* No need to reverse an empty segment */
1069 if (segment
.num_data
== 0 || segment
.num_data
== 0)
1072 dup_segment
= cpml_segment_deep_dup(&segment
);
1073 if (dup_segment
== NULL
)
1076 cpml_segment_reverse(dup_segment
);
1077 cpml_segment_transform(dup_segment
, &matrix
);
1078 dup_segment
->data
[0].header
.type
= CPML_MOVE
;
1080 adg_path_append_segment(path
, dup_segment
);
1082 g_free(dup_segment
);
1085 _adg_dup_reverse_named_pairs(model
, &matrix
);
1089 * adg_path_reflect_explicit:
1090 * @path: an #AdgPath
1091 * @x: the vector x component
1092 * @y: the vector y component
1094 * Convenient function to call adg_path_reflect() using explicit
1095 * vector components instead of #CpmlVector.
1100 adg_path_reflect_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
1107 adg_path_reflect(path
, &vector
);
1112 _adg_clear(AdgModel
*model
)
1115 AdgPathPrivate
*data
;
1117 path
= (AdgPath
*) model
;
1120 g_array_set_size(data
->cairo
.array
, 0);
1121 _adg_clear_operation(path
);
1122 _adg_clear_parent(model
);
1126 _adg_clear_parent(AdgModel
*model
)
1128 if (_ADG_OLD_MODEL_CLASS
->clear
)
1129 _ADG_OLD_MODEL_CLASS
->clear(model
);
1133 _adg_changed(AdgModel
*model
)
1135 _adg_clear_parent(model
);
1137 if (_ADG_OLD_MODEL_CLASS
->changed
)
1138 _ADG_OLD_MODEL_CLASS
->changed(model
);
1141 static cairo_path_t
*
1142 _adg_get_cairo_path(AdgTrail
*trail
)
1144 _adg_clear_parent((AdgModel
*) trail
);
1145 return _adg_read_cairo_path((AdgPath
*) trail
);
1148 static cairo_path_t
*
1149 _adg_read_cairo_path(AdgPath
*path
)
1151 AdgPathPrivate
*data
= path
->data
;
1152 cairo_path_t
*cairo_path
= &data
->cairo
.path
;
1153 GArray
*array
= data
->cairo
.array
;
1155 /* Always regenerate the cairo_path_t as it is a trivial operation */
1156 cairo_path
->status
= CAIRO_STATUS_SUCCESS
;
1157 cairo_path
->data
= (cairo_path_data_t
*) array
->data
;
1158 cairo_path
->num_data
= array
->len
;
1164 _adg_primitive_length(CpmlPrimitiveType type
)
1175 return cpml_primitive_type_get_n_points(type
);
1180 _adg_primitive_remap(CpmlPrimitive
*primitive
, gpointer to
,
1181 const CpmlPrimitive
*old
, gconstpointer from
)
1183 primitive
->org
= REMAPPED(old
->org
, from
, to
);
1184 primitive
->segment
= REMAPPED(old
->segment
, from
, to
);
1185 primitive
->data
= REMAPPED(old
->data
, from
, to
);
1189 _adg_rescan(AdgPath
*path
)
1191 AdgPathPrivate
*data
;
1192 CpmlSegment segment
;
1193 CpmlPrimitive current
, *last
, *over
;
1199 last
->segment
= NULL
;
1202 over
->segment
= NULL
;
1206 /* When no data is present, just bail out */
1207 if (! cpml_segment_from_cairo(&segment
, _adg_read_cairo_path(path
)))
1211 cpml_primitive_from_segment(¤t
, &segment
);
1213 cpml_primitive_copy(over
, last
);
1214 cpml_primitive_copy(last
, ¤t
);
1215 } while (cpml_primitive_next(¤t
));
1216 } while (cpml_segment_next(&segment
));
1220 _adg_append_primitive(AdgPath
*path
, CpmlPrimitive
*current
)
1222 AdgPathPrivate
*data
;
1223 cairo_path_data_t
*path_data
;
1224 CpmlPrimitiveType type
;
1226 gconstpointer old_data
;
1230 path_data
= current
->data
;
1231 length
= path_data
->header
.length
;
1232 type
= path_data
->header
.type
;
1234 /* Execute any pending operation */
1235 _adg_do_operation(path
, path_data
);
1237 /* Append the path data to the internal path array */
1238 old_data
= (data
->cairo
.array
)->data
;
1239 data
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
1241 new_data
= (data
->cairo
.array
)->data
;
1243 /* Set path data to point to the recently appended cairo_path_data_t
1244 * primitive: the first struct is the header */
1245 path_data
= (cairo_path_data_t
*) new_data
+
1246 (data
->cairo
.array
)->len
- length
;
1248 if (type
== CPML_MOVE
) {
1249 /* Remap last and over, but do not change their content */
1250 _adg_primitive_remap(&data
->last
, new_data
, &data
->last
, old_data
);
1251 _adg_primitive_remap(&data
->over
, new_data
, &data
->over
, old_data
);
1253 /* Store the last primitive into over */
1254 _adg_primitive_remap(&data
->over
, new_data
, &data
->last
, old_data
);
1256 /* Set the last primitive for subsequent binary operations */
1257 /* TODO: the assumption path_data - 1 is the last point is not true
1258 * e.g. when there are embedded data in primitives */
1259 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
1260 data
->last
.segment
= NULL
;
1261 data
->last
.data
= path_data
;
1264 data
->cp_is_valid
= type
!= CPML_CLOSE
;
1265 if (data
->cp_is_valid
) {
1266 /* Save the last point in the current point */
1267 size_t n
= type
== CPML_MOVE
? 1 : cpml_primitive_type_get_n_points(type
) - 1;
1268 cpml_pair_from_cairo(&data
->cp
, &path_data
[n
]);
1271 /* Invalidate cairo_path: should be recomputed */
1272 _adg_clear_parent((AdgModel
*) path
);
1276 _adg_clear_operation(AdgPath
*path
)
1278 AdgPathPrivate
*data
;
1279 AdgOperation
*operation
;
1282 operation
= &data
->operation
;
1284 if (operation
->action
!= ADG_ACTION_NONE
) {
1285 g_warning(_("%s: a '%s' operation is still active while clearing the path"),
1286 G_STRLOC
, _adg_action_name(operation
->action
));
1287 operation
->action
= ADG_ACTION_NONE
;
1290 data
->cp_is_valid
= FALSE
;
1291 data
->last
.data
= NULL
;
1292 data
->over
.data
= NULL
;
1296 _adg_append_operation(AdgPath
*path
, gint action
, ...)
1298 AdgPathPrivate
*data
;
1299 AdgOperation
*operation
;
1300 AdgAction real_action
;
1303 real_action
= (AdgAction
) action
;
1306 if (data
->last
.data
== NULL
) {
1307 g_warning(_("%s: requested a '%s' operation on a path without current primitive"),
1308 G_STRLOC
, _adg_action_name(real_action
));
1312 operation
= &data
->operation
;
1313 if (operation
->action
!= ADG_ACTION_NONE
) {
1314 g_warning(_("%s: requested a '%s' operation while a '%s' operation was active"),
1315 G_STRLOC
, _adg_action_name(real_action
),
1316 _adg_action_name(operation
->action
));
1317 /* XXX: http://dev.entidi.com/p/adg/issues/50/ */
1321 va_start(var_args
, action
);
1323 switch (real_action
) {
1325 case ADG_ACTION_CHAMFER
:
1326 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
1327 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
1330 case ADG_ACTION_FILLET
:
1331 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
1334 case ADG_ACTION_NONE
:
1339 g_warning(_("%s: %d path operation not recognized"), G_STRLOC
, real_action
);
1344 operation
->action
= real_action
;
1347 if (data
->last
.data
[0].header
.type
== CPML_CLOSE
) {
1348 /* Special case: an action with the close primitive should
1349 * be resolved now by changing the close primitive to a
1350 * line-to and using it as second operand and use the first
1351 * primitive of the current segment as first operand */
1353 cairo_path_data_t
*path_data
;
1354 CpmlSegment segment
;
1355 CpmlPrimitive current
;
1357 length
= data
->cairo
.array
->len
;
1359 /* Ensure the close path primitive is not the only data */
1360 g_return_val_if_fail(length
> 1, FALSE
);
1362 /* Allocate one more item once for all to accept the
1363 * conversion from a close to line-to primitive */
1364 data
->cairo
.array
= g_array_set_size(data
->cairo
.array
, length
+ 1);
1365 path_data
= (cairo_path_data_t
*) data
->cairo
.array
->data
;
1366 --data
->cairo
.array
->len
;
1368 /* Set segment and current (the first primitive of segment) */
1369 cpml_segment_from_cairo(&segment
, _adg_read_cairo_path(path
));
1370 while (cpml_segment_next(&segment
))
1372 cpml_primitive_from_segment(¤t
, &segment
);
1374 /* Convert close path to a line-to primitive */
1375 ++data
->cairo
.array
->len
;
1376 path_data
[length
- 1].header
.type
= CPML_LINE
;
1377 path_data
[length
- 1].header
.length
= 2;
1378 path_data
[length
] = *current
.org
;
1380 data
->last
.segment
= &segment
;
1381 data
->last
.org
= &path_data
[length
- 2];
1382 data
->last
.data
= &path_data
[length
- 1];
1384 _adg_do_action(path
, real_action
, ¤t
);
1391 _adg_do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
1393 AdgPathPrivate
*data
;
1395 CpmlSegment segment
;
1396 CpmlPrimitive current
;
1397 cairo_path_data_t current_org
;
1400 action
= data
->operation
.action
;
1401 cpml_segment_from_cairo(&segment
, _adg_read_cairo_path(path
));
1403 /* Construct the current primitive, that is the primitive to be
1404 * mixed with the last primitive with the specified operation.
1405 * Its org is a copy of the end point of the last primitive: it can be
1406 * modified without affecting anything else. It is expected the operation
1407 * functions will add to @path the primitives required but NOT to add
1408 * @current, as this one will be inserted automatically. */
1409 current
.segment
= &segment
;
1410 current
.org
= ¤t_org
;
1411 current
.data
= path_data
;
1412 cpml_pair_to_cairo(&data
->cp
, ¤t_org
);
1414 _adg_do_action(path
, action
, ¤t
);
1418 _adg_do_action(AdgPath
*path
, AdgAction action
, CpmlPrimitive
*primitive
)
1421 case ADG_ACTION_NONE
:
1423 case ADG_ACTION_CHAMFER
:
1424 _adg_do_chamfer(path
, primitive
);
1426 case ADG_ACTION_FILLET
:
1427 _adg_do_fillet(path
, primitive
);
1430 g_return_if_reached();
1435 _adg_do_chamfer(AdgPath
*path
, CpmlPrimitive
*current
)
1437 AdgPathPrivate
*data
;
1438 CpmlPrimitive
*last
;
1439 gdouble delta1
, delta2
;
1445 delta1
= data
->operation
.data
.chamfer
.delta1
;
1446 len1
= cpml_primitive_get_length(last
);
1448 if (delta1
>= len1
) {
1449 g_warning(_("%s: first chamfer delta of %lf is greather than the available %lf length"),
1450 G_STRLOC
, delta1
, len1
);
1454 delta2
= data
->operation
.data
.chamfer
.delta2
;
1455 len2
= cpml_primitive_get_length(current
);
1457 if (delta2
>= len2
) {
1458 g_warning(_("%s: second chamfer delta of %lf is greather than the available %lf length"),
1459 G_STRLOC
, delta1
, len1
);
1463 /* Change the end point of the last primitive */
1464 cpml_primitive_put_pair_at(last
, 1. - delta1
/ len1
, &pair
);
1465 cpml_primitive_set_point(last
, -1, &pair
);
1467 /* Change the start point of the current primitive */
1468 cpml_primitive_put_pair_at(current
, delta2
/ len2
, &pair
);
1469 cpml_primitive_set_point(current
, 0, &pair
);
1471 /* Add the chamfer line */
1472 data
->operation
.action
= ADG_ACTION_NONE
;
1473 adg_path_append(path
, CPML_LINE
, &pair
);
1477 _adg_do_fillet(AdgPath
*path
, CpmlPrimitive
*current
)
1479 AdgPathPrivate
*data
;
1480 CpmlPrimitive
*last
, *current_dup
, *last_dup
;
1481 gdouble radius
, offset
, pos
;
1482 CpmlPair center
, vector
, p
[3];
1486 current_dup
= cpml_primitive_deep_dup(current
);
1487 last_dup
= cpml_primitive_deep_dup(last
);
1488 radius
= data
->operation
.data
.fillet
.radius
;
1489 offset
= _adg_is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1491 /* Find the center of the fillet from the intersection between
1492 * the last and current primitives offseted by radius */
1493 cpml_primitive_offset(current_dup
, offset
);
1494 cpml_primitive_offset(last_dup
, offset
);
1495 if (cpml_primitive_put_intersections(current_dup
, last_dup
, 1, ¢er
) == 0) {
1496 g_warning(_("%s: fillet with radius of %lf is not applicable here"),
1498 g_free(current_dup
);
1503 /* Compute the start point of the fillet */
1504 pos
= cpml_primitive_get_closest_pos(last_dup
, ¢er
);
1505 cpml_primitive_put_vector_at(last_dup
, pos
, &vector
);
1506 cpml_vector_set_length(&vector
, offset
);
1507 cpml_vector_normal(&vector
);
1508 p
[0].x
= center
.x
- vector
.x
;
1509 p
[0].y
= center
.y
- vector
.y
;
1511 /* Compute the mid point of the fillet */
1512 cpml_pair_from_cairo(&vector
, current
->org
);
1513 vector
.x
-= center
.x
;
1514 vector
.y
-= center
.y
;
1515 cpml_vector_set_length(&vector
, radius
);
1516 p
[1].x
= center
.x
+ vector
.x
;
1517 p
[1].y
= center
.y
+ vector
.y
;
1519 /* Compute the end point of the fillet */
1520 pos
= cpml_primitive_get_closest_pos(current_dup
, ¢er
);
1521 cpml_primitive_put_vector_at(current_dup
, pos
, &vector
);
1522 cpml_vector_set_length(&vector
, offset
);
1523 cpml_vector_normal(&vector
);
1524 p
[2].x
= center
.x
- vector
.x
;
1525 p
[2].y
= center
.y
- vector
.y
;
1527 g_free(current_dup
);
1530 /* Change the end point of the last primitive */
1531 cpml_primitive_set_point(last
, -1, &p
[0]);
1533 /* Change the start point of the current primitive */
1534 cpml_primitive_set_point(current
, 0, &p
[2]);
1536 /* Add the fillet arc */
1537 data
->operation
.action
= ADG_ACTION_NONE
;
1538 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
1542 _adg_is_convex(const CpmlPrimitive
*primitive1
, const CpmlPrimitive
*primitive2
)
1545 gdouble angle1
, angle2
;
1547 cpml_primitive_put_vector_at(primitive1
, -1, &v1
);
1548 cpml_primitive_put_vector_at(primitive2
, 0, &v2
);
1550 /* Probably there is a smarter way to get this without trygonometry */
1551 angle1
= cpml_vector_angle(&v1
);
1552 angle2
= cpml_vector_angle(&v2
);
1554 if (angle1
> angle2
)
1557 return angle2
-angle1
> G_PI
;
1560 static const gchar
*
1561 _adg_action_name(AdgAction action
)
1564 case ADG_ACTION_NONE
:
1566 case ADG_ACTION_CHAMFER
:
1568 case ADG_ACTION_FILLET
:
1576 _adg_get_named_pair(AdgModel
*model
, const gchar
*name
,
1577 CpmlPair
*pair
, gpointer user_data
)
1579 GSList
**named_pairs
;
1580 AdgNamedPair
*named_pair
;
1582 named_pairs
= user_data
;
1584 named_pair
= g_new(AdgNamedPair
, 1);
1585 named_pair
->name
= name
;
1586 named_pair
->pair
= *pair
;
1588 *named_pairs
= g_slist_prepend(*named_pairs
, named_pair
);
1592 _adg_dup_reverse_named_pairs(AdgModel
*model
, const cairo_matrix_t
*matrix
)
1594 AdgNamedPair
*old_named_pair
;
1595 AdgNamedPair named_pair
;
1596 GSList
*named_pairs
;
1598 /* Populate named_pairs with all the named pairs of model */
1600 adg_model_foreach_named_pair(model
, _adg_get_named_pair
, &named_pairs
);
1602 /* Readd the pairs applying the reversing transformation matrix to
1603 * their coordinates and prepending a "-" to their name */
1604 while (named_pairs
) {
1605 old_named_pair
= named_pairs
->data
;
1607 named_pair
.name
= g_strdup_printf("-%s", old_named_pair
->name
);
1608 named_pair
.pair
= old_named_pair
->pair
;
1609 cpml_pair_transform(&named_pair
.pair
, matrix
);
1611 adg_model_set_named_pair(model
, named_pair
.name
, &named_pair
.pair
);
1613 g_free((gpointer
) named_pair
.name
);
1614 named_pairs
= g_slist_delete_link(named_pairs
, named_pairs
);