2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * bc.c: GLabels barcode module
6 * Copyright (C) 2001-2003 Jim Evins <evins@snaught.com>.
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.
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.
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
24 #include <libgnome/libgnome.h>
27 #include "bc-postnet.h"
28 #include "bc-gnubarcode.h"
32 /*========================================================*/
33 /* Private macros and constants. */
34 /*========================================================*/
36 /*========================================================*/
38 /*========================================================*/
45 gboolean text_optional;
46 gboolean can_checksum;
47 gboolean checksum_optional;
48 gchar *default_digits;
52 /*========================================================*/
53 /* Private globals. */
54 /*========================================================*/
56 Backend backends[] = {
58 { "POSTNET", N_("POSTNET (any)"), gl_barcode_postnet_new,
59 FALSE, FALSE, TRUE, FALSE, "000000000"},
61 { "POSTNET-5", N_("POSTNET-5 (ZIP only)"), gl_barcode_postnet_new,
62 FALSE, FALSE, TRUE, FALSE, "00000"},
64 { "POSTNET-9", N_("POSTNET-9 (ZIP+4)"), gl_barcode_postnet_new,
65 FALSE, FALSE, TRUE, FALSE, "000000000"},
67 { "POSTNET-11", N_("POSTNET-11 (DPBC)"), gl_barcode_postnet_new,
68 FALSE, FALSE, TRUE, FALSE, "00000000000"},
70 { "EAN", N_("EAN (any)"), gl_barcode_gnubarcode_new,
71 TRUE, TRUE, TRUE, FALSE, "000000000000 00000"},
73 { "EAN-8", N_("EAN-8"), gl_barcode_gnubarcode_new,
74 TRUE, TRUE, TRUE, FALSE, "0000000"},
76 { "EAN-8+2", N_("EAN-8 +2"), gl_barcode_gnubarcode_new,
77 TRUE, TRUE, TRUE, FALSE, "0000000 00"},
79 { "EAN-8+5", N_("EAN-8 +5"), gl_barcode_gnubarcode_new,
80 TRUE, TRUE, TRUE, FALSE, "0000000 00000"},
82 { "EAN-13", N_("EAN-13"), gl_barcode_gnubarcode_new,
83 TRUE, TRUE, TRUE, FALSE, "000000000000"},
85 { "EAN-13+2", N_("EAN-13 +2"), gl_barcode_gnubarcode_new,
86 TRUE, TRUE, TRUE, FALSE, "000000000000 00"},
88 { "EAN-13+5", N_("EAN-13 +5"), gl_barcode_gnubarcode_new,
89 TRUE, TRUE, TRUE, FALSE, "000000000000 00000"},
91 { "UPC", N_("UPC (UPC-A or UPC-E)"), gl_barcode_gnubarcode_new,
92 TRUE, TRUE, TRUE, FALSE, "00000000000 00000"},
94 { "UPC-A", N_("UPC-A"), gl_barcode_gnubarcode_new,
95 TRUE, TRUE, TRUE, FALSE, "00000000000"},
97 { "UPC-A+2", N_("UPC-A +2"), gl_barcode_gnubarcode_new,
98 TRUE, TRUE, TRUE, FALSE, "00000000000 00"},
100 { "UPC-A+5", N_("UPC-A +5"), gl_barcode_gnubarcode_new,
101 TRUE, TRUE, TRUE, FALSE, "00000000000 00000"},
103 { "UPC-E", N_("UPC-E"), gl_barcode_gnubarcode_new,
104 TRUE, TRUE, TRUE, FALSE, "000000"},
106 { "UPC-E+2", N_("UPC-E +2"), gl_barcode_gnubarcode_new,
107 TRUE, TRUE, TRUE, FALSE, "000000 00"},
109 { "UPC-E+5", N_("UPC-E +5"), gl_barcode_gnubarcode_new,
110 TRUE, TRUE, TRUE, FALSE, "000000 00000"},
112 { "ISBN", N_("ISBN"), gl_barcode_gnubarcode_new,
113 TRUE, TRUE, TRUE, TRUE, "0-00000-000-0"},
115 { "ISBN+5", N_("ISBN +5"), gl_barcode_gnubarcode_new,
116 TRUE, TRUE, TRUE, TRUE, "0-00000-000-0 00000"},
118 { "Code39", N_("Code 39"), gl_barcode_gnubarcode_new,
119 TRUE, TRUE, TRUE, TRUE, "0000000000"},
121 { "Code128", N_("Code 128"), gl_barcode_gnubarcode_new,
122 TRUE, TRUE, TRUE, TRUE, "0000000000"},
124 { "Code128C", N_("Code 128C"), gl_barcode_gnubarcode_new,
125 TRUE, TRUE, TRUE, TRUE, "0000000000"},
127 { "Code128B", N_("Code 128B"), gl_barcode_gnubarcode_new,
128 TRUE, TRUE, TRUE, TRUE, "0000000000"},
130 { "I25", N_("Interleaved 2 of 5"), gl_barcode_gnubarcode_new,
131 TRUE, TRUE, TRUE, TRUE, "0000000000"},
133 { "CBR", N_("Codabar"), gl_barcode_gnubarcode_new,
134 TRUE, TRUE, TRUE, TRUE, "0000000000"},
136 { "MSI", N_("MSI"), gl_barcode_gnubarcode_new,
137 TRUE, TRUE, TRUE, TRUE, "0000000000"},
139 { "PLS", N_("Plessey"), gl_barcode_gnubarcode_new,
140 TRUE, TRUE, TRUE, TRUE, "0000000000"},
142 { NULL, NULL, NULL, FALSE, FALSE, FALSE, FALSE, NULL}
146 /*========================================================*/
147 /* Private function prototypes. */
148 /*========================================================*/
151 /*---------------------------------------------------------------------------*/
152 /* Convert id to index into above table. */
153 /*---------------------------------------------------------------------------*/
155 id_to_index (const gchar *id)
160 return 0; /* NULL request default. I.e., the first element. */
163 for (i=0; backends[i].id != NULL; i++) {
164 if (g_strcasecmp (id, backends[i].id) == 0) {
169 g_warning( "Unknown barcode id \"%s\"", id );
173 /*---------------------------------------------------------------------------*/
174 /* Convert name to index into above table. */
175 /*---------------------------------------------------------------------------*/
177 name_to_index (const gchar *name)
181 g_return_val_if_fail (name!=NULL, 0);
183 for (i=0; backends[i].id != NULL; i++) {
184 if (g_strcasecmp (name, backends[i].name) == 0) {
189 g_warning( "Unknown barcode name \"%s\"", name );
193 /*****************************************************************************/
194 /* Call appropriate barcode backend to create barcode in intermediate format.*/
195 /*****************************************************************************/
197 gl_barcode_new (const gchar *id,
199 gboolean checksum_flag,
207 g_return_val_if_fail (digits!=NULL, NULL);
209 i = id_to_index (id);
210 gbc = backends[i].new (backends[i].id,
220 /*****************************************************************************/
221 /* Free previously created barcode. */
222 /*****************************************************************************/
224 gl_barcode_free (glBarcode **gbc)
230 for (p = (*gbc)->lines; p != NULL; p = p->next) {
234 g_list_free ((*gbc)->lines);
235 (*gbc)->lines = NULL;
237 for (p = (*gbc)->chars; p != NULL; p = p->next) {
241 g_list_free ((*gbc)->chars);
242 (*gbc)->chars = NULL;
249 /*****************************************************************************/
250 /* Get a list of names for valid barcode styles. */
251 /*****************************************************************************/
253 gl_barcode_get_styles_list (void)
258 for (i=0; backends[i].id != NULL; i++) {
259 list = g_list_append (list, g_strdup (backends[i].name));
265 /*****************************************************************************/
266 /* Free up a previously allocated list of style names. */
267 /*****************************************************************************/
269 gl_barcode_free_styles_list (GList *styles_list)
273 for (p=styles_list; p != NULL; p=p->next) {
278 g_list_free (styles_list);
282 /*****************************************************************************/
283 /* Return an appropriate set of digits for the given barcode style. */
284 /*****************************************************************************/
286 gl_barcode_default_digits (const gchar *id)
288 return g_strdup (backends[id_to_index (id)].default_digits);
291 /*****************************************************************************/
292 /* Query text capabilities. */
293 /*****************************************************************************/
295 gl_barcode_can_text (const gchar *id)
297 return backends[id_to_index (id)].can_text;
301 gl_barcode_text_optional (const gchar *id)
303 return backends[id_to_index (id)].text_optional;
306 /*****************************************************************************/
307 /* Query checksum capabilities. */
308 /*****************************************************************************/
310 gl_barcode_can_csum (const gchar *id)
312 return backends[id_to_index (id)].can_checksum;
316 gl_barcode_csum_optional (const gchar *id)
318 return backends[id_to_index (id)].checksum_optional;
321 /*****************************************************************************/
322 /* Convert style to text. */
323 /*****************************************************************************/
325 gl_barcode_id_to_name (const gchar *id)
327 return backends[id_to_index (id)].name;
330 /*****************************************************************************/
331 /* Convert name to style. */
332 /*****************************************************************************/
334 gl_barcode_name_to_id (const gchar *name)
336 g_return_val_if_fail (name!=NULL, backends[0].id);
338 return backends[name_to_index (name)].id;