adg: provide example on how to customize a style
[adg.git] / src / adg / adg-rdim.c
blob02a63555cf056495d836af2afe05ea4bbb8d337c
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2017 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-rdim
23 * @short_description: Radial dimensions
25 * The #AdgRDim entity represents a radial dimension.
27 * Since: 1.0
28 **/
30 /**
31 * AdgRDim:
33 * All fields are private and should not be used directly.
34 * Use its public methods instead.
36 * Since: 1.0
37 **/
40 #include "adg-internal.h"
41 #include "adg-container.h"
42 #include "adg-alignment.h"
43 #include "adg-model.h"
44 #include "adg-point.h"
45 #include "adg-trail.h"
46 #include "adg-marker.h"
47 #include "adg-style.h"
48 #include "adg-dim-style.h"
49 #include "adg-textual.h"
50 #include "adg-toy-text.h"
51 #include "adg-dim.h"
52 #include "adg-dim-private.h"
53 #include <math.h>
55 #include "adg-rdim.h"
56 #include "adg-rdim-private.h"
59 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_rdim_parent_class)
60 #define _ADG_OLD_ENTITY_CLASS ((AdgEntityClass *) adg_rdim_parent_class)
63 G_DEFINE_TYPE(AdgRDim, adg_rdim, ADG_TYPE_DIM)
65 enum {
66 PROP_0,
70 static void _adg_dispose (GObject *object);
71 static void _adg_global_changed (AdgEntity *entity);
72 static void _adg_local_changed (AdgEntity *entity);
73 static void _adg_invalidate (AdgEntity *entity);
74 static void _adg_arrange (AdgEntity *entity);
75 static void _adg_render (AdgEntity *entity,
76 cairo_t *cr);
77 static gchar * _adg_default_value (AdgDim *dim);
78 static gboolean _adg_update_geometry (AdgRDim *rdim);
79 static void _adg_update_entities (AdgRDim *rdim);
80 static void _adg_clear_trail (AdgRDim *rdim);
81 static void _adg_dispose_trail (AdgRDim *rdim);
82 static void _adg_dispose_marker (AdgRDim *rdim);
83 static cairo_path_t * _adg_trail_callback (AdgTrail *trail,
84 gpointer user_data);
87 static void
88 adg_rdim_class_init(AdgRDimClass *klass)
90 GObjectClass *gobject_class;
91 AdgEntityClass *entity_class;
92 AdgDimClass *dim_class;
94 gobject_class = (GObjectClass *) klass;
95 entity_class = (AdgEntityClass *) klass;
96 dim_class = (AdgDimClass *) klass;
98 g_type_class_add_private(klass, sizeof(AdgRDimPrivate));
100 gobject_class->dispose = _adg_dispose;
102 entity_class->global_changed = _adg_global_changed;
103 entity_class->local_changed = _adg_local_changed;
104 entity_class->invalidate = _adg_invalidate;
105 entity_class->arrange = _adg_arrange;
106 entity_class->render = _adg_render;
108 dim_class->default_value = _adg_default_value;
111 static void
112 adg_rdim_init(AdgRDim *rdim)
114 AdgRDimPrivate *data;
115 cairo_path_data_t move_to, line_to;
117 data = G_TYPE_INSTANCE_GET_PRIVATE(rdim, ADG_TYPE_RDIM, AdgRDimPrivate);
118 move_to.header.type = CPML_MOVE;
119 move_to.header.length = 2;
120 line_to.header.type = CPML_LINE;
121 line_to.header.length = 2;
123 data->trail = NULL;
124 data->marker = NULL;
125 data->geometry_arranged = FALSE;
126 data->radius = -1.;
127 data->shift.base.x = data->shift.base.y = 0;
128 cairo_matrix_init_identity(&data->quote.global_map);
130 data->cairo.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
131 data->cairo.path.data = data->cairo.data;
132 data->cairo.path.num_data = G_N_ELEMENTS(data->cairo.data);
133 data->cairo.path.data[0] = move_to;
134 data->cairo.path.data[2] = line_to;
135 data->cairo.path.data[4] = move_to;
136 data->cairo.path.data[6] = line_to;
138 rdim->data = data;
140 /* Override the generic default dress with a more specific one */
141 adg_dim_set_dim_dress((AdgDim *) rdim, ADG_DRESS_DIMENSION_RADIUS);
144 static void
145 _adg_dispose(GObject *object)
147 AdgRDim *rdim = (AdgRDim *) object;
149 _adg_dispose_trail(rdim);
150 _adg_dispose_marker(rdim);
152 if (_ADG_OLD_OBJECT_CLASS->dispose)
153 _ADG_OLD_OBJECT_CLASS->dispose(object);
158 * adg_rdim_new:
160 * Creates a new uninitialized radial dimension. To be useful, you
161 * need at least define the center of the arc to quote in #AdgDim:ref1,
162 * a point on the arc in #AdgDim:ref2 and the position of the quote
163 * in #AdgDim:pos using any valid #AdgDim method.
165 * Returns: a newly created quote
167 * Since: 1.0
169 AdgRDim *
170 adg_rdim_new(void)
172 return g_object_new(ADG_TYPE_RDIM, NULL);
176 * adg_rdim_new_full:
177 * @center: (allow-none): center of the arc to quote
178 * @radius: (allow-none): where the quote must be applied on the arc
179 * @pos: (allow-none): position of the quote text
181 * Creates a new quote by specifying explicitely all the needed
182 * data to get a valid quote.
184 * Returns: the newly created quote.
186 * Since: 1.0
188 AdgRDim *
189 adg_rdim_new_full(const CpmlPair *center, const CpmlPair *radius,
190 const CpmlPair *pos)
192 AdgRDim *rdim;
193 AdgDim *dim;
195 rdim = adg_rdim_new();
196 dim = (AdgDim *) rdim;
198 if (center != NULL)
199 adg_dim_set_ref1_from_pair(dim, center);
201 if (radius != NULL)
202 adg_dim_set_ref2_from_pair(dim, radius);
204 if (pos != NULL)
205 adg_dim_set_pos_from_pair(dim, pos);
207 return rdim;
211 * adg_rdim_new_full_explicit:
212 * @center_x: x coordinate of the center of the arc to quote
213 * @center_y: y coordinate of the center of the arc to quote
214 * @radius_x: x coordiante where the quote must be applied on the arc
215 * @radius_y: y coordiante where the quote must be applied on the arc
216 * @pos_x: x coordinate of the quote text
217 * @pos_y: y coordinate of the quote text
219 * Does the same job of adg_rdim_new_full() but using specific
220 * coordinates instead of #CpmlPair structures.
222 * Returns: the newly created quote.
224 * Since: 1.0
226 AdgRDim *
227 adg_rdim_new_full_explicit(gdouble center_x, gdouble center_y,
228 gdouble radius_x, gdouble radius_y,
229 gdouble pos_x, gdouble pos_y)
231 CpmlPair center, radius, pos;
233 center.x = center_x;
234 center.y = center_y;
235 radius.x = radius_x;
236 radius.y = radius_y;
237 pos.x = pos_x;
238 pos.y = pos_y;
240 return adg_rdim_new_full(&center, &radius, &pos);
244 * adg_rdim_new_full_from_model:
245 * @model: (transfer none): the model from which the named pairs are taken
246 * @center: (allow-none): the center point of the arc to quote
247 * @radius: (allow-none): an arbitrary point on the arc
248 * @pos: (allow-none): the position reference
250 * Creates a new radial dimension, specifing all the needed properties in
251 * one shot and using named pairs from @model.
253 * Returns: the newly created radial dimension entity
255 * Since: 1.0
257 AdgRDim *
258 adg_rdim_new_full_from_model(AdgModel *model, const gchar *center,
259 const gchar *radius, const gchar *pos)
261 AdgRDim *rdim;
262 AdgDim *dim;
264 g_return_val_if_fail(model != NULL, NULL);
266 rdim = adg_rdim_new();
267 dim = (AdgDim *) rdim;
269 if (center != NULL)
270 adg_dim_set_ref1_from_model(dim, model, center);
272 if (radius != NULL)
273 adg_dim_set_ref2_from_model(dim, model, radius);
275 if (pos != NULL)
276 adg_dim_set_pos_from_model(dim, model, pos);
278 return rdim;
282 static void
283 _adg_global_changed(AdgEntity *entity)
285 AdgRDimPrivate *data = ((AdgRDim *) entity)->data;
287 _adg_clear_trail((AdgRDim *) entity);
289 if (_ADG_OLD_ENTITY_CLASS->global_changed)
290 _ADG_OLD_ENTITY_CLASS->global_changed(entity);
292 if (data->marker != NULL)
293 adg_entity_global_changed((AdgEntity *) data->marker);
296 static void
297 _adg_local_changed(AdgEntity *entity)
299 _adg_clear_trail((AdgRDim *) entity);
301 if (_ADG_OLD_ENTITY_CLASS->local_changed)
302 _ADG_OLD_ENTITY_CLASS->local_changed(entity);
305 static void
306 _adg_invalidate(AdgEntity *entity)
308 AdgRDim *rdim;
309 AdgRDimPrivate *data;
311 rdim = (AdgRDim *) entity;
312 data = rdim->data;
314 _adg_dispose_trail(rdim);
315 _adg_dispose_marker(rdim);
316 data->geometry_arranged = FALSE;
317 _adg_clear_trail(rdim);
319 if (_ADG_OLD_ENTITY_CLASS->invalidate)
320 _ADG_OLD_ENTITY_CLASS->invalidate(entity);
323 static void
324 _adg_arrange(AdgEntity *entity)
326 AdgRDim *rdim;
327 AdgDim *dim;
328 AdgRDimPrivate *data;
329 AdgAlignment *quote;
330 AdgEntity *quote_entity;
331 gboolean outside;
332 const cairo_matrix_t *global, *local;
333 CpmlPair ref2, base;
334 CpmlPair pair;
335 CpmlExtents extents;
337 if (_ADG_OLD_ENTITY_CLASS->arrange != NULL)
338 _ADG_OLD_ENTITY_CLASS->arrange(entity);
340 rdim = (AdgRDim *) entity;
341 dim = (AdgDim *) rdim;
342 data = rdim->data;
343 quote = adg_dim_get_quote(dim);
344 quote_entity = (AdgEntity *) quote;
346 if (! _adg_update_geometry(rdim))
347 return;
349 _adg_update_entities(rdim);
351 /* Check for cached result */
352 if (data->cairo.path.status == CAIRO_STATUS_SUCCESS) {
353 adg_entity_set_global_map(quote_entity, &data->quote.global_map);
354 return;
357 outside = adg_dim_get_outside(dim);
358 if (outside == ADG_THREE_STATE_UNKNOWN)
359 outside = ADG_THREE_STATE_OFF;
361 global = adg_entity_get_global_matrix(entity);
362 local = adg_entity_get_local_matrix(entity);
363 extents.is_defined = FALSE;
365 cpml_pair_copy(&ref2, (CpmlPair *) adg_dim_get_ref2(dim));
366 cpml_pair_copy(&base, &data->point.base);
368 cpml_pair_transform(&ref2, local);
369 cpml_pair_transform(&base, local);
370 base.x += data->shift.base.x;
371 base.y += data->shift.base.y;
373 /* baseline start */
374 cpml_pair_to_cairo(&base, &data->cairo.data[1]);
376 /* baseline end */
377 cpml_pair_to_cairo(&ref2, &data->cairo.data[3]);
379 if (outside) {
380 AdgDimStyle *dim_style;
381 gdouble beyond;
382 CpmlVector vector;
384 dim_style = _ADG_GET_DIM_STYLE(dim);
385 beyond = adg_dim_style_get_beyond(dim_style);
386 vector.x = ref2.x - base.x;
387 vector.y = ref2.y - base.y;
388 cpml_vector_set_length(&vector, beyond);
389 pair.x = ref2.x + vector.x;
390 pair.y = ref2.y + vector.y;
392 data->cairo.data[2].header.length = 2;
394 /* Outside segment start */
395 cpml_pair_to_cairo(&pair, &data->cairo.data[5]);
397 /* Outside segment end */
398 cpml_pair_to_cairo(&ref2, &data->cairo.data[7]);
399 } else {
400 data->cairo.data[2].header.length = 6;
403 /* Arrange the quote */
404 if (quote != NULL) {
405 cairo_matrix_t map;
407 adg_alignment_set_factor_explicit(quote, 1, 0);
409 cpml_pair_from_cairo(&pair, &data->cairo.data[1]);
410 cairo_matrix_init_translate(&map, pair.x, pair.y);
411 cairo_matrix_rotate(&map, data->angle);
412 adg_entity_set_global_map(quote_entity, &map);
413 adg_entity_arrange(quote_entity);
414 cpml_extents_add(&extents, adg_entity_get_extents(quote_entity));
416 adg_matrix_copy(&data->quote.global_map, &map);
419 data->cairo.path.status = CAIRO_STATUS_SUCCESS;
421 /* Arrange the trail */
422 if (data->trail != NULL) {
423 CpmlExtents trail_extents;
424 cpml_extents_copy(&trail_extents, adg_trail_get_extents(data->trail));
425 cpml_extents_transform(&trail_extents, global);
426 cpml_extents_add(&extents, &trail_extents);
427 } else {
428 _adg_dispose_marker(rdim);
431 /* Arrange the marker */
432 if (data->marker != NULL) {
433 AdgEntity *marker_entity = (AdgEntity *) data->marker;
434 adg_marker_set_segment(data->marker, data->trail, outside ? 2 : 1);
435 adg_entity_local_changed(marker_entity);
436 adg_entity_arrange(marker_entity);
437 cpml_extents_add(&extents, adg_entity_get_extents(marker_entity));
440 adg_entity_set_extents(entity, &extents);
443 static void
444 _adg_render(AdgEntity *entity, cairo_t *cr)
446 AdgRDim *rdim;
447 AdgDim *dim;
448 AdgRDimPrivate *data;
449 AdgDimStyle *dim_style;
450 AdgDress dress;
451 const cairo_path_t *cairo_path;
453 rdim = (AdgRDim *) entity;
454 data = rdim->data;
456 if (!data->geometry_arranged) {
457 /* Entity not arranged, probably due to undefined pair found */
458 return;
461 dim = (AdgDim *) entity;
462 dim_style = _ADG_GET_DIM_STYLE(dim);
464 adg_style_apply((AdgStyle *) dim_style, entity, cr);
465 adg_entity_render((AdgEntity *) adg_dim_get_quote(dim), cr);
467 if (data->marker != NULL)
468 adg_entity_render((AdgEntity *) data->marker, cr);
470 cairo_transform(cr, adg_entity_get_global_matrix(entity));
471 dress = adg_dim_style_get_line_dress(dim_style);
472 adg_entity_apply_dress(entity, dress, cr);
474 cairo_path = adg_trail_get_cairo_path(data->trail);
475 cairo_append_path(cr, cairo_path);
476 cairo_stroke(cr);
479 static gchar *
480 _adg_default_value(AdgDim *dim)
482 AdgRDim *rdim;
483 AdgRDimPrivate *data;
485 rdim = (AdgRDim *) dim;
486 data = rdim->data;
488 if (! _adg_update_geometry(rdim))
489 return g_strdup("undef");
491 return adg_dim_get_text(dim, data->radius);
494 static gboolean
495 _adg_update_geometry(AdgRDim *rdim)
497 AdgRDimPrivate *data;
498 AdgDim *dim;
499 AdgDimStyle *dim_style;
500 AdgPoint *ref1_point, *ref2_point, *pos_point;
501 const CpmlPair *ref1, *ref2, *pos;
502 gdouble spacing, level, pos_distance;
503 CpmlVector vector;
505 data = rdim->data;
507 /* Check for cached results */
508 if (data->geometry_arranged)
509 return TRUE;
511 dim = (AdgDim *) rdim;
512 ref1_point = adg_dim_get_ref1(dim);
513 ref2_point = adg_dim_get_ref2(dim);
514 pos_point = adg_dim_get_pos(dim);
516 /* Check if the needed points are all properly defined */
517 if (! adg_point_update(ref1_point) ||
518 ! adg_point_update(ref2_point) ||
519 ! adg_point_update(pos_point))
520 return FALSE;
522 ref1 = (CpmlPair *) ref1_point;
523 ref2 = (CpmlPair *) ref2_point;
524 pos = (CpmlPair *) pos_point;
526 /* Check if the given points have valid coordinates */
527 if (cpml_pair_equal(ref1, ref2)) {
528 g_warning(_("%s: ref1 and ref2 cannot be coincidents (%lf, %lf)"),
529 G_STRLOC, ref1->x, ref1->y);
530 return FALSE;
533 dim_style = _ADG_GET_DIM_STYLE(rdim);
534 spacing = adg_dim_style_get_baseline_spacing(dim_style);
535 level = adg_dim_get_level(dim);
536 pos_distance = cpml_pair_distance(pos, ref1);
537 vector.x = ref2->x - ref1->x;
538 vector.y = ref2->y - ref1->y;
540 if (cpml_pair_squared_distance(pos, ref1) <
541 cpml_pair_squared_distance(pos, ref2)) {
542 vector.x = -vector.x;
543 vector.y = -vector.y;
546 /* radius */
547 data->radius = cpml_pair_distance(&vector, NULL);
549 /* angle */
550 data->angle = adg_dim_quote_angle(dim, cpml_vector_angle(&vector));
552 /* point.base */
553 cpml_pair_copy(&data->point.base, &vector);
554 cpml_vector_set_length(&data->point.base, pos_distance);
555 data->point.base.x += ref1->x;
556 data->point.base.y += ref1->y;
558 /* shift.base */
559 cpml_pair_copy(&data->shift.base, &vector);
560 cpml_vector_set_length(&data->shift.base, spacing * level);
562 data->geometry_arranged = TRUE;
564 return TRUE;
567 static void
568 _adg_update_entities(AdgRDim *rdim)
570 AdgEntity *entity;
571 AdgRDimPrivate *data;
572 AdgDimStyle *dim_style;
574 entity = (AdgEntity *) rdim;
575 data = rdim->data;
576 dim_style = _ADG_GET_DIM_STYLE(rdim);
578 if (data->trail == NULL)
579 data->trail = adg_trail_new(_adg_trail_callback, rdim);
581 if (data->marker == NULL) {
582 data->marker = adg_dim_style_marker2_new(dim_style);
583 adg_entity_set_parent((AdgEntity *) data->marker, entity);
587 static void
588 _adg_clear_trail(AdgRDim *rdim)
590 AdgRDimPrivate *data = rdim->data;
592 if (data->trail != NULL)
593 adg_model_clear((AdgModel *) data->trail);
595 data->cairo.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
598 static void
599 _adg_dispose_trail(AdgRDim *rdim)
601 AdgRDimPrivate *data = rdim->data;
603 if (data->trail != NULL) {
604 g_object_unref(data->trail);
605 data->trail = NULL;
609 static void
610 _adg_dispose_marker(AdgRDim *rdim)
612 AdgRDimPrivate *data = rdim->data;
614 if (data->marker != NULL) {
615 g_object_unref(data->marker);
616 data->marker = NULL;
620 static cairo_path_t *
621 _adg_trail_callback(AdgTrail *trail, gpointer user_data)
623 AdgRDim *rdim;
624 AdgRDimPrivate *data;
626 rdim = (AdgRDim *) user_data;
627 data = rdim->data;
629 return &data->cairo.path;