[docs] Improved the ADG docs
[adg.git] / adg / adg-ldim.c
blob6d904e9d8e8a0b60c41ec0457c826f1b7aea073d
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.
21 /**
22 * SECTION:adg-ldim
23 * @short_description: Linear dimensions
25 * The #AdgLDim entity represents a linear dimension.
28 /**
29 * AdgLDim:
31 * All fields are private and should not be used directly.
32 * Use its public methods instead.
33 **/
36 #include "adg-ldim.h"
37 #include "adg-ldim-private.h"
38 #include "adg-container.h"
39 #include "adg-util.h"
40 #include "adg-intl.h"
43 enum {
44 PROP_0,
45 PROP_DIRECTION
49 static void get_property (GObject *object,
50 guint param_id,
51 GValue *value,
52 GParamSpec *pspec);
53 static void set_property (GObject *object,
54 guint param_id,
55 const GValue *value,
56 GParamSpec *pspec);
57 static void model_matrix_changed (AdgEntity *entity,
58 AdgMatrix *parent_matrix);
59 static void paper_matrix_changed (AdgEntity *entity,
60 AdgMatrix *parent_matrix);
61 static void invalidate (AdgEntity *entity);
62 static void render (AdgEntity *entity,
63 cairo_t *cr);
64 static gchar * default_quote (AdgDim *dim);
65 static void update (AdgLDim *ldim);
66 static void clear (AdgLDim *ldim);
69 G_DEFINE_TYPE(AdgLDim, adg_ldim, ADG_TYPE_DIM);
72 static void
73 adg_ldim_class_init(AdgLDimClass *klass)
75 GObjectClass *gobject_class;
76 AdgEntityClass *entity_class;
77 AdgDimClass *dim_class;
78 GParamSpec *param;
80 gobject_class = (GObjectClass *) klass;
81 entity_class = (AdgEntityClass *) klass;
82 dim_class = (AdgDimClass *) klass;
84 g_type_class_add_private(klass, sizeof(AdgLDimPrivate));
86 gobject_class->get_property = get_property;
87 gobject_class->set_property = set_property;
89 entity_class->model_matrix_changed = model_matrix_changed;
90 entity_class->paper_matrix_changed = paper_matrix_changed;
91 entity_class->invalidate = invalidate;
92 entity_class->render = render;
94 dim_class->default_quote = default_quote;
96 param = g_param_spec_double("direction",
97 P_("Direction"),
98 P_("The inclination angle of the extension lines"),
99 -G_MAXDOUBLE, G_MAXDOUBLE, ADG_DIR_RIGHT,
100 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
101 g_object_class_install_property(gobject_class, PROP_DIRECTION, param);
104 static void
105 adg_ldim_init(AdgLDim *ldim)
107 AdgLDimPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(ldim, ADG_TYPE_LDIM,
108 AdgLDimPrivate);
110 data->direction = ADG_DIR_RIGHT;
112 data->path.status = CAIRO_STATUS_INVALID_PATH_DATA;
113 data->path.data = data->path_data;
114 data->path.num_data = 12;
116 data->path_data[0].header.type = CAIRO_PATH_MOVE_TO;
117 data->path_data[0].header.length = 2;
118 data->path_data[2].header.type = CAIRO_PATH_LINE_TO;
119 data->path_data[2].header.length = 2;
120 data->path_data[4].header.type = CAIRO_PATH_MOVE_TO;
121 data->path_data[4].header.length = 2;
122 data->path_data[6].header.type = CAIRO_PATH_LINE_TO;
123 data->path_data[6].header.length = 2;
124 data->path_data[8].header.type = CAIRO_PATH_MOVE_TO;
125 data->path_data[8].header.length = 2;
126 data->path_data[10].header.type = CAIRO_PATH_LINE_TO;
127 data->path_data[10].header.length = 2;
129 data->director.data = data->director_data;
130 data->director.num_data = 4;
131 data->director.path = NULL;
133 data->director_data[0].header.type = CAIRO_PATH_MOVE_TO;
134 data->director_data[0].header.length = 2;
135 data->director_data[2].header.type = CAIRO_PATH_LINE_TO;
136 data->director_data[2].header.length = 2;
138 ldim->data = data;
141 static void
142 get_property(GObject *object,
143 guint prop_id, GValue *value, GParamSpec *pspec)
145 AdgLDimPrivate *data = ((AdgLDim *) object)->data;
147 switch (prop_id) {
148 case PROP_DIRECTION:
149 g_value_set_double(value, data->direction);
150 break;
151 default:
152 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
153 break;
157 static void
158 set_property(GObject *object,
159 guint prop_id, const GValue *value, GParamSpec *pspec)
161 AdgLDimPrivate *data = ((AdgLDim *) object)->data;
163 switch (prop_id) {
164 case PROP_DIRECTION:
165 data->direction = g_value_get_double(value);
166 break;
167 default:
168 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
169 break;
175 * adg_ldim_new:
177 * Creates a new - unreferenced - linear dimension. You must, at least, define
178 * the reference points with adg_dim_set_ref(), the dimension direction with
179 * adg_ldim_set_direction() and the position reference using adg_dim_set_pos()
180 * or, better, adg_ldim_set_pos().
182 * Return value: the new entity
184 AdgEntity *
185 adg_ldim_new(void)
187 return (AdgEntity *) g_object_new(ADG_TYPE_LDIM, NULL);
191 * adg_ldim_new_full:
192 * @ref1: the first reference point
193 * @ref2: the second reference point
194 * @direction: angle where to extend the dimension
195 * @pos: the position reference
197 * Creates a new linear dimension, specifing all the needed properties in
198 * one shot.
200 * Return value: the new entity
202 AdgEntity *
203 adg_ldim_new_full(const AdgPair *ref1, const AdgPair *ref2,
204 gdouble direction, const AdgPair *pos)
206 AdgEntity *entity = (AdgEntity *) g_object_new(ADG_TYPE_LDIM,
207 "ref1", ref1,
208 "ref2", ref2,
209 "direction", direction,
210 NULL);
211 adg_ldim_set_pos((AdgLDim *) entity, pos);
212 return entity;
216 * adg_ldim_new_full_explicit:
217 * @ref1_x: the x coordinate of the first reference point
218 * @ref1_y: the y coordinate of the first reference point
219 * @ref2_x: the x coordinate of the second reference point
220 * @ref2_y: the y coordinate of the second reference point
221 * @direction: angle where to extend the dimension
222 * @pos_x: the x coordinate of the position reference
223 * @pos_y: the y coordinate of the position reference
225 * Wrappes adg_ldim_new_full() with explicit quotes.
227 * Return value: the new entity
229 AdgEntity *
230 adg_ldim_new_full_explicit(gdouble ref1_x, gdouble ref1_y,
231 gdouble ref2_x, gdouble ref2_y,
232 gdouble direction, gdouble pos_x, gdouble pos_y)
234 AdgPair ref1;
235 AdgPair ref2;
236 AdgPair pos;
238 ref1.x = ref1_x;
239 ref1.y = ref1_y;
240 ref2.x = ref2_x;
241 ref2.y = ref2_y;
242 pos.x = pos_x;
243 pos.y = pos_y;
245 return adg_ldim_new_full(&ref1, &ref2, direction, &pos);
249 * adg_ldim_set_pos:
250 * @ldim: an #AdgLDim entity
251 * @pos: an #AdgPair structure
253 * Sets the position references (pos1 and pos2 properties) of @ldim using a
254 * single @pos point. Before this call, @ldim MUST HAVE defined the reference
255 * points and the direction. If these conditions are not met, an error message
256 * is logged and the position references will not be set.
258 void
259 adg_ldim_set_pos(AdgLDim *ldim, const AdgPair *pos)
261 AdgLDimPrivate *data;
262 const AdgPair *ref1, *ref2;
263 AdgPair pos1, pos2;
264 CpmlPair baseline_vector, extension_vector;
265 gdouble d, k;
267 g_return_if_fail(ADG_IS_LDIM(ldim));
269 data = ldim->data;
270 ref1 = adg_dim_get_ref1((AdgDim *) ldim);
271 ref2 = adg_dim_get_ref2((AdgDim *) ldim);
273 cpml_vector_from_angle(&extension_vector, data->direction, 1);
275 baseline_vector.x = -extension_vector.y;
276 baseline_vector.y = extension_vector.x;
278 d = extension_vector.y * baseline_vector.x -
279 extension_vector.x * baseline_vector.y;
280 g_return_if_fail(d != 0);
282 k = ((pos->y - ref1->y) * baseline_vector.x -
283 (pos->x - ref1->x) * baseline_vector.y) / d;
284 pos1.x = ref1->x + k * extension_vector.x;
285 pos1.y = ref1->y + k * extension_vector.y;
287 k = ((pos->y - ref2->y) * baseline_vector.x -
288 (pos->x - ref2->x) * baseline_vector.y) / d;
289 pos2.x = ref2->x + k * extension_vector.x;
290 pos2.y = ref2->y + k * extension_vector.y;
292 adg_dim_set_pos((AdgDim *) ldim, &pos1, &pos2);
296 * adg_ldim_set_pos_explicit:
297 * @ldim: an #AdgLDim entity
298 * @pos_x: the new x coordinate position reference
299 * @pos_y: the new y coordinate position reference
301 * Wrappers adg_ldim_set_pos() with explicit coordinates.
303 void
304 adg_ldim_set_pos_explicit(AdgLDim *ldim, gdouble pos_x, gdouble pos_y)
306 AdgPair pos;
308 pos.x = pos_x;
309 pos.y = pos_y;
311 adg_ldim_set_pos(ldim, &pos);
315 * adg_ldim_get_direction:
316 * @ldim: an #AdgLDim entity
318 * Gets the direction where @ldim will extend.
320 * Return value: the direction angle in radians
322 gdouble
323 adg_ldim_get_direction(AdgLDim *ldim)
325 AdgLDimPrivate *data;
327 g_return_val_if_fail(ADG_IS_LDIM(ldim), 0);
329 data = ldim->data;
331 return data->direction;
335 * adg_ldim_set_direction:
336 * @ldim: an #AdgLDim entity
337 * @direction: an angle value, in radians
339 * Sets the direction angle where to extend @ldim.
341 void
342 adg_ldim_set_direction(AdgLDim *ldim, gdouble direction)
344 AdgLDimPrivate *data;
346 g_return_if_fail(ADG_IS_LDIM(ldim));
348 data = ldim->data;
349 data->direction = direction;
351 g_object_notify((GObject *) ldim, "direction");
355 static void
356 model_matrix_changed(AdgEntity *entity, AdgMatrix *parent_matrix)
358 AdgEntityClass *entity_class = (AdgEntityClass *) adg_ldim_parent_class;
360 clear((AdgLDim *) entity);
362 if (entity_class->model_matrix_changed != NULL)
363 entity_class->model_matrix_changed(entity, parent_matrix);
366 static void
367 paper_matrix_changed(AdgEntity *entity, AdgMatrix *parent_matrix)
369 AdgEntityClass *entity_class = (AdgEntityClass *) adg_ldim_parent_class;
371 clear((AdgLDim *) entity);
373 if (entity_class->paper_matrix_changed != NULL)
374 entity_class->paper_matrix_changed(entity, parent_matrix);
377 static void
378 invalidate(AdgEntity *entity)
380 AdgEntityClass *entity_class = (AdgEntityClass *) adg_ldim_parent_class;
382 clear((AdgLDim *) entity);
384 if (entity_class->invalidate != NULL)
385 entity_class->invalidate(entity);
388 static void
389 render(AdgEntity *entity, cairo_t *cr)
391 AdgLDim *ldim;
392 AdgLDimPrivate *data;
393 AdgEntityClass *entity_class;
394 AdgStyle *dim_style;
395 AdgStyle *line_style;
396 AdgStyle *arrow_style;
398 ldim = (AdgLDim *) entity;
399 data = ldim->data;
400 entity_class = (AdgEntityClass *) adg_ldim_parent_class;
401 dim_style = adg_entity_get_style(entity, ADG_SLOT_DIM_STYLE);
402 line_style = adg_dim_style_get_line_style((AdgDimStyle *) dim_style);
403 arrow_style = adg_dim_style_get_arrow_style((AdgDimStyle *) dim_style);
405 update(ldim);
407 cairo_save(cr);
408 adg_entity_apply(entity, ADG_SLOT_DIM_STYLE, cr);
410 adg_arrow_style_render((AdgArrowStyle *) arrow_style, cr, &data->director);
411 cpml_segment_reverse(&data->director);
412 adg_arrow_style_render((AdgArrowStyle *) arrow_style, cr, &data->director);
413 cpml_segment_reverse(&data->director);
415 adg_style_apply(line_style, cr);
417 cairo_append_path(cr, &data->path);
418 cairo_stroke(cr);
420 adg_dim_render_quote((AdgDim *) ldim, cr);
422 cairo_restore(cr);
424 if (entity_class->render != NULL)
425 entity_class->render(entity, cr);
428 static gchar *
429 default_quote(AdgDim *dim)
431 const AdgPair *pos1, *pos2;
432 AdgStyle *dim_style;
433 gdouble distance;
434 const gchar *format;
436 pos1 = adg_dim_get_pos1(dim);
437 pos2 = adg_dim_get_pos2(dim);
438 distance = cpml_pair_distance(pos1, pos2);
439 dim_style = adg_entity_get_style((AdgEntity *) dim, ADG_SLOT_DIM_STYLE);
440 format = adg_dim_style_get_number_format((AdgDimStyle *) dim_style);
442 return g_strdup_printf(format, distance);
445 static void
446 update(AdgLDim *ldim)
448 AdgLDimPrivate *data;
449 AdgStyle *dim_style;
450 AdgStyle *arrow_style;
451 const AdgPair *ref1, *ref2, *pos1, *pos2;
452 gdouble level;
453 gdouble baseline_spacing, from_offset;
454 cairo_path_data_t *baseline1, *baseline2;
455 cairo_path_data_t *from1, *to1, *from2, *to2;
456 cairo_path_data_t *arrow1, *arrow2;
457 AdgMatrix paper2model;
458 CpmlPair vector;
459 AdgPair offset;
460 gdouble angle;
462 data = ldim->data;
463 if (data->path.status == CAIRO_STATUS_SUCCESS)
464 return;
466 dim_style = adg_entity_get_style((AdgEntity *) ldim, ADG_SLOT_DIM_STYLE);
467 arrow_style = adg_dim_style_get_arrow_style((AdgDimStyle *) dim_style);
469 ref1 = adg_dim_get_ref1((AdgDim *) ldim);
470 ref2 = adg_dim_get_ref2((AdgDim *) ldim);
471 pos1 = adg_dim_get_pos1((AdgDim *) ldim);
472 pos2 = adg_dim_get_pos2((AdgDim *) ldim);
473 level = adg_dim_get_level((AdgDim *) ldim);
475 baseline_spacing = adg_dim_style_get_baseline_spacing((AdgDimStyle *) dim_style);
476 from_offset = adg_dim_style_get_from_offset((AdgDimStyle *) dim_style);
478 baseline1 = data->path_data + 1;
479 baseline2 = data->path_data + 3;
480 from1 = data->path_data + 5;
481 to1 = data->path_data + 7;
482 from2 = data->path_data + 9;
483 to2 = data->path_data + 11;
484 arrow1 = data->director_data + 1;
485 arrow2 = data->director_data + 3;
487 /* Get the inverted transformation matrix */
488 if (!adg_entity_build_paper2model((AdgEntity *) ldim, &paper2model))
489 return;
491 /* Set vector to the director of the extension lines */
492 cpml_vector_from_angle(&vector, data->direction, 1);
494 /* Calculate from1 and from2 */
495 offset.x = vector.x * from_offset;
496 offset.y = vector.y * from_offset;
497 cairo_matrix_transform_distance(&paper2model, &offset.x, &offset.y);
499 from1->point.x = ref1->x + offset.x;
500 from1->point.y = ref1->y + offset.y;
501 from2->point.x = ref2->x + offset.x;
502 from2->point.y = ref2->y + offset.y;
504 /* Calculate arrow1 and arrow2 */
505 offset.x = vector.x * baseline_spacing * level;
506 offset.y = vector.y * baseline_spacing * level;
507 cairo_matrix_transform_distance(&paper2model, &offset.x, &offset.y);
509 arrow1->point.x = pos1->x + offset.x;
510 arrow1->point.y = pos1->y + offset.y;
511 arrow2->point.x = pos2->x + offset.x;
512 arrow2->point.y = pos2->y + offset.y;
514 /* Calculate to1 and to2 */
515 offset.x = vector.x * adg_dim_style_get_to_offset((AdgDimStyle *) dim_style);
516 offset.y = vector.y * adg_dim_style_get_to_offset((AdgDimStyle *) dim_style);
517 cairo_matrix_transform_distance(&paper2model, &offset.x, &offset.y);
519 to1->point.x = arrow1->point.x + offset.x;
520 to1->point.y = arrow1->point.y + offset.y;
521 to2->point.x = arrow2->point.x + offset.x;
522 to2->point.y = arrow2->point.y + offset.y;
524 /* Set vector to the director of the baseline */
525 offset.x = arrow2->point.x - arrow1->point.x;
526 offset.y = arrow2->point.y - arrow1->point.y;
527 cpml_vector_set_length(cpml_pair_copy(&vector, &offset), 1);
529 /* Update the AdgDim cache contents */
530 adg_dim_set_org_explicit((AdgDim *) ldim,
531 (arrow1->point.x + arrow2->point.x) / 2.,
532 (arrow1->point.y + arrow2->point.y) / 2.);
533 angle = cpml_vector_angle(&vector);
534 adg_dim_set_angle((AdgDim *) ldim, angle);
536 /* Calculate baseline1 and baseline2 */
537 offset.y = adg_arrow_style_get_margin((AdgArrowStyle *) arrow_style);
538 offset.x = vector.x * offset.y;
539 offset.y *= vector.y;
540 cairo_matrix_transform_distance(&paper2model, &offset.x, &offset.y);
542 baseline1->point.x = arrow1->point.x + offset.x;
543 baseline1->point.y = arrow1->point.y + offset.y;
544 baseline2->point.x = arrow2->point.x - offset.x;
545 baseline2->point.y = arrow2->point.y - offset.y;
547 data->path.status = CAIRO_STATUS_SUCCESS;
550 static void
551 clear(AdgLDim *ldim)
553 AdgLDimPrivate *data = ldim->data;
555 data->path.status = CAIRO_STATUS_INVALID_PATH_DATA;