]> git.sur5r.net Git - glabels/blob - src/bc-iec16022.c
Organized master branch to be top-level directory for glabels, instead of
[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 #include "bc-iec16022.h"
24
25 #include <glib.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <iec16022ecc200.h>
30
31 #include "debug.h"
32
33
34 /*========================================================*/
35 /* Private macros and constants.                          */
36 /*========================================================*/
37 #define MIN_PIXEL_SIZE 1.0
38
39
40 /*===========================================*/
41 /* Local function prototypes                 */
42 /*===========================================*/
43 static glBarcode *render_iec16022 (const gchar *grid,
44                                    gint         i_width,
45                                    gint         i_height,
46                                    gdouble      w,
47                                    gdouble      h);
48
49
50 /*****************************************************************************/
51 /* Generate intermediate representation of barcode.                          */
52 /*****************************************************************************/
53 glBarcode *
54 gl_barcode_iec16022_new (const gchar    *id,
55                          gboolean        text_flag,
56                          gboolean        checksum_flag,
57                          gdouble         w,
58                          gdouble         h,
59                          const gchar    *digits)
60 {
61         gchar               *grid;
62         gint                 i_width, i_height;
63         glBarcode           *gbc;
64
65         if ( strlen (digits) == 0 )
66         {
67                 return NULL;
68         }
69
70         i_width  = 0;
71         i_height = 0;
72
73         grid = (gchar *)iec16022ecc200 (&i_width, &i_height, NULL,
74                                         strlen (digits), (unsigned char *)digits,
75                                         NULL, NULL, NULL);
76
77         /* now render with our custom back-end,
78            to create appropriate intermdediate format */
79         gbc = render_iec16022 (grid, i_width, i_height, w, h);
80
81         free (grid);
82         return gbc;
83 }
84
85
86 /*--------------------------------------------------------------------------
87  * PRIVATE.  Render to glBarcode intermediate representation of barcode.
88  *--------------------------------------------------------------------------*/
89 static glBarcode *
90 render_iec16022 (const gchar *grid,
91                  gint         i_width,
92                  gint         i_height,
93                  gdouble      w,
94                  gdouble      h)
95 {
96         glBarcode     *gbc;
97         glBarcodeLine *line;
98         gint           x, y;
99         gdouble        aspect_ratio, pixel_size;
100
101         /* Treat requested size as a bounding box, scale to maintain aspect
102          * ratio while fitting it in this bounding box. */
103         aspect_ratio = (gdouble)i_height / (gdouble)i_width;
104         if ( h > w*aspect_ratio ) {
105                 h = w * aspect_ratio;
106         } else {
107                 w = h / aspect_ratio;
108         }
109
110         /* Now determine pixel size. */
111         pixel_size = w / i_width;
112         if ( pixel_size < MIN_PIXEL_SIZE )
113         {
114                 pixel_size = MIN_PIXEL_SIZE;
115         }
116
117         gbc = g_new0 (glBarcode, 1);
118
119         /* Now traverse the code string and create a list of boxes */
120         for ( y = i_height-1; y >= 0; y-- )
121         {
122
123                 for ( x = 0; x < i_width; x++ )
124                 {
125
126                         if (*grid++)
127                         {
128                                 line = g_new0 (glBarcodeLine, 1);
129                                 line->x      = x*pixel_size + pixel_size/2.0;
130                                 line->y      = y*pixel_size;
131                                 line->length = pixel_size;
132                                 line->width  = pixel_size;
133                                 gbc->lines = g_list_append (gbc->lines, line);
134                         }
135
136                 }
137
138         }
139
140         /* Fill in other info */
141         gbc->height = i_height * pixel_size;
142         gbc->width  = i_width  * pixel_size;
143
144         return gbc;
145 }
146
147
148
149 /*
150  * Local Variables:       -- emacs
151  * mode: C                -- emacs
152  * c-basic-offset: 8      -- emacs
153  * tab-width: 8           -- emacs
154  * indent-tabs-mode: nil  -- emacs
155  * End:                   -- emacs
156  */