summaryrefslogtreecommitdiff
path: root/glabels2/src/bc.c
blob: 89de2fbbcb060f16807718284121ec145e7207bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */

/*
 *  (GLABELS) Label and Business Card Creation program for GNOME
 *
 *  bc.c:  GLabels barcode module
 *
 *  Copyright (C) 2001-2003  Jim Evins <evins@snaught.com>.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */
#include <config.h>

#include "bc.h"

#include <glib/gi18n.h>
#include <glib/gmem.h>
#include <glib/gstrfuncs.h>
#include <glib/gmessages.h>

#include "bc-postnet.h"
#include "bc-gnubarcode.h"
#include "bc-iec16022.h"

#include "debug.h"

/*========================================================*/
/* Private macros and constants.                          */
/*========================================================*/

/*========================================================*/
/* Private types.                                         */
/*========================================================*/

typedef struct {
	gchar            *id;
	gchar            *name;
	glBarcodeNewFunc  new;
	gboolean          can_text;
	gboolean          text_optional;
	gboolean          can_checksum;
	gboolean          checksum_optional;
	gchar            *default_digits;
	gboolean          can_freeform;
	guint             prefered_n;
} Backend;


/*========================================================*/
/* Private globals.                                       */
/*========================================================*/

static const Backend backends[] = {

	{ "POSTNET", N_("POSTNET (any)"), gl_barcode_postnet_new,
	  FALSE, FALSE, TRUE, FALSE, "12345-6789-12", FALSE, 11},

	{ "POSTNET-5", N_("POSTNET-5 (ZIP only)"), gl_barcode_postnet_new,
	  FALSE, FALSE, TRUE, FALSE, "12345", FALSE, 5},

	{ "POSTNET-9", N_("POSTNET-9 (ZIP+4)"), gl_barcode_postnet_new,
	  FALSE, FALSE, TRUE, FALSE, "12345-6789", FALSE, 9},

	{ "POSTNET-11", N_("POSTNET-11 (DPBC)"), gl_barcode_postnet_new,
	  FALSE, FALSE, TRUE, FALSE, "12345-6789-12", FALSE, 11},

	{ "CEPNET", N_("CEPNET"), gl_barcode_postnet_new,
	  FALSE, FALSE, TRUE, FALSE, "12345-678", FALSE, 8},

	{ "EAN", N_("EAN (any)"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, FALSE, "000000000000 00000", FALSE, 17},

	{ "EAN-8", N_("EAN-8"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, FALSE, "0000000", FALSE, 7},

	{ "EAN-8+2", N_("EAN-8 +2"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, FALSE, "0000000 00", FALSE, 9},

	{ "EAN-8+5", N_("EAN-8 +5"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, FALSE, "0000000 00000", FALSE, 12},

	{ "EAN-13", N_("EAN-13"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, FALSE, "000000000000", FALSE, 12},

	{ "EAN-13+2", N_("EAN-13 +2"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, FALSE, "000000000000 00", FALSE, 14},

	{ "EAN-13+5", N_("EAN-13 +5"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, FALSE, "000000000000 00000", FALSE, 17},

	{ "UPC", N_("UPC (UPC-A or UPC-E)"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, FALSE, "00000000000 00000", FALSE, 16},

	{ "UPC-A", N_("UPC-A"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, FALSE, "00000000000", FALSE, 11},

	{ "UPC-A+2", N_("UPC-A +2"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, FALSE, "00000000000 00", FALSE, 13},

	{ "UPC-A+5", N_("UPC-A +5"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, FALSE, "00000000000 00000", FALSE, 16},

	{ "UPC-E", N_("UPC-E"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, FALSE, "000000", FALSE, 6},

	{ "UPC-E+2", N_("UPC-E +2"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, FALSE, "000000 00", FALSE, 8},

	{ "UPC-E+5", N_("UPC-E +5"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, FALSE, "000000 00000", FALSE, 11},

	{ "ISBN", N_("ISBN"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, TRUE, "0-00000-000-0", FALSE, 10},

	{ "ISBN+5", N_("ISBN +5"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, TRUE, "0-00000-000-0 00000", FALSE, 15},

	{ "Code39", N_("Code 39"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},

	{ "Code128", N_("Code 128"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},

	{ "Code128C", N_("Code 128C"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},

	{ "Code128B", N_("Code 128B"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},

	{ "I25", N_("Interleaved 2 of 5"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},

	{ "CBR", N_("Codabar"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},

	{ "MSI", N_("MSI"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},

	{ "PLS", N_("Plessey"), gl_barcode_gnubarcode_new,
	  TRUE, TRUE, TRUE, TRUE, "0000000000", TRUE, 10},

	{ "IEC16022", N_("IEC16022 (DataMatrix)"), gl_barcode_iec16022_new,
	  FALSE, FALSE, TRUE, FALSE, "12345678", TRUE, 8},

	{ NULL, NULL, NULL, FALSE, FALSE, FALSE, FALSE, NULL, FALSE, 0}

};

/*========================================================*/
/* Private function prototypes.                           */
/*========================================================*/


/*---------------------------------------------------------------------------*/
/* Convert id to index into above table.                                     */
/*---------------------------------------------------------------------------*/
static gint
id_to_index (const gchar *id)
{
	gint i;

	if (id == 0) {
		return 0; /* NULL request default. I.e., the first element. */
	}

	for (i=0; backends[i].id != NULL; i++) {
		if (g_strcasecmp (id, backends[i].id) == 0) {
			return i;
		}
	}

	g_message( "Unknown barcode id \"%s\"", id );
	return 0;
}

/*---------------------------------------------------------------------------*/
/* Convert name to index into above table.                                   */
/*---------------------------------------------------------------------------*/
static gint
name_to_index (const gchar *name)
{
	gint i;

	g_return_val_if_fail (name!=NULL, 0);

	for (i=0; backends[i].id != NULL; i++) {
		if (g_strcasecmp (name, backends[i].name) == 0) {
			return i;
		}
	}

	g_message( "Unknown barcode name \"%s\"", name );
	return 0;
}

/*****************************************************************************/
/* Call appropriate barcode backend to create barcode in intermediate format.*/
/*****************************************************************************/
glBarcode *
gl_barcode_new (const gchar    *id,
		gboolean        text_flag,
		gboolean        checksum_flag,
		gdouble         w,
		gdouble         h,
		const gchar    *digits)
{
	glBarcode *gbc;
	gint       i;

	g_return_val_if_fail (digits!=NULL, NULL);

	i = id_to_index (id);
	gbc = backends[i].new (backends[i].id,
			       text_flag,
			       checksum_flag,
			       w,
			       h,
			       digits);

	return gbc;
}

/*****************************************************************************/
/* Free previously created barcode.                                          */
/*****************************************************************************/
void
gl_barcode_free (glBarcode **gbc)
{
	GList *p;

	if (*gbc != NULL) {

		for (p = (*gbc)->lines; p != NULL; p = p->next) {
			g_free (p->data);
			p->data = NULL;
		}
		g_list_free ((*gbc)->lines);
		(*gbc)->lines = NULL;

		for (p = (*gbc)->chars; p != NULL; p = p->next) {
			g_free (p->data);
			p->data = NULL;
		}
		g_list_free ((*gbc)->chars);
		(*gbc)->chars = NULL;

		g_free (*gbc);
		*gbc = NULL;
	}
}

/*****************************************************************************/
/* Get a list of names for valid barcode styles.                             */
/*****************************************************************************/
GList *
gl_barcode_get_styles_list  (void)
{
	gint   i;
	GList *list = NULL;

	for (i=0; backends[i].id != NULL; i++) {
		list = g_list_append (list, g_strdup (backends[i].name));
	}

	return list;
}

/*****************************************************************************/
/* Free up a previously allocated list of style names.                       */
/*****************************************************************************/
void
gl_barcode_free_styles_list (GList *styles_list)
{
	GList *p;

	for (p=styles_list; p != NULL; p=p->next) {
		g_free (p->data);
		p->data = NULL;
	}

	g_list_free (styles_list);
}


/*****************************************************************************/
/* Return an appropriate set of digits for the given barcode style.          */
/*****************************************************************************/
gchar *
gl_barcode_default_digits (const gchar *id,
			   guint        n)
{
	int i;

	i = id_to_index (id);

	if (backends[i].can_freeform) {

		return g_strnfill (MAX (n,1), '0');

	} else {

		return g_strdup (backends[i].default_digits);

	}
}

/*****************************************************************************/
/* Query text capabilities.                                                  */
/*****************************************************************************/
gboolean
gl_barcode_can_text (const gchar *id)
{
	return backends[id_to_index (id)].can_text;
}

gboolean
gl_barcode_text_optional (const gchar *id)
{
	return backends[id_to_index (id)].text_optional;
}

/*****************************************************************************/
/* Query checksum capabilities.                                              */
/*****************************************************************************/
gboolean
gl_barcode_can_csum (const gchar *id)
{
	return backends[id_to_index (id)].can_checksum;
}

gboolean
gl_barcode_csum_optional (const gchar *id)
{
	return backends[id_to_index (id)].checksum_optional;
}


/*****************************************************************************/
/* Query if freeform input is allowed.                                       */
/*****************************************************************************/
gboolean
gl_barcode_can_freeform     (const gchar    *id)
{
	return backends[id_to_index (id)].can_freeform;
}

/*****************************************************************************/
/* Query prefered number of digits of input.                                 */
/*****************************************************************************/
guint
gl_barcode_get_prefered_n (const gchar    *id)
{
	return backends[id_to_index (id)].prefered_n;
}

/*****************************************************************************/
/* Convert style to text.                                                    */
/*****************************************************************************/
const gchar *
gl_barcode_id_to_name (const gchar *id)
{
	return backends[id_to_index (id)].name;
}

/*****************************************************************************/
/* Convert name to style.                                                    */
/*****************************************************************************/
const gchar *
gl_barcode_name_to_id (const gchar *name)
{
	g_return_val_if_fail (name!=NULL, backends[0].id);

	return backends[name_to_index (name)].id;
}