3 * Copyright (C) 2001-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
7 * gLabels is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU 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.
12 * gLabels 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 General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with gLabels. If not, see <http://www.gnu.org/licenses/>.
26 #include <glib/gi18n.h>
28 #include "bc-postnet.h"
29 #include "bc-gnubarcode.h"
30 #include "bc-iec16022.h"
31 #include "bc-iec18004.h"
36 /*========================================================*/
37 /* Private macros and constants. */
38 /*========================================================*/
41 /*========================================================*/
43 /*========================================================*/
50 gboolean text_optional;
51 gboolean can_checksum;
52 gboolean checksum_optional;
53 gchar *default_digits;
54 gboolean can_freeform;
59 /*========================================================*/
60 /* Private globals. */
61 /*========================================================*/
63 static const Backend backends[] = {
65 { "POSTNET", N_("POSTNET (any)"), gl_barcode_postnet_new,
66 FALSE, FALSE, TRUE, FALSE, "12345-6789-12", FALSE, 11},
68 { "POSTNET-5", N_("POSTNET-5 (ZIP only)"), gl_barcode_postnet_new,
69 FALSE, FALSE, TRUE, FALSE, "12345", FALSE, 5},
71 { "POSTNET-9", N_("POSTNET-9 (ZIP+4)"), gl_barcode_postnet_new,
72 FALSE, FALSE, TRUE, FALSE, "12345-6789", FALSE, 9},
74 { "POSTNET-11", N_("POSTNET-11 (DPBC)"), gl_barcode_postnet_new,
75 FALSE, FALSE, TRUE, FALSE, "12345-6789-12", FALSE, 11},
77 { "CEPNET", N_("CEPNET"), gl_barcode_postnet_new,
78 FALSE, FALSE, TRUE, FALSE, "12345-678", FALSE, 8},
80 { "EAN", N_("EAN (any)"), gl_barcode_gnubarcode_new,
81 TRUE, TRUE, TRUE, FALSE, "000000000000 00000", FALSE, 17},
83 { "EAN-8", N_("EAN-8"), gl_barcode_gnubarcode_new,
84 TRUE, TRUE, TRUE, FALSE, "0000000", FALSE, 7},
86 { "EAN-8+2", N_("EAN-8 +2"), gl_barcode_gnubarcode_new,
87 TRUE, TRUE, TRUE, FALSE, "0000000 00", FALSE, 9},
89 { "EAN-8+5", N_("EAN-8 +5"), gl_barcode_gnubarcode_new,
90 TRUE, TRUE, TRUE, FALSE, "0000000 00000", FALSE, 12},
92 { "EAN-13", N_("EAN-13"), gl_barcode_gnubarcode_new,
93 TRUE, TRUE, TRUE, FALSE, "000000000000", FALSE, 12},
95 { "EAN-13+2", N_("EAN-13 +2"), gl_barcode_gnubarcode_new,
96 TRUE, TRUE, TRUE, FALSE, "000000000000 00", FALSE, 14},
98 { "EAN-13+5", N_("EAN-13 +5"), gl_barcode_gnubarcode_new,
99 TRUE, TRUE, TRUE, FALSE, "000000000000 00000", FALSE, 17},
101 { "UPC", N_("UPC (UPC-A or UPC-E)"), gl_barcode_gnubarcode_new,
102 TRUE, TRUE, TRUE, FALSE, "00000000000 00000", FALSE, 16},
104 { "UPC-A", N_("UPC-A"), gl_barcode_gnubarcode_new,
105 TRUE, TRUE, TRUE, FALSE, "00000000000", FALSE, 11},
107 { "UPC-A+2", N_("UPC-A +2"), gl_barcode_gnubarcode_new,
108 TRUE, TRUE, TRUE, FALSE, "00000000000 00", FALSE, 13},
110 { "UPC-A+5", N_("UPC-A +5"), gl_barcode_gnubarcode_new,
111 TRUE, TRUE, TRUE, FALSE, "00000000000 00000", FALSE, 16},
113 { "UPC-E", N_("UPC-E"), gl_barcode_gnubarcode_new,
114 TRUE, TRUE, TRUE, FALSE, "000000", FALSE, 6},
116 { "UPC-E+2", N_("UPC-E +2"), gl_barcode_gnubarcode_new,
117 TRUE, TRUE, TRUE, FALSE, "000000 00", FALSE, 8},
119 { "UPC-E+5", N_("UPC-E +5"), gl_barcode_gnubarcode_new,
120 TRUE, TRUE, TRUE, FALSE, "000000 00000", FALSE, 11},
122 { "ISBN", N_("ISBN"), gl_barcode_gnubarcode_new,
123 TRUE, TRUE, TRUE, TRUE, "0-00000-000-0", FALSE, 10},
125 { "ISBN+5", N_("ISBN +5"), gl_barcode_gnubarcode_new,
126 TRUE, TRUE, TRUE, TRUE, "0-00000-000-0 00000", FALSE, 15},
128 { "Code39", N_("Code 39"), gl_barcode_gnubarcode_new,
129 TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
131 { "Code128", N_("Code 128"), gl_barcode_gnubarcode_new,
132 TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
134 { "Code128C", N_("Code 128C"), gl_barcode_gnubarcode_new,
135 TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
137 { "Code128B", N_("Code 128B"), gl_barcode_gnubarcode_new,
138 TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
140 { "I25", N_("Interleaved 2 of 5"), gl_barcode_gnubarcode_new,
141 TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
143 { "CBR", N_("Codabar"), gl_barcode_gnubarcode_new,
144 TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
146 { "MSI", N_("MSI"), gl_barcode_gnubarcode_new,
147 TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
149 { "PLS", N_("Plessey"), gl_barcode_gnubarcode_new,
150 TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
152 { "IEC16022", N_("IEC16022 (DataMatrix)"), gl_barcode_iec16022_new,
153 FALSE, FALSE, TRUE, FALSE, "12345678", TRUE, 8},
155 { "IEC18004", N_("IEC18004 (QRCode)"), gl_barcode_iec18004_new,
156 FALSE, FALSE, TRUE, FALSE, "12345678", TRUE, 8},
158 { NULL, NULL, NULL, FALSE, FALSE, FALSE, FALSE, NULL, FALSE, 0}
163 /*========================================================*/
164 /* Private function prototypes. */
165 /*========================================================*/
168 /*---------------------------------------------------------------------------*/
169 /* Convert id to index into above table. */
170 /*---------------------------------------------------------------------------*/
172 id_to_index (const gchar *id)
177 return 0; /* NULL request default. I.e., the first element. */
180 for (i=0; backends[i].id != NULL; i++) {
181 if (g_ascii_strcasecmp (id, backends[i].id) == 0) {
186 g_message( "Unknown barcode id \"%s\"", id );
191 /*---------------------------------------------------------------------------*/
192 /* Convert name to index into above table. */
193 /*---------------------------------------------------------------------------*/
195 name_to_index (const gchar *name)
199 g_return_val_if_fail (name!=NULL, 0);
201 for (i=0; backends[i].id != NULL; i++) {
202 if (strcmp (name, gettext (backends[i].name)) == 0) {
207 g_message( "Unknown barcode name \"%s\"", name );
212 /*****************************************************************************/
213 /* Call appropriate barcode backend to create barcode in intermediate format.*/
214 /*****************************************************************************/
216 gl_barcode_new (const gchar *id,
218 gboolean checksum_flag,
226 g_return_val_if_fail (digits!=NULL, NULL);
228 i = id_to_index (id);
229 gbc = backends[i].new (backends[i].id,
240 /*****************************************************************************/
241 /* Free previously created barcode. */
242 /*****************************************************************************/
244 gl_barcode_free (glBarcode **gbc)
250 for (p = (*gbc)->lines; p != NULL; p = p->next) {
254 g_list_free ((*gbc)->lines);
255 (*gbc)->lines = NULL;
257 for (p = (*gbc)->chars; p != NULL; p = p->next) {
261 g_list_free ((*gbc)->chars);
262 (*gbc)->chars = NULL;
270 /*****************************************************************************/
271 /* Get a list of names for valid barcode styles. */
272 /*****************************************************************************/
274 gl_barcode_get_styles_list (void)
279 for (i=0; backends[i].id != NULL; i++) {
280 list = g_list_append (list, g_strdup (gettext (backends[i].name)));
287 /*****************************************************************************/
288 /* Free up a previously allocated list of style names. */
289 /*****************************************************************************/
291 gl_barcode_free_styles_list (GList *styles_list)
295 for (p=styles_list; p != NULL; p=p->next) {
300 g_list_free (styles_list);
304 /*****************************************************************************/
305 /* Return an appropriate set of digits for the given barcode style. */
306 /*****************************************************************************/
308 gl_barcode_default_digits (const gchar *id,
313 i = id_to_index (id);
315 if (backends[i].can_freeform) {
317 return g_strnfill (MAX (n,1), '0');
321 return g_strdup (backends[i].default_digits);
327 /*****************************************************************************/
328 /* Query text capabilities. */
329 /*****************************************************************************/
331 gl_barcode_can_text (const gchar *id)
333 return backends[id_to_index (id)].can_text;
338 gl_barcode_text_optional (const gchar *id)
340 return backends[id_to_index (id)].text_optional;
344 /*****************************************************************************/
345 /* Query checksum capabilities. */
346 /*****************************************************************************/
348 gl_barcode_can_csum (const gchar *id)
350 return backends[id_to_index (id)].can_checksum;
355 gl_barcode_csum_optional (const gchar *id)
357 return backends[id_to_index (id)].checksum_optional;
361 /*****************************************************************************/
362 /* Query if freeform input is allowed. */
363 /*****************************************************************************/
365 gl_barcode_can_freeform (const gchar *id)
367 return backends[id_to_index (id)].can_freeform;
371 /*****************************************************************************/
372 /* Query prefered number of digits of input. */
373 /*****************************************************************************/
375 gl_barcode_get_prefered_n (const gchar *id)
377 return backends[id_to_index (id)].prefered_n;
381 /*****************************************************************************/
382 /* Convert style to text. */
383 /*****************************************************************************/
385 gl_barcode_id_to_name (const gchar *id)
387 return gettext (backends[id_to_index (id)].name);
391 /*****************************************************************************/
392 /* Convert name to style. */
393 /*****************************************************************************/
395 gl_barcode_name_to_id (const gchar *name)
397 g_return_val_if_fail (name!=NULL, backends[0].id);
399 return backends[name_to_index (name)].id;
405 * Local Variables: -- emacs
407 * c-basic-offset: 8 -- emacs
408 * tab-width: 8 -- emacs
409 * indent-tabs-mode: nil -- emacs