[AdgContainer] Adding child with g_slist_prepend()
[adg.git] / adg / adg-path.c
blobf43332978f6a9b736a02636a5caacbc2e415dc31
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.
21 /**
22 * SECTION:adg-path
23 * @short_description: The basic model representing a generic path
25 * The #AdgPath model is a virtual path: in a few words, it is a
26 * simple conceptual #cairo_path_t struct. This class implements
27 * methods 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.
44 **/
46 /**
47 * AdgPath:
49 * All fields are private and should not be used directly.
50 * Use its public methods instead.
51 **/
54 #include "adg-path.h"
55 #include "adg-path-private.h"
56 #include "adg-primitive.h"
57 #include "adg-intl.h"
59 #include <math.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 cairo_path_t * get_cpml_path (AdgPath *path);
67 static GArray * arc_to_curves (GArray *array,
68 const cairo_path_data_t
69 *src);
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,
76 AdgOperator operator,
77 ...);
78 static void do_operation (AdgPath *path,
79 cairo_path_data_t
80 *path_data);
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
86 *primitive1,
87 const CpmlPrimitive
88 *primitive2);
91 G_DEFINE_TYPE(AdgPath, adg_path, ADG_TYPE_MODEL);
94 static void
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;
110 static void
111 adg_path_init(AdgPath *path)
113 AdgPathPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(path, ADG_TYPE_PATH,
114 AdgPathPrivate);
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;
123 path->data = data;
126 static void
127 finalize(GObject *object)
129 AdgPath *path;
130 AdgPathPrivate *data;
131 GObjectClass *object_class;
133 path = (AdgPath *) object;
134 data = path->data;
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);
147 * adg_path_new:
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
155 AdgModel *
156 adg_path_new(void)
158 return (AdgModel *) g_object_new(ADG_TYPE_PATH, NULL);
163 * adg_path_get_cairo_path:
164 * @path: an #AdgPath
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
173 * adg_path_clear()).
175 * <important>
176 * <title>TODO</title>
177 * <itemizedlist>
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>
182 * </itemizedlist>
183 * </important>
185 * Returns: a pointer to the internal cairo path or %NULL on errors
187 const cairo_path_t *
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:
197 * @path: an #AdgPath
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
207 * valid path.
209 * Keep in mind any changes to @path makes the value returned by
210 * this function useless, as it is likely to contain plain garbage.
212 * Returns: a pointer to the internal cpml path or %NULL on errors
214 cairo_path_t *
215 adg_path_get_cpml_path(AdgPath *path)
217 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
219 clear_cairo_path(path);
221 return get_cpml_path(path);
225 * adg_path_get_current_point:
226 * @path: an #AdgPath
227 * @x: where to store the x coordinate of the current point
228 * @y: where to store the y coordinate of the current point
230 * Gets the current point of @path, which is conceptually the
231 * final point reached by the path so far.
233 * If there is no defined current point, @x and @y will both be set
234 * to 0 and a warning will be triggered. It is possible to check this
235 * in advance with adg_path_has_current_point().
237 * Most #AdgPath methods alter the current point and most of them
238 * expect a current point to be defined otherwise will fail triggering
239 * a warning. Check the description of every method for specific details.
241 void
242 adg_path_get_current_point(AdgPath *path, gdouble *x, gdouble *y)
244 AdgPathPrivate *data;
246 g_return_if_fail(ADG_IS_PATH(path));
248 data = path->data;
250 if (data->cp_is_valid) {
251 *x = data->cp.x;
252 *y = data->cp.y;
253 } else {
254 *x = *y = 0.;
255 g_return_if_reached();
260 * adg_path_has_current_point:
261 * @path: an #AdgPath
263 * Returns whether a current point is defined on @path.
264 * See adg_path_get_current_point() for details on the current point.
266 * Returns: whether a current point is defined
268 gboolean
269 adg_path_has_current_point(AdgPath *path)
271 AdgPathPrivate *data;
273 g_return_val_if_fail(ADG_IS_PATH(path), FALSE);
275 data = path->data;
277 return data->cp_is_valid;
281 * adg_path_clear:
282 * @path: an #AdgPath
284 * Releases the internal memory hold by @path and resets its status,
285 * so that after this call @path contains an empty path.
287 void
288 adg_path_clear(AdgPath *path)
290 AdgPathPrivate *data;
292 g_return_if_fail(ADG_IS_PATH(path));
294 data = path->data;
296 g_array_set_size(data->path, 0);
297 clear_cairo_path(path);
298 clear_operation(path);
303 * adg_path_append:
304 * @path: an #AdgPath
305 * @type: a #cairo_data_type_t value
306 * @...: point data, specified as #AdgPair pointers
308 * Generic method to append a primitive to @path. The number of #AdgPair
309 * structs depends on @type: there is no way with this function to
310 * reserve more cairo_path_data_t structs than what is needed by the
311 * primitive.
313 * This function accepts also the special %CAIRO_PATH_ARC_TO primitive.
315 * If @path has no current point while the requested primitive needs it,
316 * a warning message will be triggered without other effect.
318 void
319 adg_path_append(AdgPath *path, CpmlPrimitiveType type, ...)
321 va_list var_args;
323 va_start(var_args, type);
324 adg_path_append_valist(path, type, var_args);
325 va_end(var_args);
329 * adg_path_append_valist:
330 * @path: an #AdgPath
331 * @type: a #cairo_data_type_t value
332 * @var_args: point data, specified as #AdgPair pointers
334 * va_list version of adg_path_append().
336 void
337 adg_path_append_valist(AdgPath *path, CpmlPrimitiveType type, va_list var_args)
339 AdgPathPrivate *data;
340 AdgPrimitive primitive;
341 gint length, cnt;
342 cairo_path_data_t org;
343 cairo_path_data_t *path_data;
345 g_return_if_fail(ADG_IS_PATH(path));
347 data = path->data;
348 length = needed_pairs(type, data->cp_is_valid);
349 if (length == 0)
350 return;
352 /* Set a copy of the current point as the primitive origin */
353 cpml_pair_to_cairo(&data->cp, &org);
354 primitive.org = &org;
356 /* Build the cairo_path_data_t array */
357 primitive.data = path_data = g_new(cairo_path_data_t, length);
359 path_data->header.type = type;
360 path_data->header.length = length;
362 for (cnt = 1; cnt < length; ++ cnt) {
363 ++ path_data;
364 cpml_pair_to_cairo(va_arg(var_args, AdgPair *), path_data);
367 /* Terminate the creation of the temporary primitive */
368 primitive.segment = NULL;
370 /* Append this primitive to @path */
371 append_primitive(path, &primitive);
373 g_free(primitive.data);
377 * adg_path_append_primitive:
378 * @path: an #AdgPath
379 * @primitive: the #AdgPrimitive to append
381 * Appends @primitive to @path. The primitive to add is considered the
382 * continuation of the current path so the <structfield>org</structfield>
383 * component of @primitive is not used. Anyway the current poins is
384 * checked against it: they must be equal or the function will fail
385 * without further processing.
387 void
388 adg_path_append_primitive(AdgPath *path, const AdgPrimitive *primitive)
390 AdgPathPrivate *data;
391 AdgPrimitive *primitive_dup;
393 g_return_if_fail(ADG_IS_PATH(path));
394 g_return_if_fail(primitive != NULL);
396 data = path->data;
398 g_return_if_fail(primitive->org->point.x == data->cp.x &&
399 primitive->org->point.y == data->cp.y);
401 /* The primitive data could be modified by pending operations:
402 * work on a copy */
403 primitive_dup = adg_primitive_deep_dup(primitive);
405 append_primitive(path, primitive_dup);
407 g_free(primitive_dup);
411 * adg_path_append_segment:
412 * @path: an #AdgPath
413 * @segment: the #AdgSegment to append
415 * Appends @segment to @path.
417 void
418 adg_path_append_segment(AdgPath *path, const AdgSegment *segment)
420 AdgPathPrivate *data;
422 g_return_if_fail(ADG_IS_PATH(path));
423 g_return_if_fail(segment != NULL);
425 data = path->data;
427 clear_cairo_path(path);
428 data->path = g_array_append_vals(data->path,
429 segment->data, segment->num_data);
433 * adg_path_append_cairo_path:
434 * @path: an #AdgPath
435 * @cairo_path: the #cairo_path_t path to append
437 * Appends a whole cairo path to @path.
439 void
440 adg_path_append_cairo_path(AdgPath *path, const cairo_path_t *cairo_path)
442 AdgPathPrivate *data;
444 g_return_if_fail(ADG_IS_PATH(path));
446 data = path->data;
448 clear_cairo_path(path);
449 data->path = g_array_append_vals(data->path,
450 cairo_path->data, cairo_path->num_data);
454 * adg_path_move_to:
455 * @path: an #AdgPath
456 * @x: the new x coordinate
457 * @y: the new y coordinate
459 * Begins a new segment. After this call the current point will be (@x, @y).
461 void
462 adg_path_move_to(AdgPath *path, gdouble x, gdouble y)
464 AdgPair p;
466 p.x = x;
467 p.y = y;
469 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p);
473 * adg_path_line_to:
474 * @path: an #AdgPath
475 * @x: the new x coordinate
476 * @y: the new y coordinate
478 * Adds a line to @path from the current point to position (@x, @y).
479 * After this call the current point will be (@x, @y).
481 * If @path has no current point before this call, this function will
482 * trigger a warning without other effect.
484 void
485 adg_path_line_to(AdgPath *path, gdouble x, gdouble y)
487 AdgPair p;
489 p.x = x;
490 p.y = y;
492 adg_path_append(path, CAIRO_PATH_LINE_TO, &p);
496 * adg_path_arc_to:
497 * @path: an #AdgPath
498 * @x1: the x coordinate of an intermediate point
499 * @y1: the y coordinate of an intermediate point
500 * @x2: the x coordinate of the end of the arc
501 * @y2: the y coordinate of the end of the arc
503 * Adds an arc to the path from the current point to (@x2, @y2),
504 * passing throught (@x1, @y1). After this call the current point
505 * will be (@x2, @y2).
507 * If @path has no current point before this call, this function will
508 * trigger a warning without other effect.
510 void
511 adg_path_arc_to(AdgPath *path, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
513 AdgPair p[2];
515 p[0].x = x1;
516 p[0].y = y1;
517 p[1].x = x2;
518 p[1].y = y2;
520 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[0], &p[1]);
524 * adg_path_curve_to:
525 * @path: an #AdgPath
526 * @x1: the x coordinate of the first control point
527 * @y1: the y coordinate of the first control point
528 * @x2: the x coordinate of the second control point
529 * @y2: the y coordinate of the second control point
530 * @x3: the x coordinate of the end of the curve
531 * @y3: the y coordinate of the end of the curve
533 * Adds a cubic Bézier curve to the path from the current point to
534 * position (@x3, @y3), using (@x1, @y1) and (@x2, @y2) as the
535 * control points. After this call the current point will be (@x3, @y3).
537 * If @path has no current point before this call, this function will
538 * trigger a warning without other effect.
540 void
541 adg_path_curve_to(AdgPath *path, gdouble x1, gdouble y1,
542 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
544 AdgPair p[3];
546 p[0].x = x1;
547 p[0].y = y1;
548 p[1].x = x2;
549 p[1].y = y2;
550 p[2].x = x3;
551 p[2].y = y3;
553 adg_path_append(path, CAIRO_PATH_CURVE_TO, &p[0], &p[1], &p[2]);
557 * adg_path_close:
558 * @path: an #AdgPath
560 * Adds a line segment to the path from the current point to the
561 * beginning of the current segment, (the most recent point passed
562 * to an adg_path_move_to()), and closes this segment.
563 * After this call the current point will be unset.
565 * The behavior of adg_path_close() is distinct from simply calling
566 * adg_line_to() with the coordinates of the segment starting point.
567 * When a closed segment is stroked, there are no caps on the ends.
568 * Instead, there is a line join connecting the final and initial
569 * primitive of the segment.
571 * If @path has no current point before this call, this function will
572 * trigger a warning without other effect.
574 void
575 adg_path_close(AdgPath *path)
577 adg_path_append(path, CAIRO_PATH_CLOSE_PATH);
581 * adg_path_arc
582 * @path: an #AdgPath
583 * @xc: x position of the center of the arc
584 * @yc: y position of the center of the arc
585 * @r: the radius of the arc
586 * @start: the start angle, in radians
587 * @end: the end angle, in radians
589 * A more usual way to add an arc to @path. After this call, the current
590 * point will be the computed end point of the arc. The arc will be
591 * rendered in increasing angle, accordling to @start and @end. This means
592 * if @start is less than @end, the arc will be rendered in clockwise
593 * direction (accordling to the default cairo coordinate system) while if
594 * @start is greather than @end, the arc will be rendered in couterclockwise
595 * direction.
597 * By explicitely setting the whole arc data, the start point could be
598 * different from the current point. In this case, if @path has no
599 * current point before the call a %CAIRO_PATH_MOVE_TO to the start
600 * point of the arc will be automatically prepended to the arc.
601 * If @path has a current point, a %CAIRO_PATH_LINE_TO to the start
602 * point of the arc will be used instead of the moveto.
604 void
605 adg_path_arc(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
606 gdouble start, gdouble end)
608 AdgPathPrivate *data;
609 AdgPair center, p[3];
611 g_return_if_fail(ADG_IS_PATH(path));
613 data = path->data;
614 center.x = xc;
615 center.y = yc;
617 cpml_vector_from_angle(&p[0], start, r);
618 cpml_vector_from_angle(&p[1], (end-start) / 2, r);
619 cpml_vector_from_angle(&p[2], end, r);
621 cpml_pair_add(&p[0], &center);
622 cpml_pair_add(&p[1], &center);
623 cpml_pair_add(&p[2], &center);
625 if (!data->cp_is_valid)
626 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p[0]);
627 else if (p[0].x != data->cp.x || p[0].y != data->cp.y)
628 adg_path_append(path, CAIRO_PATH_LINE_TO, &p[0]);
630 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[1], &p[2]);
634 * adg_path_chamfer
635 * @path: an #AdgPath
636 * @delta1: the distance from the intersection point of the current primitive
637 * @delta2: the distance from the intersection point of the next primitive
639 * A binary operator that generates a chamfer between two primitives.
640 * The first primitive involved is the current primitive, the second will
641 * be the next primitive appended to @path after this call. The second
642 * primitive is required: if the chamfer operation is not properly
643 * terminated (by not providing the second primitive), any API accessing
644 * the path in reading mode will raise a warning.
646 * The chamfer operation requires two lengths: @delta1 specifies the
647 * "quantity" to trim on the first primitive while @delta2 is the same
648 * applied on the second primitive. The term "quantity" means the length
649 * of the portion to cut out from the original primitive (that is the
650 * primitive as would be without the chamfer).
652 void
653 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
655 g_return_if_fail(ADG_IS_PATH(path));
657 if (!append_operation(path, ADG_OPERATOR_CHAMFER, delta1, delta2))
658 return;
662 * adg_path_fillet:
663 * @path: an #AdgPath
664 * @radius: the radius of the fillet
667 * A binary operator that joins to primitives with an arc.
668 * The first primitive involved is the current primitive, the second will
669 * be the next primitive appended to @path after this call. The second
670 * primitive is required: if the fillet operation is not properly
671 * terminated (by not providing the second primitive), any API accessing
672 * the path in reading mode will raise a warning.
674 void
675 adg_path_fillet(AdgPath *path, gdouble radius)
677 g_return_if_fail(ADG_IS_PATH(path));
679 if (!append_operation(path, ADG_OPERATOR_FILLET, radius))
680 return;
685 * adg_path_dump:
686 * @path: an #AdgPath
688 * Dumps the data content of @path to stdout in a human readable format.
690 void
691 adg_path_dump(AdgPath *path)
693 CpmlSegment segment;
694 cairo_path_t *cairo_path;
696 g_return_if_fail(ADG_IS_PATH(path));
698 cairo_path = get_cairo_path(path);
700 g_return_if_fail(cairo_path != NULL);
702 if (!cpml_segment_from_cairo(&segment, cairo_path)) {
703 g_warning("Invalid path data to dump!\n");
704 } else {
705 do {
706 cpml_segment_dump(&segment);
707 } while (cpml_segment_next(&segment));
712 static void
713 changed(AdgModel *model)
715 AdgModelClass *model_class = (AdgModelClass *) adg_path_parent_class;
717 adg_path_clear((AdgPath *) model);
719 if (model_class->changed != NULL)
720 model_class->changed(model);
723 static void
724 clear_cairo_path(AdgPath *path)
726 AdgPathPrivate *data;
727 cairo_path_t *cairo_path;
729 data = path->data;
730 cairo_path = &data->cairo_path;
732 if (cairo_path->data == NULL)
733 return;
735 g_free(cairo_path->data);
737 cairo_path->status = CAIRO_STATUS_INVALID_PATH_DATA;
738 cairo_path->data = NULL;
739 cairo_path->num_data = 0;
742 static cairo_path_t *
743 get_cairo_path(AdgPath *path)
745 AdgPathPrivate *data;
746 cairo_path_t *cairo_path;
747 const GArray *src;
748 GArray *dst;
749 const cairo_path_data_t *p_src;
750 int i;
752 data = path->data;
753 cairo_path = &data->cairo_path;
755 /* Check for cached result */
756 if (cairo_path->data != NULL)
757 return cairo_path;
759 src = data->path;
760 dst = g_array_sized_new(FALSE, FALSE, sizeof(cairo_path_data_t), src->len);
762 /* Cycle the path and convert arcs to Bézier curves */
763 for (i = 0; i < src->len; i += p_src->header.length) {
764 p_src = (const cairo_path_data_t *) src->data + i;
766 if (p_src->header.type == CAIRO_PATH_ARC_TO)
767 dst = arc_to_curves(dst, p_src);
768 else
769 dst = g_array_append_vals(dst, p_src, p_src->header.length);
772 cairo_path->status = CAIRO_STATUS_SUCCESS;
773 cairo_path->num_data = dst->len;
774 cairo_path->data = (cairo_path_data_t *) g_array_free(dst, FALSE);
776 return cairo_path;
779 static cairo_path_t *
780 get_cpml_path(AdgPath *path)
782 AdgPathPrivate *data;
783 cairo_path_t *cpml_path;
785 data = path->data;
786 cpml_path = &data->cpml_path;
788 cpml_path->status = CAIRO_STATUS_SUCCESS;
789 cpml_path->data = (cairo_path_data_t *) data->path->data;
790 cpml_path->num_data = data->path->len;
792 return cpml_path;
795 static GArray *
796 arc_to_curves(GArray *array, const cairo_path_data_t *src)
798 CpmlPrimitive arc;
799 double start, end;
801 /* Build the arc primitive: the arc origin is supposed to be the previous
802 * point (src-1): this means a primitive must exist before the arc */
803 arc.segment = NULL;
804 arc.org = (cairo_path_data_t *) (src-1);
805 arc.data = (cairo_path_data_t *) src;
807 if (cpml_arc_info(&arc, NULL, NULL, &start, &end)) {
808 CpmlSegment segment;
809 int n_curves;
810 cairo_path_data_t *curves;
812 n_curves = ceil(fabs(end-start) / M_PI_2);
813 curves = g_new(cairo_path_data_t, n_curves * 4);
814 segment.data = curves;
815 cpml_arc_to_curves(&arc, &segment, n_curves);
817 array = g_array_append_vals(array, curves, n_curves * 4);
819 g_free(curves);
822 return array;
825 static void
826 append_primitive(AdgPath *path, AdgPrimitive *current)
828 AdgPathPrivate *data;
829 cairo_path_data_t *path_data;
830 int length;
832 data = path->data;
833 path_data = current->data;
834 length = path_data[0].header.length;
836 /* Execute any pending operation */
837 do_operation(path, path_data);
839 /* Append the path data to the internal path array */
840 data->path = g_array_append_vals(data->path, path_data, length);
842 /* Set path data to point to the recently appended cairo_path_data_t
843 * primitive: the first struct is the header */
844 path_data = (cairo_path_data_t *) data->path->data +
845 data->path->len - length;
847 /* Set the last primitive for subsequent binary operations */
848 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
849 data->last.segment = NULL;
850 data->last.data = path_data;
852 /* Save the last point as the current point, if applicable */
853 data->cp_is_valid = length > 1;
854 if (length > 1)
855 cpml_pair_from_cairo(&data->cp, &path_data[length-1]);
857 /* Invalidate cairo_path: should be recomputed */
858 clear_cairo_path(path);
861 static gint
862 needed_pairs(CpmlPrimitiveType type, gboolean cp_is_valid)
864 switch (type) {
866 case CAIRO_PATH_CLOSE_PATH:
867 g_return_val_if_fail(cp_is_valid, 0);
868 return 1;
870 case CAIRO_PATH_MOVE_TO:
871 return 2;
873 case CAIRO_PATH_LINE_TO:
874 g_return_val_if_fail(cp_is_valid, 0);
875 return 2;
877 case CAIRO_PATH_ARC_TO:
878 g_return_val_if_fail(cp_is_valid, 0);
879 return 3;
881 case CAIRO_PATH_CURVE_TO:
882 g_return_val_if_fail(cp_is_valid, 0);
883 return 4;
885 default:
886 g_return_val_if_reached(0);
889 return 0;
892 static void
893 clear_operation(AdgPath *path)
895 AdgPathPrivate *data;
896 AdgOperation *operation;
898 data = path->data;
899 operation = &data->operation;
901 if (operation->operator == ADG_OPERATOR_NONE)
902 return;
904 g_warning("An operation is still active while clearing the path "
905 "(operator `%d')", operation->operator);
906 operation->operator = ADG_OPERATOR_NONE;
909 static gboolean
910 append_operation(AdgPath *path, AdgOperator operator, ...)
912 AdgPathPrivate *data;
913 AdgOperation *operation;
914 va_list var_args;
916 data = path->data;
918 if (!data->cp_is_valid) {
919 g_warning("Operation requested but path has no current primitive "
920 "(operator `%d')", operator);
921 return FALSE;
924 operation = &data->operation;
925 if (operation->operator != ADG_OPERATOR_NONE) {
926 /* TODO: this is a rude semplification, as a lot of operators can
927 * and may cohexist. As an example, a fillet followed by a
928 * polar chamfer is not difficult to compute */
929 g_warning("Operation requested but another operation is yet active"
930 "(operators: new `%d', old `%d')",
931 operator, operation->operator);
932 return FALSE;
935 va_start(var_args, operator);
937 switch (operator) {
939 case ADG_OPERATOR_CHAMFER:
940 operation->data.chamfer.delta1 = va_arg(var_args, double);
941 operation->data.chamfer.delta2 = va_arg(var_args, double);
942 break;
944 case ADG_OPERATOR_FILLET:
945 operation->data.fillet.radius = va_arg(var_args, double);
946 break;
948 case ADG_OPERATOR_NONE:
949 va_end(var_args);
950 return TRUE;
952 default:
953 g_warning("Operation not recognized (operator `%d')", operator);
954 va_end(var_args);
955 return FALSE;
958 operation->operator = operator;
959 va_end(var_args);
961 return TRUE;
964 static void
965 do_operation(AdgPath *path, cairo_path_data_t *path_data)
967 AdgPathPrivate *data;
968 AdgOperator operator;
969 CpmlSegment segment;
970 CpmlPrimitive current;
971 cairo_path_data_t current_org;
973 data = path->data;
974 operator = data->operation.operator;
975 cpml_segment_from_cairo(&segment, get_cpml_path(path));
977 /* Construct the current primitive, that is the primitive to be inserted.
978 * Its org is a copy of the end point of the last primitive: it can be
979 * modified without affecting anything else. It is expected the operation
980 * functions will add to @path the primitives required but NOT to add
981 * @current, as this one will be inserted automatically. */
982 current.segment = &segment;
983 current.org = &current_org;
984 current.data = path_data;
985 cpml_pair_to_cairo(&data->cp, &current_org);
987 switch (operator) {
989 case ADG_OPERATOR_NONE:
990 return;
992 case ADG_OPERATOR_CHAMFER:
993 do_chamfer(path, &current);
994 break;
996 case ADG_OPERATOR_FILLET:
997 do_fillet(path, &current);
998 break;
1000 default:
1001 g_warning("Operation not implemented (operator `%d')", operator);
1002 return;
1006 static void
1007 do_chamfer(AdgPath *path, CpmlPrimitive *current)
1009 AdgPathPrivate *data;
1010 CpmlPrimitive *last;
1011 gdouble delta1, delta2;
1012 gdouble len1, len2;
1013 AdgPair pair;
1014 cairo_path_data_t line[2];
1016 data = path->data;
1017 last = &data->last;
1018 delta1 = data->operation.data.chamfer.delta1;
1019 len1 = cpml_primitive_length(last);
1021 if (delta1 >= len1) {
1022 g_warning("Chamfer too big for the last primitive (%lf >= %lf)",
1023 delta1, len1);
1024 return;
1027 delta2 = data->operation.data.chamfer.delta2;
1028 len2 = cpml_primitive_length(current);
1030 if (delta2 >= len2) {
1031 g_warning("Chamfer too big for the current primitive (%lf >= %lf)",
1032 delta2, len2);
1033 return;
1036 /* Change the end point of the last primitive */
1037 cpml_primitive_pair_at(last, &pair, 1. - delta1 / len1);
1038 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(last, -1));
1040 /* Change the start point of the current primitive */
1041 cpml_primitive_pair_at(current, &pair, delta2 / len2);
1042 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(current, 0));
1044 /* Add the chamfer line */
1045 line[0].header.type = CAIRO_PATH_LINE_TO;
1046 line[0].header.length = 2;
1047 line[1].point.x = pair.x;
1048 line[1].point.y = pair.y;
1049 data->path = g_array_append_vals(data->path, line, 2);
1051 data->operation.operator = ADG_OPERATOR_NONE;
1054 static void
1055 do_fillet(AdgPath *path, CpmlPrimitive *current)
1057 AdgPathPrivate *data;
1058 CpmlPrimitive *last, *current_dup, *last_dup;
1059 gdouble radius, offset, pos;
1060 AdgPair center, vector, p[3];
1061 cairo_path_data_t arc[3];
1063 data = path->data;
1064 last = &data->last;
1065 current_dup = adg_primitive_deep_dup(current);
1067 /* Force current_dup to point to the original segment so a
1068 * CAIRO_PATH_CLOSE_PATH primitive will work as expected */
1069 current_dup->segment = current->segment;
1071 last_dup = adg_primitive_deep_dup(last);
1072 radius = data->operation.data.fillet.radius;
1073 offset = is_convex(last_dup, current_dup) ? -radius : radius;
1075 /* Find the center of the fillet from the intersection between
1076 * the last and current primitives offseted by radius */
1077 cpml_primitive_offset(current_dup, offset);
1078 cpml_primitive_offset(last_dup, offset);
1079 if (cpml_primitive_intersection(current_dup, last_dup,
1080 &center, 1) == 0) {
1081 g_warning("Fillet not applicable (radius = %lf)", radius);
1082 g_free(current_dup);
1083 g_free(last_dup);
1084 return;
1087 /* Compute the start point of the fillet */
1088 pos = cpml_primitive_near_pos(last_dup, &center);
1089 cpml_primitive_vector_at(last_dup, &vector, pos);
1090 cpml_vector_set_length(&vector, offset);
1091 cpml_vector_normal(&vector);
1092 cpml_pair_sub(cpml_pair_copy(&p[0], &center), &vector);
1094 /* Compute the mid point of the fillet */
1095 cpml_pair_from_cairo(&vector, current->org);
1096 cpml_pair_sub(&vector, &center);
1097 cpml_vector_set_length(&vector, radius);
1098 cpml_pair_add(cpml_pair_copy(&p[1], &center), &vector);
1100 /* Compute the end point of the fillet */
1101 pos = cpml_primitive_near_pos(current_dup, &center);
1102 cpml_primitive_vector_at(current_dup, &vector, pos);
1103 cpml_vector_set_length(&vector, offset);
1104 cpml_vector_normal(&vector);
1105 cpml_pair_sub(cpml_pair_copy(&p[2], &center), &vector);
1107 g_free(current_dup);
1108 g_free(last_dup);
1110 /* Modify the end point of the last primitive */
1111 cpml_pair_to_cairo(&p[0], cpml_primitive_get_point(last, -1));
1113 /* Add the fillet arc */
1114 arc[0].header.type = CAIRO_PATH_ARC_TO;
1115 arc[0].header.length = 3;
1116 cpml_pair_to_cairo(&p[1], &arc[1]);
1117 cpml_pair_to_cairo(&p[2], &arc[2]);
1118 data->path = g_array_append_vals(data->path, arc, 3);
1120 data->operation.operator = ADG_OPERATOR_NONE;
1123 static gboolean
1124 is_convex(const CpmlPrimitive *primitive1, const CpmlPrimitive *primitive2)
1126 CpmlVector v1, v2;
1127 gdouble angle1, angle2;
1129 cpml_primitive_vector_at(primitive1, &v1, -1);
1130 cpml_primitive_vector_at(primitive2, &v2, 0);
1132 /* Probably there is a smarter way to get this without trygonometry */
1133 angle1 = cpml_vector_angle(&v1);
1134 angle2 = cpml_vector_angle(&v2);
1136 if (angle1 > angle2)
1137 angle1 -= M_PI*2;
1139 return angle2-angle1 > M_PI;