]> git.sur5r.net Git - glabels/blob - src/bc.c
Fixed some gettext problems
[glabels] / src / bc.c
1 /*
2  *  bc.c
3  *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <config.h>
22
23 #include "bc.h"
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27
28 #include "bc-postnet.h"
29 #include "bc-gnubarcode.h"
30 #include "bc-iec16022.h"
31 #include "bc-iec18004.h"
32
33 #include "debug.h"
34
35
36 /*========================================================*/
37 /* Private macros and constants.                          */
38 /*========================================================*/
39
40
41 /*========================================================*/
42 /* Private types.                                         */
43 /*========================================================*/
44
45 typedef struct {
46         gchar            *id;
47         gchar            *name;
48         glBarcodeNewFunc  new;
49         gboolean          can_text;
50         gboolean          text_optional;
51         gboolean          can_checksum;
52         gboolean          checksum_optional;
53         gchar            *default_digits;
54         gboolean          can_freeform;
55         guint             prefered_n;
56 } Backend;
57
58
59 /*========================================================*/
60 /* Private globals.                                       */
61 /*========================================================*/
62
63 static const Backend backends[] = {
64
65         { "POSTNET", N_("POSTNET (any)"), gl_barcode_postnet_new,
66           FALSE, FALSE, TRUE, FALSE, "12345-6789-12", FALSE, 11},
67
68         { "POSTNET-5", N_("POSTNET-5 (ZIP only)"), gl_barcode_postnet_new,
69           FALSE, FALSE, TRUE, FALSE, "12345", FALSE, 5},
70
71         { "POSTNET-9", N_("POSTNET-9 (ZIP+4)"), gl_barcode_postnet_new,
72           FALSE, FALSE, TRUE, FALSE, "12345-6789", FALSE, 9},
73
74         { "POSTNET-11", N_("POSTNET-11 (DPBC)"), gl_barcode_postnet_new,
75           FALSE, FALSE, TRUE, FALSE, "12345-6789-12", FALSE, 11},
76
77         { "CEPNET", N_("CEPNET"), gl_barcode_postnet_new,
78           FALSE, FALSE, TRUE, FALSE, "12345-678", FALSE, 8},
79
80         { "EAN", N_("EAN (any)"), gl_barcode_gnubarcode_new,
81           TRUE, TRUE, TRUE, FALSE, "000000000000 00000", FALSE, 17},
82
83         { "EAN-8", N_("EAN-8"), gl_barcode_gnubarcode_new,
84           TRUE, TRUE, TRUE, FALSE, "0000000", FALSE, 7},
85
86         { "EAN-8+2", N_("EAN-8 +2"), gl_barcode_gnubarcode_new,
87           TRUE, TRUE, TRUE, FALSE, "0000000 00", FALSE, 9},
88
89         { "EAN-8+5", N_("EAN-8 +5"), gl_barcode_gnubarcode_new,
90           TRUE, TRUE, TRUE, FALSE, "0000000 00000", FALSE, 12},
91
92         { "EAN-13", N_("EAN-13"), gl_barcode_gnubarcode_new,
93           TRUE, TRUE, TRUE, FALSE, "000000000000", FALSE, 12},
94
95         { "EAN-13+2", N_("EAN-13 +2"), gl_barcode_gnubarcode_new,
96           TRUE, TRUE, TRUE, FALSE, "000000000000 00", FALSE, 14},
97
98         { "EAN-13+5", N_("EAN-13 +5"), gl_barcode_gnubarcode_new,
99           TRUE, TRUE, TRUE, FALSE, "000000000000 00000", FALSE, 17},
100
101         { "UPC", N_("UPC (UPC-A or UPC-E)"), gl_barcode_gnubarcode_new,
102           TRUE, TRUE, TRUE, FALSE, "00000000000 00000", FALSE, 16},
103
104         { "UPC-A", N_("UPC-A"), gl_barcode_gnubarcode_new,
105           TRUE, TRUE, TRUE, FALSE, "00000000000", FALSE, 11},
106
107         { "UPC-A+2", N_("UPC-A +2"), gl_barcode_gnubarcode_new,
108           TRUE, TRUE, TRUE, FALSE, "00000000000 00", FALSE, 13},
109
110         { "UPC-A+5", N_("UPC-A +5"), gl_barcode_gnubarcode_new,
111           TRUE, TRUE, TRUE, FALSE, "00000000000 00000", FALSE, 16},
112
113         { "UPC-E", N_("UPC-E"), gl_barcode_gnubarcode_new,
114           TRUE, TRUE, TRUE, FALSE, "000000", FALSE, 6},
115
116         { "UPC-E+2", N_("UPC-E +2"), gl_barcode_gnubarcode_new,
117           TRUE, TRUE, TRUE, FALSE, "000000 00", FALSE, 8},
118
119         { "UPC-E+5", N_("UPC-E +5"), gl_barcode_gnubarcode_new,
120           TRUE, TRUE, TRUE, FALSE, "000000 00000", FALSE, 11},
121
122         { "ISBN", N_("ISBN"), gl_barcode_gnubarcode_new,
123           TRUE, TRUE, TRUE, TRUE, "0-00000-000-0", FALSE, 10},
124
125         { "ISBN+5", N_("ISBN +5"), gl_barcode_gnubarcode_new,
126           TRUE, TRUE, TRUE, TRUE, "0-00000-000-0 00000", FALSE, 15},
127
128         { "Code39", N_("Code 39"), gl_barcode_gnubarcode_new,
129           TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
130
131         { "Code128", N_("Code 128"), gl_barcode_gnubarcode_new,
132           TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
133
134         { "Code128C", N_("Code 128C"), gl_barcode_gnubarcode_new,
135           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
136
137         { "Code128B", N_("Code 128B"), gl_barcode_gnubarcode_new,
138           TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
139
140         { "I25", N_("Interleaved 2 of 5"), gl_barcode_gnubarcode_new,
141           TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
142
143         { "CBR", N_("Codabar"), gl_barcode_gnubarcode_new,
144           TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
145
146         { "MSI", N_("MSI"), gl_barcode_gnubarcode_new,
147           TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
148
149         { "PLS", N_("Plessey"), gl_barcode_gnubarcode_new,
150           TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
151
152         { "IEC16022", N_("IEC16022 (DataMatrix)"), gl_barcode_iec16022_new,
153           FALSE, FALSE, TRUE, FALSE, "12345678", TRUE, 8},
154
155         { "IEC18004", N_("IEC18004 (QRCode)"), gl_barcode_iec18004_new,
156           FALSE, FALSE, TRUE, FALSE, "12345678", TRUE, 8},
157
158         { NULL, NULL, NULL, FALSE, FALSE, FALSE, FALSE, NULL, FALSE, 0}
159
160 };
161
162
163 /*========================================================*/
164 /* Private function prototypes.                           */
165 /*========================================================*/
166
167
168 /*---------------------------------------------------------------------------*/
169 /* Convert id to index into above table.                                     */
170 /*---------------------------------------------------------------------------*/
171 static gint
172 id_to_index (const gchar *id)
173 {
174         gint i;
175
176         if (id == 0) {
177                 return 0; /* NULL request default. I.e., the first element. */
178         }
179
180         for (i=0; backends[i].id != NULL; i++) {
181                 if (g_ascii_strcasecmp (id, backends[i].id) == 0) {
182                         return i;
183                 }
184         }
185
186         g_message( "Unknown barcode id \"%s\"", id );
187         return 0;
188 }
189
190
191 /*---------------------------------------------------------------------------*/
192 /* Convert name to index into above table.                                   */
193 /*---------------------------------------------------------------------------*/
194 static gint
195 name_to_index (const gchar *name)
196 {
197         gint i;
198
199         g_return_val_if_fail (name!=NULL, 0);
200
201         for (i=0; backends[i].id != NULL; i++) {
202                 if (strcmp (name, gettext (backends[i].name)) == 0) {
203                         return i;
204                 }
205         }
206
207         g_message( "Unknown barcode name \"%s\"", name );
208         return 0;
209 }
210
211
212 /*****************************************************************************/
213 /* Call appropriate barcode backend to create barcode in intermediate format.*/
214 /*****************************************************************************/
215 glBarcode *
216 gl_barcode_new (const gchar    *id,
217                 gboolean        text_flag,
218                 gboolean        checksum_flag,
219                 gdouble         w,
220                 gdouble         h,
221                 const gchar    *digits)
222 {
223         glBarcode *gbc;
224         gint       i;
225
226         g_return_val_if_fail (digits!=NULL, NULL);
227
228         i = id_to_index (id);
229         gbc = backends[i].new (backends[i].id,
230                                text_flag,
231                                checksum_flag,
232                                w,
233                                h,
234                                digits);
235
236         return gbc;
237 }
238
239
240 /*****************************************************************************/
241 /* Free previously created barcode.                                          */
242 /*****************************************************************************/
243 void
244 gl_barcode_free (glBarcode **gbc)
245 {
246         GList *p;
247
248         if (*gbc != NULL) {
249
250                 for (p = (*gbc)->lines; p != NULL; p = p->next) {
251                         g_free (p->data);
252                         p->data = NULL;
253                 }
254                 g_list_free ((*gbc)->lines);
255                 (*gbc)->lines = NULL;
256
257                 for (p = (*gbc)->chars; p != NULL; p = p->next) {
258                         g_free (p->data);
259                         p->data = NULL;
260                 }
261                 g_list_free ((*gbc)->chars);
262                 (*gbc)->chars = NULL;
263
264                 g_free (*gbc);
265                 *gbc = NULL;
266         }
267 }
268
269
270 /*****************************************************************************/
271 /* Get a list of names for valid barcode styles.                             */
272 /*****************************************************************************/
273 GList *
274 gl_barcode_get_styles_list  (void)
275 {
276         gint   i;
277         GList *list = NULL;
278
279         for (i=0; backends[i].id != NULL; i++) {
280                 list = g_list_append (list, g_strdup (gettext (backends[i].name)));
281         }
282
283         return list;
284 }
285
286
287 /*****************************************************************************/
288 /* Free up a previously allocated list of style names.                       */
289 /*****************************************************************************/
290 void
291 gl_barcode_free_styles_list (GList *styles_list)
292 {
293         GList *p;
294
295         for (p=styles_list; p != NULL; p=p->next) {
296                 g_free (p->data);
297                 p->data = NULL;
298         }
299
300         g_list_free (styles_list);
301 }
302
303
304 /*****************************************************************************/
305 /* Return an appropriate set of digits for the given barcode style.          */
306 /*****************************************************************************/
307 gchar *
308 gl_barcode_default_digits (const gchar *id,
309                            guint        n)
310 {
311         int i;
312
313         i = id_to_index (id);
314
315         if (backends[i].can_freeform) {
316
317                 return g_strnfill (MAX (n,1), '0');
318
319         } else {
320
321                 return g_strdup (backends[i].default_digits);
322
323         }
324 }
325
326
327 /*****************************************************************************/
328 /* Query text capabilities.                                                  */
329 /*****************************************************************************/
330 gboolean
331 gl_barcode_can_text (const gchar *id)
332 {
333         return backends[id_to_index (id)].can_text;
334 }
335
336
337 gboolean
338 gl_barcode_text_optional (const gchar *id)
339 {
340         return backends[id_to_index (id)].text_optional;
341 }
342
343
344 /*****************************************************************************/
345 /* Query checksum capabilities.                                              */
346 /*****************************************************************************/
347 gboolean
348 gl_barcode_can_csum (const gchar *id)
349 {
350         return backends[id_to_index (id)].can_checksum;
351 }
352
353
354 gboolean
355 gl_barcode_csum_optional (const gchar *id)
356 {
357         return backends[id_to_index (id)].checksum_optional;
358 }
359
360
361 /*****************************************************************************/
362 /* Query if freeform input is allowed.                                       */
363 /*****************************************************************************/
364 gboolean
365 gl_barcode_can_freeform     (const gchar    *id)
366 {
367         return backends[id_to_index (id)].can_freeform;
368 }
369
370
371 /*****************************************************************************/
372 /* Query prefered number of digits of input.                                 */
373 /*****************************************************************************/
374 guint
375 gl_barcode_get_prefered_n (const gchar    *id)
376 {
377         return backends[id_to_index (id)].prefered_n;
378 }
379
380
381 /*****************************************************************************/
382 /* Convert style to text.                                                    */
383 /*****************************************************************************/
384 const gchar *
385 gl_barcode_id_to_name (const gchar *id)
386 {
387         return gettext (backends[id_to_index (id)].name);
388 }
389
390
391 /*****************************************************************************/
392 /* Convert name to style.                                                    */
393 /*****************************************************************************/
394 const gchar *
395 gl_barcode_name_to_id (const gchar *name)
396 {
397         g_return_val_if_fail (name!=NULL, backends[0].id);
398
399         return backends[name_to_index (name)].id;
400 }
401
402
403
404 /*
405  * Local Variables:       -- emacs
406  * mode: C                -- emacs
407  * c-basic-offset: 8      -- emacs
408  * tab-width: 8           -- emacs
409  * indent-tabs-mode: nil  -- emacs
410  * End:                   -- emacs
411  */