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.
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 gboolean cp_is_valid
);
80 static void clear_operation (AdgPath
*path
);
81 static gboolean
append_operation (AdgPath
*path
,
84 static void do_operation (AdgPath
*path
,
87 static void do_chamfer (AdgPath
*path
,
88 AdgPrimitive
*current
);
89 static void do_fillet (AdgPath
*path
,
90 AdgPrimitive
*current
);
91 static gboolean
is_convex (const AdgPrimitive
95 static void dup_reversed_pair (const gchar
*name
,
100 G_DEFINE_TYPE(AdgPath
, adg_path
, ADG_TYPE_TRAIL
);
104 adg_path_class_init(AdgPathClass
*klass
)
106 GObjectClass
*gobject_class
;
107 AdgModelClass
*model_class
;
108 AdgTrailClass
*trail_class
;
110 gobject_class
= (GObjectClass
*) klass
;
111 model_class
= (AdgModelClass
*) klass
;
112 trail_class
= (AdgTrailClass
*) klass
;
114 g_type_class_add_private(klass
, sizeof(AdgPathPrivate
));
116 gobject_class
->finalize
= finalize
;
118 model_class
->clear
= clear
;
119 model_class
->changed
= changed
;
121 trail_class
->get_cpml_path
= get_cpml_path
;
125 adg_path_init(AdgPath
*path
)
127 AdgPathPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(path
, ADG_TYPE_PATH
,
130 data
->cp_is_valid
= FALSE
;
131 data
->cpml
.array
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
132 data
->operation
.action
= ADG_ACTION_NONE
;
138 finalize(GObject
*object
)
141 AdgPathPrivate
*data
;
143 path
= (AdgPath
*) object
;
146 g_array_free(data
->cpml
.array
, TRUE
);
147 clear_operation(path
);
149 if (PARENT_OBJECT_CLASS
->finalize
!= NULL
)
150 PARENT_OBJECT_CLASS
->finalize(object
);
157 * Creates a new path model. The path should be constructed
158 * programmatically by using the methods provided by #AdgPath.
160 * Returns: the newly created path model
165 return g_object_new(ADG_TYPE_PATH
, NULL
);
169 * adg_path_current_point:
172 * Gets the current point of @path, which is conceptually the
173 * final point reached by the path so far.
175 * If there is no defined current point, %NULL is returned.
176 * It is possible to check this in advance with
177 * adg_path_has_current_point().
179 * Most #AdgPath methods alter the current point and most of them
180 * expect a current point to be defined otherwise will fail triggering
181 * a warning. Check the description of every method for specific details.
183 * Returns: the current point or %NULL on no current point set or errors
186 adg_path_current_point(AdgPath
*path
)
188 AdgPathPrivate
*data
;
190 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
194 if (!data
->cp_is_valid
)
201 * adg_path_has_current_point:
204 * Returns whether a current point is defined on @path.
205 * See adg_path_get_current_point() for details on the current point.
207 * Returns: whether a current point is defined
210 adg_path_has_current_point(AdgPath
*path
)
212 AdgPathPrivate
*data
;
214 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
218 return data
->cp_is_valid
;
222 * adg_path_last_primitive:
225 * Gets the last primitive appended to @path. The returned struct
226 * is owned by @path and should not be freed or modified.
228 * Returns: a pointer to the last appended primitive or %NULL on errors
231 adg_path_last_primitive(AdgPath
*path
)
233 AdgPathPrivate
*data
;
235 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
243 * adg_path_over_primitive:
246 * Gets the primitive before the last one appended to @path. The
247 * "over" term comes from forth, where the %OVER operator works
248 * on the stack in the same way as adg_path_over_primitive() works
249 * on @path. The returned struct is owned by @path and should not
250 * be freed or modified.
252 * Returns: a pointer to the primitive before the last appended one
256 adg_path_over_primitive(AdgPath
*path
)
258 AdgPathPrivate
*data
;
260 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
270 * @type: a #cairo_data_type_t value
271 * @...: point data, specified as #AdgPair pointers
273 * Generic method to append a primitive to @path. The number of #AdgPair
274 * structs depends on @type: there is no way with this function to
275 * reserve more cairo_path_data_t structs than what is needed by the
278 * This function accepts also the special %CAIRO_PATH_ARC_TO primitive.
280 * If @path has no current point while the requested primitive needs it,
281 * a warning message will be triggered without other effect.
284 adg_path_append(AdgPath
*path
, CpmlPrimitiveType type
, ...)
288 va_start(var_args
, type
);
289 adg_path_append_valist(path
, type
, var_args
);
294 * adg_path_append_valist:
296 * @type: a #cairo_data_type_t value
297 * @var_args: point data, specified as #AdgPair pointers
299 * va_list version of adg_path_append().
302 adg_path_append_valist(AdgPath
*path
, CpmlPrimitiveType type
, va_list var_args
)
304 AdgPathPrivate
*data
;
305 AdgPrimitive primitive
;
307 cairo_path_data_t org
;
308 cairo_path_data_t
*path_data
;
310 g_return_if_fail(ADG_IS_PATH(path
));
313 length
= needed_pairs(type
, data
->cp_is_valid
);
317 /* Set a copy of the current point as the primitive origin */
318 cpml_pair_to_cairo(&data
->cp
, &org
);
319 primitive
.org
= &org
;
321 /* Build the cairo_path_data_t array */
322 primitive
.data
= path_data
= g_new(cairo_path_data_t
, length
);
324 path_data
->header
.type
= type
;
325 path_data
->header
.length
= length
;
327 for (cnt
= 1; cnt
< length
; ++ cnt
) {
329 cpml_pair_to_cairo(va_arg(var_args
, AdgPair
*), path_data
);
332 /* Terminate the creation of the temporary primitive */
333 primitive
.segment
= NULL
;
335 /* Append this primitive to @path */
336 append_primitive(path
, &primitive
);
338 g_free(primitive
.data
);
342 * adg_path_append_primitive:
344 * @primitive: the #AdgPrimitive to append
346 * Appends @primitive to @path. The primitive to add is considered the
347 * continuation of the current path so the <structfield>org</structfield>
348 * component of @primitive is not used. Anyway the current poins is
349 * checked against it: they must be equal or the function will fail
350 * without further processing.
353 adg_path_append_primitive(AdgPath
*path
, const AdgPrimitive
*primitive
)
355 AdgPathPrivate
*data
;
356 AdgPrimitive
*primitive_dup
;
358 g_return_if_fail(ADG_IS_PATH(path
));
359 g_return_if_fail(primitive
!= NULL
);
363 g_return_if_fail(primitive
->org
->point
.x
== data
->cp
.x
&&
364 primitive
->org
->point
.y
== data
->cp
.y
);
366 /* The primitive data could be modified by pending operations:
368 primitive_dup
= adg_primitive_deep_dup(primitive
);
370 append_primitive(path
, primitive_dup
);
372 g_free(primitive_dup
);
376 * adg_path_append_segment:
378 * @segment: the #AdgSegment to append
380 * Appends @segment to @path.
383 adg_path_append_segment(AdgPath
*path
, const AdgSegment
*segment
)
385 AdgPathPrivate
*data
;
387 g_return_if_fail(ADG_IS_PATH(path
));
388 g_return_if_fail(segment
!= NULL
);
392 clear_parent((AdgModel
*) path
);
393 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
394 segment
->data
, segment
->num_data
);
398 * adg_path_append_cpml_path:
400 * @cpml_path: the #cairo_path_t path to append
402 * Appends a whole #CpmlPath to @path. #CpmlPath is a superset of
403 * #cairo_path_t, so this function can be feeded with both.
406 adg_path_append_cpml_path(AdgPath
*path
, const CpmlPath
*cpml_path
)
408 AdgPathPrivate
*data
;
410 g_return_if_fail(ADG_IS_PATH(path
));
414 clear_parent((AdgModel
*) path
);
415 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
417 cpml_path
->num_data
);
423 * @pair: the destination coordinates
425 * Begins a new segment. After this call the current point will be @pair.
428 adg_path_move_to(AdgPath
*path
, const AdgPair
*pair
)
430 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, pair
);
434 * adg_path_move_to_explicit:
436 * @x: the new x coordinate
437 * @y: the new y coordinate
439 * Convenient function to call adg_path_move_to() using explicit
440 * coordinates instead of #AdgPair.
443 adg_path_move_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
450 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, &p
);
456 * @pair: the destination coordinates
458 * Adds a line to @path from the current point to @pair. After this
459 * call the current point will be @pair.
461 * If @path has no current point before this call, this function will
462 * trigger a warning without other effect.
465 adg_path_line_to(AdgPath
*path
, const AdgPair
*pair
)
467 adg_path_append(path
, CAIRO_PATH_LINE_TO
, pair
);
471 * adg_path_line_to_explicit:
473 * @x: the new x coordinate
474 * @y: the new y coordinate
476 * Convenient function to call adg_path_line_to() using explicit
477 * coordinates instead of #AdgPair.
480 adg_path_line_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
487 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &p
);
493 * @throught: an arbitrary point on the arc
494 * @pair: the destination coordinates
496 * Adds an arc to the path from the current point to @pair, passing
497 * throught @throught. After this call the current point will be @pair.
499 * If @path has no current point before this call, this function will
500 * trigger a warning without other effect.
503 adg_path_arc_to(AdgPath
*path
, const AdgPair
*throught
, const AdgPair
*pair
)
505 adg_path_append(path
, CAIRO_PATH_ARC_TO
, throught
, pair
);
509 * adg_path_arc_to_explicit:
511 * @x1: the x coordinate of an intermediate point
512 * @y1: the y coordinate of an intermediate point
513 * @x2: the x coordinate of the end of the arc
514 * @y2: the y coordinate of the end of the arc
516 * Convenient function to call adg_path_arc_to() using explicit
517 * coordinates instead of #AdgPair.
520 adg_path_arc_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
521 gdouble x2
, gdouble y2
)
530 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[0], &p
[1]);
536 * @control1: the first control point of the curve
537 * @control2: the second control point of the curve
538 * @pair: the destination coordinates
540 * Adds a cubic Bézier curve to the path from the current point to
541 * position @pair, using @control1 and @control2 as control points.
542 * After this call the current point will be @pair.
544 * If @path has no current point before this call, this function will
545 * trigger a warning without other effect.
548 adg_path_curve_to(AdgPath
*path
, const AdgPair
*control1
,
549 const AdgPair
*control2
, const AdgPair
*pair
)
551 adg_path_append(path
, CAIRO_PATH_CURVE_TO
, control1
, control2
, pair
);
555 * adg_path_curve_to_explicit:
557 * @x1: the x coordinate of the first control point
558 * @y1: the y coordinate of the first control point
559 * @x2: the x coordinate of the second control point
560 * @y2: the y coordinate of the second control point
561 * @x3: the x coordinate of the end of the curve
562 * @y3: the y coordinate of the end of the curve
564 * Convenient function to call adg_path_curve_to() using explicit
565 * coordinates instead of #AdgPair.
568 adg_path_curve_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
569 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
580 adg_path_append(path
, CAIRO_PATH_CURVE_TO
, &p
[0], &p
[1], &p
[2]);
587 * Adds a line segment to the path from the current point to the
588 * beginning of the current segment, (the most recent point passed
589 * to an adg_path_move_to()), and closes this segment.
590 * After this call the current point will be unset.
592 * The behavior of adg_path_close() is distinct from simply calling
593 * adg_line_to() with the coordinates of the segment starting point.
594 * When a closed segment is stroked, there are no caps on the ends.
595 * Instead, there is a line join connecting the final and initial
596 * primitive of the segment.
598 * If @path has no current point before this call, this function will
599 * trigger a warning without other effect.
602 adg_path_close(AdgPath
*path
)
604 adg_path_append(path
, CAIRO_PATH_CLOSE_PATH
);
610 * @center: coordinates of the center of the arc
611 * @r: the radius of the arc
612 * @start: the start angle, in radians
613 * @end: the end angle, in radians
615 * A more usual way to add an arc to @path. After this call, the current
616 * point will be the computed end point of the arc. The arc will be
617 * rendered in increasing angle, accordling to @start and @end. This means
618 * if @start is less than @end, the arc will be rendered in clockwise
619 * direction (accordling to the default cairo coordinate system) while if
620 * @start is greather than @end, the arc will be rendered in couterclockwise
623 * By explicitely setting the whole arc data, the start point could be
624 * different from the current point. In this case, if @path has no
625 * current point before the call a %CAIRO_PATH_MOVE_TO to the start
626 * point of the arc will be automatically prepended to the arc.
627 * If @path has a current point, a %CAIRO_PATH_LINE_TO to the start
628 * point of the arc will be used instead of the "move to" primitive.
631 adg_path_arc(AdgPath
*path
, const AdgPair
*center
, gdouble r
,
632 gdouble start
, gdouble end
)
634 AdgPathPrivate
*data
;
637 g_return_if_fail(ADG_IS_PATH(path
));
640 cpml_vector_from_angle(&p
[0], start
);
641 cpml_vector_from_angle(&p
[1], (end
-start
) / 2);
642 cpml_vector_from_angle(&p
[2], end
);
644 cpml_vector_set_length(&p
[0], r
);
645 cpml_vector_set_length(&p
[1], r
);
646 cpml_vector_set_length(&p
[2], r
);
648 cpml_pair_add(&p
[0], center
);
649 cpml_pair_add(&p
[1], center
);
650 cpml_pair_add(&p
[2], center
);
652 if (!data
->cp_is_valid
)
653 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, &p
[0]);
654 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
655 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &p
[0]);
657 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[1], &p
[2]);
661 * adg_path_arc_explicit:
663 * @xc: x position of the center of the arc
664 * @yc: y position of the center of the arc
665 * @r: the radius of the arc
666 * @start: the start angle, in radians
667 * @end: the end angle, in radians
669 * Convenient function to call adg_path_arc() using explicit
670 * coordinates instead of #AdgPair.
673 adg_path_arc_explicit(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
674 gdouble start
, gdouble end
)
681 adg_path_arc(path
, ¢er
, r
, start
, end
);
687 * @delta1: the distance from the intersection point of the current primitive
688 * @delta2: the distance from the intersection point of the next primitive
690 * A binary action that generates a chamfer between two primitives.
691 * The first primitive involved is the current primitive, the second will
692 * be the next primitive appended to @path after this call. The second
693 * primitive is required: if the chamfer operation is not properly
694 * terminated (by not providing the second primitive), any API accessing
695 * the path in reading mode will raise a warning.
697 * The chamfer operation requires two lengths: @delta1 specifies the
698 * "quantity" to trim on the first primitive while @delta2 is the same
699 * applied on the second primitive. The term "quantity" means the length
700 * of the portion to cut out from the original primitive (that is the
701 * primitive as would be without the chamfer).
704 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
706 g_return_if_fail(ADG_IS_PATH(path
));
708 if (!append_operation(path
, ADG_ACTION_CHAMFER
, delta1
, delta2
))
715 * @radius: the radius of the fillet
717 * A binary action that joins to primitives with an arc.
718 * The first primitive involved is the current primitive, the second will
719 * be the next primitive appended to @path after this call. The second
720 * primitive is required: if the fillet operation is not properly
721 * terminated (by not providing the second primitive), any API accessing
722 * the path in reading mode will raise a warning.
725 adg_path_fillet(AdgPath
*path
, gdouble radius
)
727 g_return_if_fail(ADG_IS_PATH(path
));
729 if (!append_operation(path
, ADG_ACTION_FILLET
, radius
))
736 * @vector: the slope of the axis
738 * Reflects the first segment or @path around the axis passing
739 * throught (0, 0) and with a @vector slope. The internal segment
740 * is duplicated and the proper transformation (computed from
741 * @vector) to mirror the segment is applied on all its points.
742 * The result is then reversed with cpml_segment_reverse() and
743 * appended to the original path with adg_path_append_segment().
746 adg_path_reflect(AdgPath
*path
, const CpmlVector
*vector
)
748 AdgNamedPairData data
;
749 AdgSegment segment
, *dup_segment
;
751 g_return_if_fail(ADG_IS_PATH(path
));
753 data
.model
= (AdgModel
*) path
;
755 if (vector
== NULL
) {
756 cairo_matrix_init_scale(&data
.matrix
, 1, -1);
759 gdouble cos2angle
, sin2angle
;
761 cpml_pair_copy(&slope
, vector
);
762 cpml_vector_set_length(&slope
, 1);
764 if (slope
.x
== 0 && slope
.y
== 0) {
765 g_warning(_("%s: the axis of the reflection is not known"),
770 sin2angle
= 2. * vector
->x
* vector
->y
;
771 cos2angle
= 2. * vector
->x
* vector
->x
- 1;
773 cairo_matrix_init(&data
.matrix
, cos2angle
, sin2angle
,
774 sin2angle
, -cos2angle
, 0, 0);
777 adg_trail_get_segment((AdgTrail
*) path
, &segment
, 1);
778 dup_segment
= adg_segment_deep_dup(&segment
);
780 cpml_segment_reverse(dup_segment
);
781 cpml_segment_transform(dup_segment
, &data
.matrix
);
782 dup_segment
->data
[0].header
.type
= CAIRO_PATH_LINE_TO
;
784 adg_path_append_segment(path
, dup_segment
);
788 adg_model_foreach_named_pair(data
.model
, dup_reversed_pair
, &data
);
793 clear(AdgModel
*model
)
796 AdgPathPrivate
*data
;
798 path
= (AdgPath
*) model
;
801 g_array_set_size(data
->cpml
.array
, 0);
802 clear_operation(path
);
807 clear_parent(AdgModel
*model
)
809 if (PARENT_MODEL_CLASS
->clear
!= NULL
)
810 PARENT_MODEL_CLASS
->clear(model
);
814 changed(AdgModel
*model
)
818 if (PARENT_MODEL_CLASS
->changed
!= NULL
)
819 PARENT_MODEL_CLASS
->changed(model
);
823 get_cpml_path(AdgTrail
*trail
)
825 clear_parent((AdgModel
*) trail
);
826 return read_cpml_path((AdgPath
*) trail
);
830 read_cpml_path(AdgPath
*path
)
832 AdgPathPrivate
*data
= path
->data
;
834 /* Always regenerate the CpmlPath as it is a trivial operation */
835 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
836 data
->cpml
.path
.data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
;
837 data
->cpml
.path
.num_data
= (data
->cpml
.array
)->len
;
839 return &data
->cpml
.path
;
843 append_primitive(AdgPath
*path
, AdgPrimitive
*current
)
845 AdgPathPrivate
*data
;
846 cairo_path_data_t
*path_data
;
850 path_data
= current
->data
;
851 length
= path_data
[0].header
.length
;
853 /* Execute any pending operation */
854 do_operation(path
, path_data
);
856 /* Append the path data to the internal path array */
857 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
860 /* Set path data to point to the recently appended cairo_path_data_t
861 * primitive: the first struct is the header */
862 path_data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
+
863 (data
->cpml
.array
)->len
- length
;
865 /* Store the over primitive */
866 memcpy(&data
->over
, &data
->last
, sizeof(AdgPrimitive
));
868 /* Set the last primitive for subsequent binary operations */
869 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
870 data
->last
.segment
= NULL
;
871 data
->last
.data
= path_data
;
873 /* Save the last point as the current point, if applicable */
874 data
->cp_is_valid
= length
> 1;
876 cpml_pair_from_cairo(&data
->cp
, &path_data
[length
-1]);
878 /* Invalidate cairo_path: should be recomputed */
879 clear_parent((AdgModel
*) path
);
883 needed_pairs(CpmlPrimitiveType type
, gboolean cp_is_valid
)
887 case CAIRO_PATH_CLOSE_PATH
:
888 g_return_val_if_fail(cp_is_valid
, 0);
891 case CAIRO_PATH_MOVE_TO
:
894 case CAIRO_PATH_LINE_TO
:
895 g_return_val_if_fail(cp_is_valid
, 0);
898 case CAIRO_PATH_ARC_TO
:
899 g_return_val_if_fail(cp_is_valid
, 0);
902 case CAIRO_PATH_CURVE_TO
:
903 g_return_val_if_fail(cp_is_valid
, 0);
907 g_return_val_if_reached(0);
914 clear_operation(AdgPath
*path
)
916 AdgPathPrivate
*data
;
917 AdgOperation
*operation
;
920 operation
= &data
->operation
;
922 if (operation
->action
!= ADG_ACTION_NONE
) {
923 g_warning(_("%s: a `%d' operation is still active while clearing the path"),
924 G_STRLOC
, operation
->action
);
925 operation
->action
= ADG_ACTION_NONE
;
930 append_operation(AdgPath
*path
, AdgAction action
, ...)
932 AdgPathPrivate
*data
;
933 AdgOperation
*operation
;
938 if (!data
->cp_is_valid
) {
939 g_warning(_("%s: requested a `%d' operation on a path without current primitive"),
944 operation
= &data
->operation
;
945 if (operation
->action
!= ADG_ACTION_NONE
) {
946 g_warning(_("%s: requested a `%d' operation while a `%d' operation is active"),
947 G_STRLOC
, action
, operation
->action
);
948 ADG_MESSAGE("TODO: this is a rude simplification, as a lot of "
949 "actions could be chained up. As an example, a fillet "
950 "followed by a polar chamfer is quite common.");
954 va_start(var_args
, action
);
958 case ADG_ACTION_CHAMFER
:
959 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
960 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
963 case ADG_ACTION_FILLET
:
964 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
967 case ADG_ACTION_NONE
:
972 g_warning(_("%s: `%d' operation not recognized"),
978 operation
->action
= action
;
985 do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
987 AdgPathPrivate
*data
;
990 AdgPrimitive current
;
991 cairo_path_data_t current_org
;
994 action
= data
->operation
.action
;
995 cpml_segment_from_cairo(&segment
, read_cpml_path(path
));
997 /* Construct the current primitive, that is the primitive to be inserted.
998 * Its org is a copy of the end point of the last primitive: it can be
999 * modified without affecting anything else. It is expected the operation
1000 * functions will add to @path the primitives required but NOT to add
1001 * @current, as this one will be inserted automatically. */
1002 current
.segment
= &segment
;
1003 current
.org
= ¤t_org
;
1004 current
.data
= path_data
;
1005 cpml_pair_to_cairo(&data
->cp
, ¤t_org
);
1009 case ADG_ACTION_NONE
:
1012 case ADG_ACTION_CHAMFER
:
1013 do_chamfer(path
, ¤t
);
1016 case ADG_ACTION_FILLET
:
1017 do_fillet(path
, ¤t
);
1021 g_warning(_("%s: `%d' operation not recognized"),
1028 do_chamfer(AdgPath
*path
, AdgPrimitive
*current
)
1030 AdgPathPrivate
*data
;
1032 gdouble delta1
, delta2
;
1038 delta1
= data
->operation
.data
.chamfer
.delta1
;
1039 len1
= cpml_primitive_length(last
);
1041 if (delta1
>= len1
) {
1042 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1043 G_STRLOC
, delta1
, len1
);
1047 delta2
= data
->operation
.data
.chamfer
.delta2
;
1048 len2
= cpml_primitive_length(current
);
1050 if (delta2
>= len2
) {
1051 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1052 G_STRLOC
, delta1
, len1
);
1056 /* Change the end point of the last primitive */
1057 cpml_primitive_pair_at(last
, &pair
, 1. - delta1
/ len1
);
1058 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(last
, -1));
1060 /* Change the start point of the current primitive */
1061 cpml_primitive_pair_at(current
, &pair
, delta2
/ len2
);
1062 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(current
, 0));
1064 /* Add the chamfer line */
1065 data
->operation
.action
= ADG_ACTION_NONE
;
1066 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &pair
);
1070 do_fillet(AdgPath
*path
, AdgPrimitive
*current
)
1072 AdgPathPrivate
*data
;
1073 AdgPrimitive
*last
, *current_dup
, *last_dup
;
1074 gdouble radius
, offset
, pos
;
1075 AdgPair center
, vector
, p
[3];
1079 current_dup
= adg_primitive_deep_dup(current
);
1081 /* Force current_dup to point to the original segment so a
1082 * CAIRO_PATH_CLOSE_PATH primitive will work as expected */
1083 current_dup
->segment
= current
->segment
;
1085 last_dup
= adg_primitive_deep_dup(last
);
1086 radius
= data
->operation
.data
.fillet
.radius
;
1087 offset
= is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1089 /* Find the center of the fillet from the intersection between
1090 * the last and current primitives offseted by radius */
1091 cpml_primitive_offset(current_dup
, offset
);
1092 cpml_primitive_offset(last_dup
, offset
);
1093 if (cpml_primitive_intersection(current_dup
, last_dup
,
1095 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1097 g_free(current_dup
);
1102 /* Compute the start point of the fillet */
1103 pos
= cpml_primitive_near_pos(last_dup
, ¢er
);
1104 cpml_primitive_vector_at(last_dup
, &vector
, pos
);
1105 cpml_vector_set_length(&vector
, offset
);
1106 cpml_vector_normal(&vector
);
1107 cpml_pair_sub(cpml_pair_copy(&p
[0], ¢er
), &vector
);
1109 /* Compute the mid point of the fillet */
1110 cpml_pair_from_cairo(&vector
, current
->org
);
1111 cpml_pair_sub(&vector
, ¢er
);
1112 cpml_vector_set_length(&vector
, radius
);
1113 cpml_pair_add(cpml_pair_copy(&p
[1], ¢er
), &vector
);
1115 /* Compute the end point of the fillet */
1116 pos
= cpml_primitive_near_pos(current_dup
, ¢er
);
1117 cpml_primitive_vector_at(current_dup
, &vector
, pos
);
1118 cpml_vector_set_length(&vector
, offset
);
1119 cpml_vector_normal(&vector
);
1120 cpml_pair_sub(cpml_pair_copy(&p
[2], ¢er
), &vector
);
1122 g_free(current_dup
);
1125 /* Change the end point of the last primitive */
1126 cpml_pair_to_cairo(&p
[0], cpml_primitive_get_point(last
, -1));
1128 /* Change the start point of the current primitive */
1129 cpml_pair_to_cairo(&p
[2], cpml_primitive_get_point(current
, 0));
1131 /* Add the fillet arc */
1132 data
->operation
.action
= ADG_ACTION_NONE
;
1133 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[1], &p
[2]);
1137 is_convex(const AdgPrimitive
*primitive1
, const AdgPrimitive
*primitive2
)
1140 gdouble angle1
, angle2
;
1142 cpml_primitive_vector_at(primitive1
, &v1
, -1);
1143 cpml_primitive_vector_at(primitive2
, &v2
, 0);
1145 /* Probably there is a smarter way to get this without trygonometry */
1146 angle1
= cpml_vector_angle(&v1
);
1147 angle2
= cpml_vector_angle(&v2
);
1149 if (angle1
> angle2
)
1152 return angle2
-angle1
> M_PI
;
1156 dup_reversed_pair(const gchar
*name
, AdgPair
*pair
, gpointer user_data
)
1158 AdgNamedPairData
*data
;
1162 data
= (AdgNamedPairData
*) user_data
;
1163 new_name
= g_strdup_printf("-%s", name
);
1164 cpml_pair_copy(&new_pair
, pair
);
1166 cpml_pair_transform(&new_pair
, &data
->matrix
);
1167 adg_model_set_named_pair(data
->model
, new_name
, &new_pair
);