[AdgPath] Updated docblock of adg_path_chamfer()
[adg.git] / adg / adg-path.c
blobf5a29e8f75c9231552afc6c8bf32ab19236b5d83
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:path
23 * @title: AdgPath
24 * @short_description: The basic model representing a generic path
26 * The #AdgPath model is a virtual path: in a few words, it is a
27 * simple conceptual #cairo_path_t struct. This class implements
28 * methods to manipulate the underlying cairo path.
30 * Although some of the provided methods are clearly based on the
31 * original cairo path manipulation API, their behavior could be
32 * sligthly different. This is intentional, because the ADG provides
33 * additional path manipulation algorithms, sometime quite complex,
34 * and a more restrictive filter on the path quality is required.
35 * Also, the ADG is designed to be used by technicians while cairo
36 * targets a broader range of developers.
38 * As an example, following the rule of the less surprise, some
39 * cairo functions guess the current point when it is not defined,
40 * while the #AdgPath methods trigger a warning without other effect.
41 * Furthermore, after a cairo_path_close_path() call a MOVE_TO
42 * primitive to the starting point of the segment is automatically
43 * added by cairo while in the ADG, after an adg_path_close(), the
44 * current point is simply unset.
45 **/
47 #include "adg-path.h"
48 #include "adg-path-private.h"
49 #include "adg-intl.h"
51 #include <math.h>
54 static void finalize (GObject *object);
55 static void changed (AdgModel *model);
56 static void clear_cairo_path (AdgPath *path);
57 static cairo_path_t * get_cairo_path (AdgPath *path);
58 static cairo_path_t * get_cpml_path (AdgPath *path);
59 static GArray * arc_to_curves (GArray *array,
60 const cairo_path_data_t *src);
61 static void append_primitive_valist (AdgPath *path,
62 cairo_path_data_type_t type,
63 int length,
64 va_list var_args);
65 static cairo_path_data_t *
66 create_nodes_valist (cairo_path_data_type_t type,
67 int length,
68 va_list var_args);
69 static void clear_operation (AdgPath *path);
70 static gboolean append_operation (AdgPath *path,
71 AdgOperator operator,
72 ...);
73 static void do_operation (AdgPath *path,
74 cairo_path_data_t *data);
75 static void do_chamfer (AdgPath *path,
76 CpmlPrimitive *current);
79 G_DEFINE_TYPE(AdgPath, adg_path, ADG_TYPE_MODEL);
82 static void
83 adg_path_class_init(AdgPathClass *klass)
85 GObjectClass *gobject_class;
86 AdgModelClass *model_class;
88 gobject_class = (GObjectClass *) klass;
89 model_class = (AdgModelClass *) klass;
91 g_type_class_add_private(klass, sizeof(AdgPathPrivate));
93 gobject_class->finalize = finalize;
95 model_class->changed = changed;
98 static void
99 adg_path_init(AdgPath *path)
101 AdgPathPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE(path, ADG_TYPE_PATH,
102 AdgPathPrivate);
104 priv->cp_is_valid = FALSE;
105 priv->path = g_array_new(FALSE, FALSE, sizeof(cairo_path_data_t));
106 priv->cairo_path.status = CAIRO_STATUS_INVALID_PATH_DATA;
107 priv->cairo_path.data = NULL;
108 priv->cairo_path.num_data = 0;
109 priv->operation.operator = ADG_OPERATOR_NONE;
111 path->priv = priv;
114 static void
115 finalize(GObject *object)
117 AdgPath *path;
118 GObjectClass *object_class;
120 path = (AdgPath *) object;
121 object_class = (GObjectClass *) adg_path_parent_class;
123 g_array_free(path->priv->path, TRUE);
124 clear_cairo_path(path);
125 clear_operation(path);
127 if (object_class->finalize != NULL)
128 object_class->finalize(object);
133 * adg_path_new:
135 * Creates a new path model. The path must be constructed in the @callback
136 * function: AdgPath will cache and reuse the cairo_copy_path() returned by
137 * the cairo context after the @callback call.
139 * Return value: the new model
141 AdgModel *
142 adg_path_new(void)
144 return (AdgModel *) g_object_new(ADG_TYPE_PATH, NULL);
149 * adg_path_get_cairo_path:
150 * @path: an #AdgPath
152 * Gets a pointer to the cairo path structure of @path. The return value
153 * is owned by @path and must be considered read-only.
155 * This function also converts %CAIRO_PATH_ARC_TO primitives, not
156 * recognized by cairo, into approximated Bézier curves. The conversion
157 * is cached so any furter request is O(1). This cache is cleared
158 * whenever @path is modified (by adding a new primitive or by calling
159 * adg_path_clear()).
161 * <important>
162 * <title>TODO</title>
163 * <itemizedlist>
164 * <listitem>Actually, the arcs are approximated to Bézier using the
165 * hardcoded max angle of PI/2. This should be customizable
166 * by adding, for instance, a property to the #AdgPath class
167 * with a default value of PI/2.</listitem>
168 * </itemizedlist>
169 * </important>
171 * Return value: a pointer to the internal cairo path or %NULL on errors
173 const cairo_path_t *
174 adg_path_get_cairo_path(AdgPath *path)
176 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
178 return get_cairo_path(path);
182 * adg_path_get_cpml_path:
183 * @path: an #AdgPath
185 * Gets a pointer to the cairo path structure of @path. The return
186 * value is owned by @path and must not be freed.
188 * This function is similar to adg_path_get_cairo_path() but with
189 * two important differences: firstly the arc primitives are not
190 * expanded to Bézier curves and secondly the returned path is
191 * not read-only. This means it is allowed to modify the returned
192 * path as long as its size is retained and its data contains a
193 * valid path.
195 * Keep in mind any changes to @path makes the value returned by
196 * this function useless, as it is likely to contain plain garbage.
198 * Return value: a pointer to the internal cpml path or %NULL on errors
200 cairo_path_t *
201 adg_path_get_cpml_path(AdgPath *path)
203 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
205 clear_cairo_path(path);
207 return get_cpml_path(path);
211 * adg_path_dup_cpml_path:
212 * @path: an #AdgPath
214 * Gets a duplicate of the cairo path structure of @path. The return
215 * value should be freed with g_free(). The returned #cairo_path_t
216 * struct also contains the #cairo_path_data_t array in the same
217 * chunk of memory, so a g_free() to the returned pointer also frees
218 * the data array.
220 * Although quite similar to adg_path_get_cpml_path(), the use case
221 * of this method is different: being a duplicate, modifying the
222 * returned path does not modify @path itsself.
224 * Return value: a newly allocated #cairo_path_t pointer to be freed
225 * with g_free() or %NULL on errors
227 cairo_path_t *
228 adg_path_dup_cpml_path(AdgPath *path)
230 cairo_path_t *cpml_path;
231 gsize head_size, data_size;
233 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
235 head_size = sizeof(cairo_path_t);
236 data_size = sizeof(cairo_path_data_t) * path->priv->path->len;
238 /* Both the cairo_path_t struct and the cairo_path_data_t array
239 * are stored in the same chunk of memory, so only a single
240 * g_free() is needed */
241 cpml_path = g_malloc(head_size + data_size);
242 cpml_path->status = CAIRO_STATUS_SUCCESS;
243 cpml_path->data = (cairo_path_data_t *) ((gchar *) cpml_path + head_size);
244 cpml_path->num_data = path->priv->path->len;
245 memcpy(cpml_path->data, path->priv->path->data, data_size);
247 return cpml_path;
251 * adg_path_get_current_point:
252 * @path: an #AdgPath
253 * @x: return value for x coordinate of the current point
254 * @y: return value for y coordinate of the current point
256 * Gets the current point of @path, which is conceptually the
257 * final point reached by the path so far.
259 * If there is no defined current point, @x and @y will both be set
260 * to 0 and a warning will be triggered. It is possible to check this
261 * in advance with adg_path_has_current_point().
263 * Most #AdgPath methods alter the current point and most of them
264 * expect a current point to be defined otherwise will fail triggering
265 * a warning. Check the description of every method for specific details.
267 void
268 adg_path_get_current_point(AdgPath *path, gdouble *x, gdouble *y)
270 g_return_if_fail(ADG_IS_PATH(path));
272 if (path->priv->cp_is_valid) {
273 *x = path->priv->cp.x;
274 *y = path->priv->cp.y;
275 } else {
276 *x = *y = 0.;
277 g_return_if_reached();
282 * adg_path_has_current_point:
283 * @path: an #AdgPath
285 * Returns whether a current point is defined on @path.
286 * See adg_path_get_current_point() for details on the current point.
288 * Return value: whether a current point is defined
290 gboolean
291 adg_path_has_current_point(AdgPath *path)
293 g_return_val_if_fail(ADG_IS_PATH(path), FALSE);
295 return path->priv->cp_is_valid;
299 * adg_path_clear:
300 * @path: an #AdgPath
302 * Releases the internal memory hold by @path and resets its status,
303 * so that after this call @path contains an empty path.
305 void
306 adg_path_clear(AdgPath *path)
308 g_return_if_fail(ADG_IS_PATH(path));
310 g_array_set_size(path->priv->path, 0);
311 clear_cairo_path(path);
312 clear_operation(path);
317 * adg_path_append:
318 * @path: an #AdgPath
319 * @type: a #cairo_data_type_t value
320 * @...: point data, specified as #AdgPair pointers
322 * Generic method to append a primitive to @path. The number of #AdgPair
323 * structs depends on @type: there is no way with this function to
324 * reserve more cairo_path_data_t structs than what is needed by the
325 * primitive.
327 * This function accepts also the special %CAIRO_PATH_ARC_TO primitive.
329 * If @path has no current point while the requested primitive needs it,
330 * a warning message will be triggered without other effect.
332 void
333 adg_path_append(AdgPath *path, cairo_path_data_type_t type, ...)
335 va_list var_args;
337 va_start(var_args, type);
338 adg_path_append_valist(path, type, var_args);
339 va_end(var_args);
343 * adg_path_append_valist:
344 * @path: an #AdgPath
345 * @type: a #cairo_data_type_t value
346 * @var_args: point data, specified as #AdgPair pointers
348 * va_list version of adg_path_append().
350 void
351 adg_path_append_valist(AdgPath *path, cairo_path_data_type_t type,
352 va_list var_args)
354 gint length;
356 g_return_if_fail(ADG_IS_PATH(path));
358 switch (type) {
360 case CAIRO_PATH_CLOSE_PATH:
361 g_return_if_fail(path->priv->cp_is_valid);
362 length = 1;
363 break;
365 case CAIRO_PATH_MOVE_TO:
366 length = 2;
367 break;
369 case CAIRO_PATH_LINE_TO:
370 g_return_if_fail(path->priv->cp_is_valid);
371 length = 2;
372 break;
374 case CAIRO_PATH_ARC_TO:
375 g_return_if_fail(path->priv->cp_is_valid);
376 length = 3;
377 break;
379 case CAIRO_PATH_CURVE_TO:
380 g_return_if_fail(path->priv->cp_is_valid);
381 length = 4;
382 break;
384 default:
385 g_assert_not_reached();
386 return;
389 append_primitive_valist(path, type, length, var_args);
393 * adg_path_append_cairo_path:
394 * @path: an #AdgPath
395 * @cairo_path: the #cairo_path_t path to append
397 * Appends a whole cairo path to @path.
399 void
400 adg_path_append_cairo_path(AdgPath *path, const cairo_path_t *cairo_path)
402 g_return_if_fail(ADG_IS_PATH(path));
404 clear_cairo_path(path);
405 path->priv->path = g_array_append_vals(path->priv->path,
406 cairo_path->data,
407 cairo_path->num_data);
412 * adg_path_move_to:
413 * @path: an #AdgPath
414 * @x: the new x coordinate
415 * @y: the new y coordinate
417 * Begins a new segment. After this call the current point will be (@x, @y).
419 void
420 adg_path_move_to(AdgPath *path, gdouble x, gdouble y)
422 AdgPair p;
424 p.x = x;
425 p.y = y;
427 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p);
431 * adg_path_line_to:
432 * @path: an #AdgPath
433 * @x: the new x coordinate
434 * @y: the new y coordinate
436 * Adds a line to @path from the current point to position (@x, @y).
437 * After this call the current point will be (@x, @y).
439 * If @path has no current point before this call, this function will
440 * trigger a warning without other effect.
442 void
443 adg_path_line_to(AdgPath *path, gdouble x, gdouble y)
445 AdgPair p;
447 p.x = x;
448 p.y = y;
450 adg_path_append(path, CAIRO_PATH_LINE_TO, &p);
454 * adg_path_arc_to:
455 * @path: an #AdgPath
456 * @x1: the x coordinate of an intermediate point
457 * @y1: the y coordinate of an intermediate point
458 * @x2: the x coordinate of the end of the arc
459 * @y2: the y coordinate of the end of the arc
461 * Adds an arc to the path from the current point to (@x2, @y2),
462 * passing throught (@x1, @y1). After this call the current point
463 * will be (@x2, @y2).
465 * If @path has no current point before this call, this function will
466 * trigger a warning without other effect.
468 void
469 adg_path_arc_to(AdgPath *path, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
471 AdgPair p[2];
473 p[0].x = x1;
474 p[0].y = y1;
475 p[1].x = x2;
476 p[1].y = y2;
478 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[0], &p[1]);
482 * adg_path_curve_to:
483 * @path: an #AdgPath
484 * @x1: the x coordinate of the first control point
485 * @y1: the y coordinate of the first control point
486 * @x2: the x coordinate of the second control point
487 * @y2: the y coordinate of the second control point
488 * @x3: the x coordinate of the end of the curve
489 * @y3: the y coordinate of the end of the curve
491 * Adds a cubic Bézier curve to the path from the current point to
492 * position (@x3, @y3), using (@x1, @y1) and (@x2, @y2) as the
493 * control points. After this call the current point will be (@x3, @y3).
495 * If @path has no current point before this call, this function will
496 * trigger a warning without other effect.
498 void
499 adg_path_curve_to(AdgPath *path, gdouble x1, gdouble y1,
500 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
502 AdgPair p[3];
504 p[0].x = x1;
505 p[0].y = y1;
506 p[1].x = x2;
507 p[1].y = y2;
508 p[2].x = x3;
509 p[2].y = y3;
511 adg_path_append(path, CAIRO_PATH_CURVE_TO, &p[0], &p[1], &p[2]);
515 * adg_path_close:
516 * @path: an #AdgPath
518 * Adds a line segment to the path from the current point to the
519 * beginning of the current segment, (the most recent point passed
520 * to an adg_path_move_to()), and closes this segment.
521 * After this call the current point will be unset.
523 * The behavior of adg_path_close() is distinct from simply calling
524 * adg_line_to() with the coordinates of the segment starting point.
525 * When a closed segment is stroked, there are no caps on the ends.
526 * Instead, there is a line join connecting the final and initial
527 * primitive of the segment.
529 * If @path has no current point before this call, this function will
530 * trigger a warning without other effect.
532 void
533 adg_path_close(AdgPath *path)
535 adg_path_append(path, CAIRO_PATH_CLOSE_PATH);
539 * adg_path_arc
540 * @path: an #AdgPath
541 * @xc: x position of the center of the arc
542 * @yc: y position of the center of the arc
543 * @r: the radius of the arc
544 * @start: the start angle, in radians
545 * @end: the end angle, in radians
547 * A more usual way to add an arc to @path. After this call, the current
548 * point will be the computed end point of the arc. The arc will be
549 * rendered in increasing angle, accordling to @start and @end. This means
550 * if @start is less than @end, the arc will be rendered in clockwise
551 * direction (accordling to the default cairo coordinate system) while if
552 * @start is greather than @end, the arc will be rendered in couterclockwise
553 * direction.
555 * By explicitely setting the whole arc data, the start point could be
556 * different from the current point. In this case, if @path has no
557 * current point before the call a %CAIRO_PATH_MOVE_TO to the start
558 * point of the arc will be automatically prepended to the arc.
559 * If @path has a current point, a %CAIRO_PATH_LINE_TO to the start
560 * point of the arc will be used instead of the moveto.
562 void
563 adg_path_arc(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
564 gdouble start, gdouble end)
566 AdgPair center, p[3];
568 g_return_if_fail(ADG_IS_PATH(path));
570 center.x = xc;
571 center.y = yc;
573 cpml_vector_from_angle(&p[0], start, r);
574 cpml_vector_from_angle(&p[1], (end-start) / 2, r);
575 cpml_vector_from_angle(&p[2], end, r);
577 cpml_pair_add(&p[0], &center);
578 cpml_pair_add(&p[1], &center);
579 cpml_pair_add(&p[2], &center);
581 if (!path->priv->cp_is_valid)
582 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p[0]);
583 else if (p[0].x != path->priv->cp.x || p[0].y != path->priv->cp.y)
584 adg_path_append(path, CAIRO_PATH_LINE_TO, &p[0]);
586 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[1], &p[2]);
590 * adg_path_chamfer
591 * @path: an #AdgPath
592 * @delta1: the distance from the intersection point of the current primitive
593 * @delta2: the distance from the intersection point of the next primitive
595 * A binary operator that generates a chamfer between two primitives.
596 * The first primitive involved is the current primitive, the second will
597 * be the next primitive appended to @path after this call. The second
598 * primitive is required: if the chamfer operation is not properly
599 * terminated not providing the second primitive, any API accessing the
600 * path in reading mode will raise a warning.
602 * The chamfer operation requires two lengths: @delta1 specifies the
603 * "quantity" to trim on the first primitive while @delta2 is the same
604 * applied on the second primitive. The term "quantity" means the length
605 * of the portion to cut out from the original primitive (that is the
606 * primitive as would be without the chamfer).
608 void
609 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
611 g_return_if_fail(ADG_IS_PATH(path));
613 if (!append_operation(path, ADG_OPERATOR_CHAMFER, delta1, delta2))
614 return;
619 * adg_path_dump:
620 * @path: an #AdgPath
622 * Dumps the data content of @path to stdout in a human readable format.
624 void
625 adg_path_dump(AdgPath *path)
627 CpmlSegment segment;
628 cairo_path_t *cairo_path;
630 g_return_if_fail(ADG_IS_PATH(path));
632 cairo_path = get_cairo_path(path);
634 g_return_if_fail(cairo_path != NULL);
636 if (!cpml_segment_from_cairo(&segment, cairo_path)) {
637 g_warning("Invalid path data to dump!\n");
638 } else {
639 do {
640 cpml_segment_dump(&segment);
641 } while (cpml_segment_next(&segment));
646 static void
647 changed(AdgModel *model)
649 AdgModelClass *model_class = (AdgModelClass *) adg_path_parent_class;
651 adg_path_clear((AdgPath *) model);
653 if (model_class->changed != NULL)
654 model_class->changed(model);
657 static void
658 clear_cairo_path(AdgPath *path)
660 cairo_path_t *cairo_path = &path->priv->cairo_path;
662 if (cairo_path->data == NULL)
663 return;
665 g_free(cairo_path->data);
667 cairo_path->status = CAIRO_STATUS_INVALID_PATH_DATA;
668 cairo_path->data = NULL;
669 cairo_path->num_data = 0;
672 static cairo_path_t *
673 get_cairo_path(AdgPath *path)
675 cairo_path_t *cairo_path;
676 const GArray *src;
677 GArray *dst;
678 const cairo_path_data_t *p_src;
679 int i;
681 /* Check for cached result */
682 cairo_path = &path->priv->cairo_path;
683 if (cairo_path->data != NULL)
684 return cairo_path;
686 src = path->priv->path;
687 dst = g_array_sized_new(FALSE, FALSE, sizeof(cairo_path_data_t), src->len);
689 /* Cycle the path and convert arcs to Bézier curves */
690 for (i = 0; i < src->len; i += p_src->header.length) {
691 p_src = (const cairo_path_data_t *) src->data + i;
693 if (p_src->header.type == CAIRO_PATH_ARC_TO)
694 dst = arc_to_curves(dst, p_src);
695 else
696 dst = g_array_append_vals(dst, p_src, p_src->header.length);
699 cairo_path->status = CAIRO_STATUS_SUCCESS;
700 cairo_path->num_data = dst->len;
701 cairo_path->data = (cairo_path_data_t *) g_array_free(dst, FALSE);
703 return cairo_path;
706 static cairo_path_t *
707 get_cpml_path(AdgPath *path)
709 cairo_path_t *cpml_path = &path->priv->cpml_path;
711 cpml_path->status = CAIRO_STATUS_SUCCESS;
712 cpml_path->data = (cairo_path_data_t *) path->priv->path->data;
713 cpml_path->num_data = path->priv->path->len;
715 return cpml_path;
718 static GArray *
719 arc_to_curves(GArray *array, const cairo_path_data_t *src)
721 CpmlPrimitive arc;
722 double start, end;
724 /* Build the arc primitive: the arc origin is supposed to be the previous
725 * point (src-1): this means a primitive must exist before the arc */
726 arc.segment = NULL;
727 arc.org = (cairo_path_data_t *) (src-1);
728 arc.data = (cairo_path_data_t *) src;
730 if (cpml_arc_info(&arc, NULL, NULL, &start, &end)) {
731 CpmlSegment segment;
732 int n_curves;
733 cairo_path_data_t *curves;
735 n_curves = ceil(fabs(end-start) / M_PI_2);
736 curves = g_new(cairo_path_data_t, n_curves * 4);
737 segment.data = curves;
738 cpml_arc_to_curves(&arc, &segment, n_curves);
740 array = g_array_append_vals(array, curves, n_curves * 4);
742 g_free(curves);
745 return array;
748 static void
749 append_primitive_valist(AdgPath *path, cairo_path_data_type_t type,
750 int length, va_list var_args)
752 AdgPathPrivate *priv;
753 cairo_path_data_t *nodes;
755 priv = path->priv;
756 nodes = create_nodes_valist(type, length, var_args);
758 /* Execute any pending operation */
759 do_operation(path, nodes);
761 /* Append the nodes to the internal path array */
762 priv->path = g_array_append_vals(priv->path, nodes, length);
763 g_free(nodes);
764 nodes = (cairo_path_data_t *) priv->path->data + priv->path->len - length;
766 /* Set the last primitive for subsequent binary operations */
767 priv->last.org = priv->cp_is_valid ? nodes - 1 : NULL;
768 priv->last.segment = NULL;
769 priv->last.data = nodes;
771 /* Save the last point as the current point, if applicable */
772 priv->cp_is_valid = length > 1;
773 if (length > 1)
774 cpml_pair_from_cairo(&priv->cp, &nodes[length-1]);
776 /* Invalidate cairo_path: should be recomputed */
777 clear_cairo_path(path);
780 static cairo_path_data_t *
781 create_nodes_valist(cairo_path_data_type_t type, int length, va_list var_args)
783 cairo_path_data_t *node, *nodes;
785 node = nodes = g_new(cairo_path_data_t, length);
787 /* Append the header item */
788 node->header.type = type;
789 node->header.length = length;
791 /* Append the data items (that is, the AdgPair points) */
792 while (--length) {
793 ++ node;
794 cpml_pair_to_cairo(va_arg(var_args, AdgPair *), node);
797 return nodes;
800 static void
801 clear_operation(AdgPath *path)
803 AdgOperation *operation = &path->priv->operation;
805 if (operation->operator == ADG_OPERATOR_NONE)
806 return;
808 g_warning("An operation is still active while clearing the path "
809 "(operator `%d')", operation->operator);
810 operation->operator = ADG_OPERATOR_NONE;
813 static gboolean
814 append_operation(AdgPath *path, AdgOperator operator, ...)
816 AdgOperation *operation;
817 va_list var_args;
819 if (!path->priv->cp_is_valid) {
820 g_warning("Operation requested but path has no current primitive "
821 "(operator `%d')", operator);
822 return FALSE;
825 operation = &path->priv->operation;
826 va_start(var_args, operator);
828 if (operation->operator != ADG_OPERATOR_NONE) {
829 /* TODO: this is a rude semplification, as a lot of operators can
830 * and may cohexist. As an example, a fillet followed by a
831 * polar chamfer is not difficult to compute */
832 g_warning("Operation requested but another operation is yet active"
833 "(operators: new `%d', old `%d')",
834 operator, operation->operator);
835 return FALSE;
838 switch (operator) {
840 case ADG_OPERATOR_CHAMFER:
841 operation->data.chamfer.delta1 = va_arg(var_args, double);
842 operation->data.chamfer.delta2 = va_arg(var_args, double);
843 break;
845 case ADG_OPERATOR_FILLET:
846 operation->data.fillet.radius = va_arg(var_args, double);
847 break;
849 case ADG_OPERATOR_NONE:
850 va_end(var_args);
851 return TRUE;
853 default:
854 g_warning("Operation not recognized (operator `%d')", operator);
855 va_end(var_args);
856 return FALSE;
859 operation->operator = operator;
860 va_end(var_args);
862 return TRUE;
865 static void
866 do_operation(AdgPath *path, cairo_path_data_t *data)
868 AdgPathPrivate *priv;
869 AdgOperator operator;
870 CpmlPrimitive current;
871 cairo_path_data_t current_org;
873 priv = path->priv;
874 operator = priv->operation.operator;
875 current.segment = NULL;
876 current.org = &current_org;
877 current.data = data;
878 cpml_pair_to_cairo(&priv->cp, &current_org);
880 switch (operator) {
882 case ADG_OPERATOR_NONE:
883 return;
885 case ADG_OPERATOR_CHAMFER:
886 do_chamfer(path, &current);
887 break;
889 default:
890 g_warning("Operation not implemented (operator `%d')", operator);
891 return;
896 static void
897 do_chamfer(AdgPath *path, CpmlPrimitive *current)
899 AdgPathPrivate *priv;
900 CpmlPrimitive *last;
901 gdouble delta1, delta2;
902 gdouble len1, len2;
903 CpmlPair pair;
904 cairo_path_data_t line[2];
906 priv = path->priv;
907 last = &priv->last;
908 delta1 = priv->operation.data.chamfer.delta1;
909 len1 = cpml_primitive_length(last);
911 if (delta1 >= len1) {
912 g_warning("Chamfer too big for the last primitive (%lf >= %lf)",
913 delta1, len1);
914 return;
917 delta2 = priv->operation.data.chamfer.delta2;
918 len2 = cpml_primitive_length(current);
920 if (delta2 >= len2) {
921 g_warning("Chamfer too big for the current primitive (%lf >= %lf)",
922 delta2, len2);
923 return;
926 /* Change the end point of the last primitive */
927 cpml_primitive_pair_at(last, &pair, 1. - delta1 / len1);
928 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(last, -1));
930 /* Change the start point of the current primitive */
931 cpml_primitive_pair_at(current, &pair, delta2 / len2);
932 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(current, 0));
934 /* Add the chamfer line */
935 line[0].header.type = CAIRO_PATH_LINE_TO;
936 line[0].header.length = 2;
937 line[1].point.x = pair.x;
938 line[1].point.y = pair.y;
939 priv->path = g_array_append_vals(priv->path, line, 2);
941 priv->operation.operator = ADG_OPERATOR_NONE;