1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * bc-iec16022.c: front-end to iec16022-library module
8 * Copyright (C) 2001-2006 Jim Evins <evins@snaught.com>.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "bc-iec16022.h"
32 #include <glib/gmessages.h>
34 #include "iec16022ecc200.h"
38 /*========================================================*/
39 /* Private macros and constants. */
40 /*========================================================*/
41 #define MIN_PIXEL_SIZE 1.0
43 /*===========================================*/
44 /* Local function prototypes */
45 /*===========================================*/
46 static glBarcode *render_iec16022 (const gchar *grid,
52 /*****************************************************************************/
53 /* Generate intermediate representation of barcode. */
54 /*****************************************************************************/
56 gl_barcode_iec16022_new (const gchar *id,
58 gboolean checksum_flag,
64 gint i_width, i_height;
67 if ( strlen (digits) == 0 )
75 grid = (gchar *)iec16022ecc200 (&i_width, &i_height, NULL,
76 strlen (digits), (unsigned char *)digits,
79 /* now render with our custom back-end,
80 to create appropriate intermdediate format */
81 gbc = render_iec16022 (grid, i_width, i_height, w, h);
87 /*--------------------------------------------------------------------------
88 * PRIVATE. Render to glBarcode intermediate representation of barcode.
89 *--------------------------------------------------------------------------*/
91 render_iec16022 (const gchar *grid,
100 gdouble aspect_ratio, pixel_size;
102 /* Treat requested size as a bounding box, scale to maintain aspect
103 * ratio while fitting it in this bounding box. */
104 aspect_ratio = (gdouble)i_height / (gdouble)i_width;
105 if ( h > w*aspect_ratio ) {
106 h = w * aspect_ratio;
108 w = h / aspect_ratio;
111 /* Now determine pixel size. */
112 pixel_size = w / i_width;
113 if ( pixel_size < MIN_PIXEL_SIZE )
115 pixel_size = MIN_PIXEL_SIZE;
118 gbc = g_new0 (glBarcode, 1);
120 /* Now traverse the code string and create a list of boxes */
121 for ( y = i_height-1; y >= 0; y-- )
124 for ( x = 0; x < i_width; x++ )
129 line = g_new0 (glBarcodeLine, 1);
130 line->x = x*pixel_size + pixel_size/2.0;
131 line->y = y*pixel_size;
132 line->length = pixel_size;
133 line->width = pixel_size;
134 gbc->lines = g_list_append (gbc->lines, line);
141 /* Fill in other info */
142 gbc->height = i_height * pixel_size;
143 gbc->width = i_width * pixel_size;
146 g_print ("w=%f, h=%f\n", gbc->width, gbc->height);