1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 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 %CAIRO_PATH_MOVE_TO
47 * primitive to the starting point of the segment is automatically
48 * added by cairo; in ADG, after an adg_path_close() the current
49 * point is simply unset.
55 * All fields are private and should not be used directly.
56 * Use its public methods instead.
60 #include "adg-internal.h"
62 #include "adg-path-private.h"
63 #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 void dup_reversed_pair (const gchar
*name
,
100 static const gchar
* action_name (AdgAction action
);
103 G_DEFINE_TYPE(AdgPath
, adg_path
, ADG_TYPE_TRAIL
);
107 adg_path_class_init(AdgPathClass
*klass
)
109 GObjectClass
*gobject_class
;
110 AdgModelClass
*model_class
;
111 AdgTrailClass
*trail_class
;
113 gobject_class
= (GObjectClass
*) klass
;
114 model_class
= (AdgModelClass
*) klass
;
115 trail_class
= (AdgTrailClass
*) klass
;
117 g_type_class_add_private(klass
, sizeof(AdgPathPrivate
));
119 gobject_class
->finalize
= finalize
;
121 model_class
->clear
= clear
;
122 model_class
->changed
= changed
;
124 trail_class
->get_cpml_path
= get_cpml_path
;
128 adg_path_init(AdgPath
*path
)
130 AdgPathPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(path
, ADG_TYPE_PATH
,
133 data
->cp_is_valid
= FALSE
;
134 data
->cpml
.array
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
135 data
->operation
.action
= ADG_ACTION_NONE
;
141 finalize(GObject
*object
)
144 AdgPathPrivate
*data
;
146 path
= (AdgPath
*) object
;
149 g_array_free(data
->cpml
.array
, TRUE
);
150 clear_operation(path
);
152 if (PARENT_OBJECT_CLASS
->finalize
)
153 PARENT_OBJECT_CLASS
->finalize(object
);
160 * Creates a new path model. The path should be constructed
161 * programmatically by using the methods provided by #AdgPath.
163 * Returns: the newly created path model
168 return g_object_new(ADG_TYPE_PATH
, NULL
);
172 * adg_path_get_current_point:
175 * Gets the current point of @path, which is conceptually the
176 * final point reached by the path so far.
178 * If there is no defined current point, %NULL is returned.
179 * It is possible to check this in advance with
180 * adg_path_has_current_point().
182 * Most #AdgPath methods alter the current point and most of them
183 * expect a current point to be defined otherwise will fail triggering
184 * a warning. Check the description of every method for specific details.
186 * Returns: the current point or %NULL on no current point set or errors
189 adg_path_get_current_point(AdgPath
*path
)
191 AdgPathPrivate
*data
;
193 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
197 if (!data
->cp_is_valid
)
204 * adg_path_has_current_point:
207 * Returns whether a current point is defined on @path.
208 * See adg_path_get_current_point() for details on the current point.
210 * Returns: whether a current point is defined
213 adg_path_has_current_point(AdgPath
*path
)
215 AdgPathPrivate
*data
;
217 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
221 return data
->cp_is_valid
;
225 * adg_path_last_primitive:
228 * Gets the last primitive appended to @path. The returned struct
229 * is owned by @path and should not be freed or modified.
231 * Returns: a pointer to the last appended primitive or %NULL on errors
234 adg_path_last_primitive(AdgPath
*path
)
236 AdgPathPrivate
*data
;
238 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
246 * adg_path_over_primitive:
249 * Gets the primitive before the last one appended to @path. The
250 * "over" term comes from forth, where the %OVER operator works
251 * on the stack in the same way as adg_path_over_primitive() works
252 * on @path. The returned struct is owned by @path and should not
253 * be freed or modified.
255 * Returns: a pointer to the primitive before the last appended one
259 adg_path_over_primitive(AdgPath
*path
)
261 AdgPathPrivate
*data
;
263 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
273 * @type: a #cairo_data_type_t value
274 * @...: point data, specified as #AdgPair pointers
276 * Generic method to append a primitive to @path. The number of #AdgPair
277 * structs depends on @type: there is no way with this function to
278 * reserve more cairo_path_data_t structs than what is needed by the
281 * This function accepts also the special %CAIRO_PATH_ARC_TO primitive.
283 * If @path has no current point while the requested primitive needs it,
284 * a warning message will be triggered without other effect.
287 adg_path_append(AdgPath
*path
, CpmlPrimitiveType type
, ...)
291 va_start(var_args
, type
);
292 adg_path_append_valist(path
, type
, var_args
);
297 * adg_path_append_valist:
299 * @type: a #cairo_data_type_t value
300 * @var_args: point data, specified as #AdgPair pointers
302 * va_list version of adg_path_append().
305 adg_path_append_valist(AdgPath
*path
, CpmlPrimitiveType type
, va_list var_args
)
307 AdgPathPrivate
*data
;
308 AdgPrimitive primitive
;
310 cairo_path_data_t org
;
311 cairo_path_data_t
*path_data
;
313 g_return_if_fail(ADG_IS_PATH(path
));
316 length
= needed_pairs(type
);
320 /* Set a copy of the current point as the primitive origin */
321 cpml_pair_to_cairo(&data
->cp
, &org
);
322 primitive
.org
= &org
;
324 /* Build the cairo_path_data_t array */
325 primitive
.data
= path_data
= g_new(cairo_path_data_t
, length
);
327 path_data
->header
.type
= type
;
328 path_data
->header
.length
= length
;
330 for (cnt
= 1; cnt
< length
; ++ cnt
) {
332 cpml_pair_to_cairo(va_arg(var_args
, AdgPair
*), path_data
);
335 /* Terminate the creation of the temporary primitive */
336 primitive
.segment
= NULL
;
338 /* Append this primitive to @path */
339 append_primitive(path
, &primitive
);
341 g_free(primitive
.data
);
345 * adg_path_append_primitive:
347 * @primitive: the #AdgPrimitive to append
349 * Appends @primitive to @path. The primitive to add is considered the
350 * continuation of the current path so the <structfield>org</structfield>
351 * component of @primitive is not used. Anyway the current poins is
352 * checked against it: they must be equal or the function will fail
353 * without further processing.
356 adg_path_append_primitive(AdgPath
*path
, const AdgPrimitive
*primitive
)
358 AdgPathPrivate
*data
;
359 AdgPrimitive
*primitive_dup
;
361 g_return_if_fail(ADG_IS_PATH(path
));
362 g_return_if_fail(primitive
!= NULL
);
366 g_return_if_fail(primitive
->org
->point
.x
== data
->cp
.x
&&
367 primitive
->org
->point
.y
== data
->cp
.y
);
369 /* The primitive data could be modified by pending operations:
371 primitive_dup
= adg_primitive_deep_dup(primitive
);
373 append_primitive(path
, primitive_dup
);
375 g_free(primitive_dup
);
379 * adg_path_append_segment:
381 * @segment: the #AdgSegment to append
383 * Appends @segment to @path.
386 adg_path_append_segment(AdgPath
*path
, const AdgSegment
*segment
)
388 AdgPathPrivate
*data
;
390 g_return_if_fail(ADG_IS_PATH(path
));
391 g_return_if_fail(segment
!= NULL
);
395 clear_parent((AdgModel
*) path
);
396 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
397 segment
->data
, segment
->num_data
);
401 * adg_path_append_cpml_path:
403 * @cpml_path: the #cairo_path_t path to append
405 * Appends a whole #CpmlPath to @path. #CpmlPath is a superset of
406 * #cairo_path_t, so this function can be feeded with both.
409 adg_path_append_cpml_path(AdgPath
*path
, const CpmlPath
*cpml_path
)
411 AdgPathPrivate
*data
;
413 g_return_if_fail(ADG_IS_PATH(path
));
417 clear_parent((AdgModel
*) path
);
418 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
420 cpml_path
->num_data
);
426 * @pair: the destination coordinates
428 * Begins a new segment. After this call the current point will be @pair.
431 adg_path_move_to(AdgPath
*path
, const AdgPair
*pair
)
433 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, pair
);
437 * adg_path_move_to_explicit:
439 * @x: the new x coordinate
440 * @y: the new y coordinate
442 * Convenient function to call adg_path_move_to() using explicit
443 * coordinates instead of #AdgPair.
446 adg_path_move_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
453 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, &p
);
459 * @pair: the destination coordinates
461 * Adds a line to @path from the current point to @pair. After this
462 * call the current point will be @pair.
464 * If @path has no current point before this call, this function will
465 * trigger a warning without other effect.
468 adg_path_line_to(AdgPath
*path
, const AdgPair
*pair
)
470 adg_path_append(path
, CAIRO_PATH_LINE_TO
, pair
);
474 * adg_path_line_to_explicit:
476 * @x: the new x coordinate
477 * @y: the new y coordinate
479 * Convenient function to call adg_path_line_to() using explicit
480 * coordinates instead of #AdgPair.
483 adg_path_line_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
490 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &p
);
496 * @throught: an arbitrary point on the arc
497 * @pair: the destination coordinates
499 * Adds an arc to the path from the current point to @pair, passing
500 * throught @throught. After this call the current point will be @pair.
502 * If @path has no current point before this call, this function will
503 * trigger a warning without other effect.
506 adg_path_arc_to(AdgPath
*path
, const AdgPair
*throught
, const AdgPair
*pair
)
508 adg_path_append(path
, CAIRO_PATH_ARC_TO
, throught
, pair
);
512 * adg_path_arc_to_explicit:
514 * @x1: the x coordinate of an intermediate point
515 * @y1: the y coordinate of an intermediate point
516 * @x2: the x coordinate of the end of the arc
517 * @y2: the y coordinate of the end of the arc
519 * Convenient function to call adg_path_arc_to() using explicit
520 * coordinates instead of #AdgPair.
523 adg_path_arc_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
524 gdouble x2
, gdouble y2
)
533 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[0], &p
[1]);
539 * @control1: the first control point of the curve
540 * @control2: the second control point of the curve
541 * @pair: the destination coordinates
543 * Adds a cubic Bézier curve to the path from the current point to
544 * position @pair, using @control1 and @control2 as control points.
545 * After this call the current point will be @pair.
547 * If @path has no current point before this call, this function will
548 * trigger a warning without other effect.
551 adg_path_curve_to(AdgPath
*path
, const AdgPair
*control1
,
552 const AdgPair
*control2
, const AdgPair
*pair
)
554 adg_path_append(path
, CAIRO_PATH_CURVE_TO
, control1
, control2
, pair
);
558 * adg_path_curve_to_explicit:
560 * @x1: the x coordinate of the first control point
561 * @y1: the y coordinate of the first control point
562 * @x2: the x coordinate of the second control point
563 * @y2: the y coordinate of the second control point
564 * @x3: the x coordinate of the end of the curve
565 * @y3: the y coordinate of the end of the curve
567 * Convenient function to call adg_path_curve_to() using explicit
568 * coordinates instead of #AdgPair.
571 adg_path_curve_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
572 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
583 adg_path_append(path
, CAIRO_PATH_CURVE_TO
, &p
[0], &p
[1], &p
[2]);
590 * Adds a line segment to the path from the current point to the
591 * beginning of the current segment, (the most recent point passed
592 * to an adg_path_move_to()), and closes this segment.
593 * After this call the current point will be unset.
595 * The behavior of adg_path_close() is distinct from simply calling
596 * adg_line_to() with the coordinates of the segment starting point.
597 * When a closed segment is stroked, there are no caps on the ends.
598 * Instead, there is a line join connecting the final and initial
599 * primitive of the segment.
601 * If @path has no current point before this call, this function will
602 * trigger a warning without other effect.
605 adg_path_close(AdgPath
*path
)
607 adg_path_append(path
, CAIRO_PATH_CLOSE_PATH
);
613 * @center: coordinates of the center of the arc
614 * @r: the radius of the arc
615 * @start: the start angle, in radians
616 * @end: the end angle, in radians
618 * A more usual way to add an arc to @path. After this call, the current
619 * point will be the computed end point of the arc. The arc will be
620 * rendered in increasing angle, accordling to @start and @end. This means
621 * if @start is less than @end, the arc will be rendered in clockwise
622 * direction (accordling to the default cairo coordinate system) while if
623 * @start is greather than @end, the arc will be rendered in couterclockwise
626 * By explicitely setting the whole arc data, the start point could be
627 * different from the current point. In this case, if @path has no
628 * current point before the call a %CAIRO_PATH_MOVE_TO to the start
629 * point of the arc will be automatically prepended to the arc.
630 * If @path has a current point, a %CAIRO_PATH_LINE_TO to the start
631 * point of the arc will be used instead of the "move to" primitive.
634 adg_path_arc(AdgPath
*path
, const AdgPair
*center
, gdouble r
,
635 gdouble start
, gdouble end
)
637 AdgPathPrivate
*data
;
640 g_return_if_fail(ADG_IS_PATH(path
));
643 cpml_vector_from_angle(&p
[0], start
);
644 cpml_vector_from_angle(&p
[1], (end
-start
) / 2);
645 cpml_vector_from_angle(&p
[2], end
);
647 cpml_vector_set_length(&p
[0], r
);
648 cpml_vector_set_length(&p
[1], r
);
649 cpml_vector_set_length(&p
[2], r
);
651 cpml_pair_add(&p
[0], center
);
652 cpml_pair_add(&p
[1], center
);
653 cpml_pair_add(&p
[2], center
);
655 if (!data
->cp_is_valid
)
656 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, &p
[0]);
657 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
658 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &p
[0]);
660 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[1], &p
[2]);
664 * adg_path_arc_explicit:
666 * @xc: x position of the center of the arc
667 * @yc: y position of the center of the arc
668 * @r: the radius of the arc
669 * @start: the start angle, in radians
670 * @end: the end angle, in radians
672 * Convenient function to call adg_path_arc() using explicit
673 * coordinates instead of #AdgPair.
676 adg_path_arc_explicit(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
677 gdouble start
, gdouble end
)
684 adg_path_arc(path
, ¢er
, r
, start
, end
);
690 * @delta1: the distance from the intersection point of the current primitive
691 * @delta2: the distance from the intersection point of the next primitive
693 * A binary action that generates a chamfer between two primitives.
694 * The first primitive involved is the current primitive, the second will
695 * be the next primitive appended to @path after this call. The second
696 * primitive is required: if the chamfer operation is not properly
697 * terminated (by not providing the second primitive), any API accessing
698 * the path in reading mode will raise a warning.
700 * An exception is a chamfer after a %CAIRO_PATH_CLOSE_PATH primitive.
701 * In this case the second primitive is not required: the current close
702 * path is used as first operand while the first primitive of the
703 * current segment is used as second operand.
705 * The chamfer operation requires two lengths: @delta1 specifies the
706 * "quantity" to trim on the first primitive while @delta2 is the same
707 * applied on the second primitive. The term "quantity" means the length
708 * of the portion to cut out from the original primitive (that is the
709 * primitive as would be without the chamfer).
712 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
714 g_return_if_fail(ADG_IS_PATH(path
));
716 if (!append_operation(path
, ADG_ACTION_CHAMFER
, delta1
, delta2
))
723 * @radius: the radius of the fillet
725 * A binary action that joins to primitives with an arc.
726 * The first primitive involved is the current primitive, the second will
727 * be the next primitive appended to @path after this call. The second
728 * primitive is required: if the fillet operation is not properly
729 * terminated (by not providing the second primitive), any API accessing
730 * the path in reading mode will raise a warning.
732 * An exception is a fillet after a %CAIRO_PATH_CLOSE_PATH primitive.
733 * In this case the second primitive is not required: the current close
734 * path is used as first operand while the first primitive of the
735 * current segment is used as second operand.
738 adg_path_fillet(AdgPath
*path
, gdouble radius
)
740 g_return_if_fail(ADG_IS_PATH(path
));
742 if (!append_operation(path
, ADG_ACTION_FILLET
, radius
))
749 * @vector: the slope of the axis
751 * Reflects the first segment or @path around the axis passing
752 * throught (0, 0) and with a @vector slope. The internal segment
753 * is duplicated and the proper transformation (computed from
754 * @vector) to mirror the segment is applied on all its points.
755 * The result is then reversed with cpml_segment_reverse() and
756 * appended to the original path with adg_path_append_segment().
759 adg_path_reflect(AdgPath
*path
, const CpmlVector
*vector
)
761 AdgNamedPairData data
;
762 AdgSegment segment
, *dup_segment
;
764 g_return_if_fail(ADG_IS_PATH(path
));
766 data
.model
= (AdgModel
*) path
;
768 if (vector
== NULL
) {
769 cairo_matrix_init_scale(&data
.matrix
, 1, -1);
772 gdouble cos2angle
, sin2angle
;
774 cpml_pair_copy(&slope
, vector
);
775 cpml_vector_set_length(&slope
, 1);
777 if (slope
.x
== 0 && slope
.y
== 0) {
778 g_warning(_("%s: the axis of the reflection is not known"),
783 sin2angle
= 2. * vector
->x
* vector
->y
;
784 cos2angle
= 2. * vector
->x
* vector
->x
- 1;
786 cairo_matrix_init(&data
.matrix
, cos2angle
, sin2angle
,
787 sin2angle
, -cos2angle
, 0, 0);
790 adg_trail_put_segment((AdgTrail
*) path
, 1, &segment
);
791 dup_segment
= adg_segment_deep_dup(&segment
);
793 cpml_segment_reverse(dup_segment
);
794 cpml_segment_transform(dup_segment
, &data
.matrix
);
795 dup_segment
->data
[0].header
.type
= CAIRO_PATH_LINE_TO
;
797 adg_path_append_segment(path
, dup_segment
);
801 adg_model_foreach_named_pair(data
.model
, dup_reversed_pair
, &data
);
806 clear(AdgModel
*model
)
809 AdgPathPrivate
*data
;
811 path
= (AdgPath
*) model
;
814 g_array_set_size(data
->cpml
.array
, 0);
815 clear_operation(path
);
820 clear_parent(AdgModel
*model
)
822 if (PARENT_MODEL_CLASS
->clear
)
823 PARENT_MODEL_CLASS
->clear(model
);
827 changed(AdgModel
*model
)
831 if (PARENT_MODEL_CLASS
->changed
)
832 PARENT_MODEL_CLASS
->changed(model
);
836 get_cpml_path(AdgTrail
*trail
)
838 clear_parent((AdgModel
*) trail
);
839 return read_cpml_path((AdgPath
*) trail
);
843 read_cpml_path(AdgPath
*path
)
845 AdgPathPrivate
*data
= path
->data
;
847 /* Always regenerate the CpmlPath as it is a trivial operation */
848 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
849 data
->cpml
.path
.data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
;
850 data
->cpml
.path
.num_data
= (data
->cpml
.array
)->len
;
852 return &data
->cpml
.path
;
856 append_primitive(AdgPath
*path
, AdgPrimitive
*current
)
858 AdgPathPrivate
*data
;
859 cairo_path_data_t
*path_data
;
863 path_data
= current
->data
;
864 length
= path_data
[0].header
.length
;
866 /* Execute any pending operation */
867 do_operation(path
, path_data
);
869 /* Append the path data to the internal path array */
870 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
873 /* Set path data to point to the recently appended cairo_path_data_t
874 * primitive: the first struct is the header */
875 path_data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
+
876 (data
->cpml
.array
)->len
- length
;
878 /* Store the over primitive */
879 memcpy(&data
->over
, &data
->last
, sizeof(AdgPrimitive
));
881 /* Set the last primitive for subsequent binary operations */
882 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
883 data
->last
.segment
= NULL
;
884 data
->last
.data
= path_data
;
886 /* Save the last point as the current point, if applicable */
887 data
->cp_is_valid
= length
> 1;
889 cpml_pair_from_cairo(&data
->cp
, &path_data
[length
-1]);
891 /* Invalidate cairo_path: should be recomputed */
892 clear_parent((AdgModel
*) path
);
896 needed_pairs(CpmlPrimitiveType type
)
899 case CAIRO_PATH_CLOSE_PATH
:
901 case CAIRO_PATH_MOVE_TO
:
903 case CAIRO_PATH_LINE_TO
:
905 case CAIRO_PATH_ARC_TO
:
907 case CAIRO_PATH_CURVE_TO
:
910 g_return_val_if_reached(0);
917 clear_operation(AdgPath
*path
)
919 AdgPathPrivate
*data
;
920 AdgOperation
*operation
;
923 operation
= &data
->operation
;
925 if (operation
->action
!= ADG_ACTION_NONE
) {
926 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
927 G_STRLOC
, action_name(operation
->action
));
928 operation
->action
= ADG_ACTION_NONE
;
933 append_operation(AdgPath
*path
, AdgAction action
, ...)
935 AdgPathPrivate
*data
;
936 AdgOperation
*operation
;
941 if (data
->last
.data
== NULL
) {
942 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
943 G_STRLOC
, action_name(action
));
947 operation
= &data
->operation
;
948 if (operation
->action
!= ADG_ACTION_NONE
) {
949 g_warning(_("%s: requested a `%s' operation while a `%s' operation is active"),
950 G_STRLOC
, action_name(action
), action_name(operation
->action
));
951 ADG_MESSAGE("TODO: this is a rude simplification, as a lot of "
952 "actions could be chained up. As an example, a fillet "
953 "followed by a polar chamfer is quite common.");
957 va_start(var_args
, action
);
961 case ADG_ACTION_CHAMFER
:
962 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
963 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
966 case ADG_ACTION_FILLET
:
967 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
970 case ADG_ACTION_NONE
:
975 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC
, action
);
980 operation
->action
= action
;
983 if (data
->last
.data
[0].header
.type
== CAIRO_PATH_CLOSE_PATH
) {
984 /* Special case: an action with the close primitive should
985 * be resolved now by changing the close primitive to a
986 * line-to and using it as second operand and use the first
987 * primitive of the current segment as first operand */
989 cairo_path_data_t
*path_data
;
991 CpmlPrimitive current
;
993 length
= data
->cpml
.array
->len
;
995 /* Ensure the close path primitive is not the only data */
996 g_return_val_if_fail(length
> 1, FALSE
);
998 /* Allocate one more item once for all to accept the
999 * conversion from a close to line-to primitive */
1000 data
->cpml
.array
= g_array_set_size(data
->cpml
.array
, length
+ 1);
1001 path_data
= (cairo_path_data_t
*) data
->cpml
.array
->data
;
1002 --data
->cpml
.array
->len
;
1004 /* Set segment and current (the first primitive of segment) */
1005 cpml_segment_from_cairo(&segment
, read_cpml_path(path
));
1006 while (cpml_segment_next(&segment
))
1008 cpml_primitive_from_segment(¤t
, &segment
);
1010 /* Convert close path to a line-to primitive */
1011 ++data
->cpml
.array
->len
;
1012 path_data
[length
- 1].header
.type
= CAIRO_PATH_LINE_TO
;
1013 path_data
[length
- 1].header
.length
= 2;
1014 path_data
[length
] = *current
.org
;
1016 data
->last
.segment
= &segment
;
1017 data
->last
.org
= &path_data
[length
- 2];
1018 data
->last
.data
= &path_data
[length
- 1];
1020 do_action(path
, action
, ¤t
);
1028 do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
1030 AdgPathPrivate
*data
;
1033 AdgPrimitive current
;
1034 cairo_path_data_t current_org
;
1037 action
= data
->operation
.action
;
1038 cpml_segment_from_cairo(&segment
, read_cpml_path(path
));
1040 /* Construct the current primitive, that is the primitive to be
1041 * mixed with the last primitive with the specified operation.
1042 * Its org is a copy of the end point of the last primitive: it can be
1043 * modified without affecting anything else. It is expected the operation
1044 * functions will add to @path the primitives required but NOT to add
1045 * @current, as this one will be inserted automatically. */
1046 current
.segment
= &segment
;
1047 current
.org
= ¤t_org
;
1048 current
.data
= path_data
;
1049 cpml_pair_to_cairo(&data
->cp
, ¤t_org
);
1051 do_action(path
, action
, ¤t
);
1055 do_action(AdgPath
*path
, AdgAction action
, AdgPrimitive
*primitive
)
1058 case ADG_ACTION_NONE
:
1060 case ADG_ACTION_CHAMFER
:
1061 do_chamfer(path
, primitive
);
1063 case ADG_ACTION_FILLET
:
1064 do_fillet(path
, primitive
);
1067 g_return_if_reached();
1072 do_chamfer(AdgPath
*path
, AdgPrimitive
*current
)
1074 AdgPathPrivate
*data
;
1076 gdouble delta1
, delta2
;
1082 delta1
= data
->operation
.data
.chamfer
.delta1
;
1083 len1
= cpml_primitive_length(last
);
1085 if (delta1
>= len1
) {
1086 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1087 G_STRLOC
, delta1
, len1
);
1091 delta2
= data
->operation
.data
.chamfer
.delta2
;
1092 len2
= cpml_primitive_length(current
);
1094 if (delta2
>= len2
) {
1095 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1096 G_STRLOC
, delta1
, len1
);
1100 /* Change the end point of the last primitive */
1101 cpml_primitive_pair_at(last
, &pair
, 1. - delta1
/ len1
);
1102 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(last
, -1));
1104 /* Change the start point of the current primitive */
1105 cpml_primitive_pair_at(current
, &pair
, delta2
/ len2
);
1106 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(current
, 0));
1108 /* Add the chamfer line */
1109 data
->operation
.action
= ADG_ACTION_NONE
;
1110 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &pair
);
1114 do_fillet(AdgPath
*path
, AdgPrimitive
*current
)
1116 AdgPathPrivate
*data
;
1117 AdgPrimitive
*last
, *current_dup
, *last_dup
;
1118 gdouble radius
, offset
, pos
;
1119 AdgPair center
, vector
, p
[3];
1123 current_dup
= adg_primitive_deep_dup(current
);
1124 last_dup
= adg_primitive_deep_dup(last
);
1125 radius
= data
->operation
.data
.fillet
.radius
;
1126 offset
= is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1128 /* Find the center of the fillet from the intersection between
1129 * the last and current primitives offseted by radius */
1130 cpml_primitive_offset(current_dup
, offset
);
1131 cpml_primitive_offset(last_dup
, offset
);
1132 if (cpml_primitive_intersection(current_dup
, last_dup
,
1134 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1136 g_free(current_dup
);
1141 /* Compute the start point of the fillet */
1142 pos
= cpml_primitive_near_pos(last_dup
, ¢er
);
1143 cpml_primitive_vector_at(last_dup
, &vector
, pos
);
1144 cpml_vector_set_length(&vector
, offset
);
1145 cpml_vector_normal(&vector
);
1146 cpml_pair_copy(&p
[0], ¢er
);
1147 cpml_pair_sub(&p
[0], &vector
);
1149 /* Compute the mid point of the fillet */
1150 cpml_pair_from_cairo(&vector
, current
->org
);
1151 cpml_pair_sub(&vector
, ¢er
);
1152 cpml_vector_set_length(&vector
, radius
);
1153 cpml_pair_copy(&p
[1], ¢er
);
1154 cpml_pair_add(&p
[1], &vector
);
1156 /* Compute the end point of the fillet */
1157 pos
= cpml_primitive_near_pos(current_dup
, ¢er
);
1158 cpml_primitive_vector_at(current_dup
, &vector
, pos
);
1159 cpml_vector_set_length(&vector
, offset
);
1160 cpml_vector_normal(&vector
);
1161 cpml_pair_copy(&p
[2], ¢er
);
1162 cpml_pair_sub(&p
[2], &vector
);
1164 g_free(current_dup
);
1167 /* Change the end point of the last primitive */
1168 cpml_pair_to_cairo(&p
[0], cpml_primitive_get_point(last
, -1));
1170 /* Change the start point of the current primitive */
1171 cpml_pair_to_cairo(&p
[2], cpml_primitive_get_point(current
, 0));
1173 /* Add the fillet arc */
1174 data
->operation
.action
= ADG_ACTION_NONE
;
1175 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[1], &p
[2]);
1179 is_convex(const AdgPrimitive
*primitive1
, const AdgPrimitive
*primitive2
)
1182 gdouble angle1
, angle2
;
1184 cpml_primitive_vector_at(primitive1
, &v1
, -1);
1185 cpml_primitive_vector_at(primitive2
, &v2
, 0);
1187 /* Probably there is a smarter way to get this without trygonometry */
1188 angle1
= cpml_vector_angle(&v1
);
1189 angle2
= cpml_vector_angle(&v2
);
1191 if (angle1
> angle2
)
1194 return angle2
-angle1
> M_PI
;
1198 dup_reversed_pair(const gchar
*name
, AdgPair
*pair
, gpointer user_data
)
1200 AdgNamedPairData
*data
;
1204 data
= (AdgNamedPairData
*) user_data
;
1205 new_name
= g_strdup_printf("-%s", name
);
1206 cpml_pair_copy(&new_pair
, pair
);
1208 cpml_pair_transform(&new_pair
, &data
->matrix
);
1209 adg_model_set_named_pair(data
->model
, new_name
, &new_pair
);
1214 static const gchar
*
1215 action_name(AdgAction action
)
1218 case ADG_ACTION_NONE
:
1220 case ADG_ACTION_CHAMFER
:
1222 case ADG_ACTION_FILLET
: