cpml: removed CpmlPath
[adg.git] / src / adg / adg-adim.c
blob282b20cd924625cd5512dd641c3577f95dd41ec5
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 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.
27 * Since: 1.0
30 /**
31 * AdgADim:
33 * All fields are privates and should not be used directly.
34 * Use its public methods instead.
36 * Since: 1.0
37 **/
40 #include "adg-internal.h"
41 #include "adg-container.h"
42 #include "adg-alignment.h"
43 #include "adg-model.h"
44 #include "adg-point.h"
45 #include "adg-trail.h"
46 #include "adg-marker.h"
47 #include "adg-style.h"
48 #include "adg-dim-style.h"
49 #include "adg-textual.h"
50 #include "adg-toy-text.h"
51 #include "adg-dim.h"
52 #include "adg-dim-private.h"
54 #include "adg-adim.h"
55 #include "adg-adim-private.h"
58 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_adim_parent_class)
59 #define _ADG_OLD_ENTITY_CLASS ((AdgEntityClass *) adg_adim_parent_class)
62 G_DEFINE_TYPE(AdgADim, adg_adim, ADG_TYPE_DIM)
64 enum {
65 PROP_0,
66 PROP_VALUE,
67 PROP_ORG1,
68 PROP_ORG2,
69 PROP_HAS_EXTENSION1,
70 PROP_HAS_EXTENSION2
74 static void _adg_dispose (GObject *object);
75 static void _adg_get_property (GObject *object,
76 guint param_id,
77 GValue *value,
78 GParamSpec *pspec);
79 static void _adg_set_property (GObject *object,
80 guint param_id,
81 const GValue *value,
82 GParamSpec *pspec);
83 static void _adg_global_changed (AdgEntity *entity);
84 static void _adg_local_changed (AdgEntity *entity);
85 static void _adg_invalidate (AdgEntity *entity);
86 static void _adg_arrange (AdgEntity *entity);
87 static void _adg_render (AdgEntity *entity,
88 cairo_t *cr);
89 static gchar * _adg_default_value (AdgDim *dim);
90 static gboolean _adg_update_geometry (AdgADim *adim);
91 static void _adg_update_entities (AdgADim *adim);
92 static void _adg_unset_trail (AdgADim *adim);
93 static void _adg_dispose_trail (AdgADim *adim);
94 static void _adg_dispose_markers (AdgADim *adim);
95 static gboolean _adg_get_info (AdgADim *adim,
96 CpmlVector vector[],
97 CpmlPair *center,
98 gdouble *distance);
99 static cairo_path_t * _adg_trail_callback (AdgTrail *trail,
100 gpointer user_data);
103 static void
104 adg_adim_class_init(AdgADimClass *klass)
106 GObjectClass *gobject_class;
107 AdgEntityClass *entity_class;
108 AdgDimClass *dim_class;
109 GParamSpec *param, *old_param;
111 gobject_class = (GObjectClass *) klass;
112 entity_class = (AdgEntityClass *) klass;
113 dim_class = (AdgDimClass *) klass;
115 g_type_class_add_private(klass, sizeof(AdgADimPrivate));
117 gobject_class->dispose = _adg_dispose;
118 gobject_class->get_property = _adg_get_property;
119 gobject_class->set_property = _adg_set_property;
121 entity_class->global_changed = _adg_global_changed;
122 entity_class->local_changed = _adg_local_changed;
123 entity_class->invalidate = _adg_invalidate;
124 entity_class->arrange = _adg_arrange;
125 entity_class->render = _adg_render;
127 dim_class->default_value = _adg_default_value;
129 /* Override #AdgDim:value to append a degree symbol
130 * to the default set value template string */
131 old_param = g_object_class_find_property(gobject_class, "value");
132 param = g_param_spec_string(old_param->name,
133 g_param_spec_get_nick(old_param),
134 g_param_spec_get_blurb(old_param),
135 "<>" ADG_UTF8_DEGREE,
136 old_param->flags);
137 g_object_class_install_property(gobject_class, PROP_VALUE, param);
139 param = g_param_spec_boxed("org1",
140 P_("First Origin"),
141 P_("Where the first line comes from: this point is used toghether with \"ref1\" to align the first extension line"),
142 ADG_TYPE_POINT,
143 G_PARAM_READWRITE);
144 g_object_class_install_property(gobject_class, PROP_ORG1, param);
146 param = g_param_spec_boxed("org2",
147 P_("Second Origin"),
148 P_("Where the second line comes from: this point is used toghether with \"ref2\" to align the second extension line"),
149 ADG_TYPE_POINT,
150 G_PARAM_READWRITE);
151 g_object_class_install_property(gobject_class, PROP_ORG2, param);
153 param = g_param_spec_boolean("has-extension1",
154 P_("Has First Extension Line flag"),
155 P_("Show (TRUE) or hide (FALSE) the first extension line"),
156 TRUE, G_PARAM_READWRITE);
157 g_object_class_install_property(gobject_class, PROP_HAS_EXTENSION1, param);
159 param = g_param_spec_boolean("has-extension2",
160 P_("Has Second Extension Line flag"),
161 P_("Show (TRUE) or hide (FALSE) the second extension line"),
162 TRUE, G_PARAM_READWRITE);
163 g_object_class_install_property(gobject_class, PROP_HAS_EXTENSION2, param);
166 static void
167 adg_adim_init(AdgADim *adim)
169 AdgADimPrivate *data;
170 cairo_path_data_t move_to, line_to, arc_to;
172 data = G_TYPE_INSTANCE_GET_PRIVATE(adim, ADG_TYPE_ADIM, AdgADimPrivate);
173 move_to.header.type = CPML_MOVE;
174 move_to.header.length = 2;
175 line_to.header.type = CPML_LINE;
176 line_to.header.length = 2;
177 arc_to.header.type = CPML_ARC;
178 arc_to.header.length = 3;
180 data->org1 = NULL;
181 data->org2 = NULL;
182 data->has_extension1 = TRUE;
183 data->has_extension2 = TRUE;
185 data->cairo.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
186 data->cairo.path.data = data->cairo.data;
187 data->cairo.path.num_data = G_N_ELEMENTS(data->cairo.data);
188 data->cairo.path.data[0] = move_to;
189 data->cairo.path.data[2] = arc_to;
190 data->cairo.path.data[5] = move_to;
191 data->cairo.path.data[7] = line_to;
192 data->cairo.path.data[9] = move_to;
193 data->cairo.path.data[11] = line_to;
195 data->trail = NULL;
196 data->marker1 = NULL;
197 data->marker2 = NULL;
199 data->geometry_arranged = FALSE;
201 adim->data = data;
204 static void
205 _adg_dispose(GObject *object)
207 AdgEntity *entity;
208 AdgADim *adim;
209 AdgADimPrivate *data;
211 entity = (AdgEntity *) object;
212 adim = (AdgADim *) entity;
213 data = adim->data;
215 _adg_dispose_trail(adim);
216 _adg_dispose_markers(adim);
218 if (data->org1 != NULL)
219 data->org1 = adg_entity_point(entity, data->org1, NULL);
221 if (data->org2 != NULL)
222 data->org2 = adg_entity_point(entity, data->org2, NULL);
224 if (_ADG_OLD_OBJECT_CLASS->dispose)
225 _ADG_OLD_OBJECT_CLASS->dispose(object);
228 static void
229 _adg_get_property(GObject *object, guint prop_id,
230 GValue *value, GParamSpec *pspec)
232 AdgADimPrivate *data = ((AdgADim *) object)->data;
234 switch (prop_id) {
235 case PROP_VALUE:
236 g_value_set_string(value, adg_dim_get_value((AdgDim *) object));
237 break;
238 case PROP_ORG1:
239 g_value_set_boxed(value, data->org1);
240 break;
241 case PROP_ORG2:
242 g_value_set_boxed(value, data->org2);
243 break;
244 case PROP_HAS_EXTENSION1:
245 g_value_set_boolean(value, data->has_extension1);
246 break;
247 case PROP_HAS_EXTENSION2:
248 g_value_set_boolean(value, data->has_extension2);
249 break;
250 default:
251 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
252 break;
256 static void
257 _adg_set_property(GObject *object, guint prop_id,
258 const GValue *value, GParamSpec *pspec)
260 AdgEntity *entity;
261 AdgADimPrivate *data;
263 entity = (AdgEntity *) object;
264 data = ((AdgADim *) object)->data;
266 switch (prop_id) {
267 case PROP_VALUE:
268 adg_dim_set_value((AdgDim *) object, g_value_get_string(value));
269 break;
270 case PROP_ORG1:
271 data->org1 = adg_entity_point(entity, data->org1,
272 g_value_get_boxed(value));
273 break;
274 case PROP_ORG2:
275 data->org2 = adg_entity_point(entity, data->org2,
276 g_value_get_boxed(value));
277 break;
278 case PROP_HAS_EXTENSION1:
279 data->has_extension1 = g_value_get_boolean(value);
280 break;
281 case PROP_HAS_EXTENSION2:
282 data->has_extension2 = g_value_get_boolean(value);
283 break;
284 default:
285 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
286 break;
292 * adg_adim_new:
294 * Creates a new - undefined - angular dimension. You must, at least,
295 * define the first line by setting #AdgADim:org1 (start point) and
296 * #AdgDim:ref1 (end point), the second line by setting #AdgADim:org2
297 * (start point) and #AdgDim:ref2 (end point) and the position of
298 * the quote in #AdgDim:pos.
300 * Returns: the newly created angular dimension entity
302 * Since: 1.0
304 AdgADim *
305 adg_adim_new(void)
307 return g_object_new(ADG_TYPE_ADIM, NULL);
311 * adg_adim_new_full:
312 * @ref1: (allow-none): first reference point
313 * @ref2: (allow-none): second reference point
314 * @org1: (allow-none): first origin point
315 * @org2: (allow-none): second origin point
316 * @pos: (allow-none): the position point
318 * Creates a new angular dimension, specifing all the needed
319 * properties in one shot using #CpmlPair.
321 * Returns: the newly created angular dimension entity
323 * Since: 1.0
325 AdgADim *
326 adg_adim_new_full(const CpmlPair *ref1, const CpmlPair *ref2,
327 const CpmlPair *org1, const CpmlPair *org2,
328 const CpmlPair *pos)
330 AdgADim *adim;
331 AdgDim *dim;
333 adim = adg_adim_new();
334 dim = (AdgDim *) adim;
336 if (ref1 != NULL)
337 adg_dim_set_ref1_from_pair(dim, ref1);
339 if (ref2 != NULL)
340 adg_dim_set_ref2_from_pair(dim, ref2);
342 if (pos != NULL)
343 adg_dim_set_pos_from_pair(dim, pos);
345 if (org1 != NULL)
346 adg_adim_set_org1_from_pair(adim, org1);
348 if (org2 != NULL)
349 adg_adim_set_org2_from_pair(adim, org2);
351 return adim;
355 * adg_adim_new_full_explicit:
356 * @ref1_x: the x coordinate of end point of the first line
357 * @ref1_y: the y coordinate of end point of the first line
358 * @ref2_x: the x coordinate of end point of the second line
359 * @ref2_y: the y coordinate of end point of the second line
360 * @org1_x: the x coordinate of start point of the first line
361 * @org1_y: the y coordinate of start point of the first line
362 * @org2_x: the x coordinate of start point of the second line
363 * @org2_y: the y coordinate of start point of the second line
364 * @pos_x: the x coordinate of the position reference
365 * @pos_y: the y coordinate of the position reference
367 * Wrappes adg_adim_new_full() with explicit values.
369 * Returns: the newly created linear dimension entity
371 * Since: 1.0
373 AdgADim *
374 adg_adim_new_full_explicit(gdouble ref1_x, gdouble ref1_y,
375 gdouble ref2_x, gdouble ref2_y,
376 gdouble org1_x, gdouble org1_y,
377 gdouble org2_x, gdouble org2_y,
378 gdouble pos_x, gdouble pos_y)
380 CpmlPair ref1, ref2, org1, org2, pos;
382 ref1.x = ref1_x;
383 ref1.y = ref1_y;
384 ref2.x = ref2_x;
385 ref2.y = ref2_y;
386 org1.x = org1_x;
387 org1.y = org1_y;
388 org2.x = org2_x;
389 org2.y = org2_y;
390 pos.x = pos_x;
391 pos.y = pos_y;
393 return adg_adim_new_full(&ref1, &ref2, &org1, &org2, &pos);
397 * adg_adim_new_full_from_model:
398 * @model: (transfer none): the model from which the named pairs are taken
399 * @ref1: (allow-none): the end point of the first line
400 * @ref2: (allow-none): the end point of the second line
401 * @org1: (allow-none): the origin of the first line
402 * @org2: (allow-none): the origin of the second line
403 * @pos: (allow-none): the position reference
405 * Creates a new angular dimension, specifing all the needed properties
406 * in one shot and using named pairs from @model.
408 * Returns: the newly created angular dimension entity
410 * Since: 1.0
412 AdgADim *
413 adg_adim_new_full_from_model(AdgModel *model,
414 const gchar *ref1, const gchar *ref2,
415 const gchar *org1, const gchar *org2,
416 const gchar *pos)
418 AdgADim *adim;
419 AdgDim *dim;
421 adim = adg_adim_new();
422 dim = (AdgDim *) adim;
424 if (ref1 != NULL)
425 adg_dim_set_ref1_from_model(dim, model, ref1);
427 if (ref2 != NULL)
428 adg_dim_set_ref2_from_model(dim, model, ref2);
430 if (pos != NULL)
431 adg_dim_set_pos_from_model(dim, model, pos);
433 if (org1 != NULL)
434 adg_adim_set_org1_from_model(adim, model, org1);
436 if (org2 != NULL)
437 adg_adim_set_org2_from_model(adim, model, org2);
439 return adim;
443 * adg_adim_set_org1:
444 * @adim: an #AdgADim
445 * @org1: the new point to use as first reference
447 * Sets the #AdgADim:org1 property to @org1. The old point
448 * is silently discarded, unreferencing its model if that
449 * point was bound to a named pair (hence, possibly destroying
450 * the model if this was the last reference).
452 * @org1 can be %NULL, in which case the point is destroyed.
454 * Since: 1.0
456 void
457 adg_adim_set_org1(AdgADim *adim, const AdgPoint *org1)
459 g_return_if_fail(ADG_IS_ADIM(adim));
460 g_object_set(adim, "org1", org1, NULL);
464 * adg_adim_set_org1_explicit:
465 * @adim: an #AdgADim
466 * @x: x coordinate of the first reference point
467 * @y: y coordinate of the first reference point
469 * Sets the #AdgADim:org1 property to the (@x, @y) explicit
470 * coordinates. The old point is silently discarded,
471 * unreferencing its model if that point was bound to a named
472 * pair (hence, possibly destroying the model if this was the
473 * last reference).
475 * Since: 1.0
477 void
478 adg_adim_set_org1_explicit(AdgADim *adim, gdouble x, gdouble y)
480 AdgPoint *point = adg_point_new();
482 adg_point_set_pair_explicit(point, x, y);
483 adg_adim_set_org1(adim, point);
485 adg_point_destroy(point);
489 * adg_adim_set_org1_from_pair:
490 * @adim: an #AdgADim
491 * @org1: the coordinates pair of the first reference point
493 * Convenient function to set the #AdgADim:org1 property using a
494 * pair instead of explicit coordinates.
496 * Since: 1.0
498 void
499 adg_adim_set_org1_from_pair(AdgADim *adim, const CpmlPair *org1)
501 g_return_if_fail(org1 != NULL);
503 adg_adim_set_org1_explicit(adim, org1->x, org1->y);
507 * adg_adim_set_org1_from_model:
508 * @adim: an #AdgADim
509 * @model: the source #AdgModel
510 * @org1: a named pair in @model
512 * Binds #AdgADim:org1 to the @org1 named pair of @model. If @model
513 * is %NULL, the point will be unset. In any case, the old point
514 * is silently discarded, unreferencing its model if that point
515 * was bound to a named pair (hence, possibly destroying the model
516 * if this was the last reference).
518 * The assignment is lazy so @org1 could be not be present in @model.
519 * Anyway, at the first access to this point an error will be raised
520 * if the named pair is still missing.
522 * Since: 1.0
524 void
525 adg_adim_set_org1_from_model(AdgADim *adim, AdgModel *model, const gchar *org1)
527 AdgPoint *point = adg_point_new();
529 adg_point_set_pair_from_model(point, model, org1);
530 adg_adim_set_org1(adim, point);
532 adg_point_destroy(point);
536 * adg_adim_get_org1:
537 * @adim: an #AdgADim
539 * Gets the #AdgADim:org1 point. The returned point is internally owned
540 * and must not be freed or modified.
542 * Returns: (transfer none): the first reference point
544 * Since: 1.0
546 AdgPoint *
547 adg_adim_get_org1(AdgADim *adim)
549 AdgADimPrivate *data;
551 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
553 data = adim->data;
555 return data->org1;
559 * adg_adim_set_org2:
560 * @adim: an #AdgADim
561 * @org2: the new point to use as first reference
563 * Sets the #AdgADim:org2 property to @org2. The old point
564 * is silently discarded, unreferencing its model if that
565 * point was bound to a named pair (hence, possibly destroying
566 * the model if this was the last reference).
568 * @org2 can be %NULL, in which case the point is destroyed.
570 * Since: 1.0
572 void
573 adg_adim_set_org2(AdgADim *adim, const AdgPoint *org2)
575 g_return_if_fail(ADG_IS_ADIM(adim));
576 g_object_set(adim, "org2", org2, NULL);
580 * adg_adim_set_org2_explicit:
581 * @adim: an #AdgADim
582 * @x: x coordinate of the first reference point
583 * @y: y coordinate of the first reference point
585 * Sets the #AdgADim:org2 property to the (@x, @y) explicit
586 * coordinates. The old point is silently discarded,
587 * unreferencing its model if that point was bound to a named
588 * pair (hence, possibly destroying the model if this was the
589 * last reference).
591 * Since: 1.0
593 void
594 adg_adim_set_org2_explicit(AdgADim *adim, gdouble x, gdouble y)
596 AdgPoint *point = adg_point_new();
598 adg_point_set_pair_explicit(point, x, y);
599 adg_adim_set_org2(adim, point);
601 adg_point_destroy(point);
605 * adg_adim_set_org2_from_pair:
606 * @adim: an #AdgADim
607 * @org2: the coordinates pair of the first reference point
609 * Convenient function to set the #AdgADim:org2 property using a
610 * pair instead of explicit coordinates.
612 * Since: 1.0
614 void
615 adg_adim_set_org2_from_pair(AdgADim *adim, const CpmlPair *org2)
617 g_return_if_fail(org2 != NULL);
619 adg_adim_set_org2_explicit(adim, org2->x, org2->y);
623 * adg_adim_set_org2_from_model:
624 * @adim: an #AdgADim
625 * @model: the source #AdgModel
626 * @org2: a named pair in @model
628 * Binds #AdgADim:org2 to the @org2 named pair of @model. If @model
629 * is %NULL, the point will be unset. In any case, the old point
630 * is silently discarded, unreferencing its model if that point
631 * was bound to a named pair (hence, possibly destroying the model
632 * if this was the last reference).
634 * The assignment is lazy so @org2 could be not be present in @model.
635 * Anyway, at the first access to this point an error will be raised
636 * if the named pair is still missing.
638 * Since: 1.0
640 void
641 adg_adim_set_org2_from_model(AdgADim *adim, AdgModel *model, const gchar *org2)
643 AdgPoint *point = adg_point_new();
645 adg_point_set_pair_from_model(point, model, org2);
646 adg_adim_set_org2(adim, point);
648 adg_point_destroy(point);
652 * adg_adim_get_org2:
653 * @adim: an #AdgADim
655 * Gets the #AdgADim:org2 point. The returned point is internally owned
656 * and must not be freed or modified.
658 * Returns: (transfer none): the second reference point
660 * Since: 1.0
662 AdgPoint *
663 adg_adim_get_org2(AdgADim *adim)
665 AdgADimPrivate *data;
667 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
669 data = adim->data;
671 return data->org2;
675 * adg_adim_switch_extension1:
676 * @adim: an #AdgADim entity
677 * @new_state: the new state
679 * Shows (if @new_state is %TRUE) or hides (if @new_state is %FALSE)
680 * the first extension line of @adim.
682 * Since: 1.0
684 void
685 adg_adim_switch_extension1(AdgADim *adim, gboolean new_state)
687 g_return_if_fail(ADG_IS_ADIM(adim));
688 g_return_if_fail(adg_is_boolean_value(new_state));
689 g_object_set(adim, "has-extension1", new_state, NULL);
693 * adg_adim_has_extension1:
694 * @adim: an #AdgADim entity
696 * Checks if @adim should render the first extension line.
698 * Returns: %TRUE on first extension line presents, %FALSE otherwise
700 * Since: 1.0
702 gboolean
703 adg_adim_has_extension1(AdgADim *adim)
705 AdgADimPrivate *data;
707 g_return_val_if_fail(ADG_IS_ADIM(adim), FALSE);
709 data = adim->data;
711 return data->has_extension1;
715 * adg_adim_switch_extension2:
716 * @adim: an #AdgADim entity
717 * @new_state: the new new_state
719 * Shows (if @new_state is %TRUE) or hides (if @new_state is %FALSE)
720 * the second extension line of @adim.
722 * Since: 1.0
724 void
725 adg_adim_switch_extension2(AdgADim *adim, gboolean new_state)
727 g_return_if_fail(ADG_IS_ADIM(adim));
728 g_return_if_fail(adg_is_boolean_value(new_state));
729 g_object_set(adim, "has-extension2", new_state, NULL);
733 * adg_adim_has_extension2:
734 * @adim: an #AdgADim entity
736 * Checks if @adim should render the second extension line.
738 * Returns: %TRUE on first extension line presents, %FALSE otherwise
740 * Since: 1.0
742 gboolean
743 adg_adim_has_extension2(AdgADim *adim)
745 AdgADimPrivate *data;
747 g_return_val_if_fail(ADG_IS_ADIM(adim), FALSE);
749 data = adim->data;
751 return data->has_extension2;
755 static void
756 _adg_global_changed(AdgEntity *entity)
758 AdgADimPrivate *data = ((AdgADim *) entity)->data;
760 _adg_unset_trail((AdgADim *) entity);
762 if (_ADG_OLD_ENTITY_CLASS->global_changed)
763 _ADG_OLD_ENTITY_CLASS->global_changed(entity);
765 if (data->marker1 != NULL)
766 adg_entity_global_changed((AdgEntity *) data->marker1);
768 if (data->marker2 != NULL)
769 adg_entity_global_changed((AdgEntity *) data->marker2);
772 static void
773 _adg_local_changed(AdgEntity *entity)
775 _adg_unset_trail((AdgADim *) entity);
777 if (_ADG_OLD_ENTITY_CLASS->local_changed)
778 _ADG_OLD_ENTITY_CLASS->local_changed(entity);
781 static void
782 _adg_invalidate(AdgEntity *entity)
784 AdgADim *adim;
785 AdgADimPrivate *data;
787 adim = (AdgADim *) entity;
788 data = adim->data;
790 _adg_dispose_trail(adim);
791 _adg_dispose_markers(adim);
792 data->geometry_arranged = FALSE;
793 _adg_unset_trail(adim);
795 if (data->org1)
796 adg_point_invalidate(data->org1);
797 if (data->org2)
798 adg_point_invalidate(data->org2);
800 if (_ADG_OLD_ENTITY_CLASS->invalidate)
801 _ADG_OLD_ENTITY_CLASS->invalidate(entity);
804 static void
805 _adg_arrange(AdgEntity *entity)
807 AdgADim *adim;
808 AdgDim *dim;
809 AdgADimPrivate *data;
810 AdgAlignment *quote;
811 const cairo_matrix_t *global, *local;
812 CpmlPair ref1, ref2, base1, base12, base2;
813 CpmlPair pair;
814 CpmlExtents extents;
815 AdgEntity *marker_entity;
817 if (_ADG_OLD_ENTITY_CLASS->arrange)
818 _ADG_OLD_ENTITY_CLASS->arrange(entity);
820 adim = (AdgADim *) entity;
822 if (! _adg_update_geometry(adim))
823 return;
825 dim = (AdgDim *) adim;
826 data = adim->data;
827 quote = adg_dim_get_quote(dim);
829 _adg_update_entities(adim);
831 if (data->cairo.path.status == CAIRO_STATUS_SUCCESS) {
832 AdgEntity *quote_entity = (AdgEntity *) quote;
833 adg_entity_set_global_map(quote_entity, &data->quote.global_map);
834 return;
837 global = adg_entity_get_global_matrix(entity);
838 local = adg_entity_get_local_matrix(entity);
839 extents.is_defined = FALSE;
841 cpml_pair_copy(&ref1, (CpmlPair *) adg_dim_get_ref1(dim));
842 cpml_pair_copy(&ref2, (CpmlPair *) adg_dim_get_ref2(dim));
843 cpml_pair_copy(&base1, &data->point.base1);
844 cpml_pair_copy(&base12, &data->point.base12);
845 cpml_pair_copy(&base2, &data->point.base2);
847 /* Apply the local matrix to the relevant points */
848 cpml_pair_transform(&ref1, local);
849 cpml_pair_transform(&ref2, local);
850 cpml_pair_transform(&base1, local);
851 cpml_pair_transform(&base12, local);
852 cpml_pair_transform(&base2, local);
854 /* Combine points and global shifts to build the path */
855 pair.x = ref1.x + data->shift.from1.x;
856 pair.y = ref1.y + data->shift.from1.y;
857 cpml_pair_to_cairo(&pair, &data->cairo.data[6]);
859 pair.x = base1.x + data->shift.base1.x;
860 pair.y = base1.y + data->shift.base1.y;
861 cpml_pair_to_cairo(&pair, &data->cairo.data[1]);
863 pair.x += data->shift.to1.x;
864 pair.y += data->shift.to1.y;
865 cpml_pair_to_cairo(&pair, &data->cairo.data[8]);
867 pair.x = base12.x + data->shift.base12.x;
868 pair.y = base12.y + data->shift.base12.y;
869 cpml_pair_to_cairo(&pair, &data->cairo.data[3]);
871 pair.x = ref2.x + data->shift.from2.x;
872 pair.y = ref2.y + data->shift.from2.y;
873 cpml_pair_to_cairo(&pair, &data->cairo.data[10]);
875 pair.x = base2.x + data->shift.base2.x;
876 pair.y = base2.y + data->shift.base2.y;
877 cpml_pair_to_cairo(&pair, &data->cairo.data[4]);
879 pair.x += data->shift.to2.x;
880 pair.y += data->shift.to2.y;
881 cpml_pair_to_cairo(&pair, &data->cairo.data[12]);
883 /* Play with header lengths to show or hide the extension lines */
884 if (data->has_extension1) {
885 data->cairo.data[7].header.length = data->has_extension2 ? 2 : 6;
886 } else {
887 data->cairo.data[2].header.length = data->has_extension2 ? 7 : 11;
890 data->cairo.path.status = CAIRO_STATUS_SUCCESS;
892 /* Arrange the quote */
893 if (quote != NULL) {
894 AdgEntity *quote_entity;
895 gdouble angle;
896 cairo_matrix_t map;
898 quote_entity = (AdgEntity *) quote;
899 angle = adg_dim_quote_angle(dim, (data->angle1 + data->angle2) / 2 + G_PI_2);
900 cpml_pair_from_cairo(&pair, &data->cairo.data[3]);
902 adg_alignment_set_factor_explicit(quote, 0.5, 0);
904 cairo_matrix_init_translate(&map, pair.x, pair.y);
905 cairo_matrix_rotate(&map, angle);
906 adg_entity_set_global_map(quote_entity, &map);
907 adg_entity_arrange(quote_entity);
908 cpml_extents_add(&extents, adg_entity_get_extents(quote_entity));
910 adg_matrix_copy(&data->quote.global_map, &map);
913 /* Arrange the trail */
914 if (data->trail != NULL) {
915 CpmlExtents trail_extents;
916 cpml_extents_copy(&trail_extents, adg_trail_get_extents(data->trail));
917 cpml_extents_transform(&trail_extents, global);
918 cpml_extents_add(&extents, &trail_extents);
919 } else {
920 _adg_dispose_markers(adim);
923 /* Arrange the markers */
924 if (data->marker1 != NULL) {
925 marker_entity = (AdgEntity *) data->marker1;
926 adg_marker_set_segment(data->marker1, data->trail, 1);
927 adg_entity_local_changed(marker_entity);
928 adg_entity_arrange(marker_entity);
929 cpml_extents_add(&extents, adg_entity_get_extents(marker_entity));
931 if (data->marker2 != NULL) {
932 marker_entity = (AdgEntity *) data->marker2;
933 adg_marker_set_segment(data->marker2, data->trail, 1);
934 adg_entity_local_changed(marker_entity);
935 adg_entity_arrange(marker_entity);
936 cpml_extents_add(&extents, adg_entity_get_extents(marker_entity));
939 adg_entity_set_extents(entity, &extents);
942 static void
943 _adg_render(AdgEntity *entity, cairo_t *cr)
945 AdgADim *adim;
946 AdgDim *dim;
947 AdgADimPrivate *data;
948 AdgDimStyle *dim_style;
949 AdgDress dress;
950 const cairo_path_t *cairo_path;
952 adim = (AdgADim *) entity;
953 data = adim->data;
955 if (!data->geometry_arranged) {
956 /* Entity not arranged, probably due to undefined pair found */
957 return;
960 dim = (AdgDim *) entity;
961 dim_style = _ADG_GET_DIM_STYLE(dim);
963 adg_style_apply((AdgStyle *) dim_style, entity, cr);
964 adg_entity_render((AdgEntity *) adg_dim_get_quote(dim), cr);
966 if (data->marker1 != NULL)
967 adg_entity_render((AdgEntity *) data->marker1, cr);
968 if (data->marker2 != NULL)
969 adg_entity_render((AdgEntity *) data->marker2, cr);
971 cairo_transform(cr, adg_entity_get_global_matrix(entity));
972 dress = adg_dim_style_get_line_dress(dim_style);
973 adg_entity_apply_dress(entity, dress, cr);
975 cairo_path = adg_trail_get_cairo_path(data->trail);
976 cairo_append_path(cr, cairo_path);
977 cairo_stroke(cr);
980 static gchar *
981 _adg_default_value(AdgDim *dim)
983 AdgADim *adim;
984 AdgADimPrivate *data;
985 AdgDimStyle *dim_style;
986 gdouble angle;
987 const gchar *format;
989 adim = (AdgADim *) dim;
990 data = adim->data;
991 dim_style = _ADG_GET_DIM_STYLE(dim);
992 format = adg_dim_style_get_number_format(dim_style);
994 if (! _adg_update_geometry(adim))
995 return g_strdup("undef");
997 angle = (data->angle2 - data->angle1) * 180 / G_PI;
998 return g_strdup_printf(format, angle);
1001 /* With "geometry" is considered any data (point, vector or angle)
1002 * that can be cached: this is strictly related on how the arrange()
1003 * method works */
1004 static gboolean
1005 _adg_update_geometry(AdgADim *adim)
1007 AdgADimPrivate *data;
1008 AdgDimStyle *dim_style;
1009 gdouble from_offset, to_offset;
1010 gdouble spacing, level;
1011 CpmlVector vector[3];
1012 CpmlPair center;
1013 gdouble distance;
1015 data = adim->data;
1017 /* Check for cached results */
1018 if (data->geometry_arranged)
1019 return TRUE;
1021 if (! _adg_get_info(adim, vector, &center, &distance))
1022 return FALSE;
1024 dim_style = _ADG_GET_DIM_STYLE(adim);
1025 from_offset = adg_dim_style_get_from_offset(dim_style);
1026 to_offset = adg_dim_style_get_to_offset(dim_style);
1027 spacing = adg_dim_style_get_baseline_spacing(dim_style);
1028 level = adg_dim_get_level((AdgDim *) adim);
1030 /* shift.from1 */
1031 cpml_vector_set_length(&vector[0], from_offset);
1032 cpml_pair_copy(&data->shift.from1, &vector[0]);
1034 /* shift.base1 */
1035 cpml_vector_set_length(&vector[0], level * spacing);
1036 cpml_pair_copy(&data->shift.base1, &vector[0]);
1038 /* shift.to1 */
1039 cpml_vector_set_length(&vector[0], to_offset);
1040 cpml_pair_copy(&data->shift.to1, &vector[0]);
1042 /* shift.from2 */
1043 cpml_vector_set_length(&vector[2], from_offset);
1044 cpml_pair_copy(&data->shift.from2, &vector[2]);
1046 /* shift.base2 */
1047 cpml_vector_set_length(&vector[2], level * spacing);
1048 cpml_pair_copy(&data->shift.base2, &vector[2]);
1050 /* shift.to2 */
1051 cpml_vector_set_length(&vector[2], to_offset);
1052 cpml_pair_copy(&data->shift.to2, &vector[2]);
1054 /* shift.base12 */
1055 cpml_vector_set_length(&vector[1], level * spacing);
1056 cpml_pair_copy(&data->shift.base12, &vector[1]);
1058 /* Distance can be 0, so the following will leave the
1059 * vector array in undefined state */
1061 /* point.base1 */
1062 cpml_vector_set_length(&vector[0], distance);
1063 data->point.base1.x = vector[0].x + center.x;
1064 data->point.base1.y = vector[0].y + center.y;
1066 /* point.base2 */
1067 cpml_vector_set_length(&vector[2], distance);
1068 data->point.base2.x = vector[2].x + center.x;
1069 data->point.base2.y = vector[2].y + center.y;
1071 /* point.base12 */
1072 cpml_vector_set_length(&vector[1], distance);
1073 data->point.base12.x = vector[1].x + center.x;
1074 data->point.base12.y = vector[1].y + center.y;
1076 data->geometry_arranged = TRUE;
1078 return TRUE;
1081 static void
1082 _adg_update_entities(AdgADim *adim)
1084 AdgEntity *entity;
1085 AdgADimPrivate *data;
1086 AdgDimStyle *dim_style;
1088 entity = (AdgEntity *) adim;
1089 data = adim->data;
1090 dim_style = _ADG_GET_DIM_STYLE(adim);
1092 if (data->trail == NULL)
1093 data->trail = adg_trail_new(_adg_trail_callback, adim);
1095 if (data->marker1 == NULL) {
1096 data->marker1 = adg_dim_style_marker1_new(dim_style);
1097 adg_entity_set_parent((AdgEntity *) data->marker1, entity);
1100 if (data->marker2 == NULL) {
1101 data->marker2 = adg_dim_style_marker2_new(dim_style);
1102 adg_entity_set_parent((AdgEntity *) data->marker2, entity);
1106 static void
1107 _adg_unset_trail(AdgADim *adim)
1109 AdgADimPrivate *data = adim->data;
1111 if (data->trail != NULL)
1112 adg_model_clear((AdgModel *) data->trail);
1114 data->cairo.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
1117 static void
1118 _adg_dispose_trail(AdgADim *adim)
1120 AdgADimPrivate *data = adim->data;
1122 if (data->trail != NULL) {
1123 g_object_unref(data->trail);
1124 data->trail = NULL;
1128 static void
1129 _adg_dispose_markers(AdgADim *adim)
1131 AdgADimPrivate *data = adim->data;
1133 if (data->marker1 != NULL) {
1134 g_object_unref(data->marker1);
1135 data->marker1 = NULL;
1138 if (data->marker2 != NULL) {
1139 g_object_unref(data->marker2);
1140 data->marker2 = NULL;
1144 static gboolean
1145 _adg_get_info(AdgADim *adim, CpmlVector vector[],
1146 CpmlPair *center, gdouble *distance)
1148 AdgDim *dim;
1149 AdgADimPrivate *data;
1150 AdgPoint *ref1_point, *ref2_point, *pos_point;
1151 const CpmlPair *ref1, *ref2, *pos;
1152 const CpmlPair *org1, *org2;
1153 gdouble factor;
1155 dim = (AdgDim *) adim;
1156 data = adim->data;
1157 ref1_point = adg_dim_get_ref1(dim);
1158 ref2_point = adg_dim_get_ref2(dim);
1159 pos_point = adg_dim_get_pos(dim);
1161 /* Check if the needed points are all properly defined */
1162 if (! adg_point_update(ref1_point) ||
1163 ! adg_point_update(ref2_point) ||
1164 ! adg_point_update(pos_point) ||
1165 ! adg_point_update(data->org1) ||
1166 ! adg_point_update(data->org2))
1168 return FALSE;
1171 ref1 = (CpmlPair *) ref1_point;
1172 ref2 = (CpmlPair *) ref2_point;
1173 pos = (CpmlPair *) pos_point;
1174 org1 = (CpmlPair *) data->org1;
1175 org2 = (CpmlPair *) data->org2;
1177 /* Check if the given points have valid coordinates */
1178 if (cpml_pair_equal(ref1, org1)) {
1179 g_warning(_("%s: ref1 and org1 cannot be coincidents (%lf, %lf)"),
1180 G_STRLOC, ref1->x, ref1->y);
1181 return FALSE;
1184 if (cpml_pair_equal(ref2, org2)) {
1185 g_warning(_("%s: ref2 and org2 cannot be coincidents (%lf, %lf)"),
1186 G_STRLOC, ref2->x, ref2->y);
1187 return FALSE;
1190 vector[0].x = ref1->x - org1->x;
1191 vector[0].y = ref1->y - org1->y;
1192 vector[2].x = ref2->x - org2->x;
1193 vector[2].y = ref2->y - org2->y;
1195 factor = vector[0].x * vector[2].y - vector[0].y * vector[2].x;
1196 if (factor == 0) {
1197 /* Parallel lines: hang with an error message */
1198 g_warning(_("%s: trying to set an angular dimension on parallel lines"),
1199 G_STRLOC);
1200 return FALSE;
1203 factor = ((ref1->y - ref2->y) * vector[2].x -
1204 (ref1->x - ref2->x) * vector[2].y) / factor;
1206 center->x = ref1->x + vector[0].x * factor;
1207 center->y = ref1->y + vector[0].y * factor;
1208 *distance = cpml_pair_distance(center, pos);
1209 data->angle1 = cpml_vector_angle(&vector[0]);
1210 data->angle2 = cpml_vector_angle(&vector[2]);
1211 while (data->angle2 < data->angle1)
1212 data->angle2 += G_PI * 2;
1214 cpml_vector_from_angle(&vector[1], (data->angle1 + data->angle2) / 2);
1216 return TRUE;
1219 static cairo_path_t *
1220 _adg_trail_callback(AdgTrail *trail, gpointer user_data)
1222 AdgADim *adim;
1223 AdgADimPrivate *data;
1225 adim = (AdgADim *) user_data;
1226 data = adim->data;
1228 return &data->cairo.path;