]> git.sur5r.net Git - glabels/blob - src/bc-iec16022.c
dbd06ecbcbc3de6c847aa93203a2cf2135c821c6
[glabels] / src / bc-iec16022.c
1 /*
2  *  bc-iec16022.c
3  *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels 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 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels 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.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #ifdef HAVE_LIBIEC16022
24
25 #include "bc-iec16022.h"
26
27 #include <glib.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <iec16022ecc200.h>
32
33 #include "debug.h"
34
35
36 /*========================================================*/
37 /* Private macros and constants.                          */
38 /*========================================================*/
39 #define MIN_PIXEL_SIZE 1.0
40
41
42 /*===========================================*/
43 /* Local function prototypes                 */
44 /*===========================================*/
45 static glBarcode *render_iec16022 (const gchar *grid,
46                                    gint         i_width,
47                                    gint         i_height,
48                                    gdouble      w,
49                                    gdouble      h);
50
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 /*--------------------------------------------------------------------------
89  * PRIVATE.  Render to glBarcode intermediate representation of barcode.
90  *--------------------------------------------------------------------------*/
91 static glBarcode *
92 render_iec16022 (const gchar *grid,
93                  gint         i_width,
94                  gint         i_height,
95                  gdouble      w,
96                  gdouble      h)
97 {
98         glBarcode          *gbc;
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 = gl_barcode_new ();
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                                 gl_barcode_add_box (gbc, x*pixel_size, y*pixel_size, pixel_size, pixel_size);
130                         }
131
132                 }
133
134         }
135
136         /* Fill in other info */
137         gbc->height = i_height * pixel_size;
138         gbc->width  = i_width  * pixel_size;
139
140         return gbc;
141 }
142
143 #endif /* HAVE_LIBIEC16022 */
144
145
146
147 /*
148  * Local Variables:       -- emacs
149  * mode: C                -- emacs
150  * c-basic-offset: 8      -- emacs
151  * tab-width: 8           -- emacs
152  * indent-tabs-mode: nil  -- emacs
153  * End:                   -- emacs
154  */