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 #AdgRDim 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-rdim-private.h"
38 #include "adg-dim-private.h"
39 #include "adg-dim-style.h"
42 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_rdim_parent_class)
43 #define PARENT_ENTITY_CLASS ((AdgEntityClass *) adg_rdim_parent_class)
46 static void dispose (GObject
*object
);
47 static void local_changed (AdgEntity
*entity
);
48 static void invalidate (AdgEntity
*entity
);
49 static void arrange (AdgEntity
*entity
);
50 static void render (AdgEntity
*entity
,
52 static gchar
* default_value (AdgDim
*dim
);
53 static void update_geometry (AdgRDim
*rdim
);
54 static void update_entities (AdgRDim
*rdim
);
55 static void unset_trail (AdgRDim
*rdim
);
56 static void dispose_marker (AdgRDim
*rdim
);
57 static CpmlPath
* trail_callback (AdgTrail
*trail
,
61 G_DEFINE_TYPE(AdgRDim
, adg_rdim
, ADG_TYPE_DIM
);
65 adg_rdim_class_init(AdgRDimClass
*klass
)
67 GObjectClass
*gobject_class
;
68 AdgEntityClass
*entity_class
;
69 AdgDimClass
*dim_class
;
71 gobject_class
= (GObjectClass
*) klass
;
72 entity_class
= (AdgEntityClass
*) klass
;
73 dim_class
= (AdgDimClass
*) klass
;
75 g_type_class_add_private(klass
, sizeof(AdgRDimPrivate
));
77 gobject_class
->dispose
= dispose
;
79 entity_class
->local_changed
= local_changed
;
80 entity_class
->invalidate
= invalidate
;
81 entity_class
->arrange
= arrange
;
82 entity_class
->render
= render
;
84 dim_class
->default_value
= default_value
;
88 adg_rdim_init(AdgRDim
*rdim
)
91 cairo_path_data_t move_to
, line_to
;
93 data
= G_TYPE_INSTANCE_GET_PRIVATE(rdim
, ADG_TYPE_RDIM
, AdgRDimPrivate
);
94 move_to
.header
.type
= CAIRO_PATH_MOVE_TO
;
95 move_to
.header
.length
= 2;
96 line_to
.header
.type
= CAIRO_PATH_LINE_TO
;
97 line_to
.header
.length
= 2;
101 data
->geometry_arranged
= FALSE
;
103 data
->shift
.base
.x
= data
->shift
.base
.y
= 0;
104 cairo_matrix_init_identity(&data
->quote
.local_map
);
105 cairo_matrix_init_identity(&data
->quote
.global_map
);
107 data
->cpml
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
108 data
->cpml
.path
.data
= data
->cpml
.data
;
109 data
->cpml
.path
.num_data
= G_N_ELEMENTS(data
->cpml
.data
);
110 data
->cpml
.path
.data
[0] = move_to
;
111 data
->cpml
.path
.data
[2] = line_to
;
112 data
->cpml
.path
.data
[4] = move_to
;
113 data
->cpml
.path
.data
[6] = line_to
;
119 dispose(GObject
*object
)
121 dispose_marker((AdgRDim
*) object
);
123 if (PARENT_OBJECT_CLASS
->dispose
!= NULL
)
124 PARENT_OBJECT_CLASS
->dispose(object
);
131 * Creates a new uninitialized radial dimension. To be useful, you
132 * need at least define the center of the arc to quote (ref1) and
133 * a point on the arc (ref2) with adg_dim_set_ref() and the
134 * position of the quote with adg_dim_set_pos().
136 * Returns: a newly created quote
141 return g_object_new(ADG_TYPE_RDIM
, NULL
);
146 * @center: center of the arc to quote
147 * @radius: where the quote must be applied on the arc
148 * @pos: position of the quote text
150 * Creates a new quote by specifying explicitely all the needed
151 * data to get a valid quote.
153 * Returns: a newly created quote
156 adg_rdim_new_full(const AdgPair
*center
, const AdgPair
*radius
,
159 return g_object_new(ADG_TYPE_RDIM
, "ref1", center
, "ref2", radius
,
164 * adg_rdim_new_full_explicit:
165 * @center_x: x coordinate of the center of the arc to quote
166 * @center_y: y coordinate of the center of the arc to quote
167 * @radius_x: x coordiante where the quote must be applied on the arc
168 * @radius_y: y coordiante where the quote must be applied on the arc
169 * @pos_x: x coordinate of the quote text
170 * @pos_y: y coordinate of the quote text
172 * Does the same job of adg_rdim_full() but using specific coordinates
173 * instead of AdgPair structures.
174 * data to get a valid quote.
176 * Returns: a newly created quote
179 adg_rdim_new_full_explicit(gdouble center_x
, gdouble center_y
,
180 gdouble radius_x
, gdouble radius_y
,
181 gdouble pos_x
, gdouble pos_y
)
183 AdgPair center
, radius
, pos
;
192 return adg_rdim_new_full(¢er
, &radius
, &pos
);
197 local_changed(AdgEntity
*entity
)
199 unset_trail((AdgRDim
*) entity
);
201 PARENT_ENTITY_CLASS
->local_changed(entity
);
205 invalidate(AdgEntity
*entity
)
208 AdgRDimPrivate
*data
;
210 rdim
= (AdgRDim
*) entity
;
213 dispose_marker(rdim
);
214 data
->geometry_arranged
= FALSE
;
217 if (PARENT_ENTITY_CLASS
->invalidate
!= NULL
)
218 PARENT_ENTITY_CLASS
->invalidate(entity
);
222 arrange(AdgEntity
*entity
)
226 AdgRDimPrivate
*data
;
228 AdgDimStyle
*dim_style
;
230 const AdgMatrix
*local
;
231 AdgPair ref1
, ref2
, base
;
234 PARENT_ENTITY_CLASS
->arrange(entity
);
236 rdim
= (AdgRDim
*) entity
;
237 dim
= (AdgDim
*) rdim
;
239 quote
= adg_dim_get_quote(dim
);
241 update_geometry(rdim
);
242 update_entities(rdim
);
244 if (data
->cpml
.path
.status
== CAIRO_STATUS_SUCCESS
) {
245 AdgEntity
*quote_entity
= (AdgEntity
*) quote
;
246 adg_entity_set_global_map(quote_entity
, &data
->quote
.global_map
);
247 adg_entity_set_local_map(quote_entity
, &data
->quote
.local_map
);
251 dim_style
= GET_DIM_STYLE(dim
);
252 outside
= adg_dim_get_outside(dim
);
253 if (outside
== ADG_THREE_STATE_UNKNOWN
)
254 outside
= ADG_THREE_STATE_OFF
;
256 local
= adg_entity_local_matrix(entity
);
257 cpml_pair_copy(&ref1
, adg_dim_get_ref1(dim
));
258 cpml_pair_copy(&ref2
, adg_dim_get_ref2(dim
));
259 cpml_pair_copy(&base
, &data
->point
.base
);
261 cpml_pair_transform(&ref1
, local
);
262 cpml_pair_transform(&ref2
, local
);
263 cpml_pair_transform(&base
, local
);
264 cpml_pair_add(&base
, &data
->shift
.base
);
267 cpml_pair_to_cairo(&base
, &data
->cpml
.data
[1]);
270 cpml_pair_to_cairo(&ref2
, &data
->cpml
.data
[3]);
273 data
->cpml
.data
[2].header
.length
= 2;
274 /* TODO: outside radius dimensions */
276 data
->cpml
.data
[2].header
.length
= 6;
279 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
282 /* Update global and local map of the quote container */
283 AdgEntity
*quote_entity
;
286 quote_entity
= (AdgEntity
*) quote
;
287 adg_matrix_copy(&matrix
, local
);
288 cairo_matrix_invert(&matrix
);
290 cpml_pair_from_cairo(&pair
, &data
->cpml
.data
[1]);
291 cairo_matrix_transform_point(&matrix
, &pair
.x
, &pair
.y
);
292 cairo_matrix_init_translate(&matrix
, pair
.x
, pair
.y
);
293 adg_entity_set_local_map(quote_entity
, &matrix
);
295 cairo_matrix_init_rotate(&matrix
, data
->angle
);
296 adg_entity_transform_global_map(quote_entity
, &matrix
,
297 ADG_TRANSFORM_BEFORE
);
299 adg_entity_get_global_map(quote_entity
, &data
->quote
.global_map
);
300 adg_entity_get_local_map(quote_entity
, &data
->quote
.local_map
);
303 if (data
->marker
!= NULL
) {
304 adg_marker_set_segment(data
->marker
, data
->trail
, outside
? 2 : 1);
305 adg_entity_local_changed((AdgEntity
*) data
->marker
);
308 /* TODO: compute the extents */
312 render(AdgEntity
*entity
, cairo_t
*cr
)
316 AdgRDimPrivate
*data
;
317 AdgDimStyle
*dim_style
;
319 const cairo_path_t
*cairo_path
;
321 rdim
= (AdgRDim
*) entity
;
322 dim
= (AdgDim
*) entity
;
324 dim_style
= GET_DIM_STYLE(dim
);
326 adg_style_apply((AdgStyle
*) dim_style
, entity
, cr
);
328 if (data
->marker
!= NULL
)
329 adg_entity_render((AdgEntity
*) data
->marker
, cr
);
331 adg_entity_render((AdgEntity
*) adg_dim_get_quote(dim
), cr
);
333 dress
= adg_dim_style_get_line_dress(dim_style
);
334 adg_entity_apply_dress(entity
, dress
, cr
);
336 cairo_path
= adg_trail_get_cairo_path(data
->trail
);
337 cairo_append_path(cr
, cairo_path
);
342 default_value(AdgDim
*dim
)
345 AdgRDimPrivate
*data
;
346 AdgDimStyle
*dim_style
;
349 rdim
= (AdgRDim
*) dim
;
351 dim_style
= GET_DIM_STYLE(dim
);
352 format
= adg_dim_style_get_number_format(dim_style
);
354 update_geometry(rdim
);
356 return g_strdup_printf(format
, data
->radius
);
360 update_geometry(AdgRDim
*rdim
)
362 AdgRDimPrivate
*data
;
364 AdgDimStyle
*dim_style
;
365 const AdgPair
*ref1
, *ref2
;
367 gdouble spacing
, level
, pos_distance
;
372 if (data
->geometry_arranged
)
375 dim
= (AdgDim
*) rdim
;
376 dim_style
= GET_DIM_STYLE(rdim
);
377 ref1
= adg_dim_get_ref1(dim
);
378 ref2
= adg_dim_get_ref2(dim
);
379 pos
= adg_dim_get_pos(dim
);
380 spacing
= adg_dim_style_get_baseline_spacing(dim_style
);
381 level
= adg_dim_get_level(dim
);
382 pos_distance
= cpml_pair_distance(pos
, ref1
);
383 cpml_pair_sub(cpml_pair_copy(&vector
, ref2
), ref1
);
384 if (cpml_pair_squared_distance(pos
, ref1
) <
385 cpml_pair_squared_distance(pos
, ref2
))
386 cpml_pair_negate(&vector
);
389 data
->radius
= cpml_pair_distance(&vector
, NULL
);
392 data
->angle
= adg_dim_quote_angle(dim
, cpml_vector_angle(&vector
));
395 cpml_pair_copy(&data
->point
.base
, &vector
);
396 cpml_vector_set_length(&data
->point
.base
, pos_distance
);
397 cpml_pair_add(&data
->point
.base
, ref1
);
400 cpml_pair_copy(&data
->shift
.base
, &vector
);
401 cpml_vector_set_length(&data
->shift
.base
, spacing
* level
);
403 data
->geometry_arranged
= TRUE
;
407 update_entities(AdgRDim
*rdim
)
409 AdgRDimPrivate
*data
;
410 AdgDimStyle
*dim_style
;
413 dim_style
= GET_DIM_STYLE(rdim
);
415 if (data
->trail
== NULL
)
416 data
->trail
= adg_trail_new(trail_callback
, rdim
);
418 if (data
->marker
== NULL
)
419 data
->marker
= adg_dim_style_marker2_new(dim_style
);
423 unset_trail(AdgRDim
*rdim
)
425 AdgRDimPrivate
*data
= rdim
->data
;
427 if (data
->trail
!= NULL
)
428 adg_model_clear((AdgModel
*) data
->trail
);
430 data
->cpml
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
434 dispose_marker(AdgRDim
*rdim
)
436 AdgRDimPrivate
*data
= rdim
->data
;
438 if (data
->trail
!= NULL
) {
439 g_object_unref(data
->trail
);
443 if (data
->marker
!= NULL
) {
444 g_object_unref(data
->marker
);
450 trail_callback(AdgTrail
*trail
, gpointer user_data
)
453 AdgRDimPrivate
*data
;
455 rdim
= (AdgRDim
*) user_data
;
458 return &data
->cpml
.path
;