1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010 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"
38 #include "adg-adim-private.h"
39 #include "adg-dim-private.h"
41 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_adim_parent_class)
42 #define _ADG_OLD_ENTITY_CLASS ((AdgEntityClass *) adg_adim_parent_class)
45 G_DEFINE_TYPE(AdgADim
, adg_adim
, ADG_TYPE_DIM
);
57 static void _adg_dispose (GObject
*object
);
58 static void _adg_get_property (GObject
*object
,
62 static void _adg_set_property (GObject
*object
,
66 static void _adg_global_changed (AdgEntity
*entity
);
67 static void _adg_local_changed (AdgEntity
*entity
);
68 static void _adg_invalidate (AdgEntity
*entity
);
69 static void _adg_arrange (AdgEntity
*entity
);
70 static void _adg_render (AdgEntity
*entity
,
72 static gchar
* _adg_default_value (AdgDim
*dim
);
73 static gboolean
_adg_update_geometry (AdgADim
*adim
);
74 static void _adg_update_entities (AdgADim
*adim
);
75 static void _adg_unset_trail (AdgADim
*adim
);
76 static void _adg_dispose_markers (AdgADim
*adim
);
77 static gboolean
_adg_get_info (AdgADim
*adim
,
81 static CpmlPath
* _adg_trail_callback (AdgTrail
*trail
,
86 adg_adim_class_init(AdgADimClass
*klass
)
88 GObjectClass
*gobject_class
;
89 AdgEntityClass
*entity_class
;
90 AdgDimClass
*dim_class
;
91 GParamSpec
*param
, *old_param
;
93 gobject_class
= (GObjectClass
*) klass
;
94 entity_class
= (AdgEntityClass
*) klass
;
95 dim_class
= (AdgDimClass
*) klass
;
97 g_type_class_add_private(klass
, sizeof(AdgADimPrivate
));
99 gobject_class
->dispose
= _adg_dispose
;
100 gobject_class
->get_property
= _adg_get_property
;
101 gobject_class
->set_property
= _adg_set_property
;
103 entity_class
->global_changed
= _adg_global_changed
;
104 entity_class
->local_changed
= _adg_local_changed
;
105 entity_class
->invalidate
= _adg_invalidate
;
106 entity_class
->arrange
= _adg_arrange
;
107 entity_class
->render
= _adg_render
;
109 dim_class
->default_value
= _adg_default_value
;
111 /* Override #AdgDim:value to append a degree symbol
112 * to the default set value template string */
113 old_param
= g_object_class_find_property(gobject_class
, "value");
114 param
= g_param_spec_string(old_param
->name
,
115 g_param_spec_get_nick(old_param
),
116 g_param_spec_get_blurb(old_param
),
117 "<>" ADG_UTF8_DEGREE
,
119 g_object_class_install_property(gobject_class
, PROP_VALUE
, param
);
121 param
= g_param_spec_boxed("org1",
123 P_("Where the first line comes from: this point is used toghether with \"ref1\" to align the first extension line"),
126 g_object_class_install_property(gobject_class
, PROP_ORG1
, param
);
128 param
= g_param_spec_boxed("org2",
130 P_("Where the second line comes from: this point is used toghether with \"ref2\" to align the second extension line"),
133 g_object_class_install_property(gobject_class
, PROP_ORG2
, param
);
135 param
= g_param_spec_boolean("has-extension1",
136 P_("Has First Extension Line flag"),
137 P_("Show (TRUE) or hide (FALSE) the first extension line"),
138 TRUE
, G_PARAM_READWRITE
);
139 g_object_class_install_property(gobject_class
, PROP_HAS_EXTENSION1
, param
);
141 param
= g_param_spec_boolean("has-extension2",
142 P_("Has Second Extension Line flag"),
143 P_("Show (TRUE) or hide (FALSE) the second extension line"),
144 TRUE
, G_PARAM_READWRITE
);
145 g_object_class_install_property(gobject_class
, PROP_HAS_EXTENSION2
, param
);
149 adg_adim_init(AdgADim
*adim
)
151 AdgADimPrivate
*data
;
152 cairo_path_data_t move_to
, line_to
, arc_to
;
154 data
= G_TYPE_INSTANCE_GET_PRIVATE(adim
, ADG_TYPE_ADIM
, AdgADimPrivate
);
155 move_to
.header
.type
= CPML_MOVE
;
156 move_to
.header
.length
= 2;
157 line_to
.header
.type
= CPML_LINE
;
158 line_to
.header
.length
= 2;
159 arc_to
.header
.type
= CPML_ARC
;
160 arc_to
.header
.length
= 3;
164 data
->has_extension1
= TRUE
;
165 data
->has_extension2
= TRUE
;
167 data
->cpml
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
168 data
->cpml
.path
.data
= data
->cpml
.data
;
169 data
->cpml
.path
.num_data
= G_N_ELEMENTS(data
->cpml
.data
);
170 data
->cpml
.path
.data
[0] = move_to
;
171 data
->cpml
.path
.data
[2] = arc_to
;
172 data
->cpml
.path
.data
[5] = move_to
;
173 data
->cpml
.path
.data
[7] = line_to
;
174 data
->cpml
.path
.data
[9] = move_to
;
175 data
->cpml
.path
.data
[11] = line_to
;
178 data
->marker1
= NULL
;
179 data
->marker2
= NULL
;
181 data
->geometry_arranged
= FALSE
;
187 _adg_dispose(GObject
*object
)
190 AdgADimPrivate
*data
;
192 entity
= (AdgEntity
*) object
;
193 data
= ((AdgADim
*) object
)->data
;
195 _adg_dispose_markers((AdgADim
*) object
);
197 if (data
->org1
!= NULL
)
198 data
->org1
= adg_entity_point(entity
, data
->org1
, NULL
);
200 if (data
->org2
!= NULL
)
201 data
->org2
= adg_entity_point(entity
, data
->org2
, NULL
);
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 AdgADimPrivate
*data
= ((AdgADim
*) object
)->data
;
215 g_value_set_string(value
, adg_dim_get_value((AdgDim
*) object
));
218 g_value_set_boxed(value
, data
->org1
);
221 g_value_set_boxed(value
, data
->org2
);
223 case PROP_HAS_EXTENSION1
:
224 g_value_set_boolean(value
, data
->has_extension1
);
226 case PROP_HAS_EXTENSION2
:
227 g_value_set_boolean(value
, data
->has_extension2
);
230 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
236 _adg_set_property(GObject
*object
, guint prop_id
,
237 const GValue
*value
, GParamSpec
*pspec
)
240 AdgADimPrivate
*data
;
242 entity
= (AdgEntity
*) object
;
243 data
= ((AdgADim
*) object
)->data
;
247 adg_dim_set_value((AdgDim
*) object
, g_value_get_string(value
));
250 data
->org1
= adg_entity_point(entity
, data
->org1
,
251 g_value_get_boxed(value
));
254 data
->org2
= adg_entity_point(entity
, data
->org2
,
255 g_value_get_boxed(value
));
257 case PROP_HAS_EXTENSION1
:
258 data
->has_extension1
= g_value_get_boolean(value
);
260 case PROP_HAS_EXTENSION2
:
261 data
->has_extension2
= g_value_get_boolean(value
);
264 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
273 * Creates a new - undefined - angular dimension. You must, at least,
274 * define the first line by setting #AdgADim:org1 (start point) and
275 * #AdgDim:ref1 (end point), the second line by setting #AdgADim:org2
276 * (start point) and #AdgDim:ref2 (end point) and the position of
277 * the quote in #AdgDim:pos.
279 * Returns: the newly created angular dimension entity
284 return g_object_new(ADG_TYPE_ADIM
, NULL
);
289 * @ref1: first reference point
290 * @ref2: second reference point
291 * @org1: first origin point
292 * @org2: second origin point
294 * Creates a new angular dimension, specifing all the needed
295 * properties in one shot using #AdgPair.
297 * Returns: the newly created angular dimension entity
300 adg_adim_new_full(const AdgPair
*ref1
, const AdgPair
*ref2
,
301 const AdgPair
*org1
, const AdgPair
*org2
,
307 adim
= g_object_new(ADG_TYPE_ADIM
, NULL
);
308 dim
= (AdgDim
*) adim
;
310 adg_dim_set_ref1_from_pair(dim
, ref1
);
311 adg_dim_set_ref2_from_pair(dim
, ref2
);
312 adg_dim_set_pos_from_pair(dim
, pos
);
313 adg_adim_set_org1_from_pair(adim
, org1
);
314 adg_adim_set_org2_from_pair(adim
, org2
);
320 * adg_adim_new_full_explicit:
321 * @ref1_x: the x coordinate of the first reference point
322 * @ref1_y: the y coordinate of the first reference point
323 * @ref2_x: the x coordinate of the second reference point
324 * @ref2_y: the y coordinate of the second reference point
325 * @direction: angle where to extend the dimension
326 * @pos_x: the x coordinate of the position reference
327 * @pos_y: the y coordinate of the position reference
329 * Wrappes adg_adim_new_full() with explicit values.
331 * Returns: the newly created linear dimension entity
334 adg_adim_new_full_explicit(gdouble ref1_x
, gdouble ref1_y
,
335 gdouble ref2_x
, gdouble ref2_y
,
336 gdouble org1_x
, gdouble org1_y
,
337 gdouble org2_x
, gdouble org2_y
,
338 gdouble pos_x
, gdouble pos_y
)
340 AdgPair ref1
, ref2
, org1
, org2
, pos
;
353 return adg_adim_new_full(&ref1
, &ref2
, &org1
, &org2
, &pos
);
357 * adg_adim_new_full_from_model:
358 * @model: the model from which the named pairs are taken
359 * @ref1: the end point of the first line
360 * @ref2: the end point of the second line
361 * @org1: the origin of the first line
362 * @org2: the origin of the second line
363 * @pos: the position reference
365 * Creates a new angular dimension, specifing all the needed properties
366 * in one shot and using named pairs from @model.
368 * Returns: the newly created angular dimension entity
371 adg_adim_new_full_from_model(AdgModel
*model
,
372 const gchar
*ref1
, const gchar
*ref2
,
373 const gchar
*org1
, const gchar
*org2
,
379 adim
= g_object_new(ADG_TYPE_ADIM
, NULL
);
380 dim
= (AdgDim
*) adim
;
382 adg_dim_set_ref1_from_model(dim
, model
, ref1
);
383 adg_dim_set_ref2_from_model(dim
, model
, ref2
);
384 adg_dim_set_pos_from_model(dim
, model
, pos
);
385 adg_adim_set_org1_from_model(adim
, model
, org1
);
386 adg_adim_set_org2_from_model(adim
, model
, org2
);
394 * @org1: the new point to use as first reference
396 * Sets the #AdgADim:org1 property to @org1. The old point
397 * is silently discarded, unreferencing its model if that
398 * point was bound to a named pair (hence, possibly destroying
399 * the model if this was the last reference).
401 * @org1 can be %NULL, in which case the point is destroyed.
404 adg_adim_set_org1(AdgADim
*adim
, const AdgPoint
*org1
)
406 g_return_if_fail(ADG_IS_ADIM(adim
));
407 g_object_set((GObject
*) adim
, "org1", org1
, NULL
);
411 * adg_adim_set_org1_explicit:
413 * @x: x coordinate of the first reference point
414 * @y: y coordinate of the first reference point
416 * Sets the #AdgADim:org1 property to the (@x, @y) explicit
417 * coordinates. The old point is silently discarded,
418 * unreferencing its model if that point was bound to a named
419 * pair (hence, possibly destroying the model if this was the
423 adg_adim_set_org1_explicit(AdgADim
*adim
, gdouble x
, gdouble y
)
425 AdgPoint
*point
= adg_point_new();
427 adg_point_set_pair_explicit(point
, x
, y
);
428 adg_adim_set_org1(adim
, point
);
430 adg_point_destroy(point
);
434 * adg_adim_set_org1_from_pair:
436 * @org1: the coordinates pair of the first reference point
438 * Convenient function to set the #AdgADim:org1 property using a
439 * pair instead of explicit coordinates.
442 adg_adim_set_org1_from_pair(AdgADim
*adim
, const AdgPair
*org1
)
444 g_return_if_fail(org1
!= NULL
);
446 adg_adim_set_org1_explicit(adim
, org1
->x
, org1
->y
);
450 * adg_adim_set_org1_from_model:
452 * @model: the source #AdgModel
453 * @org1: a named pair in @model
455 * Binds #AdgADim:org1 to the @org1 named pair of @model. If @model
456 * is %NULL, the point will be unset. In any case, the old point
457 * is silently discarded, unreferencing its model if that point
458 * was bound to a named pair (hence, possibly destroying the model
459 * if this was the last reference).
461 * The assignment is lazy so @org1 could be not be present in @model.
462 * Anyway, at the first access to this point an error will be raised
463 * if the named pair is still missing.
466 adg_adim_set_org1_from_model(AdgADim
*adim
, AdgModel
*model
, const gchar
*org1
)
468 AdgPoint
*point
= adg_point_new();
470 adg_point_set_pair_from_model(point
, model
, org1
);
471 adg_adim_set_org1(adim
, point
);
473 adg_point_destroy(point
);
480 * Gets the #AdgADim:org1 point. The returned point is internally owned
481 * and must not be freed or modified. Anyway, it is not const because
482 * adg_point_get_pair() must be able to modify the internal cache of
483 * the returned point.
485 * Returns: the first reference point
488 adg_adim_get_org1(AdgADim
*adim
)
490 AdgADimPrivate
*data
;
492 g_return_val_if_fail(ADG_IS_ADIM(adim
), NULL
);
502 * @org2: the new point to use as first reference
504 * Sets the #AdgADim:org2 property to @org2. The old point
505 * is silently discarded, unreferencing its model if that
506 * point was bound to a named pair (hence, possibly destroying
507 * the model if this was the last reference).
509 * @org2 can be %NULL, in which case the point is destroyed.
512 adg_adim_set_org2(AdgADim
*adim
, const AdgPoint
*org2
)
514 g_return_if_fail(ADG_IS_ADIM(adim
));
515 g_object_set((GObject
*) adim
, "org2", org2
, NULL
);
519 * adg_adim_set_org2_explicit:
521 * @x: x coordinate of the first reference point
522 * @y: y coordinate of the first reference point
524 * Sets the #AdgADim:org2 property to the (@x, @y) explicit
525 * coordinates. The old point is silently discarded,
526 * unreferencing its model if that point was bound to a named
527 * pair (hence, possibly destroying the model if this was the
531 adg_adim_set_org2_explicit(AdgADim
*adim
, gdouble x
, gdouble y
)
533 AdgPoint
*point
= adg_point_new();
535 adg_point_set_pair_explicit(point
, x
, y
);
536 adg_adim_set_org2(adim
, point
);
538 adg_point_destroy(point
);
542 * adg_adim_set_org2_from_pair:
544 * @org2: the coordinates pair of the first reference point
546 * Convenient function to set the #AdgADim:org2 property using a
547 * pair instead of explicit coordinates.
550 adg_adim_set_org2_from_pair(AdgADim
*adim
, const AdgPair
*org2
)
552 g_return_if_fail(org2
!= NULL
);
554 adg_adim_set_org2_explicit(adim
, org2
->x
, org2
->y
);
558 * adg_adim_set_org2_from_model:
560 * @model: the source #AdgModel
561 * @org2: a named pair in @model
563 * Binds #AdgADim:org2 to the @org2 named pair of @model. If @model
564 * is %NULL, the point will be unset. In any case, the old point
565 * is silently discarded, unreferencing its model if that point
566 * was bound to a named pair (hence, possibly destroying the model
567 * if this was the last reference).
569 * The assignment is lazy so @org2 could be not be present in @model.
570 * Anyway, at the first access to this point an error will be raised
571 * if the named pair is still missing.
574 adg_adim_set_org2_from_model(AdgADim
*adim
, AdgModel
*model
, const gchar
*org2
)
576 AdgPoint
*point
= adg_point_new();
578 adg_point_set_pair_from_model(point
, model
, org2
);
579 adg_adim_set_org2(adim
, point
);
581 adg_point_destroy(point
);
588 * Gets the #AdgADim:org2 point. The returned point is internally owned
589 * and must not be freed or modified. Anyway, it is not const because
590 * adg_point_get_pair() must be able to modify the internal cache of
591 * the returned point.
593 * Returns: the first reference point
596 adg_adim_get_org2(AdgADim
*adim
)
598 AdgADimPrivate
*data
;
600 g_return_val_if_fail(ADG_IS_ADIM(adim
), NULL
);
608 * adg_adim_switch_extension1:
609 * @adim: an #AdgADim entity
610 * @new_state: the new state
612 * Shows (if @new_state is %TRUE) or hides (if @new_state is %FALSE)
613 * the first extension line of @adim.
616 adg_adim_switch_extension1(AdgADim
*adim
, gboolean new_state
)
618 g_return_if_fail(ADG_IS_ADIM(adim
));
619 g_return_if_fail(adg_is_boolean_value(new_state
));
620 g_object_set(adim
, "has-extension1", new_state
, NULL
);
624 * adg_adim_has_extension1:
625 * @adim: an #AdgADim entity
627 * Checks if @adim should render the first extension line.
629 * Returns: %TRUE on first extension line presents, %FALSE otherwise
632 adg_adim_has_extension1(AdgADim
*adim
)
634 AdgADimPrivate
*data
;
636 g_return_val_if_fail(ADG_IS_ADIM(adim
), FALSE
);
640 return data
->has_extension1
;
644 * adg_adim_switch_extension2:
645 * @adim: an #AdgADim entity
646 * @new_state: the new new_state
648 * Shows (if @new_state is %TRUE) or hides (if @new_state is %FALSE)
649 * the second extension line of @adim.
652 adg_adim_switch_extension2(AdgADim
*adim
, gboolean new_state
)
654 g_return_if_fail(ADG_IS_ADIM(adim
));
655 g_return_if_fail(adg_is_boolean_value(new_state
));
656 g_object_set(adim
, "has-extension2", new_state
, NULL
);
660 * adg_adim_has_extension2:
661 * @adim: an #AdgADim entity
663 * Checks if @adim should render the second extension line.
665 * Returns: %TRUE on first extension line presents, %FALSE otherwise
668 adg_adim_has_extension2(AdgADim
*adim
)
670 AdgADimPrivate
*data
;
672 g_return_val_if_fail(ADG_IS_ADIM(adim
), FALSE
);
676 return data
->has_extension2
;
681 _adg_global_changed(AdgEntity
*entity
)
683 AdgADimPrivate
*data
= ((AdgADim
*) entity
)->data
;
685 _adg_unset_trail((AdgADim
*) entity
);
687 if (_ADG_OLD_ENTITY_CLASS
->global_changed
)
688 _ADG_OLD_ENTITY_CLASS
->global_changed(entity
);
690 if (data
->marker1
!= NULL
)
691 adg_entity_global_changed((AdgEntity
*) data
->marker1
);
693 if (data
->marker2
!= NULL
)
694 adg_entity_global_changed((AdgEntity
*) data
->marker2
);
698 _adg_local_changed(AdgEntity
*entity
)
700 _adg_unset_trail((AdgADim
*) entity
);
702 if (_ADG_OLD_ENTITY_CLASS
->local_changed
)
703 _ADG_OLD_ENTITY_CLASS
->local_changed(entity
);
707 _adg_invalidate(AdgEntity
*entity
)
710 AdgADimPrivate
*data
;
712 adim
= (AdgADim
*) entity
;
715 _adg_dispose_markers(adim
);
716 data
->geometry_arranged
= FALSE
;
717 _adg_unset_trail(adim
);
720 adg_point_invalidate(data
->org1
);
722 adg_point_invalidate(data
->org2
);
724 if (_ADG_OLD_ENTITY_CLASS
->invalidate
)
725 _ADG_OLD_ENTITY_CLASS
->invalidate(entity
);
729 _adg_arrange(AdgEntity
*entity
)
733 AdgADimPrivate
*data
;
735 const AdgMatrix
*local
;
736 AdgPair ref1
, ref2
, base1
, base12
, base2
;
739 if (_ADG_OLD_ENTITY_CLASS
->arrange
)
740 _ADG_OLD_ENTITY_CLASS
->arrange(entity
);
742 adim
= (AdgADim
*) entity
;
744 if (!_adg_update_geometry(adim
))
747 dim
= (AdgDim
*) adim
;
749 quote
= adg_dim_get_quote(dim
);
751 _adg_update_entities(adim
);
753 if (data
->cpml
.path
.status
== CAIRO_STATUS_SUCCESS
) {
754 AdgEntity
*quote_entity
= (AdgEntity
*) quote
;
755 adg_entity_set_global_map(quote_entity
, &data
->quote
.global_map
);
759 local
= adg_entity_get_local_matrix(entity
);
760 cpml_pair_copy(&ref1
, adg_point_get_pair(adg_dim_get_ref1(dim
)));
761 cpml_pair_copy(&ref2
, adg_point_get_pair(adg_dim_get_ref2(dim
)));
762 cpml_pair_copy(&base1
, &data
->point
.base1
);
763 cpml_pair_copy(&base12
, &data
->point
.base12
);
764 cpml_pair_copy(&base2
, &data
->point
.base2
);
766 /* Apply the local matrix to the relevant points */
767 cpml_pair_transform(&ref1
, local
);
768 cpml_pair_transform(&ref2
, local
);
769 cpml_pair_transform(&base1
, local
);
770 cpml_pair_transform(&base12
, local
);
771 cpml_pair_transform(&base2
, local
);
773 /* Combine points and global shifts to build the path */
774 pair
.x
= ref1
.x
+ data
->shift
.from1
.x
;
775 pair
.y
= ref1
.y
+ data
->shift
.from1
.y
;
776 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[6]);
778 pair
.x
= base1
.x
+ data
->shift
.base1
.x
;
779 pair
.y
= base1
.y
+ data
->shift
.base1
.y
;
780 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[1]);
782 pair
.x
+= data
->shift
.to1
.x
;
783 pair
.y
+= data
->shift
.to1
.y
;
784 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[8]);
786 pair
.x
= base12
.x
+ data
->shift
.base12
.x
;
787 pair
.y
= base12
.y
+ data
->shift
.base12
.y
;
788 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[3]);
790 pair
.x
= ref2
.x
+ data
->shift
.from2
.x
;
791 pair
.y
= ref2
.y
+ data
->shift
.from2
.y
;
792 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[10]);
794 pair
.x
= base2
.x
+ data
->shift
.base2
.x
;
795 pair
.y
= base2
.y
+ data
->shift
.base2
.y
;
796 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[4]);
798 pair
.x
+= data
->shift
.to2
.x
;
799 pair
.y
+= data
->shift
.to2
.y
;
800 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[12]);
802 /* Play with header lengths to show or hide the extension lines */
803 if (data
->has_extension1
) {
804 data
->cpml
.data
[7].header
.length
= data
->has_extension2
? 2 : 6;
806 data
->cpml
.data
[2].header
.length
= data
->has_extension2
? 7 : 11;
809 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
812 /* Update global and local map of the quote */
813 AdgEntity
*quote_entity
;
817 quote_entity
= (AdgEntity
*) quote
;
818 angle
= adg_dim_quote_angle(dim
, (data
->angle1
+ data
->angle2
) / 2 + G_PI_2
);
819 cpml_pair_from_cairo(&pair
, &data
->cpml
.data
[3]);
821 adg_alignment_set_factor_explicit(quote
, 0.5, 0);
823 cairo_matrix_init_translate(&map
, pair
.x
, pair
.y
);
824 cairo_matrix_rotate(&map
, angle
);
825 adg_entity_set_global_map(quote_entity
, &map
);
827 adg_matrix_copy(&data
->quote
.global_map
,
828 adg_entity_get_global_map(quote_entity
));
831 /* Signal to the markers (if any) that the path has changed */
832 if (data
->marker1
!= NULL
) {
833 adg_marker_set_segment(data
->marker1
, data
->trail
, 1);
834 adg_entity_local_changed((AdgEntity
*) data
->marker1
);
837 if (data
->marker2
!= NULL
) {
838 adg_marker_set_segment(data
->marker2
, data
->trail
, 1);
839 adg_entity_local_changed((AdgEntity
*) data
->marker2
);
842 /* TODO: compute the extents */
846 _adg_render(AdgEntity
*entity
, cairo_t
*cr
)
850 AdgADimPrivate
*data
;
851 AdgDimStyle
*dim_style
;
853 const cairo_path_t
*cairo_path
;
855 adim
= (AdgADim
*) entity
;
858 if (!data
->geometry_arranged
) {
859 /* Entity not arranged, probably due to undefined pair found */
863 dim
= (AdgDim
*) entity
;
864 dim_style
= _ADG_GET_DIM_STYLE(dim
);
866 adg_style_apply((AdgStyle
*) dim_style
, entity
, cr
);
867 adg_entity_render((AdgEntity
*) adg_dim_get_quote(dim
), cr
);
869 if (data
->marker1
!= NULL
)
870 adg_entity_render((AdgEntity
*) data
->marker1
, cr
);
871 if (data
->marker2
!= NULL
)
872 adg_entity_render((AdgEntity
*) data
->marker2
, cr
);
874 cairo_transform(cr
, adg_entity_get_global_matrix(entity
));
875 dress
= adg_dim_style_get_line_dress(dim_style
);
876 adg_entity_apply_dress(entity
, dress
, cr
);
878 cairo_path
= adg_trail_get_cairo_path(data
->trail
);
879 cairo_append_path(cr
, cairo_path
);
884 _adg_default_value(AdgDim
*dim
)
887 AdgADimPrivate
*data
;
888 AdgDimStyle
*dim_style
;
892 adim
= (AdgADim
*) dim
;
894 dim_style
= _ADG_GET_DIM_STYLE(dim
);
895 format
= adg_dim_style_get_number_format(dim_style
);
897 if (!_adg_update_geometry(adim
))
898 return g_strdup("undef");
900 angle
= (data
->angle2
- data
->angle1
) * 180 / M_PI
;
901 return g_strdup_printf(format
, angle
);
904 /* With "geometry" is considered any data (point, vector or angle)
905 * that can be cached: this is strictly related on how the arrange()
908 _adg_update_geometry(AdgADim
*adim
)
910 AdgADimPrivate
*data
;
911 AdgDimStyle
*dim_style
;
912 gdouble from_offset
, to_offset
;
913 gdouble spacing
, level
;
914 CpmlVector vector
[3];
920 if (data
->geometry_arranged
)
922 else if (!_adg_get_info(adim
, vector
, ¢er
, &distance
))
925 dim_style
= _ADG_GET_DIM_STYLE(adim
);
926 from_offset
= adg_dim_style_get_from_offset(dim_style
);
927 to_offset
= adg_dim_style_get_to_offset(dim_style
);
928 spacing
= adg_dim_style_get_baseline_spacing(dim_style
);
929 level
= adg_dim_get_level((AdgDim
*) adim
);
932 cpml_vector_set_length(&vector
[0], from_offset
);
933 cpml_pair_copy(&data
->shift
.from1
, &vector
[0]);
936 cpml_vector_set_length(&vector
[0], level
* spacing
);
937 cpml_pair_copy(&data
->shift
.base1
, &vector
[0]);
940 cpml_vector_set_length(&vector
[0], to_offset
);
941 cpml_pair_copy(&data
->shift
.to1
, &vector
[0]);
944 cpml_vector_set_length(&vector
[2], from_offset
);
945 cpml_pair_copy(&data
->shift
.from2
, &vector
[2]);
948 cpml_vector_set_length(&vector
[2], level
* spacing
);
949 cpml_pair_copy(&data
->shift
.base2
, &vector
[2]);
952 cpml_vector_set_length(&vector
[2], to_offset
);
953 cpml_pair_copy(&data
->shift
.to2
, &vector
[2]);
956 cpml_vector_set_length(&vector
[1], level
* spacing
);
957 cpml_pair_copy(&data
->shift
.base12
, &vector
[1]);
959 /* Distance can be 0, so the following will leave the
960 * vector array in undefined state */
963 cpml_vector_set_length(&vector
[0], distance
);
964 data
->point
.base1
.x
= vector
[0].x
+ center
.x
;
965 data
->point
.base1
.y
= vector
[0].y
+ center
.y
;
968 cpml_vector_set_length(&vector
[2], distance
);
969 data
->point
.base2
.x
= vector
[2].x
+ center
.x
;
970 data
->point
.base2
.y
= vector
[2].y
+ center
.y
;
973 cpml_vector_set_length(&vector
[1], distance
);
974 data
->point
.base12
.x
= vector
[1].x
+ center
.x
;
975 data
->point
.base12
.y
= vector
[1].y
+ center
.y
;
977 data
->geometry_arranged
= TRUE
;
983 _adg_update_entities(AdgADim
*adim
)
986 AdgADimPrivate
*data
;
987 AdgDimStyle
*dim_style
;
989 entity
= (AdgEntity
*) adim
;
991 dim_style
= _ADG_GET_DIM_STYLE(adim
);
993 if (data
->trail
== NULL
)
994 data
->trail
= adg_trail_new(_adg_trail_callback
, adim
);
996 if (data
->marker1
== NULL
) {
997 data
->marker1
= adg_dim_style_marker1_new(dim_style
);
998 adg_entity_set_parent((AdgEntity
*) data
->marker1
, entity
);
1001 if (data
->marker2
== NULL
) {
1002 data
->marker2
= adg_dim_style_marker2_new(dim_style
);
1003 adg_entity_set_parent((AdgEntity
*) data
->marker2
, entity
);
1008 _adg_unset_trail(AdgADim
*adim
)
1010 AdgADimPrivate
*data
= adim
->data
;
1012 if (data
->trail
!= NULL
)
1013 adg_model_clear((AdgModel
*) data
->trail
);
1015 data
->cpml
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
1019 _adg_dispose_markers(AdgADim
*adim
)
1021 AdgADimPrivate
*data
= adim
->data
;
1023 if (data
->trail
!= NULL
) {
1024 g_object_unref(data
->trail
);
1028 if (data
->marker1
!= NULL
) {
1029 g_object_unref(data
->marker1
);
1030 data
->marker1
= NULL
;
1033 if (data
->marker2
!= NULL
) {
1034 g_object_unref(data
->marker2
);
1035 data
->marker2
= NULL
;
1040 _adg_get_info(AdgADim
*adim
, CpmlVector vector
[],
1041 AdgPair
*center
, gdouble
*distance
)
1044 AdgADimPrivate
*data
;
1045 const AdgPair
*ref1
, *ref2
, *pos
;
1046 const AdgPair
*org1
, *org2
;
1049 dim
= (AdgDim
*) adim
;
1051 ref1
= adg_point_get_pair(adg_dim_get_ref1(dim
));
1052 ref2
= adg_point_get_pair(adg_dim_get_ref2(dim
));
1053 pos
= adg_point_get_pair(adg_dim_get_pos(dim
));
1054 org1
= adg_point_get_pair(data
->org1
);
1055 org2
= adg_point_get_pair(data
->org2
);
1057 if (ref1
== NULL
|| ref2
== NULL
|| pos
== NULL
||
1058 org1
== NULL
|| org2
== NULL
)
1061 vector
[0].x
= ref1
->x
- org1
->x
;
1062 vector
[0].y
= ref1
->y
- org1
->y
;
1063 vector
[2].x
= ref2
->x
- org2
->x
;
1064 vector
[2].y
= ref2
->y
- org2
->y
;
1066 factor
= vector
[0].x
* vector
[2].y
- vector
[0].y
* vector
[2].x
;
1068 /* Parallel lines: hang with an error message */
1069 g_warning("%s: trying to set an angular dimension on parallel lines",
1074 factor
= ((ref1
->y
- ref2
->y
) * vector
[2].x
-
1075 (ref1
->x
- ref2
->x
) * vector
[2].y
) / factor
;
1077 center
->x
= ref1
->x
+ vector
[0].x
* factor
;
1078 center
->y
= ref1
->y
+ vector
[0].y
* factor
;
1079 *distance
= cpml_pair_distance(center
, pos
);
1080 data
->angle1
= cpml_vector_angle(&vector
[0]);
1081 data
->angle2
= cpml_vector_angle(&vector
[2]);
1082 while (data
->angle2
< data
->angle1
)
1083 data
->angle2
+= M_PI
* 2;
1085 cpml_vector_from_angle(&vector
[1], (data
->angle1
+ data
->angle2
) / 2);
1091 _adg_trail_callback(AdgTrail
*trail
, gpointer user_data
)
1094 AdgADimPrivate
*data
;
1096 adim
= (AdgADim
*) user_data
;
1099 return &data
->cpml
.path
;