adg: provide example on how to customize a style
[adg.git] / src / adg / adg-dash.c
blob223b6c02c40931dccabacc11f9c7d70c3f016572
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2017 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-dash
23 * @Section_Id:AdgDash
24 * @title: AdgDash
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
31 * stroke begins.
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
41 * Since: 1.0
42 **/
44 /**
45 * AdgDash:
47 * All fields are private and should not be used directly.
48 * Use its public methods instead.
50 * Since: 1.0
51 **/
54 #include "adg-internal.h"
56 #include "adg-dash.h"
57 #include "adg-dash-private.h"
59 #include <string.h>
62 GType
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);
72 return dash_type;
75 /**
76 * adg_dash_dup:
77 * @src: an #AdgDash instance
79 * Duplicates @src. The returned value must be freed with adg_dash_destroy()
80 * when no longer needed.
82 * Returns: (transfer full): the duplicate of @src.
84 * Since: 1.0
85 **/
86 AdgDash *
87 adg_dash_dup(const AdgDash *src)
89 AdgDash *dash;
91 g_return_val_if_fail(src != NULL, NULL);
93 dash = g_memdup(src, sizeof(AdgDash));
94 dash->dashes = g_memdup(src->dashes, sizeof(gdouble) * src->num_dashes);
96 return dash;
99 /**
100 * adg_dash_new:
102 * Creates a new empty dash pattern.
104 * Returns: (transfer full): the newly created dash pattern.
106 * Since: 1.0
108 AdgDash *
109 adg_dash_new(void)
111 AdgDash *dash = g_new(AdgDash, 1);
113 dash->dashes = NULL;
114 dash->num_dashes = 0;
115 dash->offset = 0;
117 return dash;
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.
129 * Since: 1.0
131 AdgDash *
132 adg_dash_new_with_dashes(gint num_dashes, ...)
134 AdgDash *dash;
135 va_list var_args;
137 dash = adg_dash_new();
139 va_start(var_args, num_dashes);
140 adg_dash_append_dashes_valist(dash, num_dashes, var_args);
141 va_end(var_args);
143 return dash;
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.
153 * Since: 1.0
155 void
156 adg_dash_append_dash(AdgDash *dash, gdouble length)
158 g_return_if_fail(dash != NULL);
160 ++ dash->num_dashes;
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.
174 * Since: 1.0
176 void
177 adg_dash_append_dashes(AdgDash *dash, gint num_dashes, ...)
179 va_list var_args;
181 va_start(var_args, num_dashes);
182 adg_dash_append_dashes_valist(dash, num_dashes, var_args);
183 va_end(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().
194 * Since: 1.0
196 void
197 adg_dash_append_dashes_valist(AdgDash *dash, gint num_dashes, va_list var_args)
199 gdouble length;
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().
218 * Since: 1.0
220 void
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.
242 * Since: 1.0
244 gint
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
256 * pattern of @dash.
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.
260 * Since: 1.0
262 const gdouble *
263 adg_dash_get_dashes(const AdgDash *dash)
265 g_return_val_if_fail(dash != NULL, NULL);
266 return dash->dashes;
270 * adg_dash_clear_dashes:
271 * @dash: an #AdgDash instance
273 * Resets the dashes of @dash, effectively clearing the pattern.
275 * Since: 1.0
277 void
278 adg_dash_clear_dashes(AdgDash *dash)
280 g_return_if_fail(dash != NULL);
282 g_free(dash->dashes);
283 dash->dashes = NULL;
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.
294 * Since: 1.0
296 void
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.
312 * Since: 1.0
314 gdouble
315 adg_dash_get_offset(const AdgDash *dash)
317 g_return_val_if_fail(dash != NULL, 0);
318 return dash->offset;
322 * adg_dash_destroy:
323 * @dash: an #AdgDash instance
325 * Destroys @dash, freeing every resource owned by it. After the destruction
326 * @dash cannot be used anymore.
328 * Since: 1.0
330 void
331 adg_dash_destroy(AdgDash *dash)
333 g_return_if_fail(dash != NULL);
335 g_free(dash->dashes);
336 g_free(dash);