[AdgDim] Dress name mangling and code cleanup
[adg.git] / adg / adg-adim.c
blobce220d5e3a8ad080f0afb942c4542a6de13f3f1e
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-dim-private.h"
39 #include "adg-intl.h"
41 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_adim_parent_class)
42 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_adim_parent_class)
45 enum {
46 PROP_0,
47 PROP_ORG1,
48 PROP_ORG2,
49 PROP_HAS_EXTENSION1,
50 PROP_HAS_EXTENSION2
54 static void dispose (GObject *object);
55 static void get_property (GObject *object,
56 guint param_id,
57 GValue *value,
58 GParamSpec *pspec);
59 static void set_property (GObject *object,
60 guint param_id,
61 const GValue *value,
62 GParamSpec *pspec);
63 static void local_changed (AdgEntity *entity);
64 static void invalidate (AdgEntity *entity);
65 static void arrange (AdgEntity *entity);
66 static void render (AdgEntity *entity,
67 cairo_t *cr);
68 static gchar * default_value (AdgDim *dim);
69 static void update_geometry (AdgADim *adim);
70 static void update_entities (AdgADim *adim);
71 static gboolean set_org1 (AdgADim *adim,
72 const AdgPair *org1);
73 static gboolean set_org2 (AdgADim *adim,
74 const AdgPair *org2);
75 static void unset_trail (AdgADim *adim);
76 static void dispose_markers (AdgADim *adim);
77 static gboolean get_info (AdgADim *adim,
78 CpmlVector vector[],
79 AdgPair *center,
80 gdouble *distance);
81 static CpmlPath * trail_callback (AdgTrail *trail,
82 gpointer user_data);
85 G_DEFINE_TYPE(AdgADim, adg_adim, ADG_TYPE_DIM);
88 static void
89 adg_adim_class_init(AdgADimClass *klass)
91 GObjectClass *gobject_class;
92 AdgEntityClass *entity_class;
93 AdgDimClass *dim_class;
94 GParamSpec *param;
96 gobject_class = (GObjectClass *) klass;
97 entity_class = (AdgEntityClass *) klass;
98 dim_class = (AdgDimClass *) klass;
100 g_type_class_add_private(klass, sizeof(AdgADimPrivate));
102 gobject_class->dispose = dispose;
103 gobject_class->get_property = get_property;
104 gobject_class->set_property = set_property;
106 entity_class->local_changed = local_changed;
107 entity_class->invalidate = invalidate;
108 entity_class->arrange = arrange;
109 entity_class->render = render;
111 dim_class->default_value = default_value;
113 param = g_param_spec_boxed("org1",
114 P_("First Origin"),
115 P_("Where the first line comes from: this point is used toghether with \"ref1\" to align the first extension line"),
116 ADG_TYPE_PAIR,
117 G_PARAM_READWRITE);
118 g_object_class_install_property(gobject_class, PROP_ORG1, param);
120 param = g_param_spec_boxed("org2",
121 P_("Second Origin"),
122 P_("Where the second line comes from: this point is used toghether with \"ref2\" to align the second extension line"),
123 ADG_TYPE_PAIR,
124 G_PARAM_READWRITE);
125 g_object_class_install_property(gobject_class, PROP_ORG2, param);
128 static void
129 adg_adim_init(AdgADim *adim)
131 AdgADimPrivate *data;
132 cairo_path_data_t move_to, line_to, arc_to;
134 data = G_TYPE_INSTANCE_GET_PRIVATE(adim, ADG_TYPE_ADIM, AdgADimPrivate);
135 move_to.header.type = CAIRO_PATH_MOVE_TO;
136 move_to.header.length = 2;
137 line_to.header.type = CAIRO_PATH_LINE_TO;
138 line_to.header.length = 2;
139 arc_to.header.type = CAIRO_PATH_ARC_TO;
140 arc_to.header.length = 3;
142 data->org1.x = data->org1.y = 0;
143 data->org2.x = data->org2.y = 0;
144 data->has_extension1 = TRUE;
145 data->has_extension2 = TRUE;
147 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
148 data->cpml.path.data = data->cpml.data;
149 data->cpml.path.num_data = G_N_ELEMENTS(data->cpml.data);
150 data->cpml.path.data[0] = move_to;
151 data->cpml.path.data[2] = arc_to;
152 data->cpml.path.data[5] = move_to;
153 data->cpml.path.data[7] = line_to;
154 data->cpml.path.data[9] = move_to;
155 data->cpml.path.data[11] = line_to;
157 data->trail = NULL;
158 data->marker1 = NULL;
159 data->marker2 = NULL;
161 data->geometry_arranged = FALSE;
163 adim->data = data;
166 static void
167 dispose(GObject *object)
169 dispose_markers((AdgADim *) object);
171 if (PARENT_OBJECT_CLASS->dispose != NULL)
172 PARENT_OBJECT_CLASS->dispose(object);
175 static void
176 get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
178 AdgADimPrivate *data = ((AdgADim *) object)->data;
180 switch (prop_id) {
181 case PROP_ORG1:
182 g_value_set_boxed(value, &data->org1);
183 break;
184 case PROP_ORG2:
185 g_value_set_boxed(value, &data->org2);
186 break;
187 case PROP_HAS_EXTENSION1:
188 g_value_set_boolean(value, data->has_extension1);
189 break;
190 case PROP_HAS_EXTENSION2:
191 g_value_set_boolean(value, data->has_extension2);
192 break;
193 default:
194 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
195 break;
199 static void
200 set_property(GObject *object, guint prop_id,
201 const GValue *value, GParamSpec *pspec)
203 AdgADim *adim;
204 AdgADimPrivate *data;
206 adim = (AdgADim *) object;
207 data = adim->data;
209 switch (prop_id) {
210 case PROP_ORG1:
211 set_org1(adim, g_value_get_boxed(value));
212 break;
213 case PROP_ORG2:
214 set_org2(adim, g_value_get_boxed(value));
215 break;
216 case PROP_HAS_EXTENSION1:
217 data->has_extension1 = g_value_get_boolean(value);
218 break;
219 case PROP_HAS_EXTENSION2:
220 data->has_extension2 = g_value_get_boolean(value);
221 break;
222 default:
223 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
224 break;
230 * adg_adim_new:
232 * Creates a new - undefined - angular dimension. You must, at least,
233 * define the reference points with adg_dim_set_ref(), the origins of
234 * the lines ending with the reference points with adg_adim_set_org()
235 * and the reference for positioning the quote with adg_dim_set_pos().
237 * Returns: the newly created angular dimension entity
239 AdgADim *
240 adg_adim_new(void)
242 return g_object_new(ADG_TYPE_ADIM, NULL);
246 * adg_adim_new_full:
247 * @ref1: first reference point
248 * @ref2: second reference point
249 * @org1: first origin point
250 * @org2: second origin point
252 * Creates a new angular dimension, specifing all the needed
253 * properties in one shot.
255 * Returns: the newly created angular dimension entity
257 AdgADim *
258 adg_adim_new_full(const AdgPair *ref1, const AdgPair *ref2,
259 const AdgPair *org1, const AdgPair *org2,
260 const AdgPair *pos)
262 return g_object_new(ADG_TYPE_ADIM, "ref1", ref1, "ref2", ref2,
263 "org1", org1, "org2", org2, "pos", pos, NULL);
267 * adg_adim_new_full_explicit:
268 * @ref1_x: the x coordinate of the first reference point
269 * @ref1_y: the y coordinate of the first reference point
270 * @ref2_x: the x coordinate of the second reference point
271 * @ref2_y: the y coordinate of the second reference point
272 * @direction: angle where to extend the dimension
273 * @pos_x: the x coordinate of the position reference
274 * @pos_y: the y coordinate of the position reference
276 * Wrappes adg_adim_new_full() with explicit values.
278 * Returns: the newly created linear dimension entity
280 AdgADim *
281 adg_adim_new_full_explicit(gdouble ref1_x, gdouble ref1_y,
282 gdouble ref2_x, gdouble ref2_y,
283 gdouble org1_x, gdouble org1_y,
284 gdouble org2_x, gdouble org2_y,
285 gdouble pos_x, gdouble pos_y)
287 AdgPair ref1, ref2, org1, org2, pos;
289 ref1.x = ref1_x;
290 ref1.y = ref1_y;
291 ref2.x = ref2_x;
292 ref2.y = ref2_y;
293 org1.x = org1_x;
294 org1.y = org1_y;
295 org2.x = org2_x;
296 org2.y = org2_y;
297 pos.x = pos_x;
298 pos.y = pos_y;
300 return adg_adim_new_full(&ref1, &ref2, &org1, &org2, &pos);
304 * adg_adim_get_org1:
305 * @adim: an #AdgADim
307 * Gets the first origin of @adim. The returned pair is owned by
308 * @adim and should not be modified or freed.
310 * Returns: a pointer to the internal #AdgPair or %NULL on errors
312 const AdgPair *
313 adg_adim_get_org1(AdgADim *adim)
315 AdgADimPrivate *data;
317 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
319 data = adim->data;
321 return &data->org1;
325 * adg_adim_get_org2:
326 * @adim: an #AdgADim
328 * Gets the second origin of @adim. The returned pair is owned by
329 * @adim and should not be modified or freed.
331 * Returns: a pointer to the internal #AdgPair or %NULL on errors
333 const AdgPair *
334 adg_adim_get_org2(AdgADim *adim)
336 AdgADimPrivate *data;
338 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
340 data = adim->data;
342 return &data->org2;
346 * adg_adim_set_org:
347 * @adim: an #AdgADim
348 * @org1: the first origin
349 * @org2: the second origin
351 * Sets at once the two origins on @adim.
353 void
354 adg_adim_set_org(AdgADim *adim, const AdgPair *org1, const AdgPair *org2)
356 GObject *object;
358 g_return_if_fail(ADG_IS_ADIM(adim));
360 object = (GObject *) adim;
362 g_object_freeze_notify(object);
364 if (set_org1(adim, org1))
365 g_object_notify(object, "org1");
367 if (set_org2(adim, org1))
368 g_object_notify(object, "org2");
370 g_object_thaw_notify(object);
374 static void
375 local_changed(AdgEntity *entity)
377 unset_trail((AdgADim *) entity);
379 PARENT_ENTITY_CLASS->local_changed(entity);
382 static void
383 invalidate(AdgEntity *entity)
385 AdgADim *adim;
386 AdgADimPrivate *data;
388 adim = (AdgADim *) entity;
389 data = adim->data;
391 dispose_markers(adim);
392 data->geometry_arranged = FALSE;
393 unset_trail(adim);
395 if (PARENT_ENTITY_CLASS->invalidate != NULL)
396 PARENT_ENTITY_CLASS->invalidate(entity);
399 static void
400 arrange(AdgEntity *entity)
402 AdgADim *adim;
403 AdgDim *dim;
404 AdgADimPrivate *data;
405 AdgContainer *quote;
406 const AdgMatrix *local;
407 AdgPair ref1, ref2, base1, base12, base2;
408 AdgPair pair;
410 PARENT_ENTITY_CLASS->arrange(entity);
412 adim = (AdgADim *) entity;
413 dim = (AdgDim *) adim;
414 data = adim->data;
415 quote = adg_dim_get_quote(dim);
417 update_geometry(adim);
418 update_entities(adim);
420 if (data->cpml.path.status == CAIRO_STATUS_SUCCESS) {
421 AdgEntity *quote_entity = (AdgEntity *) quote;
422 adg_entity_set_global_map(quote_entity, &data->quote.global_map);
423 adg_entity_set_local_map(quote_entity, &data->quote.local_map);
424 return;
427 local = adg_entity_local_matrix(entity);
429 /* Apply the local matrix to the relevant points */
430 cpml_pair_transform(cpml_pair_copy(&ref1, adg_dim_get_ref1(dim)), local);
431 cpml_pair_transform(cpml_pair_copy(&ref2, adg_dim_get_ref2(dim)), local);
432 cpml_pair_transform(cpml_pair_copy(&base1, &data->point.base1), local);
433 cpml_pair_transform(cpml_pair_copy(&base12, &data->point.base12), local);
434 cpml_pair_transform(cpml_pair_copy(&base2, &data->point.base2), local);
436 /* Combine points and global shifts to build the path */
437 cpml_pair_add(cpml_pair_copy(&pair, &ref1), &data->shift.from1);
438 cpml_pair_to_cairo(&pair, &data->cpml.data[6]);
440 cpml_pair_add(cpml_pair_copy(&pair, &base1), &data->shift.base1);
441 cpml_pair_to_cairo(&pair, &data->cpml.data[1]);
443 cpml_pair_add(&pair, &data->shift.to1);
444 cpml_pair_to_cairo(&pair, &data->cpml.data[8]);
446 cpml_pair_add(cpml_pair_copy(&pair, &base12), &data->shift.base12);
447 cpml_pair_to_cairo(&pair, &data->cpml.data[3]);
449 cpml_pair_add(cpml_pair_copy(&pair, &ref2), &data->shift.from2);
450 cpml_pair_to_cairo(&pair, &data->cpml.data[10]);
452 cpml_pair_add(cpml_pair_copy(&pair, &base2), &data->shift.base2);
453 cpml_pair_to_cairo(&pair, &data->cpml.data[4]);
455 cpml_pair_add(&pair, &data->shift.to2);
456 cpml_pair_to_cairo(&pair, &data->cpml.data[12]);
458 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
460 if (quote != NULL) {
461 /* Update global and local map of the quote container */
462 AdgEntity *quote_entity;
463 gdouble angle;
464 AdgMatrix matrix;
466 quote_entity = (AdgEntity *) quote;
467 angle = adg_dim_quote_angle(dim, (data->angle1 + data->angle2) / 2 + G_PI_2);
468 adg_matrix_copy(&matrix, local);
469 cairo_matrix_invert(&matrix);
471 cpml_pair_from_cairo(&pair, &data->cpml.data[3]);
472 cairo_matrix_transform_point(&matrix, &pair.x, &pair.y);
473 cairo_matrix_init_translate(&matrix, pair.x, pair.y);
474 adg_entity_set_local_map(quote_entity, &matrix);
476 cairo_matrix_init_rotate(&matrix, angle);
477 adg_entity_transform_global_map(quote_entity, &matrix, ADG_TRANSFORM_BEFORE);
479 adg_entity_get_global_map(quote_entity, &data->quote.global_map);
480 adg_entity_get_local_map(quote_entity, &data->quote.local_map);
483 /* Signal to the markers (if any) that the path has changed */
484 if (data->marker1 != NULL) {
485 adg_marker_set_segment(data->marker1, data->trail, 1);
486 adg_entity_local_changed((AdgEntity *) data->marker1);
489 if (data->marker2 != NULL) {
490 adg_marker_set_segment(data->marker2, data->trail, 1);
491 adg_entity_local_changed((AdgEntity *) data->marker2);
494 /* TODO: compute the extents */
497 static void
498 render(AdgEntity *entity, cairo_t *cr)
500 AdgADim *adim;
501 AdgDim *dim;
502 AdgADimPrivate *data;
503 AdgDimStyle *dim_style;
504 AdgDress dress;
505 const cairo_path_t *cairo_path;
507 adim = (AdgADim *) entity;
508 dim = (AdgDim *) entity;
509 data = adim->data;
510 dim_style = GET_DIM_STYLE(dim);
512 adg_style_apply((AdgStyle *) dim_style, entity, cr);
514 if (data->marker1 != NULL)
515 adg_entity_render((AdgEntity *) data->marker1, cr);
517 if (data->marker2 != NULL)
518 adg_entity_render((AdgEntity *) data->marker2, cr);
520 adg_entity_render((AdgEntity *) adg_dim_get_quote(dim), cr);
522 dress = adg_dim_style_get_line_dress(dim_style);
523 adg_entity_apply_dress(entity, dress, cr);
525 cairo_path = adg_trail_get_cairo_path(data->trail);
526 cairo_append_path(cr, cairo_path);
527 cairo_stroke(cr);
530 static gchar *
531 default_value(AdgDim *dim)
533 AdgADim *adim;
534 AdgADimPrivate *data;
535 AdgDimStyle *dim_style;
536 gdouble angle;
537 const gchar *format;
539 adim = (AdgADim *) dim;
540 data = adim->data;
541 dim_style = GET_DIM_STYLE(dim);
542 format = adg_dim_style_get_number_format(dim_style);
544 update_geometry(adim);
545 angle = (data->angle2 - data->angle1) * 180 / M_PI;
547 return g_strdup_printf(format, angle);
550 /* With "geometry" is considered any data (point, vector or angle)
551 * that can be cached: this is strictly related on how the arrange()
552 * method works */
553 static void
554 update_geometry(AdgADim *adim)
556 AdgADimPrivate *data;
557 AdgDimStyle *dim_style;
558 gdouble from_offset, to_offset;
559 gdouble spacing, level;
560 CpmlVector vector[3];
561 CpmlPair center;
562 gdouble distance;
564 data = adim->data;
566 if (data->geometry_arranged)
567 return;
569 if (!get_info(adim, vector, &center, &distance)) {
570 /* Parallel lines: hang with an error message */
571 g_warning("%s: trying to set an angular dimension on parallel lines",
572 G_STRLOC);
573 return;
576 dim_style = GET_DIM_STYLE(adim);
577 from_offset = adg_dim_style_get_from_offset(dim_style);
578 to_offset = adg_dim_style_get_to_offset(dim_style);
579 spacing = adg_dim_style_get_baseline_spacing(dim_style);
580 level = adg_dim_get_level((AdgDim *) adim);
582 /* shift.from1 */
583 cpml_vector_set_length(&vector[0], from_offset);
584 cpml_pair_copy(&data->shift.from1, &vector[0]);
586 /* shift.base1 */
587 cpml_vector_set_length(&vector[0], level * spacing);
588 cpml_pair_copy(&data->shift.base1, &vector[0]);
590 /* shift.to1 */
591 cpml_vector_set_length(&vector[0], to_offset);
592 cpml_pair_copy(&data->shift.to1, &vector[0]);
594 /* shift.from2 */
595 cpml_vector_set_length(&vector[2], from_offset);
596 cpml_pair_copy(&data->shift.from2, &vector[2]);
598 /* shift.base2 */
599 cpml_vector_set_length(&vector[2], level * spacing);
600 cpml_pair_copy(&data->shift.base2, &vector[2]);
602 /* shift.to2 */
603 cpml_vector_set_length(&vector[2], to_offset);
604 cpml_pair_copy(&data->shift.to2, &vector[2]);
606 /* shift.base12 */
607 cpml_vector_set_length(&vector[1], level * spacing);
608 cpml_pair_copy(&data->shift.base12, &vector[1]);
610 /* Distance can be 0, so the following will leave the
611 * vector array in undefined state */
613 /* point.base1 */
614 cpml_vector_set_length(&vector[0], distance);
615 cpml_pair_add(cpml_pair_copy(&data->point.base1, &vector[0]), &center);
617 /* point.base2 */
618 cpml_vector_set_length(&vector[2], distance);
619 cpml_pair_add(cpml_pair_copy(&data->point.base2, &vector[2]), &center);
621 /* point.base12 */
622 cpml_vector_set_length(&vector[1], distance);
623 cpml_pair_add(cpml_pair_copy(&data->point.base12, &vector[1]), &center);
625 data->geometry_arranged = TRUE;
628 static void
629 update_entities(AdgADim *adim)
631 AdgADimPrivate *data;
632 AdgDimStyle *dim_style;
634 data = adim->data;
635 dim_style = GET_DIM_STYLE(adim);
637 if (data->trail == NULL)
638 data->trail = adg_trail_new(trail_callback, adim);
640 if (data->marker1 == NULL)
641 data->marker1 = adg_dim_style_marker1_new(dim_style);
643 if (data->marker2 == NULL)
644 data->marker2 = adg_dim_style_marker2_new(dim_style);
647 static gboolean
648 set_org1(AdgADim *adim, const AdgPair *org1)
650 AdgADimPrivate *data = adim->data;
652 if (adg_pair_equal(&data->org1, org1))
653 return FALSE;
655 data->org1 = *org1;
657 return TRUE;
660 static gboolean
661 set_org2(AdgADim *adim, const AdgPair *org2)
663 AdgADimPrivate *data = adim->data;
665 if (adg_pair_equal(&data->org2, org2))
666 return FALSE;
668 data->org2 = *org2;
670 return TRUE;
673 static void
674 unset_trail(AdgADim *adim)
676 AdgADimPrivate *data = adim->data;
678 if (data->trail != NULL)
679 adg_model_clear((AdgModel *) data->trail);
681 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
684 static void
685 dispose_markers(AdgADim *adim)
687 AdgADimPrivate *data = adim->data;
689 if (data->trail != NULL) {
690 g_object_unref(data->trail);
691 data->trail = NULL;
694 if (data->marker1 != NULL) {
695 g_object_unref(data->marker1);
696 data->marker1 = NULL;
699 if (data->marker2 != NULL) {
700 g_object_unref(data->marker2);
701 data->marker2 = NULL;
705 static gboolean
706 get_info(AdgADim *adim, CpmlVector vector[],
707 AdgPair *center, gdouble *distance)
709 AdgDim *dim;
710 AdgADimPrivate *data;
711 const AdgPair *ref1, *ref2;
712 gdouble factor;
714 dim = (AdgDim *) adim;
715 data = adim->data;
716 ref1 = adg_dim_get_ref1(dim);
717 ref2 = adg_dim_get_ref2(dim);
719 cpml_pair_sub(cpml_pair_copy(&vector[0], ref1), &data->org1);
720 cpml_pair_sub(cpml_pair_copy(&vector[2], ref2), &data->org2);
722 factor = vector[0].x * vector[2].y - vector[0].y * vector[2].x;
723 if (factor == 0)
724 return FALSE;
726 factor = ((ref1->y - ref2->y) * vector[2].x -
727 (ref1->x - ref2->x) * vector[2].y) / factor;
729 center->x = ref1->x + vector[0].x * factor;
730 center->y = ref1->y + vector[0].y * factor;
731 *distance = cpml_pair_distance(center, adg_dim_get_pos(dim));
732 data->angle1 = cpml_vector_angle(&vector[0]);
733 data->angle2 = cpml_vector_angle(&vector[2]);
734 while (data->angle2 < data->angle1)
735 data->angle2 += M_PI * 2;
737 cpml_vector_from_angle(&vector[1], (data->angle1 + data->angle2) / 2);
739 return TRUE;
742 static CpmlPath *
743 trail_callback(AdgTrail *trail, gpointer user_data)
745 AdgADim *adim;
746 AdgADimPrivate *data;
748 adim = (AdgADim *) user_data;
749 data = adim->data;
751 return &data->cpml.path;