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 #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"
65 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_path_parent_class)
66 #define PARENT_MODEL_CLASS ((AdgModelClass *) adg_path_parent_class)
69 static void finalize (GObject
*object
);
70 static void clear (AdgModel
*model
);
71 static void clear_parent (AdgModel
*model
);
72 static void changed (AdgModel
*model
);
73 static CpmlPath
* get_cpml_path (AdgTrail
*trail
);
74 static CpmlPath
* read_cpml_path (AdgPath
*path
);
75 static void append_primitive (AdgPath
*path
,
76 AdgPrimitive
*primitive
);
77 static gint
needed_pairs (CpmlPrimitiveType type
);
78 static void clear_operation (AdgPath
*path
);
79 static gboolean
append_operation (AdgPath
*path
,
82 static void do_operation (AdgPath
*path
,
85 static void do_action (AdgPath
*path
,
87 AdgPrimitive
*primitive
);
88 static void do_chamfer (AdgPath
*path
,
89 AdgPrimitive
*current
);
90 static void do_fillet (AdgPath
*path
,
91 AdgPrimitive
*current
);
92 static gboolean
is_convex (const AdgPrimitive
96 static void dup_reversed_pair (const gchar
*name
,
99 static const gchar
* action_name (AdgAction action
);
102 G_DEFINE_TYPE(AdgPath
, adg_path
, ADG_TYPE_TRAIL
);
106 adg_path_class_init(AdgPathClass
*klass
)
108 GObjectClass
*gobject_class
;
109 AdgModelClass
*model_class
;
110 AdgTrailClass
*trail_class
;
112 gobject_class
= (GObjectClass
*) klass
;
113 model_class
= (AdgModelClass
*) klass
;
114 trail_class
= (AdgTrailClass
*) klass
;
116 g_type_class_add_private(klass
, sizeof(AdgPathPrivate
));
118 gobject_class
->finalize
= finalize
;
120 model_class
->clear
= clear
;
121 model_class
->changed
= changed
;
123 trail_class
->get_cpml_path
= get_cpml_path
;
127 adg_path_init(AdgPath
*path
)
129 AdgPathPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(path
, ADG_TYPE_PATH
,
132 data
->cp_is_valid
= FALSE
;
133 data
->cpml
.array
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
134 data
->operation
.action
= ADG_ACTION_NONE
;
140 finalize(GObject
*object
)
143 AdgPathPrivate
*data
;
145 path
= (AdgPath
*) object
;
148 g_array_free(data
->cpml
.array
, TRUE
);
149 clear_operation(path
);
151 if (PARENT_OBJECT_CLASS
->finalize
)
152 PARENT_OBJECT_CLASS
->finalize(object
);
159 * Creates a new path model. The path should be constructed
160 * programmatically by using the methods provided by #AdgPath.
162 * Returns: the newly created path model
167 return g_object_new(ADG_TYPE_PATH
, NULL
);
171 * adg_path_get_current_point:
174 * Gets the current point of @path, which is conceptually the
175 * final point reached by the path so far.
177 * If there is no defined current point, %NULL is returned.
178 * It is possible to check this in advance with
179 * adg_path_has_current_point().
181 * Most #AdgPath methods alter the current point and most of them
182 * expect a current point to be defined otherwise will fail triggering
183 * a warning. Check the description of every method for specific details.
185 * Returns: the current point or %NULL on no current point set or errors
188 adg_path_get_current_point(AdgPath
*path
)
190 AdgPathPrivate
*data
;
192 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
196 if (!data
->cp_is_valid
)
203 * adg_path_has_current_point:
206 * Returns whether a current point is defined on @path.
207 * See adg_path_get_current_point() for details on the current point.
209 * Returns: whether a current point is defined
212 adg_path_has_current_point(AdgPath
*path
)
214 AdgPathPrivate
*data
;
216 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
220 return data
->cp_is_valid
;
224 * adg_path_last_primitive:
227 * Gets the last primitive appended to @path. The returned struct
228 * is owned by @path and should not be freed or modified.
230 * Returns: a pointer to the last appended primitive or %NULL on errors
233 adg_path_last_primitive(AdgPath
*path
)
235 AdgPathPrivate
*data
;
237 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
245 * adg_path_over_primitive:
248 * Gets the primitive before the last one appended to @path. The
249 * "over" term comes from forth, where the %OVER operator works
250 * on the stack in the same way as adg_path_over_primitive() works
251 * on @path. The returned struct is owned by @path and should not
252 * be freed or modified.
254 * Returns: a pointer to the primitive before the last appended one
258 adg_path_over_primitive(AdgPath
*path
)
260 AdgPathPrivate
*data
;
262 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
272 * @type: a #cairo_data_type_t value
273 * @...: point data, specified as #AdgPair pointers
275 * Generic method to append a primitive to @path. The number of #AdgPair
276 * structs depends on @type: there is no way with this function to
277 * reserve more cairo_path_data_t structs than what is needed by the
280 * This function accepts also the special #CPML_ARC primitive.
282 * If @path has no current point while the requested primitive needs it,
283 * a warning message will be triggered without other effect.
286 adg_path_append(AdgPath
*path
, CpmlPrimitiveType type
, ...)
290 va_start(var_args
, type
);
291 adg_path_append_valist(path
, type
, var_args
);
296 * adg_path_append_valist:
298 * @type: a #cairo_data_type_t value
299 * @var_args: point data, specified as #AdgPair pointers
301 * va_list version of adg_path_append().
304 adg_path_append_valist(AdgPath
*path
, CpmlPrimitiveType type
, va_list var_args
)
306 AdgPathPrivate
*data
;
307 AdgPrimitive primitive
;
309 cairo_path_data_t org
;
310 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
) {
331 pair
= va_arg(var_args
, AdgPair
*);
333 g_free(primitive
.data
);
334 g_warning(_("%s: null pair caught while parsing arguments"),
340 cpml_pair_to_cairo(pair
, path_data
);
343 /* Terminate the creation of the temporary primitive */
344 primitive
.segment
= NULL
;
346 /* Append this primitive to @path */
347 append_primitive(path
, &primitive
);
349 g_free(primitive
.data
);
353 * adg_path_append_primitive:
355 * @primitive: the #AdgPrimitive to append
357 * Appends @primitive to @path. The primitive to add is considered the
358 * continuation of the current path so the <structfield>org</structfield>
359 * component of @primitive is not used. Anyway the current point is
360 * checked against it: they must be equal or the function will fail
361 * without further processing.
364 adg_path_append_primitive(AdgPath
*path
, const AdgPrimitive
*primitive
)
366 AdgPathPrivate
*data
;
367 AdgPrimitive
*primitive_dup
;
369 g_return_if_fail(ADG_IS_PATH(path
));
370 g_return_if_fail(primitive
!= NULL
);
371 g_return_if_fail(primitive
->org
!= NULL
);
375 g_return_if_fail(primitive
->org
->point
.x
== data
->cp
.x
&&
376 primitive
->org
->point
.y
== data
->cp
.y
);
378 /* The primitive data could be modified by pending operations:
380 primitive_dup
= adg_primitive_deep_dup(primitive
);
382 append_primitive(path
, primitive_dup
);
384 g_free(primitive_dup
);
388 * adg_path_append_segment:
390 * @segment: the #AdgSegment to append
392 * Appends @segment to @path.
395 adg_path_append_segment(AdgPath
*path
, const AdgSegment
*segment
)
397 AdgPathPrivate
*data
;
399 g_return_if_fail(ADG_IS_PATH(path
));
400 g_return_if_fail(segment
!= NULL
);
404 clear_parent((AdgModel
*) path
);
405 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
406 segment
->data
, segment
->num_data
);
410 * adg_path_append_cpml_path:
412 * @cpml_path: the #cairo_path_t path to append
414 * Appends a whole #CpmlPath to @path. #CpmlPath is a superset of
415 * #cairo_path_t, so this function can be feeded with both.
418 adg_path_append_cpml_path(AdgPath
*path
, const CpmlPath
*cpml_path
)
420 AdgPathPrivate
*data
;
422 g_return_if_fail(ADG_IS_PATH(path
));
423 g_return_if_fail(cpml_path
!= NULL
);
427 clear_parent((AdgModel
*) path
);
428 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
430 cpml_path
->num_data
);
436 * @pair: the destination coordinates
438 * Begins a new segment. After this call the current point will be @pair.
441 adg_path_move_to(AdgPath
*path
, const AdgPair
*pair
)
443 adg_path_append(path
, CPML_MOVE
, pair
);
447 * adg_path_move_to_explicit:
449 * @x: the new x coordinate
450 * @y: the new y coordinate
452 * Convenient function to call adg_path_move_to() using explicit
453 * coordinates instead of #AdgPair.
456 adg_path_move_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
463 adg_path_append(path
, CPML_MOVE
, &p
);
469 * @pair: the destination coordinates
471 * Adds a line to @path from the current point to @pair. After this
472 * call the current point will be @pair.
474 * If @path has no current point before this call, this function will
475 * trigger a warning without other effect.
478 adg_path_line_to(AdgPath
*path
, const AdgPair
*pair
)
480 adg_path_append(path
, CPML_LINE
, pair
);
484 * adg_path_line_to_explicit:
486 * @x: the new x coordinate
487 * @y: the new y coordinate
489 * Convenient function to call adg_path_line_to() using explicit
490 * coordinates instead of #AdgPair.
493 adg_path_line_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
500 adg_path_append(path
, CPML_LINE
, &p
);
506 * @throught: an arbitrary point on the arc
507 * @pair: the destination coordinates
509 * Adds an arc to the path from the current point to @pair, passing
510 * throught @throught. After this call the current point will be @pair.
512 * If @path has no current point before this call, this function will
513 * trigger a warning without other effect.
516 adg_path_arc_to(AdgPath
*path
, const AdgPair
*throught
, const AdgPair
*pair
)
518 adg_path_append(path
, CPML_ARC
, throught
, pair
);
522 * adg_path_arc_to_explicit:
524 * @x1: the x coordinate of an intermediate point
525 * @y1: the y coordinate of an intermediate point
526 * @x2: the x coordinate of the end of the arc
527 * @y2: the y coordinate of the end of the arc
529 * Convenient function to call adg_path_arc_to() using explicit
530 * coordinates instead of #AdgPair.
533 adg_path_arc_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
534 gdouble x2
, gdouble y2
)
543 adg_path_append(path
, CPML_ARC
, &p
[0], &p
[1]);
549 * @control1: the first control point of the curve
550 * @control2: the second control point of the curve
551 * @pair: the destination coordinates
553 * Adds a cubic Bézier curve to the path from the current point to
554 * position @pair, using @control1 and @control2 as control points.
555 * After this call the current point will be @pair.
557 * If @path has no current point before this call, this function will
558 * trigger a warning without other effect.
561 adg_path_curve_to(AdgPath
*path
, const AdgPair
*control1
,
562 const AdgPair
*control2
, const AdgPair
*pair
)
564 adg_path_append(path
, CPML_CURVE
, control1
, control2
, pair
);
568 * adg_path_curve_to_explicit:
570 * @x1: the x coordinate of the first control point
571 * @y1: the y coordinate of the first control point
572 * @x2: the x coordinate of the second control point
573 * @y2: the y coordinate of the second control point
574 * @x3: the x coordinate of the end of the curve
575 * @y3: the y coordinate of the end of the curve
577 * Convenient function to call adg_path_curve_to() using explicit
578 * coordinates instead of #AdgPair.
581 adg_path_curve_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
582 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
593 adg_path_append(path
, CPML_CURVE
, &p
[0], &p
[1], &p
[2]);
600 * Adds a line segment to the path from the current point to the
601 * beginning of the current segment, (the most recent point passed
602 * to an adg_path_move_to()), and closes this segment.
603 * After this call the current point will be unset.
605 * The behavior of adg_path_close() is distinct from simply calling
606 * adg_line_to() with the coordinates of the segment starting point.
607 * When a closed segment is stroked, there are no caps on the ends.
608 * Instead, there is a line join connecting the final and initial
609 * primitive of the segment.
611 * If @path has no current point before this call, this function will
612 * trigger a warning without other effect.
615 adg_path_close(AdgPath
*path
)
617 adg_path_append(path
, CPML_CLOSE
);
623 * @center: coordinates of the center of the arc
624 * @r: the radius of the arc
625 * @start: the start angle, in radians
626 * @end: the end angle, in radians
628 * A more usual way to add an arc to @path. After this call, the current
629 * point will be the computed end point of the arc. The arc will be
630 * rendered in increasing angle, accordling to @start and @end. This means
631 * if @start is less than @end, the arc will be rendered in clockwise
632 * direction (accordling to the default cairo coordinate system) while if
633 * @start is greather than @end, the arc will be rendered in couterclockwise
636 * By explicitely setting the whole arc data, the start point could be
637 * different from the current point. In this case, if @path has no
638 * current point before the call a #CPML_MOVE to the start point of
639 * the arc will be automatically prepended to the arc. If @path has a
640 * current point, a #CPML_LINE to the start point of the arc will be
641 * used instead of the "move to" primitive.
644 adg_path_arc(AdgPath
*path
, const AdgPair
*center
, gdouble r
,
645 gdouble start
, gdouble end
)
647 AdgPathPrivate
*data
;
650 g_return_if_fail(ADG_IS_PATH(path
));
651 g_return_if_fail(center
!= NULL
);
654 cpml_vector_from_angle(&p
[0], start
);
655 cpml_vector_from_angle(&p
[1], (end
-start
) / 2);
656 cpml_vector_from_angle(&p
[2], end
);
658 cpml_vector_set_length(&p
[0], r
);
659 cpml_vector_set_length(&p
[1], r
);
660 cpml_vector_set_length(&p
[2], r
);
662 cpml_pair_add(&p
[0], center
);
663 cpml_pair_add(&p
[1], center
);
664 cpml_pair_add(&p
[2], center
);
666 if (!data
->cp_is_valid
)
667 adg_path_append(path
, CPML_MOVE
, &p
[0]);
668 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
669 adg_path_append(path
, CPML_LINE
, &p
[0]);
671 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
675 * adg_path_arc_explicit:
677 * @xc: x position of the center of the arc
678 * @yc: y position of the center of the arc
679 * @r: the radius of the arc
680 * @start: the start angle, in radians
681 * @end: the end angle, in radians
683 * Convenient function to call adg_path_arc() using explicit
684 * coordinates instead of #AdgPair.
687 adg_path_arc_explicit(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
688 gdouble start
, gdouble end
)
695 adg_path_arc(path
, ¢er
, r
, start
, end
);
701 * @delta1: the distance from the intersection point of the current primitive
702 * @delta2: the distance from the intersection point of the next primitive
704 * A binary action that generates a chamfer between two primitives.
705 * The first primitive involved is the current primitive, the second will
706 * be the next primitive appended to @path after this call. The second
707 * primitive is required: if the chamfer operation is not properly
708 * terminated (by not providing the second primitive), any API accessing
709 * the path in reading mode will raise a warning.
711 * An exception is a chamfer after a #CPML_CLOSE primitive. In this case,
712 * the second primitive is not required: the current close path is used
713 * as first operand while the first primitive of the current segment is
714 * used as second operand.
716 * The chamfer operation requires two lengths: @delta1 specifies the
717 * "quantity" to trim on the first primitive while @delta2 is the same
718 * applied on the second primitive. The term "quantity" means the length
719 * of the portion to cut out from the original primitive (that is the
720 * primitive as would be without the chamfer).
723 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
725 g_return_if_fail(ADG_IS_PATH(path
));
727 if (!append_operation(path
, ADG_ACTION_CHAMFER
, delta1
, delta2
))
734 * @radius: the radius of the fillet
736 * A binary action that joins to primitives with an arc.
737 * The first primitive involved is the current primitive, the second will
738 * be the next primitive appended to @path after this call. The second
739 * primitive is required: if the fillet operation is not properly
740 * terminated (by not providing the second primitive), any API accessing
741 * the path in reading mode will raise a warning.
743 * An exception is a fillet after a #CPML_CLOSE primitive. In this case,
744 * the second primitive is not required: the current close path is used
745 * as first operand while the first primitive of the current segment is
746 * used as second operand.
749 adg_path_fillet(AdgPath
*path
, gdouble radius
)
751 g_return_if_fail(ADG_IS_PATH(path
));
753 if (!append_operation(path
, ADG_ACTION_FILLET
, radius
))
760 * @vector: the slope of the axis
762 * Reflects the first segment or @path around the axis passing
763 * throught (0, 0) and with a @vector slope. The internal segment
764 * is duplicated and the proper transformation (computed from
765 * @vector) to mirror the segment is applied on all its points.
766 * The result is then reversed with cpml_segment_reverse() and
767 * appended to the original path with adg_path_append_segment().
769 * For convenience, if @vector is %NULL the path is reversed
770 * around the x axis (y=0).
773 adg_path_reflect(AdgPath
*path
, const CpmlVector
*vector
)
775 AdgNamedPairData data
;
776 AdgSegment segment
, *dup_segment
;
778 g_return_if_fail(ADG_IS_PATH(path
));
779 g_return_if_fail(vector
== NULL
|| vector
->x
!= 0 || vector
->y
!= 0);
781 data
.model
= (AdgModel
*) path
;
783 if (vector
== NULL
) {
784 cairo_matrix_init_scale(&data
.matrix
, 1, -1);
787 gdouble cos2angle
, sin2angle
;
789 cpml_pair_copy(&slope
, vector
);
790 cpml_vector_set_length(&slope
, 1);
792 if (slope
.x
== 0 && slope
.y
== 0) {
793 g_warning(_("%s: the axis of the reflection is not known"),
798 sin2angle
= 2. * vector
->x
* vector
->y
;
799 cos2angle
= 2. * vector
->x
* vector
->x
- 1;
801 cairo_matrix_init(&data
.matrix
, cos2angle
, sin2angle
,
802 sin2angle
, -cos2angle
, 0, 0);
805 if (!adg_trail_put_segment((AdgTrail
*) path
, 1, &segment
))
808 /* No need to reverse an empty segment */
809 if (segment
.num_data
== 0 || segment
.num_data
== 0)
812 dup_segment
= adg_segment_deep_dup(&segment
);
813 if (dup_segment
== NULL
)
816 cpml_segment_reverse(dup_segment
);
817 cpml_segment_transform(dup_segment
, &data
.matrix
);
818 dup_segment
->data
[0].header
.type
= CPML_LINE
;
820 adg_path_append_segment(path
, dup_segment
);
824 adg_model_foreach_named_pair(data
.model
, dup_reversed_pair
, &data
);
829 clear(AdgModel
*model
)
832 AdgPathPrivate
*data
;
834 path
= (AdgPath
*) model
;
837 g_array_set_size(data
->cpml
.array
, 0);
838 clear_operation(path
);
843 clear_parent(AdgModel
*model
)
845 if (PARENT_MODEL_CLASS
->clear
)
846 PARENT_MODEL_CLASS
->clear(model
);
850 changed(AdgModel
*model
)
854 if (PARENT_MODEL_CLASS
->changed
)
855 PARENT_MODEL_CLASS
->changed(model
);
859 get_cpml_path(AdgTrail
*trail
)
861 clear_parent((AdgModel
*) trail
);
862 return read_cpml_path((AdgPath
*) trail
);
866 read_cpml_path(AdgPath
*path
)
868 AdgPathPrivate
*data
= path
->data
;
870 /* Always regenerate the CpmlPath as it is a trivial operation */
871 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
872 data
->cpml
.path
.data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
;
873 data
->cpml
.path
.num_data
= (data
->cpml
.array
)->len
;
875 return &data
->cpml
.path
;
879 append_primitive(AdgPath
*path
, AdgPrimitive
*current
)
881 AdgPathPrivate
*data
;
882 cairo_path_data_t
*path_data
;
886 path_data
= current
->data
;
887 length
= path_data
[0].header
.length
;
889 /* Execute any pending operation */
890 do_operation(path
, path_data
);
892 /* Append the path data to the internal path array */
893 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
896 /* Set path data to point to the recently appended cairo_path_data_t
897 * primitive: the first struct is the header */
898 path_data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
+
899 (data
->cpml
.array
)->len
- length
;
901 /* Store the over primitive */
902 memcpy(&data
->over
, &data
->last
, sizeof(AdgPrimitive
));
904 /* Set the last primitive for subsequent binary operations */
905 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
906 data
->last
.segment
= NULL
;
907 data
->last
.data
= path_data
;
909 /* Save the last point as the current point, if applicable */
910 data
->cp_is_valid
= length
> 1;
912 cpml_pair_from_cairo(&data
->cp
, &path_data
[length
-1]);
914 /* Invalidate cairo_path: should be recomputed */
915 clear_parent((AdgModel
*) path
);
919 needed_pairs(CpmlPrimitiveType type
)
933 g_return_val_if_reached(0);
940 clear_operation(AdgPath
*path
)
942 AdgPathPrivate
*data
;
943 AdgOperation
*operation
;
946 operation
= &data
->operation
;
948 if (operation
->action
!= ADG_ACTION_NONE
) {
949 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
950 G_STRLOC
, action_name(operation
->action
));
951 operation
->action
= ADG_ACTION_NONE
;
956 append_operation(AdgPath
*path
, AdgAction action
, ...)
958 AdgPathPrivate
*data
;
959 AdgOperation
*operation
;
964 if (data
->last
.data
== NULL
) {
965 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
966 G_STRLOC
, action_name(action
));
970 operation
= &data
->operation
;
971 if (operation
->action
!= ADG_ACTION_NONE
) {
972 g_warning(_("%s: requested a `%s' operation while a `%s' operation is active"),
973 G_STRLOC
, action_name(action
), action_name(operation
->action
));
974 ADG_MESSAGE("TODO: this is a rude simplification, as a lot of "
975 "actions could be chained up. As an example, a fillet "
976 "followed by a polar chamfer is quite common.");
980 va_start(var_args
, action
);
984 case ADG_ACTION_CHAMFER
:
985 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
986 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
989 case ADG_ACTION_FILLET
:
990 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
993 case ADG_ACTION_NONE
:
998 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC
, action
);
1003 operation
->action
= action
;
1006 if (data
->last
.data
[0].header
.type
== CPML_CLOSE
) {
1007 /* Special case: an action with the close primitive should
1008 * be resolved now by changing the close primitive to a
1009 * line-to and using it as second operand and use the first
1010 * primitive of the current segment as first operand */
1012 cairo_path_data_t
*path_data
;
1013 CpmlSegment segment
;
1014 CpmlPrimitive current
;
1016 length
= data
->cpml
.array
->len
;
1018 /* Ensure the close path primitive is not the only data */
1019 g_return_val_if_fail(length
> 1, FALSE
);
1021 /* Allocate one more item once for all to accept the
1022 * conversion from a close to line-to primitive */
1023 data
->cpml
.array
= g_array_set_size(data
->cpml
.array
, length
+ 1);
1024 path_data
= (cairo_path_data_t
*) data
->cpml
.array
->data
;
1025 --data
->cpml
.array
->len
;
1027 /* Set segment and current (the first primitive of segment) */
1028 cpml_segment_from_cairo(&segment
, read_cpml_path(path
));
1029 while (cpml_segment_next(&segment
))
1031 cpml_primitive_from_segment(¤t
, &segment
);
1033 /* Convert close path to a line-to primitive */
1034 ++data
->cpml
.array
->len
;
1035 path_data
[length
- 1].header
.type
= CPML_LINE
;
1036 path_data
[length
- 1].header
.length
= 2;
1037 path_data
[length
] = *current
.org
;
1039 data
->last
.segment
= &segment
;
1040 data
->last
.org
= &path_data
[length
- 2];
1041 data
->last
.data
= &path_data
[length
- 1];
1043 do_action(path
, action
, ¤t
);
1051 do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
1053 AdgPathPrivate
*data
;
1056 AdgPrimitive current
;
1057 cairo_path_data_t current_org
;
1060 action
= data
->operation
.action
;
1061 cpml_segment_from_cairo(&segment
, read_cpml_path(path
));
1063 /* Construct the current primitive, that is the primitive to be
1064 * mixed with the last primitive with the specified operation.
1065 * Its org is a copy of the end point of the last primitive: it can be
1066 * modified without affecting anything else. It is expected the operation
1067 * functions will add to @path the primitives required but NOT to add
1068 * @current, as this one will be inserted automatically. */
1069 current
.segment
= &segment
;
1070 current
.org
= ¤t_org
;
1071 current
.data
= path_data
;
1072 cpml_pair_to_cairo(&data
->cp
, ¤t_org
);
1074 do_action(path
, action
, ¤t
);
1078 do_action(AdgPath
*path
, AdgAction action
, AdgPrimitive
*primitive
)
1081 case ADG_ACTION_NONE
:
1083 case ADG_ACTION_CHAMFER
:
1084 do_chamfer(path
, primitive
);
1086 case ADG_ACTION_FILLET
:
1087 do_fillet(path
, primitive
);
1090 g_return_if_reached();
1095 do_chamfer(AdgPath
*path
, AdgPrimitive
*current
)
1097 AdgPathPrivate
*data
;
1099 gdouble delta1
, delta2
;
1105 delta1
= data
->operation
.data
.chamfer
.delta1
;
1106 len1
= cpml_primitive_get_length(last
);
1108 if (delta1
>= len1
) {
1109 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1110 G_STRLOC
, delta1
, len1
);
1114 delta2
= data
->operation
.data
.chamfer
.delta2
;
1115 len2
= cpml_primitive_get_length(current
);
1117 if (delta2
>= len2
) {
1118 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1119 G_STRLOC
, delta1
, len1
);
1123 /* Change the end point of the last primitive */
1124 cpml_primitive_put_pair_at(last
, 1. - delta1
/ len1
, &pair
);
1125 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(last
, -1));
1127 /* Change the start point of the current primitive */
1128 cpml_primitive_put_pair_at(current
, delta2
/ len2
, &pair
);
1129 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(current
, 0));
1131 /* Add the chamfer line */
1132 data
->operation
.action
= ADG_ACTION_NONE
;
1133 adg_path_append(path
, CPML_LINE
, &pair
);
1137 do_fillet(AdgPath
*path
, AdgPrimitive
*current
)
1139 AdgPathPrivate
*data
;
1140 AdgPrimitive
*last
, *current_dup
, *last_dup
;
1141 gdouble radius
, offset
, pos
;
1142 AdgPair center
, vector
, p
[3];
1146 current_dup
= adg_primitive_deep_dup(current
);
1147 last_dup
= adg_primitive_deep_dup(last
);
1148 radius
= data
->operation
.data
.fillet
.radius
;
1149 offset
= is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1151 /* Find the center of the fillet from the intersection between
1152 * the last and current primitives offseted by radius */
1153 cpml_primitive_offset(current_dup
, offset
);
1154 cpml_primitive_offset(last_dup
, offset
);
1155 if (cpml_primitive_put_intersections(current_dup
, last_dup
, 1, ¢er
) == 0) {
1156 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1158 g_free(current_dup
);
1163 /* Compute the start point of the fillet */
1164 pos
= cpml_primitive_get_closest_pos(last_dup
, ¢er
);
1165 cpml_primitive_put_vector_at(last_dup
, pos
, &vector
);
1166 cpml_vector_set_length(&vector
, offset
);
1167 cpml_vector_normal(&vector
);
1168 cpml_pair_copy(&p
[0], ¢er
);
1169 cpml_pair_sub(&p
[0], &vector
);
1171 /* Compute the mid point of the fillet */
1172 cpml_pair_from_cairo(&vector
, current
->org
);
1173 cpml_pair_sub(&vector
, ¢er
);
1174 cpml_vector_set_length(&vector
, radius
);
1175 cpml_pair_copy(&p
[1], ¢er
);
1176 cpml_pair_add(&p
[1], &vector
);
1178 /* Compute the end point of the fillet */
1179 pos
= cpml_primitive_get_closest_pos(current_dup
, ¢er
);
1180 cpml_primitive_put_vector_at(current_dup
, pos
, &vector
);
1181 cpml_vector_set_length(&vector
, offset
);
1182 cpml_vector_normal(&vector
);
1183 cpml_pair_copy(&p
[2], ¢er
);
1184 cpml_pair_sub(&p
[2], &vector
);
1186 g_free(current_dup
);
1189 /* Change the end point of the last primitive */
1190 cpml_pair_to_cairo(&p
[0], cpml_primitive_get_point(last
, -1));
1192 /* Change the start point of the current primitive */
1193 cpml_pair_to_cairo(&p
[2], cpml_primitive_get_point(current
, 0));
1195 /* Add the fillet arc */
1196 data
->operation
.action
= ADG_ACTION_NONE
;
1197 adg_path_append(path
, CPML_ARC
, &p
[1], &p
[2]);
1201 is_convex(const AdgPrimitive
*primitive1
, const AdgPrimitive
*primitive2
)
1204 gdouble angle1
, angle2
;
1206 cpml_primitive_put_vector_at(primitive1
, -1, &v1
);
1207 cpml_primitive_put_vector_at(primitive2
, 0, &v2
);
1209 /* Probably there is a smarter way to get this without trygonometry */
1210 angle1
= cpml_vector_angle(&v1
);
1211 angle2
= cpml_vector_angle(&v2
);
1213 if (angle1
> angle2
)
1216 return angle2
-angle1
> M_PI
;
1220 dup_reversed_pair(const gchar
*name
, AdgPair
*pair
, gpointer user_data
)
1222 AdgNamedPairData
*data
;
1226 data
= (AdgNamedPairData
*) user_data
;
1227 new_name
= g_strdup_printf("-%s", name
);
1228 cpml_pair_copy(&new_pair
, pair
);
1230 cpml_pair_transform(&new_pair
, &data
->matrix
);
1231 adg_model_set_named_pair(data
->model
, new_name
, &new_pair
);
1236 static const gchar
*
1237 action_name(AdgAction action
)
1240 case ADG_ACTION_NONE
:
1242 case ADG_ACTION_CHAMFER
:
1244 case ADG_ACTION_FILLET
: