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.
23 * @short_description: Linear dimensions
25 * The #AdgLDim entity represents a linear dimension.
33 * All fields are private and should not be used directly.
34 * Use its public methods instead.
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"
52 #include "adg-dim-private.h"
55 #include "adg-ldim-private.h"
58 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_ldim_parent_class)
59 #define _ADG_OLD_ENTITY_CLASS ((AdgEntityClass *) adg_ldim_parent_class)
62 G_DEFINE_TYPE(AdgLDim
, adg_ldim
, ADG_TYPE_DIM
)
72 static void _adg_dispose (GObject
*object
);
73 static void _adg_get_property (GObject
*object
,
77 static void _adg_set_property (GObject
*object
,
81 static void _adg_global_changed (AdgEntity
*entity
);
82 static void _adg_local_changed (AdgEntity
*entity
);
83 static void _adg_invalidate (AdgEntity
*entity
);
84 static void _adg_arrange (AdgEntity
*entity
);
85 static void _adg_render (AdgEntity
*entity
,
87 static gchar
* _adg_default_value (AdgDim
*dim
);
88 static gboolean
_adg_update_geometry (AdgLDim
*ldim
);
89 static void _adg_update_shift (AdgLDim
*ldim
);
90 static void _adg_update_entities (AdgLDim
*ldim
);
91 static void _adg_choose_flags (AdgLDim
*ldim
,
94 static void _adg_update_quote (AdgLDim
*ldim
,
101 static void _adg_update_extents (AdgLDim
*ldim
);
102 static void _adg_unset_trail (AdgLDim
*ldim
);
103 static void _adg_dispose_trail (AdgLDim
*ldim
);
104 static void _adg_dispose_markers (AdgLDim
*ldim
);
105 static CpmlPath
* _adg_trail_callback (AdgTrail
*trail
,
110 adg_ldim_class_init(AdgLDimClass
*klass
)
112 GObjectClass
*gobject_class
;
113 AdgEntityClass
*entity_class
;
114 AdgDimClass
*dim_class
;
117 gobject_class
= (GObjectClass
*) klass
;
118 entity_class
= (AdgEntityClass
*) klass
;
119 dim_class
= (AdgDimClass
*) klass
;
121 g_type_class_add_private(klass
, sizeof(AdgLDimPrivate
));
123 gobject_class
->dispose
= _adg_dispose
;
124 gobject_class
->get_property
= _adg_get_property
;
125 gobject_class
->set_property
= _adg_set_property
;
127 entity_class
->global_changed
= _adg_global_changed
;
128 entity_class
->local_changed
= _adg_local_changed
;
129 entity_class
->invalidate
= _adg_invalidate
;
130 entity_class
->arrange
= _adg_arrange
;
131 entity_class
->render
= _adg_render
;
133 dim_class
->default_value
= _adg_default_value
;
135 param
= g_param_spec_double("direction",
137 P_("The inclination angle of the extension lines"),
138 -G_PI
, G_PI
, ADG_DIR_RIGHT
,
139 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
);
140 g_object_class_install_property(gobject_class
, PROP_DIRECTION
, param
);
142 param
= g_param_spec_boolean("has-extension1",
143 P_("Has First Extension Line flag"),
144 P_("Show (TRUE) or hide (FALSE) the first extension line"),
145 TRUE
, G_PARAM_READWRITE
);
146 g_object_class_install_property(gobject_class
, PROP_HAS_EXTENSION1
, param
);
148 param
= g_param_spec_boolean("has-extension2",
149 P_("Has Second Extension Line flag"),
150 P_("Show (TRUE) or hide (FALSE) the second extension line"),
151 TRUE
, G_PARAM_READWRITE
);
152 g_object_class_install_property(gobject_class
, PROP_HAS_EXTENSION2
, param
);
156 adg_ldim_init(AdgLDim
*ldim
)
158 AdgLDimPrivate
*data
;
159 cairo_path_data_t move_to
, line_to
;
161 data
= G_TYPE_INSTANCE_GET_PRIVATE(ldim
, ADG_TYPE_LDIM
, AdgLDimPrivate
);
162 move_to
.header
.type
= CPML_MOVE
;
163 move_to
.header
.length
= 2;
164 line_to
.header
.type
= CPML_LINE
;
165 line_to
.header
.length
= 2;
167 data
->direction
= ADG_DIR_RIGHT
;
168 data
->has_extension1
= TRUE
;
169 data
->has_extension2
= TRUE
;
171 data
->cpml
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
172 data
->cpml
.path
.data
= data
->cpml
.data
;
173 data
->cpml
.path
.num_data
= G_N_ELEMENTS(data
->cpml
.data
);
174 data
->cpml
.path
.data
[0] = move_to
;
175 data
->cpml
.path
.data
[2] = line_to
;
176 data
->cpml
.path
.data
[4] = move_to
;
177 data
->cpml
.path
.data
[6] = line_to
;
178 data
->cpml
.path
.data
[8] = move_to
;
179 data
->cpml
.path
.data
[10] = line_to
;
180 data
->cpml
.path
.data
[12] = move_to
;
181 data
->cpml
.path
.data
[14] = line_to
;
182 data
->cpml
.path
.data
[16] = move_to
;
183 data
->cpml
.path
.data
[18] = line_to
;
186 data
->marker1
= NULL
;
187 data
->marker2
= NULL
;
189 data
->geometry
.is_arranged
= FALSE
;
190 data
->shift
.is_arranged
= FALSE
;
196 _adg_dispose(GObject
*object
)
198 AdgLDim
*ldim
= (AdgLDim
*) object
;
200 _adg_dispose_trail(ldim
);
201 _adg_dispose_markers(ldim
);
203 if (_ADG_OLD_OBJECT_CLASS
->dispose
)
204 _ADG_OLD_OBJECT_CLASS
->dispose(object
);
208 _adg_get_property(GObject
*object
, guint prop_id
,
209 GValue
*value
, GParamSpec
*pspec
)
211 AdgLDimPrivate
*data
= ((AdgLDim
*) object
)->data
;
215 g_value_set_double(value
, data
->direction
);
217 case PROP_HAS_EXTENSION1
:
218 g_value_set_boolean(value
, data
->has_extension1
);
220 case PROP_HAS_EXTENSION2
:
221 g_value_set_boolean(value
, data
->has_extension2
);
224 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
230 _adg_set_property(GObject
*object
, guint prop_id
,
231 const GValue
*value
, GParamSpec
*pspec
)
234 AdgLDimPrivate
*data
;
236 ldim
= (AdgLDim
*) object
;
241 data
->direction
= cpml_angle(g_value_get_double(value
));
243 case PROP_HAS_EXTENSION1
:
244 data
->has_extension1
= g_value_get_boolean(value
);
246 case PROP_HAS_EXTENSION2
:
247 data
->has_extension2
= g_value_get_boolean(value
);
250 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
259 * Creates a new - undefined - linear dimension. You must, at least,
260 * define the start of the dimension in #AdgDim:ref1, the end in
261 * #AdgDim:ref2 and the position of the quote in #AdgDim:pos using
262 * any valid #AdgDim method. The director of the dimension (that is,
263 * if it is horizontal, vertical or oblique at a specific angle)
264 * should be specified with adg_ldim_set_direction().
266 * Returns: the newly created linear dimension entity
273 return g_object_new(ADG_TYPE_LDIM
, NULL
);
278 * @ref1: allow-none: the first reference point
279 * @ref2: allow-none: the second reference point
280 * @pos: allow-none: the position reference
281 * @direction: angle where to extend the dimension
283 * Creates a new linear dimension, specifing all the needed properties in
286 * Returns: the newly created linear dimension entity
291 adg_ldim_new_full(const AdgPair
*ref1
, const AdgPair
*ref2
,
292 const AdgPair
*pos
, gdouble direction
)
297 ldim
= adg_ldim_new();
298 dim
= (AdgDim
*) ldim
;
301 adg_dim_set_ref1_from_pair(dim
, ref1
);
304 adg_dim_set_ref2_from_pair(dim
, ref2
);
307 adg_dim_set_pos_from_pair(dim
, pos
);
313 * adg_ldim_new_full_explicit:
314 * @ref1_x: the x coordinate of the first reference point
315 * @ref1_y: the y coordinate of the first reference point
316 * @ref2_x: the x coordinate of the second reference point
317 * @ref2_y: the y coordinate of the second reference point
318 * @pos_x: the x coordinate of the position reference
319 * @pos_y: the y coordinate of the position reference
320 * @direction: angle where to extend the dimension
322 * Wrappes adg_ldim_new_full() with explicit values.
324 * Returns: the newly created linear dimension entity
329 adg_ldim_new_full_explicit(gdouble ref1_x
, gdouble ref1_y
,
330 gdouble ref2_x
, gdouble ref2_y
,
331 gdouble pos_x
, gdouble pos_y
, gdouble direction
)
344 return adg_ldim_new_full(&ref1
, &ref2
, &pos
, direction
);
348 * adg_ldim_new_full_from_model:
349 * @model: transfer-none: the model from which the named pairs are taken
350 * @ref1: allow-none: the first reference point
351 * @ref2: allow-none: the second reference point
352 * @pos: allow-none: the position reference
353 * @direction: angle where to extend the dimension
355 * Creates a new linear dimension, specifing all the needed properties in
356 * one shot and using named pairs from @model.
358 * Returns: the newly created linear dimension entity
363 adg_ldim_new_full_from_model(AdgModel
*model
,
364 const gchar
*ref1
, const gchar
*ref2
,
365 const gchar
*pos
, gdouble direction
)
370 g_return_val_if_fail(model
!= NULL
, NULL
);
372 ldim
= adg_ldim_new();
373 dim
= (AdgDim
*) ldim
;
375 adg_ldim_set_direction(ldim
, direction
);
378 adg_dim_set_ref1_from_model(dim
, model
, ref1
);
381 adg_dim_set_ref2_from_model(dim
, model
, ref2
);
384 adg_dim_set_pos_from_model(dim
, model
, pos
);
386 return (AdgLDim
*) dim
;
390 * adg_ldim_set_direction:
391 * @ldim: an #AdgLDim entity
392 * @direction: an angle value, in radians
394 * Sets the direction angle where to extend @ldim.
395 * @direction is normalized by cpml_angle() before being used.
400 adg_ldim_set_direction(AdgLDim
*ldim
, gdouble direction
)
402 g_return_if_fail(ADG_IS_LDIM(ldim
));
403 g_object_set(ldim
, "direction", direction
, NULL
);
407 * adg_ldim_get_direction:
408 * @ldim: an #AdgLDim entity
410 * Gets the direction where @ldim will extend.
412 * Returns: the direction angle in radians
417 adg_ldim_get_direction(AdgLDim
*ldim
)
419 AdgLDimPrivate
*data
;
421 g_return_val_if_fail(ADG_IS_LDIM(ldim
), 0);
425 return data
->direction
;
429 * adg_ldim_switch_extension1:
430 * @ldim: an #AdgLDim entity
431 * @new_state: the new state
433 * Shows (if @new_state is %TRUE) or hide (if @new_state is %FALSE)
434 * the first extension line of @ldim.
439 adg_ldim_switch_extension1(AdgLDim
*ldim
, gboolean new_state
)
441 g_return_if_fail(ADG_IS_LDIM(ldim
));
442 g_return_if_fail(adg_is_boolean_value(new_state
));
443 g_object_set(ldim
, "has-extension1", new_state
, NULL
);
447 * adg_ldim_has_extension1:
448 * @ldim: an #AdgLDim entity
450 * Checks if @ldim should render also the first extension line.
452 * Returns: %TRUE on first extension line presents, %FALSE otherwise
457 adg_ldim_has_extension1(AdgLDim
*ldim
)
459 AdgLDimPrivate
*data
;
461 g_return_val_if_fail(ADG_IS_LDIM(ldim
), FALSE
);
465 return data
->has_extension1
;
469 * adg_ldim_switch_extension2:
470 * @ldim: an #AdgLDim entity
471 * @new_state: the new new_state
473 * Shows (if @new_state is %TRUE) or hide (if @new_state is %FALSE)
474 * the second extension line of @ldim.
479 adg_ldim_switch_extension2(AdgLDim
*ldim
, gboolean new_state
)
481 g_return_if_fail(ADG_IS_LDIM(ldim
));
482 g_return_if_fail(adg_is_boolean_value(new_state
));
483 g_object_set(ldim
, "has-extension2", new_state
, NULL
);
487 * adg_ldim_has_extension2:
488 * @ldim: an #AdgLDim entity
490 * Checks if @ldim should render also the second extension line.
492 * Returns: %TRUE on first extension line presents, %FALSE otherwise
497 adg_ldim_has_extension2(AdgLDim
*ldim
)
499 AdgLDimPrivate
*data
;
501 g_return_val_if_fail(ADG_IS_LDIM(ldim
), FALSE
);
505 return data
->has_extension2
;
510 _adg_global_changed(AdgEntity
*entity
)
512 AdgLDimPrivate
*data
= ((AdgLDim
*) entity
)->data
;
514 _adg_unset_trail((AdgLDim
*) entity
);
516 if (_ADG_OLD_ENTITY_CLASS
->global_changed
)
517 _ADG_OLD_ENTITY_CLASS
->global_changed(entity
);
520 adg_entity_global_changed((AdgEntity
*) data
->marker1
);
523 adg_entity_global_changed((AdgEntity
*) data
->marker2
);
527 _adg_local_changed(AdgEntity
*entity
)
529 _adg_unset_trail((AdgLDim
*) entity
);
531 if (_ADG_OLD_ENTITY_CLASS
->local_changed
)
532 _ADG_OLD_ENTITY_CLASS
->local_changed(entity
);
536 _adg_invalidate(AdgEntity
*entity
)
539 AdgLDimPrivate
*data
;
541 ldim
= (AdgLDim
*) entity
;
544 _adg_dispose_trail(ldim
);
545 _adg_dispose_markers(ldim
);
546 data
->geometry
.is_arranged
= FALSE
;
547 data
->shift
.is_arranged
= FALSE
;
548 _adg_unset_trail(ldim
);
550 if (_ADG_OLD_ENTITY_CLASS
->invalidate
)
551 _ADG_OLD_ENTITY_CLASS
->invalidate(entity
);
555 _adg_arrange(AdgEntity
*entity
)
559 AdgLDimPrivate
*data
;
561 AdgDimStyle
*dim_style
;
562 gboolean outside
, detach
;
563 const AdgMatrix
*local
;
564 AdgPair ref1
, ref2
, pos
, base1
, base2
;
567 if (_ADG_OLD_ENTITY_CLASS
->arrange
)
568 _ADG_OLD_ENTITY_CLASS
->arrange(entity
);
570 ldim
= (AdgLDim
*) entity
;
571 if (! _adg_update_geometry(ldim
))
574 dim
= (AdgDim
*) ldim
;
576 quote
= adg_dim_get_quote(dim
);
578 _adg_update_shift(ldim
);
579 _adg_update_entities(ldim
);
581 /* Check for cached result */
582 if (data
->cpml
.path
.status
== CAIRO_STATUS_SUCCESS
) {
583 adg_entity_set_global_map((AdgEntity
*) quote
, &data
->quote
.global_map
);
587 _adg_choose_flags(ldim
, &outside
, &detach
);
589 dim_style
= _ADG_GET_DIM_STYLE(dim
);
590 local
= adg_entity_get_local_matrix(entity
);
592 cpml_pair_copy(&ref1
, adg_point_get_pair(adg_dim_get_ref1(dim
)));
593 cpml_pair_copy(&ref2
, adg_point_get_pair(adg_dim_get_ref2(dim
)));
594 cpml_pair_copy(&pos
, adg_point_get_pair(adg_dim_get_pos(dim
)));
595 cpml_pair_copy(&base1
, &data
->geometry
.base1
);
596 cpml_pair_copy(&base2
, &data
->geometry
.base2
);
598 cpml_pair_transform(&ref1
, local
);
599 cpml_pair_transform(&ref2
, local
);
600 cpml_pair_transform(&pos
, local
);
601 cpml_pair_transform(&base1
, local
);
602 base1
.x
+= data
->shift
.base
.x
;
603 base1
.y
+= data
->shift
.base
.y
;
604 cpml_pair_transform(&base2
, local
);
605 base2
.x
+= data
->shift
.base
.x
;
606 base2
.y
+= data
->shift
.base
.y
;
608 pair
.x
= ref1
.x
+ data
->shift
.from
.x
;
609 pair
.y
= ref1
.y
+ data
->shift
.from
.y
;
610 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[13]);
612 cpml_pair_to_cairo(&base1
, &data
->cpml
.data
[1]);
614 pair
.x
= base1
.x
+ data
->shift
.to
.x
;
615 pair
.y
= base1
.y
+ data
->shift
.to
.y
;
616 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[15]);
618 pair
.x
= ref2
.x
+ data
->shift
.from
.x
;
619 pair
.y
= ref2
.y
+ data
->shift
.from
.y
;
620 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[17]);
622 cpml_pair_to_cairo(&base2
, &data
->cpml
.data
[3]);
624 pair
.x
= base2
.x
+ data
->shift
.to
.x
;
625 pair
.y
= base2
.y
+ data
->shift
.to
.y
;
626 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[19]);
628 /* Calculate the outside segments */
633 beyond
= adg_dim_style_get_beyond(dim_style
);
634 cpml_pair_from_cairo(&pair
, &data
->cpml
.data
[1]);
636 cpml_pair_from_cairo(&vector
, &data
->cpml
.data
[3]);
639 cpml_vector_set_length(&vector
, beyond
);
641 cpml_pair_from_cairo(&pair
, &data
->cpml
.data
[1]);
642 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[5]);
646 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[7]);
648 cpml_pair_from_cairo(&pair
, &data
->cpml
.data
[3]);
649 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[11]);
653 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[9]);
655 data
->cpml
.data
[2].header
.length
= 2;
657 data
->cpml
.data
[2].header
.length
= 10;
659 data
->cpml
.data
[10].header
.length
= 2;
661 _adg_update_quote(ldim
, &base1
, &base2
, &pos
, detach
, outside
,
662 adg_dim_style_get_quote_shift(dim_style
)->x
);
664 /* Play with header lengths to show or hide the extension lines */
665 if (data
->has_extension1
) {
666 data
->cpml
.data
[14].header
.length
= data
->has_extension2
? 2 : 6;
668 gint n
= ! outside
&& ! detach
? 2 : 10;
669 data
->cpml
.data
[14].header
.length
= 2;
670 data
->cpml
.data
[n
].header
.length
+= 4;
671 if (!data
->has_extension2
)
672 data
->cpml
.data
[n
].header
.length
+= 4;
675 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
677 /* Update the markers, if present */
678 if (data
->trail
!= NULL
) {
679 if (data
->marker1
!= NULL
)
680 adg_marker_set_segment(data
->marker1
, data
->trail
, outside
? 2 : 1);
681 if (data
->marker2
!= NULL
)
682 adg_marker_set_segment(data
->marker2
, data
->trail
, outside
? 3 : 1);
684 _adg_dispose_markers(ldim
);
687 _adg_update_extents(ldim
);
691 _adg_render(AdgEntity
*entity
, cairo_t
*cr
)
695 AdgLDimPrivate
*data
;
696 AdgDimStyle
*dim_style
;
698 const cairo_path_t
*cairo_path
;
700 ldim
= (AdgLDim
*) entity
;
701 dim
= (AdgDim
*) entity
;
704 if (!data
->geometry
.is_arranged
) {
705 /* Entity not arranged, probably due to undefined pair found */
709 dim_style
= _ADG_GET_DIM_STYLE(dim
);
711 adg_style_apply((AdgStyle
*) dim_style
, entity
, cr
);
712 adg_entity_render((AdgEntity
*) adg_dim_get_quote(dim
), cr
);
715 adg_entity_render((AdgEntity
*) data
->marker1
, cr
);
717 adg_entity_render((AdgEntity
*) data
->marker2
, cr
);
719 cairo_transform(cr
, adg_entity_get_global_matrix(entity
));
720 dress
= adg_dim_style_get_line_dress(dim_style
);
721 adg_entity_apply_dress(entity
, dress
, cr
);
723 cairo_path
= adg_trail_get_cairo_path(data
->trail
);
724 cairo_append_path(cr
, cairo_path
);
729 _adg_default_value(AdgDim
*dim
)
732 AdgLDimPrivate
*data
;
733 AdgDimStyle
*dim_style
;
736 ldim
= (AdgLDim
*) dim
;
738 dim_style
= _ADG_GET_DIM_STYLE(dim
);
739 format
= adg_dim_style_get_number_format(dim_style
);
741 if (! _adg_update_geometry(ldim
))
742 return g_strdup("undef");
744 return g_strdup_printf(format
, data
->geometry
.distance
);
748 _adg_update_geometry(AdgLDim
*ldim
)
750 AdgLDimPrivate
*data
;
752 const AdgPair
*ref1
, *ref2
;
754 CpmlVector baseline
, extension
;
758 dim
= (AdgDim
*) ldim
;
759 ref1
= adg_point_get_pair(adg_dim_get_ref1(dim
));
760 ref2
= adg_point_get_pair(adg_dim_get_ref2(dim
));
761 pos
= adg_point_get_pair(adg_dim_get_pos(dim
));
763 /* Check if the needed pairs are properly defined */
764 if (ref1
== NULL
|| ref2
== NULL
|| pos
== NULL
) {
765 data
->geometry
.is_arranged
= FALSE
;
767 } else if (data
->geometry
.is_arranged
) {
771 cpml_vector_from_angle(&extension
, data
->direction
);
772 cpml_pair_copy(&baseline
, &extension
);
773 cpml_vector_normal(&baseline
);
775 d
= extension
.y
* baseline
.x
-
776 extension
.x
* baseline
.y
;
777 g_return_val_if_fail(d
!= 0, FALSE
);
779 k
= ((pos
->y
- ref1
->y
) * baseline
.x
-
780 (pos
->x
- ref1
->x
) * baseline
.y
) / d
;
781 data
->geometry
.base1
.x
= ref1
->x
+ k
* extension
.x
;
782 data
->geometry
.base1
.y
= ref1
->y
+ k
* extension
.y
;
784 k
= ((pos
->y
- ref2
->y
) * baseline
.x
-
785 (pos
->x
- ref2
->x
) * baseline
.y
) / d
;
786 data
->geometry
.base2
.x
= ref2
->x
+ k
* extension
.x
;
787 data
->geometry
.base2
.y
= ref2
->y
+ k
* extension
.y
;
789 data
->geometry
.distance
= cpml_pair_distance(&data
->geometry
.base1
,
790 &data
->geometry
.base2
);
792 data
->geometry
.is_arranged
= TRUE
;
798 _adg_update_shift(AdgLDim
*ldim
)
800 AdgLDimPrivate
*data
;
801 AdgDimStyle
*dim_style
;
802 gdouble from_offset
, to_offset
;
803 gdouble baseline_spacing
, level
;
808 if (data
->shift
.is_arranged
)
811 dim_style
= _ADG_GET_DIM_STYLE(ldim
);
812 from_offset
= adg_dim_style_get_from_offset(dim_style
);
813 to_offset
= adg_dim_style_get_to_offset(dim_style
);
814 baseline_spacing
= adg_dim_style_get_baseline_spacing(dim_style
);
815 level
= adg_dim_get_level((AdgDim
*) ldim
);
817 cpml_vector_from_angle(&vector
, data
->direction
);
819 cpml_vector_set_length(&vector
, from_offset
);
820 cpml_pair_copy(&data
->shift
.from
, &vector
);
822 cpml_vector_set_length(&vector
, to_offset
);
823 cpml_pair_copy(&data
->shift
.to
, &vector
);
825 cpml_vector_set_length(&vector
, level
* baseline_spacing
);
826 cpml_pair_copy(&data
->shift
.base
, &vector
);
828 data
->shift
.is_arranged
= TRUE
;
832 _adg_update_entities(AdgLDim
*ldim
)
835 AdgLDimPrivate
*data
;
836 AdgDimStyle
*dim_style
;
838 entity
= (AdgEntity
*) ldim
;
840 dim_style
= _ADG_GET_DIM_STYLE(ldim
);
842 if (data
->trail
== NULL
)
843 data
->trail
= adg_trail_new(_adg_trail_callback
, ldim
);
845 if (data
->marker1
== NULL
) {
846 data
->marker1
= adg_dim_style_marker1_new(dim_style
);
847 adg_entity_set_parent((AdgEntity
*) data
->marker1
, entity
);
850 if (data
->marker2
== NULL
) {
851 data
->marker2
= adg_dim_style_marker2_new(dim_style
);
852 adg_entity_set_parent((AdgEntity
*) data
->marker2
, entity
);
857 _adg_choose_flags(AdgLDim
*ldim
, gboolean
*to_outside
, gboolean
*to_detach
)
860 AdgThreeState outside
, detached
;
861 AdgLDimPrivate
*data
;
862 const AdgMatrix
*local
, *global
;
863 gdouble local_factor
, global_factor
;
864 gdouble available_space
, markers_space
, quote_space
;
866 dim
= (AdgDim
*) ldim
;
867 outside
= adg_dim_get_outside(dim
);
868 detached
= adg_dim_get_detached(dim
);
870 *to_outside
= outside
== ADG_THREE_STATE_ON
;
871 *to_detach
= detached
== ADG_THREE_STATE_ON
;
873 /* On explicit flags, no further investigation is required */
874 if (outside
!= ADG_THREE_STATE_UNKNOWN
&&
875 detached
!= ADG_THREE_STATE_UNKNOWN
)
879 local
= adg_entity_get_local_matrix((AdgEntity
*) ldim
);
880 global
= adg_entity_get_global_matrix((AdgEntity
*) ldim
);
881 local_factor
= (local
->xx
+ local
->yy
) / 2;
882 global_factor
= (global
->xx
+ global
->yy
) / 2;
883 available_space
= data
->geometry
.distance
* local_factor
* global_factor
;
886 if (outside
!= ADG_THREE_STATE_ON
) {
887 if (data
->marker1
!= NULL
)
888 markers_space
+= adg_marker_get_size(data
->marker1
);
889 if (data
->marker2
!= NULL
)
890 markers_space
+= adg_marker_get_size(data
->marker2
);
892 markers_space
*= global_factor
;
895 if (detached
== ADG_THREE_STATE_ON
) {
896 /* Leave at least 0.25 markers_space between the markers */
897 quote_space
= markers_space
* 0.25;
899 AdgEntity
*quote
= (AdgEntity
*) adg_dim_get_quote(dim
);
900 adg_entity_arrange(quote
);
901 quote_space
= adg_entity_get_extents(quote
)->size
.x
;
904 if (outside
== ADG_THREE_STATE_UNKNOWN
&&
905 detached
== ADG_THREE_STATE_UNKNOWN
) {
906 /* Both flags need to be choosed */
907 if (quote_space
+ markers_space
< available_space
) {
910 } else if (quote_space
< available_space
) {
915 *to_outside
= markers_space
* 1.25 > available_space
;
917 } else if (outside
== ADG_THREE_STATE_UNKNOWN
) {
918 /* Only the outside flag may be guessed */
919 *to_outside
= quote_space
+ markers_space
> available_space
;
921 /* Only the detached flag may be guessed */
922 *to_detach
= quote_space
+ markers_space
> available_space
;
927 _adg_update_quote(AdgLDim
*ldim
, AdgPair
*base1
, AdgPair
*base2
, AdgPair
*pos
,
928 gboolean detach
, gboolean outside
, gdouble gap
)
930 AdgLDimPrivate
*data
;
931 AdgEntity
*quote_entity
;
932 AdgPair middle
, factor
, org
;
936 quote_entity
= (AdgEntity
*) adg_dim_get_quote((AdgDim
*) ldim
);
937 if (quote_entity
== NULL
)
940 data
= (AdgLDimPrivate
*) ldim
->data
;
941 middle
.x
= (base1
->x
+ base2
->x
) / 2;
942 middle
.y
= (base1
->y
+ base2
->y
) / 2;
943 dir
.x
= base2
->x
- base1
->x
;
944 dir
.y
= base2
->y
- base1
->y
;
945 cpml_vector_set_length(&dir
, 1);
948 /* Detached quote: position the quote at "pos" */
949 AdgPair tmp_pair
, quote_end
;
951 gdouble distance1
, distance2
, quote_size
;
953 cairo_path_data_t
*to_extend
;
955 /* Set "org" to the properly converted "pos" coordinates */
956 org
.x
= pos
->x
+ data
->shift
.base
.x
;
957 org
.y
= pos
->y
+ data
->shift
.base
.y
;
959 /* Check in which side of the base line the quote must be rendered.
960 * This is done by checking if the (pos..middle) vector is closer
961 * to the base line vector or to its inverse, that is if they are
962 * concordants or discordants. For this purpose, an algorithm
963 * based on squared distances is used. */
964 tmp_pair
.x
= org
.x
- middle
.x
;
965 tmp_pair
.y
= org
.y
- middle
.y
;
968 distance2
= cpml_pair_squared_distance(&dir
, &tmp_pair
);
969 distance1
= cpml_pair_squared_distance(&vector
, &tmp_pair
);
970 on_side1
= distance2
> distance1
;
972 /* Properly align the quote, depending on its side */
973 factor
.x
= on_side1
? 1 : 0;
977 /* Add a gap between the quote and the extension line */
978 org
.x
+= vector
.x
* gap
;
979 org
.y
+= vector
.y
* gap
;
981 /* Calculate the end point (on the base line) of the quote */
982 quote_size
= adg_entity_get_extents(quote_entity
)->size
.x
;
983 quote_end
.x
= org
.x
+ vector
.x
* quote_size
;
984 quote_end
.y
= org
.y
+ vector
.y
* quote_size
;
986 /* Extends the base line to include the "quote_end" pair,
987 * so a detached quote is properly underlined */
989 to_extend
= &data
->cpml
.data
[on_side1
? 7 : 9];
991 to_extend
= &data
->cpml
.data
[9];
992 cpml_pair_to_cairo(&middle
, &data
->cpml
.data
[9]);
993 cpml_pair_to_cairo(on_side1
? base1
: base2
, &data
->cpml
.data
[11]);
994 data
->cpml
.data
[2].header
.length
= 6;
997 /* Extend the base line only if needed */
998 cpml_pair_from_cairo(&tmp_pair
, to_extend
);
999 distance1
= cpml_pair_squared_distance("e_end
, &middle
);
1000 distance2
= cpml_pair_squared_distance(&tmp_pair
, &middle
);
1001 if (distance1
> distance2
)
1002 cpml_pair_to_cairo("e_end
, to_extend
);
1004 /* Center the quote in the middle of the base line */
1006 cpml_pair_copy(&org
, &middle
);
1010 adg_alignment_set_factor((AdgAlignment
*) quote_entity
, &factor
);
1011 cairo_matrix_init(&map
, dir
.x
, dir
.y
, -dir
.y
, dir
.x
, org
.x
, org
.y
);
1012 adg_entity_set_global_map(quote_entity
, &map
);
1013 adg_entity_arrange(quote_entity
);
1015 adg_matrix_copy(&data
->quote
.global_map
, &map
);
1019 _adg_update_extents(AdgLDim
*ldim
)
1021 AdgLDimPrivate
*data
;
1022 CpmlExtents new_extents
;
1023 const CpmlExtents
*extents
;
1025 AdgEntity
*marker_entity
;
1028 quote
= (AdgEntity
*) adg_dim_get_quote((AdgDim
*) ldim
);
1029 new_extents
.is_defined
= FALSE
;
1031 /* The quote is always present (otherwise something bad happened) */
1032 extents
= adg_entity_get_extents(quote
);
1033 if (extents
!= NULL
)
1034 cpml_extents_add(&new_extents
, extents
);
1036 if (data
->trail
!= NULL
) {
1037 extents
= adg_trail_get_extents(data
->trail
);
1038 if (extents
!= NULL
)
1039 cpml_extents_add(&new_extents
, extents
);
1042 if (data
->marker1
!= NULL
) {
1043 marker_entity
= (AdgEntity
*) data
->marker1
;
1044 adg_entity_local_changed(marker_entity
);
1045 adg_entity_arrange(marker_entity
);
1046 extents
= adg_entity_get_extents(marker_entity
);
1047 if (extents
!= NULL
)
1048 cpml_extents_add(&new_extents
, extents
);
1051 if (data
->marker2
!= NULL
) {
1052 marker_entity
= (AdgEntity
*) data
->marker2
;
1053 adg_entity_local_changed(marker_entity
);
1054 adg_entity_arrange(marker_entity
);
1055 extents
= adg_entity_get_extents(marker_entity
);
1056 if (extents
!= NULL
)
1057 cpml_extents_add(&new_extents
, extents
);
1060 adg_entity_set_extents((AdgEntity
*) ldim
, &new_extents
);
1064 _adg_unset_trail(AdgLDim
*ldim
)
1066 AdgLDimPrivate
*data
= ldim
->data
;
1068 if (data
->trail
!= NULL
)
1069 adg_model_clear((AdgModel
*) data
->trail
);
1071 data
->cpml
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
1075 _adg_dispose_trail(AdgLDim
*ldim
)
1077 AdgLDimPrivate
*data
= ldim
->data
;
1079 if (data
->trail
!= NULL
) {
1080 g_object_unref(data
->trail
);
1086 _adg_dispose_markers(AdgLDim
*ldim
)
1088 AdgLDimPrivate
*data
= ldim
->data
;
1090 if (data
->marker1
!= NULL
) {
1091 g_object_unref(data
->marker1
);
1092 data
->marker1
= NULL
;
1095 if (data
->marker2
!= NULL
) {
1096 g_object_unref(data
->marker2
);
1097 data
->marker2
= NULL
;
1102 _adg_trail_callback(AdgTrail
*trail
, gpointer user_data
)
1105 AdgLDimPrivate
*data
;
1107 ldim
= (AdgLDim
*) user_data
;
1110 return &data
->cpml
.path
;