]> git.sur5r.net Git - glabels/blob - src/bc-iec16022.c
Imported Upstream version 2.2.8
[glabels] / src / bc-iec16022.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /*
4  *  (GLABELS) Label and Business Card Creation program for GNOME
5  *
6  *  bc-iec16022.c:  front-end to iec16022-library module
7  *
8  *  Copyright (C) 2001-2006  Jim Evins <evins@snaught.com>.
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include <config.h>
26
27 #include "bc-iec16022.h"
28
29 #include <ctype.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <glib/gmessages.h>
33
34 #include "iec16022ecc200.h"
35
36 #include "debug.h"
37
38 /*========================================================*/
39 /* Private macros and constants.                          */
40 /*========================================================*/
41 #define MIN_PIXEL_SIZE 1.0
42
43 /*===========================================*/
44 /* Local function prototypes                 */
45 /*===========================================*/
46 static glBarcode *render_iec16022 (const gchar *grid,
47                                    gint         i_width,
48                                    gint         i_height,
49                                    gdouble      w,
50                                    gdouble      h);
51
52 /*****************************************************************************/
53 /* Generate intermediate representation of barcode.                          */
54 /*****************************************************************************/
55 glBarcode *
56 gl_barcode_iec16022_new (const gchar    *id,
57                          gboolean        text_flag,
58                          gboolean        checksum_flag,
59                          gdouble         w,
60                          gdouble         h,
61                          const gchar    *digits)
62 {
63         gchar               *grid;
64         gint                 i_width, i_height;
65         glBarcode           *gbc;
66
67         if ( strlen (digits) == 0 )
68         {
69                 return NULL;
70         }
71
72         i_width  = 0;
73         i_height = 0;
74
75         grid = (gchar *)iec16022ecc200 (&i_width, &i_height, NULL,
76                                         strlen (digits), (unsigned char *)digits,
77                                         NULL, NULL, NULL);
78
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);
82
83         free (grid);
84         return gbc;
85 }
86
87 /*--------------------------------------------------------------------------
88  * PRIVATE.  Render to glBarcode intermediate representation of barcode.
89  *--------------------------------------------------------------------------*/
90 static glBarcode *
91 render_iec16022 (const gchar *grid,
92                  gint         i_width,
93                  gint         i_height,
94                  gdouble      w,
95                  gdouble      h)
96 {
97         glBarcode     *gbc;
98         glBarcodeLine *line;
99         gint           x, y;
100         gdouble        aspect_ratio, pixel_size;
101
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;
107         } else {
108                 w = h / aspect_ratio;
109         }
110
111         /* Now determine pixel size. */
112         pixel_size = w / i_width;
113         if ( pixel_size < MIN_PIXEL_SIZE )
114         {
115                 pixel_size = MIN_PIXEL_SIZE;
116         }
117
118         gbc = g_new0 (glBarcode, 1);
119
120         /* Now traverse the code string and create a list of boxes */
121         for ( y = i_height-1; y >= 0; y-- )
122         {
123
124                 for ( x = 0; x < i_width; x++ )
125                 {
126
127                         if (*grid++)
128                         {
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);
135                         }
136
137                 }
138
139         }
140
141         /* Fill in other info */
142         gbc->height = i_height * pixel_size;
143         gbc->width  = i_width  * pixel_size;
144
145 #if 0
146         g_print ("w=%f, h=%f\n", gbc->width, gbc->height);
147 #endif
148
149         return gbc;
150 }
151