[AdgLineStyle] Removed order bug
[adg.git] / adg / adg-trail.c
blobd5ab5613e1fec93dc7ac714c2c7afefec4cf1e6f
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 != NULL)
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 g_return_val_if_fail(ADG_IS_TRAIL(trail), NULL);
242 return ADG_TRAIL_GET_CLASS(trail)->get_cpml_path(trail);
246 * adg_trail_get_segment:
247 * @trail: an #AdgTrail
248 * @segment: the destination #AdgSegment
249 * @n: the segment number to retrieve, where %1 is the first segment
251 * Convenient function to get a segment from @trail. The segment is
252 * got from the CPML path: check out adg_trail_get_cpml_path() for
253 * further information.
255 * Returns: %TRUE on success or %FALSE on errors
257 gboolean
258 adg_trail_get_segment(AdgTrail *trail, AdgSegment *segment, guint n)
260 CpmlPath *cpml_path;
261 guint cnt;
263 g_return_val_if_fail(ADG_IS_TRAIL(trail), FALSE);
264 g_return_val_if_fail(segment != NULL, FALSE);
266 if (n == 0) {
267 g_warning("%s: requested undefined segment for type `%s'",
268 G_STRLOC, g_type_name(G_OBJECT_TYPE(trail)));
269 return FALSE;
272 cpml_path = adg_trail_get_cpml_path(trail);
274 cpml_segment_from_cairo(segment, cpml_path);
275 for (cnt = 1; cnt < n; ++cnt)
276 if (!cpml_segment_next(segment)) {
277 g_warning("%s: segment `%u' out of range for type `%s'",
278 G_STRLOC, n, g_type_name(G_OBJECT_TYPE(trail)));
279 return FALSE;
282 return TRUE;
286 * adg_trail_extents:
287 * @trail: an #AdgTrail
289 * Gets the extents of @trail. The returned pointer is owned by
290 * @trail and should not be freed nor modified.
292 * Returns: the requested extents or %NULL on errors
294 const CpmlExtents *
295 adg_trail_extents(AdgTrail *trail)
297 AdgTrailPrivate *data;
299 g_return_val_if_fail(ADG_IS_TRAIL(trail), NULL);
301 data = trail->data;
303 if (!data->extents.is_defined) {
304 CpmlPath *cpml_path;
305 CpmlSegment segment;
306 CpmlExtents extents;
308 cpml_path = adg_trail_get_cpml_path(trail);
310 if (cpml_segment_from_cairo(&segment, cpml_path))
311 do {
312 cpml_segment_extents(&segment, &extents);
313 cpml_extents_add(&data->extents, &extents);
314 } while (cpml_segment_next(&segment));
317 return &data->extents;
321 * adg_trail_dump:
322 * @trail: an #AdgTrail
324 * Dumps the data content of @trail to stdout in a human readable format.
326 void
327 adg_trail_dump(AdgTrail *trail)
329 CpmlSegment segment;
330 cairo_path_t *cairo_path;
332 g_return_if_fail(ADG_IS_TRAIL(trail));
334 cairo_path = (cairo_path_t *) adg_trail_get_cairo_path(trail);
336 g_return_if_fail(cairo_path != NULL);
338 if (!cpml_segment_from_cairo(&segment, cairo_path)) {
339 g_warning("Invalid path data to dump!\n");
340 } else {
341 do {
342 cpml_segment_dump(&segment);
343 } while (cpml_segment_next(&segment));
348 static void
349 clear(AdgModel *model)
351 AdgTrailPrivate *data = ((AdgTrail *) model)->data;
353 g_free(data->cairo_path.data);
355 data->cairo_path.status = CAIRO_STATUS_INVALID_PATH_DATA;
356 data->cairo_path.data = NULL;
357 data->cairo_path.num_data = 0;
359 if (PARENT_MODEL_CLASS->clear != NULL)
360 PARENT_MODEL_CLASS->clear(model);
363 static CpmlPath *
364 get_cpml_path(AdgTrail *trail)
366 AdgTrailPrivate *data = trail->data;
368 if (data->callback == NULL) {
369 g_warning("%s: callback not defined for instance of type `%s'",
370 G_STRLOC, g_type_name(G_OBJECT_TYPE(trail)));
371 return NULL;
374 return data->callback(trail, data->user_data);
377 static GArray *
378 arc_to_curves(GArray *array, const cairo_path_data_t *src)
380 CpmlPrimitive arc;
381 double start, end;
383 /* Build the arc primitive: the arc origin is supposed to be the previous
384 * point (src-1): this means a primitive must exist before the arc */
385 arc.segment = NULL;
386 arc.org = (cairo_path_data_t *) (src-1);
387 arc.data = (cairo_path_data_t *) src;
389 if (cpml_arc_info(&arc, NULL, NULL, &start, &end)) {
390 CpmlSegment segment;
391 int n_curves;
392 cairo_path_data_t *curves;
394 n_curves = ceil(fabs(end-start) / M_PI_2);
395 curves = g_new(cairo_path_data_t, n_curves * 4);
396 segment.data = curves;
397 cpml_arc_to_curves(&arc, &segment, n_curves);
399 array = g_array_append_vals(array, curves, n_curves * 4);
401 g_free(curves);
404 return array;