Tested build process under ss-dev of OpenIndiana
[adg.git] / src / adg / adg-adim.c
blob825aa5d2d4fd2daccc449a5377d1b23c28bf5d08
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:adg-adim
23 * @short_description: Angular dimensions
25 * The #AdgADim entity defines an angular dimension.
28 /**
29 * AdgADim:
31 * All fields are privates and should not be used directly.
32 * Use its public methods instead.
33 **/
36 #include "adg-internal.h"
37 #include "adg-container.h"
38 #include "adg-alignment.h"
39 #include "adg-model.h"
40 #include "adg-point.h"
41 #include "adg-trail.h"
42 #include "adg-marker.h"
43 #include "adg-style.h"
44 #include "adg-dim-style.h"
45 #include "adg-textual.h"
46 #include "adg-toy-text.h"
47 #include "adg-dim.h"
48 #include "adg-dim-private.h"
49 #include <math.h>
51 #include "adg-adim.h"
52 #include "adg-adim-private.h"
55 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_adim_parent_class)
56 #define _ADG_OLD_ENTITY_CLASS ((AdgEntityClass *) adg_adim_parent_class)
59 G_DEFINE_TYPE(AdgADim, adg_adim, ADG_TYPE_DIM)
61 enum {
62 PROP_0,
63 PROP_VALUE,
64 PROP_ORG1,
65 PROP_ORG2,
66 PROP_HAS_EXTENSION1,
67 PROP_HAS_EXTENSION2
71 static void _adg_dispose (GObject *object);
72 static void _adg_get_property (GObject *object,
73 guint param_id,
74 GValue *value,
75 GParamSpec *pspec);
76 static void _adg_set_property (GObject *object,
77 guint param_id,
78 const GValue *value,
79 GParamSpec *pspec);
80 static void _adg_global_changed (AdgEntity *entity);
81 static void _adg_local_changed (AdgEntity *entity);
82 static void _adg_invalidate (AdgEntity *entity);
83 static void _adg_arrange (AdgEntity *entity);
84 static void _adg_render (AdgEntity *entity,
85 cairo_t *cr);
86 static gchar * _adg_default_value (AdgDim *dim);
87 static gboolean _adg_update_geometry (AdgADim *adim);
88 static void _adg_update_entities (AdgADim *adim);
89 static void _adg_unset_trail (AdgADim *adim);
90 static void _adg_dispose_markers (AdgADim *adim);
91 static gboolean _adg_get_info (AdgADim *adim,
92 CpmlVector vector[],
93 AdgPair *center,
94 gdouble *distance);
95 static CpmlPath * _adg_trail_callback (AdgTrail *trail,
96 gpointer user_data);
99 static void
100 adg_adim_class_init(AdgADimClass *klass)
102 GObjectClass *gobject_class;
103 AdgEntityClass *entity_class;
104 AdgDimClass *dim_class;
105 GParamSpec *param, *old_param;
107 gobject_class = (GObjectClass *) klass;
108 entity_class = (AdgEntityClass *) klass;
109 dim_class = (AdgDimClass *) klass;
111 g_type_class_add_private(klass, sizeof(AdgADimPrivate));
113 gobject_class->dispose = _adg_dispose;
114 gobject_class->get_property = _adg_get_property;
115 gobject_class->set_property = _adg_set_property;
117 entity_class->global_changed = _adg_global_changed;
118 entity_class->local_changed = _adg_local_changed;
119 entity_class->invalidate = _adg_invalidate;
120 entity_class->arrange = _adg_arrange;
121 entity_class->render = _adg_render;
123 dim_class->default_value = _adg_default_value;
125 /* Override #AdgDim:value to append a degree symbol
126 * to the default set value template string */
127 old_param = g_object_class_find_property(gobject_class, "value");
128 param = g_param_spec_string(old_param->name,
129 g_param_spec_get_nick(old_param),
130 g_param_spec_get_blurb(old_param),
131 "<>" ADG_UTF8_DEGREE,
132 old_param->flags);
133 g_object_class_install_property(gobject_class, PROP_VALUE, param);
135 param = g_param_spec_boxed("org1",
136 P_("First Origin"),
137 P_("Where the first line comes from: this point is used toghether with \"ref1\" to align the first extension line"),
138 ADG_TYPE_POINT,
139 G_PARAM_READWRITE);
140 g_object_class_install_property(gobject_class, PROP_ORG1, param);
142 param = g_param_spec_boxed("org2",
143 P_("Second Origin"),
144 P_("Where the second line comes from: this point is used toghether with \"ref2\" to align the second extension line"),
145 ADG_TYPE_POINT,
146 G_PARAM_READWRITE);
147 g_object_class_install_property(gobject_class, PROP_ORG2, param);
149 param = g_param_spec_boolean("has-extension1",
150 P_("Has First Extension Line flag"),
151 P_("Show (TRUE) or hide (FALSE) the first extension line"),
152 TRUE, G_PARAM_READWRITE);
153 g_object_class_install_property(gobject_class, PROP_HAS_EXTENSION1, param);
155 param = g_param_spec_boolean("has-extension2",
156 P_("Has Second Extension Line flag"),
157 P_("Show (TRUE) or hide (FALSE) the second extension line"),
158 TRUE, G_PARAM_READWRITE);
159 g_object_class_install_property(gobject_class, PROP_HAS_EXTENSION2, param);
162 static void
163 adg_adim_init(AdgADim *adim)
165 AdgADimPrivate *data;
166 cairo_path_data_t move_to, line_to, arc_to;
168 data = G_TYPE_INSTANCE_GET_PRIVATE(adim, ADG_TYPE_ADIM, AdgADimPrivate);
169 move_to.header.type = CPML_MOVE;
170 move_to.header.length = 2;
171 line_to.header.type = CPML_LINE;
172 line_to.header.length = 2;
173 arc_to.header.type = CPML_ARC;
174 arc_to.header.length = 3;
176 data->org1 = NULL;
177 data->org2 = NULL;
178 data->has_extension1 = TRUE;
179 data->has_extension2 = TRUE;
181 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
182 data->cpml.path.data = data->cpml.data;
183 data->cpml.path.num_data = G_N_ELEMENTS(data->cpml.data);
184 data->cpml.path.data[0] = move_to;
185 data->cpml.path.data[2] = arc_to;
186 data->cpml.path.data[5] = move_to;
187 data->cpml.path.data[7] = line_to;
188 data->cpml.path.data[9] = move_to;
189 data->cpml.path.data[11] = line_to;
191 data->trail = NULL;
192 data->marker1 = NULL;
193 data->marker2 = NULL;
195 data->geometry_arranged = FALSE;
197 adim->data = data;
200 static void
201 _adg_dispose(GObject *object)
203 AdgEntity *entity;
204 AdgADimPrivate *data;
206 entity = (AdgEntity *) object;
207 data = ((AdgADim *) object)->data;
209 _adg_dispose_markers((AdgADim *) object);
211 if (data->org1 != NULL)
212 data->org1 = adg_entity_point(entity, data->org1, NULL);
214 if (data->org2 != NULL)
215 data->org2 = adg_entity_point(entity, data->org2, NULL);
217 if (_ADG_OLD_OBJECT_CLASS->dispose)
218 _ADG_OLD_OBJECT_CLASS->dispose(object);
221 static void
222 _adg_get_property(GObject *object, guint prop_id,
223 GValue *value, GParamSpec *pspec)
225 AdgADimPrivate *data = ((AdgADim *) object)->data;
227 switch (prop_id) {
228 case PROP_VALUE:
229 g_value_set_string(value, adg_dim_get_value((AdgDim *) object));
230 break;
231 case PROP_ORG1:
232 g_value_set_boxed(value, data->org1);
233 break;
234 case PROP_ORG2:
235 g_value_set_boxed(value, data->org2);
236 break;
237 case PROP_HAS_EXTENSION1:
238 g_value_set_boolean(value, data->has_extension1);
239 break;
240 case PROP_HAS_EXTENSION2:
241 g_value_set_boolean(value, data->has_extension2);
242 break;
243 default:
244 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
245 break;
249 static void
250 _adg_set_property(GObject *object, guint prop_id,
251 const GValue *value, GParamSpec *pspec)
253 AdgEntity *entity;
254 AdgADimPrivate *data;
256 entity = (AdgEntity *) object;
257 data = ((AdgADim *) object)->data;
259 switch (prop_id) {
260 case PROP_VALUE:
261 adg_dim_set_value((AdgDim *) object, g_value_get_string(value));
262 break;
263 case PROP_ORG1:
264 data->org1 = adg_entity_point(entity, data->org1,
265 g_value_get_boxed(value));
266 break;
267 case PROP_ORG2:
268 data->org2 = adg_entity_point(entity, data->org2,
269 g_value_get_boxed(value));
270 break;
271 case PROP_HAS_EXTENSION1:
272 data->has_extension1 = g_value_get_boolean(value);
273 break;
274 case PROP_HAS_EXTENSION2:
275 data->has_extension2 = g_value_get_boolean(value);
276 break;
277 default:
278 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
279 break;
285 * adg_adim_new:
287 * Creates a new - undefined - angular dimension. You must, at least,
288 * define the first line by setting #AdgADim:org1 (start point) and
289 * #AdgDim:ref1 (end point), the second line by setting #AdgADim:org2
290 * (start point) and #AdgDim:ref2 (end point) and the position of
291 * the quote in #AdgDim:pos.
293 * Returns: the newly created angular dimension entity
295 AdgADim *
296 adg_adim_new(void)
298 return g_object_new(ADG_TYPE_ADIM, NULL);
302 * adg_adim_new_full:
303 * @ref1: first reference point
304 * @ref2: second reference point
305 * @org1: first origin point
306 * @org2: second origin point
308 * Creates a new angular dimension, specifing all the needed
309 * properties in one shot using #AdgPair.
311 * Returns: the newly created angular dimension entity
313 AdgADim *
314 adg_adim_new_full(const AdgPair *ref1, const AdgPair *ref2,
315 const AdgPair *org1, const AdgPair *org2,
316 const AdgPair *pos)
318 AdgADim *adim;
319 AdgDim *dim;
321 adim = g_object_new(ADG_TYPE_ADIM, NULL);
322 dim = (AdgDim *) adim;
324 adg_dim_set_ref1_from_pair(dim, ref1);
325 adg_dim_set_ref2_from_pair(dim, ref2);
326 adg_dim_set_pos_from_pair(dim, pos);
327 adg_adim_set_org1_from_pair(adim, org1);
328 adg_adim_set_org2_from_pair(adim, org2);
330 return adim;
334 * adg_adim_new_full_explicit:
335 * @ref1_x: the x coordinate of the first reference point
336 * @ref1_y: the y coordinate of the first reference point
337 * @ref2_x: the x coordinate of the second reference point
338 * @ref2_y: the y coordinate of the second reference point
339 * @direction: angle where to extend the dimension
340 * @pos_x: the x coordinate of the position reference
341 * @pos_y: the y coordinate of the position reference
343 * Wrappes adg_adim_new_full() with explicit values.
345 * Returns: the newly created linear dimension entity
347 AdgADim *
348 adg_adim_new_full_explicit(gdouble ref1_x, gdouble ref1_y,
349 gdouble ref2_x, gdouble ref2_y,
350 gdouble org1_x, gdouble org1_y,
351 gdouble org2_x, gdouble org2_y,
352 gdouble pos_x, gdouble pos_y)
354 AdgPair ref1, ref2, org1, org2, pos;
356 ref1.x = ref1_x;
357 ref1.y = ref1_y;
358 ref2.x = ref2_x;
359 ref2.y = ref2_y;
360 org1.x = org1_x;
361 org1.y = org1_y;
362 org2.x = org2_x;
363 org2.y = org2_y;
364 pos.x = pos_x;
365 pos.y = pos_y;
367 return adg_adim_new_full(&ref1, &ref2, &org1, &org2, &pos);
371 * adg_adim_new_full_from_model:
372 * @model: the model from which the named pairs are taken
373 * @ref1: the end point of the first line
374 * @ref2: the end point of the second line
375 * @org1: the origin of the first line
376 * @org2: the origin of the second line
377 * @pos: the position reference
379 * Creates a new angular dimension, specifing all the needed properties
380 * in one shot and using named pairs from @model.
382 * Returns: the newly created angular dimension entity
384 AdgADim *
385 adg_adim_new_full_from_model(AdgModel *model,
386 const gchar *ref1, const gchar *ref2,
387 const gchar *org1, const gchar *org2,
388 const gchar *pos)
390 AdgADim *adim;
391 AdgDim *dim;
393 adim = g_object_new(ADG_TYPE_ADIM, NULL);
394 dim = (AdgDim *) adim;
396 adg_dim_set_ref1_from_model(dim, model, ref1);
397 adg_dim_set_ref2_from_model(dim, model, ref2);
398 adg_dim_set_pos_from_model(dim, model, pos);
399 adg_adim_set_org1_from_model(adim, model, org1);
400 adg_adim_set_org2_from_model(adim, model, org2);
402 return adim;
406 * adg_adim_set_org1:
407 * @adim: an #AdgADim
408 * @org1: the new point to use as first reference
410 * Sets the #AdgADim:org1 property to @org1. The old point
411 * is silently discarded, unreferencing its model if that
412 * point was bound to a named pair (hence, possibly destroying
413 * the model if this was the last reference).
415 * @org1 can be %NULL, in which case the point is destroyed.
417 void
418 adg_adim_set_org1(AdgADim *adim, const AdgPoint *org1)
420 g_return_if_fail(ADG_IS_ADIM(adim));
421 g_object_set(adim, "org1", org1, NULL);
425 * adg_adim_set_org1_explicit:
426 * @adim: an #AdgADim
427 * @x: x coordinate of the first reference point
428 * @y: y coordinate of the first reference point
430 * Sets the #AdgADim:org1 property to the (@x, @y) explicit
431 * coordinates. The old point is silently discarded,
432 * unreferencing its model if that point was bound to a named
433 * pair (hence, possibly destroying the model if this was the
434 * last reference).
436 void
437 adg_adim_set_org1_explicit(AdgADim *adim, gdouble x, gdouble y)
439 AdgPoint *point = adg_point_new();
441 adg_point_set_pair_explicit(point, x, y);
442 adg_adim_set_org1(adim, point);
444 adg_point_destroy(point);
448 * adg_adim_set_org1_from_pair:
449 * @adim: an #AdgADim
450 * @org1: the coordinates pair of the first reference point
452 * Convenient function to set the #AdgADim:org1 property using a
453 * pair instead of explicit coordinates.
455 void
456 adg_adim_set_org1_from_pair(AdgADim *adim, const AdgPair *org1)
458 g_return_if_fail(org1 != NULL);
460 adg_adim_set_org1_explicit(adim, org1->x, org1->y);
464 * adg_adim_set_org1_from_model:
465 * @adim: an #AdgADim
466 * @model: the source #AdgModel
467 * @org1: a named pair in @model
469 * Binds #AdgADim:org1 to the @org1 named pair of @model. If @model
470 * is %NULL, the point will be unset. In any case, the old point
471 * is silently discarded, unreferencing its model if that point
472 * was bound to a named pair (hence, possibly destroying the model
473 * if this was the last reference).
475 * The assignment is lazy so @org1 could be not be present in @model.
476 * Anyway, at the first access to this point an error will be raised
477 * if the named pair is still missing.
479 void
480 adg_adim_set_org1_from_model(AdgADim *adim, AdgModel *model, const gchar *org1)
482 AdgPoint *point = adg_point_new();
484 adg_point_set_pair_from_model(point, model, org1);
485 adg_adim_set_org1(adim, point);
487 adg_point_destroy(point);
491 * adg_adim_get_org1:
492 * @adim: an #AdgADim
494 * Gets the #AdgADim:org1 point. The returned point is internally owned
495 * and must not be freed or modified. Anyway, it is not const because
496 * adg_point_get_pair() must be able to modify the internal cache of
497 * the returned point.
499 * Returns: the first reference point
501 AdgPoint *
502 adg_adim_get_org1(AdgADim *adim)
504 AdgADimPrivate *data;
506 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
508 data = adim->data;
510 return data->org1;
514 * adg_adim_set_org2:
515 * @adim: an #AdgADim
516 * @org2: the new point to use as first reference
518 * Sets the #AdgADim:org2 property to @org2. The old point
519 * is silently discarded, unreferencing its model if that
520 * point was bound to a named pair (hence, possibly destroying
521 * the model if this was the last reference).
523 * @org2 can be %NULL, in which case the point is destroyed.
525 void
526 adg_adim_set_org2(AdgADim *adim, const AdgPoint *org2)
528 g_return_if_fail(ADG_IS_ADIM(adim));
529 g_object_set(adim, "org2", org2, NULL);
533 * adg_adim_set_org2_explicit:
534 * @adim: an #AdgADim
535 * @x: x coordinate of the first reference point
536 * @y: y coordinate of the first reference point
538 * Sets the #AdgADim:org2 property to the (@x, @y) explicit
539 * coordinates. The old point is silently discarded,
540 * unreferencing its model if that point was bound to a named
541 * pair (hence, possibly destroying the model if this was the
542 * last reference).
544 void
545 adg_adim_set_org2_explicit(AdgADim *adim, gdouble x, gdouble y)
547 AdgPoint *point = adg_point_new();
549 adg_point_set_pair_explicit(point, x, y);
550 adg_adim_set_org2(adim, point);
552 adg_point_destroy(point);
556 * adg_adim_set_org2_from_pair:
557 * @adim: an #AdgADim
558 * @org2: the coordinates pair of the first reference point
560 * Convenient function to set the #AdgADim:org2 property using a
561 * pair instead of explicit coordinates.
563 void
564 adg_adim_set_org2_from_pair(AdgADim *adim, const AdgPair *org2)
566 g_return_if_fail(org2 != NULL);
568 adg_adim_set_org2_explicit(adim, org2->x, org2->y);
572 * adg_adim_set_org2_from_model:
573 * @adim: an #AdgADim
574 * @model: the source #AdgModel
575 * @org2: a named pair in @model
577 * Binds #AdgADim:org2 to the @org2 named pair of @model. If @model
578 * is %NULL, the point will be unset. In any case, the old point
579 * is silently discarded, unreferencing its model if that point
580 * was bound to a named pair (hence, possibly destroying the model
581 * if this was the last reference).
583 * The assignment is lazy so @org2 could be not be present in @model.
584 * Anyway, at the first access to this point an error will be raised
585 * if the named pair is still missing.
587 void
588 adg_adim_set_org2_from_model(AdgADim *adim, AdgModel *model, const gchar *org2)
590 AdgPoint *point = adg_point_new();
592 adg_point_set_pair_from_model(point, model, org2);
593 adg_adim_set_org2(adim, point);
595 adg_point_destroy(point);
599 * adg_adim_get_org2:
600 * @adim: an #AdgADim
602 * Gets the #AdgADim:org2 point. The returned point is internally owned
603 * and must not be freed or modified. Anyway, it is not const because
604 * adg_point_get_pair() must be able to modify the internal cache of
605 * the returned point.
607 * Returns: the first reference point
609 AdgPoint *
610 adg_adim_get_org2(AdgADim *adim)
612 AdgADimPrivate *data;
614 g_return_val_if_fail(ADG_IS_ADIM(adim), NULL);
616 data = adim->data;
618 return data->org2;
622 * adg_adim_switch_extension1:
623 * @adim: an #AdgADim entity
624 * @new_state: the new state
626 * Shows (if @new_state is %TRUE) or hides (if @new_state is %FALSE)
627 * the first extension line of @adim.
629 void
630 adg_adim_switch_extension1(AdgADim *adim, gboolean new_state)
632 g_return_if_fail(ADG_IS_ADIM(adim));
633 g_return_if_fail(adg_is_boolean_value(new_state));
634 g_object_set(adim, "has-extension1", new_state, NULL);
638 * adg_adim_has_extension1:
639 * @adim: an #AdgADim entity
641 * Checks if @adim should render the first extension line.
643 * Returns: %TRUE on first extension line presents, %FALSE otherwise
645 gboolean
646 adg_adim_has_extension1(AdgADim *adim)
648 AdgADimPrivate *data;
650 g_return_val_if_fail(ADG_IS_ADIM(adim), FALSE);
652 data = adim->data;
654 return data->has_extension1;
658 * adg_adim_switch_extension2:
659 * @adim: an #AdgADim entity
660 * @new_state: the new new_state
662 * Shows (if @new_state is %TRUE) or hides (if @new_state is %FALSE)
663 * the second extension line of @adim.
665 void
666 adg_adim_switch_extension2(AdgADim *adim, gboolean new_state)
668 g_return_if_fail(ADG_IS_ADIM(adim));
669 g_return_if_fail(adg_is_boolean_value(new_state));
670 g_object_set(adim, "has-extension2", new_state, NULL);
674 * adg_adim_has_extension2:
675 * @adim: an #AdgADim entity
677 * Checks if @adim should render the second extension line.
679 * Returns: %TRUE on first extension line presents, %FALSE otherwise
681 gboolean
682 adg_adim_has_extension2(AdgADim *adim)
684 AdgADimPrivate *data;
686 g_return_val_if_fail(ADG_IS_ADIM(adim), FALSE);
688 data = adim->data;
690 return data->has_extension2;
694 static void
695 _adg_global_changed(AdgEntity *entity)
697 AdgADimPrivate *data = ((AdgADim *) entity)->data;
699 _adg_unset_trail((AdgADim *) entity);
701 if (_ADG_OLD_ENTITY_CLASS->global_changed)
702 _ADG_OLD_ENTITY_CLASS->global_changed(entity);
704 if (data->marker1 != NULL)
705 adg_entity_global_changed((AdgEntity *) data->marker1);
707 if (data->marker2 != NULL)
708 adg_entity_global_changed((AdgEntity *) data->marker2);
711 static void
712 _adg_local_changed(AdgEntity *entity)
714 _adg_unset_trail((AdgADim *) entity);
716 if (_ADG_OLD_ENTITY_CLASS->local_changed)
717 _ADG_OLD_ENTITY_CLASS->local_changed(entity);
720 static void
721 _adg_invalidate(AdgEntity *entity)
723 AdgADim *adim;
724 AdgADimPrivate *data;
726 adim = (AdgADim *) entity;
727 data = adim->data;
729 _adg_dispose_markers(adim);
730 data->geometry_arranged = FALSE;
731 _adg_unset_trail(adim);
733 if (data->org1)
734 adg_point_invalidate(data->org1);
735 if (data->org2)
736 adg_point_invalidate(data->org2);
738 if (_ADG_OLD_ENTITY_CLASS->invalidate)
739 _ADG_OLD_ENTITY_CLASS->invalidate(entity);
742 static void
743 _adg_arrange(AdgEntity *entity)
745 AdgADim *adim;
746 AdgDim *dim;
747 AdgADimPrivate *data;
748 AdgAlignment *quote;
749 const AdgMatrix *local;
750 AdgPair ref1, ref2, base1, base12, base2;
751 AdgPair pair;
753 if (_ADG_OLD_ENTITY_CLASS->arrange)
754 _ADG_OLD_ENTITY_CLASS->arrange(entity);
756 adim = (AdgADim *) entity;
758 if (!_adg_update_geometry(adim))
759 return;
761 dim = (AdgDim *) adim;
762 data = adim->data;
763 quote = adg_dim_get_quote(dim);
765 _adg_update_entities(adim);
767 if (data->cpml.path.status == CAIRO_STATUS_SUCCESS) {
768 AdgEntity *quote_entity = (AdgEntity *) quote;
769 adg_entity_set_global_map(quote_entity, &data->quote.global_map);
770 return;
773 local = adg_entity_get_local_matrix(entity);
774 cpml_pair_copy(&ref1, adg_point_get_pair(adg_dim_get_ref1(dim)));
775 cpml_pair_copy(&ref2, adg_point_get_pair(adg_dim_get_ref2(dim)));
776 cpml_pair_copy(&base1, &data->point.base1);
777 cpml_pair_copy(&base12, &data->point.base12);
778 cpml_pair_copy(&base2, &data->point.base2);
780 /* Apply the local matrix to the relevant points */
781 cpml_pair_transform(&ref1, local);
782 cpml_pair_transform(&ref2, local);
783 cpml_pair_transform(&base1, local);
784 cpml_pair_transform(&base12, local);
785 cpml_pair_transform(&base2, local);
787 /* Combine points and global shifts to build the path */
788 pair.x = ref1.x + data->shift.from1.x;
789 pair.y = ref1.y + data->shift.from1.y;
790 cpml_pair_to_cairo(&pair, &data->cpml.data[6]);
792 pair.x = base1.x + data->shift.base1.x;
793 pair.y = base1.y + data->shift.base1.y;
794 cpml_pair_to_cairo(&pair, &data->cpml.data[1]);
796 pair.x += data->shift.to1.x;
797 pair.y += data->shift.to1.y;
798 cpml_pair_to_cairo(&pair, &data->cpml.data[8]);
800 pair.x = base12.x + data->shift.base12.x;
801 pair.y = base12.y + data->shift.base12.y;
802 cpml_pair_to_cairo(&pair, &data->cpml.data[3]);
804 pair.x = ref2.x + data->shift.from2.x;
805 pair.y = ref2.y + data->shift.from2.y;
806 cpml_pair_to_cairo(&pair, &data->cpml.data[10]);
808 pair.x = base2.x + data->shift.base2.x;
809 pair.y = base2.y + data->shift.base2.y;
810 cpml_pair_to_cairo(&pair, &data->cpml.data[4]);
812 pair.x += data->shift.to2.x;
813 pair.y += data->shift.to2.y;
814 cpml_pair_to_cairo(&pair, &data->cpml.data[12]);
816 /* Play with header lengths to show or hide the extension lines */
817 if (data->has_extension1) {
818 data->cpml.data[7].header.length = data->has_extension2 ? 2 : 6;
819 } else {
820 data->cpml.data[2].header.length = data->has_extension2 ? 7 : 11;
823 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
825 if (quote != NULL) {
826 /* Update global and local map of the quote */
827 AdgEntity *quote_entity;
828 gdouble angle;
829 AdgMatrix map;
831 quote_entity = (AdgEntity *) quote;
832 angle = adg_dim_quote_angle(dim, (data->angle1 + data->angle2) / 2 + G_PI_2);
833 cpml_pair_from_cairo(&pair, &data->cpml.data[3]);
835 adg_alignment_set_factor_explicit(quote, 0.5, 0);
837 cairo_matrix_init_translate(&map, pair.x, pair.y);
838 cairo_matrix_rotate(&map, angle);
839 adg_entity_set_global_map(quote_entity, &map);
841 adg_matrix_copy(&data->quote.global_map,
842 adg_entity_get_global_map(quote_entity));
845 /* Signal to the markers (if any) that the path has changed */
846 if (data->marker1 != NULL) {
847 adg_marker_set_segment(data->marker1, data->trail, 1);
848 adg_entity_local_changed((AdgEntity *) data->marker1);
851 if (data->marker2 != NULL) {
852 adg_marker_set_segment(data->marker2, data->trail, 1);
853 adg_entity_local_changed((AdgEntity *) data->marker2);
856 /* TODO: compute the extents */
859 static void
860 _adg_render(AdgEntity *entity, cairo_t *cr)
862 AdgADim *adim;
863 AdgDim *dim;
864 AdgADimPrivate *data;
865 AdgDimStyle *dim_style;
866 AdgDress dress;
867 const cairo_path_t *cairo_path;
869 adim = (AdgADim *) entity;
870 data = adim->data;
872 if (!data->geometry_arranged) {
873 /* Entity not arranged, probably due to undefined pair found */
874 return;
877 dim = (AdgDim *) entity;
878 dim_style = _ADG_GET_DIM_STYLE(dim);
880 adg_style_apply((AdgStyle *) dim_style, entity, cr);
881 adg_entity_render((AdgEntity *) adg_dim_get_quote(dim), cr);
883 if (data->marker1 != NULL)
884 adg_entity_render((AdgEntity *) data->marker1, cr);
885 if (data->marker2 != NULL)
886 adg_entity_render((AdgEntity *) data->marker2, cr);
888 cairo_transform(cr, adg_entity_get_global_matrix(entity));
889 dress = adg_dim_style_get_line_dress(dim_style);
890 adg_entity_apply_dress(entity, dress, cr);
892 cairo_path = adg_trail_get_cairo_path(data->trail);
893 cairo_append_path(cr, cairo_path);
894 cairo_stroke(cr);
897 static gchar *
898 _adg_default_value(AdgDim *dim)
900 AdgADim *adim;
901 AdgADimPrivate *data;
902 AdgDimStyle *dim_style;
903 gdouble angle;
904 const gchar *format;
906 adim = (AdgADim *) dim;
907 data = adim->data;
908 dim_style = _ADG_GET_DIM_STYLE(dim);
909 format = adg_dim_style_get_number_format(dim_style);
911 if (!_adg_update_geometry(adim))
912 return g_strdup("undef");
914 angle = (data->angle2 - data->angle1) * 180 / M_PI;
915 return g_strdup_printf(format, angle);
918 /* With "geometry" is considered any data (point, vector or angle)
919 * that can be cached: this is strictly related on how the arrange()
920 * method works */
921 static gboolean
922 _adg_update_geometry(AdgADim *adim)
924 AdgADimPrivate *data;
925 AdgDimStyle *dim_style;
926 gdouble from_offset, to_offset;
927 gdouble spacing, level;
928 CpmlVector vector[3];
929 CpmlPair center;
930 gdouble distance;
932 data = adim->data;
934 if (data->geometry_arranged)
935 return TRUE;
936 else if (!_adg_get_info(adim, vector, &center, &distance))
937 return FALSE;
939 dim_style = _ADG_GET_DIM_STYLE(adim);
940 from_offset = adg_dim_style_get_from_offset(dim_style);
941 to_offset = adg_dim_style_get_to_offset(dim_style);
942 spacing = adg_dim_style_get_baseline_spacing(dim_style);
943 level = adg_dim_get_level((AdgDim *) adim);
945 /* shift.from1 */
946 cpml_vector_set_length(&vector[0], from_offset);
947 cpml_pair_copy(&data->shift.from1, &vector[0]);
949 /* shift.base1 */
950 cpml_vector_set_length(&vector[0], level * spacing);
951 cpml_pair_copy(&data->shift.base1, &vector[0]);
953 /* shift.to1 */
954 cpml_vector_set_length(&vector[0], to_offset);
955 cpml_pair_copy(&data->shift.to1, &vector[0]);
957 /* shift.from2 */
958 cpml_vector_set_length(&vector[2], from_offset);
959 cpml_pair_copy(&data->shift.from2, &vector[2]);
961 /* shift.base2 */
962 cpml_vector_set_length(&vector[2], level * spacing);
963 cpml_pair_copy(&data->shift.base2, &vector[2]);
965 /* shift.to2 */
966 cpml_vector_set_length(&vector[2], to_offset);
967 cpml_pair_copy(&data->shift.to2, &vector[2]);
969 /* shift.base12 */
970 cpml_vector_set_length(&vector[1], level * spacing);
971 cpml_pair_copy(&data->shift.base12, &vector[1]);
973 /* Distance can be 0, so the following will leave the
974 * vector array in undefined state */
976 /* point.base1 */
977 cpml_vector_set_length(&vector[0], distance);
978 data->point.base1.x = vector[0].x + center.x;
979 data->point.base1.y = vector[0].y + center.y;
981 /* point.base2 */
982 cpml_vector_set_length(&vector[2], distance);
983 data->point.base2.x = vector[2].x + center.x;
984 data->point.base2.y = vector[2].y + center.y;
986 /* point.base12 */
987 cpml_vector_set_length(&vector[1], distance);
988 data->point.base12.x = vector[1].x + center.x;
989 data->point.base12.y = vector[1].y + center.y;
991 data->geometry_arranged = TRUE;
993 return TRUE;
996 static void
997 _adg_update_entities(AdgADim *adim)
999 AdgEntity *entity;
1000 AdgADimPrivate *data;
1001 AdgDimStyle *dim_style;
1003 entity = (AdgEntity *) adim;
1004 data = adim->data;
1005 dim_style = _ADG_GET_DIM_STYLE(adim);
1007 if (data->trail == NULL)
1008 data->trail = adg_trail_new(_adg_trail_callback, adim);
1010 if (data->marker1 == NULL) {
1011 data->marker1 = adg_dim_style_marker1_new(dim_style);
1012 adg_entity_set_parent((AdgEntity *) data->marker1, entity);
1015 if (data->marker2 == NULL) {
1016 data->marker2 = adg_dim_style_marker2_new(dim_style);
1017 adg_entity_set_parent((AdgEntity *) data->marker2, entity);
1021 static void
1022 _adg_unset_trail(AdgADim *adim)
1024 AdgADimPrivate *data = adim->data;
1026 if (data->trail != NULL)
1027 adg_model_clear((AdgModel *) data->trail);
1029 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
1032 static void
1033 _adg_dispose_markers(AdgADim *adim)
1035 AdgADimPrivate *data = adim->data;
1037 if (data->trail != NULL) {
1038 g_object_unref(data->trail);
1039 data->trail = NULL;
1042 if (data->marker1 != NULL) {
1043 g_object_unref(data->marker1);
1044 data->marker1 = NULL;
1047 if (data->marker2 != NULL) {
1048 g_object_unref(data->marker2);
1049 data->marker2 = NULL;
1053 static gboolean
1054 _adg_get_info(AdgADim *adim, CpmlVector vector[],
1055 AdgPair *center, gdouble *distance)
1057 AdgDim *dim;
1058 AdgADimPrivate *data;
1059 const AdgPair *ref1, *ref2, *pos;
1060 const AdgPair *org1, *org2;
1061 gdouble factor;
1063 dim = (AdgDim *) adim;
1064 data = adim->data;
1065 ref1 = adg_point_get_pair(adg_dim_get_ref1(dim));
1066 ref2 = adg_point_get_pair(adg_dim_get_ref2(dim));
1067 pos = adg_point_get_pair(adg_dim_get_pos(dim));
1068 org1 = adg_point_get_pair(data->org1);
1069 org2 = adg_point_get_pair(data->org2);
1071 if (ref1 == NULL || ref2 == NULL || pos == NULL ||
1072 org1 == NULL || org2 == NULL)
1073 return FALSE;
1075 vector[0].x = ref1->x - org1->x;
1076 vector[0].y = ref1->y - org1->y;
1077 vector[2].x = ref2->x - org2->x;
1078 vector[2].y = ref2->y - org2->y;
1080 factor = vector[0].x * vector[2].y - vector[0].y * vector[2].x;
1081 if (factor == 0) {
1082 /* Parallel lines: hang with an error message */
1083 g_warning(_("%s: trying to set an angular dimension on parallel lines"),
1084 G_STRLOC);
1085 return FALSE;
1088 factor = ((ref1->y - ref2->y) * vector[2].x -
1089 (ref1->x - ref2->x) * vector[2].y) / factor;
1091 center->x = ref1->x + vector[0].x * factor;
1092 center->y = ref1->y + vector[0].y * factor;
1093 *distance = cpml_pair_distance(center, pos);
1094 data->angle1 = cpml_vector_angle(&vector[0]);
1095 data->angle2 = cpml_vector_angle(&vector[2]);
1096 while (data->angle2 < data->angle1)
1097 data->angle2 += M_PI * 2;
1099 cpml_vector_from_angle(&vector[1], (data->angle1 + data->angle2) / 2);
1101 return TRUE;
1104 static CpmlPath *
1105 _adg_trail_callback(AdgTrail *trail, gpointer user_data)
1107 AdgADim *adim;
1108 AdgADimPrivate *data;
1110 adim = (AdgADim *) user_data;
1111 data = adim->data;
1113 return &data->cpml.path;