1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2021 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
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"
79 #include "adg-model.h"
81 #include "adg-trail.h"
82 #include "adg-trail-private.h"
85 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_trail_parent_class)
86 #define _ADG_OLD_MODEL_CLASS ((AdgModelClass *) adg_trail_parent_class)
88 #define EMPTY_PATH(p) ((p) == NULL || (p)->data == NULL || (p)->num_data <= 0)
90 G_DEFINE_TYPE_WITH_PRIVATE(AdgTrail
, adg_trail
, ADG_TYPE_MODEL
)
98 static void _adg_finalize (GObject
*object
);
99 static void _adg_get_property (GObject
*object
,
103 static void _adg_set_property (GObject
*object
,
107 static void _adg_clear (AdgModel
*model
);
108 static cairo_path_t
* _adg_get_cairo_path (AdgTrail
*trail
);
109 static GArray
* _adg_arc_to_curves (GArray
*array
,
110 const cairo_path_data_t
*src
,
115 adg_trail_class_init(AdgTrailClass
*klass
)
117 GObjectClass
*gobject_class
;
118 AdgModelClass
*model_class
;
121 gobject_class
= (GObjectClass
*) klass
;
122 model_class
= (AdgModelClass
*) klass
;
124 gobject_class
->finalize
= _adg_finalize
;
125 gobject_class
->get_property
= _adg_get_property
;
126 gobject_class
->set_property
= _adg_set_property
;
128 model_class
->clear
= _adg_clear
;
130 klass
->get_cairo_path
= _adg_get_cairo_path
;
132 param
= g_param_spec_double("max-angle",
134 P_("Max arc angle to approximate with a single Bezier curve: check adg_trail_set_max_angle() for details"),
137 g_object_class_install_property(gobject_class
, PROP_MAX_ANGLE
, param
);
141 adg_trail_init(AdgTrail
*trail
)
143 AdgTrailPrivate
*data
= adg_trail_get_instance_private(trail
);
144 data
->cairo_path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
145 data
->cairo_path
.data
= NULL
;
146 data
->cairo_path
.num_data
= 0;
147 data
->callback
= NULL
;
148 data
->user_data
= NULL
;
149 data
->max_angle
= G_PI_2
;
150 data
->in_construction
= FALSE
;
151 data
->extents
.is_defined
= FALSE
;
155 _adg_finalize(GObject
*object
)
157 _adg_clear((AdgModel
*) object
);
159 if (_ADG_OLD_OBJECT_CLASS
->finalize
)
160 _ADG_OLD_OBJECT_CLASS
->finalize(object
);
164 _adg_get_property(GObject
*object
, guint prop_id
,
165 GValue
*value
, GParamSpec
*pspec
)
167 AdgTrail
*trail
= (AdgTrail
*) object
;
168 AdgTrailPrivate
*data
= adg_trail_get_instance_private(trail
);
172 g_value_set_double(value
, data
->max_angle
);
175 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
181 _adg_set_property(GObject
*object
, guint prop_id
,
182 const GValue
*value
, GParamSpec
*pspec
)
184 AdgTrail
*trail
= (AdgTrail
*) object
;
185 AdgTrailPrivate
*data
= adg_trail_get_instance_private(trail
);
189 data
->max_angle
= g_value_get_double(value
);
192 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
200 * @callback: (scope notified): the #cairo_path_t constructor function
201 * @user_data: generic pointer to pass to the callback
203 * Creates a new trail model. The #cairo_path_t must be constructed by
204 * the @callback function: #AdgTrail will not cache anything, so you
205 * should implement any caching mechanism in the callback, if needed.
207 * Returns: (transfer full): a new trail model.
212 adg_trail_new(AdgTrailCallback callback
, gpointer user_data
)
214 AdgTrail
*trail
= g_object_new(ADG_TYPE_TRAIL
, NULL
);
215 AdgTrailPrivate
*data
= adg_trail_get_instance_private(trail
);
217 data
->callback
= callback
;
218 data
->user_data
= user_data
;
224 * adg_trail_get_cairo_path:
225 * @trail: an #AdgTrail
227 * Gets a pointer to the cairo path of @trail. The returned path is
228 * owned by @trail and must be considered read-only.
230 * This function gets the #cairo_path_t of @trail by calling
231 * adg_trail_cairo_path() and converts its #CPML_ARC primitives,
232 * not recognized by cairo, into approximated Bézier curves
233 * primitives (#CPML_CURVE). The conversion is cached, so any further
234 * request is O(1). This cache is cleared only by the
235 * adg_model_clear() method.
237 * Returns: (transfer none): a pointer to the internal cairo path or <constant>NULL</constant> on errors.
242 adg_trail_get_cairo_path(AdgTrail
*trail
)
244 AdgTrailPrivate
*data
;
245 cairo_path_t
*cairo_path
;
247 const cairo_path_data_t
*p_src
;
250 g_return_val_if_fail(ADG_IS_TRAIL(trail
), NULL
);
252 data
= adg_trail_get_instance_private(trail
);
254 /* Check for cached result */
255 if (data
->cairo_path
.data
!= NULL
)
256 return &data
->cairo_path
;
258 cairo_path
= adg_trail_cairo_path(trail
);
259 if (EMPTY_PATH(cairo_path
))
262 dst
= g_array_sized_new(FALSE
, FALSE
,
263 sizeof(cairo_path_data_t
), cairo_path
->num_data
);
265 /* Cycle the cairo_path_t and convert arcs to Bézier curves */
266 for (i
= 0; i
< cairo_path
->num_data
; i
+= p_src
->header
.length
) {
267 p_src
= (const cairo_path_data_t
*) cairo_path
->data
+ i
;
269 if (p_src
->header
.type
== CPML_ARC
)
270 dst
= _adg_arc_to_curves(dst
, p_src
, data
->max_angle
);
272 dst
= g_array_append_vals(dst
, p_src
, p_src
->header
.length
);
275 cairo_path
= &data
->cairo_path
;
276 cairo_path
->status
= CAIRO_STATUS_SUCCESS
;
277 cairo_path
->num_data
= dst
->len
;
278 cairo_path
->data
= (cairo_path_data_t
*) g_array_free(dst
, FALSE
);
284 * adg_trail_cairo_path:
285 * @trail: an #AdgTrail
287 * Gets the cairo path structure defined by @trail. The returned
288 * value is managed by the #AdgTrail implementation, that is this
289 * function directly calls the <function>get_cairo_path</function>
290 * virtual method that any trail instance must have.
292 * Whenever used internally by the ADG project, the returned path
293 * is (by convention) owned by @trail and so it should not be freed.
294 * Anyway, callers are allowed to modify it as long as its size is
295 * retained and its data contains a valid path: this is needed to
296 * let the #AdgMarker infrastructure work properly (the markers
297 * should be able to modify the trail where they are applied).
299 * Any further call to this method will probably make the pointer
300 * previously returned useless because the #cairo_path_t could be
301 * relocated and the old #cairo_path_t will likely contain rubbish.
303 * Returns: (transfer none): a pointer to the #cairo_path_t or <constant>NULL</constant> on errors.
308 adg_trail_cairo_path(AdgTrail
*trail
)
310 AdgTrailClass
*klass
;
311 AdgTrailPrivate
*data
;
312 cairo_path_t
*cairo_path
;
314 g_return_val_if_fail(ADG_IS_TRAIL(trail
), NULL
);
316 klass
= ADG_TRAIL_GET_CLASS(trail
);
317 if (klass
->get_cairo_path
== NULL
)
320 data
= adg_trail_get_instance_private(trail
);
321 if (data
->in_construction
) {
322 g_warning(_("%s: you cannot access the path from the callback you provided to build it"),
327 data
->in_construction
= TRUE
;
328 cairo_path
= klass
->get_cairo_path(trail
);
329 data
->in_construction
= FALSE
;
335 * adg_trail_n_segments:
336 * @trail: an #AdgTrail
338 * Convenient function that returns the number of non-empty segments defined
339 * by the cairo path embedded in @trail.
341 * Returns: the number of segments or 0 on errrors.
346 adg_trail_n_segments(AdgTrail
*trail
)
348 cairo_path_t
*cairo_path
;
349 CpmlSegment iterator
;
352 g_return_val_if_fail(ADG_IS_TRAIL(trail
), 0);
354 cairo_path
= adg_trail_cairo_path(trail
);
357 if (! EMPTY_PATH(cairo_path
) && cpml_segment_from_cairo(&iterator
, cairo_path
)) {
358 for (n
= 1; cpml_segment_next(&iterator
); ++n
) {
366 * adg_trail_put_segment:
367 * @trail: an #AdgTrail
368 * @n_segment: the segment to retrieve, where 1 is the first segment
369 * @segment: (out): the destination #CpmlSegment
371 * Convenient function to get a segment from @trail. The segment is
372 * got from the cairo path: check out adg_trail_cairo_path() for
373 * further information.
375 * When the segment is not found, either because @n_segment is out
376 * of range or because there is still no path bound to @trail, this
377 * function will return <constant>FALSE</constant> leaving @segment
378 * untouched. If the segment is found and @segment is
379 * not <constant>NULL</constant>, the resulting segment is copied in @segment.
381 * Returns: <constant>TRUE</constant> on success or <constant>FALSE</constant> on errors.
386 adg_trail_put_segment(AdgTrail
*trail
, guint n_segment
, CpmlSegment
*segment
)
388 cairo_path_t
*cairo_path
;
390 CpmlSegment iterator
;
393 g_return_val_if_fail(ADG_IS_TRAIL(trail
), FALSE
);
395 if (n_segment
== 0) {
396 g_warning(_("%s: requested undefined segment for type '%s'"),
397 G_STRLOC
, g_type_name(G_OBJECT_TYPE(trail
)));
401 cairo_path
= adg_trail_cairo_path(trail
);
402 found
= ! EMPTY_PATH(cairo_path
) &&
403 cpml_segment_from_cairo(&iterator
, cairo_path
);
405 for (cnt
= 1; found
&& cnt
< n_segment
; ++cnt
)
406 found
= cpml_segment_next(&iterator
);
408 if (found
&& segment
!= NULL
)
409 cpml_segment_copy(segment
, &iterator
);
415 * adg_trail_get_extents:
416 * @trail: an #AdgTrail
418 * Gets the extents of @trail. The returned pointer is owned by
419 * @trail and should not be freed nor modified.
421 * Returns: the requested extents or <constant>NULL</constant> on errors.
426 adg_trail_get_extents(AdgTrail
*trail
)
428 AdgTrailPrivate
*data
;
430 g_return_val_if_fail(ADG_IS_TRAIL(trail
), NULL
);
432 data
= adg_trail_get_instance_private(trail
);
434 if (!data
->extents
.is_defined
) {
435 cairo_path_t
*cairo_path
;
439 cairo_path
= adg_trail_cairo_path(trail
);
440 if (! EMPTY_PATH(cairo_path
) &&
441 cpml_segment_from_cairo(&segment
, cairo_path
)) {
443 cpml_segment_put_extents(&segment
, &extents
);
444 cpml_extents_add(&data
->extents
, &extents
);
445 } while (cpml_segment_next(&segment
));
449 return &data
->extents
;
454 * @trail: an #AdgTrail
456 * Dumps the data content of @trail to stdout in a human readable format.
461 adg_trail_dump(AdgTrail
*trail
)
464 cairo_path_t
*cairo_path
;
466 g_return_if_fail(ADG_IS_TRAIL(trail
));
468 cairo_path
= (cairo_path_t
*) adg_trail_get_cairo_path(trail
);
470 g_return_if_fail(cairo_path
!= NULL
);
472 if (!cpml_segment_from_cairo(&segment
, cairo_path
)) {
473 g_warning(_("%s: invalid path data"), G_STRLOC
);
476 cpml_segment_dump(&segment
);
477 } while (cpml_segment_next(&segment
));
482 * adg_trail_set_max_angle:
483 * @trail: an #AdgTrail
484 * @angle: the new angle (in radians)
486 * Sets the max angle of @trail to @angle, basically setting
487 * the #AdgTrail:max-angle property.
489 * This property is used to specify the maximum ciruclar arc
490 * that will be approximated by a single Bézier curve in the
491 * adg_trail_get_cairo_path() method. Basically this can be
492 * used to fine tune the fitting algorithm: lower values mean
493 * an arc will be approximated with more curves, lowering the
494 * error but incrementing time and memory needed. The default
495 * value of %G_PI_2 is usually good in most cases.
497 * Check the cairo-arc.c source file (part of the cairo project)
498 * for mathematical details. A copy can probably be consulted
499 * online at the cairo repository on freedesktop. Here is a link
500 * to the 1.10.2 version:
502 * http://cgit.freedesktop.org/cairo/tree/src/cairo-arc.c?id=1.10.2
507 adg_trail_set_max_angle(AdgTrail
*trail
, gdouble angle
)
509 g_return_if_fail(ADG_IS_TRAIL(trail
));
510 g_object_set(trail
, "max-angle", angle
, NULL
);
514 * adg_trail_get_max_angle:
515 * @trail: an #AdgTrail
517 * Gets the #AdgTrail:max-angle property value of @trail.
518 * Refer to adg_trail_set_max_angle() for details of what
519 * this parameter is used for.
521 * Returns: the value (in radians) of the max angle
526 adg_trail_get_max_angle(AdgTrail
*trail
)
528 AdgTrailPrivate
*data
;
530 g_return_val_if_fail(ADG_IS_TRAIL(trail
), 0);
532 data
= adg_trail_get_instance_private(trail
);
533 return data
->max_angle
;
538 _adg_clear(AdgModel
*model
)
540 AdgTrailPrivate
*data
= adg_trail_get_instance_private((AdgTrail
*) model
);
542 g_free(data
->cairo_path
.data
);
544 data
->cairo_path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
545 data
->cairo_path
.data
= NULL
;
546 data
->cairo_path
.num_data
= 0;
547 data
->extents
.is_defined
= FALSE
;
549 if (_ADG_OLD_MODEL_CLASS
->clear
)
550 _ADG_OLD_MODEL_CLASS
->clear(model
);
553 static cairo_path_t
*
554 _adg_get_cairo_path(AdgTrail
*trail
)
556 AdgTrailPrivate
*data
= adg_trail_get_instance_private(trail
);
558 if (data
->callback
== NULL
) {
559 g_warning(_("%s: callback not defined for instance of type '%s'"),
560 G_STRLOC
, g_type_name(G_OBJECT_TYPE(trail
)));
564 return data
->callback(trail
, data
->user_data
);
568 _adg_arc_to_curves(GArray
*array
, const cairo_path_data_t
*src
,
574 /* Build the arc primitive: the arc origin is supposed to be the previous
575 * point (src-1): this means a primitive must exist before the arc */
577 arc
.org
= (cairo_path_data_t
*) (src
-1);
578 arc
.data
= (cairo_path_data_t
*) src
;
580 if (cpml_arc_info(&arc
, NULL
, NULL
, &start
, &end
)) {
583 cairo_path_data_t
*curves
;
585 n_curves
= ceil(fabs(end
-start
) / max_angle
);
586 curves
= g_new(cairo_path_data_t
, n_curves
* 4);
587 segment
.data
= curves
;
588 cpml_arc_to_curves(&arc
, &segment
, n_curves
);
590 array
= g_array_append_vals(array
, curves
, n_curves
* 4);