]> git.sur5r.net Git - glabels/blob - src/bc-iec16022.c
Imported Upstream version 3.0.0
[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 lglBarcode *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 lglBarcode *
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         lglBarcode          *gbc;
66
67         if ( *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 lglBarcode intermediate representation of barcode.
90  *--------------------------------------------------------------------------*/
91 static lglBarcode *
92 render_iec16022 (const gchar *grid,
93                  gint         i_width,
94                  gint         i_height,
95                  gdouble      w,
96                  gdouble      h)
97 {
98         lglBarcode         *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         {
107                 h = w * aspect_ratio;
108         }
109         else
110         {
111                 w = h / aspect_ratio;
112         }
113
114         /* Now determine pixel size. */
115         pixel_size = w / i_width;
116         if ( pixel_size < MIN_PIXEL_SIZE )
117         {
118                 pixel_size = MIN_PIXEL_SIZE;
119         }
120
121         gbc = lgl_barcode_new ();
122
123         /* Now traverse the code string and create a list of boxes */
124         for ( y = i_height-1; y >= 0; y-- )
125         {
126
127                 for ( x = 0; x < i_width; x++ )
128                 {
129
130                         if (*grid++)
131                         {
132                                 lgl_barcode_add_box (gbc, x*pixel_size, y*pixel_size, pixel_size, pixel_size);
133                         }
134
135                 }
136
137         }
138
139         /* Fill in other info */
140         gbc->height = i_height * pixel_size;
141         gbc->width  = i_width  * pixel_size;
142
143         return gbc;
144 }
145
146 #endif /* HAVE_LIBIEC16022 */
147
148
149
150 /*
151  * Local Variables:       -- emacs
152  * mode: C                -- emacs
153  * c-basic-offset: 8      -- emacs
154  * tab-width: 8           -- emacs
155  * indent-tabs-mode: nil  -- emacs
156  * End:                   -- emacs
157  */