]> git.sur5r.net Git - glabels/blob - glabels2/src/bc.c
Initial revision
[glabels] / glabels2 / src / bc.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  bc.c:  GLabels barcode module
5  *
6  *  Copyright (C) 2001-2002  Jim Evins <evins@snaught.com>.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22 #include <config.h>
23
24 #include "bc.h"
25 #include "bc-postnet.h"
26 #include "bc-gnubarcode.h"
27
28 #include "debug.h"
29 \f
30 /*****************************************************************************/
31 /* Call appropriate barcode backend to create barcode in intermediate format.*/
32 /*****************************************************************************/
33 glBarcode *
34 gl_barcode_new (glBarcodeStyle style,
35                 gboolean text_flag,
36                 gdouble scale,
37                 gchar * digits)
38 {
39         glBarcode *gbc;
40
41         switch (style) {
42
43         case GL_BARCODE_STYLE_POSTNET:
44                 /* Use the POSTNET backend module */
45                 gbc = gl_barcode_postnet_new (digits);
46                 break;
47
48         default:
49                 /* Use the GNU barcode library backend */
50                 gbc = gl_barcode_gnubarcode_new (style, text_flag, scale, digits);
51                 break;
52
53         }
54         return gbc;
55 }
56 \f
57 /*****************************************************************************/
58 /* Free previously created barcode.                                          */
59 /*****************************************************************************/
60 void
61 gl_barcode_free (glBarcode ** gbc)
62 {
63         GList *p;
64
65         if (*gbc != NULL) {
66
67                 for (p = (*gbc)->lines; p != NULL; p = p->next) {
68                         g_free (p->data);
69                         p->data = NULL;
70                 }
71                 g_list_free ((*gbc)->lines);
72                 (*gbc)->lines = NULL;
73
74                 for (p = (*gbc)->chars; p != NULL; p = p->next) {
75                         g_free (p->data);
76                         p->data = NULL;
77                 }
78                 g_list_free ((*gbc)->chars);
79                 (*gbc)->chars = NULL;
80
81                 g_free (*gbc);
82                 *gbc = NULL;
83         }
84 }
85 \f
86 /*****************************************************************************/
87 /* Return an appropriate set of digits for the given barcode style.          */
88 /*****************************************************************************/
89 gchar *
90 gl_barcode_default_digits (glBarcodeStyle style)
91 {
92         switch (style) {
93
94         case GL_BARCODE_STYLE_POSTNET:
95                 return g_strdup ("000000000");
96         case GL_BARCODE_STYLE_EAN:
97                 return g_strdup ("000000000000 00000");
98         case GL_BARCODE_STYLE_UPC:
99                 return g_strdup ("00000000000 00000");
100         case GL_BARCODE_STYLE_ISBN:
101                 return g_strdup ("0-00000-000-0 00000");
102         case GL_BARCODE_STYLE_39:
103         case GL_BARCODE_STYLE_128:
104         case GL_BARCODE_STYLE_128C:
105         case GL_BARCODE_STYLE_128B:
106         case GL_BARCODE_STYLE_I25:
107         case GL_BARCODE_STYLE_CBR:
108         case GL_BARCODE_STYLE_MSI:
109         case GL_BARCODE_STYLE_PLS:
110                 return g_strdup ("0000000000");
111         default:
112                 return g_strdup ("0");
113         }
114
115 }
116
117 /*****************************************************************************/
118 /* Convert style to text.                                                    */
119 /*****************************************************************************/
120 const gchar *
121 gl_barcode_style_to_text (glBarcodeStyle style)
122 {
123         switch (style) {
124         case GL_BARCODE_STYLE_POSTNET:
125                 return "POSTNET";
126         case GL_BARCODE_STYLE_EAN:
127                 return "EAN";
128         case GL_BARCODE_STYLE_UPC:
129                 return "UPC";
130         case GL_BARCODE_STYLE_ISBN:
131                 return "ISBN";
132         case GL_BARCODE_STYLE_39:
133                 return "Code39";
134         case GL_BARCODE_STYLE_128:
135                 return "Code128";
136         case GL_BARCODE_STYLE_128C:
137                 return "Code128C";
138         case GL_BARCODE_STYLE_128B:
139                 return "Code128B";
140         case GL_BARCODE_STYLE_I25:
141                 return "I25";
142         case GL_BARCODE_STYLE_CBR:
143                 return "CBR";
144         case GL_BARCODE_STYLE_MSI:
145                 return "MSI";
146         case GL_BARCODE_STYLE_PLS:
147                 return "PLS";
148         default:
149                 g_warning( "Illegal barcode style %d", style );
150                 return "?";
151         }
152 }
153
154 /*****************************************************************************/
155 /* Convert text to style.                                                    */
156 /*****************************************************************************/
157 glBarcodeStyle
158 gl_barcode_text_to_style (const gchar * text)
159 {
160
161         if (g_strcasecmp (text, "POSTNET") == 0) {
162                 return GL_BARCODE_STYLE_POSTNET;
163         }
164         if (g_strcasecmp (text, "EAN") == 0) {
165                 return GL_BARCODE_STYLE_EAN;
166         }
167         if (g_strcasecmp (text, "UPC") == 0) {
168                 return GL_BARCODE_STYLE_UPC;
169         }
170         if (g_strcasecmp (text, "ISBN") == 0) {
171                 return GL_BARCODE_STYLE_ISBN;
172         }
173         if (g_strcasecmp (text, "Code39") == 0) {
174                 return GL_BARCODE_STYLE_39;
175         }
176         if (g_strcasecmp (text, "Code128") == 0) {
177                 return GL_BARCODE_STYLE_128;
178         }
179         if (g_strcasecmp (text, "Code128C") == 0) {
180                 return GL_BARCODE_STYLE_128C;
181         }
182         if (g_strcasecmp (text, "Code128B") == 0) {
183                 return GL_BARCODE_STYLE_128B;
184         }
185         if (g_strcasecmp (text, "I25") == 0) {
186                 return GL_BARCODE_STYLE_I25;
187         }
188         if (g_strcasecmp (text, "CBR") == 0) {
189                 return GL_BARCODE_STYLE_CBR;
190         }
191         if (g_strcasecmp (text, "MSI") == 0) {
192                 return GL_BARCODE_STYLE_MSI;
193         }
194         if (g_strcasecmp (text, "PLS") == 0) {
195                 return GL_BARCODE_STYLE_PLS;
196         } else {
197                 g_warning( "Unknown barcode style text \"%s\"", text );
198                 return GL_BARCODE_STYLE_POSTNET;
199         }
200
201 }