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.
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.
43 * All fields are private and should not be used directly.
44 * Use its public methods instead.
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
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
76 #include "adg-internal.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
)
99 static void _adg_finalize (GObject
*object
);
100 static void _adg_get_property (GObject
*object
,
104 static void _adg_set_property (GObject
*object
,
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
,
116 adg_trail_class_init(AdgTrailClass
*klass
)
118 GObjectClass
*gobject_class
;
119 AdgModelClass
*model_class
;
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",
137 P_("Max arc angle to approximate with a single Bezier curve: check adg_trail_set_max_angle() for details"),
140 g_object_class_install_property(gobject_class
, PROP_MAX_ANGLE
, param
);
144 adg_trail_init(AdgTrail
*trail
)
146 AdgTrailPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(trail
, ADG_TYPE_TRAIL
,
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
;
162 _adg_finalize(GObject
*object
)
164 _adg_clear((AdgModel
*) object
);
166 if (_ADG_OLD_OBJECT_CLASS
->finalize
)
167 _ADG_OLD_OBJECT_CLASS
->finalize(object
);
171 _adg_get_property(GObject
*object
, guint prop_id
,
172 GValue
*value
, GParamSpec
*pspec
)
175 AdgTrailPrivate
*data
;
177 trail
= (AdgTrail
*) object
;
182 g_value_set_double(value
, data
->max_angle
);
185 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
191 _adg_set_property(GObject
*object
, guint prop_id
,
192 const GValue
*value
, GParamSpec
*pspec
)
195 AdgTrailPrivate
*data
;
197 trail
= (AdgTrail
*) object
;
202 data
->max_angle
= g_value_get_double(value
);
205 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
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.
225 adg_trail_new(AdgTrailCallback callback
, gpointer user_data
)
228 AdgTrailPrivate
*data
;
230 trail
= g_object_new(ADG_TYPE_TRAIL
, NULL
);
233 data
->callback
= callback
;
234 data
->user_data
= user_data
;
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
258 adg_trail_get_cairo_path(AdgTrail
*trail
)
260 AdgTrailPrivate
*data
;
261 cairo_path_t
*cairo_path
;
263 const cairo_path_data_t
*p_src
;
266 g_return_val_if_fail(ADG_IS_TRAIL(trail
), NULL
);
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
))
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
);
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
);
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.
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
)
337 if (data
->in_construction
) {
338 g_warning(_("%s: you cannot access the path from the callback you provided to build it"),
343 data
->in_construction
= TRUE
;
344 cairo_path
= klass
->get_cairo_path(trail
);
345 data
->in_construction
= FALSE
;
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
371 adg_trail_put_segment(AdgTrail
*trail
, guint n_segment
, CpmlSegment
*segment
)
373 cairo_path_t
*cairo_path
;
375 CpmlSegment iterator
;
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
)));
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
));
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
)));
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
415 adg_trail_get_extents(AdgTrail
*trail
)
417 AdgTrailPrivate
*data
;
419 g_return_val_if_fail(ADG_IS_TRAIL(trail
), NULL
);
423 if (!data
->extents
.is_defined
) {
424 cairo_path_t
*cairo_path
;
428 cairo_path
= adg_trail_cairo_path(trail
);
429 if (! EMPTY_PATH(cairo_path
) &&
430 cpml_segment_from_cairo(&segment
, cairo_path
)) {
432 cpml_segment_put_extents(&segment
, &extents
);
433 cpml_extents_add(&data
->extents
, &extents
);
434 } while (cpml_segment_next(&segment
));
438 return &data
->extents
;
443 * @trail: an #AdgTrail
445 * Dumps the data content of @trail to stdout in a human readable format.
450 adg_trail_dump(AdgTrail
*trail
)
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
);
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
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
515 adg_trail_get_max_angle(AdgTrail
*trail
)
517 AdgTrailPrivate
*data
;
519 g_return_val_if_fail(ADG_IS_TRAIL(trail
), 0);
522 return data
->max_angle
;
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
)));
553 return data
->callback(trail
, data
->user_data
);
557 _adg_arc_to_curves(GArray
*array
, const cairo_path_data_t
*src
,
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 */
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
)) {
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);