2 * code39.c -- encoding for code39
4 * Copyright (c) 1999 Alessandro Rubini (rubini@gnu.org)
5 * Copyright (c) 1999 Prosa Srl. (prosa@prosa.it)
7 * This program 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 2 of the License, or
10 * (at your option) any later version.
12 * This program 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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
31 /* this is ordered in decades to simplify encoding */
32 static char alphabet[] =
33 "1234567890" "ABCDEFGHIJ" "KLMNOPQRST" "UVWXYZ-. *" "$/+%";
35 /* the checksum alphabet has a different order */
36 static char checkbet[] =
37 "0123456789" "ABCDEFGHIJ" "KLMNOPQRST" "UVWXYZ-. $" "/+%";
39 /* The first 40 symbols repeat this bar pattern */
40 static char *bars[] = {
41 "31113","13113","33111","11313","31311",
42 "13311","11133","31131","13131","11331"};
44 /* The first 4 decades use these space patterns */
45 static char *spaces[] = {"1311","1131","1113","3111"};
47 /* the last four symbols are special */
48 static char *specialbars[] = {
49 "11111","11111","11111","11111"};
51 static char *specialspaces[] = {
52 "3331","3313","3133","1333"};
54 static char *fillers[]= {
61 * Check that the text can be encoded. Returns 0 or -1.
62 * If it's all lowecase convert to uppercase and accept it
64 int Barcode_39_verify(unsigned char *text)
66 int i, lower=0, upper=0;
70 for (i=0; text[i]; i++) {
71 if (isupper(text[i])) upper++;
72 if (islower(text[i])) lower++;
73 if (!strchr(alphabet,toupper(text[i])))
81 static int add_one(char *ptr, int code)
89 b = specialbars[code-40];
90 s = specialspaces[code-40];
92 sprintf(ptr,"1%c%c%c%c%c%c%c%c%c", /* separator */
93 b[0], s[0], b[1], s[1], b[2], s[2],
99 * The encoding functions fills the "partial" and "textinfo" fields.
100 * Lowercase chars are converted to uppercase
102 int Barcode_39_encode(struct Barcode_Item *bc)
105 static char *partial; /* dynamic */
106 static char *textinfo; /* dynamic */
107 char *c, *ptr, *textptr;
108 int i, code, textpos, checksum = 0;
114 bc->partial = bc->textinfo = NULL; /* safe */
117 bc->encoding = strdup("code 39");
124 /* the partial code is 10* (head + text + check + tail) + margin + term. */
125 partial = malloc( (strlen(text) + 3) * 10 +2);
131 /* the text information is at most "nnn:fff:c " * strlen +term */
132 textinfo = malloc(10*strlen(text) + 2);
139 strcpy(partial, fillers[0]);
140 ptr = partial + strlen(partial);
144 for (i=0; text[i]; i++) {
145 c = strchr(alphabet, toupper(text[i]));
147 bc->error = EINVAL; /* impossible if text is verified */
154 c = strchr(checkbet,*c);
155 if (c) /* the '*' is not there */
156 checksum += (c-checkbet);
157 sprintf(textptr, "%i:12:%c ", textpos, toupper(text[i]));
159 textpos += 16; /* width of each code */
160 textptr += strlen(textptr);
163 /* Add the checksum */
164 if ( (bc->flags & BARCODE_NO_CHECKSUM)==0 ) {
165 code = (strchr(alphabet, checkbet[checksum % 43]) - alphabet);
168 strcat(ptr, fillers[1]); /* end */
169 bc->partial = partial;
170 bc->textinfo = textinfo;