cpml: removed CpmlPath
[adg.git] / src / adg / adg-trail.c
blob1c4d235c67737bce04699b2ef8f108c24a69d23e
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 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-trail
23 * @short_description: A bare model built around #cairo_path_t
25 * The #AdgTrail model is a really basic model built around the #cairo_path_t
26 * struct: for a full fledged path model consider using #AdgPath.
28 * A trail is a path model that demands all the implementation details to
29 * the caller: this requires a deep knowledge of the ADG details but
30 * provides a great customization level. It should be used when an
31 * #AdgPath is not enough, such as when a model is subject to change
32 * dynamically and the global and local maps do not suffice to express
33 * this alteration. A typical example is the path used to draw extension
34 * lines and base line of #AdgLDim: every point is subject to different
35 * constrains not expressible with a single affine transformation.
37 * Since: 1.0
38 **/
40 /**
41 * AdgTrail:
43 * All fields are private and should not be used directly.
44 * Use its public methods instead.
46 * Since: 1.0
47 **/
49 /**
50 * AdgTrailClass:
51 * @get_cairo_path: virtual method to get the #cairo_path_t bound to the trail.
53 * The default @get_cairo_path calls the #AdgTrailCallback callback passed
54 * to adg_trail_new() during construction. No caching is performed in
55 * between.
57 * Since: 1.0
58 **/
60 /**
61 * AdgTrailCallback:
62 * @trail: an #AdgTrail
63 * @user_data: the general purpose pointer set by adg_trail_new()
65 * This is the callback used to generate the #cairo_path_t and it is
66 * called directly by adg_trail_cairo_path(). The caller owns
67 * the returned path, that is the finalization of the returned
68 * #cairo_path_t should be made by the caller when appropriate.
70 * Returns: the #cairo_path_t of this trail model
72 * Since: 1.0
73 **/
76 #include "adg-internal.h"
77 #include <math.h>
78 #include <string.h>
80 #include "adg-model.h"
82 #include "adg-trail.h"
83 #include "adg-trail-private.h"
86 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_trail_parent_class)
87 #define _ADG_OLD_MODEL_CLASS ((AdgModelClass *) adg_trail_parent_class)
89 #define EMPTY_PATH(p) ((p) == NULL || (p)->data == NULL || (p)->num_data <= 0)
91 G_DEFINE_TYPE(AdgTrail, adg_trail, ADG_TYPE_MODEL)
93 enum {
94 PROP_0,
95 PROP_MAX_ANGLE
99 static void _adg_finalize (GObject *object);
100 static void _adg_get_property (GObject *object,
101 guint param_id,
102 GValue *value,
103 GParamSpec *pspec);
104 static void _adg_set_property (GObject *object,
105 guint param_id,
106 const GValue *value,
107 GParamSpec *pspec);
108 static void _adg_clear (AdgModel *model);
109 static cairo_path_t * _adg_get_cairo_path (AdgTrail *trail);
110 static GArray * _adg_arc_to_curves (GArray *array,
111 const cairo_path_data_t *src,
112 gdouble max_angle);
115 static void
116 adg_trail_class_init(AdgTrailClass *klass)
118 GObjectClass *gobject_class;
119 AdgModelClass *model_class;
120 GParamSpec *param;
122 gobject_class = (GObjectClass *) klass;
123 model_class = (AdgModelClass *) klass;
125 g_type_class_add_private(klass, sizeof(AdgTrailPrivate));
127 gobject_class->finalize = _adg_finalize;
128 gobject_class->get_property = _adg_get_property;
129 gobject_class->set_property = _adg_set_property;
131 model_class->clear = _adg_clear;
133 klass->get_cairo_path = _adg_get_cairo_path;
135 param = g_param_spec_double("max-angle",
136 P_("Max Angle"),
137 P_("Max arc angle to approximate with a single Bezier curve: check adg_trail_set_max_angle() for details"),
138 0, G_PI, G_PI_2,
139 G_PARAM_READWRITE);
140 g_object_class_install_property(gobject_class, PROP_MAX_ANGLE, param);
143 static void
144 adg_trail_init(AdgTrail *trail)
146 AdgTrailPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(trail, ADG_TYPE_TRAIL,
147 AdgTrailPrivate);
149 data->cairo_path.status = CAIRO_STATUS_INVALID_PATH_DATA;
150 data->cairo_path.data = NULL;
151 data->cairo_path.num_data = 0;
152 data->callback = NULL;
153 data->user_data = NULL;
154 data->max_angle = G_PI_2;
155 data->in_construction = FALSE;
156 data->extents.is_defined = FALSE;
158 trail->data = data;
161 static void
162 _adg_finalize(GObject *object)
164 _adg_clear((AdgModel *) object);
166 if (_ADG_OLD_OBJECT_CLASS->finalize)
167 _ADG_OLD_OBJECT_CLASS->finalize(object);
170 static void
171 _adg_get_property(GObject *object, guint prop_id,
172 GValue *value, GParamSpec *pspec)
174 AdgTrail *trail;
175 AdgTrailPrivate *data;
177 trail = (AdgTrail *) object;
178 data = trail->data;
180 switch (prop_id) {
181 case PROP_MAX_ANGLE:
182 g_value_set_double(value, data->max_angle);
183 break;
184 default:
185 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
186 break;
190 static void
191 _adg_set_property(GObject *object, guint prop_id,
192 const GValue *value, GParamSpec *pspec)
194 AdgTrail *trail;
195 AdgTrailPrivate *data;
197 trail = (AdgTrail *) object;
198 data = trail->data;
200 switch (prop_id) {
201 case PROP_MAX_ANGLE:
202 data->max_angle = g_value_get_double(value);
203 break;
204 default:
205 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
206 break;
212 * adg_trail_new:
213 * @callback: (scope notified): the #cairo_path_t constructor function
214 * @user_data: generic pointer to pass to the callback
216 * Creates a new trail model. The #cairo_path_t must be constructed by
217 * the @callback function: #AdgTrail will not cache anything, so you
218 * should implement any caching mechanism in the callback, if needed.
220 * Returns: (transfer full): a new trail model.
222 * Since: 1.0
224 AdgTrail *
225 adg_trail_new(AdgTrailCallback callback, gpointer user_data)
227 AdgTrail *trail;
228 AdgTrailPrivate *data;
230 trail = g_object_new(ADG_TYPE_TRAIL, NULL);
231 data = trail->data;
233 data->callback = callback;
234 data->user_data = user_data;
236 return trail;
240 * adg_trail_get_cairo_path:
241 * @trail: an #AdgTrail
243 * Gets a pointer to the cairo path of @trail. The returned path is
244 * owned by @trail and must be considered read-only.
246 * This function gets the #cairo_path_t of @trail by calling
247 * adg_trail_cairo_path() and converts its #CPML_ARC primitives,
248 * not recognized by cairo, into approximated Bézier curves
249 * primitives (#CPML_CURVE). The conversion is cached, so any further
250 * request is O(1). This cache is cleared only by the
251 * adg_model_clear() method.
253 * Returns: a pointer to the internal cairo path or %NULL on errors
255 * Since: 1.0
257 const cairo_path_t *
258 adg_trail_get_cairo_path(AdgTrail *trail)
260 AdgTrailPrivate *data;
261 cairo_path_t *cairo_path;
262 GArray *dst;
263 const cairo_path_data_t *p_src;
264 int i;
266 g_return_val_if_fail(ADG_IS_TRAIL(trail), NULL);
268 data = trail->data;
270 /* Check for cached result */
271 if (data->cairo_path.data != NULL)
272 return &data->cairo_path;
274 cairo_path = adg_trail_cairo_path(trail);
275 if (EMPTY_PATH(cairo_path))
276 return NULL;
278 dst = g_array_sized_new(FALSE, FALSE,
279 sizeof(cairo_path_data_t), cairo_path->num_data);
281 /* Cycle the cairo_path_t and convert arcs to Bézier curves */
282 for (i = 0; i < cairo_path->num_data; i += p_src->header.length) {
283 p_src = (const cairo_path_data_t *) cairo_path->data + i;
285 if ((CpmlPrimitiveType) p_src->header.type == CPML_ARC)
286 dst = _adg_arc_to_curves(dst, p_src, data->max_angle);
287 else
288 dst = g_array_append_vals(dst, p_src, p_src->header.length);
291 cairo_path = &data->cairo_path;
292 cairo_path->status = CAIRO_STATUS_SUCCESS;
293 cairo_path->num_data = dst->len;
294 cairo_path->data = (cairo_path_data_t *) g_array_free(dst, FALSE);
296 return cairo_path;
300 * adg_trail_cairo_path:
301 * @trail: an #AdgTrail
303 * Gets the cairo path structure defined by @trail. The returned
304 * value is managed by the #AdgTrail implementation, that is this
305 * function directly calls the #AdgTrailClass::get_cairo_path()
306 * virtual method that any trail instance must have.
308 * Whenever used internally by the ADG project, the returned path
309 * is (by convention) owned by @trail and so it should not be freed.
310 * Anyway, callers are allowed to modify it as long as its size is
311 * retained and its data contains a valid path: this is needed to
312 * let the #AdgMarker infrastructure work properly (the markers
313 * should be able to modify the trail where they are applied).
315 * Any further call to this method will probably make the pointer
316 * previously returned useless because the #cairo_path_t could be
317 * relocated and the old #cairo_path_t will likely contain rubbish.
319 * Returns: (transfer none): a pointer to the #cairo_path_t or %NULL on errors.
321 * Since: 1.0
323 cairo_path_t *
324 adg_trail_cairo_path(AdgTrail *trail)
326 AdgTrailClass *klass;
327 AdgTrailPrivate *data;
328 cairo_path_t *cairo_path;
330 g_return_val_if_fail(ADG_IS_TRAIL(trail), NULL);
332 klass = ADG_TRAIL_GET_CLASS(trail);
333 if (klass->get_cairo_path == NULL)
334 return NULL;
336 data = trail->data;
337 if (data->in_construction) {
338 g_warning(_("%s: you cannot access the path from the callback you provided to build it"),
339 G_STRLOC);
340 return NULL;
343 data->in_construction = TRUE;
344 cairo_path = klass->get_cairo_path(trail);
345 data->in_construction = FALSE;
347 return cairo_path;
351 * adg_trail_put_segment:
352 * @trail: an #AdgTrail
353 * @n_segment: the segment to retrieve, where %1 is the first segment
354 * @segment: the destination #CpmlSegment
356 * Convenient function to get a segment from @trail. The segment is
357 * got from the cairo path: check out adg_trail_cairo_path() for
358 * further information.
360 * When the segment is not found, either because @n_segment is out
361 * of range or because there is still no path bound to @trail, this
362 * function will return %FALSE leaving @segment untouched. If the
363 * segment is found and @segment is not %NULL, the resulting segment
364 * is copied in @segment.
366 * Returns: %TRUE on success or %FALSE on errors
368 * Since: 1.0
370 gboolean
371 adg_trail_put_segment(AdgTrail *trail, guint n_segment, CpmlSegment *segment)
373 cairo_path_t *cairo_path;
374 gboolean found;
375 CpmlSegment iterator;
376 guint cnt;
378 g_return_val_if_fail(ADG_IS_TRAIL(trail), FALSE);
380 if (n_segment == 0) {
381 g_warning(_("%s: requested undefined segment for type `%s'"),
382 G_STRLOC, g_type_name(G_OBJECT_TYPE(trail)));
383 return FALSE;
386 cairo_path = adg_trail_cairo_path(trail);
387 found = ! EMPTY_PATH(cairo_path) &&
388 cpml_segment_from_cairo(&iterator, cairo_path);
390 for (cnt = 1; found && cnt < n_segment; ++cnt)
391 found = cpml_segment_next(&iterator);
393 if (found && segment)
394 memcpy(segment, &iterator, sizeof(CpmlSegment));
396 if (!found)
397 g_warning(_("%s: segment %u is out of range for type `%s'"),
398 G_STRLOC, n_segment, g_type_name(G_OBJECT_TYPE(trail)));
400 return found;
404 * adg_trail_get_extents:
405 * @trail: an #AdgTrail
407 * Gets the extents of @trail. The returned pointer is owned by
408 * @trail and should not be freed nor modified.
410 * Returns: the requested extents or %NULL on errors
412 * Since: 1.0
414 const CpmlExtents *
415 adg_trail_get_extents(AdgTrail *trail)
417 AdgTrailPrivate *data;
419 g_return_val_if_fail(ADG_IS_TRAIL(trail), NULL);
421 data = trail->data;
423 if (!data->extents.is_defined) {
424 cairo_path_t *cairo_path;
425 CpmlSegment segment;
426 CpmlExtents extents;
428 cairo_path = adg_trail_cairo_path(trail);
429 if (! EMPTY_PATH(cairo_path) &&
430 cpml_segment_from_cairo(&segment, cairo_path)) {
431 do {
432 cpml_segment_put_extents(&segment, &extents);
433 cpml_extents_add(&data->extents, &extents);
434 } while (cpml_segment_next(&segment));
438 return &data->extents;
442 * adg_trail_dump:
443 * @trail: an #AdgTrail
445 * Dumps the data content of @trail to stdout in a human readable format.
447 * Since: 1.0
449 void
450 adg_trail_dump(AdgTrail *trail)
452 CpmlSegment segment;
453 cairo_path_t *cairo_path;
455 g_return_if_fail(ADG_IS_TRAIL(trail));
457 cairo_path = (cairo_path_t *) adg_trail_get_cairo_path(trail);
459 g_return_if_fail(cairo_path != NULL);
461 if (!cpml_segment_from_cairo(&segment, cairo_path)) {
462 g_warning(_("%s: invalid path data"), G_STRLOC);
463 } else {
464 do {
465 cpml_segment_dump(&segment);
466 } while (cpml_segment_next(&segment));
471 * adg_trail_set_max_angle:
472 * @trail: an #AdgTrail
473 * @angle: the new angle (in radians)
475 * Sets the max angle of @trail to @angle, basically setting
476 * the #AdgTrail:max-angle property.
478 * This property is used to specify the maximum ciruclar arc
479 * that will be approximated by a single Bézier curve in the
480 * adg_trail_get_cairo_path() method. Basically this can be
481 * used to fine tune the fitting algorithm: lower values mean
482 * an arc will be approximated with more curves, lowering the
483 * error but incrementing time and memory needed. The default
484 * value of #G_PI_2 is usually good in most cases.
486 * Check the cairo-arc.c source file (part of the cairo project)
487 * for mathematical details. A copy can probably be consulted
488 * online at the cairo repository on freedesktop. Here is a link
489 * to the 1.10.2 version:
491 * http://cgit.freedesktop.org/cairo/tree/src/cairo-arc.c?id=1.10.2
493 * Since: 1.0
495 void
496 adg_trail_set_max_angle(AdgTrail *trail, gdouble angle)
498 g_return_if_fail(ADG_IS_TRAIL(trail));
499 g_object_set(trail, "max-angle", angle, NULL);
503 * adg_trail_get_max_angle:
504 * @trail: an #AdgTrail
506 * Gets the #AdgTrail:max-angle property value of @trail.
507 * Refer to adg_trail_set_max_angle() for details of what
508 * this parameter is used for.
510 * Returns: the value (in radians) of the max angle
512 * Since: 1.0
514 gdouble
515 adg_trail_get_max_angle(AdgTrail *trail)
517 AdgTrailPrivate *data;
519 g_return_val_if_fail(ADG_IS_TRAIL(trail), 0);
521 data = trail->data;
522 return data->max_angle;
526 static void
527 _adg_clear(AdgModel *model)
529 AdgTrailPrivate *data = ((AdgTrail *) model)->data;
531 g_free(data->cairo_path.data);
533 data->cairo_path.status = CAIRO_STATUS_INVALID_PATH_DATA;
534 data->cairo_path.data = NULL;
535 data->cairo_path.num_data = 0;
536 data->extents.is_defined = FALSE;
538 if (_ADG_OLD_MODEL_CLASS->clear)
539 _ADG_OLD_MODEL_CLASS->clear(model);
542 static cairo_path_t *
543 _adg_get_cairo_path(AdgTrail *trail)
545 AdgTrailPrivate *data = trail->data;
547 if (data->callback == NULL) {
548 g_warning(_("%s: callback not defined for instance of type `%s'"),
549 G_STRLOC, g_type_name(G_OBJECT_TYPE(trail)));
550 return NULL;
553 return data->callback(trail, data->user_data);
556 static GArray *
557 _adg_arc_to_curves(GArray *array, const cairo_path_data_t *src,
558 gdouble max_angle)
560 CpmlPrimitive arc;
561 double start, end;
563 /* Build the arc primitive: the arc origin is supposed to be the previous
564 * point (src-1): this means a primitive must exist before the arc */
565 arc.segment = NULL;
566 arc.org = (cairo_path_data_t *) (src-1);
567 arc.data = (cairo_path_data_t *) src;
569 if (cpml_arc_info(&arc, NULL, NULL, &start, &end)) {
570 CpmlSegment segment;
571 int n_curves;
572 cairo_path_data_t *curves;
574 n_curves = ceil(fabs(end-start) / max_angle);
575 curves = g_new(cairo_path_data_t, n_curves * 4);
576 segment.data = curves;
577 cpml_arc_to_curves(&arc, &segment, n_curves);
579 array = g_array_append_vals(array, curves, n_curves * 4);
581 g_free(curves);
584 return array;