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-container.h"
49 static void get_property (GObject
*object
,
53 static void set_property (GObject
*object
,
57 static gboolean
invalidate (AdgEntity
*entity
);
58 static gboolean
render (AdgEntity
*entity
,
60 static gchar
* default_quote (AdgDim
*dim
);
61 static void update (AdgLDim
*ldim
);
62 static void clear (AdgLDim
*ldim
);
65 G_DEFINE_TYPE(AdgLDim
, adg_ldim
, ADG_TYPE_DIM
);
69 adg_ldim_class_init(AdgLDimClass
*klass
)
71 GObjectClass
*gobject_class
;
72 AdgEntityClass
*entity_class
;
73 AdgDimClass
*dim_class
;
76 gobject_class
= (GObjectClass
*) klass
;
77 entity_class
= (AdgEntityClass
*) klass
;
78 dim_class
= (AdgDimClass
*) klass
;
80 g_type_class_add_private(klass
, sizeof(AdgLDimPrivate
));
82 gobject_class
->get_property
= get_property
;
83 gobject_class
->set_property
= set_property
;
85 entity_class
->invalidate
= invalidate
;
86 entity_class
->render
= render
;
88 dim_class
->default_quote
= default_quote
;
90 param
= g_param_spec_double("direction",
92 P_("The inclination angle of the extension lines"),
93 -G_MAXDOUBLE
, G_MAXDOUBLE
, ADG_DIR_RIGHT
,
94 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
);
95 g_object_class_install_property(gobject_class
, PROP_DIRECTION
, param
);
99 adg_ldim_init(AdgLDim
*ldim
)
101 AdgLDimPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(ldim
, ADG_TYPE_LDIM
,
104 data
->direction
= ADG_DIR_RIGHT
;
106 data
->path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
107 data
->path
.data
= data
->path_data
;
108 data
->path
.num_data
= 12;
110 data
->path_data
[0].header
.type
= CAIRO_PATH_MOVE_TO
;
111 data
->path_data
[0].header
.length
= 2;
112 data
->path_data
[2].header
.type
= CAIRO_PATH_LINE_TO
;
113 data
->path_data
[2].header
.length
= 2;
114 data
->path_data
[4].header
.type
= CAIRO_PATH_MOVE_TO
;
115 data
->path_data
[4].header
.length
= 2;
116 data
->path_data
[6].header
.type
= CAIRO_PATH_LINE_TO
;
117 data
->path_data
[6].header
.length
= 2;
118 data
->path_data
[8].header
.type
= CAIRO_PATH_MOVE_TO
;
119 data
->path_data
[8].header
.length
= 2;
120 data
->path_data
[10].header
.type
= CAIRO_PATH_LINE_TO
;
121 data
->path_data
[10].header
.length
= 2;
123 data
->director
.data
= data
->director_data
;
124 data
->director
.num_data
= 4;
125 data
->director
.path
= NULL
;
127 data
->director_data
[0].header
.type
= CAIRO_PATH_MOVE_TO
;
128 data
->director_data
[0].header
.length
= 2;
129 data
->director_data
[2].header
.type
= CAIRO_PATH_LINE_TO
;
130 data
->director_data
[2].header
.length
= 2;
136 get_property(GObject
*object
,
137 guint prop_id
, GValue
*value
, GParamSpec
*pspec
)
139 AdgLDimPrivate
*data
= ((AdgLDim
*) object
)->data
;
143 g_value_set_double(value
, data
->direction
);
146 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
152 set_property(GObject
*object
,
153 guint prop_id
, const GValue
*value
, GParamSpec
*pspec
)
155 AdgLDimPrivate
*data
= ((AdgLDim
*) object
)->data
;
159 data
->direction
= g_value_get_double(value
);
162 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
171 * Creates a new - unreferenced - linear dimension. You must, at least, define
172 * the reference points with adg_dim_set_ref(), the dimension direction with
173 * adg_ldim_set_direction() and the position reference using adg_dim_set_pos()
174 * or, better, adg_ldim_set_pos().
176 * Return value: the new entity
181 return (AdgEntity
*) g_object_new(ADG_TYPE_LDIM
, NULL
);
186 * @ref1: the first reference point
187 * @ref2: the second reference point
188 * @direction: angle where to extend the dimension
189 * @pos: the position reference
191 * Creates a new linear dimension, specifing all the needed properties in
194 * Return value: the new entity
197 adg_ldim_new_full(const AdgPair
*ref1
, const AdgPair
*ref2
,
198 gdouble direction
, const AdgPair
*pos
)
200 AdgEntity
*entity
= (AdgEntity
*) g_object_new(ADG_TYPE_LDIM
,
203 "direction", direction
,
205 adg_ldim_set_pos((AdgLDim
*) entity
, pos
);
210 * adg_ldim_new_full_explicit:
211 * @ref1_x: the x coordinate of the first reference point
212 * @ref1_y: the y coordinate of the first reference point
213 * @ref2_x: the x coordinate of the second reference point
214 * @ref2_y: the y coordinate of the second reference point
215 * @direction: angle where to extend the dimension
216 * @pos_x: the x coordinate of the position reference
217 * @pos_y: the y coordinate of the position reference
219 * Wrappes adg_ldim_new_full() with explicit quotes.
221 * Return value: the new entity
224 adg_ldim_new_full_explicit(gdouble ref1_x
, gdouble ref1_y
,
225 gdouble ref2_x
, gdouble ref2_y
,
226 gdouble direction
, gdouble pos_x
, gdouble pos_y
)
239 return adg_ldim_new_full(&ref1
, &ref2
, direction
, &pos
);
244 * @ldim: an #AdgLDim entity
245 * @pos: an #AdgPair structure
247 * Sets the position references (pos1 and pos2 properties) of @ldim using a
248 * single @pos point. Before this call, @ldim MUST HAVE defined the reference
249 * points and the direction. If these conditions are not met, an error message
250 * is logged and the position references will not be set.
253 adg_ldim_set_pos(AdgLDim
*ldim
, const AdgPair
*pos
)
255 AdgLDimPrivate
*data
;
256 const AdgPair
*ref1
, *ref2
;
258 CpmlPair baseline_vector
, extension_vector
;
261 g_return_if_fail(ADG_IS_LDIM(ldim
));
264 ref1
= adg_dim_get_ref1((AdgDim
*) ldim
);
265 ref2
= adg_dim_get_ref2((AdgDim
*) ldim
);
267 cpml_vector_from_angle(&extension_vector
, data
->direction
, 1);
269 baseline_vector
.x
= -extension_vector
.y
;
270 baseline_vector
.y
= extension_vector
.x
;
272 d
= extension_vector
.y
* baseline_vector
.x
-
273 extension_vector
.x
* baseline_vector
.y
;
274 g_return_if_fail(d
!= 0);
276 k
= ((pos
->y
- ref1
->y
) * baseline_vector
.x
-
277 (pos
->x
- ref1
->x
) * baseline_vector
.y
) / d
;
278 pos1
.x
= ref1
->x
+ k
* extension_vector
.x
;
279 pos1
.y
= ref1
->y
+ k
* extension_vector
.y
;
281 k
= ((pos
->y
- ref2
->y
) * baseline_vector
.x
-
282 (pos
->x
- ref2
->x
) * baseline_vector
.y
) / d
;
283 pos2
.x
= ref2
->x
+ k
* extension_vector
.x
;
284 pos2
.y
= ref2
->y
+ k
* extension_vector
.y
;
286 adg_dim_set_pos((AdgDim
*) ldim
, &pos1
, &pos2
);
290 * adg_ldim_set_pos_explicit:
291 * @ldim: an #AdgLDim entity
292 * @pos_x: the new x coordinate position reference
293 * @pos_y: the new y coordinate position reference
295 * Wrappers adg_ldim_set_pos() with explicit coordinates.
298 adg_ldim_set_pos_explicit(AdgLDim
*ldim
, gdouble pos_x
, gdouble pos_y
)
305 adg_ldim_set_pos(ldim
, &pos
);
309 * adg_ldim_get_direction:
310 * @ldim: an #AdgLDim entity
312 * Gets the direction where @ldim will extend.
314 * Return value: the direction angle in radians
317 adg_ldim_get_direction(AdgLDim
*ldim
)
319 AdgLDimPrivate
*data
;
321 g_return_val_if_fail(ADG_IS_LDIM(ldim
), 0);
325 return data
->direction
;
329 * adg_ldim_set_direction:
330 * @ldim: an #AdgLDim entity
331 * @direction: an angle value, in radians
333 * Sets the direction angle where to extend @ldim.
336 adg_ldim_set_direction(AdgLDim
*ldim
, gdouble direction
)
338 AdgLDimPrivate
*data
;
340 g_return_if_fail(ADG_IS_LDIM(ldim
));
343 data
->direction
= direction
;
345 g_object_notify((GObject
*) ldim
, "direction");
350 invalidate(AdgEntity
*entity
)
352 clear((AdgLDim
*) entity
);
353 return ((AdgEntityClass
*) adg_ldim_parent_class
)->invalidate(entity
);
357 render(AdgEntity
*entity
, cairo_t
*cr
)
360 AdgLDimPrivate
*data
;
362 AdgStyle
*arrow_style
;
363 AdgStyle
*line_style
;
366 ldim
= (AdgLDim
*) entity
;
368 dim_style
= adg_entity_get_style(entity
, ADG_SLOT_DIM_STYLE
);
369 arrow_style
= adg_dim_style_get_arrow_style((AdgDimStyle
*) dim_style
);
370 line_style
= adg_dim_style_get_line_style((AdgDimStyle
*) dim_style
);
371 adg_entity_get_local_matrix(entity
, &local
);
374 adg_entity_apply(entity
, ADG_SLOT_DIM_STYLE
, cr
);
377 cairo_transform(cr
, &local
);
378 adg_arrow_style_render((AdgArrowStyle
*) arrow_style
, cr
, &data
->director
);
379 cpml_segment_reverse(&data
->director
);
380 adg_arrow_style_render((AdgArrowStyle
*) arrow_style
, cr
, &data
->director
);
381 cpml_segment_reverse(&data
->director
);
382 cairo_append_path(cr
, &data
->path
);
385 adg_style_apply(line_style
, cr
);
388 adg_dim_render_quote((AdgDim
*) ldim
, cr
);
394 default_quote(AdgDim
*dim
)
396 const AdgPair
*pos1
, *pos2
;
401 pos1
= adg_dim_get_pos1(dim
);
402 pos2
= adg_dim_get_pos2(dim
);
403 distance
= cpml_pair_distance(pos1
, pos2
);
404 dim_style
= adg_entity_get_style((AdgEntity
*) dim
, ADG_SLOT_DIM_STYLE
);
405 format
= adg_dim_style_get_number_format((AdgDimStyle
*) dim_style
);
407 return g_strdup_printf(format
, distance
);
411 update(AdgLDim
*ldim
)
413 AdgLDimPrivate
*data
;
415 AdgStyle
*arrow_style
;
416 const AdgPair
*ref1
, *ref2
, *pos1
, *pos2
;
418 gdouble baseline_spacing
, from_offset
;
419 cairo_path_data_t
*baseline1
, *baseline2
;
420 cairo_path_data_t
*from1
, *to1
, *from2
, *to2
;
421 cairo_path_data_t
*arrow1
, *arrow2
;
428 dim_style
= adg_entity_get_style((AdgEntity
*) ldim
, ADG_SLOT_DIM_STYLE
);
429 arrow_style
= adg_dim_style_get_arrow_style((AdgDimStyle
*) dim_style
);
431 ref1
= adg_dim_get_ref1((AdgDim
*) ldim
);
432 ref2
= adg_dim_get_ref2((AdgDim
*) ldim
);
433 pos1
= adg_dim_get_pos1((AdgDim
*) ldim
);
434 pos2
= adg_dim_get_pos2((AdgDim
*) ldim
);
435 level
= adg_dim_get_level((AdgDim
*) ldim
);
437 baseline_spacing
= adg_dim_style_get_baseline_spacing((AdgDimStyle
*) dim_style
);
438 from_offset
= adg_dim_style_get_from_offset((AdgDimStyle
*) dim_style
);
440 baseline1
= data
->path_data
+ 1;
441 baseline2
= data
->path_data
+ 3;
442 from1
= data
->path_data
+ 5;
443 to1
= data
->path_data
+ 7;
444 from2
= data
->path_data
+ 9;
445 to2
= data
->path_data
+ 11;
446 arrow1
= data
->director_data
+ 1;
447 arrow2
= data
->director_data
+ 3;
449 /* Get the inverted local matrix */
450 adg_entity_get_local_matrix((AdgEntity
*) ldim
, &unlocal
);
451 if (cairo_matrix_invert(&unlocal
) != CAIRO_STATUS_SUCCESS
)
454 /* Set vector to the director of the extension lines */
455 cpml_vector_from_angle(&vector
, data
->direction
, 1);
457 /* Calculate from1 and from2 */
458 offset
.x
= vector
.x
* from_offset
;
459 offset
.y
= vector
.y
* from_offset
;
460 cairo_matrix_transform_distance(&unlocal
, &offset
.x
, &offset
.y
);
462 from1
->point
.x
= ref1
->x
+ offset
.x
;
463 from1
->point
.y
= ref1
->y
+ offset
.y
;
464 from2
->point
.x
= ref2
->x
+ offset
.x
;
465 from2
->point
.y
= ref2
->y
+ offset
.y
;
467 /* Calculate arrow1 and arrow2 */
468 offset
.x
= vector
.x
* baseline_spacing
* level
;
469 offset
.y
= vector
.y
* baseline_spacing
* level
;
470 cairo_matrix_transform_distance(&unlocal
, &offset
.x
, &offset
.y
);
472 arrow1
->point
.x
= pos1
->x
+ offset
.x
;
473 arrow1
->point
.y
= pos1
->y
+ offset
.y
;
474 arrow2
->point
.x
= pos2
->x
+ offset
.x
;
475 arrow2
->point
.y
= pos2
->y
+ offset
.y
;
477 /* Calculate to1 and to2 */
478 offset
.x
= vector
.x
* adg_dim_style_get_to_offset((AdgDimStyle
*) dim_style
);
479 offset
.y
= vector
.y
* adg_dim_style_get_to_offset((AdgDimStyle
*) dim_style
);
480 cairo_matrix_transform_distance(&unlocal
, &offset
.x
, &offset
.y
);
482 to1
->point
.x
= arrow1
->point
.x
+ offset
.x
;
483 to1
->point
.y
= arrow1
->point
.y
+ offset
.y
;
484 to2
->point
.x
= arrow2
->point
.x
+ offset
.x
;
485 to2
->point
.y
= arrow2
->point
.y
+ offset
.y
;
487 /* Set vector to the director of the baseline */
488 offset
.x
= arrow2
->point
.x
- arrow1
->point
.x
;
489 offset
.y
= arrow2
->point
.y
- arrow1
->point
.y
;
490 cpml_vector_set_length(cpml_pair_copy(&vector
, &offset
), 1);
492 /* Update the AdgDim cache contents */
493 adg_dim_set_org_explicit((AdgDim
*) ldim
,
494 (arrow1
->point
.x
+ arrow2
->point
.x
) / 2.,
495 (arrow1
->point
.y
+ arrow2
->point
.y
) / 2.);
496 angle
= cpml_vector_angle(&vector
);
497 adg_dim_set_angle((AdgDim
*) ldim
, angle
);
499 /* Calculate baseline1 and baseline2 */
500 offset
.y
= adg_arrow_style_get_margin((AdgArrowStyle
*) arrow_style
);
501 offset
.x
= vector
.x
* offset
.y
;
502 offset
.y
*= vector
.y
;
503 cairo_matrix_transform_distance(&unlocal
, &offset
.x
, &offset
.y
);
505 baseline1
->point
.x
= arrow1
->point
.x
+ offset
.x
;
506 baseline1
->point
.y
= arrow1
->point
.y
+ offset
.y
;
507 baseline2
->point
.x
= arrow2
->point
.x
- offset
.x
;
508 baseline2
->point
.y
= arrow2
->point
.y
- offset
.y
;
510 data
->path
.status
= CAIRO_STATUS_SUCCESS
;
516 AdgLDimPrivate
*data
= ldim
->data
;
518 data
->path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;