Moved AdgPair to CpmlPair
[adg.git] / src / adg / adg-adim.c
bloba5f77f1e3efdb9a371189b68cdd576641971a07a
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012 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 CpmlPath * _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->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
186 data->cpml.path.data = data->cpml.data;
187 data->cpml.path.num_data = G_N_ELEMENTS(data->cpml.data);
188 data->cpml.path.data[0] = move_to;
189 data->cpml.path.data[2] = arc_to;
190 data->cpml.path.data[5] = move_to;
191 data->cpml.path.data[7] = line_to;
192 data->cpml.path.data[9] = move_to;
193 data->cpml.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. Anyway, it is not const because
541 * adg_point_get_pair() must be able to modify the internal cache of
542 * the returned point.
544 * Returns: the first reference point
546 * Since: 1.0
548 AdgPoint *
549 adg_adim_get_org1(AdgADim *adim)
551 AdgADimPrivate *data;
553 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
555 data = adim->data;
557 return data->org1;
561 * adg_adim_set_org2:
562 * @adim: an #AdgADim
563 * @org2: the new point to use as first reference
565 * Sets the #AdgADim:org2 property to @org2. The old point
566 * is silently discarded, unreferencing its model if that
567 * point was bound to a named pair (hence, possibly destroying
568 * the model if this was the last reference).
570 * @org2 can be %NULL, in which case the point is destroyed.
572 * Since: 1.0
574 void
575 adg_adim_set_org2(AdgADim *adim, const AdgPoint *org2)
577 g_return_if_fail(ADG_IS_ADIM(adim));
578 g_object_set(adim, "org2", org2, NULL);
582 * adg_adim_set_org2_explicit:
583 * @adim: an #AdgADim
584 * @x: x coordinate of the first reference point
585 * @y: y coordinate of the first reference point
587 * Sets the #AdgADim:org2 property to the (@x, @y) explicit
588 * coordinates. The old point is silently discarded,
589 * unreferencing its model if that point was bound to a named
590 * pair (hence, possibly destroying the model if this was the
591 * last reference).
593 * Since: 1.0
595 void
596 adg_adim_set_org2_explicit(AdgADim *adim, gdouble x, gdouble y)
598 AdgPoint *point = adg_point_new();
600 adg_point_set_pair_explicit(point, x, y);
601 adg_adim_set_org2(adim, point);
603 adg_point_destroy(point);
607 * adg_adim_set_org2_from_pair:
608 * @adim: an #AdgADim
609 * @org2: the coordinates pair of the first reference point
611 * Convenient function to set the #AdgADim:org2 property using a
612 * pair instead of explicit coordinates.
614 * Since: 1.0
616 void
617 adg_adim_set_org2_from_pair(AdgADim *adim, const CpmlPair *org2)
619 g_return_if_fail(org2 != NULL);
621 adg_adim_set_org2_explicit(adim, org2->x, org2->y);
625 * adg_adim_set_org2_from_model:
626 * @adim: an #AdgADim
627 * @model: the source #AdgModel
628 * @org2: a named pair in @model
630 * Binds #AdgADim:org2 to the @org2 named pair of @model. If @model
631 * is %NULL, the point will be unset. In any case, the old point
632 * is silently discarded, unreferencing its model if that point
633 * was bound to a named pair (hence, possibly destroying the model
634 * if this was the last reference).
636 * The assignment is lazy so @org2 could be not be present in @model.
637 * Anyway, at the first access to this point an error will be raised
638 * if the named pair is still missing.
640 * Since: 1.0
642 void
643 adg_adim_set_org2_from_model(AdgADim *adim, AdgModel *model, const gchar *org2)
645 AdgPoint *point = adg_point_new();
647 adg_point_set_pair_from_model(point, model, org2);
648 adg_adim_set_org2(adim, point);
650 adg_point_destroy(point);
654 * adg_adim_get_org2:
655 * @adim: an #AdgADim
657 * Gets the #AdgADim:org2 point. The returned point is internally owned
658 * and must not be freed or modified. Anyway, it is not const because
659 * adg_point_get_pair() must be able to modify the internal cache of
660 * the returned point.
662 * Returns: the first reference point
664 * Since: 1.0
666 AdgPoint *
667 adg_adim_get_org2(AdgADim *adim)
669 AdgADimPrivate *data;
671 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
673 data = adim->data;
675 return data->org2;
679 * adg_adim_switch_extension1:
680 * @adim: an #AdgADim entity
681 * @new_state: the new state
683 * Shows (if @new_state is %TRUE) or hides (if @new_state is %FALSE)
684 * the first extension line of @adim.
686 * Since: 1.0
688 void
689 adg_adim_switch_extension1(AdgADim *adim, gboolean new_state)
691 g_return_if_fail(ADG_IS_ADIM(adim));
692 g_return_if_fail(adg_is_boolean_value(new_state));
693 g_object_set(adim, "has-extension1", new_state, NULL);
697 * adg_adim_has_extension1:
698 * @adim: an #AdgADim entity
700 * Checks if @adim should render the first extension line.
702 * Returns: %TRUE on first extension line presents, %FALSE otherwise
704 * Since: 1.0
706 gboolean
707 adg_adim_has_extension1(AdgADim *adim)
709 AdgADimPrivate *data;
711 g_return_val_if_fail(ADG_IS_ADIM(adim), FALSE);
713 data = adim->data;
715 return data->has_extension1;
719 * adg_adim_switch_extension2:
720 * @adim: an #AdgADim entity
721 * @new_state: the new new_state
723 * Shows (if @new_state is %TRUE) or hides (if @new_state is %FALSE)
724 * the second extension line of @adim.
726 * Since: 1.0
728 void
729 adg_adim_switch_extension2(AdgADim *adim, gboolean new_state)
731 g_return_if_fail(ADG_IS_ADIM(adim));
732 g_return_if_fail(adg_is_boolean_value(new_state));
733 g_object_set(adim, "has-extension2", new_state, NULL);
737 * adg_adim_has_extension2:
738 * @adim: an #AdgADim entity
740 * Checks if @adim should render the second extension line.
742 * Returns: %TRUE on first extension line presents, %FALSE otherwise
744 * Since: 1.0
746 gboolean
747 adg_adim_has_extension2(AdgADim *adim)
749 AdgADimPrivate *data;
751 g_return_val_if_fail(ADG_IS_ADIM(adim), FALSE);
753 data = adim->data;
755 return data->has_extension2;
759 static void
760 _adg_global_changed(AdgEntity *entity)
762 AdgADimPrivate *data = ((AdgADim *) entity)->data;
764 _adg_unset_trail((AdgADim *) entity);
766 if (_ADG_OLD_ENTITY_CLASS->global_changed)
767 _ADG_OLD_ENTITY_CLASS->global_changed(entity);
769 if (data->marker1 != NULL)
770 adg_entity_global_changed((AdgEntity *) data->marker1);
772 if (data->marker2 != NULL)
773 adg_entity_global_changed((AdgEntity *) data->marker2);
776 static void
777 _adg_local_changed(AdgEntity *entity)
779 _adg_unset_trail((AdgADim *) entity);
781 if (_ADG_OLD_ENTITY_CLASS->local_changed)
782 _ADG_OLD_ENTITY_CLASS->local_changed(entity);
785 static void
786 _adg_invalidate(AdgEntity *entity)
788 AdgADim *adim;
789 AdgADimPrivate *data;
791 adim = (AdgADim *) entity;
792 data = adim->data;
794 _adg_dispose_trail(adim);
795 _adg_dispose_markers(adim);
796 data->geometry_arranged = FALSE;
797 _adg_unset_trail(adim);
799 if (data->org1)
800 adg_point_invalidate(data->org1);
801 if (data->org2)
802 adg_point_invalidate(data->org2);
804 if (_ADG_OLD_ENTITY_CLASS->invalidate)
805 _ADG_OLD_ENTITY_CLASS->invalidate(entity);
808 static void
809 _adg_arrange(AdgEntity *entity)
811 AdgADim *adim;
812 AdgDim *dim;
813 AdgADimPrivate *data;
814 AdgAlignment *quote;
815 const AdgMatrix *global, *local;
816 CpmlPair ref1, ref2, base1, base12, base2;
817 CpmlPair pair;
818 CpmlExtents extents;
819 AdgEntity *marker_entity;
821 if (_ADG_OLD_ENTITY_CLASS->arrange)
822 _ADG_OLD_ENTITY_CLASS->arrange(entity);
824 adim = (AdgADim *) entity;
826 if (!_adg_update_geometry(adim))
827 return;
829 dim = (AdgDim *) adim;
830 data = adim->data;
831 quote = adg_dim_get_quote(dim);
833 _adg_update_entities(adim);
835 if (data->cpml.path.status == CAIRO_STATUS_SUCCESS) {
836 AdgEntity *quote_entity = (AdgEntity *) quote;
837 adg_entity_set_global_map(quote_entity, &data->quote.global_map);
838 return;
841 global = adg_entity_get_global_matrix(entity);
842 local = adg_entity_get_local_matrix(entity);
843 extents.is_defined = FALSE;
845 cpml_pair_copy(&ref1, adg_point_get_pair(adg_dim_get_ref1(dim)));
846 cpml_pair_copy(&ref2, adg_point_get_pair(adg_dim_get_ref2(dim)));
847 cpml_pair_copy(&base1, &data->point.base1);
848 cpml_pair_copy(&base12, &data->point.base12);
849 cpml_pair_copy(&base2, &data->point.base2);
851 /* Apply the local matrix to the relevant points */
852 cpml_pair_transform(&ref1, local);
853 cpml_pair_transform(&ref2, local);
854 cpml_pair_transform(&base1, local);
855 cpml_pair_transform(&base12, local);
856 cpml_pair_transform(&base2, local);
858 /* Combine points and global shifts to build the path */
859 pair.x = ref1.x + data->shift.from1.x;
860 pair.y = ref1.y + data->shift.from1.y;
861 cpml_pair_to_cairo(&pair, &data->cpml.data[6]);
863 pair.x = base1.x + data->shift.base1.x;
864 pair.y = base1.y + data->shift.base1.y;
865 cpml_pair_to_cairo(&pair, &data->cpml.data[1]);
867 pair.x += data->shift.to1.x;
868 pair.y += data->shift.to1.y;
869 cpml_pair_to_cairo(&pair, &data->cpml.data[8]);
871 pair.x = base12.x + data->shift.base12.x;
872 pair.y = base12.y + data->shift.base12.y;
873 cpml_pair_to_cairo(&pair, &data->cpml.data[3]);
875 pair.x = ref2.x + data->shift.from2.x;
876 pair.y = ref2.y + data->shift.from2.y;
877 cpml_pair_to_cairo(&pair, &data->cpml.data[10]);
879 pair.x = base2.x + data->shift.base2.x;
880 pair.y = base2.y + data->shift.base2.y;
881 cpml_pair_to_cairo(&pair, &data->cpml.data[4]);
883 pair.x += data->shift.to2.x;
884 pair.y += data->shift.to2.y;
885 cpml_pair_to_cairo(&pair, &data->cpml.data[12]);
887 /* Play with header lengths to show or hide the extension lines */
888 if (data->has_extension1) {
889 data->cpml.data[7].header.length = data->has_extension2 ? 2 : 6;
890 } else {
891 data->cpml.data[2].header.length = data->has_extension2 ? 7 : 11;
894 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
896 /* Arrange the quote */
897 if (quote != NULL) {
898 AdgEntity *quote_entity;
899 gdouble angle;
900 AdgMatrix map;
902 quote_entity = (AdgEntity *) quote;
903 angle = adg_dim_quote_angle(dim, (data->angle1 + data->angle2) / 2 + G_PI_2);
904 cpml_pair_from_cairo(&pair, &data->cpml.data[3]);
906 adg_alignment_set_factor_explicit(quote, 0.5, 0);
908 cairo_matrix_init_translate(&map, pair.x, pair.y);
909 cairo_matrix_rotate(&map, angle);
910 adg_entity_set_global_map(quote_entity, &map);
911 adg_entity_arrange(quote_entity);
912 cpml_extents_add(&extents, adg_entity_get_extents(quote_entity));
914 adg_matrix_copy(&data->quote.global_map, &map);
917 /* Arrange the trail */
918 if (data->trail != NULL) {
919 CpmlExtents trail_extents;
920 cpml_extents_copy(&trail_extents, adg_trail_get_extents(data->trail));
921 cpml_extents_transform(&trail_extents, global);
922 cpml_extents_add(&extents, &trail_extents);
923 } else {
924 _adg_dispose_markers(adim);
927 /* Arrange the markers */
928 if (data->marker1 != NULL) {
929 marker_entity = (AdgEntity *) data->marker1;
930 adg_marker_set_segment(data->marker1, data->trail, 1);
931 adg_entity_local_changed(marker_entity);
932 adg_entity_arrange(marker_entity);
933 cpml_extents_add(&extents, adg_entity_get_extents(marker_entity));
935 if (data->marker2 != NULL) {
936 marker_entity = (AdgEntity *) data->marker2;
937 adg_marker_set_segment(data->marker2, data->trail, 1);
938 adg_entity_local_changed(marker_entity);
939 adg_entity_arrange(marker_entity);
940 cpml_extents_add(&extents, adg_entity_get_extents(marker_entity));
943 adg_entity_set_extents(entity, &extents);
946 static void
947 _adg_render(AdgEntity *entity, cairo_t *cr)
949 AdgADim *adim;
950 AdgDim *dim;
951 AdgADimPrivate *data;
952 AdgDimStyle *dim_style;
953 AdgDress dress;
954 const cairo_path_t *cairo_path;
956 adim = (AdgADim *) entity;
957 data = adim->data;
959 if (!data->geometry_arranged) {
960 /* Entity not arranged, probably due to undefined pair found */
961 return;
964 dim = (AdgDim *) entity;
965 dim_style = _ADG_GET_DIM_STYLE(dim);
967 adg_style_apply((AdgStyle *) dim_style, entity, cr);
968 adg_entity_render((AdgEntity *) adg_dim_get_quote(dim), cr);
970 if (data->marker1 != NULL)
971 adg_entity_render((AdgEntity *) data->marker1, cr);
972 if (data->marker2 != NULL)
973 adg_entity_render((AdgEntity *) data->marker2, cr);
975 cairo_transform(cr, adg_entity_get_global_matrix(entity));
976 dress = adg_dim_style_get_line_dress(dim_style);
977 adg_entity_apply_dress(entity, dress, cr);
979 cairo_path = adg_trail_get_cairo_path(data->trail);
980 cairo_append_path(cr, cairo_path);
981 cairo_stroke(cr);
984 static gchar *
985 _adg_default_value(AdgDim *dim)
987 AdgADim *adim;
988 AdgADimPrivate *data;
989 AdgDimStyle *dim_style;
990 gdouble angle;
991 const gchar *format;
993 adim = (AdgADim *) dim;
994 data = adim->data;
995 dim_style = _ADG_GET_DIM_STYLE(dim);
996 format = adg_dim_style_get_number_format(dim_style);
998 if (!_adg_update_geometry(adim))
999 return g_strdup("undef");
1001 angle = (data->angle2 - data->angle1) * 180 / G_PI;
1002 return g_strdup_printf(format, angle);
1005 /* With "geometry" is considered any data (point, vector or angle)
1006 * that can be cached: this is strictly related on how the arrange()
1007 * method works */
1008 static gboolean
1009 _adg_update_geometry(AdgADim *adim)
1011 AdgADimPrivate *data;
1012 AdgDimStyle *dim_style;
1013 gdouble from_offset, to_offset;
1014 gdouble spacing, level;
1015 CpmlVector vector[3];
1016 CpmlPair center;
1017 gdouble distance;
1019 data = adim->data;
1021 if (data->geometry_arranged)
1022 return TRUE;
1023 else if (!_adg_get_info(adim, vector, &center, &distance))
1024 return FALSE;
1026 dim_style = _ADG_GET_DIM_STYLE(adim);
1027 from_offset = adg_dim_style_get_from_offset(dim_style);
1028 to_offset = adg_dim_style_get_to_offset(dim_style);
1029 spacing = adg_dim_style_get_baseline_spacing(dim_style);
1030 level = adg_dim_get_level((AdgDim *) adim);
1032 /* shift.from1 */
1033 cpml_vector_set_length(&vector[0], from_offset);
1034 cpml_pair_copy(&data->shift.from1, &vector[0]);
1036 /* shift.base1 */
1037 cpml_vector_set_length(&vector[0], level * spacing);
1038 cpml_pair_copy(&data->shift.base1, &vector[0]);
1040 /* shift.to1 */
1041 cpml_vector_set_length(&vector[0], to_offset);
1042 cpml_pair_copy(&data->shift.to1, &vector[0]);
1044 /* shift.from2 */
1045 cpml_vector_set_length(&vector[2], from_offset);
1046 cpml_pair_copy(&data->shift.from2, &vector[2]);
1048 /* shift.base2 */
1049 cpml_vector_set_length(&vector[2], level * spacing);
1050 cpml_pair_copy(&data->shift.base2, &vector[2]);
1052 /* shift.to2 */
1053 cpml_vector_set_length(&vector[2], to_offset);
1054 cpml_pair_copy(&data->shift.to2, &vector[2]);
1056 /* shift.base12 */
1057 cpml_vector_set_length(&vector[1], level * spacing);
1058 cpml_pair_copy(&data->shift.base12, &vector[1]);
1060 /* Distance can be 0, so the following will leave the
1061 * vector array in undefined state */
1063 /* point.base1 */
1064 cpml_vector_set_length(&vector[0], distance);
1065 data->point.base1.x = vector[0].x + center.x;
1066 data->point.base1.y = vector[0].y + center.y;
1068 /* point.base2 */
1069 cpml_vector_set_length(&vector[2], distance);
1070 data->point.base2.x = vector[2].x + center.x;
1071 data->point.base2.y = vector[2].y + center.y;
1073 /* point.base12 */
1074 cpml_vector_set_length(&vector[1], distance);
1075 data->point.base12.x = vector[1].x + center.x;
1076 data->point.base12.y = vector[1].y + center.y;
1078 data->geometry_arranged = TRUE;
1080 return TRUE;
1083 static void
1084 _adg_update_entities(AdgADim *adim)
1086 AdgEntity *entity;
1087 AdgADimPrivate *data;
1088 AdgDimStyle *dim_style;
1090 entity = (AdgEntity *) adim;
1091 data = adim->data;
1092 dim_style = _ADG_GET_DIM_STYLE(adim);
1094 if (data->trail == NULL)
1095 data->trail = adg_trail_new(_adg_trail_callback, adim);
1097 if (data->marker1 == NULL) {
1098 data->marker1 = adg_dim_style_marker1_new(dim_style);
1099 adg_entity_set_parent((AdgEntity *) data->marker1, entity);
1102 if (data->marker2 == NULL) {
1103 data->marker2 = adg_dim_style_marker2_new(dim_style);
1104 adg_entity_set_parent((AdgEntity *) data->marker2, entity);
1108 static void
1109 _adg_unset_trail(AdgADim *adim)
1111 AdgADimPrivate *data = adim->data;
1113 if (data->trail != NULL)
1114 adg_model_clear((AdgModel *) data->trail);
1116 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
1119 static void
1120 _adg_dispose_trail(AdgADim *adim)
1122 AdgADimPrivate *data = adim->data;
1124 if (data->trail != NULL) {
1125 g_object_unref(data->trail);
1126 data->trail = NULL;
1130 static void
1131 _adg_dispose_markers(AdgADim *adim)
1133 AdgADimPrivate *data = adim->data;
1135 if (data->marker1 != NULL) {
1136 g_object_unref(data->marker1);
1137 data->marker1 = NULL;
1140 if (data->marker2 != NULL) {
1141 g_object_unref(data->marker2);
1142 data->marker2 = NULL;
1146 static gboolean
1147 _adg_get_info(AdgADim *adim, CpmlVector vector[],
1148 CpmlPair *center, gdouble *distance)
1150 AdgDim *dim;
1151 AdgADimPrivate *data;
1152 const CpmlPair *ref1, *ref2, *pos;
1153 const CpmlPair *org1, *org2;
1154 gdouble factor;
1156 dim = (AdgDim *) adim;
1157 data = adim->data;
1158 ref1 = adg_point_get_pair(adg_dim_get_ref1(dim));
1159 ref2 = adg_point_get_pair(adg_dim_get_ref2(dim));
1160 pos = adg_point_get_pair(adg_dim_get_pos(dim));
1161 org1 = adg_point_get_pair(data->org1);
1162 org2 = adg_point_get_pair(data->org2);
1164 if (ref1 == NULL || ref2 == NULL || pos == NULL ||
1165 org1 == NULL || org2 == NULL)
1166 return FALSE;
1168 vector[0].x = ref1->x - org1->x;
1169 vector[0].y = ref1->y - org1->y;
1170 vector[2].x = ref2->x - org2->x;
1171 vector[2].y = ref2->y - org2->y;
1173 factor = vector[0].x * vector[2].y - vector[0].y * vector[2].x;
1174 if (factor == 0) {
1175 /* Parallel lines: hang with an error message */
1176 g_warning(_("%s: trying to set an angular dimension on parallel lines"),
1177 G_STRLOC);
1178 return FALSE;
1181 factor = ((ref1->y - ref2->y) * vector[2].x -
1182 (ref1->x - ref2->x) * vector[2].y) / factor;
1184 center->x = ref1->x + vector[0].x * factor;
1185 center->y = ref1->y + vector[0].y * factor;
1186 *distance = cpml_pair_distance(center, pos);
1187 data->angle1 = cpml_vector_angle(&vector[0]);
1188 data->angle2 = cpml_vector_angle(&vector[2]);
1189 while (data->angle2 < data->angle1)
1190 data->angle2 += G_PI * 2;
1192 cpml_vector_from_angle(&vector[1], (data->angle1 + data->angle2) / 2);
1194 return TRUE;
1197 static CpmlPath *
1198 _adg_trail_callback(AdgTrail *trail, gpointer user_data)
1200 AdgADim *adim;
1201 AdgADimPrivate *data;
1203 adim = (AdgADim *) user_data;
1204 data = adim->data;
1206 return &data->cpml.path;