]> git.sur5r.net Git - glabels/blob - src/bc-backends.c
Imported Upstream version 3.0.0
[glabels] / src / bc-backends.c
1 /*
2  *  bc-backends.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-backends.h"
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27
28 #include "bc-builtin.h"
29 #include "bc-gnubarcode.h"
30 #include "bc-zint.h"
31 #include "bc-iec16022.h"
32 #include "bc-iec18004.h"
33
34 #include "debug.h"
35
36
37 /*========================================================*/
38 /* Private macros and constants.                          */
39 /*========================================================*/
40
41
42 /*========================================================*/
43 /* Private types.                                         */
44 /*========================================================*/
45
46 typedef lglBarcode *(*glBarcodeNewFunc) (const gchar    *id,
47                                          gboolean        text_flag,
48                                          gboolean        checksum_flag,
49                                          gdouble         w,
50                                          gdouble         h,
51                                          const gchar    *digits);
52
53
54 typedef struct {
55         gchar            *id;
56         gchar            *name;
57 } Backend;
58
59
60 typedef struct {
61         gchar            *backend_id;
62         gchar            *id;
63         gchar            *name;
64         glBarcodeNewFunc  new_barcode;
65         gboolean          can_text;
66         gboolean          text_optional;
67         gboolean          can_checksum;
68         gboolean          checksum_optional;
69         gchar            *default_digits;
70         gboolean          can_freeform;
71         guint             prefered_n;
72 } Style;
73
74
75 /*========================================================*/
76 /* Private globals.                                       */
77 /*========================================================*/
78
79 static const Backend backends[] = {
80
81         { "built-in",    N_("Built-in") },
82 #ifdef HAVE_LIBBARCODE
83         { "gnu-barcode", "GNU Barcode" },
84 #endif
85 #ifdef HAVE_LIBZINT
86         { "zint",        "Zint" },
87 #endif
88 #ifdef HAVE_LIBIEC16022
89         { "libiec16022", "IEC16022" },
90 #endif
91 #ifdef HAVE_LIBQRENCODE
92         { "libqrencode", "QREncode" },
93 #endif
94
95         { NULL, NULL }
96 };
97
98
99 static const Style styles[] = {
100
101         { "built-in", "POSTNET", N_("POSTNET (any)"), gl_barcode_builtin_new,
102           FALSE, FALSE, TRUE, FALSE, "12345-6789-12", FALSE, 11},
103
104         { "built-in", "POSTNET-5", N_("POSTNET-5 (ZIP only)"), gl_barcode_builtin_new,
105           FALSE, FALSE, TRUE, FALSE, "12345", FALSE, 5},
106
107         { "built-in", "POSTNET-9", N_("POSTNET-9 (ZIP+4)"), gl_barcode_builtin_new,
108           FALSE, FALSE, TRUE, FALSE, "12345-6789", FALSE, 9},
109
110         { "built-in", "POSTNET-11", N_("POSTNET-11 (DPBC)"), gl_barcode_builtin_new,
111           FALSE, FALSE, TRUE, FALSE, "12345-6789-12", FALSE, 11},
112
113         { "built-in", "CEPNET", N_("CEPNET"), gl_barcode_builtin_new,
114           FALSE, FALSE, TRUE, FALSE, "12345-678", FALSE, 8},
115
116         { "built-in", "ONECODE", N_("One Code"), gl_barcode_builtin_new,
117           FALSE, FALSE, TRUE, FALSE, "12345678901234567890", FALSE, 20},
118
119         { "built-in", "Code39", N_("Code 39"), gl_barcode_builtin_new,
120           TRUE, TRUE, TRUE, TRUE, "1234567890", TRUE, 10},
121
122         { "built-in", "Code39Ext", N_("Code 39 Extended"), gl_barcode_builtin_new,
123           TRUE, TRUE, TRUE, TRUE, "1234567890", TRUE, 10},
124
125 #ifdef HAVE_LIBBARCODE
126
127         { "gnu-barcode", "EAN", N_("EAN (any)"), gl_barcode_gnubarcode_new,
128           TRUE, TRUE, TRUE, FALSE, "000000000000 00000", FALSE, 17},
129
130         { "gnu-barcode", "EAN-8", N_("EAN-8"), gl_barcode_gnubarcode_new,
131           TRUE, TRUE, TRUE, FALSE, "0000000", FALSE, 7},
132
133         { "gnu-barcode", "EAN-8+2", N_("EAN-8 +2"), gl_barcode_gnubarcode_new,
134           TRUE, TRUE, TRUE, FALSE, "0000000 00", FALSE, 9},
135
136         { "gnu-barcode", "EAN-8+5", N_("EAN-8 +5"), gl_barcode_gnubarcode_new,
137           TRUE, TRUE, TRUE, FALSE, "0000000 00000", FALSE, 12},
138
139         { "gnu-barcode", "EAN-13", N_("EAN-13"), gl_barcode_gnubarcode_new,
140           TRUE, TRUE, TRUE, FALSE, "000000000000", FALSE, 12},
141
142         { "gnu-barcode", "EAN-13+2", N_("EAN-13 +2"), gl_barcode_gnubarcode_new,
143           TRUE, TRUE, TRUE, FALSE, "000000000000 00", FALSE, 14},
144
145         { "gnu-barcode", "EAN-13+5", N_("EAN-13 +5"), gl_barcode_gnubarcode_new,
146           TRUE, TRUE, TRUE, FALSE, "000000000000 00000", FALSE, 17},
147
148         { "gnu-barcode", "UPC", N_("UPC (UPC-A or UPC-E)"), gl_barcode_gnubarcode_new,
149           TRUE, TRUE, TRUE, FALSE, "00000000000 00000", FALSE, 16},
150
151         { "gnu-barcode", "UPC-A", N_("UPC-A"), gl_barcode_gnubarcode_new,
152           TRUE, TRUE, TRUE, FALSE, "00000000000", FALSE, 11},
153
154         { "gnu-barcode", "UPC-A+2", N_("UPC-A +2"), gl_barcode_gnubarcode_new,
155           TRUE, TRUE, TRUE, FALSE, "00000000000 00", FALSE, 13},
156
157         { "gnu-barcode", "UPC-A+5", N_("UPC-A +5"), gl_barcode_gnubarcode_new,
158           TRUE, TRUE, TRUE, FALSE, "00000000000 00000", FALSE, 16},
159
160         { "gnu-barcode", "UPC-E", N_("UPC-E"), gl_barcode_gnubarcode_new,
161           TRUE, TRUE, TRUE, FALSE, "000000", FALSE, 6},
162
163         { "gnu-barcode", "UPC-E+2", N_("UPC-E +2"), gl_barcode_gnubarcode_new,
164           TRUE, TRUE, TRUE, FALSE, "000000 00", FALSE, 8},
165
166         { "gnu-barcode", "UPC-E+5", N_("UPC-E +5"), gl_barcode_gnubarcode_new,
167           TRUE, TRUE, TRUE, FALSE, "000000 00000", FALSE, 11},
168
169         { "gnu-barcode", "ISBN", N_("ISBN"), gl_barcode_gnubarcode_new,
170           TRUE, TRUE, TRUE, TRUE, "0-00000-000-0", FALSE, 10},
171
172         { "gnu-barcode", "ISBN+5", N_("ISBN +5"), gl_barcode_gnubarcode_new,
173           TRUE, TRUE, TRUE, TRUE, "0-00000-000-0 00000", FALSE, 15},
174
175         { "gnu-barcode", "Code39", N_("Code 39"), gl_barcode_gnubarcode_new,
176           TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
177
178         { "gnu-barcode", "Code128", N_("Code 128"), gl_barcode_gnubarcode_new,
179           TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
180
181         { "gnu-barcode", "Code128C", N_("Code 128C"), gl_barcode_gnubarcode_new,
182           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
183
184         { "gnu-barcode", "Code128B", N_("Code 128B"), gl_barcode_gnubarcode_new,
185           TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
186
187         { "gnu-barcode", "I25", N_("Interleaved 2 of 5"), gl_barcode_gnubarcode_new,
188           TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
189
190         { "gnu-barcode", "CBR", N_("Codabar"), gl_barcode_gnubarcode_new,
191           TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
192
193         { "gnu-barcode", "MSI", N_("MSI"), gl_barcode_gnubarcode_new,
194           TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
195
196         { "gnu-barcode", "PLS", N_("Plessey"), gl_barcode_gnubarcode_new,
197           TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},
198
199         { "gnu-barcode", "Code93", N_("Code 93"), gl_barcode_gnubarcode_new,
200           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
201
202 #endif /* HAVE_LIBBARCODE */
203
204 #ifdef HAVE_LIBZINT
205
206         { "zint", "AUSP", N_("Australia Post Standard"), gl_barcode_zint_new,
207           FALSE, FALSE, TRUE, FALSE, "12345678901234567890123", TRUE, 23},
208
209         { "zint", "AUSRP", N_("Australia Post Reply Paid"), gl_barcode_zint_new,
210           FALSE, FALSE, TRUE, FALSE, "12345678", TRUE, 8},
211
212         { "zint", "AUSRT", N_("Australia Post Route Code"), gl_barcode_zint_new,
213           FALSE, FALSE, TRUE, FALSE, "12345678", TRUE, 8},
214
215         { "zint", "AUSRD", N_("Australia Post Redirect"), gl_barcode_zint_new,
216           FALSE, FALSE, TRUE, FALSE, "12345678", TRUE, 8},
217
218         { "zint", "AZTEC", N_("Aztec Code"), gl_barcode_zint_new,
219           FALSE, FALSE, TRUE, FALSE, "1234567890", TRUE, 10},
220           
221         { "zint", "AZRUN", N_("Aztec Rune"), gl_barcode_zint_new,
222           FALSE, FALSE, TRUE, FALSE, "255", TRUE, 3},
223
224         { "zint", "CBR", N_("Codabar"), gl_barcode_zint_new,
225           TRUE, TRUE, TRUE, FALSE, "ABCDABCDAB", TRUE, 10},
226
227         { "zint", "Code1", N_("Code One"),  gl_barcode_zint_new,
228           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
229
230         { "zint", "Code11", N_("Code 11"), gl_barcode_zint_new,
231           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
232           
233         { "zint", "C16K", N_("Code 16K"), gl_barcode_zint_new,
234           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
235           
236         { "zint", "C25M", N_("Code 2 of 5 Matrix"),  gl_barcode_zint_new,
237           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
238           
239         { "zint", "C25I", N_("Code 2 of 5 IATA"),  gl_barcode_zint_new,
240           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
241           
242         { "zint", "C25DL", N_("Code 2 of 5 Data Logic"),  gl_barcode_zint_new,
243           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
244
245         { "zint", "Code32", N_("Code 32 (Italian Pharmacode)"),  gl_barcode_zint_new,
246           TRUE, TRUE, TRUE, FALSE, "12345678", TRUE, 8},
247
248         { "zint", "Code39", N_("Code 39"), gl_barcode_zint_new,
249           TRUE, TRUE, FALSE, FALSE, "0000000000", TRUE, 10},
250           
251         { "zint", "Code39E", N_("Code 39 Extended"),  gl_barcode_zint_new,
252           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
253
254         { "zint", "Code49", N_("Code 49"), gl_barcode_zint_new,
255           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
256
257         { "zint", "Code93", N_("Code 93"), gl_barcode_zint_new,
258           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
259
260         { "zint", "Code128", N_("Code 128"), gl_barcode_zint_new,
261           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
262           
263         { "zint", "Code128B", N_("Code 128 (Mode C supression)"), gl_barcode_zint_new,
264           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
265           
266         { "zint", "DAFT", N_("DAFT Code"), gl_barcode_zint_new,
267           FALSE, FALSE, FALSE, FALSE, "DAFTDAFTDAFTDAFT", TRUE, 16},
268
269         { "zint", "DMTX", N_("Data Matrix"), gl_barcode_zint_new,
270           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
271
272         { "zint", "DPL", N_("Deutsche Post Leitcode"), gl_barcode_zint_new,
273           TRUE, TRUE, TRUE, FALSE, "1234567890123", TRUE, 13},
274           
275         { "zint", "DPI", N_("Deutsche Post Identcode"), gl_barcode_zint_new,
276           TRUE, TRUE, TRUE, FALSE, "12345678901", TRUE, 11},
277           
278         { "zint", "KIX", N_("Dutch Post KIX Code"), gl_barcode_zint_new,
279           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
280
281         { "zint", "EAN", N_("EAN"), gl_barcode_zint_new,
282           TRUE, TRUE, TRUE, FALSE, "1234567890123", FALSE, 13},
283
284         { "zint", "GMTX", N_("Grid Matrix"),  gl_barcode_zint_new,
285           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
286
287         { "zint", "GS1-128", N_("GS1-128"), gl_barcode_zint_new,
288           TRUE, TRUE, TRUE, FALSE, "[01]12345678901234", FALSE, 18},
289
290         { "zint", "RSS14", N_("GS1 DataBar-14"), gl_barcode_zint_new,
291           TRUE, TRUE, TRUE, FALSE, "1234567890123", TRUE, 13},
292           
293         { "zint", "RSSLTD", "GS1 DataBar-14 Limited",  gl_barcode_zint_new,
294           TRUE, TRUE, TRUE, FALSE, "1234567890123", TRUE, 13},
295           
296         { "zint", "RSSEXP", "GS1 DataBar Extended",  gl_barcode_zint_new,
297           TRUE, TRUE, TRUE, FALSE, "[01]12345678901234", FALSE, 18},
298           
299         { "zint", "RSSS", N_("GS1 DataBar-14 Stacked"), gl_barcode_zint_new,
300           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
301
302         { "zint", "RSSSO", N_("GS1 DataBar-14 Stacked Omni."), gl_barcode_zint_new,
303           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
304
305         { "zint", "RSSSE", N_("GS1 DataBar Extended Stacked"), gl_barcode_zint_new,
306           FALSE, FALSE, TRUE, FALSE, "[01]12345678901234", FALSE, 18},
307
308         { "zint", "HIBC128", N_("HIBC Code 128"), gl_barcode_zint_new,
309           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
310
311         { "zint", "HIBC39", N_("HIBC Code 39"), gl_barcode_zint_new,
312           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
313
314         { "zint", "HIBCDM", N_("HIBC Data Matrix"), gl_barcode_zint_new,
315           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
316
317         { "zint", "HIBCQR", N_("HIBC QR Code"), gl_barcode_zint_new,
318           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
319
320         { "zint", "HIBCPDF", N_("HIBC PDF417"), gl_barcode_zint_new,
321           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
322
323         { "zint", "HIBCMPDF", N_("HIBC Micro PDF417"), gl_barcode_zint_new,
324           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
325
326         { "zint", "HIBCAZ", N_("HIBC Aztec Code"), gl_barcode_zint_new,
327           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
328
329         { "zint", "I25", N_("Interleaved 2 of 5"), gl_barcode_zint_new,
330           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
331
332         { "zint", "ISBN", N_("ISBN"), gl_barcode_zint_new,
333           TRUE, TRUE, TRUE, FALSE, "123456789", FALSE, 9},
334
335         { "zint", "ITF14", N_("ITF-14"), gl_barcode_zint_new,
336           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
337
338         { "zint", "JAPAN", N_("Japanese Postal"), gl_barcode_zint_new,
339           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
340
341         { "zint", "KOREA", N_("Korean Postal"), gl_barcode_zint_new,
342           TRUE, TRUE, TRUE, FALSE, "123456", FALSE, 6},
343
344         { "zint", "LOGM", N_("LOGMARS"), gl_barcode_zint_new,
345           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
346
347         { "zint", "MAXI", N_("Maxicode"), gl_barcode_zint_new,
348           FALSE, FALSE, FALSE, FALSE, "0000000000", TRUE, 10},
349
350         { "zint", "MPDF", N_("Micro PDF417"), gl_barcode_zint_new,
351           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
352
353         { "zint", "MQR", N_("Micro QR Code"), gl_barcode_zint_new,
354           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
355
356         { "zint", "MSI", N_("MSI Plessey"), gl_barcode_zint_new,
357           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
358
359         { "zint", "NVE", N_("NVE-18"), gl_barcode_zint_new,
360           TRUE, TRUE, TRUE, FALSE, "12345678901234567", FALSE, 17},
361
362         { "zint", "PDF", N_("PDF417"), gl_barcode_zint_new,
363           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
364
365         { "zint", "PDFT", N_("PDF417 Truncated"), gl_barcode_zint_new,
366           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
367
368         { "zint", "PLAN", N_("PLANET"), gl_barcode_zint_new,
369           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
370
371         { "zint", "POSTNET", N_("PostNet"), gl_barcode_zint_new,
372           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
373
374         { "zint", "PHARMA", N_("Pharmacode"), gl_barcode_zint_new,
375           FALSE, FALSE, TRUE, FALSE, "123456", FALSE, 6},
376
377         { "zint", "PHARMA2", N_("Pharmacode 2-track"), gl_barcode_zint_new,
378           FALSE, FALSE, TRUE, FALSE, "12345678", FALSE, 8},
379
380         { "zint", "PZN", N_("Pharmazentral Nummer (PZN)"), gl_barcode_zint_new,
381           TRUE, TRUE, TRUE, FALSE, "123456", FALSE, 6},
382
383         { "zint", "QR", N_("QR Code"), gl_barcode_zint_new,
384           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
385
386         { "zint", "RM4", N_("Royal Mail 4-State"), gl_barcode_zint_new,
387           FALSE, FALSE, TRUE, FALSE, "0000000000", TRUE, 10},
388
389         { "zint", "TELE", N_("Telepen"), gl_barcode_zint_new,
390           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
391
392         { "zint", "TELEX", N_("Telepen Numeric"), gl_barcode_zint_new,
393           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
394
395         { "zint", "UPC-A", N_("UPC-A"),  gl_barcode_zint_new,
396           TRUE, TRUE, TRUE, FALSE, "12345678901", FALSE, 11},
397           
398         { "zint", "UPC-E", N_("UPC-E"),  gl_barcode_zint_new,
399           TRUE, TRUE, TRUE, FALSE, "1234567", FALSE, 7},
400           
401         { "zint", "USPS", N_("USPS One Code"), gl_barcode_zint_new,
402           FALSE, FALSE, TRUE, FALSE, "12345678901234567890", TRUE, 20},
403
404         { "zint", "PLS", N_("UK Plessey"), gl_barcode_zint_new,
405           TRUE, TRUE, TRUE, FALSE, "0000000000", TRUE, 10},
406
407 #endif /* HAVE_LIBZINT */
408
409 #ifdef HAVE_LIBIEC16022
410
411         { "libiec16022", "IEC16022", N_("IEC16022 (DataMatrix)"), gl_barcode_iec16022_new,
412           FALSE, FALSE, TRUE, FALSE, "12345678", TRUE, 8},
413
414 #endif /* HAVE_LIBIEC16022 */
415
416 #ifdef HAVE_LIBQRENCODE
417
418         { "libqrencode", "IEC18004", N_("IEC18004 (QRCode)"), gl_barcode_iec18004_new,
419           FALSE, FALSE, TRUE, FALSE, "12345678", TRUE, 8},
420
421 #endif /* HAVE_LIBQRENCODE */
422
423         { NULL, NULL, NULL, NULL, FALSE, FALSE, FALSE, FALSE, NULL, FALSE, 0}
424
425 };
426
427
428 /*========================================================*/
429 /* Private function prototypes.                           */
430 /*========================================================*/
431
432 static gint backend_id_to_index   (const gchar *id);
433 static gint backend_name_to_index (const gchar *name);
434 static gint style_id_to_index     (const gchar *backend_id,
435                                    const gchar *id);
436 static gint style_name_to_index   (const gchar *backend_id,
437                                    const gchar *name);
438
439 /*---------------------------------------------------------------------------*/
440 /* Convert backend id to index into backends table.                          */
441 /*---------------------------------------------------------------------------*/
442 static gint
443 backend_id_to_index (const gchar *id)
444 {
445         gint i;
446
447         if (id == NULL)
448         {
449                 return 0; /* NULL request default. I.e., the first element. */
450         }
451
452         for (i=0; styles[i].id != NULL; i++)
453         {
454                 if (g_ascii_strcasecmp (id, backends[i].id) == 0)
455                 {
456                         return i;
457                 }
458         }
459
460         g_message( "Unknown barcode id \"%s\"", id );
461         return 0;
462 }
463
464
465 /*---------------------------------------------------------------------------*/
466 /* Convert backend name to index into backends table.                        */
467 /*---------------------------------------------------------------------------*/
468 static gint
469 backend_name_to_index (const gchar *name)
470 {
471         gint i;
472
473         if (name == NULL)
474         {
475                 return 0; /* NULL request default. I.e., the first element. */
476         }
477
478         for (i=0; styles[i].id != NULL; i++)
479         {
480                 if (strcmp (name, gettext (backends[i].name)) == 0)
481                 {
482                         return i;
483                 }
484         }
485
486         g_message( "Unknown barcode name \"%s\"", name );
487         return 0;
488 }
489
490
491 /*---------------------------------------------------------------------------*/
492 /* Convert style id to index into styles table.                              */
493 /*---------------------------------------------------------------------------*/
494 static gint
495 style_id_to_index (const gchar *backend_id,
496                    const gchar *id)
497 {
498         gint i;
499
500         if (backend_id == NULL)
501         {
502                 return 0; /* NULL request default. I.e., the first element. */
503         }
504
505         if (id == NULL)
506         {
507                 /* Search for first element with given backend_id. */
508                 for (i=0; styles[i].id != NULL; i++)
509                 {
510                         if (g_ascii_strcasecmp (backend_id, styles[i].backend_id) == 0)
511                         {
512                                 return i;
513                         }
514                 }
515                 g_message( "Unknown barcode backend id \"%s\"", backend_id );
516                 return 0;
517         }
518
519         for (i=0; styles[i].id != NULL; i++)
520         {
521                 if ((g_ascii_strcasecmp (backend_id, styles[i].backend_id) == 0) &&
522                     (g_ascii_strcasecmp (id, styles[i].id) == 0))
523                 {
524                         return i;
525                 }
526         }
527
528         g_message( "Unknown barcode id \"%s\"", id );
529         return 0;
530 }
531
532
533 /*---------------------------------------------------------------------------*/
534 /* Convert style name to index into styles table.                            */
535 /*---------------------------------------------------------------------------*/
536 static gint
537 style_name_to_index (const gchar *backend_id,
538                      const gchar *name)
539 {
540         gint i;
541
542         if (backend_id == NULL)
543         {
544                 return 0; /* NULL request default. I.e., the first element. */
545         }
546
547         if (name == NULL)
548         {
549                 /* Search for first element with given backend_id. */
550                 for (i=0; styles[i].id != NULL; i++)
551                 {
552                         if (g_ascii_strcasecmp (backend_id, styles[i].backend_id) == 0)
553                         {
554                                 return i;
555                         }
556                 }
557                 g_message( "Unknown barcode backend id \"%s\"", backend_id );
558                 return 0;
559         }
560
561         for (i=0; styles[i].id != NULL; i++)
562         {
563                 if ((g_ascii_strcasecmp (backend_id, styles[i].backend_id) == 0) &&
564                     (strcmp (name, gettext (styles[i].name)) == 0))
565                 {
566                         return i;
567                 }
568         }
569
570         g_message( "Unknown barcode name \"%s\"", name );
571         return 0;
572 }
573
574
575 /*****************************************************************************/
576 /* Get a list of names for configured backends.                              */
577 /*****************************************************************************/
578 GList *
579 gl_barcode_backends_get_backend_list (void)
580 {
581         gint   i;
582         GList *list = NULL;
583
584         for (i=0; backends[i].id != NULL; i++)
585         {
586                 list = g_list_prepend (list, g_strdup (gettext (backends[i].name)));
587         }
588
589         return g_list_reverse (list);
590 }
591
592
593 /*****************************************************************************/
594 /* Free up a previously allocated list of backend names.                     */
595 /*****************************************************************************/
596 void
597 gl_barcode_backends_free_backend_list (GList *backend_list)
598 {
599         GList *p;
600
601         for (p=backend_list; p != NULL; p=p->next)
602         {
603                 g_free (p->data);
604                 p->data = NULL;
605         }
606
607         g_list_free (backend_list);
608 }
609
610
611 /*****************************************************************************/
612 /* Convert backend id to name.                                               */
613 /*****************************************************************************/
614 const gchar *
615 gl_barcode_backends_backend_id_to_name (const gchar *backend_id)
616 {
617         return gettext (backends[backend_id_to_index (backend_id)].name);
618 }
619
620
621 /*****************************************************************************/
622 /* Convert backend name to id.                                               */
623 /*****************************************************************************/
624 const gchar *
625 gl_barcode_backends_backend_name_to_id (const gchar *backend_name)
626 {
627         return backends[backend_name_to_index (backend_name)].id;
628 }
629
630
631 /*****************************************************************************/
632 /* Test if backend id is valid.                                              */
633 /*****************************************************************************/
634 gboolean
635 gl_barcode_backends_is_backend_id_valid (const gchar    *backend_id)
636 {
637         gint i;
638
639         if (backend_id == NULL)
640         {
641                 return FALSE;
642         }
643
644         for (i=0; backends[i].id != NULL; i++)
645         {
646                 if (g_ascii_strcasecmp (backend_id, backends[i].id) == 0)
647                 {
648                         return TRUE;
649                 }
650         }
651
652         return FALSE;
653 }
654
655
656 /*****************************************************************************/
657 /* Guess backend id from style id (for backwards compatability).             */
658 /*****************************************************************************/
659 const gchar *
660 gl_barcode_backends_guess_backend_id (const gchar *id)
661 {
662         gint i;
663
664         if (id == NULL)
665         {
666                 return styles[0].backend_id;
667         }
668
669         for (i=0; styles[i].id != NULL; i++)
670         {
671                 if (g_ascii_strcasecmp (id, styles[i].id) == 0)
672                 {
673                         return styles[i].backend_id;
674                 }
675         }
676
677         g_message( "Unknown barcode id \"%s\"", id );
678         return styles[0].backend_id;
679 }
680
681
682 /*****************************************************************************/
683 /* Get a list of names for valid barcode styles.                             */
684 /*****************************************************************************/
685 GList *
686 gl_barcode_backends_get_styles_list  (const gchar *backend_id)
687 {
688         gint   i;
689         GList *list = NULL;
690
691         for (i=0; styles[i].id != NULL; i++)
692         {
693                 if ( g_ascii_strcasecmp (styles[i].backend_id, backend_id) == 0 )
694                 {
695                         list = g_list_prepend (list, g_strdup (gettext (styles[i].name)));
696                 }
697         }
698
699         return g_list_reverse (list);
700 }
701
702
703 /*****************************************************************************/
704 /* Free up a previously allocated list of style names.                       */
705 /*****************************************************************************/
706 void
707 gl_barcode_backends_free_styles_list (GList *styles_list)
708 {
709         GList *p;
710
711         for (p=styles_list; p != NULL; p=p->next)
712         {
713                 g_free (p->data);
714                 p->data = NULL;
715         }
716
717         g_list_free (styles_list);
718 }
719
720
721 /*****************************************************************************/
722 /* Convert style to text.                                                    */
723 /*****************************************************************************/
724 const gchar *
725 gl_barcode_backends_style_id_to_name (const gchar *backend_id,
726                                       const gchar *id)
727 {
728         return gettext (styles[style_id_to_index (backend_id, id)].name);
729 }
730
731
732 /*****************************************************************************/
733 /* Convert name to style.                                                    */
734 /*****************************************************************************/
735 const gchar *
736 gl_barcode_backends_style_name_to_id (const gchar *backend_id,
737                                       const gchar *name)
738 {
739         return styles[style_name_to_index (backend_id, name)].id;
740 }
741
742
743 /*****************************************************************************/
744 /* Return an appropriate set of digits for the given barcode style.          */
745 /*****************************************************************************/
746 gchar *
747 gl_barcode_backends_style_default_digits (const gchar *backend_id,
748                                           const gchar *id,
749                                           guint        n)
750 {
751         int i;
752
753         i = style_id_to_index (backend_id, id);
754
755         if (styles[i].can_freeform)
756         {
757                 return g_strnfill (MAX (n,1), '0');
758         }
759         else
760         {
761                 return g_strdup (styles[i].default_digits);
762         }
763 }
764
765
766 /*****************************************************************************/
767 /* Query text capabilities.                                                  */
768 /*****************************************************************************/
769 gboolean
770 gl_barcode_backends_style_can_text (const gchar *backend_id,
771                                     const gchar *id)
772 {
773         return styles[style_id_to_index (backend_id, id)].can_text;
774 }
775
776
777 gboolean
778 gl_barcode_backends_style_text_optional (const gchar *backend_id,
779                                          const gchar *id)
780 {
781         return styles[style_id_to_index (backend_id, id)].text_optional;
782 }
783
784
785 /*****************************************************************************/
786 /* Query checksum capabilities.                                              */
787 /*****************************************************************************/
788 gboolean
789 gl_barcode_backends_style_can_csum (const gchar *backend_id,
790                                     const gchar *id)
791 {
792         return styles[style_id_to_index (backend_id, id)].can_checksum;
793 }
794
795
796 gboolean
797 gl_barcode_backends_style_csum_optional (const gchar *backend_id,
798                                          const gchar *id)
799 {
800         return styles[style_id_to_index (backend_id, id)].checksum_optional;
801 }
802
803
804 /*****************************************************************************/
805 /* Query if freeform input is allowed.                                       */
806 /*****************************************************************************/
807 gboolean
808 gl_barcode_backends_style_can_freeform (const gchar *backend_id,
809                                         const gchar    *id)
810 {
811         return styles[style_id_to_index (backend_id, id)].can_freeform;
812 }
813
814
815 /*****************************************************************************/
816 /* Query prefered number of digits of input.                                 */
817 /*****************************************************************************/
818 guint
819 gl_barcode_backends_style_get_prefered_n (const gchar *backend_id,
820                                           const gchar *id)
821 {
822         return styles[style_id_to_index (backend_id, id)].prefered_n;
823 }
824
825
826 /*****************************************************************************/
827 /* Call appropriate barcode backend to create barcode in intermediate format.*/
828 /*****************************************************************************/
829 lglBarcode *
830 gl_barcode_backends_new_barcode (const gchar    *backend_id,
831                                  const gchar    *id,
832                                  gboolean        text_flag,
833                                  gboolean        checksum_flag,
834                                  gdouble         w,
835                                  gdouble         h,
836                                  const gchar    *digits)
837 {
838         lglBarcode *gbc;
839         gint        i;
840
841         g_return_val_if_fail (digits!=NULL, NULL);
842
843         i = style_id_to_index (backend_id, id);
844
845         gbc = styles[i].new_barcode (styles[i].id,
846                                      text_flag,
847                                      checksum_flag,
848                                      w,
849                                      h,
850                                      digits);
851
852         return gbc;
853 }
854
855
856
857 /*
858  * Local Variables:       -- emacs
859  * mode: C                -- emacs
860  * c-basic-offset: 8      -- emacs
861  * tab-width: 8           -- emacs
862  * indent-tabs-mode: nil  -- emacs
863  * End:                   -- emacs
864  */