summaryrefslogtreecommitdiff
path: root/libglbarcode/lgl-barcode.c
blob: b7757ffa4cc8d135d90d6282866ce1b27654a8ff (plain)
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
/*
 *  lgl-barcode.c
 *  Copyright (C) 2001-2010  Jim Evins <evins@snaught.com>.
 *
 *  This file is part of libglbarcode.
 *
 *  libglbarcode 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  libglbarcode 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 libglbarcode.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "lgl-barcode.h"


/*========================================================*/
/* Private macros and constants.                          */
/*========================================================*/


/*========================================================*/
/* Private types.                                         */
/*========================================================*/


/*========================================================*/
/* Private globals.                                       */
/*========================================================*/


/*========================================================*/
/* Private function prototypes.                           */
/*========================================================*/

static void lgl_barcode_add_shape        (lglBarcode      *bc,
                                          lglBarcodeShape *shape);

static void lgl_barcode_shape_free       (lglBarcodeShape *shape);


/*****************************************************************************/
/**
 * lgl_barcode_new:
 *
 * Allocate a new #lglBarcode structure.
 *
 * This function allocates a new #lglBarcode structure.
 * 
 * <note><para>
 *       This function is intended to be used internally by barcode implementations.
 *       Typically an end-user would use lgl_barcode_create() instead.
 * </para></note>
 * 
 *
 * Returns: A newly allocated #lglBarcode structure.  Use lgl_barcode_free() to
 *          free it.
 *
 */
lglBarcode *
lgl_barcode_new (void)
{
        return g_new0 (lglBarcode, 1);
}


/*****************************************************************************/
/**
 * lgl_barcode_free:
 * @bc: The #lglBarcode structure to free
 *
 * Free a previously allocated #lglBarcode structure.
 *
 */
void
lgl_barcode_free (lglBarcode *bc)
{
        GList *p;

        if (bc != NULL)
        {

                for (p = bc->shapes; p != NULL; p = p->next)
                {
                        lgl_barcode_shape_free ((lglBarcodeShape *)p->data);
                }
                g_list_free (bc->shapes);

                g_free (bc);

        }
}


/*****************************************************************************/
/**
 * lgl_barcode_add_line:
 * @bc:     An #lglBarcode structure
 * @x:      x coordinate of top of line
 * @y:      y coordinate of top of line
 * @length: Length of line
 * @width:  Width of line
 *
 * Add a vertical line to barcode.  Coordinates are relative to top left corner
 * of barcode.  All units are in points ( 1 point = 1/72 inch ).
 *
 * <note><para>
 *        This function is intended to be used internally by barcode implementations.
 * </para></note>
 *
 */
void
lgl_barcode_add_line (lglBarcode      *bc,
                      gdouble          x,
                      gdouble          y,
                      gdouble          length,
                      gdouble          width)
{
        lglBarcodeShapeLine *line_shape = g_new0 (lglBarcodeShapeLine, 1);
        line_shape->type = LGL_BARCODE_SHAPE_LINE;

        line_shape->x      = x;
        line_shape->y      = y;
        line_shape->length = length;
        line_shape->width  = width;

        lgl_barcode_add_shape (bc, (lglBarcodeShape *)line_shape);
}


/*****************************************************************************/
/**
 * lgl_barcode_add_box:
 * @bc:     An #lglBarcode structure
 * @x:      x coordinate of top left corner of box
 * @y:      y coordinate of top left corner of box
 * @width:  Width of box
 * @height: Height of box
 *
 * Add a box to barcode.  Coordinates are relative to top left corner
 * of barcode.  All units are in points ( 1 point = 1/72 inch ).
 *
 * <note><para>
 *        This function is intended to be used internally by barcode implementations.
 * </para></note>
 *
 */
void
lgl_barcode_add_box (lglBarcode      *bc,
                     gdouble          x,
                     gdouble          y,
                     gdouble          width,
                     gdouble          height)
{
        lglBarcodeShapeBox *box_shape = g_new0 (lglBarcodeShapeBox, 1);
        box_shape->type = LGL_BARCODE_SHAPE_BOX;

        box_shape->x      = x;
        box_shape->y      = y;
        box_shape->width  = width;
        box_shape->height = height;

        lgl_barcode_add_shape (bc, (lglBarcodeShape *)box_shape);
}


/*****************************************************************************/
/**
 * lgl_barcode_add_char:
 * @bc:     An #lglBarcode structure
 * @x:      x coordinate of left baseline of character
 * @y:      y coordinate of left baseline of character
 * @fsize:  Font size
 * @c:      Character to add
 *
 * Add an ASCII character to barcode.  Coordinates are relative to top left corner
 * of barcode.  All units are in points ( 1 point = 1/72 inch ).
 *
 * <note><para>
 *        This function is intended to be used internally by barcode implementations.
 * </para></note>
 *
 */
