2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * bc_postnet.c: GLabels POSTNET barcode module
6 * Copyright (C) 2001-2002 Jim Evins <evins@snaught.com>.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * This module implements the POSTNET barcode specified in the USPS
25 * publication 25, Mar 2001.
33 #include "bc-postnet.h"
37 static gchar *symbols[] = {
50 static gchar *frame_symbol = "1";
52 #define POSTNET_BAR_WIDTH 1.25
53 #define POSTNET_FULLBAR_HEIGHT 9.00
54 #define POSTNET_HALFBAR_HEIGHT 3.50
55 #define POSTNET_BAR_PITCH 3.25
56 #define POSTNET_HORIZ_MARGIN 9.00
57 #define POSTNET_VERT_MARGIN 3.00
59 static gchar *postnet_code (gchar * digits);
61 /****************************************************************************/
62 /* Generate list of lines that form the barcode for the given digits. */
63 /****************************************************************************/
65 gl_barcode_postnet_new (gchar * digits)
72 gbc = g_new0 (glBarcode, 1);
74 /* First get code string */
75 code = postnet_code (digits);
80 /* Now traverse the code string and create a list of lines */
81 x = POSTNET_HORIZ_MARGIN;
82 for (p = code; *p != 0; p++) {
83 line = g_new0 (glBarcodeLine, 1);
85 line->y = POSTNET_VERT_MARGIN;
88 POSTNET_FULLBAR_HEIGHT - POSTNET_HALFBAR_HEIGHT;
89 line->length = POSTNET_HALFBAR_HEIGHT;
91 line->length = POSTNET_FULLBAR_HEIGHT;
93 line->width = POSTNET_BAR_WIDTH;
95 gbc->lines = g_list_append (gbc->lines, line);
97 x += POSTNET_BAR_PITCH;
102 gbc->width = x + POSTNET_HORIZ_MARGIN;
103 gbc->height = POSTNET_FULLBAR_HEIGHT + 2 * POSTNET_VERT_MARGIN;
110 /*--------------------------------------------------------------------------*/
111 /* PRIVATE. Generate string of symbols, representing barcode. */
112 /*--------------------------------------------------------------------------*/
114 postnet_code (gchar * digits)
123 code = g_string_new (frame_symbol);
126 for (p = digits, len = 0; (*p != 0) && (len < 11); p++) {
128 /* Only translate valid characters (0-9) */
131 code = g_string_append (code, symbols[d]);
135 if ((len != 5) && (len != 9) && (len != 11)) {
136 g_warning ("Invalid POSTNET length %d, should be 5(ZIP), 9(ZIP+4), or 11(DPBC)", len);
139 /* Create correction character */
140 d = (10 - (sum % 10)) % 10;
141 code = g_string_append (code, symbols[d]);
143 /* Right frame bar */
144 code = g_string_append (code, frame_symbol);
146 ret = g_strdup (code->str);
147 g_string_free (code, TRUE);