-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoctypes.c
390 lines (349 loc) · 10.4 KB
/
poctypes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* This file is part of PocPlot.
*
* PocPlot is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* PocPlot is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PocPlot; if not, see
* <https://www.gnu.org/licenses/>.
*
* Copyright 2020 Brian Stafford
*/
#include "poctypes.h"
/* Point {{{1 */
/**
* SECTION: poctypes
* @title: Poc Types
* @short_description: Boxed types and enums used by PocPlot.
*
* Boxed types and enums used by PocPlot.
*/
/**
* PocPoint:
* @x: an X coordinate
* @y: a Y coordinate
*
* A boxed type representing an (x,y) coordinate.
*/
static PocPoint *
poc_point_copy (const PocPoint *pt)
{
return g_slice_dup (PocPoint, pt);
}
static void
poc_point_free (PocPoint *pt)
{
g_slice_free (PocPoint, pt);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
G_DEFINE_BOXED_TYPE (PocPoint, poc_point, poc_point_copy, poc_point_free)
#pragma GCC diagnostic pop
/* Point array {{{1 */
/**
* PocPointArray:
* @data: pointer to #PocPoint array. The data may be moved as elements are
* added to the array.
* @len: number of values in the array.
*
* A wrapper for #GArray to create an array of #PocPoint values
*/
/**
* poc_point_array_new:
*
* A wrapper for #GArray to create an array of #PocPointArray values.
*
* Returns: (transfer full): a #PocPointArray
*/
PocPointArray *
poc_point_array_new (void)
{
return (PocPointArray *) g_array_new (FALSE, FALSE, sizeof (PocPoint));
}
/**
* poc_point_array_sized_new:
* @reserved_size: number of elements preallocated
*
* A wrapper for #GArray to create an array of #PocPointArray values with
* @reserved_size elements preallocated. This avoids frequent reallocation, if
* you are going to add many elements to the array. Note however that the size
* of the array is still zero.
*
* Returns: (transfer full): a #PocPointArray
*/
PocPointArray *
poc_point_array_sized_new (guint reserved_size)
{
return (PocPointArray *) g_array_sized_new (FALSE, FALSE, sizeof (PocPoint),
reserved_size);
}
/**
* poc_point_array_ref:
* @array: a #PocPointArray
*
* Increments the reference count of @array by one. This function is
* thread-safe and may be called from any thread.
*
* Returns: the #PocPointArray
*/
PocPointArray *
poc_point_array_ref (PocPointArray *array)
{
return (PocPointArray *) g_array_ref ((GArray *) array);
}
/**
* poc_point_array_unref:
* @array: a #PocPointArray
*
* Increments the reference count of @array by one. This function is
* thread-safe and may be called from any thread.
*/
void
poc_point_array_unref (PocPointArray *array)
{
g_array_unref ((GArray *) array);
}
PocPointArray *
poc_point_array_append_vals (PocPointArray *dst, const PocPoint *src, guint len)
{
return (PocPointArray *) g_array_append_vals ((GArray *) dst, src, len);
}
/**
* poc_point_array_set_size:
* @array: a #PocPointArray
* @size: the new size of the array
*
* Sets the size of the array, expanding it if necessary
* The new elements are set to 0.
*
* Returns: (transfer none): the #PocPointArray
*/
PocPointArray *
poc_point_array_set_size (PocPointArray *array, guint size)
{
return (PocPointArray *) g_array_set_size ((GArray *) array, size);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
G_DEFINE_BOXED_TYPE (PocPointArray, poc_point_array,
poc_point_array_ref, poc_point_array_unref)
#pragma GCC diagnostic pop
/* Double array {{{1 */
/**
* PocDoubleArray:
* @data: pointer to a #gdouble array.
* @len: number of values in the array.
*
* A wrapper for #GArray to create an array of #gdouble values. #GArray
* functions may also be used with appropriate casts.
*/
/**
* poc_double_array_new:
*
* A wrapper for #GArray to create an array of #gdouble values.
*
* Returns: (transfer full): a #PocDoubleArray
*/
PocDoubleArray *
poc_double_array_new (void)
{
return (PocDoubleArray *) g_array_new (FALSE, TRUE, sizeof (gdouble));
}
/**
* poc_double_array_sized_new:
* @reserved_size: number of elements preallocated
*
* A wrapper for #GArray to create an array of #gdouble values with
* @reserved_size elements preallocated. This avoids frequent reallocation, if
* you are going to add many elements to the array. Note however that the size
* of the array is still zero.
*
* Returns: (transfer full): a #PocDoubleArray
*/
PocDoubleArray *
poc_double_array_sized_new (guint reserved_size)
{
return (PocDoubleArray *) g_array_sized_new (FALSE, TRUE, sizeof (gdouble),
reserved_size);
}
/**
* poc_double_array_set_size:
* @array: a #PocDoubleArray
* @size: the new size of the array
*
* Sets the size of the array, expanding it if necessary
* The new elements are set to 0.
*
* Returns: (transfer none): the #PocDoubleArray
*/
PocDoubleArray *
poc_double_array_set_size (PocDoubleArray *array, guint size)
{
return (PocDoubleArray *) g_array_set_size ((GArray *) array, size);
}
/**
* poc_double_array_ref:
* @array: a #PocDoubleArray
*
* Increments the reference count of @array by one. This function is
* thread-safe and may be called from any thread.
*
* Returns: the #PocDoubleArray
*/
PocDoubleArray *
poc_double_array_ref (PocDoubleArray *array)
{
return (PocDoubleArray *) g_array_ref ((GArray *) array);
}
/**
* poc_double_array_unref:
* @array: a #PocDoubleArray
*
* Increments the reference count of @array by one. This function is
* thread-safe and may be called from any thread.
*/
void
poc_double_array_unref (PocDoubleArray *array)
{
g_array_unref ((GArray *) array);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
G_DEFINE_BOXED_TYPE (PocDoubleArray, poc_double_array,
poc_double_array_ref, poc_double_array_unref)
#pragma GCC diagnostic pop
/* Enums {{{1 */
const gchar *
poc_enum_to_string (GType enum_type, gint value)
{
GEnumClass *enum_class;
GEnumValue *enum_value;
enum_class = g_type_class_peek (enum_type);
g_return_val_if_fail (enum_class != NULL, NULL);
enum_value = g_enum_get_value (enum_class, value);
return enum_value != NULL ? enum_value->value_nick : NULL;
}
gint
poc_enum_from_string (GType enum_type, const gchar *string)
{
GEnumClass *enum_class;
GEnumValue *enum_value;
enum_class = g_type_class_peek (enum_type);
g_return_val_if_fail (enum_class != NULL, 0);
enum_value = g_enum_get_value_by_nick (enum_class, string);
g_return_val_if_fail (enum_value != NULL, 0);
return enum_value->value;
}
static void
poc_enum_transform_to_string (const GValue *src, GValue *dest)
{
const gchar *string;
string = poc_enum_to_string (G_VALUE_TYPE (src), g_value_get_enum (src));
g_value_set_static_string (dest, string);
}
static void
poc_enum_transform_from_string (const GValue *src, GValue *dest)
{
gint value;
value = poc_enum_from_string (G_VALUE_TYPE (dest), g_value_get_string (src));
g_value_set_enum (dest, value);
}
/* axis mode {{{2 */
GType
poc_axis_mode_get_type (void)
{
GType type;
static gsize poc_axis_mode_type;
static const GEnumValue values[] =
{
{ POC_AXIS_LINEAR, "POC_AXIS_LINEAR", "linear" },
{ POC_AXIS_LOG_OCTAVE, "POC_AXIS_LOG_OCTAVE", "octaves" },
{ POC_AXIS_LOG_DECADE, "POC_AXIS_LOG_DECADE", "decades" },
{ 0, NULL, NULL }
};
if (g_once_init_enter (&poc_axis_mode_type))
{
type = g_enum_register_static (g_intern_static_string ("PocAxisMode"),
values);
g_value_register_transform_func (type, G_TYPE_STRING,
poc_enum_transform_to_string);
g_value_register_transform_func (G_TYPE_STRING, type,
poc_enum_transform_from_string);
g_once_init_leave (&poc_axis_mode_type, type);
}
return poc_axis_mode_type;
}
/* line style {{{2 */
GType
poc_line_style_get_type (void)
{
GType type;
static gsize poc_line_style_type;
static const GEnumValue values[] =
{
{ POC_LINE_STYLE_SOLID, "POC_LINE_STYLE_SOLID", "solid" },
{ POC_LINE_STYLE_DOTS, "POC_LINE_STYLE_DOTS", "dots" },
{ POC_LINE_STYLE_DASH, "POC_LINE_STYLE_DASH", "dash" },
{ POC_LINE_STYLE_LONG_DASH, "POC_LINE_STYLE_LONG_DASH", "long-dash" },
{ POC_LINE_STYLE_DOT_DASH, "POC_LINE_STYLE_DOT_DASH", "dot-dash" },
{ POC_LINE_STYLE_LONG_SHORT_DASH, "POC_LINE_STYLE_LONG_SHORT_DASH", "long-short-dash" },
{ POC_LINE_STYLE_DOT_DOT_DASH, "POC_LINE_STYLE_DOT_DOT_DASH", "dot-dot-dash" },
{ 0, NULL, NULL }
};
if (g_once_init_enter (&poc_line_style_type))
{
type = g_enum_register_static (g_intern_static_string ("PocLineStyle"),
values);
g_value_register_transform_func (type, G_TYPE_STRING,
poc_enum_transform_to_string);
g_value_register_transform_func (G_TYPE_STRING, type,
poc_enum_transform_from_string);
g_once_init_leave (&poc_line_style_type, type);
}
return poc_line_style_type;
}
const double *
poc_line_style_get_dashes (PocLineStyle line_style, int *num_dashes)
{
static const double dots[] = { 1.0 };
static const double dash[] = { 2.0, 3.0 };
static const double long_dash[] = { 4.0, 3.0 };
static const double dot_dash[] = { 1.0, 1.0, 1.0, 1.0, 4.0 };
static const double long_short_dash[] = { 4.0, 3.0, 2.0, 3.0 };
static const double dot_dot_dash[] = { 1.0, 3.0, 1.0, 3.0, 4.0 };
switch (line_style)
{
case POC_LINE_STYLE_SOLID:
*num_dashes = 0;
return dots; /* just to avoid returning NULL */
case POC_LINE_STYLE_DOTS:
*num_dashes = G_N_ELEMENTS (dots);
return dots;
case POC_LINE_STYLE_DASH:
*num_dashes = G_N_ELEMENTS (dash);
return dash;
case POC_LINE_STYLE_LONG_DASH:
*num_dashes = G_N_ELEMENTS (long_dash);
return long_dash;
case POC_LINE_STYLE_DOT_DASH:
*num_dashes = G_N_ELEMENTS (dot_dash);
return dot_dash;
case POC_LINE_STYLE_LONG_SHORT_DASH:
*num_dashes = G_N_ELEMENTS (long_short_dash);
return long_short_dash;
case POC_LINE_STYLE_DOT_DOT_DASH:
*num_dashes = G_N_ELEMENTS (dot_dot_dash);
return dot_dot_dash;
default:
//FIXME - complain
*num_dashes = 0;
return NULL;
}
}