1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2015 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) ((to) + ((gchar *) (ptr) - (gchar *) (from)))
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
)
370 length
= _adg_primitive_length(type
);
374 array
= g_array_new(TRUE
, FALSE
, sizeof(pair
));
376 pair
= va_arg(var_args
, CpmlPair
*);
378 g_array_free(array
, TRUE
);
379 g_return_if_reached();
382 g_array_append_val(array
, pair
);
385 adg_path_append_array(path
, type
, (const CpmlPair
**) array
->data
);
386 g_array_free(array
, TRUE
);
390 * adg_path_append_array: (rename-to adg_path_append)
392 * @type: a #cairo_data_type_t value
393 * @pairs: (array zero-terminated=1) (element-type Cpml.Pair) (transfer none): point data, specified as a <constant>NULL</constant> terminated array of #CpmlPair pointers.
395 * A bindingable version of adg_path_append() that uses a
396 * <constant>NULL</constant> terminated array of pairs instead of variable
397 * argument list and friends.
399 * Furthermore, because of the list is <constant>NULL</constant> terminated,
400 * an arbitrary number of pairs can be passed in @pairs. This allows to embed
401 * in a primitive element more data pairs than requested, something impossible
402 * to do with adg_path_append() and adg_path_append_valist().
407 adg_path_append_array(AdgPath
*path
, CpmlPrimitiveType type
,
408 const CpmlPair
**pairs
)
412 const CpmlPair
**pair
;
413 cairo_path_data_t path_data
;
415 g_return_if_fail(ADG_IS_PATH(path
));
416 g_return_if_fail(pairs
!= NULL
);
418 length
= _adg_primitive_length(type
);
422 array
= g_array_new(FALSE
, FALSE
, sizeof(path_data
));
423 for (pair
= pairs
; *pair
!= NULL
; ++ pair
) {
424 cpml_pair_to_cairo(*pair
, &path_data
);
425 g_array_append_val(array
, path_data
);
428 if (array
->len
< length
- 1) {
429 /* Not enough pairs have been provided */
430 g_warning(_("%s: null pair caught while parsing arguments"), G_STRLOC
);
432 AdgPathPrivate
*data
;
433 CpmlPrimitive primitive
;
434 cairo_path_data_t org
;
436 /* Save a copy of the current point as primitive origin */
438 cpml_pair_to_cairo(&data
->cp
, &org
);
440 /* Prepend the cairo header */
441 path_data
.header
.type
= type
;
442 path_data
.header
.length
= array
->len
+ 1;
443 g_array_prepend_val(array
, path_data
);
445 /* Append a new primitive to @path */
446 primitive
.segment
= NULL
;
447 primitive
.org
= &org
;
448 primitive
.data
= (cairo_path_data_t
*) array
->data
;
449 _adg_append_primitive(path
, &primitive
);
452 g_array_free(array
, TRUE
);
457 * adg_path_append_primitive:
459 * @primitive: the #CpmlPrimitive to append
461 * Appends @primitive to @path. The primitive to add is considered the
462 * continuation of the current path so the <structfield>org</structfield>
463 * component of @primitive is not used. Anyway the current point is
464 * checked against it: they must be equal or the function will fail
465 * without further processing.
470 adg_path_append_primitive(AdgPath
*path
, const CpmlPrimitive
*primitive
)
472 AdgPathPrivate
*data
;
473 CpmlPrimitive
*primitive_dup
;
475 g_return_if_fail(ADG_IS_PATH(path
));
476 g_return_if_fail(primitive
!= NULL
);
477 g_return_if_fail(primitive
->org
!= NULL
);
478 g_return_if_fail(primitive
->data
!= NULL
);
482 g_return_if_fail(primitive
->org
->point
.x
== data
->cp
.x
&&
483 primitive
->org
->point
.y
== data
->cp
.y
);
485 /* The primitive data could be modified by pending operations:
487 primitive_dup
= cpml_primitive_deep_dup(primitive
);
489 _adg_append_primitive(path
, primitive_dup
);
491 g_free(primitive_dup
);
495 * adg_path_append_segment:
497 * @segment: the #CpmlSegment to append
499 * Appends @segment to @path.
504 adg_path_append_segment(AdgPath
*path
, const CpmlSegment
*segment
)
506 g_return_if_fail(ADG_IS_PATH(path
));
507 g_return_if_fail(segment
!= NULL
);
509 if (segment
->num_data
> 0) {
510 AdgPathPrivate
*data
;
512 g_return_if_fail(segment
->data
!= NULL
);
516 _adg_clear_parent((AdgModel
*) path
);
517 data
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
518 segment
->data
, segment
->num_data
);
524 * adg_path_append_cairo_path:
526 * @cairo_path: (type gpointer): the #cairo_path_t path to append
528 * Appends a whole #cairo_path_t to @path.
533 adg_path_append_cairo_path(AdgPath
*path
, const cairo_path_t
*cairo_path
)
535 AdgPathPrivate
*data
;
537 g_return_if_fail(ADG_IS_PATH(path
));
538 g_return_if_fail(cairo_path
!= NULL
);
542 _adg_clear_parent((AdgModel
*) path
);
543 data
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
545 cairo_path
->num_data
);
552 * @pair: the destination coordinates
554 * Begins a new segment. After this call the current point will be @pair.
559 adg_path_move_to(AdgPath
*path
, const CpmlPair
*pair
)
561 adg_path_append(path
, CPML_MOVE
, pair
);
565 * adg_path_move_to_explicit:
567 * @x: the new x coordinate
568 * @y: the new y coordinate
570 * Convenient function to call adg_path_move_to() using explicit
571 * coordinates instead of #CpmlPair.
576 adg_path_move_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
583 adg_path_append(path
, CPML_MOVE
, &p
);
589 * @pair: the destination coordinates
591 * Adds a line to @path from the current point to @pair. After this
592 * call the current point will be @pair.
594 * If @path has no current point before this call, this function will
595 * trigger a warning without other effect.
600 adg_path_line_to(AdgPath
*path
, const CpmlPair
*pair
)
602 adg_path_append(path
, CPML_LINE
, pair
);
606 * adg_path_line_to_explicit:
608 * @x: the new x coordinate
609 * @y: the new y coordinate
611 * Convenient function to call adg_path_line_to() using explicit
612 * coordinates instead of #CpmlPair.
617 adg_path_line_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
624 adg_path_append(path
, CPML_LINE
, &p
);
630 * @throught: an arbitrary point on the arc
631 * @pair: the destination coordinates
633 * Adds an arc to the path from the current point to @pair, passing
634 * throught @throught. After this call the current point will be @pair.
636 * If @path has no current point before this call, this function will
637 * trigger a warning without other effect.
642 adg_path_arc_to(AdgPath
*path
, const CpmlPair
*throught
, const CpmlPair
*pair
)
644 adg_path_append(path
, CPML_ARC
, throught
, pair
);
648 * adg_path_arc_to_explicit:
650 * @x1: the x coordinate of an intermediate point
651 * @y1: the y coordinate of an intermediate point
652 * @x2: the x coordinate of the end of the arc
653 * @y2: the y coordinate of the end of the arc
655 * Convenient function to call adg_path_arc_to() using explicit
656 * coordinates instead of #CpmlPair.
661 adg_path_arc_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
662 gdouble x2
, gdouble y2
)
671 adg_path_append(path
, CPML_ARC
, &p
[0], &p
[1]);
677 * @control1: the first control point of the curve
678 * @control2: the second control point of the curve
679 * @pair: the destination coordinates
681 * Adds a cubic Bézier curve to the path from the current point to
682 * position @pair, using @control1 and @control2 as control points.
683 * After this call the current point will be @pair.
685 * If @path has no current point before this call, this function will
686 * trigger a warning without other effect.
691 adg_path_curve_to(AdgPath
*path
, const CpmlPair
*control1
,
692 const CpmlPair
*control2
, const CpmlPair
*pair
)
694 adg_path_append(path
, CPML_CURVE
, control1
, control2
, pair
);
698 * adg_path_curve_to_explicit:
700 * @x1: the x coordinate of the first control point
701 * @y1: the y coordinate of the first control point
702 * @x2: the x coordinate of the second control point
703 * @y2: the y coordinate of the second control point
704 * @x3: the x coordinate of the end of the curve
705 * @y3: the y coordinate of the end of the curve
707 * Convenient function to call adg_path_curve_to() using explicit
708 * coordinates instead of #CpmlPair.
713 adg_path_curve_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
714 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
725 adg_path_append(path
, CPML_CURVE
, &p
[0], &p
[1], &p
[2]);
732 * Adds a line segment to the path from the current point to the
733 * beginning of the current segment, (the most recent point passed
734 * to an adg_path_move_to()), and closes this segment.
735 * After this call the current point will be unset.
737 * The behavior of adg_path_close() is distinct from simply calling
738 * adg_path_line_to() with the coordinates of the segment starting
739 * point. When a closed segment is stroked, there are no caps on the
740 * ends. Instead, there is a line join connecting the final and
741 * initial primitive of the segment.
743 * If @path has no current point before this call, this function will
744 * trigger a warning without other effect.
749 adg_path_close(AdgPath
*path
)
751 adg_path_append(path
, CPML_CLOSE
);
757 * @center: coordinates of the center of the arc
758 * @r: the radius of the arc
759 * @start: the start angle, in radians
760 * @end: the end angle, in radians
762 * A more usual way to add an arc to @path. After this call, the current
763 * point will be the computed end point of the arc. The arc will be
764 * rendered in increasing angle, accordling to @start and @end. This means
765 * if @start is less than @end, the arc will be rendered in clockwise
766 * direction (accordling to the default cairo coordinate system) while if
767 * @start is greather than @end, the arc will be rendered in couterclockwise
770 * By explicitely setting the whole arc data, the start point could be
771 * different from the current point. In this case, if @path has no
772 * current point before the call a %CPML_MOVE to the start point of
773 * the arc will be automatically prepended to the arc. If @path has a
774 * current point, a %CPML_LINE to the start point of the arc will be
775 * used instead of the "move to" primitive.
780 adg_path_arc(AdgPath
*path
, const CpmlPair
*center
, gdouble r
,
781 gdouble start
, gdouble end
)
783 AdgPathPrivate
*data
;
786 g_return_if_fail(ADG_IS_PATH(path
));
787 g_return_if_fail(center
!= NULL
);
790 cpml_vector_from_angle(&p
[0], start
);
791 cpml_vector_from_angle(&p
[1], (end
-start
) / 2);
792 cpml_vector_from_angle(&p
[2], end
);
794 cpml_vector_set_length(&p
[0], r
);
795 cpml_vector_set_length(&p
[1], r
);
796 cpml_vector_set_length(&p
[2], r
);
805 if (!data
->cp_is_valid
)
806 adg_path_append(path
, CPML_MOVE
, &p
[0]);
807 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
808 adg_path_append(path
, CPML_LINE
, &p
[0]);
810 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
814 * adg_path_arc_explicit:
816 * @xc: x position of the center of the arc
817 * @yc: y position of the center of the arc
818 * @r: the radius of the arc
819 * @start: the start angle, in radians
820 * @end: the end angle, in radians
822 * Convenient function to call adg_path_arc() using explicit
823 * coordinates instead of #CpmlPair.
828 adg_path_arc_explicit(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
829 gdouble start
, gdouble end
)
836 adg_path_arc(path
, ¢er
, r
, start
, end
);
842 * @delta1: the distance from the intersection point of the current primitive
843 * @delta2: the distance from the intersection point of the next primitive
845 * A binary action that generates a chamfer between two primitives.
846 * The first primitive involved is the current primitive, the second will
847 * be the next primitive appended to @path after this call. The second
848 * primitive is required: if the chamfer operation is not properly
849 * terminated (by not providing the second primitive), any API accessing
850 * the path in reading mode will raise a warning.
852 * An exception is a chamfer after a %CPML_CLOSE primitive. In this case,
853 * the second primitive is not required: the current close path is used
854 * as first operand while the first primitive of the current segment is
855 * used as second operand.
857 * The chamfer operation requires two lengths: @delta1 specifies the
858 * "quantity" to trim on the first primitive while @delta2 is the same
859 * applied on the second primitive. The term "quantity" means the length
860 * of the portion to cut out from the original primitive (that is the
861 * primitive as would be without the chamfer).
866 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
868 g_return_if_fail(ADG_IS_PATH(path
));
870 if (!_adg_append_operation(path
, ADG_ACTION_CHAMFER
, delta1
, delta2
))
877 * @radius: the radius of the fillet
879 * A binary action that joins to primitives with an arc.
880 * The first primitive involved is the current primitive, the second will
881 * be the next primitive appended to @path after this call. The second
882 * primitive is required: if the fillet operation is not properly
883 * terminated (by not providing the second primitive), any API accessing
884 * the path in reading mode will raise a warning.
886 * An exception is a fillet after a %CPML_CLOSE primitive. In this case,
887 * the second primitive is not required: the current close path is used
888 * as first operand while the first primitive of the current segment is
889 * used as second operand.
894 adg_path_fillet(AdgPath
*path
, gdouble radius
)
896 g_return_if_fail(ADG_IS_PATH(path
));
898 if (!_adg_append_operation(path
, ADG_ACTION_FILLET
, radius
))
905 * @vector: (allow-none): the slope of the axis
907 * Reflects the first segment or @path around the axis passing
908 * throught (0, 0) and with a @vector slope. The internal segment
909 * is duplicated and the proper transformation (computed from
910 * @vector) to mirror the segment is applied on all its points.
911 * The result is then reversed with cpml_segment_reverse() and
912 * appended to the original path with adg_path_append_segment().
914 * For convenience, if @vector is <constant>NULL</constant> the
915 * path is reversed around the x axis <constant>(y = 0)</constant>.
920 adg_path_reflect(AdgPath
*path
, const CpmlVector
*vector
)
923 cairo_matrix_t matrix
;
924 CpmlSegment segment
, *dup_segment
;
926 g_return_if_fail(ADG_IS_PATH(path
));
927 g_return_if_fail(vector
== NULL
|| vector
->x
!= 0 || vector
->y
!= 0);
929 model
= (AdgModel
*) path
;
931 if (vector
== NULL
) {
932 cairo_matrix_init_scale(&matrix
, 1, -1);
935 gdouble cos2angle
, sin2angle
;
937 cpml_pair_copy(&slope
, vector
);
938 cpml_vector_set_length(&slope
, 1);
940 if (slope
.x
== 0 && slope
.y
== 0) {
941 g_warning(_("%s: the axis of the reflection is not known"),
946 sin2angle
= 2. * vector
->x
* vector
->y
;
947 cos2angle
= 2. * vector
->x
* vector
->x
- 1;
949 cairo_matrix_init(&matrix
, cos2angle
, sin2angle
,
950 sin2angle
, -cos2angle
, 0, 0);
953 if (!adg_trail_put_segment((AdgTrail
*) path
, 1, &segment
))
956 /* No need to reverse an empty segment */
957 if (segment
.num_data
== 0 || segment
.num_data
== 0)
960 dup_segment
= cpml_segment_deep_dup(&segment
);
961 if (dup_segment
== NULL
)
964 cpml_segment_reverse(dup_segment
);
965 cpml_segment_transform(dup_segment
, &matrix
);
966 dup_segment
->data
[0].header
.type
= CPML_LINE
;
968 adg_path_append_segment(path
, dup_segment
);
972 _adg_dup_reverse_named_pairs(model
, &matrix
);
976 * adg_path_reflect_explicit:
978 * @x: the vector x component
979 * @y: the vector y component
981 * Convenient function to call adg_path_reflect() using explicit
982 * vector components instead of #CpmlVector.
987 adg_path_reflect_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
994 adg_path_reflect(path
, &vector
);
999 _adg_clear(AdgModel
*model
)
1002 AdgPathPrivate
*data
;
1004 path
= (AdgPath
*) model
;
1007 g_array_set_size(data
->cairo
.array
, 0);
1008 _adg_clear_operation(path
);
1009 _adg_clear_parent(model
);
1013 _adg_clear_parent(AdgModel
*model
)
1015 if (_ADG_OLD_MODEL_CLASS
->clear
)
1016 _ADG_OLD_MODEL_CLASS
->clear(model
);
1020 _adg_changed(AdgModel
*model
)
1022 _adg_clear_parent(model
);
1024 if (_ADG_OLD_MODEL_CLASS
->changed
)
1025 _ADG_OLD_MODEL_CLASS
->changed(model
);
1028 static cairo_path_t
*
1029 _adg_get_cairo_path(AdgTrail
*trail
)
1031 _adg_clear_parent((AdgModel
*) trail
);
1032 return _adg_read_cairo_path((AdgPath
*) trail
);
1035 static cairo_path_t
*
1036 _adg_read_cairo_path(AdgPath
*path
)
1038 AdgPathPrivate
*data
= path
->data
;
1039 cairo_path_t
*cairo_path
= &data
->cairo
.path
;
1040 GArray
*array
= data
->cairo
.array
;
1042 /* Always regenerate the cairo_path_t as it is a trivial operation */
1043 cairo_path
->status
= CAIRO_STATUS_SUCCESS
;
1044 cairo_path
->data
= (cairo_path_data_t
*) array
->data
;
1045 cairo_path
->num_data
= array
->len
;
1051 _adg_primitive_length(CpmlPrimitiveType type
)
1062 return cpml_primitive_type_get_n_points(type
);
1067 _adg_primitive_remap(CpmlPrimitive
*primitive
, gpointer to
,
1068 const CpmlPrimitive
*old
, gconstpointer from
)
1070 primitive
->org
= REMAPPED(old
->org
, from
, to
);
1071 primitive
->segment
= REMAPPED(old
->segment
, from
, to
);
1072 primitive
->data
= REMAPPED(old
->data
, from
, to
);
1076 _adg_rescan(AdgPath
*path
)
1078 AdgPathPrivate
*data
;
1079 CpmlSegment segment
;
1080 CpmlPrimitive current
, *last
, *over
;
1086 last
->segment
= NULL
;
1089 over
->segment
= NULL
;
1093 /* When no data is present, just bail out */
1094 if (! cpml_segment_from_cairo(&segment
, _adg_read_cairo_path(path
)))
1098 cpml_primitive_from_segment(¤t
, &segment
);
1100 cpml_primitive_copy(over
, last
);
1101 cpml_primitive_copy(last
, ¤t
);
1102 } while (cpml_primitive_next(¤t
));
1103 } while (cpml_segment_next(&segment
));
1107 _adg_append_primitive(AdgPath
*path
, CpmlPrimitive
*current
)
1109 AdgPathPrivate
*data
;
1110 cairo_path_data_t
*path_data
;
1111 CpmlPrimitiveType type
;
1113 gconstpointer old_data
;
1117 path_data
= current
->data
;
1118 length
= path_data
->header
.length
;
1119 type
= path_data
->header
.type
;
1121 /* Execute any pending operation */
1122 _adg_do_operation(path
, path_data
);
1124 /* Append the path data to the internal path array */
1125 old_data
= (data
->cairo
.array
)->data
;
1126 data
->cairo
.array
= g_array_append_vals(data
->cairo
.array
,
1128 new_data
= (data
->cairo
.array
)->data
;
1130 /* Set path data to point to the recently appended cairo_path_data_t
1131 * primitive: the first struct is the header */
1132 path_data
= (cairo_path_data_t
*) new_data
+
1133 (data
->cairo
.array
)->len
- length
;
1135 if (type
== CPML_MOVE
) {
1136 /* Remap last and over, but do not change their content */
1137 _adg_primitive_remap(&data
->last
, new_data
, &data
->last
, old_data
);
1138 _adg_primitive_remap(&data
->over
, new_data
, &data
->over
, old_data
);
1140 /* Store the last primitive into over */
1141 _adg_primitive_remap(&data
->over
, new_data
, &data
->last
, old_data
);
1143 /* Set the last primitive for subsequent binary operations */
1144 /* TODO: the assumption path_data - 1 is the last point is not true
1145 * e.g. when there are embeeded data in primitives */
1146 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
1147 data
->last
.segment
= NULL
;
1148 data
->last
.data
= path_data
;
1151 data
->cp_is_valid
= type
!= CPML_CLOSE
;
1152 if (data
->cp_is_valid
) {
1153 /* Save the last point in the current point */
1154 size_t n
= type
== CPML_MOVE
? 1 : cpml_primitive_type_get_n_points(type
) - 1;
1155 cpml_pair_from_cairo(&data
->cp
, &path_data
[n
]);
1158 /* Invalidate cairo_path: should be recomputed */
1159 _adg_clear_parent((AdgModel
*) path
);
1163 _adg_clear_operation(AdgPath
*path
)
1165 AdgPathPrivate
*data
;
1166 AdgOperation
*operation
;
1169 operation
= &data
->operation
;
1171 if (operation
->action
!= ADG_ACTION_NONE
) {
1172 g_warning(_("%s: a '%s' operation is still active while clearing the path"),
1173 G_STRLOC
, _adg_action_name(operation
->action
));
1174 operation
->action
= ADG_ACTION_NONE
;
1177 data
->cp_is_valid
= FALSE
;
1178 data
->last
.data
= NULL
;
1179 data
->over
.data
= NULL
;
1183 _adg_append_operation(AdgPath
*path
, gint action
, ...)
1185 AdgPathPrivate
*data
;
1186 AdgOperation
*operation
;
1187 AdgAction real_action
;
1190 real_action
= (AdgAction
) action
;
1193 if (data
->last
.data
== NULL
) {
1194 g_warning(_("%s: requested a '%s' operation on a path without current primitive"),
1195 G_STRLOC
, _adg_action_name(real_action
));
1199 operation
= &data
->operation
;
1200 if (operation
->action
!= ADG_ACTION_NONE
) {
1201 g_warning(_("%s: requested a '%s' operation while a '%s' operation was active"),
1202 G_STRLOC
, _adg_action_name(real_action
),
1203 _adg_action_name(operation
->action
));
1204 /* XXX: http://dev.entidi.com/p/adg/issues/50/ */
1208 va_start(var_args
, action
);
1210 switch (real_action
) {
1212 case ADG_ACTION_CHAMFER
:
1213 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
1214 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
1217 case ADG_ACTION_FILLET
:
1218 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
1221 case ADG_ACTION_NONE
:
1226 g_warning(_("%s: %d path operation not recognized"), G_STRLOC
, real_action
);
1231 operation
->action
= real_action
;
1234 if (data
->last
.data
[0].header
.type
== CPML_CLOSE
) {
1235 /* Special case: an action with the close primitive should
1236 * be resolved now by changing the close primitive to a
1237 * line-to and using it as second operand and use the first
1238 * primitive of the current segment as first operand */
1240 cairo_path_data_t
*path_data
;
1241 CpmlSegment segment
;
1242 CpmlPrimitive current
;
1244 length
= data
->cairo
.array
->len
;
1246 /* Ensure the close path primitive is not the only data */
1247 g_return_val_if_fail(length
> 1, FALSE
);
1249 /* Allocate one more item once for all to accept the
1250 * conversion from a close to line-to primitive */
1251 data
->cairo
.array
= g_array_set_size(data
->cairo
.array
, length
+ 1);
1252 path_data
= (cairo_path_data_t
*) data
->cairo
.array
->data
;
1253 --data
->cairo
.array
->len
;
1255 /* Set segment and current (the first primitive of segment) */
1256 cpml_segment_from_cairo(&segment
, _adg_read_cairo_path(path
));
1257 while (cpml_segment_next(&segment
))
1259 cpml_primitive_from_segment(¤t
, &segment
);
1261 /* Convert close path to a line-to primitive */
1262 ++data
->cairo
.array
->len
;
1263 path_data
[length
- 1].header
.type
= CPML_LINE
;
1264 path_data
[length
- 1].header
.length
= 2;
1265 path_data
[length
] = *current
.org
;
1267 data
->last
.segment
= &segment
;
1268 data
->last
.org
= &path_data
[length
- 2];
1269 data
->last
.data
= &path_data
[length
- 1];
1271 _adg_do_action(path
, real_action
, ¤t
);
1278 _adg_do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
1280 AdgPathPrivate
*data
;
1282 CpmlSegment segment
;
1283 CpmlPrimitive current
;
1284 cairo_path_data_t current_org
;
1287 action
= data
->operation
.action
;
1288 cpml_segment_from_cairo(&segment
, _adg_read_cairo_path(path
));
1290 /* Construct the current primitive, that is the primitive to be
1291 * mixed with the last primitive with the specified operation.
1292 * Its org is a copy of the end point of the last primitive: it can be
1293 * modified without affecting anything else. It is expected the operation
1294 * functions will add to @path the primitives required but NOT to add
1295 * @current, as this one will be inserted automatically. */
1296 current
.segment
= &segment
;
1297 current
.org
= ¤t_org
;
1298 current
.data
= path_data
;
1299 cpml_pair_to_cairo(&data
->cp
, ¤t_org
);
1301 _adg_do_action(path
, action
, ¤t
);
1305 _adg_do_action(AdgPath
*path
, AdgAction action
, CpmlPrimitive
*primitive
)
1308 case ADG_ACTION_NONE
:
1310 case ADG_ACTION_CHAMFER
:
1311 _adg_do_chamfer(path
, primitive
);
1313 case ADG_ACTION_FILLET
:
1314 _adg_do_fillet(path
, primitive
);
1317 g_return_if_reached();
1322 _adg_do_chamfer(AdgPath
*path
, CpmlPrimitive
*current
)
1324 AdgPathPrivate
*data
;
1325 CpmlPrimitive
*last
;
1326 gdouble delta1
, delta2
;
1332 delta1
= data
->operation
.data
.chamfer
.delta1
;
1333 len1
= cpml_primitive_get_length(last
);
1335 if (delta1
>= len1
) {
1336 g_warning(_("%s: first chamfer delta of %lf is greather than the available %lf length"),
1337 G_STRLOC
, delta1
, len1
);
1341 delta2
= data
->operation
.data
.chamfer
.delta2
;
1342 len2
= cpml_primitive_get_length(current
);
1344 if (delta2
>= len2
) {
1345 g_warning(_("%s: second chamfer delta of %lf is greather than the available %lf length"),
1346 G_STRLOC
, delta1
, len1
);
1350 /* Change the end point of the last primitive */
1351 cpml_primitive_put_pair_at(last
, 1. - delta1
/ len1
, &pair
);
1352 cpml_primitive_set_point(last
, -1, &pair
);
1354 /* Change the start point of the current primitive */
1355 cpml_primitive_put_pair_at(current
, delta2
/ len2
, &pair
);
1356 cpml_primitive_set_point(current
, 0, &pair
);
1358 /* Add the chamfer line */
1359 data
->operation
.action
= ADG_ACTION_NONE
;
1360 adg_path_append(path
, CPML_LINE
, &pair
);
1364 _adg_do_fillet(AdgPath
*path
, CpmlPrimitive
*current
)
1366 AdgPathPrivate
*data
;
1367 CpmlPrimitive
*last
, *current_dup
, *last_dup
;
1368 gdouble radius
, offset
, pos
;
1369 CpmlPair center
, vector
, p
[3];
1373 current_dup
= cpml_primitive_deep_dup(current
);
1374 last_dup
= cpml_primitive_deep_dup(last
);
1375 radius
= data
->operation
.data
.fillet
.radius
;
1376 offset
= _adg_is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1378 /* Find the center of the fillet from the intersection between
1379 * the last and current primitives offseted by radius */
1380 cpml_primitive_offset(current_dup
, offset
);
1381 cpml_primitive_offset(last_dup
, offset
);
1382 if (cpml_primitive_put_intersections(current_dup
, last_dup
, 1, ¢er
) == 0) {
1383 g_warning(_("%s: fillet with radius of %lf is not applicable here"),
1385 g_free(current_dup
);
1390 /* Compute the start point of the fillet */
1391 pos
= cpml_primitive_get_closest_pos(last_dup
, ¢er
);
1392 cpml_primitive_put_vector_at(last_dup
, pos
, &vector
);
1393 cpml_vector_set_length(&vector
, offset
);
1394 cpml_vector_normal(&vector
);
1395 p
[0].x
= center
.x
- vector
.x
;
1396 p
[0].y
= center
.y
- vector
.y
;
1398 /* Compute the mid point of the fillet */
1399 cpml_pair_from_cairo(&vector
, current
->org
);
1400 vector
.x
-= center
.x
;
1401 vector
.y
-= center
.y
;
1402 cpml_vector_set_length(&vector
, radius
);
1403 p
[1].x
= center
.x
+ vector
.x
;
1404 p
[1].y
= center
.y
+ vector
.y
;
1406 /* Compute the end point of the fillet */
1407 pos
= cpml_primitive_get_closest_pos(current_dup
, ¢er
);
1408 cpml_primitive_put_vector_at(current_dup
, pos
, &vector
);
1409 cpml_vector_set_length(&vector
, offset
);
1410 cpml_vector_normal(&vector
);
1411 p
[2].x
= center
.x
- vector
.x
;
1412 p
[2].y
= center
.y
- vector
.y
;
1414 g_free(current_dup
);
1417 /* Change the end point of the last primitive */
1418 cpml_primitive_set_point(last
, -1, &p
[0]);
1420 /* Change the start point of the current primitive */
1421 cpml_primitive_set_point(current
, 0, &p
[2]);
1423 /* Add the fillet arc */
1424 data
->operation
.action
= ADG_ACTION_NONE
;
1425 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
1429 _adg_is_convex(const CpmlPrimitive
*primitive1
, const CpmlPrimitive
*primitive2
)
1432 gdouble angle1
, angle2
;
1434 cpml_primitive_put_vector_at(primitive1
, -1, &v1
);
1435 cpml_primitive_put_vector_at(primitive2
, 0, &v2
);
1437 /* Probably there is a smarter way to get this without trygonometry */
1438 angle1
= cpml_vector_angle(&v1
);
1439 angle2
= cpml_vector_angle(&v2
);
1441 if (angle1
> angle2
)
1444 return angle2
-angle1
> G_PI
;
1447 static const gchar
*
1448 _adg_action_name(AdgAction action
)
1451 case ADG_ACTION_NONE
:
1453 case ADG_ACTION_CHAMFER
:
1455 case ADG_ACTION_FILLET
:
1463 _adg_get_named_pair(AdgModel
*model
, const gchar
*name
,
1464 CpmlPair
*pair
, gpointer user_data
)
1466 GSList
**named_pairs
;
1467 AdgNamedPair
*named_pair
;
1469 named_pairs
= user_data
;
1471 named_pair
= g_new(AdgNamedPair
, 1);
1472 named_pair
->name
= name
;
1473 named_pair
->pair
= *pair
;
1475 *named_pairs
= g_slist_prepend(*named_pairs
, named_pair
);
1479 _adg_dup_reverse_named_pairs(AdgModel
*model
, const cairo_matrix_t
*matrix
)
1481 AdgNamedPair
*old_named_pair
;
1482 AdgNamedPair named_pair
;
1483 GSList
*named_pairs
;
1485 /* Populate named_pairs with all the named pairs of model */
1487 adg_model_foreach_named_pair(model
, _adg_get_named_pair
, &named_pairs
);
1489 /* Readd the pairs applying the reversing transformation matrix to
1490 * their coordinates and prepending a "-" to their name */
1491 while (named_pairs
) {
1492 old_named_pair
= named_pairs
->data
;
1494 named_pair
.name
= g_strdup_printf("-%s", old_named_pair
->name
);
1495 named_pair
.pair
= old_named_pair
->pair
;
1496 cpml_pair_transform(&named_pair
.pair
, matrix
);
1498 adg_model_set_named_pair(model
, named_pair
.name
, &named_pair
.pair
);
1500 g_free((gpointer
) named_pair
.name
);
1501 named_pairs
= g_slist_delete_link(named_pairs
, named_pairs
);