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: Linear dimensions
25 * The #AdgLDim entity represents a linear dimension.
31 * All fields are private and should not be used directly.
32 * Use its public methods instead.
37 #include "adg-ldim-private.h"
38 #include "adg-dim-style.h"
39 #include "adg-line-style.h"
40 #include "adg-color-style.h"
43 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_ldim_parent_class)
44 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_ldim_parent_class)
55 static void dispose (GObject
*object
);
56 static void get_property (GObject
*object
,
60 static void set_property (GObject
*object
,
64 static void invalidate (AdgEntity
*entity
);
65 static void render (AdgEntity
*entity
,
67 static gchar
* default_value (AdgDim
*dim
);
68 static void fill_context (AdgLDim
*ldim
,
69 AdgLDimContext
*context
);
70 static void update_shifts (AdgLDim
*ldim
,
71 const AdgLDimContext
*context
);
72 static void layout (AdgLDim
*ldim
,
73 const AdgLDimContext
*context
);
74 static void render_cage (AdgLDim
*ldim
,
75 const AdgLDimContext
*context
);
76 static void dispose_markers (AdgLDim
*ldim
);
77 static CpmlPath
* trail_callback (AdgTrail
*trail
,
81 G_DEFINE_TYPE(AdgLDim
, adg_ldim
, ADG_TYPE_DIM
);
85 adg_ldim_class_init(AdgLDimClass
*klass
)
87 GObjectClass
*gobject_class
;
88 AdgEntityClass
*entity_class
;
89 AdgDimClass
*dim_class
;
92 gobject_class
= (GObjectClass
*) klass
;
93 entity_class
= (AdgEntityClass
*) klass
;
94 dim_class
= (AdgDimClass
*) klass
;
96 g_type_class_add_private(klass
, sizeof(AdgLDimPrivate
));
98 gobject_class
->dispose
= dispose
;
99 gobject_class
->get_property
= get_property
;
100 gobject_class
->set_property
= set_property
;
102 entity_class
->invalidate
= invalidate
;
103 entity_class
->render
= render
;
105 dim_class
->default_value
= default_value
;
107 param
= g_param_spec_double("direction",
109 P_("The inclination angle of the extension lines"),
110 -G_MAXDOUBLE
, G_MAXDOUBLE
, ADG_DIR_RIGHT
,
111 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
);
112 g_object_class_install_property(gobject_class
, PROP_DIRECTION
, param
);
114 param
= g_param_spec_boolean("has-extension1",
115 P_("Has First Extension Line flag"),
116 P_("Show (TRUE) or hide (FALSE) the first extension line"),
117 TRUE
, G_PARAM_READWRITE
);
118 g_object_class_install_property(gobject_class
, PROP_HAS_EXTENSION1
, param
);
120 param
= g_param_spec_boolean("has-extension2",
121 P_("Has Second Extension Line flag"),
122 P_("Show (TRUE) or hide (FALSE) the second extension line"),
123 TRUE
, G_PARAM_READWRITE
);
124 g_object_class_install_property(gobject_class
, PROP_HAS_EXTENSION2
, param
);
128 adg_ldim_init(AdgLDim
*ldim
)
130 AdgLDimPrivate
*data
;
131 cairo_path_data_t move_to
, line_to
;
133 data
= G_TYPE_INSTANCE_GET_PRIVATE(ldim
, ADG_TYPE_LDIM
, AdgLDimPrivate
);
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;
140 data
->has_extension1
= TRUE
;
141 data
->has_extension2
= TRUE
;
143 data
->cpml
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
144 data
->cpml
.path
.data
= data
->cpml
.data
;
145 data
->cpml
.path
.num_data
= G_N_ELEMENTS(data
->cpml
.data
);
146 data
->cpml
.path
.data
[0] = move_to
;
147 data
->cpml
.path
.data
[2] = line_to
;
148 data
->cpml
.path
.data
[4] = move_to
;
149 data
->cpml
.path
.data
[6] = line_to
;
150 data
->cpml
.path
.data
[8] = move_to
;
151 data
->cpml
.path
.data
[10] = line_to
;
152 data
->cpml
.path
.data
[12] = move_to
;
153 data
->cpml
.path
.data
[14] = line_to
;
154 data
->cpml
.path
.data
[16] = move_to
;
155 data
->cpml
.path
.data
[18] = line_to
;
158 data
->marker1
= NULL
;
159 data
->marker2
= NULL
;
165 dispose(GObject
*object
)
167 dispose_markers((AdgLDim
*) object
);
169 if (PARENT_OBJECT_CLASS
->dispose
!= NULL
)
170 PARENT_OBJECT_CLASS
->dispose(object
);
174 get_property(GObject
*object
,
175 guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
177 AdgLDimPrivate
*data
= ((AdgLDim
*) object
)->data
;
181 g_value_set_double(value
, data
->direction
);
183 case PROP_HAS_EXTENSION1
:
184 g_value_set_boolean(value
, data
->has_extension1
);
186 case PROP_HAS_EXTENSION2
:
187 g_value_set_boolean(value
, data
->has_extension2
);
190 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
196 set_property(GObject
*object
,
197 guint prop_id
, const GValue
*value
, GParamSpec
*pspec
)
199 AdgLDimPrivate
*data
= ((AdgLDim
*) object
)->data
;
203 data
->direction
= g_value_get_double(value
);
205 case PROP_HAS_EXTENSION1
:
206 data
->has_extension1
= g_value_get_boolean(value
);
208 case PROP_HAS_EXTENSION2
:
209 data
->has_extension2
= g_value_get_boolean(value
);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
221 * Creates a new - unreferenced - linear dimension. You must, at least, define
222 * the reference points with adg_dim_set_ref(), the dimension direction with
223 * adg_ldim_set_direction() and the position reference using adg_dim_set_pos()
224 * or, better, adg_ldim_set_pos().
226 * Returns: the newly created linear dimension entity
231 return g_object_new(ADG_TYPE_LDIM
, NULL
);
236 * @ref1: the first reference point
237 * @ref2: the second reference point
238 * @direction: angle where to extend the dimension
239 * @pos: the position reference
241 * Creates a new linear dimension, specifing all the needed properties in
244 * Returns: the newly created linear dimension entity
247 adg_ldim_new_full(const AdgPair
*ref1
, const AdgPair
*ref2
,
248 gdouble direction
, const AdgPair
*pos
)
250 AdgLDim
*ldim
= g_object_new(ADG_TYPE_LDIM
, "ref1", ref1
, "ref2", ref2
,
251 "direction", direction
, NULL
);
252 adg_ldim_set_pos(ldim
, pos
);
257 * adg_ldim_new_full_explicit:
258 * @ref1_x: the x coordinate of the first reference point
259 * @ref1_y: the y coordinate of the first reference point
260 * @ref2_x: the x coordinate of the second reference point
261 * @ref2_y: the y coordinate of the second reference point
262 * @direction: angle where to extend the dimension
263 * @pos_x: the x coordinate of the position reference
264 * @pos_y: the y coordinate of the position reference
266 * Wrappes adg_ldim_new_full() with explicit values.
268 * Returns: the newly created linear dimension entity
271 adg_ldim_new_full_explicit(gdouble ref1_x
, gdouble ref1_y
,
272 gdouble ref2_x
, gdouble ref2_y
,
273 gdouble direction
, gdouble pos_x
, gdouble pos_y
)
286 return adg_ldim_new_full(&ref1
, &ref2
, direction
, &pos
);
291 * @ldim: an #AdgLDim entity
292 * @pos: an #AdgPair structure
294 * Sets the position references (pos1 and pos2 properties) of @ldim using a
295 * single @pos point. Before this call, @ldim MUST HAVE defined the reference
296 * points and the direction. If these conditions are not met, an error message
297 * is logged and the position references will not be set.
300 adg_ldim_set_pos(AdgLDim
*ldim
, const AdgPair
*pos
)
302 AdgLDimPrivate
*data
;
303 const AdgPair
*ref1
, *ref2
;
305 CpmlPair baseline_vector
, extension_vector
;
308 g_return_if_fail(ADG_IS_LDIM(ldim
));
311 ref1
= adg_dim_get_ref1((AdgDim
*) ldim
);
312 ref2
= adg_dim_get_ref2((AdgDim
*) ldim
);
314 cpml_vector_from_angle(&extension_vector
, data
->direction
, 1);
316 baseline_vector
.x
= -extension_vector
.y
;
317 baseline_vector
.y
= extension_vector
.x
;
319 d
= extension_vector
.y
* baseline_vector
.x
-
320 extension_vector
.x
* baseline_vector
.y
;
321 g_return_if_fail(d
!= 0);
323 k
= ((pos
->y
- ref1
->y
) * baseline_vector
.x
-
324 (pos
->x
- ref1
->x
) * baseline_vector
.y
) / d
;
325 pos1
.x
= ref1
->x
+ k
* extension_vector
.x
;
326 pos1
.y
= ref1
->y
+ k
* extension_vector
.y
;
328 k
= ((pos
->y
- ref2
->y
) * baseline_vector
.x
-
329 (pos
->x
- ref2
->x
) * baseline_vector
.y
) / d
;
330 pos2
.x
= ref2
->x
+ k
* extension_vector
.x
;
331 pos2
.y
= ref2
->y
+ k
* extension_vector
.y
;
333 adg_dim_set_pos((AdgDim
*) ldim
, &pos1
, &pos2
);
337 * adg_ldim_set_pos_explicit:
338 * @ldim: an #AdgLDim entity
339 * @pos_x: the new x coordinate position reference
340 * @pos_y: the new y coordinate position reference
342 * Wrappers adg_ldim_set_pos() with explicit coordinates.
345 adg_ldim_set_pos_explicit(AdgLDim
*ldim
, gdouble pos_x
, gdouble pos_y
)
352 adg_ldim_set_pos(ldim
, &pos
);
356 * adg_ldim_get_direction:
357 * @ldim: an #AdgLDim entity
359 * Gets the direction where @ldim will extend.
361 * Returns: the direction angle in radians
364 adg_ldim_get_direction(AdgLDim
*ldim
)
366 AdgLDimPrivate
*data
;
368 g_return_val_if_fail(ADG_IS_LDIM(ldim
), 0);
372 return data
->direction
;
376 * adg_ldim_set_direction:
377 * @ldim: an #AdgLDim entity
378 * @direction: an angle value, in radians
380 * Sets the direction angle where to extend @ldim.
383 adg_ldim_set_direction(AdgLDim
*ldim
, gdouble direction
)
385 AdgLDimPrivate
*data
;
387 g_return_if_fail(ADG_IS_LDIM(ldim
));
390 data
->direction
= direction
;
392 g_object_notify((GObject
*) ldim
, "direction");
396 * adg_ldim_has_extension1:
397 * @ldim: an #AdgLDim entity
399 * Checks if @ldim should render also the first extension line.
401 * Returns: %TRUE on first extension line presents, %FALSE otherwise
404 adg_ldim_has_extension1(AdgLDim
*ldim
)
406 AdgLDimPrivate
*data
;
408 g_return_val_if_fail(ADG_IS_LDIM(ldim
), FALSE
);
412 return data
->has_extension1
;
416 * adg_ldim_switch_extension1:
417 * @ldim: an #AdgLDim entity
418 * @state: the new state
420 * Shows (if @state is %TRUE) or hide (if @state is %FALSE) the first
421 * extension line of @ldim.
424 adg_ldim_switch_extension1(AdgLDim
*ldim
, gboolean state
)
426 AdgLDimPrivate
*data
;
428 g_return_if_fail(ADG_IS_LDIM(ldim
));
432 data
->has_extension1
= state
;
433 g_object_notify((GObject
*) ldim
, "has-extension1");
437 * adg_ldim_has_extension2:
438 * @ldim: an #AdgLDim entity
440 * Checks if @ldim should render also the second extension line.
442 * Returns: %TRUE on first extension line presents, %FALSE otherwise
445 adg_ldim_has_extension2(AdgLDim
*ldim
)
447 AdgLDimPrivate
*data
;
449 g_return_val_if_fail(ADG_IS_LDIM(ldim
), FALSE
);
453 return data
->has_extension2
;
457 * adg_ldim_switch_extension2:
458 * @ldim: an #AdgLDim entity
459 * @state: the new state
461 * Shows (if @state is %TRUE) or hide (if @state is %FALSE) the second
462 * extension line of @ldim.
465 adg_ldim_switch_extension2(AdgLDim
*ldim
, gboolean state
)
467 AdgLDimPrivate
*data
;
469 g_return_if_fail(ADG_IS_LDIM(ldim
));
473 data
->has_extension2
= state
;
474 g_object_notify((GObject
*) ldim
, "has-extension2");
479 invalidate(AdgEntity
*entity
)
482 AdgLDimPrivate
*data
;
484 ldim
= (AdgLDim
*) entity
;
487 data
->shift
.is_arranged
= FALSE
;
488 dispose_markers((AdgLDim
*) entity
);
490 if (PARENT_ENTITY_CLASS
->invalidate
!= NULL
)
491 PARENT_ENTITY_CLASS
->invalidate(entity
);
495 render(AdgEntity
*entity
, cairo_t
*cr
)
498 AdgLDimContext context
;
500 ldim
= (AdgLDim
*) entity
;
503 fill_context(ldim
, &context
);
504 layout(ldim
, &context
);
506 adg_style_apply((AdgStyle
*) context
.color_style
, cr
);
508 render_cage(ldim
, &context
);
509 adg_entity_render((AdgEntity
*) context
.quote
, cr
);
513 default_value(AdgDim
*dim
)
516 const AdgPair
*pos1
, *pos2
;
521 entity
= (AdgEntity
*) dim
;
522 pos1
= adg_dim_get_pos1(dim
);
523 pos2
= adg_dim_get_pos2(dim
);
524 distance
= cpml_pair_distance(pos1
, pos2
);
525 dim_style
= adg_entity_style(entity
, adg_dim_get_dress(dim
));
526 format
= adg_dim_style_get_number_format((AdgDimStyle
*) dim_style
);
528 return g_strdup_printf(format
, distance
);
532 fill_context(AdgLDim
*ldim
, AdgLDimContext
*context
)
538 dim
= (AdgDim
*) ldim
;
539 entity
= (AdgEntity
*) ldim
;
541 /* Resolve dresses to styles once for all */
542 dress
= adg_dim_get_dress(dim
);
543 context
->dim_style
= (AdgDimStyle
*) adg_entity_style(entity
, dress
);
545 dress
= adg_dim_style_get_line_dress(context
->dim_style
);
546 context
->line_style
= (AdgLineStyle
*) adg_entity_style(entity
, dress
);
548 dress
= adg_dim_style_get_color_dress(context
->dim_style
);
549 context
->color_style
= (AdgColorStyle
*) adg_entity_style(entity
, dress
);
551 context
->quote
= adg_dim_get_quote(dim
, context
->cr
);
555 update_shifts(AdgLDim
*ldim
, const AdgLDimContext
*context
)
557 AdgLDimPrivate
*data
;
561 gdouble baseline_spacing
;
565 from_offset
= adg_dim_style_get_from_offset(context
->dim_style
);
566 to_offset
= adg_dim_style_get_to_offset(context
->dim_style
);
567 baseline_spacing
= adg_dim_style_get_baseline_spacing(context
->dim_style
);
568 level
= adg_dim_get_level((AdgDim
*) ldim
);
570 cairo_matrix_init_rotate(&matrix
, data
->direction
);
572 data
->shift
.from
.x
= data
->shift
.from
.y
= 0;
573 data
->shift
.marker
.x
= data
->shift
.marker
.y
= 0;
574 data
->shift
.to
.x
= data
->shift
.to
.y
= 0;
576 cairo_matrix_translate(&matrix
, from_offset
, 0);
577 cpml_pair_transform(&data
->shift
.from
, &matrix
);
578 cairo_matrix_translate(&matrix
, to_offset
-from_offset
, 0);
579 cpml_pair_transform(&data
->shift
.to
, &matrix
);
580 cairo_matrix_translate(&matrix
, level
*baseline_spacing
-to_offset
, 0);
581 cpml_pair_transform(&data
->shift
.marker
, &matrix
);
583 data
->shift
.is_arranged
= TRUE
;
587 layout(AdgLDim
*ldim
, const AdgLDimContext
*context
)
591 AdgLDimPrivate
*data
;
593 AdgPair ref1
, ref2
, pos1
, pos2
;
598 entity
= (AdgEntity
*) ldim
;
599 dim
= (AdgDim
*) ldim
;
601 adg_entity_get_local_matrix((AdgEntity
*) ldim
, &local
);
602 cpml_pair_copy(&ref1
, adg_dim_get_ref1(dim
));
603 cpml_pair_copy(&ref2
, adg_dim_get_ref2(dim
));
604 cpml_pair_copy(&pos1
, adg_dim_get_pos1(dim
));
605 cpml_pair_copy(&pos2
, adg_dim_get_pos2(dim
));
608 if (!data
->shift
.is_arranged
)
609 update_shifts(ldim
, context
);
611 cpml_pair_transform(&ref1
, &local
);
612 cpml_pair_transform(&ref2
, &local
);
613 cpml_pair_transform(&pos1
, &local
);
614 cpml_pair_transform(&pos2
, &local
);
616 cpml_pair_add(cpml_pair_copy(&pair
, &ref1
), &data
->shift
.from
);
617 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[13]);
619 cpml_pair_add(cpml_pair_copy(&pair
, &pos1
), &data
->shift
.marker
);
620 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[1]);
622 cpml_pair_add(&pair
, &data
->shift
.to
);
623 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[15]);
625 cpml_pair_add(cpml_pair_copy(&pair
, &ref2
), &data
->shift
.from
);
626 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[17]);
628 cpml_pair_add(cpml_pair_copy(&pair
, &pos2
), &data
->shift
.marker
);
629 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[3]);
631 cpml_pair_add(&pair
, &data
->shift
.to
);
632 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[19]);
634 /* Calculate the outside segments */
639 beyond
= adg_dim_style_get_beyond(context
->dim_style
);
640 cpml_pair_from_cairo(&pair
, &data
->cpml
.data
[1]);
642 cpml_pair_from_cairo(&vector
, &data
->cpml
.data
[3]);
643 cpml_pair_sub(&vector
, &pair
);
644 cpml_vector_set_length(&vector
, beyond
);
646 cpml_pair_from_cairo(&pair
, &data
->cpml
.data
[1]);
647 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[5]);
649 cpml_pair_sub(&pair
, &vector
);
650 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[7]);
652 cpml_pair_from_cairo(&pair
, &data
->cpml
.data
[3]);
653 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[11]);
655 cpml_pair_add(&pair
, &vector
);
656 cpml_pair_to_cairo(&pair
, &data
->cpml
.data
[9]);
658 data
->cpml
.data
[2].header
.length
= 2;
659 data
->cpml
.data
[10].header
.length
= 2;
662 data
->cpml
.data
[2].header
.length
= 10;
666 /* Play with header lengths to show or hide the extension lines */
667 if (data
->has_extension1
) {
668 data
->cpml
.data
[14].header
.length
= data
->has_extension2
? 2 : 6;
670 data
->cpml
.data
[n
].header
.length
+= 4;
671 if (!data
->has_extension2
)
672 data
->cpml
.data
[n
].header
.length
+= 4;
675 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
677 if (context
->quote
!= NULL
) {
678 /* Update global and local map of the quote container */
683 quote
= (AdgEntity
*) context
->quote
;
684 angle
= adg_dim_quote_angle(dim
, data
->direction
+ G_PI_2
);
685 adg_matrix_copy(&matrix
, &local
);
686 cairo_matrix_invert(&matrix
);
688 pair
.x
= (data
->cpml
.data
[1].point
.x
+ data
->cpml
.data
[3].point
.x
) / 2;
689 pair
.y
= (data
->cpml
.data
[1].point
.y
+ data
->cpml
.data
[3].point
.y
) / 2;
690 cairo_matrix_transform_point(&matrix
, &pair
.x
, &pair
.y
);
691 cairo_matrix_init_translate(&matrix
, pair
.x
, pair
.y
);
692 adg_entity_set_local_map(quote
, &matrix
);
694 cairo_matrix_init_rotate(&matrix
, angle
);
695 adg_entity_before_global_map(quote
, &matrix
);
698 /* Create the internal entities, if needed */
699 if (data
->trail
== NULL
)
700 data
->trail
= adg_trail_new(trail_callback
, ldim
);
702 if (data
->marker1
== NULL
)
703 data
->marker1
= adg_dim_style_marker1_new(context
->dim_style
);
704 if (data
->marker1
!= NULL
)
705 adg_marker_set_segment(data
->marker1
, data
->trail
, outside
? 2 : 1);
707 if (data
->marker2
== NULL
)
708 data
->marker2
= adg_dim_style_marker2_new(context
->dim_style
);
709 if (data
->marker2
!= NULL
)
710 adg_marker_set_segment(data
->marker2
, data
->trail
, outside
? 3 : 1);
714 render_cage(AdgLDim
*ldim
, const AdgLDimContext
*context
)
716 AdgLDimPrivate
*data
= ldim
->data
;
718 if (data
->marker1
!= NULL
)
719 adg_entity_render((AdgEntity
*) data
->marker1
, context
->cr
);
721 if (data
->marker2
!= NULL
)
722 adg_entity_render((AdgEntity
*) data
->marker2
, context
->cr
);
724 /* This CpmlPath has no arcs, so it can be fed directly into cairo */
725 adg_style_apply((AdgStyle
*) context
->line_style
, context
->cr
);
726 cairo_append_path(context
->cr
, &data
->cpml
.path
);
727 cairo_stroke(context
->cr
);
731 dispose_markers(AdgLDim
*ldim
)
733 AdgLDimPrivate
*data
= ldim
->data
;
735 if (data
->trail
!= NULL
) {
736 g_object_unref(data
->trail
);
740 if (data
->marker1
!= NULL
) {
741 g_object_unref(data
->marker1
);
742 data
->marker1
= NULL
;
745 if (data
->marker2
!= NULL
) {
746 g_object_unref(data
->marker2
);
747 data
->marker2
= NULL
;
752 trail_callback(AdgTrail
*trail
, gpointer user_data
)
755 AdgLDimPrivate
*data
;
757 ldim
= (AdgLDim
*) user_data
;
760 adg_trail_clear_cairo_path(trail
);
762 return &data
->cpml
.path
;