[AdgADim] Corrected arc behavior
[adg.git] / adg / adg-adim.c
blob131e0500f5ed10ee3c80eb93c08a205bc5846631
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-adim
23 * @short_description: Angular dimensions
25 * The #AdgADim entity defines an angular dimension.
28 /**
29 * AdgADim:
31 * All fields are privates and should not be used directly.
32 * Use its public methods instead.
33 **/
36 #include "adg-adim.h"
37 #include "adg-adim-private.h"
38 #include "adg-intl.h"
40 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_adim_parent_class)
41 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_adim_parent_class)
44 enum {
45 PROP_0,
46 PROP_ORG1,
47 PROP_ORG2,
48 PROP_HAS_EXTENSION1,
49 PROP_HAS_EXTENSION2
53 static void dispose (GObject *object);
54 static void get_property (GObject *object,
55 guint param_id,
56 GValue *value,
57 GParamSpec *pspec);
58 static void set_property (GObject *object,
59 guint param_id,
60 const GValue *value,
61 GParamSpec *pspec);
62 static void local_changed (AdgEntity *entity);
63 static void invalidate (AdgEntity *entity);
64 static void arrange (AdgEntity *entity);
65 static void render (AdgEntity *entity,
66 cairo_t *cr);
67 static gchar * default_value (AdgDim *dim);
68 static void update_geometry (AdgADim *adim);
69 static void update_entities (AdgADim *adim);
70 static gboolean set_org1 (AdgADim *adim,
71 const AdgPair *org1);
72 static gboolean set_org2 (AdgADim *adim,
73 const AdgPair *org2);
74 static void dispose_markers (AdgADim *adim);
75 static gboolean get_info (AdgADim *adim,
76 CpmlVector vector[],
77 AdgPair *center,
78 gdouble *distance);
79 static CpmlPath * trail_callback (AdgTrail *trail,
80 gpointer user_data);
83 G_DEFINE_TYPE(AdgADim, adg_adim, ADG_TYPE_DIM);
86 static void
87 adg_adim_class_init(AdgADimClass *klass)
89 GObjectClass *gobject_class;
90 AdgEntityClass *entity_class;
91 AdgDimClass *dim_class;
92 GParamSpec *param;
94 gobject_class = (GObjectClass *) klass;
95 entity_class = (AdgEntityClass *) klass;
96 dim_class = (AdgDimClass *) klass;
98 g_type_class_add_private(klass, sizeof(AdgADimPrivate));
100 gobject_class->dispose = dispose;
101 gobject_class->get_property = get_property;
102 gobject_class->set_property = set_property;
104 entity_class->local_changed = local_changed;
105 entity_class->invalidate = invalidate;
106 entity_class->arrange = arrange;
107 entity_class->render = render;
109 dim_class->default_value = default_value;
111 param = g_param_spec_boxed("org1",
112 P_("First Origin"),
113 P_("Where the first line comes from: this point is used toghether with \"ref1\" to align the first extension line"),
114 ADG_TYPE_PAIR,
115 G_PARAM_READWRITE);
116 g_object_class_install_property(gobject_class, PROP_ORG1, param);
118 param = g_param_spec_boxed("org2",
119 P_("Second Origin"),
120 P_("Where the second line comes from: this point is used toghether with \"ref2\" to align the second extension line"),
121 ADG_TYPE_PAIR,
122 G_PARAM_READWRITE);
123 g_object_class_install_property(gobject_class, PROP_ORG2, param);
126 static void
127 adg_adim_init(AdgADim *adim)
129 AdgADimPrivate *data;
130 cairo_path_data_t move_to, line_to, arc_to;
132 data = G_TYPE_INSTANCE_GET_PRIVATE(adim, ADG_TYPE_ADIM, AdgADimPrivate);
133 move_to.header.type = CAIRO_PATH_MOVE_TO;
134 move_to.header.length = 2;
135 line_to.header.type = CAIRO_PATH_LINE_TO;
136 line_to.header.length = 2;
137 arc_to.header.type = CAIRO_PATH_ARC_TO;
138 arc_to.header.length = 3;
140 data->org1.x = data->org1.y = 0;
141 data->org2.x = data->org2.y = 0;
142 data->has_extension1 = TRUE;
143 data->has_extension2 = TRUE;
145 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
146 data->cpml.path.data = data->cpml.data;
147 data->cpml.path.num_data = G_N_ELEMENTS(data->cpml.data);
148 data->cpml.path.data[0] = move_to;
149 data->cpml.path.data[2] = arc_to;
150 data->cpml.path.data[5] = move_to;
151 data->cpml.path.data[7] = line_to;
152 data->cpml.path.data[9] = move_to;
153 data->cpml.path.data[11] = line_to;
155 data->trail = NULL;
156 data->marker1 = NULL;
157 data->marker2 = NULL;
159 data->geometry_arranged = FALSE;
161 adim->data = data;
164 static void
165 dispose(GObject *object)
167 dispose_markers((AdgADim *) object);
169 if (PARENT_OBJECT_CLASS->dispose != NULL)
170 PARENT_OBJECT_CLASS->dispose(object);
173 static void
174 get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
176 AdgADimPrivate *data = ((AdgADim *) object)->data;
178 switch (prop_id) {
179 case PROP_ORG1:
180 g_value_set_boxed(value, &data->org1);
181 break;
182 case PROP_ORG2:
183 g_value_set_boxed(value, &data->org2);
184 break;
185 case PROP_HAS_EXTENSION1:
186 g_value_set_boolean(value, data->has_extension1);
187 break;
188 case PROP_HAS_EXTENSION2:
189 g_value_set_boolean(value, data->has_extension2);
190 break;
191 default:
192 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
193 break;
197 static void
198 set_property(GObject *object, guint prop_id,
199 const GValue *value, GParamSpec *pspec)
201 AdgADim *adim;
202 AdgADimPrivate *data;
204 adim = (AdgADim *) object;
205 data = adim->data;
207 switch (prop_id) {
208 case PROP_ORG1:
209 set_org1(adim, g_value_get_boxed(value));
210 break;
211 case PROP_ORG2:
212 set_org2(adim, g_value_get_boxed(value));
213 break;
214 case PROP_HAS_EXTENSION1:
215 data->has_extension1 = g_value_get_boolean(value);
216 break;
217 case PROP_HAS_EXTENSION2:
218 data->has_extension2 = g_value_get_boolean(value);
219 break;
220 default:
221 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
222 break;
228 * adg_adim_new:
230 * Creates a new - undefined - angular dimension. You must, at least,
231 * define the reference points with adg_dim_set_ref(), the origins of
232 * the lines ending with the reference points with adg_adim_set_org()
233 * and the reference for positioning the quote with adg_dim_set_pos().
235 * Returns: the newly created angular dimension entity
237 AdgADim *
238 adg_adim_new(void)
240 return g_object_new(ADG_TYPE_ADIM, NULL);
244 * adg_adim_new_full:
245 * @ref1: first reference point
246 * @ref2: second reference point
247 * @org1: first origin point
248 * @org2: second origin point
250 * Creates a new angular dimension, specifing all the needed
251 * properties in one shot.
253 * Returns: the newly created angular dimension entity
255 AdgADim *
256 adg_adim_new_full(const AdgPair *ref1, const AdgPair *ref2,
257 const AdgPair *org1, const AdgPair *org2,
258 const AdgPair *pos)
260 return g_object_new(ADG_TYPE_ADIM, "ref1", ref1, "ref2", ref2,
261 "org1", org1, "org2", org2, "pos", pos, NULL);
265 * adg_adim_new_full_explicit:
266 * @ref1_x: the x coordinate of the first reference point
267 * @ref1_y: the y coordinate of the first reference point
268 * @ref2_x: the x coordinate of the second reference point
269 * @ref2_y: the y coordinate of the second reference point
270 * @direction: angle where to extend the dimension
271 * @pos_x: the x coordinate of the position reference
272 * @pos_y: the y coordinate of the position reference
274 * Wrappes adg_adim_new_full() with explicit values.
276 * Returns: the newly created linear dimension entity
278 AdgADim *
279 adg_adim_new_full_explicit(gdouble ref1_x, gdouble ref1_y,
280 gdouble ref2_x, gdouble ref2_y,
281 gdouble org1_x, gdouble org1_y,
282 gdouble org2_x, gdouble org2_y,
283 gdouble pos_x, gdouble pos_y)
285 AdgPair ref1, ref2, org1, org2, pos;
287 ref1.x = ref1_x;
288 ref1.y = ref1_y;
289 ref2.x = ref2_x;
290 ref2.y = ref2_y;
291 org1.x = org1_x;
292 org1.y = org1_y;
293 org2.x = org2_x;
294 org2.y = org2_y;
295 pos.x = pos_x;
296 pos.y = pos_y;
298 return adg_adim_new_full(&ref1, &ref2, &org1, &org2, &pos);
302 * adg_adim_get_org1:
303 * @adim: an #AdgADim
305 * Gets the first origin of @adim. The returned pair is owned by
306 * @adim and should not be modified or freed.
308 * Returns: a pointer to the internal #AdgPair or %NULL on errors
310 const AdgPair *
311 adg_adim_get_org1(AdgADim *adim)
313 AdgADimPrivate *data;
315 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
317 data = adim->data;
319 return &data->org1;
323 * adg_adim_get_org2:
324 * @adim: an #AdgADim
326 * Gets the second origin of @adim. The returned pair is owned by
327 * @adim and should not be modified or freed.
329 * Returns: a pointer to the internal #AdgPair or %NULL on errors
331 const AdgPair *
332 adg_adim_get_org2(AdgADim *adim)
334 AdgADimPrivate *data;
336 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
338 data = adim->data;
340 return &data->org2;
344 * adg_adim_set_org:
345 * @adim: an #AdgADim
346 * @org1: the first origin
347 * @org2: the second origin
349 * Sets at once the two origins on @adim.
351 void
352 adg_adim_set_org(AdgADim *adim, const AdgPair *org1, const AdgPair *org2)
354 GObject *object;
356 g_return_if_fail(ADG_IS_ADIM(adim));
358 object = (GObject *) adim;
360 g_object_freeze_notify(object);
362 if (set_org1(adim, org1))
363 g_object_notify(object, "org1");
365 if (set_org2(adim, org1))
366 g_object_notify(object, "org2");
368 g_object_thaw_notify(object);
372 static void
373 local_changed(AdgEntity *entity)
375 AdgADimPrivate *data = ((AdgADim *) entity)->data;
377 if (data->trail != NULL)
378 adg_trail_clear_cairo_path(data->trail);
380 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
382 PARENT_ENTITY_CLASS->local_changed(entity);
385 static void
386 invalidate(AdgEntity *entity)
388 AdgADim *adim;
389 AdgADimPrivate *data;
391 adim = (AdgADim *) entity;
392 data = adim->data;
394 dispose_markers(adim);
395 data->geometry_arranged = FALSE;
396 adg_trail_clear_cairo_path(data->trail);
397 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
399 if (PARENT_ENTITY_CLASS->invalidate != NULL)
400 PARENT_ENTITY_CLASS->invalidate(entity);
403 static void
404 arrange(AdgEntity *entity)
406 AdgADim *adim;
407 AdgADimPrivate *data;
408 AdgDim *dim;
409 const AdgMatrix *local;
410 AdgPair ref1, ref2, base1, base12, base2;
411 AdgPair pair;
413 PARENT_ENTITY_CLASS->arrange(entity);
415 adim = (AdgADim *) entity;
416 data = adim->data;
418 update_geometry(adim);
419 update_entities(adim);
421 if (data->cpml.path.status == CAIRO_STATUS_SUCCESS)
422 return;
424 dim = (AdgDim *) adim;
425 local = adg_entity_local_matrix(entity);
427 /* Apply the local matrix to the relevant points */
428 cpml_pair_transform(cpml_pair_copy(&ref1, adg_dim_get_ref1(dim)), local);
429 cpml_pair_transform(cpml_pair_copy(&ref2, adg_dim_get_ref2(dim)), local);
430 cpml_pair_transform(cpml_pair_copy(&base1, &data->point.base1), local);
431 cpml_pair_transform(cpml_pair_copy(&base12, &data->point.base12), local);
432 cpml_pair_transform(cpml_pair_copy(&base2, &data->point.base2), local);
434 /* Combine points and global shifts to build the path */
435 cpml_pair_add(cpml_pair_copy(&pair, &ref1), &data->shift.from1);
436 cpml_pair_to_cairo(&pair, &data->cpml.data[6]);
438 cpml_pair_add(cpml_pair_copy(&pair, &base1), &data->shift.base1);
439 cpml_pair_to_cairo(&pair, &data->cpml.data[1]);
441 cpml_pair_add(&pair, &data->shift.to1);
442 cpml_pair_to_cairo(&pair, &data->cpml.data[8]);
444 cpml_pair_add(cpml_pair_copy(&pair, &base12), &data->shift.base12);
445 cpml_pair_to_cairo(&pair, &data->cpml.data[3]);
447 cpml_pair_add(cpml_pair_copy(&pair, &ref2), &data->shift.from2);
448 cpml_pair_to_cairo(&pair, &data->cpml.data[10]);
450 cpml_pair_add(cpml_pair_copy(&pair, &base2), &data->shift.base2);
451 cpml_pair_to_cairo(&pair, &data->cpml.data[4]);
453 cpml_pair_add(&pair, &data->shift.to2);
454 cpml_pair_to_cairo(&pair, &data->cpml.data[12]);
456 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
458 /* Signal to the markers (if any) that the path has changed */
459 if (data->marker1 != NULL) {
460 adg_marker_set_segment(data->marker1, data->trail, 1);
461 adg_entity_local_changed((AdgEntity *) data->marker1);
464 if (data->marker2 != NULL) {
465 adg_marker_set_segment(data->marker2, data->trail, 1);
466 adg_entity_local_changed((AdgEntity *) data->marker2);
469 /* TODO: compute the extents */
472 static void
473 render(AdgEntity *entity, cairo_t *cr)
475 AdgADim *adim;
476 AdgDim *dim;
477 AdgADimPrivate *data;
478 AdgDimStyle *dim_style;
479 AdgDress dress;
480 const cairo_path_t *cairo_path;
482 adim = (AdgADim *) entity;
483 dim = (AdgDim *) entity;
484 data = adim->data;
485 dim_style = adg_dim_get_dim_style(dim);
487 dress = adg_dim_style_get_color_dress(dim_style);
488 adg_entity_apply_dress(entity, dress, cr);
490 if (data->marker1 != NULL)
491 adg_entity_render((AdgEntity *) data->marker1, cr);
493 if (data->marker2 != NULL)
494 adg_entity_render((AdgEntity *) data->marker2, cr);
496 adg_entity_render((AdgEntity *) adg_dim_get_quote(dim), cr);
498 dress = adg_dim_style_get_line_dress(dim_style);
499 adg_entity_apply_dress(entity, dress, cr);
501 cairo_path = adg_trail_get_cairo_path(data->trail);
502 cairo_append_path(cr, cairo_path);
503 cairo_stroke(cr);
506 static gchar *
507 default_value(AdgDim *dim)
509 AdgADim *adim;
510 AdgADimPrivate *data;
511 AdgDimStyle *dim_style;
512 const gchar *format;
514 adim = (AdgADim *) dim;
515 data = adim->data;
516 dim_style = adg_dim_get_dim_style(dim);
517 format = adg_dim_style_get_number_format(dim_style);
519 update_geometry(adim);
521 return g_strdup_printf(format, data->angle * 180 / M_PI);
524 /* With "geometry" is considered any data (point, vector or angle)
525 * that can be cached: this is strictly related on how the arrange()
526 * method works */
527 static void
528 update_geometry(AdgADim *adim)
530 AdgADimPrivate *data;
531 AdgDimStyle *dim_style;
532 gdouble from_offset, to_offset;
533 gdouble spacing, level;
534 CpmlVector vector[3];
535 CpmlPair center;
536 gdouble distance;
538 data = adim->data;
540 if (data->geometry_arranged)
541 return;
543 if (!get_info(adim, vector, &center, &distance)) {
544 /* Parallel lines: hang with an error message */
545 g_warning("%s: trying to set an angular dimension on parallel lines",
546 G_STRLOC);
547 return;
550 dim_style = adg_dim_get_dim_style((AdgDim *) adim);
551 from_offset = adg_dim_style_get_from_offset(dim_style);
552 to_offset = adg_dim_style_get_to_offset(dim_style);
553 spacing = adg_dim_style_get_baseline_spacing(dim_style);
554 level = adg_dim_get_level((AdgDim *) adim);
556 /* shift.from1 */
557 cpml_vector_set_length(&vector[0], from_offset);
558 cpml_pair_copy(&data->shift.from1, &vector[0]);
560 /* shift.base1 */
561 cpml_vector_set_length(&vector[0], level * spacing);
562 cpml_pair_copy(&data->shift.base1, &vector[0]);
564 /* shift.to1 */
565 cpml_vector_set_length(&vector[0], to_offset);
566 cpml_pair_copy(&data->shift.to1, &vector[0]);
568 /* shift.from2 */
569 cpml_vector_set_length(&vector[2], from_offset);
570 cpml_pair_copy(&data->shift.from2, &vector[2]);
572 /* shift.base2 */
573 cpml_vector_set_length(&vector[2], level * spacing);
574 cpml_pair_copy(&data->shift.base2, &vector[2]);
576 /* shift.to2 */
577 cpml_vector_set_length(&vector[2], to_offset);
578 cpml_pair_copy(&data->shift.to2, &vector[2]);
580 /* shift.base12 */
581 cpml_vector_set_length(&vector[1], level * spacing);
582 cpml_pair_copy(&data->shift.base12, &vector[1]);
584 /* point.base1 */
585 cpml_vector_set_length(&vector[0], distance);
586 cpml_pair_add(cpml_pair_copy(&data->point.base1, &vector[0]), &center);
588 /* point.base2 */
589 cpml_vector_set_length(&vector[2], distance);
590 cpml_pair_add(cpml_pair_copy(&data->point.base2, &vector[2]), &center);
592 /* point.base12 */
593 cpml_vector_set_length(&vector[1], distance);
594 cpml_pair_add(cpml_pair_copy(&data->point.base12, &vector[1]), &center);
596 data->geometry_arranged = TRUE;
599 static void
600 update_entities(AdgADim *adim)
602 AdgADimPrivate *data;
603 AdgDimStyle *dim_style;
605 data = adim->data;
606 dim_style = adg_dim_get_dim_style((AdgDim *) adim);
608 if (data->trail == NULL)
609 data->trail = adg_trail_new(trail_callback, adim);
611 if (data->marker1 == NULL)
612 data->marker1 = adg_dim_style_marker1_new(dim_style);
614 if (data->marker2 == NULL)
615 data->marker2 = adg_dim_style_marker2_new(dim_style);
618 static gboolean
619 set_org1(AdgADim *adim, const AdgPair *org1)
621 AdgADimPrivate *data = adim->data;
623 if (adg_pair_equal(&data->org1, org1))
624 return FALSE;
626 data->org1 = *org1;
628 return TRUE;
631 static gboolean
632 set_org2(AdgADim *adim, const AdgPair *org2)
634 AdgADimPrivate *data = adim->data;
636 if (adg_pair_equal(&data->org2, org2))
637 return FALSE;
639 data->org2 = *org2;
641 return TRUE;
644 static void
645 dispose_markers(AdgADim *adim)
647 AdgADimPrivate *data = adim->data;
649 if (data->trail != NULL) {
650 g_object_unref(data->trail);
651 data->trail = NULL;
654 if (data->marker1 != NULL) {
655 g_object_unref(data->marker1);
656 data->marker1 = NULL;
659 if (data->marker2 != NULL) {
660 g_object_unref(data->marker2);
661 data->marker2 = NULL;
665 static gboolean
666 get_info(AdgADim *adim, CpmlVector vector[],
667 AdgPair *center, gdouble *distance)
669 AdgDim *dim;
670 AdgADimPrivate *data;
671 const AdgPair *ref1, *ref2, *pos;
672 gdouble factor, angle1, angle2;
674 dim = (AdgDim *) adim;
675 data = adim->data;
676 ref1 = adg_dim_get_ref1(dim);
677 ref2 = adg_dim_get_ref2(dim);
679 cpml_pair_sub(cpml_pair_copy(&vector[0], ref1), &data->org1);
680 cpml_pair_sub(cpml_pair_copy(&vector[2], ref2), &data->org2);
682 factor = vector[0].x * vector[2].y - vector[0].y * vector[2].x;
683 if (factor == 0)
684 return FALSE;
686 factor = ((ref1->y - ref2->y) * vector[2].x -
687 (ref1->x - ref2->x) * vector[2].y) / factor;
689 center->x = ref1->x + vector[0].x * factor;
690 center->y = ref1->y + vector[0].y * factor;
692 pos = adg_dim_get_pos(dim);
694 *distance = cpml_pair_distance(center, pos);
696 /* Ensure the distance is non-zero, as this would fuck up
697 * cpml_vector_set_length() */
698 if (*distance == 0)
699 *distance = 0.01;
701 angle1 = cpml_vector_angle(&vector[0]);
702 angle2 = cpml_vector_angle(&vector[2]);
703 while (angle2 < angle1)
704 angle2 += M_PI * 2;
706 cpml_vector_from_angle(&vector[1], (angle1 + angle2) / 2);
708 data->angle = angle2 - angle1;
710 return TRUE;
713 static CpmlPath *
714 trail_callback(AdgTrail *trail, gpointer user_data)
716 AdgADim *adim;
717 AdgADimPrivate *data;
719 adim = (AdgADim *) user_data;
720 data = adim->data;
722 return &data->cpml.path;