1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 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.
37 #include "adg-adim-private.h"
40 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_adim_parent_class)
41 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_adim_parent_class)
53 static void dispose (GObject
*object
);
54 static void get_property (GObject
*object
,
58 static void set_property (GObject
*object
,
62 static void local_changed (AdgEntity
*entity
);
63 static void invalidate (AdgEntity
*entity
);
64 static void arrange (AdgEntity
*entity
);
65 static void render (AdgEntity
*entity
,
67 static gchar
* default_value (AdgDim
*dim
);
68 static void update_geometry (AdgADim
*adim
);
69 static void update_entities (AdgADim
*adim
);
70 static gboolean
set_org1 (AdgADim
*adim
,
72 static gboolean
set_org2 (AdgADim
*adim
,
74 static void unset_trail (AdgADim
*adim
);
75 static void dispose_markers (AdgADim
*adim
);
76 static gboolean
get_info (AdgADim
*adim
,
80 static CpmlPath
* trail_callback (AdgTrail
*trail
,
84 G_DEFINE_TYPE(AdgADim
, adg_adim
, ADG_TYPE_DIM
);
88 adg_adim_class_init(AdgADimClass
*klass
)
90 GObjectClass
*gobject_class
;
91 AdgEntityClass
*entity_class
;
92 AdgDimClass
*dim_class
;
95 gobject_class
= (GObjectClass
*) klass
;
96 entity_class
= (AdgEntityClass
*) klass
;
97 dim_class
= (AdgDimClass
*) klass
;
99 g_type_class_add_private(klass
, sizeof(AdgADimPrivate
));
101 gobject_class
->dispose
= dispose
;
102 gobject_class
->get_property
= get_property
;
103 gobject_class
->set_property
= set_property
;
105 entity_class
->local_changed
= local_changed
;
106 entity_class
->invalidate
= invalidate
;
107 entity_class
->arrange
= arrange
;
108 entity_class
->render
= render
;
110 dim_class
->default_value
= default_value
;
112 param
= g_param_spec_boxed("org1",
114 P_("Where the first line comes from: this point is used toghether with \"ref1\" to align the first extension line"),
117 g_object_class_install_property(gobject_class
, PROP_ORG1
, param
);
119 param
= g_param_spec_boxed("org2",
121 P_("Where the second line comes from: this point is used toghether with \"ref2\" to align the second extension line"),
124 g_object_class_install_property(gobject_class
, PROP_ORG2
, param
);
128 adg_adim_init(AdgADim
*adim
)
130 AdgADimPrivate
*data
;
131 cairo_path_data_t move_to
, line_to
, arc_to
;
133 data
= G_TYPE_INSTANCE_GET_PRIVATE(adim
, ADG_TYPE_ADIM
, AdgADimPrivate
);
134 move_to
.header
.type
= CAIRO_PATH_MOVE_TO
;
135 move_to
.header
.length
= 2;
136 line_to
.header
.type
= CAIRO_PATH_LINE_TO
;
137 line_to
.header
.length
= 2;
138 arc_to
.header
.type
= CAIRO_PATH_ARC_TO
;
139 arc_to
.header
.length
= 3;
141 data
->org1
.x
= data
->org1
.y
= 0;
142 data
->org2
.x
= data
->org2
.y
= 0;
143 data
->has_extension1
= TRUE
;
144 data
->has_extension2
= TRUE
;
146 data
->cpml
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
147 data
->cpml
.path
.data
= data
->cpml
.data
;
148 data
->cpml
.path
.num_data
= G_N_ELEMENTS(data
->cpml
.data
);
149 data
->cpml
.path
.data
[0] = move_to
;
150 data
->cpml
.path
.data
[2] = arc_to
;
151 data
->cpml
.path
.data
[5] = move_to
;
152 data
->cpml
.path
.data
[7] = line_to
;
153 data
->cpml
.path
.data
[9] = move_to
;
154 data
->cpml
.path
.data
[11] = line_to
;
157 data
->marker1
= NULL
;
158 data
->marker2
= NULL
;
160 data
->geometry_arranged
= FALSE
;
166 dispose(GObject
*object
)
168 dispose_markers((AdgADim
*) object
);
170 if (PARENT_OBJECT_CLASS
->dispose
!= NULL
)
171 PARENT_OBJECT_CLASS
->dispose(object
);
175 get_property(GObject
*object
, guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
177 AdgADimPrivate
*data
= ((AdgADim
*) object
)->data
;
181 g_value_set_boxed(value
, &data
->org1
);
184 g_value_set_boxed(value
, &data
->org2
);
186 case PROP_HAS_EXTENSION1
:
187 g_value_set_boolean(value
, data
->has_extension1
);
189 case PROP_HAS_EXTENSION2
:
190 g_value_set_boolean(value
, data
->has_extension2
);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
199 set_property(GObject
*object
, guint prop_id
,
200 const GValue
*value
, GParamSpec
*pspec
)
203 AdgADimPrivate
*data
;
205 adim
= (AdgADim
*) object
;
210 set_org1(adim
, g_value_get_boxed(value
));
213 set_org2(adim
, g_value_get_boxed(value
));
215 case PROP_HAS_EXTENSION1
:
216 data
->has_extension1
= g_value_get_boolean(value
);
218 case PROP_HAS_EXTENSION2
:
219 data
->has_extension2
= g_value_get_boolean(value
);
222 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
231 * Creates a new - undefined - angular dimension. You must, at least,
232 * define the reference points with adg_dim_set_ref(), the origins of
233 * the lines ending with the reference points with adg_adim_set_org()
234 * and the reference for positioning the quote with adg_dim_set_pos().
236 * Returns: the newly created angular dimension entity
241 return g_object_new(ADG_TYPE_ADIM
, NULL
);
246 * @ref1: first reference point
247 * @ref2: second reference point
248 * @org1: first origin point
249 * @org2: second origin point
251 * Creates a new angular dimension, specifing all the needed
252 * properties in one shot.
254 * Returns: the newly created angular dimension entity
257 adg_adim_new_full(const AdgPair
*ref1
, const AdgPair
*ref2
,
258 const AdgPair
*org1
, const AdgPair
*org2
,
261 return g_object_new(ADG_TYPE_ADIM
, "ref1", ref1
, "ref2", ref2
,
262 "org1", org1
, "org2", org2
, "pos", pos
, NULL
);
266 * adg_adim_new_full_explicit:
267 * @ref1_x: the x coordinate of the first reference point
268 * @ref1_y: the y coordinate of the first reference point
269 * @ref2_x: the x coordinate of the second reference point
270 * @ref2_y: the y coordinate of the second reference point
271 * @direction: angle where to extend the dimension
272 * @pos_x: the x coordinate of the position reference
273 * @pos_y: the y coordinate of the position reference
275 * Wrappes adg_adim_new_full() with explicit values.
277 * Returns: the newly created linear dimension entity
280 adg_adim_new_full_explicit(gdouble ref1_x
, gdouble ref1_y
,
281 gdouble ref2_x
, gdouble ref2_y
,
282 gdouble org1_x
, gdouble org1_y
,
283 gdouble org2_x
, gdouble org2_y
,
284 gdouble pos_x
, gdouble pos_y
)
286 AdgPair ref1
, ref2
, org1
, org2
, pos
;
299 return adg_adim_new_full(&ref1
, &ref2
, &org1
, &org2
, &pos
);
306 * Gets the first origin of @adim. The returned pair is owned by
307 * @adim and should not be modified or freed.
309 * Returns: a pointer to the internal #AdgPair or %NULL on errors
312 adg_adim_get_org1(AdgADim
*adim
)
314 AdgADimPrivate
*data
;
316 g_return_val_if_fail(ADG_IS_ADIM(adim
), NULL
);
327 * Gets the second origin of @adim. The returned pair is owned by
328 * @adim and should not be modified or freed.
330 * Returns: a pointer to the internal #AdgPair or %NULL on errors
333 adg_adim_get_org2(AdgADim
*adim
)
335 AdgADimPrivate
*data
;
337 g_return_val_if_fail(ADG_IS_ADIM(adim
), NULL
);
347 * @org1: the first origin
348 * @org2: the second origin
350 * Sets at once the two origins on @adim.
353 adg_adim_set_org(AdgADim
*adim
, const AdgPair
*org1
, const AdgPair
*org2
)
357 g_return_if_fail(ADG_IS_ADIM(adim
));
359 object
= (GObject
*) adim
;
361 g_object_freeze_notify(object
);
363 if (set_org1(adim
, org1
))
364 g_object_notify(object
, "org1");
366 if (set_org2(adim
, org1
))
367 g_object_notify(object
, "org2");
369 g_object_thaw_notify(object
);
374 local_changed(AdgEntity
*entity
)
376 unset_trail((AdgADim
*) entity
);
378 PARENT_ENTITY_CLASS
->local_changed(entity
);
382 invalidate(AdgEntity
*entity
)
385 AdgADimPrivate
*data
;
387 adim
= (AdgADim
*) entity
;
390 dispose_markers(adim
);
391 data
->geometry_arranged
= FALSE
;
394 if (PARENT_ENTITY_CLASS
->invalidate
!= NULL
)
395 PARENT_ENTITY_CLASS
->invalidate(entity
);
399 arrange(AdgEntity
*entity
)
403 AdgADimPrivate
*data
;
405 const AdgMatrix
*local
;
406 AdgPair ref1
, ref2
, base1
, base12
, base2
;
409 PARENT_ENTITY_CLASS
->arrange(entity
);
411 adim
= (AdgADim
*) entity
;
412 dim
= (AdgDim
*) adim
;
414 quote
= adg_dim_get_quote(dim
);
416 update_geometry(adim
);
417 update_entities(adim
);
419 if (data
->cpml
.path
.status
== CAIRO_STATUS_SUCCESS
) {
420 AdgEntity
*quote_entity
= (AdgEntity
*) quote
;
421 adg_entity_set_global_map(quote_entity
, &data
->quote
.global_map
);
422 adg_entity_set_local_map(quote_entity
, &data
->quote
.local_map
);
426 local
= adg_entity_local_matrix(entity
);
428 /* Apply the local matrix to the relevant points */
429 cpml_pair_transform(cpml_pair_copy(&ref1
, adg_dim_get_ref1(dim
)), local
);
430 cpml_pair_transform(cpml_pair_copy(&ref2
, adg_dim_get_ref2(dim
)), local
);
431 cpml_pair_transform(cpml_pair_copy(&base1
, &data
->point
.base1
), local
);
432 cpml_pair_transform(cpml_pair_copy(&base12
, &data
->point
.base12
), local
);
433 cpml_pair_transform(cpml_pair_copy(&base2
, &data
->point
.base2
), local
);
435 /* Combine points and global shifts to build the path */
436 cpml_pair_add(cpml_pair_copy(&pair
, &ref1
), &data
->shift
.from1
);
437 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[6]);
439 cpml_pair_add(cpml_pair_copy(&pair
, &base1
), &data
->shift
.base1
);
440 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[1]);
442 cpml_pair_add(&pair
, &data
->shift
.to1
);
443 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[8]);
445 cpml_pair_add(cpml_pair_copy(&pair
, &base12
), &data
->shift
.base12
);
446 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[3]);
448 cpml_pair_add(cpml_pair_copy(&pair
, &ref2
), &data
->shift
.from2
);
449 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[10]);
451 cpml_pair_add(cpml_pair_copy(&pair
, &base2
), &data
->shift
.base2
);
452 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[4]);
454 cpml_pair_add(&pair
, &data
->shift
.to2
);
455 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[12]);
457 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
460 /* Update global and local map of the quote container */
461 AdgEntity
*quote_entity
;
465 quote_entity
= (AdgEntity
*) quote
;
466 angle
= adg_dim_quote_angle(dim
, (data
->angle1
+ data
->angle2
) / 2 + G_PI_2
);
467 adg_matrix_copy(&matrix
, local
);
468 cairo_matrix_invert(&matrix
);
470 cpml_pair_from_cairo(&pair
, &data
->cpml
.data
[3]);
471 cairo_matrix_transform_point(&matrix
, &pair
.x
, &pair
.y
);
472 cairo_matrix_init_translate(&matrix
, pair
.x
, pair
.y
);
473 adg_entity_set_local_map(quote_entity
, &matrix
);
475 cairo_matrix_init_rotate(&matrix
, angle
);
476 adg_entity_transform_global_map(quote_entity
, &matrix
, ADG_TRANSFORM_BEFORE
);
478 adg_entity_get_global_map(quote_entity
, &data
->quote
.global_map
);
479 adg_entity_get_local_map(quote_entity
, &data
->quote
.local_map
);
482 /* Signal to the markers (if any) that the path has changed */
483 if (data
->marker1
!= NULL
) {
484 adg_marker_set_segment(data
->marker1
, data
->trail
, 1);
485 adg_entity_local_changed((AdgEntity
*) data
->marker1
);
488 if (data
->marker2
!= NULL
) {
489 adg_marker_set_segment(data
->marker2
, data
->trail
, 1);
490 adg_entity_local_changed((AdgEntity
*) data
->marker2
);
493 /* TODO: compute the extents */
497 render(AdgEntity
*entity
, cairo_t
*cr
)
501 AdgADimPrivate
*data
;
502 AdgDimStyle
*dim_style
;
504 const cairo_path_t
*cairo_path
;
506 adim
= (AdgADim
*) entity
;
507 dim
= (AdgDim
*) entity
;
509 dim_style
= adg_dim_get_dim_style(dim
);
511 dress
= adg_dim_style_get_color_dress(dim_style
);
512 adg_entity_apply_dress(entity
, dress
, cr
);
514 if (data
->marker1
!= NULL
)
515 adg_entity_render((AdgEntity
*) data
->marker1
, cr
);
517 if (data
->marker2
!= NULL
)
518 adg_entity_render((AdgEntity
*) data
->marker2
, cr
);
520 adg_entity_render((AdgEntity
*) adg_dim_get_quote(dim
), cr
);
522 dress
= adg_dim_style_get_line_dress(dim_style
);
523 adg_entity_apply_dress(entity
, dress
, cr
);
525 cairo_path
= adg_trail_get_cairo_path(data
->trail
);
526 cairo_append_path(cr
, cairo_path
);
531 default_value(AdgDim
*dim
)
534 AdgADimPrivate
*data
;
535 AdgDimStyle
*dim_style
;
539 adim
= (AdgADim
*) dim
;
541 dim_style
= adg_dim_get_dim_style(dim
);
542 format
= adg_dim_style_get_number_format(dim_style
);
544 update_geometry(adim
);
545 angle
= (data
->angle2
- data
->angle1
) * 180 / M_PI
;
547 return g_strdup_printf(format
, angle
);
550 /* With "geometry" is considered any data (point, vector or angle)
551 * that can be cached: this is strictly related on how the arrange()
554 update_geometry(AdgADim
*adim
)
556 AdgADimPrivate
*data
;
557 AdgDimStyle
*dim_style
;
558 gdouble from_offset
, to_offset
;
559 gdouble spacing
, level
;
560 CpmlVector vector
[3];
566 if (data
->geometry_arranged
)
569 if (!get_info(adim
, vector
, ¢er
, &distance
)) {
570 /* Parallel lines: hang with an error message */
571 g_warning("%s: trying to set an angular dimension on parallel lines",
576 dim_style
= adg_dim_get_dim_style((AdgDim
*) adim
);
577 from_offset
= adg_dim_style_get_from_offset(dim_style
);
578 to_offset
= adg_dim_style_get_to_offset(dim_style
);
579 spacing
= adg_dim_style_get_baseline_spacing(dim_style
);
580 level
= adg_dim_get_level((AdgDim
*) adim
);
583 cpml_vector_set_length(&vector
[0], from_offset
);
584 cpml_pair_copy(&data
->shift
.from1
, &vector
[0]);
587 cpml_vector_set_length(&vector
[0], level
* spacing
);
588 cpml_pair_copy(&data
->shift
.base1
, &vector
[0]);
591 cpml_vector_set_length(&vector
[0], to_offset
);
592 cpml_pair_copy(&data
->shift
.to1
, &vector
[0]);
595 cpml_vector_set_length(&vector
[2], from_offset
);
596 cpml_pair_copy(&data
->shift
.from2
, &vector
[2]);
599 cpml_vector_set_length(&vector
[2], level
* spacing
);
600 cpml_pair_copy(&data
->shift
.base2
, &vector
[2]);
603 cpml_vector_set_length(&vector
[2], to_offset
);
604 cpml_pair_copy(&data
->shift
.to2
, &vector
[2]);
607 cpml_vector_set_length(&vector
[1], level
* spacing
);
608 cpml_pair_copy(&data
->shift
.base12
, &vector
[1]);
610 /* Distance can be 0, so the following will leave the
611 * vector array in undefined state */
614 cpml_vector_set_length(&vector
[0], distance
);
615 cpml_pair_add(cpml_pair_copy(&data
->point
.base1
, &vector
[0]), ¢er
);
618 cpml_vector_set_length(&vector
[2], distance
);
619 cpml_pair_add(cpml_pair_copy(&data
->point
.base2
, &vector
[2]), ¢er
);
622 cpml_vector_set_length(&vector
[1], distance
);
623 cpml_pair_add(cpml_pair_copy(&data
->point
.base12
, &vector
[1]), ¢er
);
625 data
->geometry_arranged
= TRUE
;
629 update_entities(AdgADim
*adim
)
631 AdgADimPrivate
*data
;
632 AdgDimStyle
*dim_style
;
635 dim_style
= adg_dim_get_dim_style((AdgDim
*) adim
);
637 if (data
->trail
== NULL
)
638 data
->trail
= adg_trail_new(trail_callback
, adim
);
640 if (data
->marker1
== NULL
)
641 data
->marker1
= adg_dim_style_marker1_new(dim_style
);
643 if (data
->marker2
== NULL
)
644 data
->marker2
= adg_dim_style_marker2_new(dim_style
);
648 set_org1(AdgADim
*adim
, const AdgPair
*org1
)
650 AdgADimPrivate
*data
= adim
->data
;
652 if (adg_pair_equal(&data
->org1
, org1
))
661 set_org2(AdgADim
*adim
, const AdgPair
*org2
)
663 AdgADimPrivate
*data
= adim
->data
;
665 if (adg_pair_equal(&data
->org2
, org2
))
674 unset_trail(AdgADim
*adim
)
676 AdgADimPrivate
*data
= adim
->data
;
678 if (data
->trail
!= NULL
)
679 adg_model_clear((AdgModel
*) data
->trail
);
681 data
->cpml
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
685 dispose_markers(AdgADim
*adim
)
687 AdgADimPrivate
*data
= adim
->data
;
689 if (data
->trail
!= NULL
) {
690 g_object_unref(data
->trail
);
694 if (data
->marker1
!= NULL
) {
695 g_object_unref(data
->marker1
);
696 data
->marker1
= NULL
;
699 if (data
->marker2
!= NULL
) {
700 g_object_unref(data
->marker2
);
701 data
->marker2
= NULL
;
706 get_info(AdgADim
*adim
, CpmlVector vector
[],
707 AdgPair
*center
, gdouble
*distance
)
710 AdgADimPrivate
*data
;
711 const AdgPair
*ref1
, *ref2
;
714 dim
= (AdgDim
*) adim
;
716 ref1
= adg_dim_get_ref1(dim
);
717 ref2
= adg_dim_get_ref2(dim
);
719 cpml_pair_sub(cpml_pair_copy(&vector
[0], ref1
), &data
->org1
);
720 cpml_pair_sub(cpml_pair_copy(&vector
[2], ref2
), &data
->org2
);
722 factor
= vector
[0].x
* vector
[2].y
- vector
[0].y
* vector
[2].x
;
726 factor
= ((ref1
->y
- ref2
->y
) * vector
[2].x
-
727 (ref1
->x
- ref2
->x
) * vector
[2].y
) / factor
;
729 center
->x
= ref1
->x
+ vector
[0].x
* factor
;
730 center
->y
= ref1
->y
+ vector
[0].y
* factor
;
731 *distance
= cpml_pair_distance(center
, adg_dim_get_pos(dim
));
732 data
->angle1
= cpml_vector_angle(&vector
[0]);
733 data
->angle2
= cpml_vector_angle(&vector
[2]);
734 while (data
->angle2
< data
->angle1
)
735 data
->angle2
+= M_PI
* 2;
737 cpml_vector_from_angle(&vector
[1], (data
->angle1
+ data
->angle2
) / 2);
743 trail_callback(AdgTrail
*trail
, gpointer user_data
)
746 AdgADimPrivate
*data
;
748 adim
= (AdgADim
*) user_data
;
751 return &data
->cpml
.path
;