2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * bc-gnubarcode.c: front-end to GNU-barcode-library module
6 * Copyright (C) 2001-2002 Jim Evins <evins@snaught.com>.
8 * Some of this code is borrowed from the postscript renderer (ps.c)
9 * from the GNU barcode library:
11 * Copyright (C) 1999 Alessaandro Rubini (rubini@gnu.org)
12 * Copyright (C) 1999 Prosa Srl. (prosa@prosa.it)
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include "bc-gnubarcode.h"
41 #define SHRINK_AMOUNT 0.15 /* shrink bars to account for ink spreading */
42 #define FONT_SCALE 0.95 /* Shrink fonts just a hair */
44 static glBarcode *render_pass1 (struct Barcode_Item *bci,
48 /*****************************************************************************/
49 /* Generate intermediate representation of barcode. */
50 /*****************************************************************************/
52 gl_barcode_gnubarcode_new (glBarcodeStyle style,
58 struct Barcode_Item *bci;
61 bci = Barcode_Create (digits);
63 /* First encode using GNU Barcode library */
65 case GL_BARCODE_STYLE_EAN:
68 case GL_BARCODE_STYLE_UPC:
71 case GL_BARCODE_STYLE_ISBN:
74 case GL_BARCODE_STYLE_39:
77 case GL_BARCODE_STYLE_128:
80 case GL_BARCODE_STYLE_128C:
83 case GL_BARCODE_STYLE_128B:
86 case GL_BARCODE_STYLE_I25:
89 case GL_BARCODE_STYLE_CBR:
92 case GL_BARCODE_STYLE_MSI:
95 case GL_BARCODE_STYLE_PLS:
99 g_warning( "Illegal barcode style %d", style );
103 Barcode_Encode (bci, flags);
104 if (!bci->partial || !bci->textinfo) {
105 g_warning ("Barcode Data Invalid");
106 Barcode_Delete (bci);
110 /* now render with our custom back-end,
111 to create appropriate intermdediate format */
112 gbc = render_pass1 (bci, text_flag, scale);
114 Barcode_Delete (bci);
118 /*--------------------------------------------------------------------------
119 * PRIVATE. Render to glBarcode intermediate representation of barcode.
121 * Some of this code is borrowed from the postscript renderer (ps.c)
122 * from the GNU barcode library:
124 * Copyright (C) 1999 Alessaandro Rubini (rubini@gnu.org)
125 * Copyright (C) 1999 Prosa Srl. (prosa@prosa.it)
127 *--------------------------------------------------------------------------*/
129 render_pass1 (struct Barcode_Item *bci,
135 glBarcodeChar *bchar;
139 gint mode = '-'; /* text below bars */
143 /* First calculate barlen */
144 barlen = bci->partial[0] - '0';
145 for (p = bci->partial + 1; *p != 0; p++) {
149 if ((*p != '+') && (*p != '-')) {
150 barlen += *p - 'a' + 1;
155 /* The width defaults to "just enough" */
156 bci->width = barlen * scale + 1;
158 /* The height defaults to 80 points (rescaled) */
160 bci->height = 80 * scale;
162 gbc = g_new0 (glBarcode, 1);
164 /* Now traverse the code string and create a list of lines */
165 x = bci->margin + (bci->partial[0] - '0') * scale;
166 for (p = bci->partial + 1, i = 1; *p != 0; p++, i++) {
167 /* special cases: '+' and '-' */
168 if (*p == '+' || *p == '-') {
169 mode = *p; /* don't count it */
173 /* j is the width of this bar/space */
178 if (i % 2) { /* bar */
179 x0 = x + (j * scale) / 2;
182 if (text_flag) { /* leave space for text */
184 /* text below bars: 10 or 5 points */
185 yr -= (isdigit (*p) ? 10 : 5) * scale;
187 /* above bars: 10 or 0 from bottom,
190 yr -= (isdigit (*p) ? 20 : 10) * scale;
193 line = g_new0 (glBarcodeLine, 1);
197 line->width = (j * scale) - SHRINK_AMOUNT;
198 gbc->lines = g_list_append (gbc->lines, line);
205 mode = '-'; /* reinstantiate default */
207 for (p = bci->textinfo; p; p = strchr (p, ' ')) {
212 if (*p == '+' || *p == '-') {
216 if (sscanf (p, "%lf:%lf:%c", &f1, &f2, &c) != 3) {
217 g_warning ("impossible data: %s", p);
220 bchar = g_new0 (glBarcodeChar, 1);
221 bchar->x = f1 * scale + bci->margin;
224 bci->margin + bci->height - 8 * scale;
226 bchar->y = bci->margin;
228 bchar->fsize = f2 * FONT_SCALE * scale;
230 gbc->chars = g_list_append (gbc->chars, bchar);
234 /* Fill in other info */
235 gbc->height = bci->height + 2.0 * bci->margin;
236 gbc->width = bci->width + 2.0 * bci->margin;