docs: updated copyright to 2012
[adg.git] / src / adg / adg-adim.c
blobbd18b9583529b8b513ace363f2aeba815665e972
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 AdgPair *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
317 * Creates a new angular dimension, specifing all the needed
318 * properties in one shot using #AdgPair.
320 * Returns: the newly created angular dimension entity
322 * Since: 1.0
324 AdgADim *
325 adg_adim_new_full(const AdgPair *ref1, const AdgPair *ref2,
326 const AdgPair *org1, const AdgPair *org2,
327 const AdgPair *pos)
329 AdgADim *adim;
330 AdgDim *dim;
332 adim = adg_adim_new();
333 dim = (AdgDim *) adim;
335 if (ref1 != NULL)
336 adg_dim_set_ref1_from_pair(dim, ref1);
338 if (ref2 != NULL)
339 adg_dim_set_ref2_from_pair(dim, ref2);
341 if (pos != NULL)
342 adg_dim_set_pos_from_pair(dim, pos);
344 if (org1 != NULL)
345 adg_adim_set_org1_from_pair(adim, org1);
347 if (org2 != NULL)
348 adg_adim_set_org2_from_pair(adim, org2);
350 return adim;
354 * adg_adim_new_full_explicit:
355 * @ref1_x: the x coordinate of end point of the first line
356 * @ref1_y: the y coordinate of end point of the first line
357 * @ref2_x: the x coordinate of end point of the second line
358 * @ref2_y: the y coordinate of end point of the second line
359 * @org1_x: the x coordinate of start point of the first line
360 * @org1_y: the y coordinate of start point of the first line
361 * @org2_x: the x coordinate of start point of the second line
362 * @org2_y: the y coordinate of start point of the second line
363 * @pos_x: the x coordinate of the position reference
364 * @pos_y: the y coordinate of the position reference
366 * Wrappes adg_adim_new_full() with explicit values.
368 * Returns: the newly created linear dimension entity
370 * Since: 1.0
372 AdgADim *
373 adg_adim_new_full_explicit(gdouble ref1_x, gdouble ref1_y,
374 gdouble ref2_x, gdouble ref2_y,
375 gdouble org1_x, gdouble org1_y,
376 gdouble org2_x, gdouble org2_y,
377 gdouble pos_x, gdouble pos_y)
379 AdgPair ref1, ref2, org1, org2, pos;
381 ref1.x = ref1_x;
382 ref1.y = ref1_y;
383 ref2.x = ref2_x;
384 ref2.y = ref2_y;
385 org1.x = org1_x;
386 org1.y = org1_y;
387 org2.x = org2_x;
388 org2.y = org2_y;
389 pos.x = pos_x;
390 pos.y = pos_y;
392 return adg_adim_new_full(&ref1, &ref2, &org1, &org2, &pos);
396 * adg_adim_new_full_from_model:
397 * @model: transfer-none: the model from which the named pairs are taken
398 * @ref1: allow-none: the end point of the first line
399 * @ref2: allow-none: the end point of the second line
400 * @org1: allow-none: the origin of the first line
401 * @org2: allow-none: the origin of the second line
402 * @pos: allow-none: the position reference
404 * Creates a new angular dimension, specifing all the needed properties
405 * in one shot and using named pairs from @model.
407 * Returns: the newly created angular dimension entity
409 * Since: 1.0
411 AdgADim *
412 adg_adim_new_full_from_model(AdgModel *model,
413 const gchar *ref1, const gchar *ref2,
414 const gchar *org1, const gchar *org2,
415 const gchar *pos)
417 AdgADim *adim;
418 AdgDim *dim;
420 adim = adg_adim_new();
421 dim = (AdgDim *) adim;
423 if (ref1 != NULL)
424 adg_dim_set_ref1_from_model(dim, model, ref1);
426 if (ref2 != NULL)
427 adg_dim_set_ref2_from_model(dim, model, ref2);
429 if (pos != NULL)
430 adg_dim_set_pos_from_model(dim, model, pos);
432 if (org1 != NULL)
433 adg_adim_set_org1_from_model(adim, model, org1);
435 if (org2 != NULL)
436 adg_adim_set_org2_from_model(adim, model, org2);
438 return adim;
442 * adg_adim_set_org1:
443 * @adim: an #AdgADim
444 * @org1: the new point to use as first reference
446 * Sets the #AdgADim:org1 property to @org1. The old point
447 * is silently discarded, unreferencing its model if that
448 * point was bound to a named pair (hence, possibly destroying
449 * the model if this was the last reference).
451 * @org1 can be %NULL, in which case the point is destroyed.
453 * Since: 1.0
455 void
456 adg_adim_set_org1(AdgADim *adim, const AdgPoint *org1)
458 g_return_if_fail(ADG_IS_ADIM(adim));
459 g_object_set(adim, "org1", org1, NULL);
463 * adg_adim_set_org1_explicit:
464 * @adim: an #AdgADim
465 * @x: x coordinate of the first reference point
466 * @y: y coordinate of the first reference point
468 * Sets the #AdgADim:org1 property to the (@x, @y) explicit
469 * coordinates. The old point is silently discarded,
470 * unreferencing its model if that point was bound to a named
471 * pair (hence, possibly destroying the model if this was the
472 * last reference).
474 * Since: 1.0
476 void
477 adg_adim_set_org1_explicit(AdgADim *adim, gdouble x, gdouble y)
479 AdgPoint *point = adg_point_new();
481 adg_point_set_pair_explicit(point, x, y);
482 adg_adim_set_org1(adim, point);
484 adg_point_destroy(point);
488 * adg_adim_set_org1_from_pair:
489 * @adim: an #AdgADim
490 * @org1: the coordinates pair of the first reference point
492 * Convenient function to set the #AdgADim:org1 property using a
493 * pair instead of explicit coordinates.
495 * Since: 1.0
497 void
498 adg_adim_set_org1_from_pair(AdgADim *adim, const AdgPair *org1)
500 g_return_if_fail(org1 != NULL);
502 adg_adim_set_org1_explicit(adim, org1->x, org1->y);
506 * adg_adim_set_org1_from_model:
507 * @adim: an #AdgADim
508 * @model: the source #AdgModel
509 * @org1: a named pair in @model
511 * Binds #AdgADim:org1 to the @org1 named pair of @model. If @model
512 * is %NULL, the point will be unset. In any case, the old point
513 * is silently discarded, unreferencing its model if that point
514 * was bound to a named pair (hence, possibly destroying the model
515 * if this was the last reference).
517 * The assignment is lazy so @org1 could be not be present in @model.
518 * Anyway, at the first access to this point an error will be raised
519 * if the named pair is still missing.
521 * Since: 1.0
523 void
524 adg_adim_set_org1_from_model(AdgADim *adim, AdgModel *model, const gchar *org1)
526 AdgPoint *point = adg_point_new();
528 adg_point_set_pair_from_model(point, model, org1);
529 adg_adim_set_org1(adim, point);
531 adg_point_destroy(point);
535 * adg_adim_get_org1:
536 * @adim: an #AdgADim
538 * Gets the #AdgADim:org1 point. The returned point is internally owned
539 * and must not be freed or modified. Anyway, it is not const because
540 * adg_point_get_pair() must be able to modify the internal cache of
541 * the returned point.
543 * Returns: the first reference point
545 * Since: 1.0
547 AdgPoint *
548 adg_adim_get_org1(AdgADim *adim)
550 AdgADimPrivate *data;
552 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
554 data = adim->data;
556 return data->org1;
560 * adg_adim_set_org2:
561 * @adim: an #AdgADim
562 * @org2: the new point to use as first reference
564 * Sets the #AdgADim:org2 property to @org2. The old point
565 * is silently discarded, unreferencing its model if that
566 * point was bound to a named pair (hence, possibly destroying
567 * the model if this was the last reference).
569 * @org2 can be %NULL, in which case the point is destroyed.
571 * Since: 1.0
573 void
574 adg_adim_set_org2(AdgADim *adim, const AdgPoint *org2)
576 g_return_if_fail(ADG_IS_ADIM(adim));
577 g_object_set(adim, "org2", org2, NULL);
581 * adg_adim_set_org2_explicit:
582 * @adim: an #AdgADim
583 * @x: x coordinate of the first reference point
584 * @y: y coordinate of the first reference point
586 * Sets the #AdgADim:org2 property to the (@x, @y) explicit
587 * coordinates. The old point is silently discarded,
588 * unreferencing its model if that point was bound to a named
589 * pair (hence, possibly destroying the model if this was the
590 * last reference).
592 * Since: 1.0
594 void
595 adg_adim_set_org2_explicit(AdgADim *adim, gdouble x, gdouble y)
597 AdgPoint *point = adg_point_new();
599 adg_point_set_pair_explicit(point, x, y);
600 adg_adim_set_org2(adim, point);
602 adg_point_destroy(point);
606 * adg_adim_set_org2_from_pair:
607 * @adim: an #AdgADim
608 * @org2: the coordinates pair of the first reference point
610 * Convenient function to set the #AdgADim:org2 property using a
611 * pair instead of explicit coordinates.
613 * Since: 1.0
615 void
616 adg_adim_set_org2_from_pair(AdgADim *adim, const AdgPair *org2)
618 g_return_if_fail(org2 != NULL);
620 adg_adim_set_org2_explicit(adim, org2->x, org2->y);
624 * adg_adim_set_org2_from_model:
625 * @adim: an #AdgADim
626 * @model: the source #AdgModel
627 * @org2: a named pair in @model
629 * Binds #AdgADim:org2 to the @org2 named pair of @model. If @model
630 * is %NULL, the point will be unset. In any case, the old point
631 * is silently discarded, unreferencing its model if that point
632 * was bound to a named pair (hence, possibly destroying the model
633 * if this was the last reference).
635 * The assignment is lazy so @org2 could be not be present in @model.
636 * Anyway, at the first access to this point an error will be raised
637 * if the named pair is still missing.
639 * Since: 1.0
641 void
642 adg_adim_set_org2_from_model(AdgADim *adim, AdgModel *model, const gchar *org2)
644 AdgPoint *point = adg_point_new();
646 adg_point_set_pair_from_model(point, model, org2);
647 adg_adim_set_org2(adim, point);
649 adg_point_destroy(point);
653 * adg_adim_get_org2:
654 * @adim: an #AdgADim
656 * Gets the #AdgADim:org2 point. The returned point is internally owned
657 * and must not be freed or modified. Anyway, it is not const because
658 * adg_point_get_pair() must be able to modify the internal cache of
659 * the returned point.
661 * Returns: the first reference point
663 * Since: 1.0
665 AdgPoint *
666 adg_adim_get_org2(AdgADim *adim)
668 AdgADimPrivate *data;
670 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
672 data = adim->data;
674 return data->org2;
678 * adg_adim_switch_extension1:
679 * @adim: an #AdgADim entity
680 * @new_state: the new state
682 * Shows (if @new_state is %TRUE) or hides (if @new_state is %FALSE)
683 * the first extension line of @adim.
685 * Since: 1.0
687 void
688 adg_adim_switch_extension1(AdgADim *adim, gboolean new_state)
690 g_return_if_fail(ADG_IS_ADIM(adim));
691 g_return_if_fail(adg_is_boolean_value(new_state));
692 g_object_set(adim, "has-extension1", new_state, NULL);
696 * adg_adim_has_extension1:
697 * @adim: an #AdgADim entity
699 * Checks if @adim should render the first extension line.
701 * Returns: %TRUE on first extension line presents, %FALSE otherwise
703 * Since: 1.0
705 gboolean
706 adg_adim_has_extension1(AdgADim *adim)
708 AdgADimPrivate *data;
710 g_return_val_if_fail(ADG_IS_ADIM(adim), FALSE);
712 data = adim->data;
714 return data->has_extension1;
718 * adg_adim_switch_extension2:
719 * @adim: an #AdgADim entity
720 * @new_state: the new new_state
722 * Shows (if @new_state is %TRUE) or hides (if @new_state is %FALSE)
723 * the second extension line of @adim.
725 * Since: 1.0
727 void
728 adg_adim_switch_extension2(AdgADim *adim, gboolean new_state)
730 g_return_if_fail(ADG_IS_ADIM(adim));
731 g_return_if_fail(adg_is_boolean_value(new_state));
732 g_object_set(adim, "has-extension2", new_state, NULL);
736 * adg_adim_has_extension2:
737 * @adim: an #AdgADim entity
739 * Checks if @adim should render the second extension line.
741 * Returns: %TRUE on first extension line presents, %FALSE otherwise
743 * Since: 1.0
745 gboolean
746 adg_adim_has_extension2(AdgADim *adim)
748 AdgADimPrivate *data;
750 g_return_val_if_fail(ADG_IS_ADIM(adim), FALSE);
752 data = adim->data;
754 return data->has_extension2;
758 static void
759 _adg_global_changed(AdgEntity *entity)
761 AdgADimPrivate *data = ((AdgADim *) entity)->data;
763 _adg_unset_trail((AdgADim *) entity);
765 if (_ADG_OLD_ENTITY_CLASS->global_changed)
766 _ADG_OLD_ENTITY_CLASS->global_changed(entity);
768 if (data->marker1 != NULL)
769 adg_entity_global_changed((AdgEntity *) data->marker1);
771 if (data->marker2 != NULL)
772 adg_entity_global_changed((AdgEntity *) data->marker2);
775 static void
776 _adg_local_changed(AdgEntity *entity)
778 _adg_unset_trail((AdgADim *) entity);
780 if (_ADG_OLD_ENTITY_CLASS->local_changed)
781 _ADG_OLD_ENTITY_CLASS->local_changed(entity);
784 static void
785 _adg_invalidate(AdgEntity *entity)
787 AdgADim *adim;
788 AdgADimPrivate *data;
790 adim = (AdgADim *) entity;
791 data = adim->data;
793 _adg_dispose_trail(adim);
794 _adg_dispose_markers(adim);
795 data->geometry_arranged = FALSE;
796 _adg_unset_trail(adim);
798 if (data->org1)
799 adg_point_invalidate(data->org1);
800 if (data->org2)
801 adg_point_invalidate(data->org2);
803 if (_ADG_OLD_ENTITY_CLASS->invalidate)
804 _ADG_OLD_ENTITY_CLASS->invalidate(entity);
807 static void
808 _adg_arrange(AdgEntity *entity)
810 AdgADim *adim;
811 AdgDim *dim;
812 AdgADimPrivate *data;
813 AdgAlignment *quote;
814 const AdgMatrix *global, *local;
815 AdgPair ref1, ref2, base1, base12, base2;
816 AdgPair pair;
817 CpmlExtents extents;
818 AdgEntity *marker_entity;
820 if (_ADG_OLD_ENTITY_CLASS->arrange)
821 _ADG_OLD_ENTITY_CLASS->arrange(entity);
823 adim = (AdgADim *) entity;
825 if (!_adg_update_geometry(adim))
826 return;
828 dim = (AdgDim *) adim;
829 data = adim->data;
830 quote = adg_dim_get_quote(dim);
832 _adg_update_entities(adim);
834 if (data->cpml.path.status == CAIRO_STATUS_SUCCESS) {
835 AdgEntity *quote_entity = (AdgEntity *) quote;
836 adg_entity_set_global_map(quote_entity, &data->quote.global_map);
837 return;
840 global = adg_entity_get_global_matrix(entity);
841 local = adg_entity_get_local_matrix(entity);
842 extents.is_defined = FALSE;
844 cpml_pair_copy(&ref1, adg_point_get_pair(adg_dim_get_ref1(dim)));
845 cpml_pair_copy(&ref2, adg_point_get_pair(adg_dim_get_ref2(dim)));
846 cpml_pair_copy(&base1, &data->point.base1);
847 cpml_pair_copy(&base12, &data->point.base12);
848 cpml_pair_copy(&base2, &data->point.base2);
850 /* Apply the local matrix to the relevant points */
851 cpml_pair_transform(&ref1, local);
852 cpml_pair_transform(&ref2, local);
853 cpml_pair_transform(&base1, local);
854 cpml_pair_transform(&base12, local);
855 cpml_pair_transform(&base2, local);
857 /* Combine points and global shifts to build the path */
858 pair.x = ref1.x + data->shift.from1.x;
859 pair.y = ref1.y + data->shift.from1.y;
860 cpml_pair_to_cairo(&pair, &data->cpml.data[6]);
862 pair.x = base1.x + data->shift.base1.x;
863 pair.y = base1.y + data->shift.base1.y;
864 cpml_pair_to_cairo(&pair, &data->cpml.data[1]);
866 pair.x += data->shift.to1.x;
867 pair.y += data->shift.to1.y;
868 cpml_pair_to_cairo(&pair, &data->cpml.data[8]);
870 pair.x = base12.x + data->shift.base12.x;
871 pair.y = base12.y + data->shift.base12.y;
872 cpml_pair_to_cairo(&pair, &data->cpml.data[3]);
874 pair.x = ref2.x + data->shift.from2.x;
875 pair.y = ref2.y + data->shift.from2.y;
876 cpml_pair_to_cairo(&pair, &data->cpml.data[10]);
878 pair.x = base2.x + data->shift.base2.x;
879 pair.y = base2.y + data->shift.base2.y;
880 cpml_pair_to_cairo(&pair, &data->cpml.data[4]);
882 pair.x += data->shift.to2.x;
883 pair.y += data->shift.to2.y;
884 cpml_pair_to_cairo(&pair, &data->cpml.data[12]);
886 /* Play with header lengths to show or hide the extension lines */
887 if (data->has_extension1) {
888 data->cpml.data[7].header.length = data->has_extension2 ? 2 : 6;
889 } else {
890 data->cpml.data[2].header.length = data->has_extension2 ? 7 : 11;
893 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
895 /* Arrange the quote */
896 if (quote != NULL) {
897 AdgEntity *quote_entity;
898 gdouble angle;
899 AdgMatrix map;
901 quote_entity = (AdgEntity *) quote;
902 angle = adg_dim_quote_angle(dim, (data->angle1 + data->angle2) / 2 + G_PI_2);
903 cpml_pair_from_cairo(&pair, &data->cpml.data[3]);
905 adg_alignment_set_factor_explicit(quote, 0.5, 0);
907 cairo_matrix_init_translate(&map, pair.x, pair.y);
908 cairo_matrix_rotate(&map, angle);
909 adg_entity_set_global_map(quote_entity, &map);
910 adg_entity_arrange(quote_entity);
911 cpml_extents_add(&extents, adg_entity_get_extents(quote_entity));
913 adg_matrix_copy(&data->quote.global_map, &map);
916 /* Arrange the trail */
917 if (data->trail != NULL) {
918 CpmlExtents trail_extents;
919 cpml_extents_copy(&trail_extents, adg_trail_get_extents(data->trail));
920 cpml_extents_transform(&trail_extents, global);
921 cpml_extents_add(&extents, &trail_extents);
922 } else {
923 _adg_dispose_markers(adim);
926 /* Arrange the markers */
927 if (data->marker1 != NULL) {
928 marker_entity = (AdgEntity *) data->marker1;
929 adg_marker_set_segment(data->marker1, data->trail, 1);
930 adg_entity_local_changed(marker_entity);
931 adg_entity_arrange(marker_entity);
932 cpml_extents_add(&extents, adg_entity_get_extents(marker_entity));
934 if (data->marker2 != NULL) {
935 marker_entity = (AdgEntity *) data->marker2;
936 adg_marker_set_segment(data->marker2, data->trail, 1);
937 adg_entity_local_changed(marker_entity);
938 adg_entity_arrange(marker_entity);
939 cpml_extents_add(&extents, adg_entity_get_extents(marker_entity));
942 adg_entity_set_extents(entity, &extents);
945 static void
946 _adg_render(AdgEntity *entity, cairo_t *cr)
948 AdgADim *adim;
949 AdgDim *dim;
950 AdgADimPrivate *data;
951 AdgDimStyle *dim_style;
952 AdgDress dress;
953 const cairo_path_t *cairo_path;
955 adim = (AdgADim *) entity;
956 data = adim->data;
958 if (!data->geometry_arranged) {
959 /* Entity not arranged, probably due to undefined pair found */
960 return;
963 dim = (AdgDim *) entity;
964 dim_style = _ADG_GET_DIM_STYLE(dim);
966 adg_style_apply((AdgStyle *) dim_style, entity, cr);
967 adg_entity_render((AdgEntity *) adg_dim_get_quote(dim), cr);
969 if (data->marker1 != NULL)
970 adg_entity_render((AdgEntity *) data->marker1, cr);
971 if (data->marker2 != NULL)
972 adg_entity_render((AdgEntity *) data->marker2, cr);
974 cairo_transform(cr, adg_entity_get_global_matrix(entity));
975 dress = adg_dim_style_get_line_dress(dim_style);
976 adg_entity_apply_dress(entity, dress, cr);
978 cairo_path = adg_trail_get_cairo_path(data->trail);
979 cairo_append_path(cr, cairo_path);
980 cairo_stroke(cr);
983 static gchar *
984 _adg_default_value(AdgDim *dim)
986 AdgADim *adim;
987 AdgADimPrivate *data;
988 AdgDimStyle *dim_style;
989 gdouble angle;
990 const gchar *format;
992 adim = (AdgADim *) dim;
993 data = adim->data;
994 dim_style = _ADG_GET_DIM_STYLE(dim);
995 format = adg_dim_style_get_number_format(dim_style);
997 if (!_adg_update_geometry(adim))
998 return g_strdup("undef");
1000 angle = (data->angle2 - data->angle1) * 180 / G_PI;
1001 return g_strdup_printf(format, angle);
1004 /* With "geometry" is considered any data (point, vector or angle)
1005 * that can be cached: this is strictly related on how the arrange()
1006 * method works */
1007 static gboolean
1008 _adg_update_geometry(AdgADim *adim)
1010 AdgADimPrivate *data;
1011 AdgDimStyle *dim_style;
1012 gdouble from_offset, to_offset;
1013 gdouble spacing, level;
1014 CpmlVector vector[3];
1015 CpmlPair center;
1016 gdouble distance;
1018 data = adim->data;
1020 if (data->geometry_arranged)
1021 return TRUE;
1022 else if (!_adg_get_info(adim, vector, &center, &distance))
1023 return FALSE;
1025 dim_style = _ADG_GET_DIM_STYLE(adim);
1026 from_offset = adg_dim_style_get_from_offset(dim_style);
1027 to_offset = adg_dim_style_get_to_offset(dim_style);
1028 spacing = adg_dim_style_get_baseline_spacing(dim_style);
1029 level = adg_dim_get_level((AdgDim *) adim);
1031 /* shift.from1 */
1032 cpml_vector_set_length(&vector[0], from_offset);
1033 cpml_pair_copy(&data->shift.from1, &vector[0]);
1035 /* shift.base1 */
1036 cpml_vector_set_length(&vector[0], level * spacing);
1037 cpml_pair_copy(&data->shift.base1, &vector[0]);
1039 /* shift.to1 */
1040 cpml_vector_set_length(&vector[0], to_offset);
1041 cpml_pair_copy(&data->shift.to1, &vector[0]);
1043 /* shift.from2 */
1044 cpml_vector_set_length(&vector[2], from_offset);
1045 cpml_pair_copy(&data->shift.from2, &vector[2]);
1047 /* shift.base2 */
1048 cpml_vector_set_length(&vector[2], level * spacing);
1049 cpml_pair_copy(&data->shift.base2, &vector[2]);
1051 /* shift.to2 */
1052 cpml_vector_set_length(&vector[2], to_offset);
1053 cpml_pair_copy(&data->shift.to2, &vector[2]);
1055 /* shift.base12 */
1056 cpml_vector_set_length(&vector[1], level * spacing);
1057 cpml_pair_copy(&data->shift.base12, &vector[1]);
1059 /* Distance can be 0, so the following will leave the
1060 * vector array in undefined state */
1062 /* point.base1 */
1063 cpml_vector_set_length(&vector[0], distance);
1064 data->point.base1.x = vector[0].x + center.x;
1065 data->point.base1.y = vector[0].y + center.y;
1067 /* point.base2 */
1068 cpml_vector_set_length(&vector[2], distance);
1069 data->point.base2.x = vector[2].x + center.x;
1070 data->point.base2.y = vector[2].y + center.y;
1072 /* point.base12 */
1073 cpml_vector_set_length(&vector[1], distance);
1074 data->point.base12.x = vector[1].x + center.x;
1075 data->point.base12.y = vector[1].y + center.y;
1077 data->geometry_arranged = TRUE;
1079 return TRUE;
1082 static void
1083 _adg_update_entities(AdgADim *adim)
1085 AdgEntity *entity;
1086 AdgADimPrivate *data;
1087 AdgDimStyle *dim_style;
1089 entity = (AdgEntity *) adim;
1090 data = adim->data;
1091 dim_style = _ADG_GET_DIM_STYLE(adim);
1093 if (data->trail == NULL)
1094 data->trail = adg_trail_new(_adg_trail_callback, adim);
1096 if (data->marker1 == NULL) {
1097 data->marker1 = adg_dim_style_marker1_new(dim_style);
1098 adg_entity_set_parent((AdgEntity *) data->marker1, entity);
1101 if (data->marker2 == NULL) {
1102 data->marker2 = adg_dim_style_marker2_new(dim_style);
1103 adg_entity_set_parent((AdgEntity *) data->marker2, entity);
1107 static void
1108 _adg_unset_trail(AdgADim *adim)
1110 AdgADimPrivate *data = adim->data;
1112 if (data->trail != NULL)
1113 adg_model_clear((AdgModel *) data->trail);
1115 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
1118 static void
1119 _adg_dispose_trail(AdgADim *adim)
1121 AdgADimPrivate *data = adim->data;
1123 if (data->trail != NULL) {
1124 g_object_unref(data->trail);
1125 data->trail = NULL;
1129 static void
1130 _adg_dispose_markers(AdgADim *adim)
1132 AdgADimPrivate *data = adim->data;
1134 if (data->marker1 != NULL) {
1135 g_object_unref(data->marker1);
1136 data->marker1 = NULL;
1139 if (data->marker2 != NULL) {
1140 g_object_unref(data->marker2);
1141 data->marker2 = NULL;
1145 static gboolean
1146 _adg_get_info(AdgADim *adim, CpmlVector vector[],
1147 AdgPair *center, gdouble *distance)
1149 AdgDim *dim;
1150 AdgADimPrivate *data;
1151 const AdgPair *ref1, *ref2, *pos;
1152 const AdgPair *org1, *org2;
1153 gdouble factor;
1155 dim = (AdgDim *) adim;
1156 data = adim->data;
1157 ref1 = adg_point_get_pair(adg_dim_get_ref1(dim));
1158 ref2 = adg_point_get_pair(adg_dim_get_ref2(dim));
1159 pos = adg_point_get_pair(adg_dim_get_pos(dim));
1160 org1 = adg_point_get_pair(data->org1);
1161 org2 = adg_point_get_pair(data->org2);
1163 if (ref1 == NULL || ref2 == NULL || pos == NULL ||
1164 org1 == NULL || org2 == NULL)
1165 return FALSE;
1167 vector[0].x = ref1->x - org1->x;
1168 vector[0].y = ref1->y - org1->y;
1169 vector[2].x = ref2->x - org2->x;
1170 vector[2].y = ref2->y - org2->y;
1172 factor = vector[0].x * vector[2].y - vector[0].y * vector[2].x;
1173 if (factor == 0) {
1174 /* Parallel lines: hang with an error message */
1175 g_warning(_("%s: trying to set an angular dimension on parallel lines"),
1176 G_STRLOC);
1177 return FALSE;
1180 factor = ((ref1->y - ref2->y) * vector[2].x -
1181 (ref1->x - ref2->x) * vector[2].y) / factor;
1183 center->x = ref1->x + vector[0].x * factor;
1184 center->y = ref1->y + vector[0].y * factor;
1185 *distance = cpml_pair_distance(center, pos);
1186 data->angle1 = cpml_vector_angle(&vector[0]);
1187 data->angle2 = cpml_vector_angle(&vector[2]);
1188 while (data->angle2 < data->angle1)
1189 data->angle2 += G_PI * 2;
1191 cpml_vector_from_angle(&vector[1], (data->angle1 + data->angle2) / 2);
1193 return TRUE;
1196 static CpmlPath *
1197 _adg_trail_callback(AdgTrail *trail, gpointer user_data)
1199 AdgADim *adim;
1200 AdgADimPrivate *data;
1202 adim = (AdgADim *) user_data;
1203 data = adim->data;
1205 return &data->cpml.path;