[AdgLDim] Implemented quote positioning
[adg.git] / adg / adg-ldim.c
blobee21167e7f1fbc50fd1407d0a2b480883d208c30
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:adg-ldim
23 * @short_description: Linear dimensions
25 * The #AdgLDim entity represents a linear dimension.
26 **/
28 /**
29 * AdgLDim:
31 * All fields are private and should not be used directly.
32 * Use its public methods instead.
33 **/
36 #include "adg-ldim.h"
37 #include "adg-ldim-private.h"
38 #include "adg-dim-private.h"
39 #include "adg-dim-style.h"
40 #include "adg-intl.h"
42 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_ldim_parent_class)
43 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_ldim_parent_class)
46 enum {
47 PROP_0,
48 PROP_DIRECTION,
49 PROP_HAS_EXTENSION1,
50 PROP_HAS_EXTENSION2
54 static void dispose (GObject *object);
55 static void get_property (GObject *object,
56 guint param_id,
57 GValue *value,
58 GParamSpec *pspec);
59 static void set_property (GObject *object,
60 guint param_id,
61 const GValue *value,
62 GParamSpec *pspec);
63 static void local_changed (AdgEntity *entity);
64 static void invalidate (AdgEntity *entity);
65 static void arrange (AdgEntity *entity);
66 static void render (AdgEntity *entity,
67 cairo_t *cr);
68 static gchar * default_value (AdgDim *dim);
69 static void update_geometry (AdgLDim *ldim);
70 static void update_shift (AdgLDim *ldim);
71 static void update_entities (AdgLDim *ldim);
72 static void choose_flags (AdgLDim *ldim,
73 gboolean *to_outside,
74 gboolean *to_detach);
75 static void unset_trail (AdgLDim *ldim);
76 static void dispose_markers (AdgLDim *ldim);
77 static CpmlPath * trail_callback (AdgTrail *trail,
78 gpointer user_data);
81 G_DEFINE_TYPE(AdgLDim, adg_ldim, ADG_TYPE_DIM);
84 static void
85 adg_ldim_class_init(AdgLDimClass *klass)
87 GObjectClass *gobject_class;
88 AdgEntityClass *entity_class;
89 AdgDimClass *dim_class;
90 GParamSpec *param;
92 gobject_class = (GObjectClass *) klass;
93 entity_class = (AdgEntityClass *) klass;
94 dim_class = (AdgDimClass *) klass;
96 g_type_class_add_private(klass, sizeof(AdgLDimPrivate));
98 gobject_class->dispose = dispose;
99 gobject_class->get_property = get_property;
100 gobject_class->set_property = set_property;
102 entity_class->local_changed = local_changed;
103 entity_class->invalidate = invalidate;
104 entity_class->arrange = arrange;
105 entity_class->render = render;
107 dim_class->default_value = default_value;
109 param = g_param_spec_double("direction",
110 P_("Direction"),
111 P_("The inclination angle of the extension lines"),
112 -G_MAXDOUBLE, G_MAXDOUBLE, ADG_DIR_RIGHT,
113 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
114 g_object_class_install_property(gobject_class, PROP_DIRECTION, param);
116 param = g_param_spec_boolean("has-extension1",
117 P_("Has First Extension Line flag"),
118 P_("Show (TRUE) or hide (FALSE) the first extension line"),
119 TRUE, G_PARAM_READWRITE);
120 g_object_class_install_property(gobject_class, PROP_HAS_EXTENSION1, param);
122 param = g_param_spec_boolean("has-extension2",
123 P_("Has Second Extension Line flag"),
124 P_("Show (TRUE) or hide (FALSE) the second extension line"),
125 TRUE, G_PARAM_READWRITE);
126 g_object_class_install_property(gobject_class, PROP_HAS_EXTENSION2, param);
129 static void
130 adg_ldim_init(AdgLDim *ldim)
132 AdgLDimPrivate *data;
133 cairo_path_data_t move_to, line_to;
135 data = G_TYPE_INSTANCE_GET_PRIVATE(ldim, ADG_TYPE_LDIM, AdgLDimPrivate);
136 move_to.header.type = CAIRO_PATH_MOVE_TO;
137 move_to.header.length = 2;
138 line_to.header.type = CAIRO_PATH_LINE_TO;
139 line_to.header.length = 2;
141 data->direction = 0;
142 data->has_extension1 = TRUE;
143 data->has_extension2 = TRUE;
145 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
146 data->cpml.path.data = data->cpml.data;
147 data->cpml.path.num_data = G_N_ELEMENTS(data->cpml.data);
148 data->cpml.path.data[0] = move_to;
149 data->cpml.path.data[2] = line_to;
150 data->cpml.path.data[4] = move_to;
151 data->cpml.path.data[6] = line_to;
152 data->cpml.path.data[8] = move_to;
153 data->cpml.path.data[10] = line_to;
154 data->cpml.path.data[12] = move_to;
155 data->cpml.path.data[14] = line_to;
156 data->cpml.path.data[16] = move_to;
157 data->cpml.path.data[18] = line_to;
159 data->trail = NULL;
160 data->marker1 = NULL;
161 data->marker2 = NULL;
163 data->geometry.is_arranged = FALSE;
164 data->shift.is_arranged = FALSE;
166 ldim->data = data;
169 static void
170 dispose(GObject *object)
172 dispose_markers((AdgLDim *) object);
174 if (PARENT_OBJECT_CLASS->dispose)
175 PARENT_OBJECT_CLASS->dispose(object);
178 static void
179 get_property(GObject *object,
180 guint prop_id, GValue *value, GParamSpec *pspec)
182 AdgLDimPrivate *data = ((AdgLDim *) object)->data;
184 switch (prop_id) {
185 case PROP_DIRECTION:
186 g_value_set_double(value, data->direction);
187 break;
188 case PROP_HAS_EXTENSION1:
189 g_value_set_boolean(value, data->has_extension1);
190 break;
191 case PROP_HAS_EXTENSION2:
192 g_value_set_boolean(value, data->has_extension2);
193 break;
194 default:
195 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
196 break;
200 static void
201 set_property(GObject *object,
202 guint prop_id, const GValue *value, GParamSpec *pspec)
204 AdgLDimPrivate *data = ((AdgLDim *) object)->data;
206 switch (prop_id) {
207 case PROP_DIRECTION:
208 data->direction = g_value_get_double(value);
209 break;
210 case PROP_HAS_EXTENSION1:
211 data->has_extension1 = g_value_get_boolean(value);
212 break;
213 case PROP_HAS_EXTENSION2:
214 data->has_extension2 = g_value_get_boolean(value);
215 break;
216 default:
217 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
218 break;
224 * adg_ldim_new:
226 * Creates a new - undefined - linear dimension. You must, at least,
227 * define the reference points with adg_dim_set_ref(), the dimension
228 * direction with adg_ldim_set_direction() and the position reference
229 * using adg_dim_set_pos().
231 * Returns: the newly created linear dimension entity
233 AdgLDim *
234 adg_ldim_new(void)
236 return g_object_new(ADG_TYPE_LDIM, NULL);
240 * adg_ldim_new_full:
241 * @ref1: the first reference point
242 * @ref2: the second reference point
243 * @pos: the position reference
244 * @direction: angle where to extend the dimension
246 * Creates a new linear dimension, specifing all the needed properties in
247 * one shot.
249 * Returns: the newly created linear dimension entity
251 AdgLDim *
252 adg_ldim_new_full(const AdgPair *ref1, const AdgPair *ref2,
253 const AdgPair *pos, gdouble direction)
255 AdgLDim *ldim;
256 AdgDim *dim;
258 ldim = g_object_new(ADG_TYPE_LDIM, "direction", direction, NULL);
259 dim = (AdgDim *) ldim;
261 adg_dim_set_ref(dim, ref1, ref2);
262 adg_dim_set_pos(dim, pos);
264 return ldim;
268 * adg_ldim_new_full_explicit:
269 * @ref1_x: the x coordinate of the first reference point
270 * @ref1_y: the y coordinate of the first reference point
271 * @ref2_x: the x coordinate of the second reference point
272 * @ref2_y: the y coordinate of the second reference point
273 * @pos_x: the x coordinate of the position reference
274 * @pos_y: the y coordinate of the position reference
275 * @direction: angle where to extend the dimension
277 * Wrappes adg_ldim_new_full() with explicit values.
279 * Returns: the newly created linear dimension entity
281 AdgLDim *
282 adg_ldim_new_full_explicit(gdouble ref1_x, gdouble ref1_y,
283 gdouble ref2_x, gdouble ref2_y,
284 gdouble pos_x, gdouble pos_y, gdouble direction)
286 AdgPair ref1;
287 AdgPair ref2;
288 AdgPair pos;
290 ref1.x = ref1_x;
291 ref1.y = ref1_y;
292 ref2.x = ref2_x;
293 ref2.y = ref2_y;
294 pos.x = pos_x;
295 pos.y = pos_y;
297 return adg_ldim_new_full(&ref1, &ref2, &pos, direction);
301 * adg_ldim_new_full_from_model:
302 * @model: the model from which the named pairs are taken
303 * @ref1: the first reference point
304 * @ref2: the second reference point
305 * @pos: the position reference
306 * @direction: angle where to extend the dimension
308 * Creates a new linear dimension, specifing all the needed properties in
309 * one shot and using named pairs from @model.
311 * Returns: the newly created linear dimension entity
313 AdgLDim *
314 adg_ldim_new_full_from_model(AdgModel *model,
315 const gchar *ref1, const gchar *ref2,
316 const gchar *pos, gdouble direction)
318 AdgLDim *ldim;
319 AdgDim *dim;
321 ldim = g_object_new(ADG_TYPE_LDIM, "direction", direction, NULL);
322 dim = (AdgDim *) ldim;
324 adg_dim_set_ref_from_model(dim, model, ref1, ref2);
325 adg_dim_set_pos_from_model(dim, model, pos);
327 return ldim;
331 * adg_ldim_set_direction:
332 * @ldim: an #AdgLDim entity
333 * @direction: an angle value, in radians
335 * Sets the direction angle where to extend @ldim.
337 void
338 adg_ldim_set_direction(AdgLDim *ldim, gdouble direction)
340 AdgLDimPrivate *data;
342 g_return_if_fail(ADG_IS_LDIM(ldim));
344 data = ldim->data;
345 data->direction = direction;
347 g_object_notify((GObject *) ldim, "direction");
351 * adg_ldim_get_direction:
352 * @ldim: an #AdgLDim entity
354 * Gets the direction where @ldim will extend.
356 * Returns: the direction angle in radians
358 gdouble
359 adg_ldim_get_direction(AdgLDim *ldim)
361 AdgLDimPrivate *data;
363 g_return_val_if_fail(ADG_IS_LDIM(ldim), 0);
365 data = ldim->data;
367 return data->direction;
371 * adg_ldim_switch_extension1:
372 * @ldim: an #AdgLDim entity
373 * @new_state: the new state
375 * Shows (if @new_state is %TRUE) or hide (if @new_state is %FALSE)
376 * the first extension line of @ldim.
378 void
379 adg_ldim_switch_extension1(AdgLDim *ldim, gboolean new_state)
381 AdgLDimPrivate *data;
383 g_return_if_fail(ADG_IS_LDIM(ldim));
385 data = ldim->data;
387 if (data->has_extension1 != new_state) {
388 data->has_extension1 = new_state;
389 g_object_notify((GObject *) ldim, "has-extension1");
394 * adg_ldim_has_extension1:
395 * @ldim: an #AdgLDim entity
397 * Checks if @ldim should render also the first extension line.
399 * Returns: %TRUE on first extension line presents, %FALSE otherwise
401 gboolean
402 adg_ldim_has_extension1(AdgLDim *ldim)
404 AdgLDimPrivate *data;
406 g_return_val_if_fail(ADG_IS_LDIM(ldim), FALSE);
408 data = ldim->data;
410 return data->has_extension1;
414 * adg_ldim_switch_extension2:
415 * @ldim: an #AdgLDim entity
416 * @new_state: the new new_state
418 * Shows (if @new_state is %TRUE) or hide (if @new_state is %FALSE)
419 * the second extension line of @ldim.
421 void
422 adg_ldim_switch_extension2(AdgLDim *ldim, gboolean new_state)
424 AdgLDimPrivate *data;
426 g_return_if_fail(ADG_IS_LDIM(ldim));
428 data = ldim->data;
430 if (data->has_extension2 != new_state) {
431 data->has_extension2 = new_state;
432 g_object_notify((GObject *) ldim, "has-extension2");
437 * adg_ldim_has_extension2:
438 * @ldim: an #AdgLDim entity
440 * Checks if @ldim should render also the second extension line.
442 * Returns: %TRUE on first extension line presents, %FALSE otherwise
444 gboolean
445 adg_ldim_has_extension2(AdgLDim *ldim)
447 AdgLDimPrivate *data;
449 g_return_val_if_fail(ADG_IS_LDIM(ldim), FALSE);
451 data = ldim->data;
453 return data->has_extension2;
457 static void
458 local_changed(AdgEntity *entity)
460 unset_trail((AdgLDim *) entity);
462 if (PARENT_ENTITY_CLASS->local_changed)
463 PARENT_ENTITY_CLASS->local_changed(entity);
466 static void
467 invalidate(AdgEntity *entity)
469 AdgLDim *ldim;
470 AdgLDimPrivate *data;
472 ldim = (AdgLDim *) entity;
473 data = ldim->data;
475 dispose_markers(ldim);
476 data->geometry.is_arranged = FALSE;
477 data->shift.is_arranged = FALSE;
478 unset_trail(ldim);
480 if (PARENT_ENTITY_CLASS->invalidate)
481 PARENT_ENTITY_CLASS->invalidate(entity);
484 /* TODO: split the following crap in more manageable functions */
485 static void
486 arrange(AdgEntity *entity)
488 AdgLDim *ldim;
489 AdgDim *dim;
490 AdgLDimPrivate *data;
491 AdgAlignment *quote;
492 AdgDimStyle *dim_style;
493 gboolean to_outside, to_detach;
494 const AdgMatrix *local;
495 AdgPair ref1, ref2, base1, base2;
496 AdgPair pair;
497 gint n;
498 CpmlExtents extents;
500 if (PARENT_ENTITY_CLASS->arrange)
501 PARENT_ENTITY_CLASS->arrange(entity);
503 ldim = (AdgLDim *) entity;
504 dim = (AdgDim *) ldim;
505 data = ldim->data;
506 quote = adg_dim_get_quote(dim);
508 update_geometry(ldim);
509 update_shift(ldim);
510 update_entities(ldim);
512 /* Check for cached result */
513 if (data->cpml.path.status == CAIRO_STATUS_SUCCESS) {
514 AdgEntity *quote_entity = (AdgEntity *) quote;
515 adg_entity_set_global_map(quote_entity, &data->quote.global_map);
516 return;
519 choose_flags(ldim, &to_outside, &to_detach);
521 dim_style = GET_DIM_STYLE(dim);
522 local = adg_entity_get_local_matrix(entity);
523 cpml_pair_copy(&ref1, adg_dim_get_ref1(dim));
524 cpml_pair_copy(&ref2, adg_dim_get_ref2(dim));
525 cpml_pair_copy(&base1, &data->geometry.base1);
526 cpml_pair_copy(&base2, &data->geometry.base2);
528 cpml_pair_transform(&ref1, local);
529 cpml_pair_transform(&ref2, local);
530 cpml_pair_transform(&base1, local);
531 cpml_pair_add(&base1, &data->shift.base);
532 cpml_pair_transform(&base2, local);
533 cpml_pair_add(&base2, &data->shift.base);
535 cpml_pair_copy(&pair, &ref1);
536 cpml_pair_add(&pair, &data->shift.from);
537 cpml_pair_to_cairo(&pair, &data->cpml.data[13]);
539 cpml_pair_to_cairo(&base1, &data->cpml.data[1]);
541 cpml_pair_copy(&pair, &base1);
542 cpml_pair_add(&pair, &data->shift.to);
543 cpml_pair_to_cairo(&pair, &data->cpml.data[15]);
545 cpml_pair_copy(&pair, &ref2);
546 cpml_pair_add(&pair, &data->shift.from);
547 cpml_pair_to_cairo(&pair, &data->cpml.data[17]);
549 cpml_pair_to_cairo(&base2, &data->cpml.data[3]);
551 cpml_pair_copy(&pair, &base2);
552 cpml_pair_add(&pair, &data->shift.to);
553 cpml_pair_to_cairo(&pair, &data->cpml.data[19]);
555 /* Calculate the outside segments */
556 if (to_outside) {
557 gdouble beyond;
558 CpmlVector vector;
560 beyond = adg_dim_style_get_beyond(dim_style);
561 cpml_pair_from_cairo(&pair, &data->cpml.data[1]);
563 cpml_pair_from_cairo(&vector, &data->cpml.data[3]);
564 cpml_pair_sub(&vector, &pair);
565 cpml_vector_set_length(&vector, beyond);
567 cpml_pair_from_cairo(&pair, &data->cpml.data[1]);
568 cpml_pair_to_cairo(&pair, &data->cpml.data[5]);
570 cpml_pair_sub(&pair, &vector);
571 cpml_pair_to_cairo(&pair, &data->cpml.data[7]);
573 cpml_pair_from_cairo(&pair, &data->cpml.data[3]);
574 cpml_pair_to_cairo(&pair, &data->cpml.data[11]);
576 cpml_pair_add(&pair, &vector);
577 cpml_pair_to_cairo(&pair, &data->cpml.data[9]);
579 data->cpml.data[2].header.length = 2;
580 n = 10;
581 } else {
582 data->cpml.data[2].header.length = 10;
583 n = 2;
586 data->cpml.data[10].header.length = 2;
587 extents.is_defined = FALSE;
589 /* Add the quote */
590 if (quote != NULL) {
591 AdgEntity *quote_entity;
592 gdouble angle;
593 AdgPair middle, factor;
594 AdgMatrix map;
596 quote_entity = (AdgEntity *) quote;
597 angle = adg_dim_quote_angle(dim, data->direction + G_PI_2);
598 middle.x = (base1.x + base2.x) / 2;
599 middle.y = (base1.y + base2.y) / 2;
600 factor.x = factor.y = 0;
602 if (to_detach) {
603 /* Detached quote: position the quote at "pos" */
604 AdgPair p_line, p_quote;
605 gdouble same_sd, opposite_sd, quote_size;
606 gint n_side;
607 cairo_path_data_t *to_extend;
609 /* Set "pair" to the properly converted "pos" coordinates */
610 cpml_pair_copy(&pair, adg_dim_get_pos(dim));
611 cpml_pair_transform(&pair, local);
612 cpml_pair_add(&pair, &data->shift.base);
614 /* Taking "middle" as the reference, check if the quote
615 * is on the _left_ or on the _right_ side. This is got
616 * by checking if (pos-middle) is closer to the quote
617 * vector or to the negated quote vector by using an
618 * algorithm based on the squared distances. */
619 p_line.x = pair.x - middle.x;
620 p_line.y = pair.y - middle.y;
621 cpml_vector_from_angle(&p_quote, angle);
623 same_sd = cpml_pair_squared_distance(&p_quote, &p_line);
624 cpml_pair_negate(&p_quote);
625 opposite_sd = cpml_pair_squared_distance(&p_quote, &p_line);
626 quote_size = adg_entity_get_extents(quote_entity)->size.x;
628 /* Properly align the quote, depending on its side */
629 if (same_sd > opposite_sd) {
630 factor.x = 1;
631 } else {
632 factor.x = 0;
633 cpml_pair_negate(&p_quote);
636 cpml_vector_set_length(&p_quote, quote_size);
637 cpml_pair_add(&p_quote, &pair);
639 /* Extends the base line to include the "p_quote" pair,
640 * that is underline a detached quote */
641 if (cpml_pair_squared_distance(&p_quote, &base1) >
642 cpml_pair_squared_distance(&p_quote, &base2))
643 n_side = 2;
644 else
645 n_side = 1;
647 if (to_outside) {
648 to_extend = &data->cpml.data[n_side == 1 ? 7 : 9];
649 } else {
650 to_extend = &data->cpml.data[9];
651 cpml_pair_to_cairo(&middle, &data->cpml.data[9]);
652 cpml_pair_to_cairo(n_side == 1 ? &base1 : &base2,
653 &data->cpml.data[11]);
654 data->cpml.data[2].header.length = 6;
655 n = 10;
658 cpml_pair_from_cairo(&p_line, to_extend);
660 /* Extend the base line only if needed */
661 if (cpml_pair_squared_distance(&p_quote, &middle) >
662 cpml_pair_squared_distance(&p_line, &middle))
663 cpml_pair_to_cairo(&p_quote, to_extend);
664 } else {
665 /* Center the quote in the middle of the base line */
666 factor.x = 0.5;
667 cpml_pair_copy(&pair, &middle);
670 adg_alignment_set_factor(quote, &factor);
671 cairo_matrix_init_translate(&map, pair.x, pair.y);
672 cairo_matrix_rotate(&map, angle);
673 adg_entity_set_global_map(quote_entity, &map);
674 adg_entity_arrange(quote_entity);
675 cpml_extents_add(&extents, adg_entity_get_extents(quote_entity));
677 adg_matrix_copy(&data->quote.global_map,
678 adg_entity_get_global_map(quote_entity));
681 /* Play with header lengths to show or hide the extension lines */
682 if (data->has_extension1) {
683 data->cpml.data[14].header.length = data->has_extension2 ? 2 : 6;
684 } else {
685 data->cpml.data[14].header.length = 2;
686 data->cpml.data[n].header.length += 4;
687 if (!data->has_extension2)
688 data->cpml.data[n].header.length += 4;
691 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
692 cpml_extents_add(&extents, adg_trail_get_extents(data->trail));
694 if (data->marker1 != NULL) {
695 AdgEntity *marker1_entity = (AdgEntity *) data->marker1;
696 adg_marker_set_segment(data->marker1, data->trail, to_outside ? 2 : 1);
697 adg_entity_arrange(marker1_entity);
698 adg_entity_local_changed(marker1_entity);
699 cpml_extents_add(&extents, adg_entity_get_extents(marker1_entity));
702 if (data->marker2 != NULL) {
703 AdgEntity *marker2_entity = (AdgEntity *) data->marker2;
704 adg_marker_set_segment(data->marker2, data->trail, to_outside ? 3 : 1);
705 adg_entity_local_changed(marker2_entity);
706 adg_entity_arrange(marker2_entity);
707 cpml_extents_add(&extents, adg_entity_get_extents(marker2_entity));
710 adg_entity_set_extents(entity, &extents);
713 static void
714 render(AdgEntity *entity, cairo_t *cr)
716 AdgLDim *ldim;
717 AdgDim *dim;
718 AdgLDimPrivate *data;
719 AdgDimStyle *dim_style;
720 AdgDress dress;
721 const cairo_path_t *cairo_path;
723 ldim = (AdgLDim *) entity;
724 dim = (AdgDim *) entity;
725 data = ldim->data;
726 dim_style = GET_DIM_STYLE(dim);
728 adg_style_apply((AdgStyle *) dim_style, entity, cr);
730 if (data->marker1 != NULL)
731 adg_entity_render((AdgEntity *) data->marker1, cr);
733 if (data->marker2 != NULL)
734 adg_entity_render((AdgEntity *) data->marker2, cr);
736 adg_entity_render((AdgEntity *) adg_dim_get_quote(dim), cr);
738 dress = adg_dim_style_get_line_dress(dim_style);
739 adg_entity_apply_dress(entity, dress, cr);
741 cairo_path = adg_trail_get_cairo_path(data->trail);
742 cairo_append_path(cr, cairo_path);
743 cairo_stroke(cr);
746 static gchar *
747 default_value(AdgDim *dim)
749 AdgLDim *ldim;
750 AdgLDimPrivate *data;
751 AdgDimStyle *dim_style;
752 const gchar *format;
754 ldim = (AdgLDim *) dim;
755 data = ldim->data;
756 dim_style = GET_DIM_STYLE(dim);
757 format = adg_dim_style_get_number_format(dim_style);
759 update_geometry(ldim);
761 return g_strdup_printf(format, data->geometry.distance);
764 static void
765 update_geometry(AdgLDim *ldim)
767 AdgLDimPrivate *data;
768 AdgDim *dim;
769 const AdgPair *ref1, *ref2;
770 const AdgPair *pos;
771 CpmlVector baseline, extension;
772 gdouble d, k;
774 data = ldim->data;
776 if (data->geometry.is_arranged)
777 return;
779 dim = (AdgDim *) ldim;
780 ref1 = adg_dim_get_ref1(dim);
781 ref2 = adg_dim_get_ref2(dim);
782 pos = adg_dim_get_pos(dim);
783 cpml_vector_from_angle(&extension, data->direction);
784 cpml_pair_copy(&baseline, &extension);
785 cpml_vector_normal(&baseline);
787 d = extension.y * baseline.x -
788 extension.x * baseline.y;
789 g_return_if_fail(d != 0);
791 k = ((pos->y - ref1->y) * baseline.x -
792 (pos->x - ref1->x) * baseline.y) / d;
793 data->geometry.base1.x = ref1->x + k * extension.x;
794 data->geometry.base1.y = ref1->y + k * extension.y;
796 k = ((pos->y - ref2->y) * baseline.x -
797 (pos->x - ref2->x) * baseline.y) / d;
798 data->geometry.base2.x = ref2->x + k * extension.x;
799 data->geometry.base2.y = ref2->y + k * extension.y;
801 data->geometry.distance = cpml_pair_distance(&data->geometry.base1,
802 &data->geometry.base2);
804 data->geometry.is_arranged = TRUE;
807 static void
808 update_shift(AdgLDim *ldim)
810 AdgLDimPrivate *data;
811 AdgDimStyle *dim_style;
812 gdouble from_offset, to_offset;
813 gdouble baseline_spacing, level;
814 CpmlVector vector;
816 data = ldim->data;
818 if (data->shift.is_arranged)
819 return;
821 dim_style = GET_DIM_STYLE(ldim);
822 from_offset = adg_dim_style_get_from_offset(dim_style);
823 to_offset = adg_dim_style_get_to_offset(dim_style);
824 baseline_spacing = adg_dim_style_get_baseline_spacing(dim_style);
825 level = adg_dim_get_level((AdgDim *) ldim);
827 cpml_vector_from_angle(&vector, data->direction);
829 cpml_vector_set_length(&vector, from_offset);
830 cpml_pair_copy(&data->shift.from, &vector);
832 cpml_vector_set_length(&vector, to_offset);
833 cpml_pair_copy(&data->shift.to, &vector);
835 cpml_vector_set_length(&vector, level * baseline_spacing);
836 cpml_pair_copy(&data->shift.base, &vector);
838 data->shift.is_arranged = TRUE;
841 static void
842 update_entities(AdgLDim *ldim)
844 AdgLDimPrivate *data;
845 AdgDimStyle *dim_style;
847 data = ldim->data;
848 dim_style = GET_DIM_STYLE(ldim);
850 if (data->trail == NULL)
851 data->trail = adg_trail_new(trail_callback, ldim);
853 if (data->marker1 == NULL)
854 data->marker1 = adg_dim_style_marker1_new(dim_style);
856 if (data->marker2 == NULL)
857 data->marker2 = adg_dim_style_marker2_new(dim_style);
860 static void
861 choose_flags(AdgLDim *ldim, gboolean *to_outside, gboolean *to_detach)
863 AdgDim *dim;
864 AdgThreeState outside, detached;
865 AdgLDimPrivate *data;
866 AdgEntity *quote;
867 const AdgMatrix *local;
868 gdouble available_space, markers_space, quote_space;
870 dim = (AdgDim *) ldim;
871 outside = adg_dim_get_outside(dim);
872 detached = adg_dim_get_detached(dim);
874 *to_outside = outside == ADG_THREE_STATE_ON;
875 *to_detach = detached == ADG_THREE_STATE_ON;
877 /* On explicit flags, no further investigation is required */
878 if (outside != ADG_THREE_STATE_UNKNOWN &&
879 detached != ADG_THREE_STATE_UNKNOWN)
880 return;
882 data = ldim->data;
883 quote = (AdgEntity *) adg_dim_get_quote((AdgDim *) ldim);
884 local = adg_entity_get_local_matrix((AdgEntity *) ldim);
885 available_space = data->geometry.distance * local->xx;
887 if (outside == ADG_THREE_STATE_ON)
888 markers_space = 0;
889 else
890 markers_space =
891 (data->marker1 == NULL ? 0 : adg_marker_get_size(data->marker1)) +
892 (data->marker2 == NULL ? 0 : adg_marker_get_size(data->marker2));
894 /* Leave at least 0.25 marker_space between the markers */
895 if (detached == ADG_THREE_STATE_ON)
896 quote_space = markers_space * 0.25;
897 else
898 quote_space = adg_entity_get_extents(quote)->size.x;
900 if (outside == ADG_THREE_STATE_UNKNOWN &&
901 detached == ADG_THREE_STATE_UNKNOWN) {
902 /* Both flags need to be choosed */
903 if (quote_space + markers_space < available_space) {
904 *to_detach = FALSE;
905 *to_outside = FALSE;
906 } else if (quote_space < available_space) {
907 *to_detach = FALSE;
908 *to_outside = TRUE;
909 } else {
910 *to_detach = TRUE;
911 *to_outside = markers_space * 1.25 > available_space;
913 } else if (outside == ADG_THREE_STATE_UNKNOWN) {
914 /* Only the outside flag may be guessed */
915 *to_outside = quote_space + markers_space > available_space;
916 } else {
917 /* Only the detached flag may be guessed */
918 *to_detach = quote_space + markers_space > available_space;
922 static void
923 unset_trail(AdgLDim *ldim)
925 AdgLDimPrivate *data = ldim->data;
927 if (data->trail != NULL)
928 adg_model_clear((AdgModel *) data->trail);
930 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
933 static void
934 dispose_markers(AdgLDim *ldim)
936 AdgLDimPrivate *data = ldim->data;
938 if (data->trail != NULL) {
939 g_object_unref(data->trail);
940 data->trail = NULL;
943 if (data->marker1 != NULL) {
944 g_object_unref(data->marker1);
945 data->marker1 = NULL;
948 if (data->marker2 != NULL) {
949 g_object_unref(data->marker2);
950 data->marker2 = NULL;
954 static CpmlPath *
955 trail_callback(AdgTrail *trail, gpointer user_data)
957 AdgLDim *ldim;
958 AdgLDimPrivate *data;
960 ldim = (AdgLDim *) user_data;
961 data = ldim->data;
963 return &data->cpml.path;