1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 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 cairo_path_t
* _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
->cairo
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
172 data
->cairo
.path
.data
= data
->cairo
.data
;
173 data
->cairo
.path
.num_data
= G_N_ELEMENTS(data
->cairo
.data
);
174 data
->cairo
.path
.data
[0] = move_to
;
175 data
->cairo
.path
.data
[2] = line_to
;
176 data
->cairo
.path
.data
[4] = move_to
;
177 data
->cairo
.path
.data
[6] = line_to
;
178 data
->cairo
.path
.data
[8] = move_to
;
179 data
->cairo
.path
.data
[10] = line_to
;
180 data
->cairo
.path
.data
[12] = move_to
;
181 data
->cairo
.path
.data
[14] = line_to
;
182 data
->cairo
.path
.data
[16] = move_to
;
183 data
->cairo
.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 CpmlPair
*ref1
, const CpmlPair
*ref2
,
292 const CpmlPair
*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
);
309 adg_ldim_set_direction(ldim
, direction
);
314 * adg_ldim_new_full_explicit:
315 * @ref1_x: the x coordinate of the first reference point
316 * @ref1_y: the y coordinate of the first reference point
317 * @ref2_x: the x coordinate of the second reference point
318 * @ref2_y: the y coordinate of the second reference point
319 * @pos_x: the x coordinate of the position reference
320 * @pos_y: the y coordinate of the position reference
321 * @direction: angle where to extend the dimension
323 * Wrappes adg_ldim_new_full() with explicit values.
325 * Returns: the newly created linear dimension entity
330 adg_ldim_new_full_explicit(gdouble ref1_x
, gdouble ref1_y
,
331 gdouble ref2_x
, gdouble ref2_y
,
332 gdouble pos_x
, gdouble pos_y
, gdouble direction
)
345 return adg_ldim_new_full(&ref1
, &ref2
, &pos
, direction
);
349 * adg_ldim_new_full_from_model:
350 * @model: (transfer none): the model from which the named pairs are taken
351 * @ref1: (allow-none): the first reference point
352 * @ref2: (allow-none): the second reference point
353 * @pos: (allow-none): the position reference
354 * @direction: angle where to extend the dimension
356 * Creates a new linear dimension, specifing all the needed properties in
357 * one shot and using named pairs from @model.
359 * Returns: the newly created linear dimension entity
364 adg_ldim_new_full_from_model(AdgModel
*model
,
365 const gchar
*ref1
, const gchar
*ref2
,
366 const gchar
*pos
, gdouble direction
)
371 g_return_val_if_fail(model
!= NULL
, NULL
);
373 ldim
= adg_ldim_new();
374 dim
= (AdgDim
*) ldim
;
377 adg_dim_set_ref1_from_model(dim
, model
, ref1
);
380 adg_dim_set_ref2_from_model(dim
, model
, ref2
);
383 adg_dim_set_pos_from_model(dim
, model
, pos
);
385 adg_ldim_set_direction(ldim
, direction
);
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 <constant>TRUE</constant>) or hide (if
434 * @new_state is <constant>FALSE</constant>) the first extension
440 adg_ldim_switch_extension1(AdgLDim
*ldim
, gboolean new_state
)
442 g_return_if_fail(ADG_IS_LDIM(ldim
));
443 g_return_if_fail(adg_is_boolean_value(new_state
));
444 g_object_set(ldim
, "has-extension1", new_state
, NULL
);
448 * adg_ldim_has_extension1:
449 * @ldim: an #AdgLDim entity
451 * Checks if @ldim should render also the first extension line.
453 * Returns: <constant>TRUE</constant> on first extension line presents, <constant>FALSE</constant> otherwise.
458 adg_ldim_has_extension1(AdgLDim
*ldim
)
460 AdgLDimPrivate
*data
;
462 g_return_val_if_fail(ADG_IS_LDIM(ldim
), FALSE
);
466 return data
->has_extension1
;
470 * adg_ldim_switch_extension2:
471 * @ldim: an #AdgLDim entity
472 * @new_state: the new new_state
474 * Shows (if @new_state is <constant>TRUE</constant>) or hide (if
475 * @new_state is <constant>FALSE</constant>) the second extension
481 adg_ldim_switch_extension2(AdgLDim
*ldim
, gboolean new_state
)
483 g_return_if_fail(ADG_IS_LDIM(ldim
));
484 g_return_if_fail(adg_is_boolean_value(new_state
));
485 g_object_set(ldim
, "has-extension2", new_state
, NULL
);
489 * adg_ldim_has_extension2:
490 * @ldim: an #AdgLDim entity
492 * Checks if @ldim should render also the second extension line.
494 * Returns: <constant>TRUE</constant> on first extension line presents, <constant>FALSE</constant> otherwise.
499 adg_ldim_has_extension2(AdgLDim
*ldim
)
501 AdgLDimPrivate
*data
;
503 g_return_val_if_fail(ADG_IS_LDIM(ldim
), FALSE
);
507 return data
->has_extension2
;
512 _adg_global_changed(AdgEntity
*entity
)
514 AdgLDimPrivate
*data
= ((AdgLDim
*) entity
)->data
;
516 _adg_unset_trail((AdgLDim
*) entity
);
518 if (_ADG_OLD_ENTITY_CLASS
->global_changed
)
519 _ADG_OLD_ENTITY_CLASS
->global_changed(entity
);
522 adg_entity_global_changed((AdgEntity
*) data
->marker1
);
525 adg_entity_global_changed((AdgEntity
*) data
->marker2
);
529 _adg_local_changed(AdgEntity
*entity
)
531 _adg_unset_trail((AdgLDim
*) entity
);
533 if (_ADG_OLD_ENTITY_CLASS
->local_changed
)
534 _ADG_OLD_ENTITY_CLASS
->local_changed(entity
);
538 _adg_invalidate(AdgEntity
*entity
)
541 AdgLDimPrivate
*data
;
543 ldim
= (AdgLDim
*) entity
;
546 _adg_dispose_trail(ldim
);
547 _adg_dispose_markers(ldim
);
548 data
->geometry
.is_arranged
= FALSE
;
549 data
->shift
.is_arranged
= FALSE
;
550 _adg_unset_trail(ldim
);
552 if (_ADG_OLD_ENTITY_CLASS
->invalidate
)
553 _ADG_OLD_ENTITY_CLASS
->invalidate(entity
);
557 _adg_arrange(AdgEntity
*entity
)
561 AdgLDimPrivate
*data
;
563 AdgDimStyle
*dim_style
;
564 gboolean outside
, detach
;
565 const cairo_matrix_t
*local
;
566 CpmlPair ref1
, ref2
, pos
, base1
, base2
;
569 if (_ADG_OLD_ENTITY_CLASS
->arrange
)
570 _ADG_OLD_ENTITY_CLASS
->arrange(entity
);
572 ldim
= (AdgLDim
*) entity
;
573 if (! _adg_update_geometry(ldim
))
576 dim
= (AdgDim
*) ldim
;
578 quote
= adg_dim_get_quote(dim
);
580 _adg_update_shift(ldim
);
581 _adg_update_entities(ldim
);
583 /* Check for cached result */
584 if (data
->cairo
.path
.status
== CAIRO_STATUS_SUCCESS
) {
585 adg_entity_set_global_map((AdgEntity
*) quote
, &data
->quote
.global_map
);
589 _adg_choose_flags(ldim
, &outside
, &detach
);
591 dim_style
= _ADG_GET_DIM_STYLE(dim
);
592 local
= adg_entity_get_local_matrix(entity
);
594 cpml_pair_copy(&ref1
, (CpmlPair
*) adg_dim_get_ref1(dim
));
595 cpml_pair_copy(&ref2
, (CpmlPair
*) adg_dim_get_ref2(dim
));
596 cpml_pair_copy(&pos
, (CpmlPair
*) adg_dim_get_pos(dim
));
597 cpml_pair_copy(&base1
, &data
->geometry
.base1
);
598 cpml_pair_copy(&base2
, &data
->geometry
.base2
);
600 cpml_pair_transform(&ref1
, local
);
601 cpml_pair_transform(&ref2
, local
);
602 cpml_pair_transform(&pos
, local
);
603 cpml_pair_transform(&base1
, local
);
604 base1
.x
+= data
->shift
.base
.x
;
605 base1
.y
+= data
->shift
.base
.y
;
606 cpml_pair_transform(&base2
, local
);
607 base2
.x
+= data
->shift
.base
.x
;
608 base2
.y
+= data
->shift
.base
.y
;
610 pair
.x
= ref1
.x
+ data
->shift
.from
.x
;
611 pair
.y
= ref1
.y
+ data
->shift
.from
.y
;
612 cpml_pair_to_cairo(&pair
, &data
->cairo
.data
[13]);
614 cpml_pair_to_cairo(&base1
, &data
->cairo
.data
[1]);
616 pair
.x
= base1
.x
+ data
->shift
.to
.x
;
617 pair
.y
= base1
.y
+ data
->shift
.to
.y
;
618 cpml_pair_to_cairo(&pair
, &data
->cairo
.data
[15]);
620 pair
.x
= ref2
.x
+ data
->shift
.from
.x
;
621 pair
.y
= ref2
.y
+ data
->shift
.from
.y
;
622 cpml_pair_to_cairo(&pair
, &data
->cairo
.data
[17]);
624 cpml_pair_to_cairo(&base2
, &data
->cairo
.data
[3]);
626 pair
.x
= base2
.x
+ data
->shift
.to
.x
;
627 pair
.y
= base2
.y
+ data
->shift
.to
.y
;
628 cpml_pair_to_cairo(&pair
, &data
->cairo
.data
[19]);
630 /* Calculate the outside segments */
635 beyond
= adg_dim_style_get_beyond(dim_style
);
636 cpml_pair_from_cairo(&pair
, &data
->cairo
.data
[1]);
638 cpml_pair_from_cairo(&vector
, &data
->cairo
.data
[3]);
641 cpml_vector_set_length(&vector
, beyond
);
643 cpml_pair_from_cairo(&pair
, &data
->cairo
.data
[1]);
644 cpml_pair_to_cairo(&pair
, &data
->cairo
.data
[5]);
648 cpml_pair_to_cairo(&pair
, &data
->cairo
.data
[7]);
650 cpml_pair_from_cairo(&pair
, &data
->cairo
.data
[3]);
651 cpml_pair_to_cairo(&pair
, &data
->cairo
.data
[11]);
655 cpml_pair_to_cairo(&pair
, &data
->cairo
.data
[9]);
657 data
->cairo
.data
[2].header
.length
= 2;
659 data
->cairo
.data
[2].header
.length
= 10;
661 data
->cairo
.data
[10].header
.length
= 2;
663 _adg_update_quote(ldim
, &base1
, &base2
, &pos
, detach
, outside
,
664 adg_dim_style_get_quote_shift(dim_style
)->x
);
666 /* Play with header lengths to show or hide the extension lines */
667 if (data
->has_extension1
) {
668 data
->cairo
.data
[14].header
.length
= data
->has_extension2
? 2 : 6;
670 gint n
= ! outside
&& ! detach
? 2 : 10;
671 data
->cairo
.data
[14].header
.length
= 2;
672 data
->cairo
.data
[n
].header
.length
+= 4;
673 if (!data
->has_extension2
)
674 data
->cairo
.data
[n
].header
.length
+= 4;
677 data
->cairo
.path
.status
= CAIRO_STATUS_SUCCESS
;
679 /* Update the markers, if present */
680 if (data
->trail
!= NULL
) {
681 if (data
->marker1
!= NULL
)
682 adg_marker_set_segment(data
->marker1
, data
->trail
, outside
? 2 : 1);
683 if (data
->marker2
!= NULL
)
684 adg_marker_set_segment(data
->marker2
, data
->trail
, outside
? 3 : 1);
686 _adg_dispose_markers(ldim
);
689 _adg_update_extents(ldim
);
693 _adg_render(AdgEntity
*entity
, cairo_t
*cr
)
697 AdgLDimPrivate
*data
;
698 AdgDimStyle
*dim_style
;
700 const cairo_path_t
*cairo_path
;
702 ldim
= (AdgLDim
*) entity
;
705 if (!data
->geometry
.is_arranged
) {
706 /* Entity not arranged, probably due to undefined pair found */
710 dim
= (AdgDim
*) entity
;
711 dim_style
= _ADG_GET_DIM_STYLE(dim
);
713 adg_style_apply((AdgStyle
*) dim_style
, entity
, cr
);
714 adg_entity_render((AdgEntity
*) adg_dim_get_quote(dim
), cr
);
717 adg_entity_render((AdgEntity
*) data
->marker1
, cr
);
719 adg_entity_render((AdgEntity
*) data
->marker2
, cr
);
721 cairo_transform(cr
, adg_entity_get_global_matrix(entity
));
722 dress
= adg_dim_style_get_line_dress(dim_style
);
723 adg_entity_apply_dress(entity
, dress
, cr
);
725 cairo_path
= adg_trail_get_cairo_path(data
->trail
);
726 cairo_append_path(cr
, cairo_path
);
731 _adg_default_value(AdgDim
*dim
)
734 AdgLDimPrivate
*data
;
735 AdgDimStyle
*dim_style
;
738 ldim
= (AdgLDim
*) dim
;
740 dim_style
= _ADG_GET_DIM_STYLE(dim
);
741 format
= adg_dim_style_get_number_format(dim_style
);
743 if (! _adg_update_geometry(ldim
))
744 return g_strdup("undef");
746 return g_strdup_printf(format
, data
->geometry
.distance
);
750 _adg_update_geometry(AdgLDim
*ldim
)
752 AdgLDimPrivate
*data
;
754 AdgPoint
*ref1_point
, *ref2_point
, *pos_point
;
755 const CpmlPair
*ref1
, *ref2
, *pos
;
756 CpmlVector baseline
, extension
;
761 /* Check for cached results */
762 if (data
->geometry
.is_arranged
)
765 dim
= (AdgDim
*) ldim
;
766 ref1_point
= adg_dim_get_ref1(dim
);
767 ref2_point
= adg_dim_get_ref2(dim
);
768 pos_point
= adg_dim_get_pos(dim
);
770 /* Check if the needed points are all defined */
771 if (! adg_point_update(ref1_point
) ||
772 ! adg_point_update(ref2_point
) ||
773 ! adg_point_update(pos_point
))
776 ref1
= (CpmlPair
*) ref1_point
;
777 ref2
= (CpmlPair
*) ref2_point
;
778 pos
= (CpmlPair
*) pos_point
;
780 /* Check if the given points have valid coordinates */
781 if (cpml_pair_equal(ref1
, ref2
)) {
782 g_warning(_("%s: ref1 and ref2 cannot be coincidents (%lf, %lf)"),
783 G_STRLOC
, ref1
->x
, ref1
->y
);
787 cpml_vector_from_angle(&extension
, data
->direction
);
788 cpml_pair_copy(&baseline
, &extension
);
789 cpml_vector_normal(&baseline
);
791 d
= extension
.y
* baseline
.x
-
792 extension
.x
* baseline
.y
;
793 g_return_val_if_fail(d
!= 0, FALSE
);
795 k
= ((pos
->y
- ref1
->y
) * baseline
.x
-
796 (pos
->x
- ref1
->x
) * baseline
.y
) / d
;
797 data
->geometry
.base1
.x
= ref1
->x
+ k
* extension
.x
;
798 data
->geometry
.base1
.y
= ref1
->y
+ k
* extension
.y
;
800 k
= ((pos
->y
- ref2
->y
) * baseline
.x
-
801 (pos
->x
- ref2
->x
) * baseline
.y
) / d
;
802 data
->geometry
.base2
.x
= ref2
->x
+ k
* extension
.x
;
803 data
->geometry
.base2
.y
= ref2
->y
+ k
* extension
.y
;
805 data
->geometry
.distance
= cpml_pair_distance(&data
->geometry
.base1
,
806 &data
->geometry
.base2
);
808 data
->geometry
.is_arranged
= TRUE
;
814 _adg_update_shift(AdgLDim
*ldim
)
816 AdgLDimPrivate
*data
;
817 AdgDimStyle
*dim_style
;
818 gdouble from_offset
, to_offset
;
819 gdouble baseline_spacing
, level
;
824 if (data
->shift
.is_arranged
)
827 dim_style
= _ADG_GET_DIM_STYLE(ldim
);
828 from_offset
= adg_dim_style_get_from_offset(dim_style
);
829 to_offset
= adg_dim_style_get_to_offset(dim_style
);
830 baseline_spacing
= adg_dim_style_get_baseline_spacing(dim_style
);
831 level
= adg_dim_get_level((AdgDim
*) ldim
);
833 cpml_vector_from_angle(&vector
, data
->direction
);
835 cpml_vector_set_length(&vector
, from_offset
);
836 cpml_pair_copy(&data
->shift
.from
, &vector
);
838 cpml_vector_set_length(&vector
, to_offset
);
839 cpml_pair_copy(&data
->shift
.to
, &vector
);
841 cpml_vector_set_length(&vector
, level
* baseline_spacing
);
842 cpml_pair_copy(&data
->shift
.base
, &vector
);
844 data
->shift
.is_arranged
= TRUE
;
848 _adg_update_entities(AdgLDim
*ldim
)
851 AdgLDimPrivate
*data
;
852 AdgDimStyle
*dim_style
;
854 entity
= (AdgEntity
*) ldim
;
856 dim_style
= _ADG_GET_DIM_STYLE(ldim
);
858 if (data
->trail
== NULL
)
859 data
->trail
= adg_trail_new(_adg_trail_callback
, ldim
);
861 if (data
->marker1
== NULL
) {
862 data
->marker1
= adg_dim_style_marker1_new(dim_style
);
863 adg_entity_set_parent((AdgEntity
*) data
->marker1
, entity
);
866 if (data
->marker2
== NULL
) {
867 data
->marker2
= adg_dim_style_marker2_new(dim_style
);
868 adg_entity_set_parent((AdgEntity
*) data
->marker2
, entity
);
873 _adg_choose_flags(AdgLDim
*ldim
, gboolean
*to_outside
, gboolean
*to_detach
)
876 AdgThreeState outside
, detached
;
877 AdgLDimPrivate
*data
;
878 const cairo_matrix_t
*local
, *global
;
879 gdouble local_factor
, global_factor
;
880 gdouble available_space
, markers_space
, quote_space
;
882 dim
= (AdgDim
*) ldim
;
883 outside
= adg_dim_get_outside(dim
);
884 detached
= adg_dim_get_detached(dim
);
886 *to_outside
= outside
== ADG_THREE_STATE_ON
;
887 *to_detach
= detached
== ADG_THREE_STATE_ON
;
889 /* On explicit flags, no further investigation is required */
890 if (outside
!= ADG_THREE_STATE_UNKNOWN
&&
891 detached
!= ADG_THREE_STATE_UNKNOWN
)
895 local
= adg_entity_get_local_matrix((AdgEntity
*) ldim
);
896 global
= adg_entity_get_global_matrix((AdgEntity
*) ldim
);
897 local_factor
= (local
->xx
+ local
->yy
) / 2;
898 global_factor
= (global
->xx
+ global
->yy
) / 2;
899 available_space
= data
->geometry
.distance
* local_factor
* global_factor
;
902 if (outside
!= ADG_THREE_STATE_ON
) {
903 if (data
->marker1
!= NULL
)
904 markers_space
+= adg_marker_get_size(data
->marker1
);
905 if (data
->marker2
!= NULL
)
906 markers_space
+= adg_marker_get_size(data
->marker2
);
908 markers_space
*= global_factor
;
911 if (detached
== ADG_THREE_STATE_ON
) {
912 /* Leave at least 0.25 markers_space between the markers */
913 quote_space
= markers_space
* 0.25;
915 AdgEntity
*quote
= (AdgEntity
*) adg_dim_get_quote(dim
);
916 adg_entity_arrange(quote
);
917 quote_space
= adg_entity_get_extents(quote
)->size
.x
;
920 if (outside
== ADG_THREE_STATE_UNKNOWN
&&
921 detached
== ADG_THREE_STATE_UNKNOWN
) {
922 /* Both flags need to be choosed */
923 if (quote_space
+ markers_space
< available_space
) {
926 } else if (quote_space
< available_space
) {
931 *to_outside
= markers_space
* 1.25 > available_space
;
933 } else if (outside
== ADG_THREE_STATE_UNKNOWN
) {
934 /* Only the outside flag may be guessed */
935 *to_outside
= quote_space
+ markers_space
> available_space
;
937 /* Only the detached flag may be guessed */
938 *to_detach
= quote_space
+ markers_space
> available_space
;
943 _adg_update_quote(AdgLDim
*ldim
, CpmlPair
*base1
, CpmlPair
*base2
, CpmlPair
*pos
,
944 gboolean detach
, gboolean outside
, gdouble gap
)
946 AdgLDimPrivate
*data
;
947 AdgEntity
*quote_entity
;
948 CpmlPair middle
, factor
, org
;
952 quote_entity
= (AdgEntity
*) adg_dim_get_quote((AdgDim
*) ldim
);
953 if (quote_entity
== NULL
)
956 data
= (AdgLDimPrivate
*) ldim
->data
;
957 middle
.x
= (base1
->x
+ base2
->x
) / 2;
958 middle
.y
= (base1
->y
+ base2
->y
) / 2;
959 dir
.x
= base2
->x
- base1
->x
;
960 dir
.y
= base2
->y
- base1
->y
;
961 cpml_vector_set_length(&dir
, 1);
964 /* Detached quote: position the quote at "pos" */
965 CpmlPair tmp_pair
, quote_end
;
967 gdouble distance1
, distance2
, quote_size
;
969 cairo_path_data_t
*to_extend
;
971 /* Set "org" to the properly converted "pos" coordinates */
972 org
.x
= pos
->x
+ data
->shift
.base
.x
;
973 org
.y
= pos
->y
+ data
->shift
.base
.y
;
975 /* Check in which side of the base line the quote must be rendered.
976 * This is done by checking if the (pos..middle) vector is closer
977 * to the base line vector or to its inverse, that is if they are
978 * concordants or discordants. For this purpose, an algorithm
979 * based on squared distances is used. */
980 tmp_pair
.x
= org
.x
- middle
.x
;
981 tmp_pair
.y
= org
.y
- middle
.y
;
984 distance2
= cpml_pair_squared_distance(&dir
, &tmp_pair
);
985 distance1
= cpml_pair_squared_distance(&vector
, &tmp_pair
);
986 on_side1
= distance2
> distance1
;
988 /* Properly align the quote, depending on its side */
989 factor
.x
= on_side1
? 1 : 0;
993 /* Add a gap between the quote and the extension line */
994 org
.x
+= vector
.x
* gap
;
995 org
.y
+= vector
.y
* gap
;
997 /* Calculate the end point (on the base line) of the quote */
998 quote_size
= adg_entity_get_extents(quote_entity
)->size
.x
;
999 quote_end
.x
= org
.x
+ vector
.x
* quote_size
;
1000 quote_end
.y
= org
.y
+ vector
.y
* quote_size
;
1002 /* Extends the base line to include the "quote_end" pair,
1003 * so a detached quote is properly underlined */
1005 to_extend
= &data
->cairo
.data
[on_side1
? 7 : 9];
1007 to_extend
= &data
->cairo
.data
[9];
1008 cpml_pair_to_cairo(&middle
, &data
->cairo
.data
[9]);
1009 cpml_pair_to_cairo(on_side1
? base1
: base2
, &data
->cairo
.data
[11]);
1010 data
->cairo
.data
[2].header
.length
= 6;
1013 /* Extend the base line only if needed */
1014 cpml_pair_from_cairo(&tmp_pair
, to_extend
);
1015 distance1
= cpml_pair_squared_distance("e_end
, &middle
);
1016 distance2
= cpml_pair_squared_distance(&tmp_pair
, &middle
);
1017 if (distance1
> distance2
)
1018 cpml_pair_to_cairo("e_end
, to_extend
);
1020 /* Center the quote in the middle of the base line */
1022 cpml_pair_copy(&org
, &middle
);
1026 adg_alignment_set_factor((AdgAlignment
*) quote_entity
, &factor
);
1027 cairo_matrix_init(&map
, dir
.x
, dir
.y
, -dir
.y
, dir
.x
, org
.x
, org
.y
);
1028 adg_entity_set_global_map(quote_entity
, &map
);
1029 adg_entity_arrange(quote_entity
);
1031 adg_matrix_copy(&data
->quote
.global_map
, &map
);
1035 _adg_update_extents(AdgLDim
*ldim
)
1037 AdgLDimPrivate
*data
;
1038 CpmlExtents new_extents
;
1039 const CpmlExtents
*extents
;
1041 AdgEntity
*marker_entity
;
1044 quote
= (AdgEntity
*) adg_dim_get_quote((AdgDim
*) ldim
);
1045 new_extents
.is_defined
= FALSE
;
1047 /* The quote is always present (otherwise something bad happened) */
1048 extents
= adg_entity_get_extents(quote
);
1049 if (extents
!= NULL
)
1050 cpml_extents_add(&new_extents
, extents
);
1052 if (data
->trail
!= NULL
) {
1053 extents
= adg_trail_get_extents(data
->trail
);
1054 if (extents
!= NULL
)
1055 cpml_extents_add(&new_extents
, extents
);
1058 if (data
->marker1
!= NULL
) {
1059 marker_entity
= (AdgEntity
*) data
->marker1
;
1060 adg_entity_local_changed(marker_entity
);
1061 adg_entity_arrange(marker_entity
);
1062 extents
= adg_entity_get_extents(marker_entity
);
1063 if (extents
!= NULL
)
1064 cpml_extents_add(&new_extents
, extents
);
1067 if (data
->marker2
!= NULL
) {
1068 marker_entity
= (AdgEntity
*) data
->marker2
;
1069 adg_entity_local_changed(marker_entity
);
1070 adg_entity_arrange(marker_entity
);
1071 extents
= adg_entity_get_extents(marker_entity
);
1072 if (extents
!= NULL
)
1073 cpml_extents_add(&new_extents
, extents
);
1076 adg_entity_set_extents((AdgEntity
*) ldim
, &new_extents
);
1080 _adg_unset_trail(AdgLDim
*ldim
)
1082 AdgLDimPrivate
*data
= ldim
->data
;
1084 if (data
->trail
!= NULL
)
1085 adg_model_clear((AdgModel
*) data
->trail
);
1087 data
->cairo
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
1091 _adg_dispose_trail(AdgLDim
*ldim
)
1093 AdgLDimPrivate
*data
= ldim
->data
;
1095 if (data
->trail
!= NULL
) {
1096 g_object_unref(data
->trail
);
1102 _adg_dispose_markers(AdgLDim
*ldim
)
1104 AdgLDimPrivate
*data
= ldim
->data
;
1106 if (data
->marker1
!= NULL
) {
1107 g_object_unref(data
->marker1
);
1108 data
->marker1
= NULL
;
1111 if (data
->marker2
!= NULL
) {
1112 g_object_unref(data
->marker2
);
1113 data
->marker2
= NULL
;
1117 static cairo_path_t
*
1118 _adg_trail_callback(AdgTrail
*trail
, gpointer user_data
)
1121 AdgLDimPrivate
*data
;
1123 ldim
= (AdgLDim
*) user_data
;
1126 return &data
->cairo
.path
;