[docs] NEWS updated
[adg.git] / adg / adg-ldim.c
blob625e473494cc43697b1d7f313f9c822cdca0ee51
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.
20 /**
21 * SECTION:ldim
22 * @title: AdgLDim
23 * @short_description: Linear dimensions
25 * The #AdgLDim entity represents a linear dimension.
28 #include "adg-ldim.h"
29 #include "adg-ldim-private.h"
30 #include "adg-container.h"
31 #include "adg-util.h"
32 #include "adg-intl.h"
34 #define PARENT_CLASS ((AdgDimClass *) adg_ldim_parent_class)
35 #define ENTITY_CLASS ((AdgEntityClass *) adg_ldim_parent_class)
38 enum {
39 PROP_0,
40 PROP_DIRECTION
44 static void get_property (GObject *object,
45 guint param_id,
46 GValue *value,
47 GParamSpec *pspec);
48 static void set_property (GObject *object,
49 guint param_id,
50 const GValue *value,
51 GParamSpec *pspec);
52 static void model_matrix_changed (AdgEntity *entity,
53 AdgMatrix *parent_matrix);
54 static void paper_matrix_changed (AdgEntity *entity,
55 AdgMatrix *parent_matrix);
56 static void invalidate (AdgEntity *entity);
57 static void render (AdgEntity *entity,
58 cairo_t *cr);
59 static gchar * default_quote (AdgDim *dim);
60 static void update (AdgLDim *ldim);
61 static void clear (AdgLDim *ldim);
64 G_DEFINE_TYPE(AdgLDim, adg_ldim, ADG_TYPE_DIM);
67 static void
68 adg_ldim_class_init(AdgLDimClass *klass)
70 GObjectClass *gobject_class;
71 AdgEntityClass *entity_class;
72 AdgDimClass *dim_class;
73 GParamSpec *param;
75 gobject_class = (GObjectClass *) klass;
76 entity_class = (AdgEntityClass *) klass;
77 dim_class = (AdgDimClass *) klass;
79 g_type_class_add_private(klass, sizeof(AdgLDimPrivate));
81 gobject_class->get_property = get_property;
82 gobject_class->set_property = set_property;
84 entity_class->model_matrix_changed = model_matrix_changed;
85 entity_class->paper_matrix_changed = paper_matrix_changed;
86 entity_class->invalidate = invalidate;
87 entity_class->render = render;
89 dim_class->default_quote = default_quote;
91 param = g_param_spec_double("direction",
92 P_("Direction"),
93 P_("The inclination angle of the extension lines"),
94 -G_MAXDOUBLE, G_MAXDOUBLE, ADG_DIR_RIGHT,
95 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
96 g_object_class_install_property(gobject_class, PROP_DIRECTION, param);
99 static void
100 adg_ldim_init(AdgLDim *ldim)
102 AdgLDimPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE(ldim, ADG_TYPE_LDIM,
103 AdgLDimPrivate);
105 priv->direction = ADG_DIR_RIGHT;
107 priv->path.status = CAIRO_STATUS_INVALID_PATH_DATA;
108 priv->path.data = priv->path_data;
109 priv->path.num_data = 12;
111 priv->path_data[0].header.type = CAIRO_PATH_MOVE_TO;
112 priv->path_data[0].header.length = 2;
113 priv->path_data[2].header.type = CAIRO_PATH_LINE_TO;
114 priv->path_data[2].header.length = 2;
115 priv->path_data[4].header.type = CAIRO_PATH_MOVE_TO;
116 priv->path_data[4].header.length = 2;
117 priv->path_data[6].header.type = CAIRO_PATH_LINE_TO;
118 priv->path_data[6].header.length = 2;
119 priv->path_data[8].header.type = CAIRO_PATH_MOVE_TO;
120 priv->path_data[8].header.length = 2;
121 priv->path_data[10].header.type = CAIRO_PATH_LINE_TO;
122 priv->path_data[10].header.length = 2;
124 priv->director.data = priv->director_data;
125 priv->director.num_data = 4;
126 priv->director.cairo_path = NULL;
128 priv->director_data[0].header.type = CAIRO_PATH_MOVE_TO;
129 priv->director_data[0].header.length = 2;
130 priv->director_data[2].header.type = CAIRO_PATH_LINE_TO;
131 priv->director_data[2].header.length = 2;
133 ldim->priv = priv;
136 static void
137 get_property(GObject *object,
138 guint prop_id, GValue *value, GParamSpec *pspec)
140 AdgLDim *ldim = ADG_LDIM(object);
142 switch (prop_id) {
143 case PROP_DIRECTION:
144 g_value_set_double(value, ldim->priv->direction);
145 break;
146 default:
147 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
148 break;
152 static void
153 set_property(GObject *object,
154 guint prop_id, const GValue *value, GParamSpec *pspec)
156 AdgLDim *ldim = ADG_LDIM(object);
158 switch (prop_id) {
159 case PROP_DIRECTION:
160 ldim->priv->direction = g_value_get_double(value);
161 break;
162 default:
163 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
164 break;
170 * adg_ldim_new:
172 * Creates a new - unreferenced - linear dimension. You must, at least, define
173 * the reference points with adg_dim_set_ref(), the dimension direction with
174 * adg_ldim_set_direction() and the position reference using adg_dim_set_pos()
175 * or, better, adg_ldim_set_pos().
177 * Return value: the new entity
179 AdgEntity *
180 adg_ldim_new(void)
182 return (AdgEntity *) g_object_new(ADG_TYPE_LDIM, NULL);
186 * adg_ldim_new_full:
187 * @ref1: the first reference point
188 * @ref2: the second reference point
189 * @direction: angle where to extend the dimension
190 * @pos: the position reference
192 * Creates a new linear dimension, specifing all the needed properties in
193 * one shot.
195 * Return value: the new entity
197 AdgEntity *
198 adg_ldim_new_full(const AdgPair *ref1, const AdgPair *ref2,
199 gdouble direction, const AdgPair *pos)
201 AdgEntity *entity = (AdgEntity *) g_object_new(ADG_TYPE_LDIM,
202 "ref1", ref1,
203 "ref2", ref2,
204 "direction", direction,
205 NULL);
206 adg_ldim_set_pos((AdgLDim *) entity, pos);
207 return entity;
211 * adg_ldim_new_full_explicit:
212 * @ref1_x: the x coordinate of the first reference point
213 * @ref1_y: the y coordinate of the first reference point
214 * @ref2_x: the x coordinate of the second reference point
215 * @ref2_y: the y coordinate of the second reference point
216 * @direction: angle where to extend the dimension
217 * @pos_x: the x coordinate of the position reference
218 * @pos_y: the y coordinate of the position reference
220 * Wrappes adg_ldim_new_full() with explicit quotes.
222 * Return value: the new entity
224 AdgEntity *
225 adg_ldim_new_full_explicit(gdouble ref1_x, gdouble ref1_y,
226 gdouble ref2_x, gdouble ref2_y,
227 gdouble direction, gdouble pos_x, gdouble pos_y)
229 AdgPair ref1;
230 AdgPair ref2;
231 AdgPair pos;
233 ref1.x = ref1_x;
234 ref1.y = ref1_y;
235 ref2.x = ref2_x;
236 ref2.y = ref2_y;
237 pos.x = pos_x;
238 pos.y = pos_y;
240 return adg_ldim_new_full(&ref1, &ref2, direction, &pos);
244 * adg_ldim_set_pos:
245 * @ldim: an #AdgLDim entity
246 * @pos: an #AdgPair structure
248 * Sets the position references (pos1 and pos2 properties) of @ldim using a
249 * single @pos point. Before this call, @ldim MUST HAVE defined the reference
250 * points and the direction. If these conditions are not met, an error message
251 * is logged and the position references will not be set.
253 void
254 adg_ldim_set_pos(AdgLDim *ldim, const AdgPair *pos)
256 const AdgPair *ref1, *ref2;
257 AdgPair pos1, pos2;
258 CpmlPair baseline_vector, extension_vector;
259 gdouble d, k;
261 g_return_if_fail(ADG_IS_LDIM(ldim));
263 ref1 = adg_dim_get_ref1((AdgDim *) ldim);
264 ref2 = adg_dim_get_ref2((AdgDim *) ldim);
266 cpml_vector_from_angle(&extension_vector, ldim->priv->direction, 1);
268 baseline_vector.x = -extension_vector.y;
269 baseline_vector.y = extension_vector.x;
271 d = extension_vector.y * baseline_vector.x -
272 extension_vector.x * baseline_vector.y;
273 g_return_if_fail(d != 0);
275 k = ((pos->y - ref1->y) * baseline_vector.x -
276 (pos->x - ref1->x) * baseline_vector.y) / d;
277 pos1.x = ref1->x + k * extension_vector.x;
278 pos1.y = ref1->y + k * extension_vector.y;
280 k = ((pos->y - ref2->y) * baseline_vector.x -
281 (pos->x - ref2->x) * baseline_vector.y) / d;
282 pos2.x = ref2->x + k * extension_vector.x;
283 pos2.y = ref2->y + k * extension_vector.y;
285 adg_dim_set_pos((AdgDim *) ldim, &pos1, &pos2);
289 * adg_ldim_set_pos_explicit:
290 * @ldim: an #AdgLDim entity
291 * @pos_x: the new x coordinate position reference
292 * @pos_y: the new y coordinate position reference
294 * Wrappers adg_ldim_set_pos() with explicit coordinates.
296 void
297 adg_ldim_set_pos_explicit(AdgLDim *ldim, gdouble pos_x, gdouble pos_y)
299 AdgPair pos;
301 pos.x = pos_x;
302 pos.y = pos_y;
304 adg_ldim_set_pos(ldim, &pos);
308 * adg_ldim_get_direction:
309 * @ldim: an #AdgLDim entity
311 * Gets the direction where @ldim will extend.
313 * Return value: the direction angle in radians
315 gdouble
316 adg_ldim_get_direction(AdgLDim *ldim)
318 g_return_val_if_fail(ADG_IS_LDIM(ldim), 0);
320 return ldim->priv->direction;
324 * adg_ldim_set_direction:
325 * @ldim: an #AdgLDim entity
326 * @direction: an angle value, in radians
328 * Sets the direction angle where to extend @ldim.
330 void
331 adg_ldim_set_direction(AdgLDim *ldim, gdouble direction)
333 g_return_if_fail(ADG_IS_LDIM(ldim));
335 ldim->priv->direction = direction;
336 g_object_notify((GObject *) ldim, "direction");
340 static void
341 model_matrix_changed(AdgEntity *entity, AdgMatrix *parent_matrix)
343 clear((AdgLDim *) entity);
344 ENTITY_CLASS->model_matrix_changed(entity, parent_matrix);
347 static void
348 paper_matrix_changed(AdgEntity *entity, AdgMatrix *parent_matrix)
350 clear((AdgLDim *) entity);
351 ENTITY_CLASS->paper_matrix_changed(entity, parent_matrix);
354 static void
355 invalidate(AdgEntity *entity)
357 clear((AdgLDim *) entity);
358 ENTITY_CLASS->invalidate(entity);
361 static void
362 render(AdgEntity *entity, cairo_t *cr)
364 AdgLDim *ldim;
365 AdgLDimPrivate *priv;
366 AdgStyle *dim_style;
367 AdgStyle *line_style;
368 AdgStyle *arrow_style;
370 ldim = (AdgLDim *) entity;
371 priv = ldim->priv;
372 dim_style = adg_entity_get_style(entity, ADG_SLOT_DIM_STYLE);
373 line_style = adg_dim_style_get_line_style((AdgDimStyle *) dim_style);
374 arrow_style = adg_dim_style_get_arrow_style((AdgDimStyle *) dim_style);
376 update(ldim);
378 cairo_save(cr);
379 adg_entity_apply(entity, ADG_SLOT_DIM_STYLE, cr);
381 adg_arrow_style_render((AdgArrowStyle *) arrow_style, cr, &priv->director);
382 cpml_segment_reverse(&priv->director);
383 adg_arrow_style_render((AdgArrowStyle *) arrow_style, cr, &priv->director);
384 cpml_segment_reverse(&priv->director);
386 adg_style_apply(line_style, cr);
388 cairo_append_path(cr, &priv->path);
389 cairo_stroke(cr);
391 adg_dim_render_quote((AdgDim *) ldim, cr);
393 cairo_restore(cr);
395 ENTITY_CLASS->render(entity, cr);
398 static gchar *
399 default_quote(AdgDim *dim)
401 const AdgPair *pos1, *pos2;
402 AdgStyle *dim_style;
403 gdouble distance;
404 const gchar *format;
406 pos1 = adg_dim_get_pos1(dim);
407 pos2 = adg_dim_get_pos2(dim);
408 distance = cpml_pair_distance(pos1, pos2);
409 dim_style = adg_entity_get_style((AdgEntity *) dim, ADG_SLOT_DIM_STYLE);
410 format = adg_dim_style_get_number_format((AdgDimStyle *) dim_style);
412 return g_strdup_printf(format, distance);
415 static void
416 update(AdgLDim *ldim)
418 AdgLDimPrivate *priv;
419 AdgStyle *dim_style;
420 AdgStyle *arrow_style;
421 const AdgPair *ref1, *ref2, *pos1, *pos2;
422 gdouble level;
423 gdouble baseline_spacing, from_offset;
424 cairo_path_data_t *baseline1, *baseline2;
425 cairo_path_data_t *from1, *to1, *from2, *to2;
426 cairo_path_data_t *arrow1, *arrow2;
427 AdgMatrix paper2model;
428 CpmlPair vector;
429 AdgPair offset;
430 gdouble angle;
432 priv = ldim->priv;
433 if (priv->path.status == CAIRO_STATUS_SUCCESS)
434 return;
436 dim_style = adg_entity_get_style((AdgEntity *) ldim, ADG_SLOT_DIM_STYLE);
437 arrow_style = adg_dim_style_get_arrow_style((AdgDimStyle *) dim_style);
439 ref1 = adg_dim_get_ref1((AdgDim *) ldim);
440 ref2 = adg_dim_get_ref2((AdgDim *) ldim);
441 pos1 = adg_dim_get_pos1((AdgDim *) ldim);
442 pos2 = adg_dim_get_pos2((AdgDim *) ldim);
443 level = adg_dim_get_level((AdgDim *) ldim);
445 baseline_spacing = adg_dim_style_get_baseline_spacing((AdgDimStyle *) dim_style);
446 from_offset = adg_dim_style_get_from_offset((AdgDimStyle *) dim_style);
448 baseline1 = priv->path_data + 1;
449 baseline2 = priv->path_data + 3;
450 from1 = priv->path_data + 5;
451 to1 = priv->path_data + 7;
452 from2 = priv->path_data + 9;
453 to2 = priv->path_data + 11;
454 arrow1 = priv->director_data + 1;
455 arrow2 = priv->director_data + 3;
457 /* Get the inverted transformation matrix */
458 if (!adg_entity_build_paper2model((AdgEntity *) ldim, &paper2model))
459 return;
461 /* Set vector to the director of the extension lines */
462 cpml_vector_from_angle(&vector, priv->direction, 1);
464 /* Calculate from1 and from2 */
465 offset.x = vector.x * from_offset;
466 offset.y = vector.y * from_offset;
467 cairo_matrix_transform_distance(&paper2model, &offset.x, &offset.y);
469 from1->point.x = ref1->x + offset.x;
470 from1->point.y = ref1->y + offset.y;
471 from2->point.x = ref2->x + offset.x;
472 from2->point.y = ref2->y + offset.y;
474 /* Calculate arrow1 and arrow2 */
475 offset.x = vector.x * baseline_spacing * level;
476 offset.y = vector.y * baseline_spacing * level;
477 cairo_matrix_transform_distance(&paper2model, &offset.x, &offset.y);
479 arrow1->point.x = pos1->x + offset.x;
480 arrow1->point.y = pos1->y + offset.y;
481 arrow2->point.x = pos2->x + offset.x;
482 arrow2->point.y = pos2->y + offset.y;
484 /* Calculate to1 and to2 */
485 offset.x = vector.x * adg_dim_style_get_to_offset((AdgDimStyle *) dim_style);
486 offset.y = vector.y * adg_dim_style_get_to_offset((AdgDimStyle *) dim_style);
487 cairo_matrix_transform_distance(&paper2model, &offset.x, &offset.y);
489 to1->point.x = arrow1->point.x + offset.x;
490 to1->point.y = arrow1->point.y + offset.y;
491 to2->point.x = arrow2->point.x + offset.x;
492 to2->point.y = arrow2->point.y + offset.y;
494 /* Set vector to the director of the baseline */
495 offset.x = arrow2->point.x - arrow1->point.x;
496 offset.y = arrow2->point.y - arrow1->point.y;
497 cpml_vector_set_length(cpml_pair_copy(&vector, &offset), 1);
499 /* Update the AdgDim cache contents */
500 adg_dim_set_org_explicit((AdgDim *) ldim,
501 (arrow1->point.x + arrow2->point.x) / 2.,
502 (arrow1->point.y + arrow2->point.y) / 2.);
503 angle = cpml_vector_angle(&vector);
504 adg_dim_set_angle((AdgDim *) ldim, angle);
506 /* Calculate baseline1 and baseline2 */
507 offset.y = adg_arrow_style_get_margin((AdgArrowStyle *) arrow_style);
508 offset.x = vector.x * offset.y;
509 offset.y *= vector.y;
510 cairo_matrix_transform_distance(&paper2model, &offset.x, &offset.y);
512 baseline1->point.x = arrow1->point.x + offset.x;
513 baseline1->point.y = arrow1->point.y + offset.y;
514 baseline2->point.x = arrow2->point.x - offset.x;
515 baseline2->point.y = arrow2->point.y - offset.y;
517 priv->path.status = CAIRO_STATUS_SUCCESS;
520 static void
521 clear(AdgLDim *ldim)
523 ldim->priv->path.status = CAIRO_STATUS_INVALID_PATH_DATA;