1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2017 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: Radial dimensions
25 * The #AdgRDim entity represents a radial dimension.
33 * All fields are private 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"
56 #include "adg-rdim-private.h"
59 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_rdim_parent_class)
60 #define _ADG_OLD_ENTITY_CLASS ((AdgEntityClass *) adg_rdim_parent_class)
63 G_DEFINE_TYPE(AdgRDim
, adg_rdim
, ADG_TYPE_DIM
)
70 static void _adg_dispose (GObject
*object
);
71 static void _adg_global_changed (AdgEntity
*entity
);
72 static void _adg_local_changed (AdgEntity
*entity
);
73 static void _adg_invalidate (AdgEntity
*entity
);
74 static void _adg_arrange (AdgEntity
*entity
);
75 static void _adg_render (AdgEntity
*entity
,
77 static gchar
* _adg_default_value (AdgDim
*dim
);
78 static gboolean
_adg_update_geometry (AdgRDim
*rdim
);
79 static void _adg_update_entities (AdgRDim
*rdim
);
80 static void _adg_clear_trail (AdgRDim
*rdim
);
81 static void _adg_dispose_trail (AdgRDim
*rdim
);
82 static void _adg_dispose_marker (AdgRDim
*rdim
);
83 static cairo_path_t
* _adg_trail_callback (AdgTrail
*trail
,
88 adg_rdim_class_init(AdgRDimClass
*klass
)
90 GObjectClass
*gobject_class
;
91 AdgEntityClass
*entity_class
;
92 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(AdgRDimPrivate
));
100 gobject_class
->dispose
= _adg_dispose
;
102 entity_class
->global_changed
= _adg_global_changed
;
103 entity_class
->local_changed
= _adg_local_changed
;
104 entity_class
->invalidate
= _adg_invalidate
;
105 entity_class
->arrange
= _adg_arrange
;
106 entity_class
->render
= _adg_render
;
108 dim_class
->default_value
= _adg_default_value
;
112 adg_rdim_init(AdgRDim
*rdim
)
114 AdgRDimPrivate
*data
;
115 cairo_path_data_t move_to
, line_to
;
117 data
= G_TYPE_INSTANCE_GET_PRIVATE(rdim
, ADG_TYPE_RDIM
, AdgRDimPrivate
);
118 move_to
.header
.type
= CPML_MOVE
;
119 move_to
.header
.length
= 2;
120 line_to
.header
.type
= CPML_LINE
;
121 line_to
.header
.length
= 2;
125 data
->geometry_arranged
= FALSE
;
127 data
->shift
.base
.x
= data
->shift
.base
.y
= 0;
128 cairo_matrix_init_identity(&data
->quote
.global_map
);
130 data
->cairo
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
131 data
->cairo
.path
.data
= data
->cairo
.data
;
132 data
->cairo
.path
.num_data
= G_N_ELEMENTS(data
->cairo
.data
);
133 data
->cairo
.path
.data
[0] = move_to
;
134 data
->cairo
.path
.data
[2] = line_to
;
135 data
->cairo
.path
.data
[4] = move_to
;
136 data
->cairo
.path
.data
[6] = line_to
;
140 /* Override the generic default dress with a more specific one */
141 adg_dim_set_dim_dress((AdgDim
*) rdim
, ADG_DRESS_DIMENSION_RADIUS
);
145 _adg_dispose(GObject
*object
)
147 AdgRDim
*rdim
= (AdgRDim
*) object
;
149 _adg_dispose_trail(rdim
);
150 _adg_dispose_marker(rdim
);
152 if (_ADG_OLD_OBJECT_CLASS
->dispose
)
153 _ADG_OLD_OBJECT_CLASS
->dispose(object
);
160 * Creates a new uninitialized radial dimension. To be useful, you
161 * need at least define the center of the arc to quote in #AdgDim:ref1,
162 * a point on the arc in #AdgDim:ref2 and the position of the quote
163 * in #AdgDim:pos using any valid #AdgDim method.
165 * Returns: a newly created quote
172 return g_object_new(ADG_TYPE_RDIM
, NULL
);
177 * @center: (allow-none): center of the arc to quote
178 * @radius: (allow-none): where the quote must be applied on the arc
179 * @pos: (allow-none): position of the quote text
181 * Creates a new quote by specifying explicitely all the needed
182 * data to get a valid quote.
184 * Returns: the newly created quote.
189 adg_rdim_new_full(const CpmlPair
*center
, const CpmlPair
*radius
,
195 rdim
= adg_rdim_new();
196 dim
= (AdgDim
*) rdim
;
199 adg_dim_set_ref1_from_pair(dim
, center
);
202 adg_dim_set_ref2_from_pair(dim
, radius
);
205 adg_dim_set_pos_from_pair(dim
, pos
);
211 * adg_rdim_new_full_explicit:
212 * @center_x: x coordinate of the center of the arc to quote
213 * @center_y: y coordinate of the center of the arc to quote
214 * @radius_x: x coordiante where the quote must be applied on the arc
215 * @radius_y: y coordiante where the quote must be applied on the arc
216 * @pos_x: x coordinate of the quote text
217 * @pos_y: y coordinate of the quote text
219 * Does the same job of adg_rdim_new_full() but using specific
220 * coordinates instead of #CpmlPair structures.
222 * Returns: the newly created quote.
227 adg_rdim_new_full_explicit(gdouble center_x
, gdouble center_y
,
228 gdouble radius_x
, gdouble radius_y
,
229 gdouble pos_x
, gdouble pos_y
)
231 CpmlPair center
, radius
, pos
;
240 return adg_rdim_new_full(¢er
, &radius
, &pos
);
244 * adg_rdim_new_full_from_model:
245 * @model: (transfer none): the model from which the named pairs are taken
246 * @center: (allow-none): the center point of the arc to quote
247 * @radius: (allow-none): an arbitrary point on the arc
248 * @pos: (allow-none): the position reference
250 * Creates a new radial dimension, specifing all the needed properties in
251 * one shot and using named pairs from @model.
253 * Returns: the newly created radial dimension entity
258 adg_rdim_new_full_from_model(AdgModel
*model
, const gchar
*center
,
259 const gchar
*radius
, const gchar
*pos
)
264 g_return_val_if_fail(model
!= NULL
, NULL
);
266 rdim
= adg_rdim_new();
267 dim
= (AdgDim
*) rdim
;
270 adg_dim_set_ref1_from_model(dim
, model
, center
);
273 adg_dim_set_ref2_from_model(dim
, model
, radius
);
276 adg_dim_set_pos_from_model(dim
, model
, pos
);
283 _adg_global_changed(AdgEntity
*entity
)
285 AdgRDimPrivate
*data
= ((AdgRDim
*) entity
)->data
;
287 _adg_clear_trail((AdgRDim
*) entity
);
289 if (_ADG_OLD_ENTITY_CLASS
->global_changed
)
290 _ADG_OLD_ENTITY_CLASS
->global_changed(entity
);
292 if (data
->marker
!= NULL
)
293 adg_entity_global_changed((AdgEntity
*) data
->marker
);
297 _adg_local_changed(AdgEntity
*entity
)
299 _adg_clear_trail((AdgRDim
*) entity
);
301 if (_ADG_OLD_ENTITY_CLASS
->local_changed
)
302 _ADG_OLD_ENTITY_CLASS
->local_changed(entity
);
306 _adg_invalidate(AdgEntity
*entity
)
309 AdgRDimPrivate
*data
;
311 rdim
= (AdgRDim
*) entity
;
314 _adg_dispose_trail(rdim
);
315 _adg_dispose_marker(rdim
);
316 data
->geometry_arranged
= FALSE
;
317 _adg_clear_trail(rdim
);
319 if (_ADG_OLD_ENTITY_CLASS
->invalidate
)
320 _ADG_OLD_ENTITY_CLASS
->invalidate(entity
);
324 _adg_arrange(AdgEntity
*entity
)
328 AdgRDimPrivate
*data
;
330 AdgEntity
*quote_entity
;
332 const cairo_matrix_t
*global
, *local
;
337 if (_ADG_OLD_ENTITY_CLASS
->arrange
!= NULL
)
338 _ADG_OLD_ENTITY_CLASS
->arrange(entity
);
340 rdim
= (AdgRDim
*) entity
;
341 dim
= (AdgDim
*) rdim
;
343 quote
= adg_dim_get_quote(dim
);
344 quote_entity
= (AdgEntity
*) quote
;
346 if (! _adg_update_geometry(rdim
))
349 _adg_update_entities(rdim
);
351 /* Check for cached result */
352 if (data
->cairo
.path
.status
== CAIRO_STATUS_SUCCESS
) {
353 adg_entity_set_global_map(quote_entity
, &data
->quote
.global_map
);
357 outside
= adg_dim_get_outside(dim
);
358 if (outside
== ADG_THREE_STATE_UNKNOWN
)
359 outside
= ADG_THREE_STATE_OFF
;
361 global
= adg_entity_get_global_matrix(entity
);
362 local
= adg_entity_get_local_matrix(entity
);
363 extents
.is_defined
= FALSE
;
365 cpml_pair_copy(&ref2
, (CpmlPair
*) adg_dim_get_ref2(dim
));
366 cpml_pair_copy(&base
, &data
->point
.base
);
368 cpml_pair_transform(&ref2
, local
);
369 cpml_pair_transform(&base
, local
);
370 base
.x
+= data
->shift
.base
.x
;
371 base
.y
+= data
->shift
.base
.y
;
374 cpml_pair_to_cairo(&base
, &data
->cairo
.data
[1]);
377 cpml_pair_to_cairo(&ref2
, &data
->cairo
.data
[3]);
380 AdgDimStyle
*dim_style
;
384 dim_style
= _ADG_GET_DIM_STYLE(dim
);
385 beyond
= adg_dim_style_get_beyond(dim_style
);
386 vector
.x
= ref2
.x
- base
.x
;
387 vector
.y
= ref2
.y
- base
.y
;
388 cpml_vector_set_length(&vector
, beyond
);
389 pair
.x
= ref2
.x
+ vector
.x
;
390 pair
.y
= ref2
.y
+ vector
.y
;
392 data
->cairo
.data
[2].header
.length
= 2;
394 /* Outside segment start */
395 cpml_pair_to_cairo(&pair
, &data
->cairo
.data
[5]);
397 /* Outside segment end */
398 cpml_pair_to_cairo(&ref2
, &data
->cairo
.data
[7]);
400 data
->cairo
.data
[2].header
.length
= 6;
403 /* Arrange the quote */
407 adg_alignment_set_factor_explicit(quote
, 1, 0);
409 cpml_pair_from_cairo(&pair
, &data
->cairo
.data
[1]);
410 cairo_matrix_init_translate(&map
, pair
.x
, pair
.y
);
411 cairo_matrix_rotate(&map
, data
->angle
);
412 adg_entity_set_global_map(quote_entity
, &map
);
413 adg_entity_arrange(quote_entity
);
414 cpml_extents_add(&extents
, adg_entity_get_extents(quote_entity
));
416 adg_matrix_copy(&data
->quote
.global_map
, &map
);
419 data
->cairo
.path
.status
= CAIRO_STATUS_SUCCESS
;
421 /* Arrange the trail */
422 if (data
->trail
!= NULL
) {
423 CpmlExtents trail_extents
;
424 cpml_extents_copy(&trail_extents
, adg_trail_get_extents(data
->trail
));
425 cpml_extents_transform(&trail_extents
, global
);
426 cpml_extents_add(&extents
, &trail_extents
);
428 _adg_dispose_marker(rdim
);
431 /* Arrange the marker */
432 if (data
->marker
!= NULL
) {
433 AdgEntity
*marker_entity
= (AdgEntity
*) data
->marker
;
434 adg_marker_set_segment(data
->marker
, data
->trail
, outside
? 2 : 1);
435 adg_entity_local_changed(marker_entity
);
436 adg_entity_arrange(marker_entity
);
437 cpml_extents_add(&extents
, adg_entity_get_extents(marker_entity
));
440 adg_entity_set_extents(entity
, &extents
);
444 _adg_render(AdgEntity
*entity
, cairo_t
*cr
)
448 AdgRDimPrivate
*data
;
449 AdgDimStyle
*dim_style
;
451 const cairo_path_t
*cairo_path
;
453 rdim
= (AdgRDim
*) entity
;
456 if (!data
->geometry_arranged
) {
457 /* Entity not arranged, probably due to undefined pair found */
461 dim
= (AdgDim
*) entity
;
462 dim_style
= _ADG_GET_DIM_STYLE(dim
);
464 adg_style_apply((AdgStyle
*) dim_style
, entity
, cr
);
465 adg_entity_render((AdgEntity
*) adg_dim_get_quote(dim
), cr
);
467 if (data
->marker
!= NULL
)
468 adg_entity_render((AdgEntity
*) data
->marker
, cr
);
470 cairo_transform(cr
, adg_entity_get_global_matrix(entity
));
471 dress
= adg_dim_style_get_line_dress(dim_style
);
472 adg_entity_apply_dress(entity
, dress
, cr
);
474 cairo_path
= adg_trail_get_cairo_path(data
->trail
);
475 cairo_append_path(cr
, cairo_path
);
480 _adg_default_value(AdgDim
*dim
)
483 AdgRDimPrivate
*data
;
485 rdim
= (AdgRDim
*) dim
;
488 if (! _adg_update_geometry(rdim
))
489 return g_strdup("undef");
491 return adg_dim_get_text(dim
, data
->radius
);
495 _adg_update_geometry(AdgRDim
*rdim
)
497 AdgRDimPrivate
*data
;
499 AdgDimStyle
*dim_style
;
500 AdgPoint
*ref1_point
, *ref2_point
, *pos_point
;
501 const CpmlPair
*ref1
, *ref2
, *pos
;
502 gdouble spacing
, level
, pos_distance
;
507 /* Check for cached results */
508 if (data
->geometry_arranged
)
511 dim
= (AdgDim
*) rdim
;
512 ref1_point
= adg_dim_get_ref1(dim
);
513 ref2_point
= adg_dim_get_ref2(dim
);
514 pos_point
= adg_dim_get_pos(dim
);
516 /* Check if the needed points are all properly defined */
517 if (! adg_point_update(ref1_point
) ||
518 ! adg_point_update(ref2_point
) ||
519 ! adg_point_update(pos_point
))
522 ref1
= (CpmlPair
*) ref1_point
;
523 ref2
= (CpmlPair
*) ref2_point
;
524 pos
= (CpmlPair
*) pos_point
;
526 /* Check if the given points have valid coordinates */
527 if (cpml_pair_equal(ref1
, ref2
)) {
528 g_warning(_("%s: ref1 and ref2 cannot be coincidents (%lf, %lf)"),
529 G_STRLOC
, ref1
->x
, ref1
->y
);
533 dim_style
= _ADG_GET_DIM_STYLE(rdim
);
534 spacing
= adg_dim_style_get_baseline_spacing(dim_style
);
535 level
= adg_dim_get_level(dim
);
536 pos_distance
= cpml_pair_distance(pos
, ref1
);
537 vector
.x
= ref2
->x
- ref1
->x
;
538 vector
.y
= ref2
->y
- ref1
->y
;
540 if (cpml_pair_squared_distance(pos
, ref1
) <
541 cpml_pair_squared_distance(pos
, ref2
)) {
542 vector
.x
= -vector
.x
;
543 vector
.y
= -vector
.y
;
547 data
->radius
= cpml_pair_distance(&vector
, NULL
);
550 data
->angle
= adg_dim_quote_angle(dim
, cpml_vector_angle(&vector
));
553 cpml_pair_copy(&data
->point
.base
, &vector
);
554 cpml_vector_set_length(&data
->point
.base
, pos_distance
);
555 data
->point
.base
.x
+= ref1
->x
;
556 data
->point
.base
.y
+= ref1
->y
;
559 cpml_pair_copy(&data
->shift
.base
, &vector
);
560 cpml_vector_set_length(&data
->shift
.base
, spacing
* level
);
562 data
->geometry_arranged
= TRUE
;
568 _adg_update_entities(AdgRDim
*rdim
)
571 AdgRDimPrivate
*data
;
572 AdgDimStyle
*dim_style
;
574 entity
= (AdgEntity
*) rdim
;
576 dim_style
= _ADG_GET_DIM_STYLE(rdim
);
578 if (data
->trail
== NULL
)
579 data
->trail
= adg_trail_new(_adg_trail_callback
, rdim
);
581 if (data
->marker
== NULL
) {
582 data
->marker
= adg_dim_style_marker2_new(dim_style
);
583 adg_entity_set_parent((AdgEntity
*) data
->marker
, entity
);
588 _adg_clear_trail(AdgRDim
*rdim
)
590 AdgRDimPrivate
*data
= rdim
->data
;
592 if (data
->trail
!= NULL
)
593 adg_model_clear((AdgModel
*) data
->trail
);
595 data
->cairo
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
599 _adg_dispose_trail(AdgRDim
*rdim
)
601 AdgRDimPrivate
*data
= rdim
->data
;
603 if (data
->trail
!= NULL
) {
604 g_object_unref(data
->trail
);
610 _adg_dispose_marker(AdgRDim
*rdim
)
612 AdgRDimPrivate
*data
= rdim
->data
;
614 if (data
->marker
!= NULL
) {
615 g_object_unref(data
->marker
);
620 static cairo_path_t
*
621 _adg_trail_callback(AdgTrail
*trail
, gpointer user_data
)
624 AdgRDimPrivate
*data
;
626 rdim
= (AdgRDim
*) user_data
;
629 return &data
->cairo
.path
;