[AdgPath] Using CpmlPath where applicable
[adg.git] / adg / adg-path.c
blob6a09b15e11bd62f5897e87bb6e1124c32d0a5193
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 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.
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 CpmlPath * 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 * 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
215 CpmlPath *
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_current_point:
227 * @path: an #AdgPath
228 * @x: where to store the x coordinate of the current point
229 * @y: where to store the y coordinate of the current point
231 * Gets the current point of @path, which is conceptually the
232 * final point reached by the path so far.
234 * If there is no defined current point, @x and @y will both be set
235 * to 0 and a warning will be triggered. It is possible to check this
236 * in advance with adg_path_has_current_point().
238 * Most #AdgPath methods alter the current point and most of them
239 * expect a current point to be defined otherwise will fail triggering
240 * a warning. Check the description of every method for specific details.
242 void
243 adg_path_get_current_point(AdgPath *path, gdouble *x, gdouble *y)
245 AdgPathPrivate *data;
247 g_return_if_fail(ADG_IS_PATH(path));
249 data = path->data;
251 if (data->cp_is_valid) {
252 *x = data->cp.x;
253 *y = data->cp.y;
254 } else {
255 *x = *y = 0.;
256 g_return_if_reached();
261 * adg_path_has_current_point:
262 * @path: an #AdgPath
264 * Returns whether a current point is defined on @path.
265 * See adg_path_get_current_point() for details on the current point.
267 * Returns: whether a current point is defined
269 gboolean
270 adg_path_has_current_point(AdgPath *path)
272 AdgPathPrivate *data;
274 g_return_val_if_fail(ADG_IS_PATH(path), FALSE);
276 data = path->data;
278 return data->cp_is_valid;
282 * adg_path_clear:
283 * @path: an #AdgPath
285 * Releases the internal memory hold by @path and resets its status,
286 * so that after this call @path contains an empty path.
288 void
289 adg_path_clear(AdgPath *path)
291 AdgPathPrivate *data;
293 g_return_if_fail(ADG_IS_PATH(path));
295 data = path->data;
297 g_array_set_size(data->path, 0);
298 clear_cairo_path(path);
299 clear_operation(path);
304 * adg_path_append:
305 * @path: an #AdgPath
306 * @type: a #cairo_data_type_t value
307 * @...: point data, specified as #AdgPair pointers
309 * Generic method to append a primitive to @path. The number of #AdgPair
310 * structs depends on @type: there is no way with this function to
311 * reserve more cairo_path_data_t structs than what is needed by the
312 * primitive.
314 * This function accepts also the special %CAIRO_PATH_ARC_TO primitive.
316 * If @path has no current point while the requested primitive needs it,
317 * a warning message will be triggered without other effect.
319 void
320 adg_path_append(AdgPath *path, CpmlPrimitiveType type, ...)
322 va_list var_args;
324 va_start(var_args, type);
325 adg_path_append_valist(path, type, var_args);
326 va_end(var_args);
330 * adg_path_append_valist:
331 * @path: an #AdgPath
332 * @type: a #cairo_data_type_t value
333 * @var_args: point data, specified as #AdgPair pointers
335 * va_list version of adg_path_append().
337 void
338 adg_path_append_valist(AdgPath *path, CpmlPrimitiveType type, va_list var_args)
340 AdgPathPrivate *data;
341 AdgPrimitive primitive;
342 gint length, cnt;
343 cairo_path_data_t org;
344 cairo_path_data_t *path_data;
346 g_return_if_fail(ADG_IS_PATH(path));
348 data = path->data;
349 length = needed_pairs(type, data->cp_is_valid);
350 if (length == 0)
351 return;
353 /* Set a copy of the current point as the primitive origin */
354 cpml_pair_to_cairo(&data->cp, &org);
355 primitive.org = &org;
357 /* Build the cairo_path_data_t array */
358 primitive.data = path_data = g_new(cairo_path_data_t, length);
360 path_data->header.type = type;
361 path_data->header.length = length;
363 for (cnt = 1; cnt < length; ++ cnt) {
364 ++ path_data;
365 cpml_pair_to_cairo(va_arg(var_args, AdgPair *), path_data);
368 /* Terminate the creation of the temporary primitive */
369 primitive.segment = NULL;
371 /* Append this primitive to @path */
372 append_primitive(path, &primitive);
374 g_free(primitive.data);
378 * adg_path_append_primitive:
379 * @path: an #AdgPath
380 * @primitive: the #AdgPrimitive to append
382 * Appends @primitive to @path. The primitive to add is considered the
383 * continuation of the current path so the <structfield>org</structfield>
384 * component of @primitive is not used. Anyway the current poins is
385 * checked against it: they must be equal or the function will fail
386 * without further processing.
388 void
389 adg_path_append_primitive(AdgPath *path, const AdgPrimitive *primitive)
391 AdgPathPrivate *data;
392 AdgPrimitive *primitive_dup;
394 g_return_if_fail(ADG_IS_PATH(path));
395 g_return_if_fail(primitive != NULL);
397 data = path->data;
399 g_return_if_fail(primitive->org->point.x == data->cp.x &&
400 primitive->org->point.y == data->cp.y);
402 /* The primitive data could be modified by pending operations:
403 * work on a copy */
404 primitive_dup = adg_primitive_deep_dup(primitive);
406 append_primitive(path, primitive_dup);
408 g_free(primitive_dup);
412 * adg_path_append_segment:
413 * @path: an #AdgPath
414 * @segment: the #AdgSegment to append
416 * Appends @segment to @path.
418 void
419 adg_path_append_segment(AdgPath *path, const AdgSegment *segment)
421 AdgPathPrivate *data;
423 g_return_if_fail(ADG_IS_PATH(path));
424 g_return_if_fail(segment != NULL);
426 data = path->data;
428 clear_cairo_path(path);
429 data->path = g_array_append_vals(data->path,
430 segment->data, segment->num_data);
434 * adg_path_append_cairo_path:
435 * @path: an #AdgPath
436 * @cairo_path: the #cairo_path_t path to append
438 * Appends a whole cairo path to @path.
440 void
441 adg_path_append_cairo_path(AdgPath *path, const cairo_path_t *cairo_path)
443 AdgPathPrivate *data;
445 g_return_if_fail(ADG_IS_PATH(path));
447 data = path->data;
449 clear_cairo_path(path);
450 data->path = g_array_append_vals(data->path,
451 cairo_path->data, cairo_path->num_data);
455 * adg_path_move_to:
456 * @path: an #AdgPath
457 * @x: the new x coordinate
458 * @y: the new y coordinate
460 * Begins a new segment. After this call the current point will be (@x, @y).
462 void
463 adg_path_move_to(AdgPath *path, gdouble x, gdouble y)
465 AdgPair p;
467 p.x = x;
468 p.y = y;
470 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p);
474 * adg_path_line_to:
475 * @path: an #AdgPath
476 * @x: the new x coordinate
477 * @y: the new y coordinate
479 * Adds a line to @path from the current point to position (@x, @y).
480 * After this call the current point will be (@x, @y).
482 * If @path has no current point before this call, this function will
483 * trigger a warning without other effect.
485 void
486 adg_path_line_to(AdgPath *path, gdouble x, gdouble y)
488 AdgPair p;
490 p.x = x;
491 p.y = y;
493 adg_path_append(path, CAIRO_PATH_LINE_TO, &p);
497 * adg_path_arc_to:
498 * @path: an #AdgPath
499 * @x1: the x coordinate of an intermediate point
500 * @y1: the y coordinate of an intermediate point
501 * @x2: the x coordinate of the end of the arc
502 * @y2: the y coordinate of the end of the arc
504 * Adds an arc to the path from the current point to (@x2, @y2),
505 * passing throught (@x1, @y1). After this call the current point
506 * will be (@x2, @y2).
508 * If @path has no current point before this call, this function will
509 * trigger a warning without other effect.
511 void
512 adg_path_arc_to(AdgPath *path, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
514 AdgPair p[2];
516 p[0].x = x1;
517 p[0].y = y1;
518 p[1].x = x2;
519 p[1].y = y2;
521 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[0], &p[1]);
525 * adg_path_curve_to:
526 * @path: an #AdgPath
527 * @x1: the x coordinate of the first control point
528 * @y1: the y coordinate of the first control point
529 * @x2: the x coordinate of the second control point
530 * @y2: the y coordinate of the second control point
531 * @x3: the x coordinate of the end of the curve
532 * @y3: the y coordinate of the end of the curve
534 * Adds a cubic Bézier curve to the path from the current point to
535 * position (@x3, @y3), using (@x1, @y1) and (@x2, @y2) as the
536 * control points. After this call the current point will be (@x3, @y3).
538 * If @path has no current point before this call, this function will
539 * trigger a warning without other effect.
541 void
542 adg_path_curve_to(AdgPath *path, gdouble x1, gdouble y1,
543 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
545 AdgPair p[3];
547 p[0].x = x1;
548 p[0].y = y1;
549 p[1].x = x2;
550 p[1].y = y2;
551 p[2].x = x3;
552 p[2].y = y3;
554 adg_path_append(path, CAIRO_PATH_CURVE_TO, &p[0], &p[1], &p[2]);
558 * adg_path_close:
559 * @path: an #AdgPath
561 * Adds a line segment to the path from the current point to the
562 * beginning of the current segment, (the most recent point passed
563 * to an adg_path_move_to()), and closes this segment.
564 * After this call the current point will be unset.
566 * The behavior of adg_path_close() is distinct from simply calling
567 * adg_line_to() with the coordinates of the segment starting point.
568 * When a closed segment is stroked, there are no caps on the ends.
569 * Instead, there is a line join connecting the final and initial
570 * primitive of the segment.
572 * If @path has no current point before this call, this function will
573 * trigger a warning without other effect.
575 void
576 adg_path_close(AdgPath *path)
578 adg_path_append(path, CAIRO_PATH_CLOSE_PATH);
582 * adg_path_arc
583 * @path: an #AdgPath
584 * @xc: x position of the center of the arc
585 * @yc: y position of the center of the arc
586 * @r: the radius of the arc
587 * @start: the start angle, in radians
588 * @end: the end angle, in radians
590 * A more usual way to add an arc to @path. After this call, the current
591 * point will be the computed end point of the arc. The arc will be
592 * rendered in increasing angle, accordling to @start and @end. This means
593 * if @start is less than @end, the arc will be rendered in clockwise
594 * direction (accordling to the default cairo coordinate system) while if
595 * @start is greather than @end, the arc will be rendered in couterclockwise
596 * direction.
598 * By explicitely setting the whole arc data, the start point could be
599 * different from the current point. In this case, if @path has no
600 * current point before the call a %CAIRO_PATH_MOVE_TO to the start
601 * point of the arc will be automatically prepended to the arc.
602 * If @path has a current point, a %CAIRO_PATH_LINE_TO to the start
603 * point of the arc will be used instead of the moveto.
605 void
606 adg_path_arc(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
607 gdouble start, gdouble end)
609 AdgPathPrivate *data;
610 AdgPair center, p[3];
612 g_return_if_fail(ADG_IS_PATH(path));
614 data = path->data;
615 center.x = xc;
616 center.y = yc;
618 cpml_vector_from_angle(&p[0], start, r);
619 cpml_vector_from_angle(&p[1], (end-start) / 2, r);
620 cpml_vector_from_angle(&p[2], end, r);
622 cpml_pair_add(&p[0], &center);
623 cpml_pair_add(&p[1], &center);
624 cpml_pair_add(&p[2], &center);
626 if (!data->cp_is_valid)
627 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p[0]);
628 else if (p[0].x != data->cp.x || p[0].y != data->cp.y)
629 adg_path_append(path, CAIRO_PATH_LINE_TO, &p[0]);
631 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[1], &p[2]);
635 * adg_path_chamfer
636 * @path: an #AdgPath
637 * @delta1: the distance from the intersection point of the current primitive
638 * @delta2: the distance from the intersection point of the next primitive
640 * A binary operator that generates a chamfer between two primitives.
641 * The first primitive involved is the current primitive, the second will
642 * be the next primitive appended to @path after this call. The second
643 * primitive is required: if the chamfer operation is not properly
644 * terminated (by not providing the second primitive), any API accessing
645 * the path in reading mode will raise a warning.
647 * The chamfer operation requires two lengths: @delta1 specifies the
648 * "quantity" to trim on the first primitive while @delta2 is the same
649 * applied on the second primitive. The term "quantity" means the length
650 * of the portion to cut out from the original primitive (that is the
651 * primitive as would be without the chamfer).
653 void
654 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
656 g_return_if_fail(ADG_IS_PATH(path));
658 if (!append_operation(path, ADG_OPERATOR_CHAMFER, delta1, delta2))
659 return;
663 * adg_path_fillet:
664 * @path: an #AdgPath
665 * @radius: the radius of the fillet
668 * A binary operator that joins to primitives with an arc.
669 * The first primitive involved is the current primitive, the second will
670 * be the next primitive appended to @path after this call. The second
671 * primitive is required: if the fillet operation is not properly
672 * terminated (by not providing the second primitive), any API accessing
673 * the path in reading mode will raise a warning.
675 void
676 adg_path_fillet(AdgPath *path, gdouble radius)
678 g_return_if_fail(ADG_IS_PATH(path));
680 if (!append_operation(path, ADG_OPERATOR_FILLET, radius))
681 return;
686 * adg_path_dump:
687 * @path: an #AdgPath
689 * Dumps the data content of @path to stdout in a human readable format.
691 void
692 adg_path_dump(AdgPath *path)
694 CpmlSegment segment;
695 cairo_path_t *cairo_path;
697 g_return_if_fail(ADG_IS_PATH(path));
699 cairo_path = get_cairo_path(path);
701 g_return_if_fail(cairo_path != NULL);
703 if (!cpml_segment_from_cairo(&segment, cairo_path)) {
704 g_warning("Invalid path data to dump!\n");
705 } else {
706 do {
707 cpml_segment_dump(&segment);
708 } while (cpml_segment_next(&segment));
713 static void
714 changed(AdgModel *model)
716 AdgModelClass *model_class = (AdgModelClass *) adg_path_parent_class;
718 adg_path_clear((AdgPath *) model);
720 if (model_class->changed != NULL)
721 model_class->changed(model);
724 static void
725 clear_cairo_path(AdgPath *path)
727 AdgPathPrivate *data;
728 cairo_path_t *cairo_path;
730 data = path->data;
731 cairo_path = &data->cairo_path;
733 if (cairo_path->data == NULL)
734 return;
736 g_free(cairo_path->data);
738 cairo_path->status = CAIRO_STATUS_INVALID_PATH_DATA;
739 cairo_path->data = NULL;
740 cairo_path->num_data = 0;
743 static cairo_path_t *
744 get_cairo_path(AdgPath *path)
746 AdgPathPrivate *data;
747 cairo_path_t *cairo_path;
748 const GArray *src;
749 GArray *dst;
750 const cairo_path_data_t *p_src;
751 int i;
753 data = path->data;
754 cairo_path = &data->cairo_path;
756 /* Check for cached result */
757 if (cairo_path->data != NULL)
758 return cairo_path;
760 src = data->path;
761 dst = g_array_sized_new(FALSE, FALSE, sizeof(cairo_path_data_t), src->len);
763 /* Cycle the path and convert arcs to Bézier curves */
764 for (i = 0; i < src->len; i += p_src->header.length) {
765 p_src = (const cairo_path_data_t *) src->data + i;
767 if (p_src->header.type == CAIRO_PATH_ARC_TO)
768 dst = arc_to_curves(dst, p_src);
769 else
770 dst = g_array_append_vals(dst, p_src, p_src->header.length);
773 cairo_path->status = CAIRO_STATUS_SUCCESS;
774 cairo_path->num_data = dst->len;
775 cairo_path->data = (cairo_path_data_t *) g_array_free(dst, FALSE);
777 return cairo_path;
780 static CpmlPath *
781 get_cpml_path(AdgPath *path)
783 AdgPathPrivate *data;
784 CpmlPath *cpml_path;
786 data = path->data;
787 cpml_path = &data->cpml_path;
789 cpml_path->status = CAIRO_STATUS_SUCCESS;
790 cpml_path->data = (cairo_path_data_t *) data->path->data;
791 cpml_path->num_data = data->path->len;
793 return cpml_path;
796 static GArray *
797 arc_to_curves(GArray *array, const cairo_path_data_t *src)
799 CpmlPrimitive arc;
800 double start, end;
802 /* Build the arc primitive: the arc origin is supposed to be the previous
803 * point (src-1): this means a primitive must exist before the arc */
804 arc.segment = NULL;
805 arc.org = (cairo_path_data_t *) (src-1);
806 arc.data = (cairo_path_data_t *) src;
808 if (cpml_arc_info(&arc, NULL, NULL, &start, &end)) {
809 CpmlSegment segment;
810 int n_curves;
811 cairo_path_data_t *curves;
813 n_curves = ceil(fabs(end-start) / M_PI_2);
814 curves = g_new(cairo_path_data_t, n_curves * 4);
815 segment.data = curves;
816 cpml_arc_to_curves(&arc, &segment, n_curves);
818 array = g_array_append_vals(array, curves, n_curves * 4);
820 g_free(curves);
823 return array;
826 static void
827 append_primitive(AdgPath *path, AdgPrimitive *current)
829 AdgPathPrivate *data;
830 cairo_path_data_t *path_data;
831 int length;
833 data = path->data;
834 path_data = current->data;
835 length = path_data[0].header.length;
837 /* Execute any pending operation */
838 do_operation(path, path_data);
840 /* Append the path data to the internal path array */
841 data->path = g_array_append_vals(data->path, path_data, length);
843 /* Set path data to point to the recently appended cairo_path_data_t
844 * primitive: the first struct is the header */
845 path_data = (cairo_path_data_t *) data->path->data +
846 data->path->len - length;
848 /* Set the last primitive for subsequent binary operations */
849 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
850 data->last.segment = NULL;
851 data->last.data = path_data;
853 /* Save the last point as the current point, if applicable */
854 data->cp_is_valid = length > 1;
855 if (length > 1)
856 cpml_pair_from_cairo(&data->cp, &path_data[length-1]);
858 /* Invalidate cairo_path: should be recomputed */
859 clear_cairo_path(path);
862 static gint
863 needed_pairs(CpmlPrimitiveType type, gboolean cp_is_valid)
865 switch (type) {
867 case CAIRO_PATH_CLOSE_PATH:
868 g_return_val_if_fail(cp_is_valid, 0);
869 return 1;
871 case CAIRO_PATH_MOVE_TO:
872 return 2;
874 case CAIRO_PATH_LINE_TO:
875 g_return_val_if_fail(cp_is_valid, 0);
876 return 2;
878 case CAIRO_PATH_ARC_TO:
879 g_return_val_if_fail(cp_is_valid, 0);
880 return 3;
882 case CAIRO_PATH_CURVE_TO:
883 g_return_val_if_fail(cp_is_valid, 0);
884 return 4;
886 default:
887 g_return_val_if_reached(0);
890 return 0;
893 static void
894 clear_operation(AdgPath *path)
896 AdgPathPrivate *data;
897 AdgOperation *operation;
899 data = path->data;
900 operation = &data->operation;
902 if (operation->operator == ADG_OPERATOR_NONE)
903 return;
905 g_warning("An operation is still active while clearing the path "
906 "(operator `%d')", operation->operator);
907 operation->operator = ADG_OPERATOR_NONE;
910 static gboolean
911 append_operation(AdgPath *path, AdgOperator operator, ...)
913 AdgPathPrivate *data;
914 AdgOperation *operation;
915 va_list var_args;
917 data = path->data;
919 if (!data->cp_is_valid) {
920 g_warning("Operation requested but path has no current primitive "
921 "(operator `%d')", operator);
922 return FALSE;
925 operation = &data->operation;
926 if (operation->operator != ADG_OPERATOR_NONE) {
927 /* TODO: this is a rude semplification, as a lot of operators can
928 * and may cohexist. As an example, a fillet followed by a
929 * polar chamfer is not difficult to compute */
930 g_warning("Operation requested but another operation is yet active"
931 "(operators: new `%d', old `%d')",
932 operator, operation->operator);
933 return FALSE;
936 va_start(var_args, operator);
938 switch (operator) {
940 case ADG_OPERATOR_CHAMFER:
941 operation->data.chamfer.delta1 = va_arg(var_args, double);
942 operation->data.chamfer.delta2 = va_arg(var_args, double);
943 break;
945 case ADG_OPERATOR_FILLET:
946 operation->data.fillet.radius = va_arg(var_args, double);
947 break;
949 case ADG_OPERATOR_NONE:
950 va_end(var_args);
951 return TRUE;
953 default:
954 g_warning("Operation not recognized (operator `%d')", operator);
955 va_end(var_args);
956 return FALSE;
959 operation->operator = operator;
960 va_end(var_args);
962 return TRUE;
965 static void
966 do_operation(AdgPath *path, cairo_path_data_t *path_data)
968 AdgPathPrivate *data;
969 AdgOperator operator;
970 CpmlSegment segment;
971 CpmlPrimitive current;
972 cairo_path_data_t current_org;
974 data = path->data;
975 operator = data->operation.operator;
976 cpml_segment_from_cairo(&segment, get_cpml_path(path));
978 /* Construct the current primitive, that is the primitive to be inserted.
979 * Its org is a copy of the end point of the last primitive: it can be
980 * modified without affecting anything else. It is expected the operation
981 * functions will add to @path the primitives required but NOT to add
982 * @current, as this one will be inserted automatically. */
983 current.segment = &segment;
984 current.org = &current_org;
985 current.data = path_data;
986 cpml_pair_to_cairo(&data->cp, &current_org);
988 switch (operator) {
990 case ADG_OPERATOR_NONE:
991 return;
993 case ADG_OPERATOR_CHAMFER:
994 do_chamfer(path, &current);
995 break;
997 case ADG_OPERATOR_FILLET:
998 do_fillet(path, &current);
999 break;
1001 default:
1002 g_warning("Operation not implemented (operator `%d')", operator);
1003 return;
1007 static void
1008 do_chamfer(AdgPath *path, CpmlPrimitive *current)
1010 AdgPathPrivate *data;
1011 CpmlPrimitive *last;
1012 gdouble delta1, delta2;
1013 gdouble len1, len2;
1014 AdgPair pair;
1015 cairo_path_data_t line[2];
1017 data = path->data;
1018 last = &data->last;
1019 delta1 = data->operation.data.chamfer.delta1;
1020 len1 = cpml_primitive_length(last);
1022 if (delta1 >= len1) {
1023 g_warning("Chamfer too big for the last primitive (%lf >= %lf)",
1024 delta1, len1);
1025 return;
1028 delta2 = data->operation.data.chamfer.delta2;
1029 len2 = cpml_primitive_length(current);
1031 if (delta2 >= len2) {
1032 g_warning("Chamfer too big for the current primitive (%lf >= %lf)",
1033 delta2, len2);
1034 return;
1037 /* Change the end point of the last primitive */
1038 cpml_primitive_pair_at(last, &pair, 1. - delta1 / len1);
1039 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(last, -1));
1041 /* Change the start point of the current primitive */
1042 cpml_primitive_pair_at(current, &pair, delta2 / len2);
1043 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(current, 0));
1045 /* Add the chamfer line */
1046 line[0].header.type = CAIRO_PATH_LINE_TO;
1047 line[0].header.length = 2;
1048 line[1].point.x = pair.x;
1049 line[1].point.y = pair.y;
1050 data->path = g_array_append_vals(data->path, line, 2);
1052 data->operation.operator = ADG_OPERATOR_NONE;
1055 static void
1056 do_fillet(AdgPath *path, CpmlPrimitive *current)
1058 AdgPathPrivate *data;
1059 CpmlPrimitive *last, *current_dup, *last_dup;
1060 gdouble radius, offset, pos;
1061 AdgPair center, vector, p[3];
1062 cairo_path_data_t arc[3];
1064 data = path->data;
1065 last = &data->last;
1066 current_dup = adg_primitive_deep_dup(current);
1068 /* Force current_dup to point to the original segment so a
1069 * CAIRO_PATH_CLOSE_PATH primitive will work as expected */
1070 current_dup->segment = current->segment;
1072 last_dup = adg_primitive_deep_dup(last);
1073 radius = data->operation.data.fillet.radius;
1074 offset = is_convex(last_dup, current_dup) ? -radius : radius;
1076 /* Find the center of the fillet from the intersection between
1077 * the last and current primitives offseted by radius */
1078 cpml_primitive_offset(current_dup, offset);
1079 cpml_primitive_offset(last_dup, offset);
1080 if (cpml_primitive_intersection(current_dup, last_dup,
1081 &center, 1) == 0) {
1082 g_warning("Fillet not applicable (radius = %lf)", radius);
1083 g_free(current_dup);
1084 g_free(last_dup);
1085 return;
1088 /* Compute the start point of the fillet */
1089 pos = cpml_primitive_near_pos(last_dup, &center);
1090 cpml_primitive_vector_at(last_dup, &vector, pos);
1091 cpml_vector_set_length(&vector, offset);
1092 cpml_vector_normal(&vector);
1093 cpml_pair_sub(cpml_pair_copy(&p[0], &center), &vector);
1095 /* Compute the mid point of the fillet */
1096 cpml_pair_from_cairo(&vector, current->org);
1097 cpml_pair_sub(&vector, &center);
1098 cpml_vector_set_length(&vector, radius);
1099 cpml_pair_add(cpml_pair_copy(&p[1], &center), &vector);
1101 /* Compute the end point of the fillet */
1102 pos = cpml_primitive_near_pos(current_dup, &center);
1103 cpml_primitive_vector_at(current_dup, &vector, pos);
1104 cpml_vector_set_length(&vector, offset);
1105 cpml_vector_normal(&vector);
1106 cpml_pair_sub(cpml_pair_copy(&p[2], &center), &vector);
1108 g_free(current_dup);
1109 g_free(last_dup);
1111 /* Modify the end point of the last primitive */
1112 cpml_pair_to_cairo(&p[0], cpml_primitive_get_point(last, -1));
1114 /* Add the fillet arc */
1115 arc[0].header.type = CAIRO_PATH_ARC_TO;
1116 arc[0].header.length = 3;
1117 cpml_pair_to_cairo(&p[1], &arc[1]);
1118 cpml_pair_to_cairo(&p[2], &arc[2]);
1119 data->path = g_array_append_vals(data->path, arc, 3);
1121 data->operation.operator = ADG_OPERATOR_NONE;
1124 static gboolean
1125 is_convex(const CpmlPrimitive *primitive1, const CpmlPrimitive *primitive2)
1127 CpmlVector v1, v2;
1128 gdouble angle1, angle2;
1130 cpml_primitive_vector_at(primitive1, &v1, -1);
1131 cpml_primitive_vector_at(primitive2, &v2, 0);
1133 /* Probably there is a smarter way to get this without trygonometry */
1134 angle1 = cpml_vector_angle(&v1);
1135 angle2 = cpml_vector_angle(&v2);
1137 if (angle1 > angle2)
1138 angle1 -= M_PI*2;
1140 return angle2-angle1 > M_PI;