2 * codabar.c -- encoding for Codabar
4 * Copyright (c) 2000 Leonid A. Broukhis (leob@mailcom.com)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
30 /* this is ordered in decades to simplify encoding */
31 static char alphabet[] =
32 "0123456789" "-$:/.+ABCD";
41 static char *patterns[] = {
42 "1111133","1111331","1113113","3311111","1131131",
43 "3111131","1311113","1311311","1331111","3113111",
44 "1113311","1133111","3111313","3131113","3131311",
45 "1131313","1133131","1313113","1113133","1113331" };
48 * Check that the text can be encoded. Returns 0 or -1.
49 * If it's all lowecase convert to uppercase and accept it.
50 * If the first character is a letter (A to D), the last one must be too;
51 * no other character should be a letter.
53 int Barcode_cbr_verify(unsigned char *text)
55 int i, lower=0, upper=0;
60 for (i=0; text[i]; i++) {
62 if (isupper(text[i])) upper++;
63 if (islower(text[i])) lower++;
64 pos = strchr(alphabet,toupper(text[i]));
67 if (i == 0 && pos - alphabet >= CODE_A)
69 else if (pos - alphabet >= CODE_A &&
70 (!startpresent || i != strlen(text) - 1))
78 static int add_one(char *ptr, int code)
80 sprintf(ptr,"1%s", /* separator */ patterns[code]);
85 * The encoding functions fills the "partial" and "textinfo" fields.
86 * Lowercase chars are converted to uppercase
88 int Barcode_cbr_encode(struct Barcode_Item *bc)
91 static char *partial; /* dynamic */
92 static char *textinfo; /* dynamic */
93 char *c, *ptr, *textptr;
94 int i, code, textpos, usesum, checksum = 0, startpresent;
100 bc->partial = bc->textinfo = NULL; /* safe */
103 bc->encoding = strdup("codabar");
110 /* the partial code is 8 * (head + text + check + tail) + margin + term. */
111 partial = malloc( (strlen(text) + 3) * 8 + 2);
117 /* the text information is at most "nnn:fff:c " * (strlen + check) +term */
118 textinfo = malloc(10*(strlen(text) + 1) + 2);
128 usesum = bc->flags & BARCODE_NO_CHECKSUM ? 0 : 1;
129 /* if no start character specified, A is used as a start character */
130 if (!isalpha(text[0])) {
131 add_one(ptr, CODE_A);
139 for (i=0; i<strlen(text); i++) {
140 c = strchr(alphabet, toupper(text[i]));
142 bc->error = EINVAL; /* impossible if text is verified */
149 sprintf(textptr, "%i:12:%c ", textpos, toupper(text[i]));
150 textpos += code < 12 ? NARROW : WIDE;
151 textptr += strlen(textptr);
154 if (startpresent && usesum && i == strlen(text) - 2) {
155 /* stuff a check symbol before the stop */
156 c = strchr(alphabet, toupper(text[i+1]));
157 if (!c) /* impossible */
162 /* Complement to a multiple of 16 */
163 checksum = (checksum + 15) / 16 * 16 - checksum;
164 add_one(ptr, checksum);
170 /* if no start character specified, B is used as a stop char */
172 checksum = (checksum + 15) / 16 * 16 - checksum;
173 add_one(ptr, checksum);
176 add_one(ptr, CODE_B);
178 bc->partial = partial;
179 bc->textinfo = textinfo;