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