1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010 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 #CpmlPath: this class
26 * implements methods to create the path and provides additional
27 * operations specific to technical drawings.
29 * #AdgPath overrides the get_cpml_path() method of the parent
30 * #AdgTrail class, avoiding the need of an #AdgTrailCallback.
31 * The path is constructed programmaticaly: keep in mind any
32 * method that modifies the path will invalidate the #CpmlPath
33 * returned by adg_trail_get_cpml_path().
35 * Although some of the provided methods are clearly based on the
36 * original cairo path manipulation API, their behavior could be
37 * sligthly different. This is intentional, because the ADG provides
38 * additional path manipulation algorithms, sometime quite complex,
39 * and a more restrictive filter on the path quality is required.
40 * Also, the ADG is designed to be used by technicians while cairo
41 * targets a broader range of developers.
43 * As an example, following the rule of the less surprise, some
44 * cairo functions guess the current point when it is not defined,
45 * while the #AdgPath methods trigger a warning without other effect.
46 * Furthermore, after cairo_path_close_path() a #CPML_MOVE primitive
47 * to the starting point of the segment is automatically added by
48 * cairo; in ADG, after an adg_path_close() the current point is unset.
54 * All fields are private and should not be used directly.
55 * Use its public methods instead.
59 #include "adg-internal.h"
61 #include "adg-path-private.h"
62 #include "adg-primitive.h"
66 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_path_parent_class)
67 #define PARENT_MODEL_CLASS ((AdgModelClass *) adg_path_parent_class)
70 static void finalize (GObject
*object
);
71 static void clear (AdgModel
*model
);
72 static void clear_parent (AdgModel
*model
);
73 static void changed (AdgModel
*model
);
74 static CpmlPath
* get_cpml_path (AdgTrail
*trail
);
75 static CpmlPath
* read_cpml_path (AdgPath
*path
);
76 static void append_primitive (AdgPath
*path
,
77 AdgPrimitive
*primitive
);
78 static gint
needed_pairs (CpmlPrimitiveType type
);
79 static void clear_operation (AdgPath
*path
);
80 static gboolean
append_operation (AdgPath
*path
,
83 static void do_operation (AdgPath
*path
,
86 static void do_action (AdgPath
*path
,
88 AdgPrimitive
*primitive
);
89 static void do_chamfer (AdgPath
*path
,
90 AdgPrimitive
*current
);
91 static void do_fillet (AdgPath
*path
,
92 AdgPrimitive
*current
);
93 static gboolean
is_convex (const AdgPrimitive
97 static const gchar
* action_name (AdgAction action
);
98 static void get_named_pair (const gchar
*name
,
101 static void dup_reverse_named_pairs (AdgModel
*model
,
102 const AdgMatrix
*matrix
);
105 G_DEFINE_TYPE(AdgPath
, adg_path
, ADG_TYPE_TRAIL
);
109 adg_path_class_init(AdgPathClass
*klass
)
111 GObjectClass
*gobject_class
;
112 AdgModelClass
*model_class
;
113 AdgTrailClass
*trail_class
;
115 gobject_class
= (GObjectClass
*) klass
;
116 model_class
= (AdgModelClass
*) klass
;
117 trail_class
= (AdgTrailClass
*) klass
;
119 g_type_class_add_private(klass
, sizeof(AdgPathPrivate
));
121 gobject_class
->finalize
= finalize
;
123 model_class
->clear
= clear
;
124 model_class
->changed
= changed
;
126 trail_class
->get_cpml_path
= get_cpml_path
;
130 adg_path_init(AdgPath
*path
)
132 AdgPathPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(path
, ADG_TYPE_PATH
,
135 data
->cp_is_valid
= FALSE
;
136 data
->cpml
.array
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
137 data
->operation
.action
= ADG_ACTION_NONE
;
143 finalize(GObject
*object
)
146 AdgPathPrivate
*data
;
148 path
= (AdgPath
*) object
;
151 g_array_free(data
->cpml
.array
, TRUE
);
152 clear_operation(path
);
154 if (PARENT_OBJECT_CLASS
->finalize
)
155 PARENT_OBJECT_CLASS
->finalize(object
);
162 * Creates a new path model. The path should be constructed
163 * programmatically by using the methods provided by #AdgPath.
165 * Returns: the newly created path model
170 return g_object_new(ADG_TYPE_PATH
, NULL
);
174 * adg_path_get_current_point:
177 * Gets the current point of @path, which is conceptually the
178 * final point reached by the path so far.
180 * If there is no defined current point, %NULL is returned.
181 * It is possible to check this in advance with
182 * adg_path_has_current_point().
184 * Most #AdgPath methods alter the current point and most of them
185 * expect a current point to be defined otherwise will fail triggering
186 * a warning. Check the description of every method for specific details.
188 * Returns: the current point or %NULL on no current point set or errors
191 adg_path_get_current_point(AdgPath
*path
)
193 AdgPathPrivate
*data
;
195 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
199 if (!data
->cp_is_valid
)
206 * adg_path_has_current_point:
209 * Returns whether a current point is defined on @path.
210 * See adg_path_get_current_point() for details on the current point.
212 * Returns: whether a current point is defined
215 adg_path_has_current_point(AdgPath
*path
)
217 AdgPathPrivate
*data
;
219 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
223 return data
->cp_is_valid
;
227 * adg_path_last_primitive:
230 * Gets the last primitive appended to @path. The returned struct
231 * is owned by @path and should not be freed or modified.
233 * Returns: a pointer to the last appended primitive or %NULL on errors
236 adg_path_last_primitive(AdgPath
*path
)
238 AdgPathPrivate
*data
;
240 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
248 * adg_path_over_primitive:
251 * Gets the primitive before the last one appended to @path. The
252 * "over" term comes from forth, where the %OVER operator works
253 * on the stack in the same way as adg_path_over_primitive() works
254 * on @path. The returned struct is owned by @path and should not
255 * be freed or modified.
257 * Returns: a pointer to the primitive before the last appended one
261 adg_path_over_primitive(AdgPath
*path
)
263 AdgPathPrivate
*data
;
265 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
275 * @type: a #cairo_data_type_t value
276 * @...: point data, specified as #AdgPair pointers
278 * Generic method to append a primitive to @path. The number of #AdgPair
279 * structs depends on @type: there is no way with this function to
280 * reserve more cairo_path_data_t structs than what is needed by the
283 * This function accepts also the special #CPML_ARC primitive.
285 * If @path has no current point while the requested primitive needs it,
286 * a warning message will be triggered without other effect.
289 adg_path_append(AdgPath
*path
, CpmlPrimitiveType type
, ...)
293 va_start(var_args
, type
);
294 adg_path_append_valist(path
, type
, var_args
);
299 * adg_path_append_valist:
301 * @type: a #cairo_data_type_t value
302 * @var_args: point data, specified as #AdgPair pointers
304 * va_list version of adg_path_append().
307 adg_path_append_valist(AdgPath
*path
, CpmlPrimitiveType type
, va_list var_args
)
309 AdgPathPrivate
*data
;
310 AdgPrimitive primitive
;
312 cairo_path_data_t org
;
313 cairo_path_data_t
*path_data
;
316 g_return_if_fail(ADG_IS_PATH(path
));
319 length
= needed_pairs(type
);
323 /* Set a copy of the current point as the primitive origin */
324 cpml_pair_to_cairo(&data
->cp
, &org
);
325 primitive
.org
= &org
;
327 /* Build the cairo_path_data_t array */
328 primitive
.data
= path_data
= g_new(cairo_path_data_t
, length
);
330 path_data
->header
.type
= type
;
331 path_data
->header
.length
= length
;
333 for (cnt
= 1; cnt
< length
; ++ cnt
) {
334 pair
= va_arg(var_args
, AdgPair
*);
336 g_free(primitive
.data
);
337 g_warning(_("%s: null pair caught while parsing arguments"),
343 cpml_pair_to_cairo(pair
, path_data
);
346 /* Terminate the creation of the temporary primitive */
347 primitive
.segment
= NULL
;
349 /* Append this primitive to @path */
350 append_primitive(path
, &primitive
);
352 g_free(primitive
.data
);
356 * adg_path_append_primitive:
358 * @primitive: the #AdgPrimitive to append
360 * Appends @primitive to @path. The primitive to add is considered the
361 * continuation of the current path so the <structfield>org</structfield>
362 * component of @primitive is not used. Anyway the current point is
363 * checked against it: they must be equal or the function will fail
364 * without further processing.
367 adg_path_append_primitive(AdgPath
*path
, const AdgPrimitive
*primitive
)
369 AdgPathPrivate
*data
;
370 AdgPrimitive
*primitive_dup
;
372 g_return_if_fail(ADG_IS_PATH(path
));
373 g_return_if_fail(primitive
!= NULL
);
374 g_return_if_fail(primitive
->org
!= NULL
);
378 g_return_if_fail(primitive
->org
->point
.x
== data
->cp
.x
&&
379 primitive
->org
->point
.y
== data
->cp
.y
);
381 /* The primitive data could be modified by pending operations:
383 primitive_dup
= adg_primitive_deep_dup(primitive
);
385 append_primitive(path
, primitive_dup
);
387 g_free(primitive_dup
);
391 * adg_path_append_segment:
393 * @segment: the #AdgSegment to append
395 * Appends @segment to @path.
398 adg_path_append_segment(AdgPath
*path
, const AdgSegment
*segment
)
400 AdgPathPrivate
*data
;
402 g_return_if_fail(ADG_IS_PATH(path
));
403 g_return_if_fail(segment
!= NULL
);
407 clear_parent((AdgModel
*) path
);
408 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
409 segment
->data
, segment
->num_data
);
413 * adg_path_append_cpml_path:
415 * @cpml_path: the #cairo_path_t path to append
417 * Appends a whole #CpmlPath to @path. #CpmlPath is a superset of
418 * #cairo_path_t, so this function can be feeded with both.
421 adg_path_append_cpml_path(AdgPath
*path
, const CpmlPath
*cpml_path
)
423 AdgPathPrivate
*data
;
425 g_return_if_fail(ADG_IS_PATH(path
));
426 g_return_if_fail(cpml_path
!= NULL
);
430 clear_parent((AdgModel
*) path
);
431 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
433 cpml_path
->num_data
);
439 * @pair: the destination coordinates
441 * Begins a new segment. After this call the current point will be @pair.
444 adg_path_move_to(AdgPath
*path
, const AdgPair
*pair
)
446 adg_path_append(path
, CPML_MOVE
, pair
);
450 * adg_path_move_to_explicit:
452 * @x: the new x coordinate
453 * @y: the new y coordinate
455 * Convenient function to call adg_path_move_to() using explicit
456 * coordinates instead of #AdgPair.
459 adg_path_move_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
466 adg_path_append(path
, CPML_MOVE
, &p
);
472 * @pair: the destination coordinates
474 * Adds a line to @path from the current point to @pair. After this
475 * call the current point will be @pair.
477 * If @path has no current point before this call, this function will
478 * trigger a warning without other effect.
481 adg_path_line_to(AdgPath
*path
, const AdgPair
*pair
)
483 adg_path_append(path
, CPML_LINE
, pair
);
487 * adg_path_line_to_explicit:
489 * @x: the new x coordinate
490 * @y: the new y coordinate
492 * Convenient function to call adg_path_line_to() using explicit
493 * coordinates instead of #AdgPair.
496 adg_path_line_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
503 adg_path_append(path
, CPML_LINE
, &p
);
509 * @throught: an arbitrary point on the arc
510 * @pair: the destination coordinates
512 * Adds an arc to the path from the current point to @pair, passing
513 * throught @throught. After this call the current point will be @pair.
515 * If @path has no current point before this call, this function will
516 * trigger a warning without other effect.
519 adg_path_arc_to(AdgPath
*path
, const AdgPair
*throught
, const AdgPair
*pair
)
521 adg_path_append(path
, CPML_ARC
, throught
, pair
);
525 * adg_path_arc_to_explicit:
527 * @x1: the x coordinate of an intermediate point
528 * @y1: the y coordinate of an intermediate point
529 * @x2: the x coordinate of the end of the arc
530 * @y2: the y coordinate of the end of the arc
532 * Convenient function to call adg_path_arc_to() using explicit
533 * coordinates instead of #AdgPair.
536 adg_path_arc_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
537 gdouble x2
, gdouble y2
)
546 adg_path_append(path
, CPML_ARC
, &p
[0], &p
[1]);
552 * @control1: the first control point of the curve
553 * @control2: the second control point of the curve
554 * @pair: the destination coordinates
556 * Adds a cubic Bézier curve to the path from the current point to
557 * position @pair, using @control1 and @control2 as control points.
558 * After this call the current point will be @pair.
560 * If @path has no current point before this call, this function will
561 * trigger a warning without other effect.
564 adg_path_curve_to(AdgPath
*path
, const AdgPair
*control1
,
565 const AdgPair
*control2
, const AdgPair
*pair
)
567 adg_path_append(path
, CPML_CURVE
, control1
, control2
, pair
);
571 * adg_path_curve_to_explicit:
573 * @x1: the x coordinate of the first control point
574 * @y1: the y coordinate of the first control point
575 * @x2: the x coordinate of the second control point
576 * @y2: the y coordinate of the second control point
577 * @x3: the x coordinate of the end of the curve
578 * @y3: the y coordinate of the end of the curve
580 * Convenient function to call adg_path_curve_to() using explicit
581 * coordinates instead of #AdgPair.
584 adg_path_curve_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
585 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
596 adg_path_append(path
, CPML_CURVE
, &p
[0], &p
[1], &p
[2]);
603 * Adds a line segment to the path from the current point to the
604 * beginning of the current segment, (the most recent point passed
605 * to an adg_path_move_to()), and closes this segment.
606 * After this call the current point will be unset.
608 * The behavior of adg_path_close() is distinct from simply calling
609 * adg_line_to() with the coordinates of the segment starting point.
610 * When a closed segment is stroked, there are no caps on the ends.
611 * Instead, there is a line join connecting the final and initial
612 * primitive of the segment.
614 * If @path has no current point before this call, this function will
615 * trigger a warning without other effect.
618 adg_path_close(AdgPath
*path
)
620 adg_path_append(path
, CPML_CLOSE
);
626 * @center: coordinates of the center of the arc
627 * @r: the radius of the arc
628 * @start: the start angle, in radians
629 * @end: the end angle, in radians
631 * A more usual way to add an arc to @path. After this call, the current
632 * point will be the computed end point of the arc. The arc will be
633 * rendered in increasing angle, accordling to @start and @end. This means
634 * if @start is less than @end, the arc will be rendered in clockwise
635 * direction (accordling to the default cairo coordinate system) while if
636 * @start is greather than @end, the arc will be rendered in couterclockwise
639 * By explicitely setting the whole arc data, the start point could be
640 * different from the current point. In this case, if @path has no
641 * current point before the call a #CPML_MOVE to the start point of
642 * the arc will be automatically prepended to the arc. If @path has a
643 * current point, a #CPML_LINE to the start point of the arc will be
644 * used instead of the "move to" primitive.
647 adg_path_arc(AdgPath
*path
, const AdgPair
*center
, gdouble r
,
648 gdouble start
, gdouble end
)
650 AdgPathPrivate
*data
;
653 g_return_if_fail(ADG_IS_PATH(path
));
654 g_return_if_fail(center
!= NULL
);
657 cpml_vector_from_angle(&p
[0], start
);
658 cpml_vector_from_angle(&p
[1], (end
-start
) / 2);
659 cpml_vector_from_angle(&p
[2], end
);
661 cpml_vector_set_length(&p
[0], r
);
662 cpml_vector_set_length(&p
[1], r
);
663 cpml_vector_set_length(&p
[2], r
);
672 if (!data
->cp_is_valid
)
673 adg_path_append(path
, CPML_MOVE
, &p
[0]);
674 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
675 adg_path_append(path
, CPML_LINE
, &p
[0]);
677 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
681 * adg_path_arc_explicit:
683 * @xc: x position of the center of the arc
684 * @yc: y position of the center of the arc
685 * @r: the radius of the arc
686 * @start: the start angle, in radians
687 * @end: the end angle, in radians
689 * Convenient function to call adg_path_arc() using explicit
690 * coordinates instead of #AdgPair.
693 adg_path_arc_explicit(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
694 gdouble start
, gdouble end
)
701 adg_path_arc(path
, ¢er
, r
, start
, end
);
707 * @delta1: the distance from the intersection point of the current primitive
708 * @delta2: the distance from the intersection point of the next primitive
710 * A binary action that generates a chamfer between two primitives.
711 * The first primitive involved is the current primitive, the second will
712 * be the next primitive appended to @path after this call. The second
713 * primitive is required: if the chamfer operation is not properly
714 * terminated (by not providing the second primitive), any API accessing
715 * the path in reading mode will raise a warning.
717 * An exception is a chamfer after a #CPML_CLOSE primitive. In this case,
718 * the second primitive is not required: the current close path is used
719 * as first operand while the first primitive of the current segment is
720 * used as second operand.
722 * The chamfer operation requires two lengths: @delta1 specifies the
723 * "quantity" to trim on the first primitive while @delta2 is the same
724 * applied on the second primitive. The term "quantity" means the length
725 * of the portion to cut out from the original primitive (that is the
726 * primitive as would be without the chamfer).
729 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
731 g_return_if_fail(ADG_IS_PATH(path
));
733 if (!append_operation(path
, ADG_ACTION_CHAMFER
, delta1
, delta2
))
740 * @radius: the radius of the fillet
742 * A binary action that joins to primitives with an arc.
743 * The first primitive involved is the current primitive, the second will
744 * be the next primitive appended to @path after this call. The second
745 * primitive is required: if the fillet operation is not properly
746 * terminated (by not providing the second primitive), any API accessing
747 * the path in reading mode will raise a warning.
749 * An exception is a fillet after a #CPML_CLOSE primitive. In this case,
750 * the second primitive is not required: the current close path is used
751 * as first operand while the first primitive of the current segment is
752 * used as second operand.
755 adg_path_fillet(AdgPath
*path
, gdouble radius
)
757 g_return_if_fail(ADG_IS_PATH(path
));
759 if (!append_operation(path
, ADG_ACTION_FILLET
, radius
))
766 * @vector: the slope of the axis
768 * Reflects the first segment or @path around the axis passing
769 * throught (0, 0) and with a @vector slope. The internal segment
770 * is duplicated and the proper transformation (computed from
771 * @vector) to mirror the segment is applied on all its points.
772 * The result is then reversed with cpml_segment_reverse() and
773 * appended to the original path with adg_path_append_segment().
775 * For convenience, if @vector is %NULL the path is reversed
776 * around the x axis (y=0).
779 adg_path_reflect(AdgPath
*path
, const CpmlVector
*vector
)
783 AdgSegment segment
, *dup_segment
;
785 g_return_if_fail(ADG_IS_PATH(path
));
786 g_return_if_fail(vector
== NULL
|| vector
->x
!= 0 || vector
->y
!= 0);
788 model
= (AdgModel
*) path
;
790 if (vector
== NULL
) {
791 cairo_matrix_init_scale(&matrix
, 1, -1);
794 gdouble cos2angle
, sin2angle
;
796 cpml_pair_copy(&slope
, vector
);
797 cpml_vector_set_length(&slope
, 1);
799 if (slope
.x
== 0 && slope
.y
== 0) {
800 g_warning(_("%s: the axis of the reflection is not known"),
805 sin2angle
= 2. * vector
->x
* vector
->y
;
806 cos2angle
= 2. * vector
->x
* vector
->x
- 1;
808 cairo_matrix_init(&matrix
, cos2angle
, sin2angle
,
809 sin2angle
, -cos2angle
, 0, 0);
812 if (!adg_trail_put_segment((AdgTrail
*) path
, 1, &segment
))
815 /* No need to reverse an empty segment */
816 if (segment
.num_data
== 0 || segment
.num_data
== 0)
819 dup_segment
= adg_segment_deep_dup(&segment
);
820 if (dup_segment
== NULL
)
823 cpml_segment_reverse(dup_segment
);
824 cpml_segment_transform(dup_segment
, &matrix
);
825 dup_segment
->data
[0].header
.type
= CPML_LINE
;
827 adg_path_append_segment(path
, dup_segment
);
831 dup_reverse_named_pairs(model
, &matrix
);
836 clear(AdgModel
*model
)
839 AdgPathPrivate
*data
;
841 path
= (AdgPath
*) model
;
844 g_array_set_size(data
->cpml
.array
, 0);
845 clear_operation(path
);
850 clear_parent(AdgModel
*model
)
852 if (PARENT_MODEL_CLASS
->clear
)
853 PARENT_MODEL_CLASS
->clear(model
);
857 changed(AdgModel
*model
)
861 if (PARENT_MODEL_CLASS
->changed
)
862 PARENT_MODEL_CLASS
->changed(model
);
866 get_cpml_path(AdgTrail
*trail
)
868 clear_parent((AdgModel
*) trail
);
869 return read_cpml_path((AdgPath
*) trail
);
873 read_cpml_path(AdgPath
*path
)
875 AdgPathPrivate
*data
= path
->data
;
877 /* Always regenerate the CpmlPath as it is a trivial operation */
878 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
879 data
->cpml
.path
.data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
;
880 data
->cpml
.path
.num_data
= (data
->cpml
.array
)->len
;
882 return &data
->cpml
.path
;
886 append_primitive(AdgPath
*path
, AdgPrimitive
*current
)
888 AdgPathPrivate
*data
;
889 cairo_path_data_t
*path_data
;
893 path_data
= current
->data
;
894 length
= path_data
[0].header
.length
;
896 /* Execute any pending operation */
897 do_operation(path
, path_data
);
899 /* Append the path data to the internal path array */
900 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
903 /* Set path data to point to the recently appended cairo_path_data_t
904 * primitive: the first struct is the header */
905 path_data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
+
906 (data
->cpml
.array
)->len
- length
;
908 /* Store the over primitive */
909 memcpy(&data
->over
, &data
->last
, sizeof(AdgPrimitive
));
911 /* Set the last primitive for subsequent binary operations */
912 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
913 data
->last
.segment
= NULL
;
914 data
->last
.data
= path_data
;
916 /* Save the last point as the current point, if applicable */
917 data
->cp_is_valid
= length
> 1;
919 cpml_pair_from_cairo(&data
->cp
, &path_data
[length
-1]);
921 /* Invalidate cairo_path: should be recomputed */
922 clear_parent((AdgModel
*) path
);
926 needed_pairs(CpmlPrimitiveType type
)
940 g_return_val_if_reached(0);
947 clear_operation(AdgPath
*path
)
949 AdgPathPrivate
*data
;
950 AdgOperation
*operation
;
953 operation
= &data
->operation
;
955 if (operation
->action
!= ADG_ACTION_NONE
) {
956 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
957 G_STRLOC
, action_name(operation
->action
));
958 operation
->action
= ADG_ACTION_NONE
;
963 append_operation(AdgPath
*path
, AdgAction action
, ...)
965 AdgPathPrivate
*data
;
966 AdgOperation
*operation
;
971 if (data
->last
.data
== NULL
) {
972 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
973 G_STRLOC
, action_name(action
));
977 operation
= &data
->operation
;
978 if (operation
->action
!= ADG_ACTION_NONE
) {
979 g_warning(_("%s: requested a `%s' operation while a `%s' operation is active"),
980 G_STRLOC
, action_name(action
), action_name(operation
->action
));
981 /* TODO: this is a rude simplification, as a lot of actions
982 * could be chained up. As an example, a fillet followed by
983 * a polar chamfer is quite common.
988 va_start(var_args
, action
);
992 case ADG_ACTION_CHAMFER
:
993 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
994 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
997 case ADG_ACTION_FILLET
:
998 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
1001 case ADG_ACTION_NONE
:
1006 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC
, action
);
1011 operation
->action
= action
;
1014 if (data
->last
.data
[0].header
.type
== CPML_CLOSE
) {
1015 /* Special case: an action with the close primitive should
1016 * be resolved now by changing the close primitive to a
1017 * line-to and using it as second operand and use the first
1018 * primitive of the current segment as first operand */
1020 cairo_path_data_t
*path_data
;
1021 CpmlSegment segment
;
1022 CpmlPrimitive current
;
1024 length
= data
->cpml
.array
->len
;
1026 /* Ensure the close path primitive is not the only data */
1027 g_return_val_if_fail(length
> 1, FALSE
);
1029 /* Allocate one more item once for all to accept the
1030 * conversion from a close to line-to primitive */
1031 data
->cpml
.array
= g_array_set_size(data
->cpml
.array
, length
+ 1);
1032 path_data
= (cairo_path_data_t
*) data
->cpml
.array
->data
;
1033 --data
->cpml
.array
->len
;
1035 /* Set segment and current (the first primitive of segment) */
1036 cpml_segment_from_cairo(&segment
, read_cpml_path(path
));
1037 while (cpml_segment_next(&segment
))
1039 cpml_primitive_from_segment(¤t
, &segment
);
1041 /* Convert close path to a line-to primitive */
1042 ++data
->cpml
.array
->len
;
1043 path_data
[length
- 1].header
.type
= CPML_LINE
;
1044 path_data
[length
- 1].header
.length
= 2;
1045 path_data
[length
] = *current
.org
;
1047 data
->last
.segment
= &segment
;
1048 data
->last
.org
= &path_data
[length
- 2];
1049 data
->last
.data
= &path_data
[length
- 1];
1051 do_action(path
, action
, ¤t
);
1059 do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
1061 AdgPathPrivate
*data
;
1064 AdgPrimitive current
;
1065 cairo_path_data_t current_org
;
1068 action
= data
->operation
.action
;
1069 cpml_segment_from_cairo(&segment
, read_cpml_path(path
));
1071 /* Construct the current primitive, that is the primitive to be
1072 * mixed with the last primitive with the specified operation.
1073 * Its org is a copy of the end point of the last primitive: it can be
1074 * modified without affecting anything else. It is expected the operation
1075 * functions will add to @path the primitives required but NOT to add
1076 * @current, as this one will be inserted automatically. */
1077 current
.segment
= &segment
;
1078 current
.org
= ¤t_org
;
1079 current
.data
= path_data
;
1080 cpml_pair_to_cairo(&data
->cp
, ¤t_org
);
1082 do_action(path
, action
, ¤t
);
1086 do_action(AdgPath
*path
, AdgAction action
, AdgPrimitive
*primitive
)
1089 case ADG_ACTION_NONE
:
1091 case ADG_ACTION_CHAMFER
:
1092 do_chamfer(path
, primitive
);
1094 case ADG_ACTION_FILLET
:
1095 do_fillet(path
, primitive
);
1098 g_return_if_reached();
1103 do_chamfer(AdgPath
*path
, AdgPrimitive
*current
)
1105 AdgPathPrivate
*data
;
1107 gdouble delta1
, delta2
;
1113 delta1
= data
->operation
.data
.chamfer
.delta1
;
1114 len1
= cpml_primitive_get_length(last
);
1116 if (delta1
>= len1
) {
1117 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1118 G_STRLOC
, delta1
, len1
);
1122 delta2
= data
->operation
.data
.chamfer
.delta2
;
1123 len2
= cpml_primitive_get_length(current
);
1125 if (delta2
>= len2
) {
1126 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1127 G_STRLOC
, delta1
, len1
);
1131 /* Change the end point of the last primitive */
1132 cpml_primitive_put_pair_at(last
, 1. - delta1
/ len1
, &pair
);
1133 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(last
, -1));
1135 /* Change the start point of the current primitive */
1136 cpml_primitive_put_pair_at(current
, delta2
/ len2
, &pair
);
1137 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(current
, 0));
1139 /* Add the chamfer line */
1140 data
->operation
.action
= ADG_ACTION_NONE
;
1141 adg_path_append(path
, CPML_LINE
, &pair
);
1145 do_fillet(AdgPath
*path
, AdgPrimitive
*current
)
1147 AdgPathPrivate
*data
;
1148 AdgPrimitive
*last
, *current_dup
, *last_dup
;
1149 gdouble radius
, offset
, pos
;
1150 AdgPair center
, vector
, p
[3];
1154 current_dup
= adg_primitive_deep_dup(current
);
1155 last_dup
= adg_primitive_deep_dup(last
);
1156 radius
= data
->operation
.data
.fillet
.radius
;
1157 offset
= is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1159 /* Find the center of the fillet from the intersection between
1160 * the last and current primitives offseted by radius */
1161 cpml_primitive_offset(current_dup
, offset
);
1162 cpml_primitive_offset(last_dup
, offset
);
1163 if (cpml_primitive_put_intersections(current_dup
, last_dup
, 1, ¢er
) == 0) {
1164 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1166 g_free(current_dup
);
1171 /* Compute the start point of the fillet */
1172 pos
= cpml_primitive_get_closest_pos(last_dup
, ¢er
);
1173 cpml_primitive_put_vector_at(last_dup
, pos
, &vector
);
1174 cpml_vector_set_length(&vector
, offset
);
1175 cpml_vector_normal(&vector
);
1176 p
[0].x
= center
.x
- vector
.x
;
1177 p
[0].y
= center
.y
- vector
.y
;
1179 /* Compute the mid point of the fillet */
1180 cpml_pair_from_cairo(&vector
, current
->org
);
1181 vector
.x
-= center
.x
;
1182 vector
.y
-= center
.y
;
1183 cpml_vector_set_length(&vector
, radius
);
1184 p
[1].x
= center
.x
+ vector
.x
;
1185 p
[1].y
= center
.y
+ vector
.y
;
1187 /* Compute the end point of the fillet */
1188 pos
= cpml_primitive_get_closest_pos(current_dup
, ¢er
);
1189 cpml_primitive_put_vector_at(current_dup
, pos
, &vector
);
1190 cpml_vector_set_length(&vector
, offset
);
1191 cpml_vector_normal(&vector
);
1192 p
[2].x
= center
.x
- vector
.x
;
1193 p
[2].y
= center
.y
- vector
.y
;
1195 g_free(current_dup
);
1198 /* Change the end point of the last primitive */
1199 cpml_pair_to_cairo(&p
[0], cpml_primitive_get_point(last
, -1));
1201 /* Change the start point of the current primitive */
1202 cpml_pair_to_cairo(&p
[2], cpml_primitive_get_point(current
, 0));
1204 /* Add the fillet arc */
1205 data
->operation
.action
= ADG_ACTION_NONE
;
1206 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
1210 is_convex(const AdgPrimitive
*primitive1
, const AdgPrimitive
*primitive2
)
1213 gdouble angle1
, angle2
;
1215 cpml_primitive_put_vector_at(primitive1
, -1, &v1
);
1216 cpml_primitive_put_vector_at(primitive2
, 0, &v2
);
1218 /* Probably there is a smarter way to get this without trygonometry */
1219 angle1
= cpml_vector_angle(&v1
);
1220 angle2
= cpml_vector_angle(&v2
);
1222 if (angle1
> angle2
)
1225 return angle2
-angle1
> M_PI
;
1228 static const gchar
*
1229 action_name(AdgAction action
)
1232 case ADG_ACTION_NONE
:
1234 case ADG_ACTION_CHAMFER
:
1236 case ADG_ACTION_FILLET
:
1244 get_named_pair(const gchar
*name
, AdgPair
*pair
, gpointer user_data
)
1246 GSList
**named_pairs
;
1247 AdgNamedPair
*named_pair
;
1249 named_pairs
= user_data
;
1251 named_pair
= g_new(AdgNamedPair
, 1);
1252 named_pair
->name
= name
;
1253 named_pair
->pair
= *pair
;
1255 *named_pairs
= g_slist_prepend(*named_pairs
, named_pair
);
1259 dup_reverse_named_pairs(AdgModel
*model
, const AdgMatrix
*matrix
)
1261 AdgNamedPair
*old_named_pair
;
1262 AdgNamedPair named_pair
;
1263 GSList
*named_pairs
;
1265 /* Populate named_pairs with all the named pairs of model */
1267 adg_model_foreach_named_pair(model
, get_named_pair
, &named_pairs
);
1269 /* Readd the pairs applying the reversing transformation matrix to
1270 * their coordinates and prepending a "-" to their name */
1271 while (named_pairs
) {
1272 old_named_pair
= named_pairs
->data
;
1274 named_pair
.name
= g_strdup_printf("-%s", old_named_pair
->name
);
1275 named_pair
.pair
= old_named_pair
->pair
;
1276 cpml_pair_transform(&named_pair
.pair
, matrix
);
1278 adg_model_set_named_pair(model
, named_pair
.name
, &named_pair
.pair
);
1280 g_free((gpointer
) named_pair
.name
);
1281 named_pairs
= g_slist_delete_link(named_pairs
, named_pairs
);