[AdgTrail] s/adg_trail_extents/adg_trail_get_extents/
[adg.git] / adg / adg-trail.c
blob1e02e2431abbdd26d220b28040d8ec70290c7b69
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;
107 data->extents.is_defined = FALSE;
109 trail->data = data;
112 static void
113 finalize(GObject *object)
115 clear((AdgModel *) object);
117 if (PARENT_OBJECT_CLASS->finalize)
118 PARENT_OBJECT_CLASS->finalize(object);
123 * adg_trail_new:
124 * @callback: the #CpmlPath constructor function
125 * @user_data: generic pointer to pass to the callback
127 * Creates a new trail model. The #CpmlPath must be constructed by the
128 * @callback function: #AdgTrail will not cache anything, so you should
129 * implement any caching mechanism in the callback, if needed.
131 * Returns: a new trail model
133 AdgTrail *
134 adg_trail_new(AdgTrailCallback callback, gpointer user_data)
136 AdgTrail *trail;
137 AdgTrailPrivate *data;
139 trail = g_object_new(ADG_TYPE_TRAIL, NULL);
140 data = trail->data;
142 data->callback = callback;
143 data->user_data = user_data;
145 return trail;
150 * adg_trail_get_cairo_path:
151 * @trail: an #AdgTrail
153 * Gets a pointer to the cairo path of @trail. The return path is
154 * owned by @trail and must be considered read-only.
156 * This function gets the #CpmlPath of @trail by calling
157 * adg_trail_get_cpml_path() and converts its %CAIRO_PATH_ARC_TO
158 * primitives, not recognized by cairo, into approximated Bézier
159 * curves primitives (%CAIRO_PATH_CURVE_TO). The conversion is
160 * cached, so any furter request is O(1). This cache is cleared
161 * only by the adg_model_clear() method.
163 * <important>
164 * <title>TODO</title>
165 * <itemizedlist>
166 * <listitem>Actually, the arcs are approximated to Bézier using the
167 * hardcoded max angle of PI/2. This should be customizable
168 * by adding, for instance, a property to the #AdgTrail class
169 * with a default value of PI/2.</listitem>
170 * </itemizedlist>
171 * </important>
173 * Returns: a pointer to the internal cairo path or %NULL on errors
175 const cairo_path_t *
176 adg_trail_get_cairo_path(AdgTrail *trail)
178 AdgTrailPrivate *data;
179 cairo_path_t *cairo_path;
180 CpmlPath *src;
181 GArray *dst;
182 const cairo_path_data_t *p_src;
183 int i;
185 g_return_val_if_fail(ADG_IS_TRAIL(trail), NULL);
187 data = trail->data;
188 cairo_path = &data->cairo_path;
190 /* Check for cached result */
191 if (cairo_path->data != NULL)
192 return cairo_path;
194 src = adg_trail_get_cpml_path(trail);
195 dst = g_array_sized_new(FALSE, FALSE,
196 sizeof(cairo_path_data_t), src->num_data);
198 /* Cycle the CpmlPath and convert arcs to Bézier curves */
199 for (i = 0; i < src->num_data; i += p_src->header.length) {
200 p_src = (const cairo_path_data_t *) src->data + i;
202 if (p_src->header.type == CAIRO_PATH_ARC_TO)
203 dst = arc_to_curves(dst, p_src);
204 else
205 dst = g_array_append_vals(dst, p_src, p_src->header.length);
208 cairo_path->status = CAIRO_STATUS_SUCCESS;
209 cairo_path->num_data = dst->len;
210 cairo_path->data = (cairo_path_data_t *) g_array_free(dst, FALSE);
212 return cairo_path;
216 * adg_trail_get_cpml_path:
217 * @trail: an #AdgTrail
219 * Gets the CPML path structure defined by @trail. The returned
220 * value is managed by the #AdgTrail implementation, that is this
221 * method simply calls the #AdgTrailClass::get_cpml_path() virtual
222 * function that any trail instance must implement.
224 * Whenever used internally by the ADG project, the returned path
225 * is (by convention) owned by @trail and so it should not be freed.
226 * Anyway, it is allowed to modify it as long as its size is
227 * retained and its data contains a valid path: this is needed to
228 * let the #AdgMarker infrastructure work properly (the markers
229 * should be able to modify the trail where they are applied).
231 * Any further call to this method will probably make the pointer
232 * previously returned useless because the #CpmlPath could be
233 * relocated and the old #CpmlPath will likely contain rubbish.
235 * Returns: a pointer to the #CpmlPath or %NULL on errors
237 CpmlPath *
238 adg_trail_get_cpml_path(AdgTrail *trail)
240 AdgTrailClass *klass;
242 g_return_val_if_fail(ADG_IS_TRAIL(trail), NULL);
244 klass = ADG_TRAIL_GET_CLASS(trail);
246 if (klass->get_cpml_path == NULL)
247 return NULL;
249 return klass->get_cpml_path(trail);
253 * adg_trail_get_segment:
254 * @trail: an #AdgTrail
255 * @segment: the destination #AdgSegment
256 * @n: the segment number to retrieve, where %1 is the first segment
258 * Convenient function to get a segment from @trail. The segment is
259 * got from the CPML path: check out adg_trail_get_cpml_path() for
260 * further information.
262 * Returns: %TRUE on success or %FALSE on errors
264 gboolean
265 adg_trail_get_segment(AdgTrail *trail, AdgSegment *segment, guint n)
267 CpmlPath *cpml_path;
268 guint cnt;
270 g_return_val_if_fail(ADG_IS_TRAIL(trail), FALSE);
271 g_return_val_if_fail(segment != NULL, FALSE);
273 if (n == 0) {
274 g_warning("%s: requested undefined segment for type `%s'",
275 G_STRLOC, g_type_name(G_OBJECT_TYPE(trail)));
276 return FALSE;
279 cpml_path = adg_trail_get_cpml_path(trail);
281 cpml_segment_from_cairo(segment, cpml_path);
282 for (cnt = 1; cnt < n; ++cnt)
283 if (!cpml_segment_next(segment)) {
284 g_warning("%s: segment `%u' out of range for type `%s'",
285 G_STRLOC, n, g_type_name(G_OBJECT_TYPE(trail)));
286 return FALSE;
289 return TRUE;
293 * adg_trail_get_extents:
294 * @trail: an #AdgTrail
296 * Gets the extents of @trail. The returned pointer is owned by
297 * @trail and should not be freed nor modified.
299 * Returns: the requested extents or %NULL on errors
301 const CpmlExtents *
302 adg_trail_get_extents(AdgTrail *trail)
304 AdgTrailPrivate *data;
306 g_return_val_if_fail(ADG_IS_TRAIL(trail), NULL);
308 data = trail->data;
310 if (!data->extents.is_defined) {
311 CpmlPath *cpml_path;
312 CpmlSegment segment;
313 CpmlExtents extents;
315 cpml_path = adg_trail_get_cpml_path(trail);
317 if (cpml_segment_from_cairo(&segment, cpml_path))
318 do {
319 cpml_segment_extents(&segment, &extents);
320 cpml_extents_add(&data->extents, &extents);
321 } while (cpml_segment_next(&segment));
324 return &data->extents;
328 * adg_trail_dump:
329 * @trail: an #AdgTrail
331 * Dumps the data content of @trail to stdout in a human readable format.
333 void
334 adg_trail_dump(AdgTrail *trail)
336 CpmlSegment segment;
337 cairo_path_t *cairo_path;
339 g_return_if_fail(ADG_IS_TRAIL(trail));
341 cairo_path = (cairo_path_t *) adg_trail_get_cairo_path(trail);
343 g_return_if_fail(cairo_path != NULL);
345 if (!cpml_segment_from_cairo(&segment, cairo_path)) {
346 g_warning("Invalid path data to dump!\n");
347 } else {
348 do {
349 cpml_segment_dump(&segment);
350 } while (cpml_segment_next(&segment));
355 static void
356 clear(AdgModel *model)
358 AdgTrailPrivate *data = ((AdgTrail *) model)->data;
360 g_free(data->cairo_path.data);
362 data->cairo_path.status = CAIRO_STATUS_INVALID_PATH_DATA;
363 data->cairo_path.data = NULL;
364 data->cairo_path.num_data = 0;
365 data->extents.is_defined = FALSE;
367 if (PARENT_MODEL_CLASS->clear)
368 PARENT_MODEL_CLASS->clear(model);
371 static CpmlPath *
372 get_cpml_path(AdgTrail *trail)
374 AdgTrailPrivate *data = trail->data;
376 if (data->callback == NULL) {
377 g_warning("%s: callback not defined for instance of type `%s'",
378 G_STRLOC, g_type_name(G_OBJECT_TYPE(trail)));
379 return NULL;
382 return data->callback(trail, data->user_data);
385 static GArray *
386 arc_to_curves(GArray *array, const cairo_path_data_t *src)
388 CpmlPrimitive arc;
389 double start, end;
391 /* Build the arc primitive: the arc origin is supposed to be the previous
392 * point (src-1): this means a primitive must exist before the arc */
393 arc.segment = NULL;
394 arc.org = (cairo_path_data_t *) (src-1);
395 arc.data = (cairo_path_data_t *) src;
397 if (cpml_arc_info(&arc, NULL, NULL, &start, &end)) {
398 CpmlSegment segment;
399 int n_curves;
400 cairo_path_data_t *curves;
402 n_curves = ceil(fabs(end-start) / M_PI_2);
403 curves = g_new(cairo_path_data_t, n_curves * 4);
404 segment.data = curves;
405 cpml_arc_to_curves(&arc, &segment, n_curves);
407 array = g_array_append_vals(array, curves, n_curves * 4);
409 g_free(curves);
412 return array;