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 dispose_markers (AdgADim
*adim
);
75 static gboolean
get_info (AdgADim
*adim
,
79 static CpmlPath
* trail_callback (AdgTrail
*trail
,
83 G_DEFINE_TYPE(AdgADim
, adg_adim
, ADG_TYPE_DIM
);
87 adg_adim_class_init(AdgADimClass
*klass
)
89 GObjectClass
*gobject_class
;
90 AdgEntityClass
*entity_class
;
91 AdgDimClass
*dim_class
;
94 gobject_class
= (GObjectClass
*) klass
;
95 entity_class
= (AdgEntityClass
*) klass
;
96 dim_class
= (AdgDimClass
*) klass
;
98 g_type_class_add_private(klass
, sizeof(AdgADimPrivate
));
100 gobject_class
->dispose
= dispose
;
101 gobject_class
->get_property
= get_property
;
102 gobject_class
->set_property
= set_property
;
104 entity_class
->local_changed
= local_changed
;
105 entity_class
->invalidate
= invalidate
;
106 entity_class
->arrange
= arrange
;
107 entity_class
->render
= render
;
109 dim_class
->default_value
= default_value
;
111 param
= g_param_spec_boxed("org1",
113 P_("Where the first line comes from: this point is used toghether with \"ref1\" to align the first extension line"),
116 g_object_class_install_property(gobject_class
, PROP_ORG1
, param
);
118 param
= g_param_spec_boxed("org2",
120 P_("Where the second line comes from: this point is used toghether with \"ref2\" to align the second extension line"),
123 g_object_class_install_property(gobject_class
, PROP_ORG2
, param
);
127 adg_adim_init(AdgADim
*adim
)
129 AdgADimPrivate
*data
;
130 cairo_path_data_t move_to
, line_to
, arc_to
;
132 data
= G_TYPE_INSTANCE_GET_PRIVATE(adim
, ADG_TYPE_ADIM
, AdgADimPrivate
);
133 move_to
.header
.type
= CAIRO_PATH_MOVE_TO
;
134 move_to
.header
.length
= 2;
135 line_to
.header
.type
= CAIRO_PATH_LINE_TO
;
136 line_to
.header
.length
= 2;
137 arc_to
.header
.type
= CAIRO_PATH_ARC_TO
;
138 arc_to
.header
.length
= 3;
140 data
->org1
.x
= data
->org1
.y
= 0;
141 data
->org2
.x
= data
->org2
.y
= 0;
142 data
->has_extension1
= TRUE
;
143 data
->has_extension2
= TRUE
;
145 data
->cpml
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
146 data
->cpml
.path
.data
= data
->cpml
.data
;
147 data
->cpml
.path
.num_data
= G_N_ELEMENTS(data
->cpml
.data
);
148 data
->cpml
.path
.data
[0] = move_to
;
149 data
->cpml
.path
.data
[2] = arc_to
;
150 data
->cpml
.path
.data
[5] = move_to
;
151 data
->cpml
.path
.data
[7] = line_to
;
152 data
->cpml
.path
.data
[9] = move_to
;
153 data
->cpml
.path
.data
[11] = line_to
;
156 data
->marker1
= NULL
;
157 data
->marker2
= NULL
;
159 data
->geometry_arranged
= FALSE
;
165 dispose(GObject
*object
)
167 dispose_markers((AdgADim
*) object
);
169 if (PARENT_OBJECT_CLASS
->dispose
!= NULL
)
170 PARENT_OBJECT_CLASS
->dispose(object
);
174 get_property(GObject
*object
, guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
176 AdgADimPrivate
*data
= ((AdgADim
*) object
)->data
;
180 g_value_set_boxed(value
, &data
->org1
);
183 g_value_set_boxed(value
, &data
->org2
);
185 case PROP_HAS_EXTENSION1
:
186 g_value_set_boolean(value
, data
->has_extension1
);
188 case PROP_HAS_EXTENSION2
:
189 g_value_set_boolean(value
, data
->has_extension2
);
192 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
198 set_property(GObject
*object
, guint prop_id
,
199 const GValue
*value
, GParamSpec
*pspec
)
202 AdgADimPrivate
*data
;
204 adim
= (AdgADim
*) object
;
209 set_org1(adim
, g_value_get_boxed(value
));
212 set_org2(adim
, g_value_get_boxed(value
));
214 case PROP_HAS_EXTENSION1
:
215 data
->has_extension1
= g_value_get_boolean(value
);
217 case PROP_HAS_EXTENSION2
:
218 data
->has_extension2
= g_value_get_boolean(value
);
221 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
230 * Creates a new - undefined - angular dimension. You must, at least,
231 * define the reference points with adg_dim_set_ref(), the origins of
232 * the lines ending with the reference points with adg_adim_set_org()
233 * and the reference for positioning the quote with adg_dim_set_pos().
235 * Returns: the newly created angular dimension entity
240 return g_object_new(ADG_TYPE_ADIM
, NULL
);
245 * @ref1: first reference point
246 * @ref2: second reference point
247 * @org1: first origin point
248 * @org2: second origin point
250 * Creates a new angular dimension, specifing all the needed
251 * properties in one shot.
253 * Returns: the newly created angular dimension entity
256 adg_adim_new_full(const AdgPair
*ref1
, const AdgPair
*ref2
,
257 const AdgPair
*org1
, const AdgPair
*org2
,
260 return g_object_new(ADG_TYPE_ADIM
, "ref1", ref1
, "ref2", ref2
,
261 "org1", org1
, "org2", org2
, "pos", pos
, NULL
);
265 * adg_adim_new_full_explicit:
266 * @ref1_x: the x coordinate of the first reference point
267 * @ref1_y: the y coordinate of the first reference point
268 * @ref2_x: the x coordinate of the second reference point
269 * @ref2_y: the y coordinate of the second reference point
270 * @direction: angle where to extend the dimension
271 * @pos_x: the x coordinate of the position reference
272 * @pos_y: the y coordinate of the position reference
274 * Wrappes adg_adim_new_full() with explicit values.
276 * Returns: the newly created linear dimension entity
279 adg_adim_new_full_explicit(gdouble ref1_x
, gdouble ref1_y
,
280 gdouble ref2_x
, gdouble ref2_y
,
281 gdouble org1_x
, gdouble org1_y
,
282 gdouble org2_x
, gdouble org2_y
,
283 gdouble pos_x
, gdouble pos_y
)
285 AdgPair ref1
, ref2
, org1
, org2
, pos
;
298 return adg_adim_new_full(&ref1
, &ref2
, &org1
, &org2
, &pos
);
305 * Gets the first origin of @adim. The returned pair is owned by
306 * @adim and should not be modified or freed.
308 * Returns: a pointer to the internal #AdgPair or %NULL on errors
311 adg_adim_get_org1(AdgADim
*adim
)
313 AdgADimPrivate
*data
;
315 g_return_val_if_fail(ADG_IS_ADIM(adim
), NULL
);
326 * Gets the second origin of @adim. The returned pair is owned by
327 * @adim and should not be modified or freed.
329 * Returns: a pointer to the internal #AdgPair or %NULL on errors
332 adg_adim_get_org2(AdgADim
*adim
)
334 AdgADimPrivate
*data
;
336 g_return_val_if_fail(ADG_IS_ADIM(adim
), NULL
);
346 * @org1: the first origin
347 * @org2: the second origin
349 * Sets at once the two origins on @adim.
352 adg_adim_set_org(AdgADim
*adim
, const AdgPair
*org1
, const AdgPair
*org2
)
356 g_return_if_fail(ADG_IS_ADIM(adim
));
358 object
= (GObject
*) adim
;
360 g_object_freeze_notify(object
);
362 if (set_org1(adim
, org1
))
363 g_object_notify(object
, "org1");
365 if (set_org2(adim
, org1
))
366 g_object_notify(object
, "org2");
368 g_object_thaw_notify(object
);
373 local_changed(AdgEntity
*entity
)
375 AdgADimPrivate
*data
= ((AdgADim
*) entity
)->data
;
377 if (data
->trail
!= NULL
)
378 adg_trail_clear_cairo_path(data
->trail
);
380 data
->cpml
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
382 PARENT_ENTITY_CLASS
->local_changed(entity
);
386 invalidate(AdgEntity
*entity
)
389 AdgADimPrivate
*data
;
391 adim
= (AdgADim
*) entity
;
394 dispose_markers(adim
);
395 data
->geometry_arranged
= FALSE
;
396 adg_trail_clear_cairo_path(data
->trail
);
397 data
->cpml
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
399 if (PARENT_ENTITY_CLASS
->invalidate
!= NULL
)
400 PARENT_ENTITY_CLASS
->invalidate(entity
);
404 arrange(AdgEntity
*entity
)
407 AdgADimPrivate
*data
;
409 const AdgMatrix
*local
;
410 AdgPair ref1
, ref2
, base1
, base12
, base2
;
413 PARENT_ENTITY_CLASS
->arrange(entity
);
415 adim
= (AdgADim
*) entity
;
418 update_geometry(adim
);
419 update_entities(adim
);
421 if (data
->cpml
.path
.status
== CAIRO_STATUS_SUCCESS
)
424 dim
= (AdgDim
*) adim
;
425 local
= adg_entity_local_matrix(entity
);
427 /* Apply the local matrix to the relevant points */
428 cpml_pair_transform(cpml_pair_copy(&ref1
, adg_dim_get_ref1(dim
)), local
);
429 cpml_pair_transform(cpml_pair_copy(&ref2
, adg_dim_get_ref2(dim
)), local
);
430 cpml_pair_transform(cpml_pair_copy(&base1
, &data
->point
.base1
), local
);
431 cpml_pair_transform(cpml_pair_copy(&base12
, &data
->point
.base12
), local
);
432 cpml_pair_transform(cpml_pair_copy(&base2
, &data
->point
.base2
), local
);
434 /* Combine points and global shifts to build the path */
435 cpml_pair_add(cpml_pair_copy(&pair
, &ref1
), &data
->shift
.from1
);
436 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[6]);
438 cpml_pair_add(cpml_pair_copy(&pair
, &base1
), &data
->shift
.base1
);
439 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[1]);
441 cpml_pair_add(&pair
, &data
->shift
.to1
);
442 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[8]);
444 cpml_pair_add(cpml_pair_copy(&pair
, &base12
), &data
->shift
.base12
);
445 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[3]);
447 cpml_pair_add(cpml_pair_copy(&pair
, &ref2
), &data
->shift
.from2
);
448 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[10]);
450 cpml_pair_add(cpml_pair_copy(&pair
, &base2
), &data
->shift
.base2
);
451 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[4]);
453 cpml_pair_add(&pair
, &data
->shift
.to2
);
454 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[12]);
456 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
458 /* Signal to the markers (if any) that the path has changed */
459 if (data
->marker1
!= NULL
) {
460 adg_marker_set_segment(data
->marker1
, data
->trail
, 1);
461 adg_entity_local_changed((AdgEntity
*) data
->marker1
);
464 if (data
->marker2
!= NULL
) {
465 adg_marker_set_segment(data
->marker2
, data
->trail
, 1);
466 adg_entity_local_changed((AdgEntity
*) data
->marker2
);
469 /* TODO: compute the extents */
473 render(AdgEntity
*entity
, cairo_t
*cr
)
477 AdgADimPrivate
*data
;
478 AdgDimStyle
*dim_style
;
480 const cairo_path_t
*cairo_path
;
482 adim
= (AdgADim
*) entity
;
483 dim
= (AdgDim
*) entity
;
485 dim_style
= adg_dim_get_dim_style(dim
);
487 dress
= adg_dim_style_get_color_dress(dim_style
);
488 adg_entity_apply_dress(entity
, dress
, cr
);
490 if (data
->marker1
!= NULL
)
491 adg_entity_render((AdgEntity
*) data
->marker1
, cr
);
493 if (data
->marker2
!= NULL
)
494 adg_entity_render((AdgEntity
*) data
->marker2
, cr
);
496 adg_entity_render((AdgEntity
*) adg_dim_get_quote(dim
), cr
);
498 dress
= adg_dim_style_get_line_dress(dim_style
);
499 adg_entity_apply_dress(entity
, dress
, cr
);
501 cairo_path
= adg_trail_get_cairo_path(data
->trail
);
502 cairo_append_path(cr
, cairo_path
);
507 default_value(AdgDim
*dim
)
510 AdgADimPrivate
*data
;
511 AdgDimStyle
*dim_style
;
514 adim
= (AdgADim
*) dim
;
516 dim_style
= adg_dim_get_dim_style(dim
);
517 format
= adg_dim_style_get_number_format(dim_style
);
519 update_geometry(adim
);
521 return g_strdup_printf(format
, data
->angle
* 180 / M_PI
);
524 /* With "geometry" is considered any data (point, vector or angle)
525 * that can be cached: this is strictly related on how the arrange()
528 update_geometry(AdgADim
*adim
)
530 AdgADimPrivate
*data
;
531 AdgDimStyle
*dim_style
;
532 gdouble from_offset
, to_offset
;
533 gdouble spacing
, level
;
534 CpmlVector vector
[3];
540 if (data
->geometry_arranged
)
543 if (!get_info(adim
, vector
, ¢er
, &distance
)) {
544 /* Parallel lines: hang with an error message */
545 g_warning("%s: trying to set an angular dimension on parallel lines",
550 dim_style
= adg_dim_get_dim_style((AdgDim
*) adim
);
551 from_offset
= adg_dim_style_get_from_offset(dim_style
);
552 to_offset
= adg_dim_style_get_to_offset(dim_style
);
553 spacing
= adg_dim_style_get_baseline_spacing(dim_style
);
554 level
= adg_dim_get_level((AdgDim
*) adim
);
557 cpml_vector_set_length(&vector
[0], from_offset
);
558 cpml_pair_copy(&data
->shift
.from1
, &vector
[0]);
561 cpml_vector_set_length(&vector
[0], level
* spacing
);
562 cpml_pair_copy(&data
->shift
.base1
, &vector
[0]);
565 cpml_vector_set_length(&vector
[0], to_offset
);
566 cpml_pair_copy(&data
->shift
.to1
, &vector
[0]);
569 cpml_vector_set_length(&vector
[2], from_offset
);
570 cpml_pair_copy(&data
->shift
.from2
, &vector
[2]);
573 cpml_vector_set_length(&vector
[2], level
* spacing
);
574 cpml_pair_copy(&data
->shift
.base2
, &vector
[2]);
577 cpml_vector_set_length(&vector
[2], to_offset
);
578 cpml_pair_copy(&data
->shift
.to2
, &vector
[2]);
581 cpml_vector_set_length(&vector
[1], level
* spacing
);
582 cpml_pair_copy(&data
->shift
.base12
, &vector
[1]);
585 cpml_vector_set_length(&vector
[0], distance
);
586 cpml_pair_add(cpml_pair_copy(&data
->point
.base1
, &vector
[0]), ¢er
);
589 cpml_vector_set_length(&vector
[2], distance
);
590 cpml_pair_add(cpml_pair_copy(&data
->point
.base2
, &vector
[2]), ¢er
);
593 cpml_vector_set_length(&vector
[1], distance
);
594 cpml_pair_add(cpml_pair_copy(&data
->point
.base12
, &vector
[1]), ¢er
);
596 data
->geometry_arranged
= TRUE
;
600 update_entities(AdgADim
*adim
)
602 AdgADimPrivate
*data
;
603 AdgDimStyle
*dim_style
;
606 dim_style
= adg_dim_get_dim_style((AdgDim
*) adim
);
608 if (data
->trail
== NULL
)
609 data
->trail
= adg_trail_new(trail_callback
, adim
);
611 if (data
->marker1
== NULL
)
612 data
->marker1
= adg_dim_style_marker1_new(dim_style
);
614 if (data
->marker2
== NULL
)
615 data
->marker2
= adg_dim_style_marker2_new(dim_style
);
619 set_org1(AdgADim
*adim
, const AdgPair
*org1
)
621 AdgADimPrivate
*data
= adim
->data
;
623 if (adg_pair_equal(&data
->org1
, org1
))
632 set_org2(AdgADim
*adim
, const AdgPair
*org2
)
634 AdgADimPrivate
*data
= adim
->data
;
636 if (adg_pair_equal(&data
->org2
, org2
))
645 dispose_markers(AdgADim
*adim
)
647 AdgADimPrivate
*data
= adim
->data
;
649 if (data
->trail
!= NULL
) {
650 g_object_unref(data
->trail
);
654 if (data
->marker1
!= NULL
) {
655 g_object_unref(data
->marker1
);
656 data
->marker1
= NULL
;
659 if (data
->marker2
!= NULL
) {
660 g_object_unref(data
->marker2
);
661 data
->marker2
= NULL
;
666 get_info(AdgADim
*adim
, CpmlVector vector
[],
667 AdgPair
*center
, gdouble
*distance
)
670 AdgADimPrivate
*data
;
671 const AdgPair
*ref1
, *ref2
, *pos
;
672 gdouble factor
, angle1
, angle2
;
674 dim
= (AdgDim
*) adim
;
676 ref1
= adg_dim_get_ref1(dim
);
677 ref2
= adg_dim_get_ref2(dim
);
679 cpml_pair_sub(cpml_pair_copy(&vector
[0], ref1
), &data
->org1
);
680 cpml_pair_sub(cpml_pair_copy(&vector
[2], ref2
), &data
->org2
);
682 factor
= vector
[0].x
* vector
[2].y
- vector
[0].y
* vector
[2].x
;
686 factor
= ((ref1
->y
- ref2
->y
) * vector
[2].x
-
687 (ref1
->x
- ref2
->x
) * vector
[2].y
) / factor
;
689 center
->x
= ref1
->x
+ vector
[0].x
* factor
;
690 center
->y
= ref1
->y
+ vector
[0].y
* factor
;
692 pos
= adg_dim_get_pos(dim
);
694 *distance
= cpml_pair_distance(center
, pos
);
696 /* Ensure the distance is non-zero, as this would fuck up
697 * cpml_vector_set_length() */
701 angle1
= cpml_vector_angle(&vector
[0]);
702 angle2
= cpml_vector_angle(&vector
[2]);
703 while (angle2
< angle1
)
706 cpml_vector_from_angle(&vector
[1], (angle1
+ angle2
) / 2);
708 data
->angle
= angle2
- angle1
;
714 trail_callback(AdgTrail
*trail
, gpointer user_data
)
717 AdgADimPrivate
*data
;
719 adim
= (AdgADim
*) user_data
;
722 return &data
->cpml
.path
;