[CPML] Removed dependency on alloca
[adg.git] / adg / adg-rdim.c
blob390f20895fcde78fad097ee4df748fa8ad7fcfc1
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010 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: Linear dimensions
25 * The #AdgRDim entity represents a linear dimension.
26 **/
28 /**
29 * AdgRDim:
31 * All fields are private and should not be used directly.
32 * Use its public methods instead.
33 **/
36 #include "adg-internal.h"
37 #include "adg-rdim.h"
38 #include "adg-rdim-private.h"
39 #include "adg-dim-private.h"
40 #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,
51 cairo_t *cr);
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,
58 gpointer user_data);
61 G_DEFINE_TYPE(AdgRDim, adg_rdim, ADG_TYPE_DIM);
64 static void
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;
87 static void
88 adg_rdim_init(AdgRDim *rdim)
90 AdgRDimPrivate *data;
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 = CPML_MOVE;
95 move_to.header.length = 2;
96 line_to.header.type = CPML_LINE;
97 line_to.header.length = 2;
99 data->trail = NULL;
100 data->marker = NULL;
101 data->geometry_arranged = FALSE;
102 data->radius = -1.;
103 data->shift.base.x = data->shift.base.y = 0;
104 cairo_matrix_init_identity(&data->quote.global_map);
106 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
107 data->cpml.path.data = data->cpml.data;
108 data->cpml.path.num_data = G_N_ELEMENTS(data->cpml.data);
109 data->cpml.path.data[0] = move_to;
110 data->cpml.path.data[2] = line_to;
111 data->cpml.path.data[4] = move_to;
112 data->cpml.path.data[6] = line_to;
114 rdim->data = data;
117 static void
118 dispose(GObject *object)
120 dispose_marker((AdgRDim *) object);
122 if (PARENT_OBJECT_CLASS->dispose)
123 PARENT_OBJECT_CLASS->dispose(object);
128 * adg_rdim_new:
130 * Creates a new uninitialized radial dimension. To be useful, you
131 * need at least define the center of the arc to quote (ref1) and
132 * a point on the arc (ref2) with adg_dim_set_ref() and the
133 * position of the quote with adg_dim_set_pos().
135 * Returns: a newly created quote
137 AdgRDim *
138 adg_rdim_new(void)
140 return g_object_new(ADG_TYPE_RDIM, NULL);
144 * adg_rdim_new_full:
145 * @center: center of the arc to quote
146 * @radius: where the quote must be applied on the arc
147 * @pos: position of the quote text
149 * Creates a new quote by specifying explicitely all the needed
150 * data to get a valid quote.
152 * Returns: a newly created quote
154 AdgRDim *
155 adg_rdim_new_full(const AdgPair *center, const AdgPair *radius,
156 const AdgPair *pos)
158 AdgRDim *rdim;
159 AdgDim *dim;
161 rdim = adg_rdim_new();
162 dim = (AdgDim *) rdim;
164 adg_dim_set_ref(dim, center, radius);
165 adg_dim_set_pos(dim, pos);
167 return rdim;
171 * adg_rdim_new_full_explicit:
172 * @center_x: x coordinate of the center of the arc to quote
173 * @center_y: y coordinate of the center of the arc to quote
174 * @radius_x: x coordiante where the quote must be applied on the arc
175 * @radius_y: y coordiante where the quote must be applied on the arc
176 * @pos_x: x coordinate of the quote text
177 * @pos_y: y coordinate of the quote text
179 * Does the same job of adg_rdim_full() but using specific coordinates
180 * instead of AdgPair structures.
181 * data to get a valid quote.
183 * Returns: a newly created quote
185 AdgRDim *
186 adg_rdim_new_full_explicit(gdouble center_x, gdouble center_y,
187 gdouble radius_x, gdouble radius_y,
188 gdouble pos_x, gdouble pos_y)
190 AdgPair center, radius, pos;
192 center.x = center_x;
193 center.y = center_y;
194 radius.x = radius_x;
195 radius.y = radius_y;
196 pos.x = pos_x;
197 pos.y = pos_y;
199 return adg_rdim_new_full(&center, &radius, &pos);
203 * adg_rdim_new_full_from_model:
204 * @model: the model from which the named pairs are taken
205 * @center: the center point of the arc to quote
206 * @radius: an arbitrary point on the arc
207 * @pos: the position reference
209 * Creates a new radial dimension, specifing all the needed properties in
210 * one shot and using named pairs from @model.
212 * Returns: the newly created radial dimension entity
214 AdgRDim *
215 adg_rdim_new_full_from_model(AdgModel *model, const gchar *center,
216 const gchar *radius, const gchar *pos)
218 AdgDim *dim = g_object_new(ADG_TYPE_RDIM, NULL);
220 adg_dim_set_ref_from_model(dim, model, center, radius);
221 adg_dim_set_pos_from_model(dim, model, pos);
223 return (AdgRDim *) dim;
227 static void
228 local_changed(AdgEntity *entity)
230 unset_trail((AdgRDim *) entity);
232 if (PARENT_ENTITY_CLASS->local_changed)
233 PARENT_ENTITY_CLASS->local_changed(entity);
236 static void
237 invalidate(AdgEntity *entity)
239 AdgRDim *rdim;
240 AdgRDimPrivate *data;
242 rdim = (AdgRDim *) entity;
243 data = rdim->data;
245 dispose_marker(rdim);
246 data->geometry_arranged = FALSE;
247 unset_trail(rdim);
249 if (PARENT_ENTITY_CLASS->invalidate)
250 PARENT_ENTITY_CLASS->invalidate(entity);
253 static void
254 arrange(AdgEntity *entity)
256 AdgRDim *rdim;
257 AdgDim *dim;
258 AdgRDimPrivate *data;
259 AdgAlignment *quote;
260 AdgDimStyle *dim_style;
261 gboolean outside;
262 const AdgMatrix *local;
263 AdgPair ref1, ref2, base;
264 AdgPair pair;
266 if (PARENT_ENTITY_CLASS->arrange)
267 PARENT_ENTITY_CLASS->arrange(entity);
269 rdim = (AdgRDim *) entity;
270 dim = (AdgDim *) rdim;
271 data = rdim->data;
272 quote = adg_dim_get_quote(dim);
274 update_geometry(rdim);
275 update_entities(rdim);
277 if (data->cpml.path.status == CAIRO_STATUS_SUCCESS) {
278 AdgEntity *quote_entity = (AdgEntity *) quote;
279 adg_entity_set_global_map(quote_entity, &data->quote.global_map);
280 return;
283 dim_style = GET_DIM_STYLE(dim);
284 outside = adg_dim_get_outside(dim);
285 if (outside == ADG_THREE_STATE_UNKNOWN)
286 outside = ADG_THREE_STATE_OFF;
288 local = adg_entity_get_local_matrix(entity);
289 cpml_pair_copy(&ref1, adg_dim_get_ref1(dim));
290 cpml_pair_copy(&ref2, adg_dim_get_ref2(dim));
291 cpml_pair_copy(&base, &data->point.base);
293 cpml_pair_transform(&ref1, local);
294 cpml_pair_transform(&ref2, local);
295 cpml_pair_transform(&base, local);
296 base.x += data->shift.base.x;
297 base.y += data->shift.base.y;
299 /* baseline start */
300 cpml_pair_to_cairo(&base, &data->cpml.data[1]);
302 /* baseline end */
303 cpml_pair_to_cairo(&ref2, &data->cpml.data[3]);
305 if (outside) {
306 data->cpml.data[2].header.length = 2;
307 /* TODO: outside radius dimensions */
308 } else {
309 data->cpml.data[2].header.length = 6;
312 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
314 if (quote != NULL) {
315 AdgEntity *quote_entity;
316 AdgMatrix map;
318 quote_entity = (AdgEntity *) quote;
319 cpml_pair_from_cairo(&pair, &data->cpml.data[1]);
321 adg_alignment_set_factor_explicit(quote, 1, 0);
323 cairo_matrix_init_translate(&map, pair.x, pair.y);
324 cairo_matrix_rotate(&map, data->angle);
325 adg_entity_set_global_map(quote_entity, &map);
327 adg_matrix_copy(&data->quote.global_map,
328 adg_entity_get_global_map(quote_entity));
331 if (data->marker != NULL) {
332 adg_marker_set_segment(data->marker, data->trail, outside ? 2 : 1);
333 adg_entity_local_changed((AdgEntity *) data->marker);
336 /* TODO: compute the extents */
339 static void
340 render(AdgEntity *entity, cairo_t *cr)
342 AdgRDim *rdim;
343 AdgDim *dim;
344 AdgRDimPrivate *data;
345 AdgDimStyle *dim_style;
346 AdgDress dress;
347 const cairo_path_t *cairo_path;
349 rdim = (AdgRDim *) entity;
350 dim = (AdgDim *) entity;
351 data = rdim->data;
352 dim_style = GET_DIM_STYLE(dim);
354 adg_style_apply((AdgStyle *) dim_style, entity, cr);
356 if (data->marker != NULL)
357 adg_entity_render((AdgEntity *) data->marker, cr);
359 adg_entity_render((AdgEntity *) adg_dim_get_quote(dim), cr);
361 dress = adg_dim_style_get_line_dress(dim_style);
362 adg_entity_apply_dress(entity, dress, cr);
364 cairo_path = adg_trail_get_cairo_path(data->trail);
365 cairo_append_path(cr, cairo_path);
366 cairo_stroke(cr);
369 static gchar *
370 default_value(AdgDim *dim)
372 AdgRDim *rdim;
373 AdgRDimPrivate *data;
374 AdgDimStyle *dim_style;
375 const gchar *format;
377 rdim = (AdgRDim *) dim;
378 data = rdim->data;
379 dim_style = GET_DIM_STYLE(dim);
380 format = adg_dim_style_get_number_format(dim_style);
382 update_geometry(rdim);
384 return g_strdup_printf(format, data->radius);
387 static void
388 update_geometry(AdgRDim *rdim)
390 AdgRDimPrivate *data;
391 AdgDim *dim;
392 AdgDimStyle *dim_style;
393 const AdgPair *ref1, *ref2;
394 const AdgPair *pos;
395 gdouble spacing, level, pos_distance;
396 CpmlVector vector;
398 data = rdim->data;
400 if (data->geometry_arranged)
401 return;
403 dim = (AdgDim *) rdim;
404 dim_style = GET_DIM_STYLE(rdim);
405 ref1 = adg_dim_get_ref1(dim);
406 ref2 = adg_dim_get_ref2(dim);
407 pos = adg_dim_get_pos(dim);
408 spacing = adg_dim_style_get_baseline_spacing(dim_style);
409 level = adg_dim_get_level(dim);
410 pos_distance = cpml_pair_distance(pos, ref1);
411 vector.x = ref2->x - ref1->x;
412 vector.y = ref2->y - ref1->y;
413 if (cpml_pair_squared_distance(pos, ref1) <
414 cpml_pair_squared_distance(pos, ref2)) {
415 vector.x = -vector.x;
416 vector.y = -vector.y;
419 /* radius */
420 data->radius = cpml_pair_distance(&vector, NULL);
422 /* angle */
423 data->angle = adg_dim_quote_angle(dim, cpml_vector_angle(&vector));
425 /* point.base */
426 cpml_pair_copy(&data->point.base, &vector);
427 cpml_vector_set_length(&data->point.base, pos_distance);
428 data->point.base.x += ref1->x;
429 data->point.base.y += ref1->y;
431 /* shift.base */
432 cpml_pair_copy(&data->shift.base, &vector);
433 cpml_vector_set_length(&data->shift.base, spacing * level);
435 data->geometry_arranged = TRUE;
438 static void
439 update_entities(AdgRDim *rdim)
441 AdgRDimPrivate *data;
442 AdgDimStyle *dim_style;
444 data = rdim->data;
445 dim_style = GET_DIM_STYLE(rdim);
447 if (data->trail == NULL)
448 data->trail = adg_trail_new(trail_callback, rdim);
450 if (data->marker == NULL)
451 data->marker = adg_dim_style_marker2_new(dim_style);
454 static void
455 unset_trail(AdgRDim *rdim)
457 AdgRDimPrivate *data = rdim->data;
459 if (data->trail != NULL)
460 adg_model_clear((AdgModel *) data->trail);
462 data->cpml.path.status = CAIRO_STATUS_INVALID_PATH_DATA;
465 static void
466 dispose_marker(AdgRDim *rdim)
468 AdgRDimPrivate *data = rdim->data;
470 if (data->trail != NULL) {
471 g_object_unref(data->trail);
472 data->trail = NULL;
475 if (data->marker != NULL) {
476 g_object_unref(data->marker);
477 data->marker = NULL;
481 static CpmlPath *
482 trail_callback(AdgTrail *trail, gpointer user_data)
484 AdgRDim *rdim;
485 AdgRDimPrivate *data;
487 rdim = (AdgRDim *) user_data;
488 data = rdim->data;
490 return &data->cpml.path;