]> git.sur5r.net Git - glabels/blob - libglbarcode/lgl-barcode.c
Imported Upstream version 3.0.0
[glabels] / libglbarcode / lgl-barcode.c
1 /*
2  *  lgl-barcode.c
3  *  Copyright (C) 2001-2010  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of libglbarcode.
6  *
7  *  libglbarcode is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  libglbarcode is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License
18  *  along with libglbarcode.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "lgl-barcode.h"
24
25
26 /*========================================================*/
27 /* Private macros and constants.                          */
28 /*========================================================*/
29
30
31 /*========================================================*/
32 /* Private types.                                         */
33 /*========================================================*/
34
35
36 /*========================================================*/
37 /* Private globals.                                       */
38 /*========================================================*/
39
40
41 /*========================================================*/
42 /* Private function prototypes.                           */
43 /*========================================================*/
44
45 static void lgl_barcode_add_shape        (lglBarcode      *bc,
46                                           lglBarcodeShape *shape);
47
48 static void lgl_barcode_shape_free       (lglBarcodeShape *shape);
49
50
51 /*****************************************************************************/
52 /**
53  * lgl_barcode_new:
54  *
55  * Allocate a new #lglBarcode structure.
56  *
57  * This function allocates a new #lglBarcode structure.
58  * 
59  * <note><para>
60  *       This function is intended to be used internally by barcode implementations.
61  *       Typically an end-user would use lgl_barcode_create() instead.
62  * </para></note>
63  * 
64  *
65  * Returns: A newly allocated #lglBarcode structure.  Use lgl_barcode_free() to
66  *          free it.
67  *
68  */
69 lglBarcode *
70 lgl_barcode_new (void)
71 {
72         return g_new0 (lglBarcode, 1);
73 }
74
75
76 /*****************************************************************************/
77 /**
78  * lgl_barcode_free:
79  * @bc: The #lglBarcode structure to free
80  *
81  * Free a previously allocated #lglBarcode structure.
82  *
83  */
84 void
85 lgl_barcode_free (lglBarcode *bc)
86 {
87         GList *p;
88
89         if (bc != NULL)
90         {
91
92                 for (p = bc->shapes; p != NULL; p = p->next)
93                 {
94                         lgl_barcode_shape_free ((lglBarcodeShape *)p->data);
95                 }
96                 g_list_free (bc->shapes);
97
98                 g_free (bc);
99
100         }
101 }
102
103
104 /*****************************************************************************/
105 /**
106  * lgl_barcode_add_line:
107  * @bc:     An #lglBarcode structure
108  * @x:      x coordinate of top of line
109  * @y:      y coordinate of top of line
110  * @length: Length of line
111  * @width:  Width of line
112  *
113  * Add a vertical line to barcode.  Coordinates are relative to top left corner
114  * of barcode.  All units are in points ( 1 point = 1/72 inch ).
115  *
116  * <note><para>
117  *        This function is intended to be used internally by barcode implementations.
118  * </para></note>
119  *
120  */
121 void
122 lgl_barcode_add_line (lglBarcode      *bc,
123                       gdouble          x,
124                       gdouble          y,
125                       gdouble          length,
126                       gdouble          width)
127 {
128         lglBarcodeShapeLine *line_shape = g_new0 (lglBarcodeShapeLine, 1);
129         line_shape->type = LGL_BARCODE_SHAPE_LINE;
130
131         line_shape->x      = x;
132         line_shape->y      = y;
133         line_shape->length = length;
134         line_shape->width  = width;
135
136         lgl_barcode_add_shape (bc, (lglBarcodeShape *)line_shape);
137 }
138
139
140 /*****************************************************************************/
141 /**
142  * lgl_barcode_add_box:
143  * @bc:     An #lglBarcode structure
144  * @x:      x coordinate of top left corner of box
145  * @y:      y coordinate of top left corner of box
146  * @width:  Width of box
147  * @height: Height of box
148  *
149  * Add a box to barcode.  Coordinates are relative to top left corner
150  * of barcode.  All units are in points ( 1 point = 1/72 inch ).
151  *
152  * <note><para>
153  *        This function is intended to be used internally by barcode implementations.
154  * </para></note>
155  *
156  */
157 void
158 lgl_barcode_add_box (lglBarcode      *bc,
159                      gdouble          x,
160                      gdouble          y,
161                      gdouble          width,
162                      gdouble          height)
163 {
164         lglBarcodeShapeBox *box_shape = g_new0 (lglBarcodeShapeBox, 1);
165         box_shape->type = LGL_BARCODE_SHAPE_BOX;
166
167         box_shape->x      = x;
168         box_shape->y      = y;
169         box_shape->width  = width;
170         box_shape->height = height;
171
172         lgl_barcode_add_shape (bc, (lglBarcodeShape *)box_shape);
173 }
174
175
176 /*****************************************************************************/
177 /**
178  * lgl_barcode_add_char:
179  * @bc:     An #lglBarcode structure
180  * @x:      x coordinate of left baseline of character
181  * @y:      y coordinate of left baseline of character
182  * @fsize:  Font size
183  * @c:      Character to add
184  *
185  * Add an ASCII character to barcode.  Coordinates are relative to top left corner
186  * of barcode.  All units are in points ( 1 point = 1/72 inch ).
187  *
188  * <note><para>
189  *        This function is intended to be used internally by barcode implementations.
190  * </para></note>
191  *
192  */
193 void
194 lgl_barcode_add_char (lglBarcode      *bc,
195                       gdouble          x,
196                       gdouble          y,
197                       gdouble          fsize,
198                       gchar            c)
199 {
200         lglBarcodeShapeChar *char_shape = g_new0 (lglBarcodeShapeChar, 1);
201         char_shape->type = LGL_BARCODE_SHAPE_CHAR;
202
203         char_shape->x      = x;
204         char_shape->y      = y;
205         char_shape->fsize  = fsize;
206         char_shape->c      = c;
207
208         lgl_barcode_add_shape (bc, (lglBarcodeShape *)char_shape);
209 }
210
211
212 /*****************************************************************************/
213 /**
214  * lgl_barcode_add_string:
215  * @bc:     An #lglBarcode structure
216  * @x:      x coordinate of horizontal center of baseline of string
217  * @y:      y coordinate of horizontal center of baseline of string
218  * @fsize:  Font size
219  * @string: String to add
220  * @length: Number of bytes in string
221  *
222  * Add a character string to barcode.  Coordinates are relative to top left corner
223  * of barcode.  All units are in points ( 1 point = 1/72 inch ).
224  *
225  * <note><para>
226  *        This function is intended to be used internally by barcode implementations.
227  * </para></note>
228  *
229  */
230 void
231 lgl_barcode_add_string (lglBarcode      *bc,
232                         gdouble          x,
233                         gdouble          y,
234                         gdouble          fsize,
235                         gchar           *string,
236                         gsize            length)
237 {
238         lglBarcodeShapeString *string_shape = g_new0 (lglBarcodeShapeString, 1);
239         string_shape->type = LGL_BARCODE_SHAPE_STRING;
240
241         string_shape->x      = x;
242         string_shape->y      = y;
243         string_shape->fsize  = fsize;
244         string_shape->string = g_strndup(string, length);
245
246         lgl_barcode_add_shape (bc, (lglBarcodeShape *)string_shape);
247 }
248
249 /*****************************************************************************/
250 /**
251  * lgl_barcode_add_ring:
252  * @bc:         An #lglBarcode structure
253  * @x:          x coordinate of center of circle
254  * @y:          y coordinate of center of circle
255  * @radius:     Radius of ring (center of line)
256  * @line_width: Width of line
257  *
258  * Add a ring to barcode.  Coordinates are relative to top left corner
259  * of barcode.  All units are in points ( 1 point = 1/72 inch ).
260  *
261  * <note><para>
262  *        This function is intended to be used internally by barcode implementations.
263  * </para></note>
264  *
265  */
266 void
267 lgl_barcode_add_ring (lglBarcode      *bc,
268                       gdouble          x,
269                       gdouble          y,
270                       gdouble          radius,
271                       gdouble          line_width)
272 {
273         lglBarcodeShapeRing *ring_shape = g_new0 (lglBarcodeShapeRing, 1);
274         ring_shape->type = LGL_BARCODE_SHAPE_RING;
275
276         ring_shape->x          = x;
277         ring_shape->y          = y;
278         ring_shape->radius     = radius;
279         ring_shape->line_width = line_width;
280
281         lgl_barcode_add_shape (bc, (lglBarcodeShape *)ring_shape);
282 }
283
284 /*****************************************************************************/
285 /**
286  * lgl_barcode_add_hexagon:
287  * @bc:         An #lglBarcode structure
288  * @x:          x coordinate of top point of hexagon
289  * @y:          y coordinate of top point of hexagon
290  * @height:     Height of hexagon
291  *
292  * Add a regular hexagon (oriented with vertexes at top and bottom) to barcode.
293  * Coordinates are relative to top left corner of barcode.  All units are in
294  * points ( 1 point = 1/72 inch ).
295  *
296  * <note><para>
297  *        This function is intended to be used internally by barcode implementations.
298  * </para></note>
299  *
300  */
301 void
302 lgl_barcode_add_hexagon (lglBarcode      *bc,
303                          gdouble          x,
304                          gdouble          y,
305                          gdouble          height)
306 {
307         lglBarcodeShapeHexagon *hexagon_shape = g_new0 (lglBarcodeShapeHexagon, 1);
308         hexagon_shape->type = LGL_BARCODE_SHAPE_HEXAGON;
309
310         hexagon_shape->x      = x;
311         hexagon_shape->y      = y;
312         hexagon_shape->height = height;
313
314         lgl_barcode_add_shape (bc, (lglBarcodeShape *)hexagon_shape);
315 }
316
317
318 /*****************************************************************************/
319 /* Add shape to barcode.                                                     */
320 /*****************************************************************************/
321 static void
322 lgl_barcode_add_shape (lglBarcode      *bc,
323                        lglBarcodeShape *shape)
324 {
325         g_return_if_fail (bc);
326         g_return_if_fail (shape);
327
328         bc->shapes = g_list_prepend (bc->shapes, shape);
329 }
330
331
332 /*****************************************************************************/
333 /* Free a shape primitive.                                                   */
334 /*****************************************************************************/
335 static void
336 lgl_barcode_shape_free (lglBarcodeShape *shape)
337 {
338         switch (shape->type)
339         {
340
341         case LGL_BARCODE_SHAPE_STRING:
342                 g_free (shape->string.string);
343                 break;
344
345         default:
346                 break;
347         }
348
349         g_free (shape);
350 }
351
352
353
354
355 /*
356  * Local Variables:       -- emacs
357  * mode: C                -- emacs
358  * c-basic-offset: 8      -- emacs
359  * tab-width: 8           -- emacs
360  * indent-tabs-mode: nil  -- emacs
361  * End:                   -- emacs
362  */