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 is a virtual path: in other words it is a
26 * non-rendered #cairo_path_t struct. This class implements methods
27 * to manipulate the underlying cairo path.
29 * Although some of the provided methods are clearly based on the
30 * original cairo path manipulation API, their behavior could be
31 * sligthly different. This is intentional, because the ADG provides
32 * additional path manipulation algorithms, sometime quite complex,
33 * and a more restrictive filter on the path quality is required.
34 * Also, the ADG is designed to be used by technicians while cairo
35 * targets a broader range of developers.
37 * As an example, following the rule of the less surprise, some
38 * cairo functions guess the current point when it is not defined,
39 * while the #AdgPath methods trigger a warning without other effect.
40 * Furthermore, after a cairo_path_close_path() call a %MOVE_TO
41 * primitive to the starting point of the segment is automatically
42 * added by cairo while in ADG, after an adg_path_close(), the
43 * current point is simply unset.
49 * All fields are private and should not be used directly.
50 * Use its public methods instead.
55 #include "adg-path-private.h"
56 #include "adg-primitive.h"
62 static void finalize (GObject
*object
);
63 static void changed (AdgModel
*model
);
64 static void clear_cairo_path (AdgPath
*path
);
65 static cairo_path_t
* get_cairo_path (AdgPath
*path
);
66 static CpmlPath
* get_cpml_path (AdgPath
*path
);
67 static GArray
* arc_to_curves (GArray
*array
,
68 const cairo_path_data_t
70 static void append_primitive (AdgPath
*path
,
71 AdgPrimitive
*primitive
);
72 static gint
needed_pairs (CpmlPrimitiveType type
,
73 gboolean cp_is_valid
);
74 static void clear_operation (AdgPath
*path
);
75 static gboolean
append_operation (AdgPath
*path
,
78 static void do_operation (AdgPath
*path
,
81 static void do_chamfer (AdgPath
*path
,
82 CpmlPrimitive
*current
);
83 static void do_fillet (AdgPath
*path
,
84 CpmlPrimitive
*current
);
85 static gboolean
is_convex (const CpmlPrimitive
91 G_DEFINE_TYPE(AdgPath
, adg_path
, ADG_TYPE_MODEL
);
95 adg_path_class_init(AdgPathClass
*klass
)
97 GObjectClass
*gobject_class
;
98 AdgModelClass
*model_class
;
100 gobject_class
= (GObjectClass
*) klass
;
101 model_class
= (AdgModelClass
*) klass
;
103 g_type_class_add_private(klass
, sizeof(AdgPathPrivate
));
105 gobject_class
->finalize
= finalize
;
107 model_class
->changed
= changed
;
111 adg_path_init(AdgPath
*path
)
113 AdgPathPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(path
, ADG_TYPE_PATH
,
116 data
->cp_is_valid
= FALSE
;
117 data
->path
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
118 data
->cairo_path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
119 data
->cairo_path
.data
= NULL
;
120 data
->cairo_path
.num_data
= 0;
121 data
->operation
.operator = ADG_OPERATOR_NONE
;
127 finalize(GObject
*object
)
130 AdgPathPrivate
*data
;
131 GObjectClass
*object_class
;
133 path
= (AdgPath
*) object
;
135 object_class
= (GObjectClass
*) adg_path_parent_class
;
137 g_array_free(data
->path
, TRUE
);
138 clear_cairo_path(path
);
139 clear_operation(path
);
141 if (object_class
->finalize
!= NULL
)
142 object_class
->finalize(object
);
149 * Creates a new path model. The path must be constructed in the @callback
150 * function: AdgPath will cache and reuse the cairo_copy_path() returned by
151 * the cairo context after the @callback call.
153 * Returns: the new model
158 return (AdgModel
*) g_object_new(ADG_TYPE_PATH
, NULL
);
163 * adg_path_get_cairo_path:
166 * Gets a pointer to the cairo path structure of @path. The return path
167 * is owned by @path and must be considered read-only.
169 * This function also converts %CAIRO_PATH_ARC_TO primitives, not
170 * recognized by cairo, into approximated Bézier curves. The conversion
171 * is cached so any furter request is O(1). This cache is cleared
172 * whenever @path is modified (by adding a new primitive or by calling
176 * <title>TODO</title>
178 * <listitem>Actually, the arcs are approximated to Bézier using the
179 * hardcoded max angle of PI/2. This should be customizable
180 * by adding, for instance, a property to the #AdgPath class
181 * with a default value of PI/2.</listitem>
185 * Returns: a pointer to the internal cairo path or %NULL on errors
188 adg_path_get_cairo_path(AdgPath
*path
)
190 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
192 return get_cairo_path(path
);
196 * adg_path_get_cpml_path:
199 * Gets a pointer to the cairo path structure of @path. The return
200 * value is owned by @path and must not be freed.
202 * This function is similar to adg_path_get_cairo_path() but with
203 * two important differences: firstly the arc primitives are not
204 * expanded to Bézier curves and secondly the returned path is
205 * not read-only. This means it is allowed to modify the returned
206 * path as long as its size is retained and its data contains a
209 * Any changes to the @path instance will make the returned pointer
210 * useless because probably the internal #CpmlPath will be relocated
211 * and the old #CpmlPath will likely contain plain garbage.
213 * Returns: a pointer to the internal cpml path or %NULL on errors
216 adg_path_get_cpml_path(AdgPath
*path
)
218 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
220 clear_cairo_path(path
);
222 return get_cpml_path(path
);
226 * adg_path_get_segment:
228 * @segment: the destination #AdgSegment
229 * @n: the segment number to retrieve
231 * Convenient function to get a segment from @path. The segment is
232 * got from the CPML path: check out adg_path_get_cpml_path() for
233 * further information.
235 * Returns: %TRUE on success or %FALSE on errors
238 adg_path_get_segment(AdgPath
*path
, AdgSegment
*segment
, guint n
)
243 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
248 cpml_path
= get_cpml_path(path
);
250 cpml_segment_from_cairo(segment
, cpml_path
);
251 for (cnt
= 1; cnt
< n
; ++cnt
)
252 if (!cpml_segment_next(segment
)) {
253 g_warning("%s: segment `%u' out of range for type `%s'",
254 G_STRLOC
, n
, g_type_name(G_OBJECT_TYPE(path
)));
262 * adg_path_get_current_point:
264 * @x: where to store the x coordinate of the current point
265 * @y: where to store the y coordinate of the current point
267 * Gets the current point of @path, which is conceptually the
268 * final point reached by the path so far.
270 * If there is no defined current point, @x and @y will both be set
271 * to 0 and a warning will be triggered. It is possible to check this
272 * in advance with adg_path_has_current_point().
274 * Most #AdgPath methods alter the current point and most of them
275 * expect a current point to be defined otherwise will fail triggering
276 * a warning. Check the description of every method for specific details.
279 adg_path_get_current_point(AdgPath
*path
, gdouble
*x
, gdouble
*y
)
281 AdgPathPrivate
*data
;
283 g_return_if_fail(ADG_IS_PATH(path
));
287 if (data
->cp_is_valid
) {
292 g_return_if_reached();
297 * adg_path_has_current_point:
300 * Returns whether a current point is defined on @path.
301 * See adg_path_get_current_point() for details on the current point.
303 * Returns: whether a current point is defined
306 adg_path_has_current_point(AdgPath
*path
)
308 AdgPathPrivate
*data
;
310 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
314 return data
->cp_is_valid
;
321 * Releases the internal memory hold by @path and resets its status,
322 * so that after this call @path contains an empty path.
325 adg_path_clear(AdgPath
*path
)
327 AdgPathPrivate
*data
;
329 g_return_if_fail(ADG_IS_PATH(path
));
333 g_array_set_size(data
->path
, 0);
334 clear_cairo_path(path
);
335 clear_operation(path
);
342 * @type: a #cairo_data_type_t value
343 * @...: point data, specified as #AdgPair pointers
345 * Generic method to append a primitive to @path. The number of #AdgPair
346 * structs depends on @type: there is no way with this function to
347 * reserve more cairo_path_data_t structs than what is needed by the
350 * This function accepts also the special %CAIRO_PATH_ARC_TO primitive.
352 * If @path has no current point while the requested primitive needs it,
353 * a warning message will be triggered without other effect.
356 adg_path_append(AdgPath
*path
, CpmlPrimitiveType type
, ...)
360 va_start(var_args
, type
);
361 adg_path_append_valist(path
, type
, var_args
);
366 * adg_path_append_valist:
368 * @type: a #cairo_data_type_t value
369 * @var_args: point data, specified as #AdgPair pointers
371 * va_list version of adg_path_append().
374 adg_path_append_valist(AdgPath
*path
, CpmlPrimitiveType type
, va_list var_args
)
376 AdgPathPrivate
*data
;
377 AdgPrimitive primitive
;
379 cairo_path_data_t org
;
380 cairo_path_data_t
*path_data
;
382 g_return_if_fail(ADG_IS_PATH(path
));
385 length
= needed_pairs(type
, data
->cp_is_valid
);
389 /* Set a copy of the current point as the primitive origin */
390 cpml_pair_to_cairo(&data
->cp
, &org
);
391 primitive
.org
= &org
;
393 /* Build the cairo_path_data_t array */
394 primitive
.data
= path_data
= g_new(cairo_path_data_t
, length
);
396 path_data
->header
.type
= type
;
397 path_data
->header
.length
= length
;
399 for (cnt
= 1; cnt
< length
; ++ cnt
) {
401 cpml_pair_to_cairo(va_arg(var_args
, AdgPair
*), path_data
);
404 /* Terminate the creation of the temporary primitive */
405 primitive
.segment
= NULL
;
407 /* Append this primitive to @path */
408 append_primitive(path
, &primitive
);
410 g_free(primitive
.data
);
414 * adg_path_append_primitive:
416 * @primitive: the #AdgPrimitive to append
418 * Appends @primitive to @path. The primitive to add is considered the
419 * continuation of the current path so the <structfield>org</structfield>
420 * component of @primitive is not used. Anyway the current poins is
421 * checked against it: they must be equal or the function will fail
422 * without further processing.
425 adg_path_append_primitive(AdgPath
*path
, const AdgPrimitive
*primitive
)
427 AdgPathPrivate
*data
;
428 AdgPrimitive
*primitive_dup
;
430 g_return_if_fail(ADG_IS_PATH(path
));
431 g_return_if_fail(primitive
!= NULL
);
435 g_return_if_fail(primitive
->org
->point
.x
== data
->cp
.x
&&
436 primitive
->org
->point
.y
== data
->cp
.y
);
438 /* The primitive data could be modified by pending operations:
440 primitive_dup
= adg_primitive_deep_dup(primitive
);
442 append_primitive(path
, primitive_dup
);
444 g_free(primitive_dup
);
448 * adg_path_append_segment:
450 * @segment: the #AdgSegment to append
452 * Appends @segment to @path.
455 adg_path_append_segment(AdgPath
*path
, const AdgSegment
*segment
)
457 AdgPathPrivate
*data
;
459 g_return_if_fail(ADG_IS_PATH(path
));
460 g_return_if_fail(segment
!= NULL
);
464 clear_cairo_path(path
);
465 data
->path
= g_array_append_vals(data
->path
,
466 segment
->data
, segment
->num_data
);
470 * adg_path_append_cairo_path:
472 * @cairo_path: the #cairo_path_t path to append
474 * Appends a whole cairo path to @path.
477 adg_path_append_cairo_path(AdgPath
*path
, const cairo_path_t
*cairo_path
)
479 AdgPathPrivate
*data
;
481 g_return_if_fail(ADG_IS_PATH(path
));
485 clear_cairo_path(path
);
486 data
->path
= g_array_append_vals(data
->path
,
487 cairo_path
->data
, cairo_path
->num_data
);
493 * @x: the new x coordinate
494 * @y: the new y coordinate
496 * Begins a new segment. After this call the current point will be (@x, @y).
499 adg_path_move_to(AdgPath
*path
, gdouble x
, gdouble y
)
506 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, &p
);
512 * @x: the new x coordinate
513 * @y: the new y coordinate
515 * Adds a line to @path from the current point to position (@x, @y).
516 * After this call the current point will be (@x, @y).
518 * If @path has no current point before this call, this function will
519 * trigger a warning without other effect.
522 adg_path_line_to(AdgPath
*path
, gdouble x
, gdouble y
)
529 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &p
);
535 * @x1: the x coordinate of an intermediate point
536 * @y1: the y coordinate of an intermediate point
537 * @x2: the x coordinate of the end of the arc
538 * @y2: the y coordinate of the end of the arc
540 * Adds an arc to the path from the current point to (@x2, @y2),
541 * passing throught (@x1, @y1). After this call the current point
542 * will be (@x2, @y2).
544 * If @path has no current point before this call, this function will
545 * trigger a warning without other effect.
548 adg_path_arc_to(AdgPath
*path
, gdouble x1
, gdouble y1
, gdouble x2
, gdouble y2
)
557 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[0], &p
[1]);
563 * @x1: the x coordinate of the first control point
564 * @y1: the y coordinate of the first control point
565 * @x2: the x coordinate of the second control point
566 * @y2: the y coordinate of the second control point
567 * @x3: the x coordinate of the end of the curve
568 * @y3: the y coordinate of the end of the curve
570 * Adds a cubic Bézier curve to the path from the current point to
571 * position (@x3, @y3), using (@x1, @y1) and (@x2, @y2) as the
572 * control points. After this call the current point will be (@x3, @y3).
574 * If @path has no current point before this call, this function will
575 * trigger a warning without other effect.
578 adg_path_curve_to(AdgPath
*path
, gdouble x1
, gdouble y1
,
579 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
590 adg_path_append(path
, CAIRO_PATH_CURVE_TO
, &p
[0], &p
[1], &p
[2]);
597 * Adds a line segment to the path from the current point to the
598 * beginning of the current segment, (the most recent point passed
599 * to an adg_path_move_to()), and closes this segment.
600 * After this call the current point will be unset.
602 * The behavior of adg_path_close() is distinct from simply calling
603 * adg_line_to() with the coordinates of the segment starting point.
604 * When a closed segment is stroked, there are no caps on the ends.
605 * Instead, there is a line join connecting the final and initial
606 * primitive of the segment.
608 * If @path has no current point before this call, this function will
609 * trigger a warning without other effect.
612 adg_path_close(AdgPath
*path
)
614 adg_path_append(path
, CAIRO_PATH_CLOSE_PATH
);
620 * @xc: x position of the center of the arc
621 * @yc: y position of the center of the arc
622 * @r: the radius of the arc
623 * @start: the start angle, in radians
624 * @end: the end angle, in radians
626 * A more usual way to add an arc to @path. After this call, the current
627 * point will be the computed end point of the arc. The arc will be
628 * rendered in increasing angle, accordling to @start and @end. This means
629 * if @start is less than @end, the arc will be rendered in clockwise
630 * direction (accordling to the default cairo coordinate system) while if
631 * @start is greather than @end, the arc will be rendered in couterclockwise
634 * By explicitely setting the whole arc data, the start point could be
635 * different from the current point. In this case, if @path has no
636 * current point before the call a %CAIRO_PATH_MOVE_TO to the start
637 * point of the arc will be automatically prepended to the arc.
638 * If @path has a current point, a %CAIRO_PATH_LINE_TO to the start
639 * point of the arc will be used instead of the moveto.
642 adg_path_arc(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
643 gdouble start
, gdouble end
)
645 AdgPathPrivate
*data
;
646 AdgPair center
, p
[3];
648 g_return_if_fail(ADG_IS_PATH(path
));
654 cpml_vector_from_angle(&p
[0], start
, r
);
655 cpml_vector_from_angle(&p
[1], (end
-start
) / 2, r
);
656 cpml_vector_from_angle(&p
[2], end
, r
);
658 cpml_pair_add(&p
[0], ¢er
);
659 cpml_pair_add(&p
[1], ¢er
);
660 cpml_pair_add(&p
[2], ¢er
);
662 if (!data
->cp_is_valid
)
663 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, &p
[0]);
664 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
665 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &p
[0]);
667 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[1], &p
[2]);
673 * @delta1: the distance from the intersection point of the current primitive
674 * @delta2: the distance from the intersection point of the next primitive
676 * A binary operator that generates a chamfer between two primitives.
677 * The first primitive involved is the current primitive, the second will
678 * be the next primitive appended to @path after this call. The second
679 * primitive is required: if the chamfer operation is not properly
680 * terminated (by not providing the second primitive), any API accessing
681 * the path in reading mode will raise a warning.
683 * The chamfer operation requires two lengths: @delta1 specifies the
684 * "quantity" to trim on the first primitive while @delta2 is the same
685 * applied on the second primitive. The term "quantity" means the length
686 * of the portion to cut out from the original primitive (that is the
687 * primitive as would be without the chamfer).
690 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
692 g_return_if_fail(ADG_IS_PATH(path
));
694 if (!append_operation(path
, ADG_OPERATOR_CHAMFER
, delta1
, delta2
))
701 * @radius: the radius of the fillet
704 * A binary operator that joins to primitives with an arc.
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 fillet 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.
712 adg_path_fillet(AdgPath
*path
, gdouble radius
)
714 g_return_if_fail(ADG_IS_PATH(path
));
716 if (!append_operation(path
, ADG_OPERATOR_FILLET
, radius
))
725 * Dumps the data content of @path to stdout in a human readable format.
728 adg_path_dump(AdgPath
*path
)
731 cairo_path_t
*cairo_path
;
733 g_return_if_fail(ADG_IS_PATH(path
));
735 cairo_path
= get_cairo_path(path
);
737 g_return_if_fail(cairo_path
!= NULL
);
739 if (!cpml_segment_from_cairo(&segment
, cairo_path
)) {
740 g_warning("Invalid path data to dump!\n");
743 cpml_segment_dump(&segment
);
744 } while (cpml_segment_next(&segment
));
750 changed(AdgModel
*model
)
752 AdgModelClass
*model_class
= (AdgModelClass
*) adg_path_parent_class
;
754 adg_path_clear((AdgPath
*) model
);
756 if (model_class
->changed
!= NULL
)
757 model_class
->changed(model
);
761 clear_cairo_path(AdgPath
*path
)
763 AdgPathPrivate
*data
;
764 cairo_path_t
*cairo_path
;
767 cairo_path
= &data
->cairo_path
;
769 if (cairo_path
->data
== NULL
)
772 g_free(cairo_path
->data
);
774 cairo_path
->status
= CAIRO_STATUS_INVALID_PATH_DATA
;
775 cairo_path
->data
= NULL
;
776 cairo_path
->num_data
= 0;
779 static cairo_path_t
*
780 get_cairo_path(AdgPath
*path
)
782 AdgPathPrivate
*data
;
783 cairo_path_t
*cairo_path
;
786 const cairo_path_data_t
*p_src
;
790 cairo_path
= &data
->cairo_path
;
792 /* Check for cached result */
793 if (cairo_path
->data
!= NULL
)
797 dst
= g_array_sized_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
), src
->len
);
799 /* Cycle the path and convert arcs to Bézier curves */
800 for (i
= 0; i
< src
->len
; i
+= p_src
->header
.length
) {
801 p_src
= (const cairo_path_data_t
*) src
->data
+ i
;
803 if (p_src
->header
.type
== CAIRO_PATH_ARC_TO
)
804 dst
= arc_to_curves(dst
, p_src
);
806 dst
= g_array_append_vals(dst
, p_src
, p_src
->header
.length
);
809 cairo_path
->status
= CAIRO_STATUS_SUCCESS
;
810 cairo_path
->num_data
= dst
->len
;
811 cairo_path
->data
= (cairo_path_data_t
*) g_array_free(dst
, FALSE
);
817 get_cpml_path(AdgPath
*path
)
819 AdgPathPrivate
*data
;
823 cpml_path
= &data
->cpml_path
;
825 cpml_path
->status
= CAIRO_STATUS_SUCCESS
;
826 cpml_path
->data
= (cairo_path_data_t
*) data
->path
->data
;
827 cpml_path
->num_data
= data
->path
->len
;
833 arc_to_curves(GArray
*array
, const cairo_path_data_t
*src
)
838 /* Build the arc primitive: the arc origin is supposed to be the previous
839 * point (src-1): this means a primitive must exist before the arc */
841 arc
.org
= (cairo_path_data_t
*) (src
-1);
842 arc
.data
= (cairo_path_data_t
*) src
;
844 if (cpml_arc_info(&arc
, NULL
, NULL
, &start
, &end
)) {
847 cairo_path_data_t
*curves
;
849 n_curves
= ceil(fabs(end
-start
) / M_PI_2
);
850 curves
= g_new(cairo_path_data_t
, n_curves
* 4);
851 segment
.data
= curves
;
852 cpml_arc_to_curves(&arc
, &segment
, n_curves
);
854 array
= g_array_append_vals(array
, curves
, n_curves
* 4);
863 append_primitive(AdgPath
*path
, AdgPrimitive
*current
)
865 AdgPathPrivate
*data
;
866 cairo_path_data_t
*path_data
;
870 path_data
= current
->data
;
871 length
= path_data
[0].header
.length
;
873 /* Execute any pending operation */
874 do_operation(path
, path_data
);
876 /* Append the path data to the internal path array */
877 data
->path
= g_array_append_vals(data
->path
, path_data
, length
);
879 /* Set path data to point to the recently appended cairo_path_data_t
880 * primitive: the first struct is the header */
881 path_data
= (cairo_path_data_t
*) data
->path
->data
+
882 data
->path
->len
- length
;
884 /* Set the last primitive for subsequent binary operations */
885 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
886 data
->last
.segment
= NULL
;
887 data
->last
.data
= path_data
;
889 /* Save the last point as the current point, if applicable */
890 data
->cp_is_valid
= length
> 1;
892 cpml_pair_from_cairo(&data
->cp
, &path_data
[length
-1]);
894 /* Invalidate cairo_path: should be recomputed */
895 clear_cairo_path(path
);
899 needed_pairs(CpmlPrimitiveType type
, gboolean cp_is_valid
)
903 case CAIRO_PATH_CLOSE_PATH
:
904 g_return_val_if_fail(cp_is_valid
, 0);
907 case CAIRO_PATH_MOVE_TO
:
910 case CAIRO_PATH_LINE_TO
:
911 g_return_val_if_fail(cp_is_valid
, 0);
914 case CAIRO_PATH_ARC_TO
:
915 g_return_val_if_fail(cp_is_valid
, 0);
918 case CAIRO_PATH_CURVE_TO
:
919 g_return_val_if_fail(cp_is_valid
, 0);
923 g_return_val_if_reached(0);
930 clear_operation(AdgPath
*path
)
932 AdgPathPrivate
*data
;
933 AdgOperation
*operation
;
936 operation
= &data
->operation
;
938 if (operation
->operator == ADG_OPERATOR_NONE
)
941 g_warning("An operation is still active while clearing the path "
942 "(operator `%d')", operation
->operator);
943 operation
->operator = ADG_OPERATOR_NONE
;
947 append_operation(AdgPath
*path
, AdgOperator
operator, ...)
949 AdgPathPrivate
*data
;
950 AdgOperation
*operation
;
955 if (!data
->cp_is_valid
) {
956 g_warning("Operation requested but path has no current primitive "
957 "(operator `%d')", operator);
961 operation
= &data
->operation
;
962 if (operation
->operator != ADG_OPERATOR_NONE
) {
963 /* TODO: this is a rude semplification, as a lot of operators can
964 * and may cohexist. As an example, a fillet followed by a
965 * polar chamfer is not difficult to compute */
966 g_warning("Operation requested but another operation is yet active"
967 "(operators: new `%d', old `%d')",
968 operator, operation
->operator);
972 va_start(var_args
, operator);
976 case ADG_OPERATOR_CHAMFER
:
977 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
978 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
981 case ADG_OPERATOR_FILLET
:
982 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
985 case ADG_OPERATOR_NONE
:
990 g_warning("Operation not recognized (operator `%d')", operator);
995 operation
->operator = operator;
1002 do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
1004 AdgPathPrivate
*data
;
1005 AdgOperator
operator;
1006 CpmlSegment segment
;
1007 CpmlPrimitive current
;
1008 cairo_path_data_t current_org
;
1011 operator = data
->operation
.operator;
1012 cpml_segment_from_cairo(&segment
, get_cpml_path(path
));
1014 /* Construct the current primitive, that is the primitive to be inserted.
1015 * Its org is a copy of the end point of the last primitive: it can be
1016 * modified without affecting anything else. It is expected the operation
1017 * functions will add to @path the primitives required but NOT to add
1018 * @current, as this one will be inserted automatically. */
1019 current
.segment
= &segment
;
1020 current
.org
= ¤t_org
;
1021 current
.data
= path_data
;
1022 cpml_pair_to_cairo(&data
->cp
, ¤t_org
);
1026 case ADG_OPERATOR_NONE
:
1029 case ADG_OPERATOR_CHAMFER
:
1030 do_chamfer(path
, ¤t
);
1033 case ADG_OPERATOR_FILLET
:
1034 do_fillet(path
, ¤t
);
1038 g_warning("Operation not implemented (operator `%d')", operator);
1044 do_chamfer(AdgPath
*path
, CpmlPrimitive
*current
)
1046 AdgPathPrivate
*data
;
1047 CpmlPrimitive
*last
;
1048 gdouble delta1
, delta2
;
1051 cairo_path_data_t line
[2];
1055 delta1
= data
->operation
.data
.chamfer
.delta1
;
1056 len1
= cpml_primitive_length(last
);
1058 if (delta1
>= len1
) {
1059 g_warning("Chamfer too big for the last primitive (%lf >= %lf)",
1064 delta2
= data
->operation
.data
.chamfer
.delta2
;
1065 len2
= cpml_primitive_length(current
);
1067 if (delta2
>= len2
) {
1068 g_warning("Chamfer too big for the current primitive (%lf >= %lf)",
1073 /* Change the end point of the last primitive */
1074 cpml_primitive_pair_at(last
, &pair
, 1. - delta1
/ len1
);
1075 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(last
, -1));
1077 /* Change the start point of the current primitive */
1078 cpml_primitive_pair_at(current
, &pair
, delta2
/ len2
);
1079 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(current
, 0));
1081 /* Add the chamfer line */
1082 line
[0].header
.type
= CAIRO_PATH_LINE_TO
;
1083 line
[0].header
.length
= 2;
1084 line
[1].point
.x
= pair
.x
;
1085 line
[1].point
.y
= pair
.y
;
1086 data
->path
= g_array_append_vals(data
->path
, line
, 2);
1088 data
->operation
.operator = ADG_OPERATOR_NONE
;
1092 do_fillet(AdgPath
*path
, CpmlPrimitive
*current
)
1094 AdgPathPrivate
*data
;
1095 CpmlPrimitive
*last
, *current_dup
, *last_dup
;
1096 gdouble radius
, offset
, pos
;
1097 AdgPair center
, vector
, p
[3];
1098 cairo_path_data_t arc
[3];
1102 current_dup
= adg_primitive_deep_dup(current
);
1104 /* Force current_dup to point to the original segment so a
1105 * CAIRO_PATH_CLOSE_PATH primitive will work as expected */
1106 current_dup
->segment
= current
->segment
;
1108 last_dup
= adg_primitive_deep_dup(last
);
1109 radius
= data
->operation
.data
.fillet
.radius
;
1110 offset
= is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1112 /* Find the center of the fillet from the intersection between
1113 * the last and current primitives offseted by radius */
1114 cpml_primitive_offset(current_dup
, offset
);
1115 cpml_primitive_offset(last_dup
, offset
);
1116 if (cpml_primitive_intersection(current_dup
, last_dup
,
1118 g_warning("Fillet not applicable (radius = %lf)", radius
);
1119 g_free(current_dup
);
1124 /* Compute the start point of the fillet */
1125 pos
= cpml_primitive_near_pos(last_dup
, ¢er
);
1126 cpml_primitive_vector_at(last_dup
, &vector
, pos
);
1127 cpml_vector_set_length(&vector
, offset
);
1128 cpml_vector_normal(&vector
);
1129 cpml_pair_sub(cpml_pair_copy(&p
[0], ¢er
), &vector
);
1131 /* Compute the mid point of the fillet */
1132 cpml_pair_from_cairo(&vector
, current
->org
);
1133 cpml_pair_sub(&vector
, ¢er
);
1134 cpml_vector_set_length(&vector
, radius
);
1135 cpml_pair_add(cpml_pair_copy(&p
[1], ¢er
), &vector
);
1137 /* Compute the end point of the fillet */
1138 pos
= cpml_primitive_near_pos(current_dup
, ¢er
);
1139 cpml_primitive_vector_at(current_dup
, &vector
, pos
);
1140 cpml_vector_set_length(&vector
, offset
);
1141 cpml_vector_normal(&vector
);
1142 cpml_pair_sub(cpml_pair_copy(&p
[2], ¢er
), &vector
);
1144 g_free(current_dup
);
1147 /* Modify the end point of the last primitive */
1148 cpml_pair_to_cairo(&p
[0], cpml_primitive_get_point(last
, -1));
1150 /* Add the fillet arc */
1151 arc
[0].header
.type
= CAIRO_PATH_ARC_TO
;
1152 arc
[0].header
.length
= 3;
1153 cpml_pair_to_cairo(&p
[1], &arc
[1]);
1154 cpml_pair_to_cairo(&p
[2], &arc
[2]);
1155 data
->path
= g_array_append_vals(data
->path
, arc
, 3);
1157 data
->operation
.operator = ADG_OPERATOR_NONE
;
1161 is_convex(const CpmlPrimitive
*primitive1
, const CpmlPrimitive
*primitive2
)
1164 gdouble angle1
, angle2
;
1166 cpml_primitive_vector_at(primitive1
, &v1
, -1);
1167 cpml_primitive_vector_at(primitive2
, &v2
, 0);
1169 /* Probably there is a smarter way to get this without trygonometry */
1170 angle1
= cpml_vector_angle(&v1
);
1171 angle2
= cpml_vector_angle(&v2
);
1173 if (angle1
> angle2
)
1176 return angle2
-angle1
> M_PI
;