[docs] Included AdgColorStyle
[adg.git] / adg / adg-path.c
blob902734f70a5629b5cbf178b53b1bd5c3fd921f33
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 represents a virtual #CpmlPath: this class
26 * implements methods to create the path and provides additional
27 * operations specific to technical drawings.
29 * #AdgPath overrides the get_cpml_path() method of the parent
30 * #AdgTrail class, avoiding the need of an #AdgTrailCallback.
31 * The path is constructed programmaticaly: keep in mind any
32 * method that modifies the path will invalidate the #CpmlPath
33 * returned by adg_trail_get_cpml_path().
35 * Although some of the provided methods are clearly based on the
36 * original cairo path manipulation API, their behavior could be
37 * sligthly different. This is intentional, because the ADG provides
38 * additional path manipulation algorithms, sometime quite complex,
39 * and a more restrictive filter on the path quality is required.
40 * Also, the ADG is designed to be used by technicians while cairo
41 * targets a broader range of developers.
43 * As an example, following the rule of the less surprise, some
44 * cairo functions guess the current point when it is not defined,
45 * while the #AdgPath methods trigger a warning without other effect.
46 * Furthermore, after cairo_path_close_path() a %CAIRO_PATH_MOVE_TO
47 * primitive to the starting point of the segment is automatically
48 * added by cairo; in ADG, after an adg_path_close() the current
49 * point is simply unset.
50 **/
52 /**
53 * AdgPath:
55 * All fields are private and should not be used directly.
56 * Use its public methods instead.
57 **/
60 #include "adg-path.h"
61 #include "adg-path-private.h"
62 #include "adg-primitive.h"
63 #include "adg-intl.h"
65 #include <math.h>
68 static void finalize (GObject *object);
69 static void changed (AdgModel *model);
70 static CpmlPath * get_cpml_path (AdgTrail *trail);
71 static CpmlPath * read_cpml_path (AdgPath *path);
72 static void append_primitive (AdgPath *path,
73 AdgPrimitive *primitive);
74 static gint needed_pairs (CpmlPrimitiveType type,
75 gboolean cp_is_valid);
76 static void clear_operation (AdgPath *path);
77 static gboolean append_operation (AdgPath *path,
78 AdgOperator operator,
79 ...);
80 static void do_operation (AdgPath *path,
81 cairo_path_data_t
82 *path_data);
83 static void do_chamfer (AdgPath *path,
84 CpmlPrimitive *current);
85 static void do_fillet (AdgPath *path,
86 CpmlPrimitive *current);
87 static gboolean is_convex (const CpmlPrimitive
88 *primitive1,
89 const CpmlPrimitive
90 *primitive2);
93 G_DEFINE_TYPE(AdgPath, adg_path, ADG_TYPE_TRAIL);
96 static void
97 adg_path_class_init(AdgPathClass *klass)
99 GObjectClass *gobject_class;
100 AdgModelClass *model_class;
101 AdgTrailClass *trail_class;
103 gobject_class = (GObjectClass *) klass;
104 model_class = (AdgModelClass *) klass;
105 trail_class = (AdgTrailClass *) klass;
107 g_type_class_add_private(klass, sizeof(AdgPathPrivate));
109 gobject_class->finalize = finalize;
111 model_class->changed = changed;
113 trail_class->get_cpml_path = get_cpml_path;
116 static void
117 adg_path_init(AdgPath *path)
119 AdgPathPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(path, ADG_TYPE_PATH,
120 AdgPathPrivate);
122 data->cp_is_valid = FALSE;
123 data->path = g_array_new(FALSE, FALSE, sizeof(cairo_path_data_t));
124 data->operation.operator = ADG_OPERATOR_NONE;
126 path->data = data;
129 static void
130 finalize(GObject *object)
132 AdgPath *path;
133 AdgPathPrivate *data;
134 GObjectClass *object_class;
136 path = (AdgPath *) object;
137 data = path->data;
138 object_class = (GObjectClass *) adg_path_parent_class;
140 g_array_free(data->path, TRUE);
141 clear_operation(path);
143 if (object_class->finalize != NULL)
144 object_class->finalize(object);
149 * adg_path_new:
151 * Creates a new path model. The path should be constructed
152 * programmatically by using the methods provided by #AdgPath.
154 * Returns: the newly created path model
156 AdgPath *
157 adg_path_new(void)
159 return g_object_new(ADG_TYPE_PATH, NULL);
163 * adg_path_get_current_point:
164 * @path: an #AdgPath
165 * @x: where to store the x coordinate of the current point
166 * @y: where to store the y coordinate of the current point
168 * Gets the current point of @path, which is conceptually the
169 * final point reached by the path so far.
171 * If there is no defined current point, @x and @y will both be set
172 * to 0 and a warning will be triggered. It is possible to check this
173 * in advance with adg_path_has_current_point().
175 * Most #AdgPath methods alter the current point and most of them
176 * expect a current point to be defined otherwise will fail triggering
177 * a warning. Check the description of every method for specific details.
179 void
180 adg_path_get_current_point(AdgPath *path, gdouble *x, gdouble *y)
182 AdgPathPrivate *data;
184 g_return_if_fail(ADG_IS_PATH(path));
186 data = path->data;
188 if (data->cp_is_valid) {
189 *x = data->cp.x;
190 *y = data->cp.y;
191 } else {
192 *x = *y = 0.;
193 g_return_if_reached();
198 * adg_path_has_current_point:
199 * @path: an #AdgPath
201 * Returns whether a current point is defined on @path.
202 * See adg_path_get_current_point() for details on the current point.
204 * Returns: whether a current point is defined
206 gboolean
207 adg_path_has_current_point(AdgPath *path)
209 AdgPathPrivate *data;
211 g_return_val_if_fail(ADG_IS_PATH(path), FALSE);
213 data = path->data;
215 return data->cp_is_valid;
219 * adg_path_clear:
220 * @path: an #AdgPath
222 * Releases the internal memory hold by @path and resets its status,
223 * so that after this call @path contains an empty path.
225 void
226 adg_path_clear(AdgPath *path)
228 AdgPathPrivate *data;
230 g_return_if_fail(ADG_IS_PATH(path));
232 data = path->data;
234 g_array_set_size(data->path, 0);
235 clear_operation(path);
236 adg_trail_clear_cairo_path((AdgTrail *) path);
241 * adg_path_append:
242 * @path: an #AdgPath
243 * @type: a #cairo_data_type_t value
244 * @...: point data, specified as #AdgPair pointers
246 * Generic method to append a primitive to @path. The number of #AdgPair
247 * structs depends on @type: there is no way with this function to
248 * reserve more cairo_path_data_t structs than what is needed by the
249 * primitive.
251 * This function accepts also the special %CAIRO_PATH_ARC_TO primitive.
253 * If @path has no current point while the requested primitive needs it,
254 * a warning message will be triggered without other effect.
256 void
257 adg_path_append(AdgPath *path, CpmlPrimitiveType type, ...)
259 va_list var_args;
261 va_start(var_args, type);
262 adg_path_append_valist(path, type, var_args);
263 va_end(var_args);
267 * adg_path_append_valist:
268 * @path: an #AdgPath
269 * @type: a #cairo_data_type_t value
270 * @var_args: point data, specified as #AdgPair pointers
272 * va_list version of adg_path_append().
274 void
275 adg_path_append_valist(AdgPath *path, CpmlPrimitiveType type, va_list var_args)
277 AdgPathPrivate *data;
278 AdgPrimitive primitive;
279 gint length, cnt;
280 cairo_path_data_t org;
281 cairo_path_data_t *path_data;
283 g_return_if_fail(ADG_IS_PATH(path));
285 data = path->data;
286 length = needed_pairs(type, data->cp_is_valid);
287 if (length == 0)
288 return;
290 /* Set a copy of the current point as the primitive origin */
291 cpml_pair_to_cairo(&data->cp, &org);
292 primitive.org = &org;
294 /* Build the cairo_path_data_t array */
295 primitive.data = path_data = g_new(cairo_path_data_t, length);
297 path_data->header.type = type;
298 path_data->header.length = length;
300 for (cnt = 1; cnt < length; ++ cnt) {
301 ++ path_data;
302 cpml_pair_to_cairo(va_arg(var_args, AdgPair *), path_data);
305 /* Terminate the creation of the temporary primitive */
306 primitive.segment = NULL;
308 /* Append this primitive to @path */
309 append_primitive(path, &primitive);
311 g_free(primitive.data);
315 * adg_path_append_primitive:
316 * @path: an #AdgPath
317 * @primitive: the #AdgPrimitive to append
319 * Appends @primitive to @path. The primitive to add is considered the
320 * continuation of the current path so the <structfield>org</structfield>
321 * component of @primitive is not used. Anyway the current poins is
322 * checked against it: they must be equal or the function will fail
323 * without further processing.
325 void
326 adg_path_append_primitive(AdgPath *path, const AdgPrimitive *primitive)
328 AdgPathPrivate *data;
329 AdgPrimitive *primitive_dup;
331 g_return_if_fail(ADG_IS_PATH(path));
332 g_return_if_fail(primitive != NULL);
334 data = path->data;
336 g_return_if_fail(primitive->org->point.x == data->cp.x &&
337 primitive->org->point.y == data->cp.y);
339 /* The primitive data could be modified by pending operations:
340 * work on a copy */
341 primitive_dup = adg_primitive_deep_dup(primitive);
343 append_primitive(path, primitive_dup);
345 g_free(primitive_dup);
349 * adg_path_append_segment:
350 * @path: an #AdgPath
351 * @segment: the #AdgSegment to append
353 * Appends @segment to @path.
355 void
356 adg_path_append_segment(AdgPath *path, const AdgSegment *segment)
358 AdgPathPrivate *data;
360 g_return_if_fail(ADG_IS_PATH(path));
361 g_return_if_fail(segment != NULL);
363 data = path->data;
365 adg_trail_clear_cairo_path((AdgTrail *) path);
366 data->path = g_array_append_vals(data->path,
367 segment->data, segment->num_data);
371 * adg_path_append_cairo_path:
372 * @path: an #AdgPath
373 * @cairo_path: the #cairo_path_t path to append
375 * Appends a whole cairo path to @path.
377 void
378 adg_path_append_cairo_path(AdgPath *path, const cairo_path_t *cairo_path)
380 AdgPathPrivate *data;
382 g_return_if_fail(ADG_IS_PATH(path));
384 data = path->data;
386 adg_trail_clear_cairo_path((AdgTrail *) path);
387 data->path = g_array_append_vals(data->path,
388 cairo_path->data, cairo_path->num_data);
392 * adg_path_move_to:
393 * @path: an #AdgPath
394 * @x: the new x coordinate
395 * @y: the new y coordinate
397 * Begins a new segment. After this call the current point will be (@x, @y).
399 void
400 adg_path_move_to(AdgPath *path, gdouble x, gdouble y)
402 AdgPair p;
404 p.x = x;
405 p.y = y;
407 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p);
411 * adg_path_line_to:
412 * @path: an #AdgPath
413 * @x: the new x coordinate
414 * @y: the new y coordinate
416 * Adds a line to @path from the current point to position (@x, @y).
417 * After this call the current point will be (@x, @y).
419 * If @path has no current point before this call, this function will
420 * trigger a warning without other effect.
422 void
423 adg_path_line_to(AdgPath *path, gdouble x, gdouble y)
425 AdgPair p;
427 p.x = x;
428 p.y = y;
430 adg_path_append(path, CAIRO_PATH_LINE_TO, &p);
434 * adg_path_arc_to:
435 * @path: an #AdgPath
436 * @x1: the x coordinate of an intermediate point
437 * @y1: the y coordinate of an intermediate point
438 * @x2: the x coordinate of the end of the arc
439 * @y2: the y coordinate of the end of the arc
441 * Adds an arc to the path from the current point to (@x2, @y2),
442 * passing throught (@x1, @y1). After this call the current point
443 * will be (@x2, @y2).
445 * If @path has no current point before this call, this function will
446 * trigger a warning without other effect.
448 void
449 adg_path_arc_to(AdgPath *path, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
451 AdgPair p[2];
453 p[0].x = x1;
454 p[0].y = y1;
455 p[1].x = x2;
456 p[1].y = y2;
458 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[0], &p[1]);
462 * adg_path_curve_to:
463 * @path: an #AdgPath
464 * @x1: the x coordinate of the first control point
465 * @y1: the y coordinate of the first control point
466 * @x2: the x coordinate of the second control point
467 * @y2: the y coordinate of the second control point
468 * @x3: the x coordinate of the end of the curve
469 * @y3: the y coordinate of the end of the curve
471 * Adds a cubic Bézier curve to the path from the current point to
472 * position (@x3, @y3), using (@x1, @y1) and (@x2, @y2) as the
473 * control points. After this call the current point will be (@x3, @y3).
475 * If @path has no current point before this call, this function will
476 * trigger a warning without other effect.
478 void
479 adg_path_curve_to(AdgPath *path, gdouble x1, gdouble y1,
480 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
482 AdgPair p[3];
484 p[0].x = x1;
485 p[0].y = y1;
486 p[1].x = x2;
487 p[1].y = y2;
488 p[2].x = x3;
489 p[2].y = y3;
491 adg_path_append(path, CAIRO_PATH_CURVE_TO, &p[0], &p[1], &p[2]);
495 * adg_path_close:
496 * @path: an #AdgPath
498 * Adds a line segment to the path from the current point to the
499 * beginning of the current segment, (the most recent point passed
500 * to an adg_path_move_to()), and closes this segment.
501 * After this call the current point will be unset.
503 * The behavior of adg_path_close() is distinct from simply calling
504 * adg_line_to() with the coordinates of the segment starting point.
505 * When a closed segment is stroked, there are no caps on the ends.
506 * Instead, there is a line join connecting the final and initial
507 * primitive of the segment.
509 * If @path has no current point before this call, this function will
510 * trigger a warning without other effect.
512 void
513 adg_path_close(AdgPath *path)
515 adg_path_append(path, CAIRO_PATH_CLOSE_PATH);
519 * adg_path_arc
520 * @path: an #AdgPath
521 * @xc: x position of the center of the arc
522 * @yc: y position of the center of the arc
523 * @r: the radius of the arc
524 * @start: the start angle, in radians
525 * @end: the end angle, in radians
527 * A more usual way to add an arc to @path. After this call, the current
528 * point will be the computed end point of the arc. The arc will be
529 * rendered in increasing angle, accordling to @start and @end. This means
530 * if @start is less than @end, the arc will be rendered in clockwise
531 * direction (accordling to the default cairo coordinate system) while if
532 * @start is greather than @end, the arc will be rendered in couterclockwise
533 * direction.
535 * By explicitely setting the whole arc data, the start point could be
536 * different from the current point. In this case, if @path has no
537 * current point before the call a %CAIRO_PATH_MOVE_TO to the start
538 * point of the arc will be automatically prepended to the arc.
539 * If @path has a current point, a %CAIRO_PATH_LINE_TO to the start
540 * point of the arc will be used instead of the moveto.
542 void
543 adg_path_arc(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
544 gdouble start, gdouble end)
546 AdgPathPrivate *data;
547 AdgPair center, p[3];
549 g_return_if_fail(ADG_IS_PATH(path));
551 data = path->data;
552 center.x = xc;
553 center.y = yc;
555 cpml_vector_from_angle(&p[0], start, r);
556 cpml_vector_from_angle(&p[1], (end-start) / 2, r);
557 cpml_vector_from_angle(&p[2], end, r);
559 cpml_pair_add(&p[0], &center);
560 cpml_pair_add(&p[1], &center);
561 cpml_pair_add(&p[2], &center);
563 if (!data->cp_is_valid)
564 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p[0]);
565 else if (p[0].x != data->cp.x || p[0].y != data->cp.y)
566 adg_path_append(path, CAIRO_PATH_LINE_TO, &p[0]);
568 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[1], &p[2]);
572 * adg_path_chamfer
573 * @path: an #AdgPath
574 * @delta1: the distance from the intersection point of the current primitive
575 * @delta2: the distance from the intersection point of the next primitive
577 * A binary operator that generates a chamfer between two primitives.
578 * The first primitive involved is the current primitive, the second will
579 * be the next primitive appended to @path after this call. The second
580 * primitive is required: if the chamfer operation is not properly
581 * terminated (by not providing the second primitive), any API accessing
582 * the path in reading mode will raise a warning.
584 * The chamfer operation requires two lengths: @delta1 specifies the
585 * "quantity" to trim on the first primitive while @delta2 is the same
586 * applied on the second primitive. The term "quantity" means the length
587 * of the portion to cut out from the original primitive (that is the
588 * primitive as would be without the chamfer).
590 void
591 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
593 g_return_if_fail(ADG_IS_PATH(path));
595 if (!append_operation(path, ADG_OPERATOR_CHAMFER, delta1, delta2))
596 return;
600 * adg_path_fillet:
601 * @path: an #AdgPath
602 * @radius: the radius of the fillet
605 * A binary operator that joins to primitives with an arc.
606 * The first primitive involved is the current primitive, the second will
607 * be the next primitive appended to @path after this call. The second
608 * primitive is required: if the fillet operation is not properly
609 * terminated (by not providing the second primitive), any API accessing
610 * the path in reading mode will raise a warning.
612 void
613 adg_path_fillet(AdgPath *path, gdouble radius)
615 g_return_if_fail(ADG_IS_PATH(path));
617 if (!append_operation(path, ADG_OPERATOR_FILLET, radius))
618 return;
622 static void
623 changed(AdgModel *model)
625 AdgModelClass *model_class = (AdgModelClass *) adg_path_parent_class;
627 adg_path_clear((AdgPath *) model);
629 if (model_class->changed != NULL)
630 model_class->changed(model);
633 static CpmlPath *
634 get_cpml_path(AdgTrail *trail)
636 adg_trail_clear_cairo_path(trail);
638 return read_cpml_path((AdgPath *) trail);
641 static CpmlPath *
642 read_cpml_path(AdgPath *path)
644 AdgPathPrivate *data;
645 CpmlPath *cpml_path;
647 data = path->data;
648 cpml_path = &data->cpml_path;
650 cpml_path->status = CAIRO_STATUS_SUCCESS;
651 cpml_path->data = (cairo_path_data_t *) data->path->data;
652 cpml_path->num_data = data->path->len;
654 return cpml_path;
657 static void
658 append_primitive(AdgPath *path, AdgPrimitive *current)
660 AdgPathPrivate *data;
661 cairo_path_data_t *path_data;
662 int length;
664 data = path->data;
665 path_data = current->data;
666 length = path_data[0].header.length;
668 /* Execute any pending operation */
669 do_operation(path, path_data);
671 /* Append the path data to the internal path array */
672 data->path = g_array_append_vals(data->path, path_data, length);
674 /* Set path data to point to the recently appended cairo_path_data_t
675 * primitive: the first struct is the header */
676 path_data = (cairo_path_data_t *) data->path->data +
677 data->path->len - length;
679 /* Set the last primitive for subsequent binary operations */
680 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
681 data->last.segment = NULL;
682 data->last.data = path_data;
684 /* Save the last point as the current point, if applicable */
685 data->cp_is_valid = length > 1;
686 if (length > 1)
687 cpml_pair_from_cairo(&data->cp, &path_data[length-1]);
689 /* Invalidate cairo_path: should be recomputed */
690 adg_trail_clear_cairo_path((AdgTrail *) path);
693 static gint
694 needed_pairs(CpmlPrimitiveType type, gboolean cp_is_valid)
696 switch (type) {
698 case CAIRO_PATH_CLOSE_PATH:
699 g_return_val_if_fail(cp_is_valid, 0);
700 return 1;
702 case CAIRO_PATH_MOVE_TO:
703 return 2;
705 case CAIRO_PATH_LINE_TO:
706 g_return_val_if_fail(cp_is_valid, 0);
707 return 2;
709 case CAIRO_PATH_ARC_TO:
710 g_return_val_if_fail(cp_is_valid, 0);
711 return 3;
713 case CAIRO_PATH_CURVE_TO:
714 g_return_val_if_fail(cp_is_valid, 0);
715 return 4;
717 default:
718 g_return_val_if_reached(0);
721 return 0;
724 static void
725 clear_operation(AdgPath *path)
727 AdgPathPrivate *data;
728 AdgOperation *operation;
730 data = path->data;
731 operation = &data->operation;
733 if (operation->operator == ADG_OPERATOR_NONE)
734 return;
736 g_warning("An operation is still active while clearing the path "
737 "(operator `%d')", operation->operator);
738 operation->operator = ADG_OPERATOR_NONE;
741 static gboolean
742 append_operation(AdgPath *path, AdgOperator operator, ...)
744 AdgPathPrivate *data;
745 AdgOperation *operation;
746 va_list var_args;
748 data = path->data;
750 if (!data->cp_is_valid) {
751 g_warning("Operation requested but path has no current primitive "
752 "(operator `%d')", operator);
753 return FALSE;
756 operation = &data->operation;
757 if (operation->operator != ADG_OPERATOR_NONE) {
758 /* TODO: this is a rude semplification, as a lot of operators can
759 * and may cohexist. As an example, a fillet followed by a
760 * polar chamfer is not difficult to compute */
761 g_warning("Operation requested but another operation is yet active"
762 "(operators: new `%d', old `%d')",
763 operator, operation->operator);
764 return FALSE;
767 va_start(var_args, operator);
769 switch (operator) {
771 case ADG_OPERATOR_CHAMFER:
772 operation->data.chamfer.delta1 = va_arg(var_args, double);
773 operation->data.chamfer.delta2 = va_arg(var_args, double);
774 break;
776 case ADG_OPERATOR_FILLET:
777 operation->data.fillet.radius = va_arg(var_args, double);
778 break;
780 case ADG_OPERATOR_NONE:
781 va_end(var_args);
782 return TRUE;
784 default:
785 g_warning("Operation not recognized (operator `%d')", operator);
786 va_end(var_args);
787 return FALSE;
790 operation->operator = operator;
791 va_end(var_args);
793 return TRUE;
796 static void
797 do_operation(AdgPath *path, cairo_path_data_t *path_data)
799 AdgPathPrivate *data;
800 AdgOperator operator;
801 CpmlSegment segment;
802 CpmlPrimitive current;
803 cairo_path_data_t current_org;
805 data = path->data;
806 operator = data->operation.operator;
807 cpml_segment_from_cairo(&segment, read_cpml_path(path));
809 /* Construct the current primitive, that is the primitive to be inserted.
810 * Its org is a copy of the end point of the last primitive: it can be
811 * modified without affecting anything else. It is expected the operation
812 * functions will add to @path the primitives required but NOT to add
813 * @current, as this one will be inserted automatically. */
814 current.segment = &segment;
815 current.org = &current_org;
816 current.data = path_data;
817 cpml_pair_to_cairo(&data->cp, &current_org);
819 switch (operator) {
821 case ADG_OPERATOR_NONE:
822 return;
824 case ADG_OPERATOR_CHAMFER:
825 do_chamfer(path, &current);
826 break;
828 case ADG_OPERATOR_FILLET:
829 do_fillet(path, &current);
830 break;
832 default:
833 g_warning("Operation not implemented (operator `%d')", operator);
834 return;
838 static void
839 do_chamfer(AdgPath *path, CpmlPrimitive *current)
841 AdgPathPrivate *data;
842 CpmlPrimitive *last;
843 gdouble delta1, delta2;
844 gdouble len1, len2;
845 AdgPair pair;
846 cairo_path_data_t line[2];
848 data = path->data;
849 last = &data->last;
850 delta1 = data->operation.data.chamfer.delta1;
851 len1 = cpml_primitive_length(last);
853 if (delta1 >= len1) {
854 g_warning("Chamfer too big for the last primitive (%lf >= %lf)",
855 delta1, len1);
856 return;
859 delta2 = data->operation.data.chamfer.delta2;
860 len2 = cpml_primitive_length(current);
862 if (delta2 >= len2) {
863 g_warning("Chamfer too big for the current primitive (%lf >= %lf)",
864 delta2, len2);
865 return;
868 /* Change the end point of the last primitive */
869 cpml_primitive_pair_at(last, &pair, 1. - delta1 / len1);
870 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(last, -1));
872 /* Change the start point of the current primitive */
873 cpml_primitive_pair_at(current, &pair, delta2 / len2);
874 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(current, 0));
876 /* Add the chamfer line */
877 line[0].header.type = CAIRO_PATH_LINE_TO;
878 line[0].header.length = 2;
879 line[1].point.x = pair.x;
880 line[1].point.y = pair.y;
881 data->path = g_array_append_vals(data->path, line, 2);
883 data->operation.operator = ADG_OPERATOR_NONE;
886 static void
887 do_fillet(AdgPath *path, CpmlPrimitive *current)
889 AdgPathPrivate *data;
890 CpmlPrimitive *last, *current_dup, *last_dup;
891 gdouble radius, offset, pos;
892 AdgPair center, vector, p[3];
893 cairo_path_data_t arc[3];
895 data = path->data;
896 last = &data->last;
897 current_dup = adg_primitive_deep_dup(current);
899 /* Force current_dup to point to the original segment so a
900 * CAIRO_PATH_CLOSE_PATH primitive will work as expected */
901 current_dup->segment = current->segment;
903 last_dup = adg_primitive_deep_dup(last);
904 radius = data->operation.data.fillet.radius;
905 offset = is_convex(last_dup, current_dup) ? -radius : radius;
907 /* Find the center of the fillet from the intersection between
908 * the last and current primitives offseted by radius */
909 cpml_primitive_offset(current_dup, offset);
910 cpml_primitive_offset(last_dup, offset);
911 if (cpml_primitive_intersection(current_dup, last_dup,
912 &center, 1) == 0) {
913 g_warning("Fillet not applicable (radius = %lf)", radius);
914 g_free(current_dup);
915 g_free(last_dup);
916 return;
919 /* Compute the start point of the fillet */
920 pos = cpml_primitive_near_pos(last_dup, &center);
921 cpml_primitive_vector_at(last_dup, &vector, pos);
922 cpml_vector_set_length(&vector, offset);
923 cpml_vector_normal(&vector);
924 cpml_pair_sub(cpml_pair_copy(&p[0], &center), &vector);
926 /* Compute the mid point of the fillet */
927 cpml_pair_from_cairo(&vector, current->org);
928 cpml_pair_sub(&vector, &center);
929 cpml_vector_set_length(&vector, radius);
930 cpml_pair_add(cpml_pair_copy(&p[1], &center), &vector);
932 /* Compute the end point of the fillet */
933 pos = cpml_primitive_near_pos(current_dup, &center);
934 cpml_primitive_vector_at(current_dup, &vector, pos);
935 cpml_vector_set_length(&vector, offset);
936 cpml_vector_normal(&vector);
937 cpml_pair_sub(cpml_pair_copy(&p[2], &center), &vector);
939 g_free(current_dup);
940 g_free(last_dup);
942 /* Modify the end point of the last primitive */
943 cpml_pair_to_cairo(&p[0], cpml_primitive_get_point(last, -1));
945 /* Add the fillet arc */
946 arc[0].header.type = CAIRO_PATH_ARC_TO;
947 arc[0].header.length = 3;
948 cpml_pair_to_cairo(&p[1], &arc[1]);
949 cpml_pair_to_cairo(&p[2], &arc[2]);
950 data->path = g_array_append_vals(data->path, arc, 3);
952 data->operation.operator = ADG_OPERATOR_NONE;
955 static gboolean
956 is_convex(const CpmlPrimitive *primitive1, const CpmlPrimitive *primitive2)
958 CpmlVector v1, v2;
959 gdouble angle1, angle2;
961 cpml_primitive_vector_at(primitive1, &v1, -1);
962 cpml_primitive_vector_at(primitive2, &v2, 0);
964 /* Probably there is a smarter way to get this without trygonometry */
965 angle1 = cpml_vector_angle(&v1);
966 angle2 = cpml_vector_angle(&v2);
968 if (angle1 > angle2)
969 angle1 -= M_PI*2;
971 return angle2-angle1 > M_PI;