void
lgl_barcode_add_char (lglBarcode      *bc,
                      gdouble          x,
                      gdouble          y,
                      gdouble          fsize,
                      gchar            c)
{
        lglBarcodeShapeChar *char_shape = g_new0 (lglBarcodeShapeChar, 1);
        char_shape->type = LGL_BARCODE_SHAPE_CHAR;

        char_shape->x      = x;
        char_shape->y      = y;
        char_shape->fsize  = fsize;
        char_shape->c      = c;

        lgl_barcode_add_shape (bc, (lglBarcodeShape *)char_shape);
}


/*****************************************************************************/
/**
 * lgl_barcode_add_string:
 * @bc:     An #lglBarcode structure
 * @x:      x coordinate of horizontal center of baseline of string
 * @y:      y coordinate of horizontal center of baseline of string
 * @fsize:  Font size
 * @string: String to add
 * @length: Number of bytes in string
 *
 * Add a character string to barcode.  Coordinates are relative to top left corner
 * of barcode.  All units are in points ( 1 point = 1/72 inch ).
 *
 * <note><para>
 *        This function is intended to be used internally by barcode implementations.
 * </para></note>
 *
 */
void
lgl_barcode_add_string (lglBarcode      *bc,
                        gdouble          x,
                        gdouble          y,
                        gdouble          fsize,
                        gchar           *string,
                        gsize            length)
{
        lglBarcodeShapeString *string_shape = g_new0 (lglBarcodeShapeString, 1);
        string_shape->type = LGL_BARCODE_SHAPE_STRING;

        string_shape->x      = x;
        string_shape->y      = y;
        string_shape->fsize  = fsize;
        string_shape->string = g_strndup(string, length);

        lgl_barcode_add_shape (bc, (lglBarcodeShape *)string_shape);
}

/*****************************************************************************/
/**
 * lgl_barcode_add_ring:
 * @bc:         An #lglBarcode structure
 * @x:          x coordinate of center of circle
 * @y:          y coordinate of center of circle
 * @radius:     Radius of ring (center of line)
 * @line_width: Width of line
 *
 * Add a ring to barcode.  Coordinates are relative to top left corner
 * of barcode.  All units are in points ( 1 point = 1/72 inch ).
 *
 * <note><para>
 *        This function is intended to be used internally by barcode implementations.
 * </para></note>
 *
 */
void
lgl_barcode_add_ring (lglBarcode      *bc,
                      gdouble          x,
                      gdouble          y,
                      gdouble          radius,
                      gdouble          line_width)
{
        lglBarcodeShapeRing *ring_shape = g_new0 (lglBarcodeShapeRing, 1);
        ring_shape->type = LGL_BARCODE_SHAPE_RING;

        ring_shape->x          = x;
        ring_shape->y          = y;
        ring_shape->radius     = radius;
        ring_shape->line_width = line_width;

        lgl_barcode_add_shape (bc, (lglBarcodeShape *)ring_shape);
}

/*****************************************************************************/
/**
 * lgl_barcode_add_hexagon:
 * @bc:         An #lglBarcode structure
 * @x:          x coordinate of top point of hexagon
 * @y:          y coordinate of top point of hexagon
 * @height:     Height of hexagon
 *
 * Add a regular hexagon (oriented with vertexes at top and bottom) to barcode.
 * Coordinates are relative to top left corner of barcode.  All units are in
 * points ( 1 point = 1/72 inch ).
 *
 * <note><para>
 *        This function is intended to be used internally by barcode implementations.
 * </para></note>
 *
 */
void
lgl_barcode_add_hexagon (lglBarcode      *bc,
                         gdouble          x,
                         gdouble          y,
                         gdouble          height)
{
        lglBarcodeShapeHexagon *hexagon_shape = g_new0 (lglBarcodeShapeHexagon, 1);
        hexagon_shape->type = LGL_BARCODE_SHAPE_HEXAGON;

        hexagon_shape->x      = x;
        hexagon_shape->y      = y;
        hexagon_shape->height = height;

        lgl_barcode_add_shape (bc, (lglBarcodeShape *)hexagon_shape);
}


/*****************************************************************************/
/* Add shape to barcode.                                                     */
/*****************************************************************************/
static void
lgl_barcode_add_shape (lglBarcode      *bc,
                       lglBarcodeShape *shape)
{
        g_return_if_fail (bc);
        g_return_if_fail (shape);

        bc->shapes = g_list_prepend (bc->shapes, shape);
}


/*****************************************************************************/
/* Free a shape primitive.                                                   */
/*****************************************************************************/
static void
lgl_barcode_shape_free (lglBarcodeShape *shape)
{
        switch (shape->type)
        {

        case LGL_BARCODE_SHAPE_STRING:
                g_free (shape->string.string);
                break;

        default:
                break;
        }

        g_free (shape);
}




/*
 * Local Variables:       -- emacs
 * mode: C                -- emacs
 * c-basic-offset: 8      -- emacs
 * tab-width: 8           -- emacs
 * indent-tabs-mode: nil  -- emacs
 * End:                   -- emacs
 */