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.
25 * @short_description: Dash pattern for line stroking
27 * The #AdgDash boxed type wraps the values needed by cairo to
28 * univoquely identify a dash pattern, an array of positive values. Each
29 * value provides the length of alternate "on" and "off" portions of the
30 * stroke. The offset specifies an offset into the pattern at which the
33 * Each "on" segment will have caps applied as if the segment were a
34 * separate sub-path. In particular, it is valid to use an "on" length of
35 * 0 with %CAIRO_LINE_CAP_ROUND or %CAIRO_LINE_CAP_SQUARE in order to
36 * distribute dots or squares along a path.
38 * Check the cairo_set_dash() documentation for further details:
39 * http://www.cairographics.org/manual/cairo-cairo-t.html#cairo-set-dash
47 * All fields are private and should not be used directly.
48 * Use its public methods instead.
54 #include "adg-internal.h"
57 #include "adg-dash-private.h"
63 adg_dash_get_type(void)
65 static GType dash_type
= 0;
67 if (G_UNLIKELY(dash_type
== 0))
68 dash_type
= g_boxed_type_register_static("AdgDash",
69 (GBoxedCopyFunc
) adg_dash_dup
,
70 (GBoxedFreeFunc
) adg_dash_destroy
);
77 * @dash: an #AdgDash instance
81 * Returns: (transfer full): the duplicate of @dash: must be freed with adg_dash_destroy() when no longer needed.
86 adg_dash_dup(const AdgDash
*dash
)
93 dup
= g_memdup(dash
, sizeof(AdgDash
));
94 dup
->dashes
= g_memdup(dash
->dashes
, sizeof(gdouble
) * dash
->num_dashes
);
102 * Creates a new empty dash pattern.
104 * Returns: (transfer full): the newly created dash pattern.
111 AdgDash
*dash
= g_new(AdgDash
, 1);
114 dash
->num_dashes
= 0;
121 * adg_dash_new_with_dashes:
122 * @num_dashes: the number of dashes to set
123 * @...: lengths (a list of double values) of each dash
125 * Creates a new dash pattern, presetting some dashes on it.
127 * Returns: (transfer full): the newly created dash pattern.
132 adg_dash_new_with_dashes(gint num_dashes
, ...)
137 dash
= adg_dash_new();
139 va_start(var_args
, num_dashes
);
140 adg_dash_append_dashes_valist(dash
, num_dashes
, var_args
);
147 * adg_dash_append_dash:
148 * @dash: an #AdgDash instance
149 * @length: the length value
151 * Appends to the @dash pattern a new dash of the specified @length value.
156 adg_dash_append_dash(AdgDash
*dash
, gdouble length
)
158 g_return_if_fail(dash
!= NULL
);
161 dash
->dashes
= g_realloc(dash
->dashes
, sizeof(gdouble
) * dash
->num_dashes
);
162 dash
->dashes
[dash
->num_dashes
- 1] = length
;
166 * adg_dash_append_dashes:
167 * @dash: an #AdgDash instance
168 * @num_dashes: number of dashes to append
169 * @...: a @num_dashes list of #gdouble
171 * Appends to the current @dash pattern @num_dashes number of dashes.
172 * The length of each dash must be specified as gdouble in the arguments.
177 adg_dash_append_dashes(AdgDash
*dash
, gint num_dashes
, ...)
181 va_start(var_args
, num_dashes
);
182 adg_dash_append_dashes_valist(dash
, num_dashes
, var_args
);
187 * adg_dash_append_dashes_valist:
188 * @dash: an #AdgDash instance
189 * @num_dashes: number of dashes to append
190 * @var_args: a va_list containing @num_dashes list of #gdouble
192 * Variadic version of adg_dash_append_dashes().
197 adg_dash_append_dashes_valist(AdgDash
*dash
, gint num_dashes
, va_list var_args
)
201 g_return_if_fail(dash
!= NULL
);
202 g_return_if_fail(num_dashes
> 0);
204 while (num_dashes
--) {
205 length
= va_arg(var_args
, gdouble
);
206 adg_dash_append_dash(dash
, length
);
211 * adg_dash_append_dashes_array: (rename-to adg_dash_append_dashes)
212 * @dash: an #AdgDash instance
213 * @num_dashes: number of dashes to append
214 * @dashes: (array length=num_dashes): array of @num_dashes gdoubles
216 * Array version of adg_dash_append_dashes().
221 adg_dash_append_dashes_array(AdgDash
*dash
,
222 gint num_dashes
, const gdouble
*dashes
)
224 g_return_if_fail(dash
!= NULL
);
226 if (num_dashes
> 0) {
227 gint old_dashes
= dash
->num_dashes
;
228 dash
->num_dashes
+= num_dashes
;
229 dash
->dashes
= g_realloc(dash
->dashes
, sizeof(gdouble
) * dash
->num_dashes
);
230 memcpy(dash
->dashes
+ old_dashes
, dashes
, sizeof(gdouble
) * num_dashes
);
235 * adg_dash_get_num_dashes:
236 * @dash: an #AdgDash instance
238 * Gets the number of dashes stored inside this dash pattern.
240 * Returns: the number of dashes or -1 if @dash is invalid.
245 adg_dash_get_num_dashes(const AdgDash
*dash
)
247 g_return_val_if_fail(dash
!= NULL
, -1);
248 return dash
->num_dashes
;
252 * adg_dash_get_dashes:
253 * @dash: an #AdgDash instance
255 * Gets the array of gdoubles containing the length of each dash of the
258 * Returns: the array of lengths or <constant>NULL</constant> on invalid @dash. The array is owned by @dash and must not be modified or freed.
263 adg_dash_get_dashes(const AdgDash
*dash
)
265 g_return_val_if_fail(dash
!= NULL
, NULL
);
270 * adg_dash_clear_dashes:
271 * @dash: an #AdgDash instance
273 * Resets the dashes of @dash, effectively clearing the pattern.
278 adg_dash_clear_dashes(AdgDash
*dash
)
280 g_return_if_fail(dash
!= NULL
);
282 g_free(dash
->dashes
);
284 dash
->num_dashes
= 0;
288 * adg_dash_set_offset:
289 * @dash: an #AdgDash instance
290 * @offset: the new offset value
292 * Sets the pattern offset of @dash to @offset.
297 adg_dash_set_offset(AdgDash
*dash
, gdouble offset
)
299 g_return_if_fail(dash
!= NULL
);
301 dash
->offset
= offset
;
305 * adg_dash_get_offset:
306 * @dash: an #AdgDash instance
308 * Gets the offset of the pattern in @dash.
310 * Returns: the offset of @dash or 0 on invalid @dash.
315 adg_dash_get_offset(const AdgDash
*dash
)
317 g_return_val_if_fail(dash
!= NULL
, 0);
323 * @dash: an #AdgDash instance
325 * Destroys @dash, freeing every resource owned by it. After the destruction
326 * @dash cannot be used anymore.
331 adg_dash_destroy(AdgDash
*dash
)
333 g_return_if_fail(dash
!= NULL
);
335 g_free(dash
->dashes
);