[build] Bumped version to 0.5.3
[adg.git] / adg / adg-trail.c
blob4d8754d2e11db1c580b078d37feb04a8d3934389
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-trail
23 * @short_description: A bare model built around #CpmlPath
25 * The #AdgTrail model is a really basic model built around the #CpmlPath
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.
36 **/
38 /**
39 * AdgTrail:
41 * All fields are private and should not be used directly.
42 * Use its public methods instead.
43 **/
45 /**
46 * AdgTrailCallback:
47 * @trail: an #AdgTrail
48 * @user_data: the general purpose pointer set by adg_trail_new()
50 * This is the callback used to generate the #CpmlPath and it is
51 * called directly by adg_trail_get_cpml_path(). The caller owns
52 * the returned path, that is the finalization of the returned
53 * #CpmlPath should be made by the caller when appropriate.
55 * Returns: the #CpmlPath of this trail model
56 **/
59 #include "adg-trail.h"
60 #include "adg-trail-private.h"
62 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_trail_parent_class)
63 #define PARENT_MODEL_CLASS ((AdgModelClass *) adg_trail_parent_class)
66 static void finalize (GObject *object);
67 static void clear (AdgModel *model);
68 static CpmlPath * get_cpml_path (AdgTrail *trail);
69 static GArray * arc_to_curves (GArray *array,
70 const cairo_path_data_t
71 *src);
74 G_DEFINE_TYPE(AdgTrail, adg_trail, ADG_TYPE_MODEL);
77 static void
78 adg_trail_class_init(AdgTrailClass *klass)
80 GObjectClass *gobject_class;
81 AdgModelClass *model_class;
83 gobject_class = (GObjectClass *) klass;
84 model_class = (AdgModelClass *) klass;
86 g_type_class_add_private(klass, sizeof(AdgTrailPrivate));
88 gobject_class->finalize = finalize;
90 model_class->clear = clear;
92 klass->get_cpml_path = get_cpml_path;
95 static void
96 adg_trail_init(AdgTrail *trail)
98 AdgTrailPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(trail, ADG_TYPE_TRAIL,
99 AdgTrailPrivate);
101 data->cairo_path.status = CAIRO_STATUS_INVALID_PATH_DATA;
102 data->cairo_path.data = NULL;
103 data->cairo_path.num_data = 0;
105 data->callback = NULL;
106 data->user_data = NULL;
108 trail->data = data;
111 static void
112 finalize(GObject *object)
114 clear((AdgModel *) object);
116 if (PARENT_OBJECT_CLASS->finalize != NULL)
117 PARENT_OBJECT_CLASS->finalize(object);
122 * adg_trail_new:
123 * @callback: the #CpmlPath constructor function
124 * @user_data: generic pointer to pass to the callback
126 * Creates a new trail model. The #CpmlPath must be constructed by the
127 * @callback function: #AdgTrail will not cache anything, so you should
128 * implement any caching mechanism in the callback, if needed.
130 * Returns: a new trail model
132 AdgTrail *
133 adg_trail_new(AdgTrailCallback callback, gpointer user_data)
135 AdgTrail *trail;
136 AdgTrailPrivate *data;
138 trail = g_object_new(ADG_TYPE_TRAIL, NULL);
139 data = trail->data;
141 data->callback = callback;
142 data->user_data = user_data;
144 return trail;
149 * adg_trail_get_cairo_path:
150 * @trail: an #AdgTrail
152 * Gets a pointer to the cairo path of @trail. The return path is
153 * owned by @trail and must be considered read-only.
155 * This function gets the #CpmlPath of @trail by calling
156 * adg_trail_get_cpml_path() and converts its %CAIRO_PATH_ARC_TO
157 * primitives, not recognized by cairo, into approximated Bézier
158 * curves primitives (%CAIRO_PATH_CURVE_TO). The conversion is
159 * cached, so any furter request is O(1). This cache is cleared
160 * only by the adg_model_clear() method.
162 * <important>
163 * <title>TODO</title>
164 * <itemizedlist>
165 * <listitem>Actually, the arcs are approximated to Bézier using the
166 * hardcoded max angle of PI/2. This should be customizable
167 * by adding, for instance, a property to the #AdgTrail class
168 * with a default value of PI/2.</listitem>
169 * </itemizedlist>
170 * </important>
172 * Returns: a pointer to the internal cairo path or %NULL on errors
174 const cairo_path_t *
175 adg_trail_get_cairo_path(AdgTrail *trail)
177 AdgTrailPrivate *data;
178 cairo_path_t *cairo_path;
179 CpmlPath *src;
180 GArray *dst;
181 const cairo_path_data_t *p_src;
182 int i;
184 g_return_val_if_fail(ADG_IS_TRAIL(trail), NULL);
186 data = trail->data;
187 cairo_path = &data->cairo_path;
189 /* Check for cached result */
190 if (cairo_path->data != NULL)
191 return cairo_path;
193 src = adg_trail_get_cpml_path(trail);
194 dst = g_array_sized_new(FALSE, FALSE,
195 sizeof(cairo_path_data_t), src->num_data);
197 /* Cycle the CpmlPath and convert arcs to Bézier curves */
198 for (i = 0; i < src->num_data; i += p_src->header.length) {
199 p_src = (const cairo_path_data_t *) src->data + i;
201 if (p_src->header.type == CAIRO_PATH_ARC_TO)
202 dst = arc_to_curves(dst, p_src);
203 else
204 dst = g_array_append_vals(dst, p_src, p_src->header.length);
207 cairo_path->status = CAIRO_STATUS_SUCCESS;
208 cairo_path->num_data = dst->len;
209 cairo_path->data = (cairo_path_data_t *) g_array_free(dst, FALSE);
211 return cairo_path;
215 * adg_trail_get_cpml_path:
216 * @trail: an #AdgTrail
218 * Gets the CPML path structure defined by @trail. The returned
219 * value is managed by the #AdgTrail implementation, that is this
220 * method simply calls the #AdgTrailClass::get_cpml_path() virtual
221 * function that any trail instance must implement.
223 * Whenever used internally by the ADG project, the returned path
224 * is (by convention) owned by @trail and so it should not be freed.
225 * Anyway, it is allowed to modify it as long as its size is
226 * retained and its data contains a valid path: this is needed to
227 * let the #AdgMarker infrastructure work properly (the markers
228 * should be able to modify the trail where they are applied).
230 * Any further call to this method will probably make the pointer
231 * previously returned useless because the #CpmlPath could be
232 * relocated and the old #CpmlPath will likely contain rubbish.
234 * Returns: a pointer to the #CpmlPath or %NULL on errors
236 CpmlPath *
237 adg_trail_get_cpml_path(AdgTrail *trail)
239 g_return_val_if_fail(ADG_IS_TRAIL(trail), NULL);
241 return ADG_TRAIL_GET_CLASS(trail)->get_cpml_path(trail);
245 * adg_trail_get_segment:
246 * @trail: an #AdgTrail
247 * @segment: the destination #AdgSegment
248 * @n: the segment number to retrieve, where %1 is the first segment
250 * Convenient function to get a segment from @trail. The segment is
251 * got from the CPML path: check out adg_trail_get_cpml_path() for
252 * further information.
254 * Returns: %TRUE on success or %FALSE on errors
256 gboolean
257 adg_trail_get_segment(AdgTrail *trail, AdgSegment *segment, guint n)
259 CpmlPath *cpml_path;
260 guint cnt;
262 g_return_val_if_fail(ADG_IS_TRAIL(trail), FALSE);
263 g_return_val_if_fail(segment != NULL, FALSE);
265 if (n == 0) {
266 g_warning("%s: requested undefined segment for type `%s'",
267 G_STRLOC, g_type_name(G_OBJECT_TYPE(trail)));
268 return FALSE;
271 cpml_path = adg_trail_get_cpml_path(trail);
273 cpml_segment_from_cairo(segment, cpml_path);
274 for (cnt = 1; cnt < n; ++cnt)
275 if (!cpml_segment_next(segment)) {
276 g_warning("%s: segment `%u' out of range for type `%s'",
277 G_STRLOC, n, g_type_name(G_OBJECT_TYPE(trail)));
278 return FALSE;
281 return TRUE;
285 * adg_trail_extents:
286 * @trail: an #AdgTrail
288 * Gets the extents of @trail. The returned pointer is owned by
289 * @trail and should not be freed nor modified.
291 * Returns: the requested extents or %NULL on errors
293 const CpmlExtents *
294 adg_trail_extents(AdgTrail *trail)
296 AdgTrailPrivate *data;
298 g_return_val_if_fail(ADG_IS_TRAIL(trail), NULL);
300 data = trail->data;
302 if (!data->extents.is_defined) {
303 CpmlPath *cpml_path;
304 CpmlSegment segment;
305 CpmlExtents extents;
307 cpml_path = adg_trail_get_cpml_path(trail);
309 if (cpml_segment_from_cairo(&segment, cpml_path))
310 do {
311 cpml_segment_extents(&segment, &extents);
312 cpml_extents_add(&data->extents, &extents);
313 } while (cpml_segment_next(&segment));
316 return &data->extents;
320 * adg_trail_dump:
321 * @trail: an #AdgTrail
323 * Dumps the data content of @trail to stdout in a human readable format.
325 void
326 adg_trail_dump(AdgTrail *trail)
328 CpmlSegment segment;
329 cairo_path_t *cairo_path;
331 g_return_if_fail(ADG_IS_TRAIL(trail));
333 cairo_path = (cairo_path_t *) adg_trail_get_cairo_path(trail);
335 g_return_if_fail(cairo_path != NULL);
337 if (!cpml_segment_from_cairo(&segment, cairo_path)) {
338 g_warning("Invalid path data to dump!\n");
339 } else {
340 do {
341 cpml_segment_dump(&segment);
342 } while (cpml_segment_next(&segment));
347 static void
348 clear(AdgModel *model)
350 AdgTrailPrivate *data = ((AdgTrail *) model)->data;
352 g_free(data->cairo_path.data);
354 data->cairo_path.status = CAIRO_STATUS_INVALID_PATH_DATA;
355 data->cairo_path.data = NULL;
356 data->cairo_path.num_data = 0;
358 if (PARENT_MODEL_CLASS->clear != NULL)
359 PARENT_MODEL_CLASS->clear(model);
362 static CpmlPath *
363 get_cpml_path(AdgTrail *trail)
365 AdgTrailPrivate *data = trail->data;
367 if (data->callback == NULL) {
368 g_warning("%s: callback not defined for instance of type `%s'",
369 G_STRLOC, g_type_name(G_OBJECT_TYPE(trail)));
370 return NULL;
373 return data->callback(trail, data->user_data);
376 static GArray *
377 arc_to_curves(GArray *array, const cairo_path_data_t *src)
379 CpmlPrimitive arc;
380 double start, end;
382 /* Build the arc primitive: the arc origin is supposed to be the previous
383 * point (src-1): this means a primitive must exist before the arc */
384 arc.segment = NULL;
385 arc.org = (cairo_path_data_t *) (src-1);
386 arc.data = (cairo_path_data_t *) src;
388 if (cpml_arc_info(&arc, NULL, NULL, &start, &end)) {
389 CpmlSegment segment;
390 int n_curves;
391 cairo_path_data_t *curves;
393 n_curves = ceil(fabs(end-start) / M_PI_2);
394 curves = g_new(cairo_path_data_t, n_curves * 4);
395 segment.data = curves;
396 cpml_arc_to_curves(&arc, &segment, n_curves);
398 array = g_array_append_vals(array, curves, n_curves * 4);
400 g_free(curves);
403 return array;