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: Angular dimensions
25 * The #AdgADim entity defines an angular dimension.
33 * All fields are privates 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-adim-private.h"
58 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_adim_parent_class)
59 #define _ADG_OLD_ENTITY_CLASS ((AdgEntityClass *) adg_adim_parent_class)
62 G_DEFINE_TYPE(AdgADim
, adg_adim
, ADG_TYPE_DIM
)
74 static void _adg_dispose (GObject
*object
);
75 static void _adg_get_property (GObject
*object
,
79 static void _adg_set_property (GObject
*object
,
83 static void _adg_global_changed (AdgEntity
*entity
);
84 static void _adg_local_changed (AdgEntity
*entity
);
85 static void _adg_invalidate (AdgEntity
*entity
);
86 static void _adg_arrange (AdgEntity
*entity
);
87 static void _adg_render (AdgEntity
*entity
,
89 static gchar
* _adg_default_value (AdgDim
*dim
);
90 static gboolean
_adg_update_geometry (AdgADim
*adim
);
91 static void _adg_update_entities (AdgADim
*adim
);
92 static void _adg_unset_trail (AdgADim
*adim
);
93 static void _adg_dispose_trail (AdgADim
*adim
);
94 static void _adg_dispose_markers (AdgADim
*adim
);
95 static gboolean
_adg_get_info (AdgADim
*adim
,
99 static CpmlPath
* _adg_trail_callback (AdgTrail
*trail
,
104 adg_adim_class_init(AdgADimClass
*klass
)
106 GObjectClass
*gobject_class
;
107 AdgEntityClass
*entity_class
;
108 AdgDimClass
*dim_class
;
109 GParamSpec
*param
, *old_param
;
111 gobject_class
= (GObjectClass
*) klass
;
112 entity_class
= (AdgEntityClass
*) klass
;
113 dim_class
= (AdgDimClass
*) klass
;
115 g_type_class_add_private(klass
, sizeof(AdgADimPrivate
));
117 gobject_class
->dispose
= _adg_dispose
;
118 gobject_class
->get_property
= _adg_get_property
;
119 gobject_class
->set_property
= _adg_set_property
;
121 entity_class
->global_changed
= _adg_global_changed
;
122 entity_class
->local_changed
= _adg_local_changed
;
123 entity_class
->invalidate
= _adg_invalidate
;
124 entity_class
->arrange
= _adg_arrange
;
125 entity_class
->render
= _adg_render
;
127 dim_class
->default_value
= _adg_default_value
;
129 /* Override #AdgDim:value to append a degree symbol
130 * to the default set value template string */
131 old_param
= g_object_class_find_property(gobject_class
, "value");
132 param
= g_param_spec_string(old_param
->name
,
133 g_param_spec_get_nick(old_param
),
134 g_param_spec_get_blurb(old_param
),
135 "<>" ADG_UTF8_DEGREE
,
137 g_object_class_install_property(gobject_class
, PROP_VALUE
, param
);
139 param
= g_param_spec_boxed("org1",
141 P_("Where the first line comes from: this point is used toghether with \"ref1\" to align the first extension line"),
144 g_object_class_install_property(gobject_class
, PROP_ORG1
, param
);
146 param
= g_param_spec_boxed("org2",
148 P_("Where the second line comes from: this point is used toghether with \"ref2\" to align the second extension line"),
151 g_object_class_install_property(gobject_class
, PROP_ORG2
, param
);
153 param
= g_param_spec_boolean("has-extension1",
154 P_("Has First Extension Line flag"),
155 P_("Show (TRUE) or hide (FALSE) the first extension line"),
156 TRUE
, G_PARAM_READWRITE
);
157 g_object_class_install_property(gobject_class
, PROP_HAS_EXTENSION1
, param
);
159 param
= g_param_spec_boolean("has-extension2",
160 P_("Has Second Extension Line flag"),
161 P_("Show (TRUE) or hide (FALSE) the second extension line"),
162 TRUE
, G_PARAM_READWRITE
);
163 g_object_class_install_property(gobject_class
, PROP_HAS_EXTENSION2
, param
);
167 adg_adim_init(AdgADim
*adim
)
169 AdgADimPrivate
*data
;
170 cairo_path_data_t move_to
, line_to
, arc_to
;
172 data
= G_TYPE_INSTANCE_GET_PRIVATE(adim
, ADG_TYPE_ADIM
, AdgADimPrivate
);
173 move_to
.header
.type
= CPML_MOVE
;
174 move_to
.header
.length
= 2;
175 line_to
.header
.type
= CPML_LINE
;
176 line_to
.header
.length
= 2;
177 arc_to
.header
.type
= CPML_ARC
;
178 arc_to
.header
.length
= 3;
182 data
->has_extension1
= TRUE
;
183 data
->has_extension2
= TRUE
;
185 data
->cpml
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
186 data
->cpml
.path
.data
= data
->cpml
.data
;
187 data
->cpml
.path
.num_data
= G_N_ELEMENTS(data
->cpml
.data
);
188 data
->cpml
.path
.data
[0] = move_to
;
189 data
->cpml
.path
.data
[2] = arc_to
;
190 data
->cpml
.path
.data
[5] = move_to
;
191 data
->cpml
.path
.data
[7] = line_to
;
192 data
->cpml
.path
.data
[9] = move_to
;
193 data
->cpml
.path
.data
[11] = line_to
;
196 data
->marker1
= NULL
;
197 data
->marker2
= NULL
;
199 data
->geometry_arranged
= FALSE
;
205 _adg_dispose(GObject
*object
)
209 AdgADimPrivate
*data
;
211 entity
= (AdgEntity
*) object
;
212 adim
= (AdgADim
*) entity
;
215 _adg_dispose_trail(adim
);
216 _adg_dispose_markers(adim
);
218 if (data
->org1
!= NULL
)
219 data
->org1
= adg_entity_point(entity
, data
->org1
, NULL
);
221 if (data
->org2
!= NULL
)
222 data
->org2
= adg_entity_point(entity
, data
->org2
, NULL
);
224 if (_ADG_OLD_OBJECT_CLASS
->dispose
)
225 _ADG_OLD_OBJECT_CLASS
->dispose(object
);
229 _adg_get_property(GObject
*object
, guint prop_id
,
230 GValue
*value
, GParamSpec
*pspec
)
232 AdgADimPrivate
*data
= ((AdgADim
*) object
)->data
;
236 g_value_set_string(value
, adg_dim_get_value((AdgDim
*) object
));
239 g_value_set_boxed(value
, data
->org1
);
242 g_value_set_boxed(value
, data
->org2
);
244 case PROP_HAS_EXTENSION1
:
245 g_value_set_boolean(value
, data
->has_extension1
);
247 case PROP_HAS_EXTENSION2
:
248 g_value_set_boolean(value
, data
->has_extension2
);
251 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
257 _adg_set_property(GObject
*object
, guint prop_id
,
258 const GValue
*value
, GParamSpec
*pspec
)
261 AdgADimPrivate
*data
;
263 entity
= (AdgEntity
*) object
;
264 data
= ((AdgADim
*) object
)->data
;
268 adg_dim_set_value((AdgDim
*) object
, g_value_get_string(value
));
271 data
->org1
= adg_entity_point(entity
, data
->org1
,
272 g_value_get_boxed(value
));
275 data
->org2
= adg_entity_point(entity
, data
->org2
,
276 g_value_get_boxed(value
));
278 case PROP_HAS_EXTENSION1
:
279 data
->has_extension1
= g_value_get_boolean(value
);
281 case PROP_HAS_EXTENSION2
:
282 data
->has_extension2
= g_value_get_boolean(value
);
285 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
294 * Creates a new - undefined - angular dimension. You must, at least,
295 * define the first line by setting #AdgADim:org1 (start point) and
296 * #AdgDim:ref1 (end point), the second line by setting #AdgADim:org2
297 * (start point) and #AdgDim:ref2 (end point) and the position of
298 * the quote in #AdgDim:pos.
300 * Returns: the newly created angular dimension entity
307 return g_object_new(ADG_TYPE_ADIM
, NULL
);
312 * @ref1: allow-none: first reference point
313 * @ref2: allow-none: second reference point
314 * @org1: allow-none: first origin point
315 * @org2: allow-none: second origin point
316 * @pos: allow-none: the position point
318 * Creates a new angular dimension, specifing all the needed
319 * properties in one shot using #CpmlPair.
321 * Returns: the newly created angular dimension entity
326 adg_adim_new_full(const CpmlPair
*ref1
, const CpmlPair
*ref2
,
327 const CpmlPair
*org1
, const CpmlPair
*org2
,
333 adim
= adg_adim_new();
334 dim
= (AdgDim
*) adim
;
337 adg_dim_set_ref1_from_pair(dim
, ref1
);
340 adg_dim_set_ref2_from_pair(dim
, ref2
);
343 adg_dim_set_pos_from_pair(dim
, pos
);
346 adg_adim_set_org1_from_pair(adim
, org1
);
349 adg_adim_set_org2_from_pair(adim
, org2
);
355 * adg_adim_new_full_explicit:
356 * @ref1_x: the x coordinate of end point of the first line
357 * @ref1_y: the y coordinate of end point of the first line
358 * @ref2_x: the x coordinate of end point of the second line
359 * @ref2_y: the y coordinate of end point of the second line
360 * @org1_x: the x coordinate of start point of the first line
361 * @org1_y: the y coordinate of start point of the first line
362 * @org2_x: the x coordinate of start point of the second line
363 * @org2_y: the y coordinate of start point of the second line
364 * @pos_x: the x coordinate of the position reference
365 * @pos_y: the y coordinate of the position reference
367 * Wrappes adg_adim_new_full() with explicit values.
369 * Returns: the newly created linear dimension entity
374 adg_adim_new_full_explicit(gdouble ref1_x
, gdouble ref1_y
,
375 gdouble ref2_x
, gdouble ref2_y
,
376 gdouble org1_x
, gdouble org1_y
,
377 gdouble org2_x
, gdouble org2_y
,
378 gdouble pos_x
, gdouble pos_y
)
380 CpmlPair ref1
, ref2
, org1
, org2
, pos
;
393 return adg_adim_new_full(&ref1
, &ref2
, &org1
, &org2
, &pos
);
397 * adg_adim_new_full_from_model:
398 * @model: transfer-none: the model from which the named pairs are taken
399 * @ref1: allow-none: the end point of the first line
400 * @ref2: allow-none: the end point of the second line
401 * @org1: allow-none: the origin of the first line
402 * @org2: allow-none: the origin of the second line
403 * @pos: allow-none: the position reference
405 * Creates a new angular dimension, specifing all the needed properties
406 * in one shot and using named pairs from @model.
408 * Returns: the newly created angular dimension entity
413 adg_adim_new_full_from_model(AdgModel
*model
,
414 const gchar
*ref1
, const gchar
*ref2
,
415 const gchar
*org1
, const gchar
*org2
,
421 adim
= adg_adim_new();
422 dim
= (AdgDim
*) adim
;
425 adg_dim_set_ref1_from_model(dim
, model
, ref1
);
428 adg_dim_set_ref2_from_model(dim
, model
, ref2
);
431 adg_dim_set_pos_from_model(dim
, model
, pos
);
434 adg_adim_set_org1_from_model(adim
, model
, org1
);
437 adg_adim_set_org2_from_model(adim
, model
, org2
);
445 * @org1: the new point to use as first reference
447 * Sets the #AdgADim:org1 property to @org1. The old point
448 * is silently discarded, unreferencing its model if that
449 * point was bound to a named pair (hence, possibly destroying
450 * the model if this was the last reference).
452 * @org1 can be %NULL, in which case the point is destroyed.
457 adg_adim_set_org1(AdgADim
*adim
, const AdgPoint
*org1
)
459 g_return_if_fail(ADG_IS_ADIM(adim
));
460 g_object_set(adim
, "org1", org1
, NULL
);
464 * adg_adim_set_org1_explicit:
466 * @x: x coordinate of the first reference point
467 * @y: y coordinate of the first reference point
469 * Sets the #AdgADim:org1 property to the (@x, @y) explicit
470 * coordinates. The old point is silently discarded,
471 * unreferencing its model if that point was bound to a named
472 * pair (hence, possibly destroying the model if this was the
478 adg_adim_set_org1_explicit(AdgADim
*adim
, gdouble x
, gdouble y
)
480 AdgPoint
*point
= adg_point_new();
482 adg_point_set_pair_explicit(point
, x
, y
);
483 adg_adim_set_org1(adim
, point
);
485 adg_point_destroy(point
);
489 * adg_adim_set_org1_from_pair:
491 * @org1: the coordinates pair of the first reference point
493 * Convenient function to set the #AdgADim:org1 property using a
494 * pair instead of explicit coordinates.
499 adg_adim_set_org1_from_pair(AdgADim
*adim
, const CpmlPair
*org1
)
501 g_return_if_fail(org1
!= NULL
);
503 adg_adim_set_org1_explicit(adim
, org1
->x
, org1
->y
);
507 * adg_adim_set_org1_from_model:
509 * @model: the source #AdgModel
510 * @org1: a named pair in @model
512 * Binds #AdgADim:org1 to the @org1 named pair of @model. If @model
513 * is %NULL, the point will be unset. In any case, the old point
514 * is silently discarded, unreferencing its model if that point
515 * was bound to a named pair (hence, possibly destroying the model
516 * if this was the last reference).
518 * The assignment is lazy so @org1 could be not be present in @model.
519 * Anyway, at the first access to this point an error will be raised
520 * if the named pair is still missing.
525 adg_adim_set_org1_from_model(AdgADim
*adim
, AdgModel
*model
, const gchar
*org1
)
527 AdgPoint
*point
= adg_point_new();
529 adg_point_set_pair_from_model(point
, model
, org1
);
530 adg_adim_set_org1(adim
, point
);
532 adg_point_destroy(point
);
539 * Gets the #AdgADim:org1 point. The returned point is internally owned
540 * and must not be freed or modified. Anyway, it is not const because
541 * adg_point_get_pair() must be able to modify the internal cache of
542 * the returned point.
544 * Returns: the first reference point
549 adg_adim_get_org1(AdgADim
*adim
)
551 AdgADimPrivate
*data
;
553 g_return_val_if_fail(ADG_IS_ADIM(adim
), NULL
);
563 * @org2: the new point to use as first reference
565 * Sets the #AdgADim:org2 property to @org2. The old point
566 * is silently discarded, unreferencing its model if that
567 * point was bound to a named pair (hence, possibly destroying
568 * the model if this was the last reference).
570 * @org2 can be %NULL, in which case the point is destroyed.
575 adg_adim_set_org2(AdgADim
*adim
, const AdgPoint
*org2
)
577 g_return_if_fail(ADG_IS_ADIM(adim
));
578 g_object_set(adim
, "org2", org2
, NULL
);
582 * adg_adim_set_org2_explicit:
584 * @x: x coordinate of the first reference point
585 * @y: y coordinate of the first reference point
587 * Sets the #AdgADim:org2 property to the (@x, @y) explicit
588 * coordinates. The old point is silently discarded,
589 * unreferencing its model if that point was bound to a named
590 * pair (hence, possibly destroying the model if this was the
596 adg_adim_set_org2_explicit(AdgADim
*adim
, gdouble x
, gdouble y
)
598 AdgPoint
*point
= adg_point_new();
600 adg_point_set_pair_explicit(point
, x
, y
);
601 adg_adim_set_org2(adim
, point
);
603 adg_point_destroy(point
);
607 * adg_adim_set_org2_from_pair:
609 * @org2: the coordinates pair of the first reference point
611 * Convenient function to set the #AdgADim:org2 property using a
612 * pair instead of explicit coordinates.
617 adg_adim_set_org2_from_pair(AdgADim
*adim
, const CpmlPair
*org2
)
619 g_return_if_fail(org2
!= NULL
);
621 adg_adim_set_org2_explicit(adim
, org2
->x
, org2
->y
);
625 * adg_adim_set_org2_from_model:
627 * @model: the source #AdgModel
628 * @org2: a named pair in @model
630 * Binds #AdgADim:org2 to the @org2 named pair of @model. If @model
631 * is %NULL, the point will be unset. In any case, the old point
632 * is silently discarded, unreferencing its model if that point
633 * was bound to a named pair (hence, possibly destroying the model
634 * if this was the last reference).
636 * The assignment is lazy so @org2 could be not be present in @model.
637 * Anyway, at the first access to this point an error will be raised
638 * if the named pair is still missing.
643 adg_adim_set_org2_from_model(AdgADim
*adim
, AdgModel
*model
, const gchar
*org2
)
645 AdgPoint
*point
= adg_point_new();
647 adg_point_set_pair_from_model(point
, model
, org2
);
648 adg_adim_set_org2(adim
, point
);
650 adg_point_destroy(point
);
657 * Gets the #AdgADim:org2 point. The returned point is internally owned
658 * and must not be freed or modified. Anyway, it is not const because
659 * adg_point_get_pair() must be able to modify the internal cache of
660 * the returned point.
662 * Returns: the first reference point
667 adg_adim_get_org2(AdgADim
*adim
)
669 AdgADimPrivate
*data
;
671 g_return_val_if_fail(ADG_IS_ADIM(adim
), NULL
);
679 * adg_adim_switch_extension1:
680 * @adim: an #AdgADim entity
681 * @new_state: the new state
683 * Shows (if @new_state is %TRUE) or hides (if @new_state is %FALSE)
684 * the first extension line of @adim.
689 adg_adim_switch_extension1(AdgADim
*adim
, gboolean new_state
)
691 g_return_if_fail(ADG_IS_ADIM(adim
));
692 g_return_if_fail(adg_is_boolean_value(new_state
));
693 g_object_set(adim
, "has-extension1", new_state
, NULL
);
697 * adg_adim_has_extension1:
698 * @adim: an #AdgADim entity
700 * Checks if @adim should render the first extension line.
702 * Returns: %TRUE on first extension line presents, %FALSE otherwise
707 adg_adim_has_extension1(AdgADim
*adim
)
709 AdgADimPrivate
*data
;
711 g_return_val_if_fail(ADG_IS_ADIM(adim
), FALSE
);
715 return data
->has_extension1
;
719 * adg_adim_switch_extension2:
720 * @adim: an #AdgADim entity
721 * @new_state: the new new_state
723 * Shows (if @new_state is %TRUE) or hides (if @new_state is %FALSE)
724 * the second extension line of @adim.
729 adg_adim_switch_extension2(AdgADim
*adim
, gboolean new_state
)
731 g_return_if_fail(ADG_IS_ADIM(adim
));
732 g_return_if_fail(adg_is_boolean_value(new_state
));
733 g_object_set(adim
, "has-extension2", new_state
, NULL
);
737 * adg_adim_has_extension2:
738 * @adim: an #AdgADim entity
740 * Checks if @adim should render the second extension line.
742 * Returns: %TRUE on first extension line presents, %FALSE otherwise
747 adg_adim_has_extension2(AdgADim
*adim
)
749 AdgADimPrivate
*data
;
751 g_return_val_if_fail(ADG_IS_ADIM(adim
), FALSE
);
755 return data
->has_extension2
;
760 _adg_global_changed(AdgEntity
*entity
)
762 AdgADimPrivate
*data
= ((AdgADim
*) entity
)->data
;
764 _adg_unset_trail((AdgADim
*) entity
);
766 if (_ADG_OLD_ENTITY_CLASS
->global_changed
)
767 _ADG_OLD_ENTITY_CLASS
->global_changed(entity
);
769 if (data
->marker1
!= NULL
)
770 adg_entity_global_changed((AdgEntity
*) data
->marker1
);
772 if (data
->marker2
!= NULL
)
773 adg_entity_global_changed((AdgEntity
*) data
->marker2
);
777 _adg_local_changed(AdgEntity
*entity
)
779 _adg_unset_trail((AdgADim
*) entity
);
781 if (_ADG_OLD_ENTITY_CLASS
->local_changed
)
782 _ADG_OLD_ENTITY_CLASS
->local_changed(entity
);
786 _adg_invalidate(AdgEntity
*entity
)
789 AdgADimPrivate
*data
;
791 adim
= (AdgADim
*) entity
;
794 _adg_dispose_trail(adim
);
795 _adg_dispose_markers(adim
);
796 data
->geometry_arranged
= FALSE
;
797 _adg_unset_trail(adim
);
800 adg_point_invalidate(data
->org1
);
802 adg_point_invalidate(data
->org2
);
804 if (_ADG_OLD_ENTITY_CLASS
->invalidate
)
805 _ADG_OLD_ENTITY_CLASS
->invalidate(entity
);
809 _adg_arrange(AdgEntity
*entity
)
813 AdgADimPrivate
*data
;
815 const AdgMatrix
*global
, *local
;
816 CpmlPair ref1
, ref2
, base1
, base12
, base2
;
819 AdgEntity
*marker_entity
;
821 if (_ADG_OLD_ENTITY_CLASS
->arrange
)
822 _ADG_OLD_ENTITY_CLASS
->arrange(entity
);
824 adim
= (AdgADim
*) entity
;
826 if (!_adg_update_geometry(adim
))
829 dim
= (AdgDim
*) adim
;
831 quote
= adg_dim_get_quote(dim
);
833 _adg_update_entities(adim
);
835 if (data
->cpml
.path
.status
== CAIRO_STATUS_SUCCESS
) {
836 AdgEntity
*quote_entity
= (AdgEntity
*) quote
;
837 adg_entity_set_global_map(quote_entity
, &data
->quote
.global_map
);
841 global
= adg_entity_get_global_matrix(entity
);
842 local
= adg_entity_get_local_matrix(entity
);
843 extents
.is_defined
= FALSE
;
845 cpml_pair_copy(&ref1
, adg_point_get_pair(adg_dim_get_ref1(dim
)));
846 cpml_pair_copy(&ref2
, adg_point_get_pair(adg_dim_get_ref2(dim
)));
847 cpml_pair_copy(&base1
, &data
->point
.base1
);
848 cpml_pair_copy(&base12
, &data
->point
.base12
);
849 cpml_pair_copy(&base2
, &data
->point
.base2
);
851 /* Apply the local matrix to the relevant points */
852 cpml_pair_transform(&ref1
, local
);
853 cpml_pair_transform(&ref2
, local
);
854 cpml_pair_transform(&base1
, local
);
855 cpml_pair_transform(&base12
, local
);
856 cpml_pair_transform(&base2
, local
);
858 /* Combine points and global shifts to build the path */
859 pair
.x
= ref1
.x
+ data
->shift
.from1
.x
;
860 pair
.y
= ref1
.y
+ data
->shift
.from1
.y
;
861 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[6]);
863 pair
.x
= base1
.x
+ data
->shift
.base1
.x
;
864 pair
.y
= base1
.y
+ data
->shift
.base1
.y
;
865 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[1]);
867 pair
.x
+= data
->shift
.to1
.x
;
868 pair
.y
+= data
->shift
.to1
.y
;
869 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[8]);
871 pair
.x
= base12
.x
+ data
->shift
.base12
.x
;
872 pair
.y
= base12
.y
+ data
->shift
.base12
.y
;
873 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[3]);
875 pair
.x
= ref2
.x
+ data
->shift
.from2
.x
;
876 pair
.y
= ref2
.y
+ data
->shift
.from2
.y
;
877 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[10]);
879 pair
.x
= base2
.x
+ data
->shift
.base2
.x
;
880 pair
.y
= base2
.y
+ data
->shift
.base2
.y
;
881 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[4]);
883 pair
.x
+= data
->shift
.to2
.x
;
884 pair
.y
+= data
->shift
.to2
.y
;
885 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[12]);
887 /* Play with header lengths to show or hide the extension lines */
888 if (data
->has_extension1
) {
889 data
->cpml
.data
[7].header
.length
= data
->has_extension2
? 2 : 6;
891 data
->cpml
.data
[2].header
.length
= data
->has_extension2
? 7 : 11;
894 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
896 /* Arrange the quote */
898 AdgEntity
*quote_entity
;
902 quote_entity
= (AdgEntity
*) quote
;
903 angle
= adg_dim_quote_angle(dim
, (data
->angle1
+ data
->angle2
) / 2 + G_PI_2
);
904 cpml_pair_from_cairo(&pair
, &data
->cpml
.data
[3]);
906 adg_alignment_set_factor_explicit(quote
, 0.5, 0);
908 cairo_matrix_init_translate(&map
, pair
.x
, pair
.y
);
909 cairo_matrix_rotate(&map
, angle
);
910 adg_entity_set_global_map(quote_entity
, &map
);
911 adg_entity_arrange(quote_entity
);
912 cpml_extents_add(&extents
, adg_entity_get_extents(quote_entity
));
914 adg_matrix_copy(&data
->quote
.global_map
, &map
);
917 /* Arrange the trail */
918 if (data
->trail
!= NULL
) {
919 CpmlExtents trail_extents
;
920 cpml_extents_copy(&trail_extents
, adg_trail_get_extents(data
->trail
));
921 cpml_extents_transform(&trail_extents
, global
);
922 cpml_extents_add(&extents
, &trail_extents
);
924 _adg_dispose_markers(adim
);
927 /* Arrange the markers */
928 if (data
->marker1
!= NULL
) {
929 marker_entity
= (AdgEntity
*) data
->marker1
;
930 adg_marker_set_segment(data
->marker1
, data
->trail
, 1);
931 adg_entity_local_changed(marker_entity
);
932 adg_entity_arrange(marker_entity
);
933 cpml_extents_add(&extents
, adg_entity_get_extents(marker_entity
));
935 if (data
->marker2
!= NULL
) {
936 marker_entity
= (AdgEntity
*) data
->marker2
;
937 adg_marker_set_segment(data
->marker2
, data
->trail
, 1);
938 adg_entity_local_changed(marker_entity
);
939 adg_entity_arrange(marker_entity
);
940 cpml_extents_add(&extents
, adg_entity_get_extents(marker_entity
));
943 adg_entity_set_extents(entity
, &extents
);
947 _adg_render(AdgEntity
*entity
, cairo_t
*cr
)
951 AdgADimPrivate
*data
;
952 AdgDimStyle
*dim_style
;
954 const cairo_path_t
*cairo_path
;
956 adim
= (AdgADim
*) entity
;
959 if (!data
->geometry_arranged
) {
960 /* Entity not arranged, probably due to undefined pair found */
964 dim
= (AdgDim
*) entity
;
965 dim_style
= _ADG_GET_DIM_STYLE(dim
);
967 adg_style_apply((AdgStyle
*) dim_style
, entity
, cr
);
968 adg_entity_render((AdgEntity
*) adg_dim_get_quote(dim
), cr
);
970 if (data
->marker1
!= NULL
)
971 adg_entity_render((AdgEntity
*) data
->marker1
, cr
);
972 if (data
->marker2
!= NULL
)
973 adg_entity_render((AdgEntity
*) data
->marker2
, cr
);
975 cairo_transform(cr
, adg_entity_get_global_matrix(entity
));
976 dress
= adg_dim_style_get_line_dress(dim_style
);
977 adg_entity_apply_dress(entity
, dress
, cr
);
979 cairo_path
= adg_trail_get_cairo_path(data
->trail
);
980 cairo_append_path(cr
, cairo_path
);
985 _adg_default_value(AdgDim
*dim
)
988 AdgADimPrivate
*data
;
989 AdgDimStyle
*dim_style
;
993 adim
= (AdgADim
*) dim
;
995 dim_style
= _ADG_GET_DIM_STYLE(dim
);
996 format
= adg_dim_style_get_number_format(dim_style
);
998 if (!_adg_update_geometry(adim
))
999 return g_strdup("undef");
1001 angle
= (data
->angle2
- data
->angle1
) * 180 / G_PI
;
1002 return g_strdup_printf(format
, angle
);
1005 /* With "geometry" is considered any data (point, vector or angle)
1006 * that can be cached: this is strictly related on how the arrange()
1009 _adg_update_geometry(AdgADim
*adim
)
1011 AdgADimPrivate
*data
;
1012 AdgDimStyle
*dim_style
;
1013 gdouble from_offset
, to_offset
;
1014 gdouble spacing
, level
;
1015 CpmlVector vector
[3];
1021 if (data
->geometry_arranged
)
1023 else if (!_adg_get_info(adim
, vector
, ¢er
, &distance
))
1026 dim_style
= _ADG_GET_DIM_STYLE(adim
);
1027 from_offset
= adg_dim_style_get_from_offset(dim_style
);
1028 to_offset
= adg_dim_style_get_to_offset(dim_style
);
1029 spacing
= adg_dim_style_get_baseline_spacing(dim_style
);
1030 level
= adg_dim_get_level((AdgDim
*) adim
);
1033 cpml_vector_set_length(&vector
[0], from_offset
);
1034 cpml_pair_copy(&data
->shift
.from1
, &vector
[0]);
1037 cpml_vector_set_length(&vector
[0], level
* spacing
);
1038 cpml_pair_copy(&data
->shift
.base1
, &vector
[0]);
1041 cpml_vector_set_length(&vector
[0], to_offset
);
1042 cpml_pair_copy(&data
->shift
.to1
, &vector
[0]);
1045 cpml_vector_set_length(&vector
[2], from_offset
);
1046 cpml_pair_copy(&data
->shift
.from2
, &vector
[2]);
1049 cpml_vector_set_length(&vector
[2], level
* spacing
);
1050 cpml_pair_copy(&data
->shift
.base2
, &vector
[2]);
1053 cpml_vector_set_length(&vector
[2], to_offset
);
1054 cpml_pair_copy(&data
->shift
.to2
, &vector
[2]);
1057 cpml_vector_set_length(&vector
[1], level
* spacing
);
1058 cpml_pair_copy(&data
->shift
.base12
, &vector
[1]);
1060 /* Distance can be 0, so the following will leave the
1061 * vector array in undefined state */
1064 cpml_vector_set_length(&vector
[0], distance
);
1065 data
->point
.base1
.x
= vector
[0].x
+ center
.x
;
1066 data
->point
.base1
.y
= vector
[0].y
+ center
.y
;
1069 cpml_vector_set_length(&vector
[2], distance
);
1070 data
->point
.base2
.x
= vector
[2].x
+ center
.x
;
1071 data
->point
.base2
.y
= vector
[2].y
+ center
.y
;
1074 cpml_vector_set_length(&vector
[1], distance
);
1075 data
->point
.base12
.x
= vector
[1].x
+ center
.x
;
1076 data
->point
.base12
.y
= vector
[1].y
+ center
.y
;
1078 data
->geometry_arranged
= TRUE
;
1084 _adg_update_entities(AdgADim
*adim
)
1087 AdgADimPrivate
*data
;
1088 AdgDimStyle
*dim_style
;
1090 entity
= (AdgEntity
*) adim
;
1092 dim_style
= _ADG_GET_DIM_STYLE(adim
);
1094 if (data
->trail
== NULL
)
1095 data
->trail
= adg_trail_new(_adg_trail_callback
, adim
);
1097 if (data
->marker1
== NULL
) {
1098 data
->marker1
= adg_dim_style_marker1_new(dim_style
);
1099 adg_entity_set_parent((AdgEntity
*) data
->marker1
, entity
);
1102 if (data
->marker2
== NULL
) {
1103 data
->marker2
= adg_dim_style_marker2_new(dim_style
);
1104 adg_entity_set_parent((AdgEntity
*) data
->marker2
, entity
);
1109 _adg_unset_trail(AdgADim
*adim
)
1111 AdgADimPrivate
*data
= adim
->data
;
1113 if (data
->trail
!= NULL
)
1114 adg_model_clear((AdgModel
*) data
->trail
);
1116 data
->cpml
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
1120 _adg_dispose_trail(AdgADim
*adim
)
1122 AdgADimPrivate
*data
= adim
->data
;
1124 if (data
->trail
!= NULL
) {
1125 g_object_unref(data
->trail
);
1131 _adg_dispose_markers(AdgADim
*adim
)
1133 AdgADimPrivate
*data
= adim
->data
;
1135 if (data
->marker1
!= NULL
) {
1136 g_object_unref(data
->marker1
);
1137 data
->marker1
= NULL
;
1140 if (data
->marker2
!= NULL
) {
1141 g_object_unref(data
->marker2
);
1142 data
->marker2
= NULL
;
1147 _adg_get_info(AdgADim
*adim
, CpmlVector vector
[],
1148 CpmlPair
*center
, gdouble
*distance
)
1151 AdgADimPrivate
*data
;
1152 const CpmlPair
*ref1
, *ref2
, *pos
;
1153 const CpmlPair
*org1
, *org2
;
1156 dim
= (AdgDim
*) adim
;
1158 ref1
= adg_point_get_pair(adg_dim_get_ref1(dim
));
1159 ref2
= adg_point_get_pair(adg_dim_get_ref2(dim
));
1160 pos
= adg_point_get_pair(adg_dim_get_pos(dim
));
1161 org1
= adg_point_get_pair(data
->org1
);
1162 org2
= adg_point_get_pair(data
->org2
);
1164 if (ref1
== NULL
|| ref2
== NULL
|| pos
== NULL
||
1165 org1
== NULL
|| org2
== NULL
)
1168 vector
[0].x
= ref1
->x
- org1
->x
;
1169 vector
[0].y
= ref1
->y
- org1
->y
;
1170 vector
[2].x
= ref2
->x
- org2
->x
;
1171 vector
[2].y
= ref2
->y
- org2
->y
;
1173 factor
= vector
[0].x
* vector
[2].y
- vector
[0].y
* vector
[2].x
;
1175 /* Parallel lines: hang with an error message */
1176 g_warning(_("%s: trying to set an angular dimension on parallel lines"),
1181 factor
= ((ref1
->y
- ref2
->y
) * vector
[2].x
-
1182 (ref1
->x
- ref2
->x
) * vector
[2].y
) / factor
;
1184 center
->x
= ref1
->x
+ vector
[0].x
* factor
;
1185 center
->y
= ref1
->y
+ vector
[0].y
* factor
;
1186 *distance
= cpml_pair_distance(center
, pos
);
1187 data
->angle1
= cpml_vector_angle(&vector
[0]);
1188 data
->angle2
= cpml_vector_angle(&vector
[2]);
1189 while (data
->angle2
< data
->angle1
)
1190 data
->angle2
+= G_PI
* 2;
1192 cpml_vector_from_angle(&vector
[1], (data
->angle1
+ data
->angle2
) / 2);
1198 _adg_trail_callback(AdgTrail
*trail
, gpointer user_data
)
1201 AdgADimPrivate
*data
;
1203 adim
= (AdgADim
*) user_data
;
1206 return &data
->cpml
.path
;