]> git.sur5r.net Git - glabels/blob - src/bc-iec18004.c
Allow user selection of barcode backend.
[glabels] / src / bc-iec18004.c
1 /*
2  *  bc-iec18004.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_LIBQRENCODE
24
25 #include "bc-iec18004.h"
26
27 #include <glib.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <qrencode.h>
32
33 #include "debug.h"
34
35
36 /*========================================================*/
37 /* Private macros and constants.                          */
38 /*========================================================*/
39
40 #define MIN_PIXEL_SIZE 1.0
41
42
43 /*===========================================*/
44 /* Local function prototypes                 */
45 /*===========================================*/
46 static glBarcode *render_iec18004 (const gchar *grid,
47                                    gint         i_width,
48                                    gint         i_height,
49                                    gdouble      w,
50                                    gdouble      h);
51
52
53 /*****************************************************************************/
54 /* Generate intermediate representation of barcode.                          */
55 /*****************************************************************************/
56 glBarcode *
57 gl_barcode_iec18004_new (const gchar    *id,
58                          gboolean        text_flag,
59                          gboolean        checksum_flag,
60                          gdouble         w,
61                          gdouble         h,
62                          const gchar    *digits)
63 {
64         gint             i_width, i_height;
65         glBarcode       *gbc;
66         QRcode          *qrcode;
67
68         if ( strlen (digits) == 0 )
69         {
70                 return NULL;
71         }
72
73         i_width  = 0;
74         i_height = 0;
75
76         qrcode = QRcode_encodeString ((const char *)digits, 0, QR_ECLEVEL_M,
77                                       QR_MODE_8, 1);
78         if (qrcode == NULL)
79         {
80                 return NULL;
81         }
82         
83         i_width = i_height = qrcode->width;
84         gbc = render_iec18004 ((const gchar *)qrcode->data, i_width, i_height,
85                                w, h);
86
87         QRcode_free ( qrcode );
88          
89         return gbc;
90 }
91
92
93 /*--------------------------------------------------------------------------
94  * PRIVATE.  Render to glBarcode intermediate representation of barcode.
95  *--------------------------------------------------------------------------*/
96 static glBarcode *
97 render_iec18004 (const gchar *grid,
98                  gint         i_width,
99                  gint         i_height,
100                  gdouble      w,
101                  gdouble      h)
102 {
103         glBarcode          *gbc;
104         gint                x, y;
105         gdouble             aspect_ratio, pixel_size;
106
107         /* Treat requested size as a bounding box, scale to maintain aspect
108          * ratio while fitting it in this bounding box. */
109         aspect_ratio = (gdouble)i_height / (gdouble)i_width;
110         if ( h > w*aspect_ratio ) {
111                 h = w * aspect_ratio;
112         } else {
113                 w = h / aspect_ratio;
114         }
115
116         /* Now determine pixel size. */
117         pixel_size = w / i_width;
118         if ( pixel_size < MIN_PIXEL_SIZE )
119         {
120                 pixel_size = MIN_PIXEL_SIZE;
121         }
122
123         gbc = gl_barcode_new ();
124
125         /* Now traverse the code string and create a list of boxes */
126         for ( y = 0; y < i_height; y++ )
127         {
128                 for ( x = 0; x < i_width; x++ )
129                 {
130
131                         /* Symbol data is represented as an array contains 
132                          * width*width uchars. Each uchar represents a module 
133                          * (dot). If the less significant bit of the uchar 
134                          * is 1, the corresponding module is black. The other
135                          * bits are meaningless for us. */
136                         if ((*grid++) & 1)
137                         {
138                                 gl_barcode_add_box (gbc, x*pixel_size, y*pixel_size, pixel_size, pixel_size);
139                         }
140
141                 }
142
143         }
144
145         /* Fill in other info */
146         gbc->height = i_height * pixel_size;
147         gbc->width  = i_width  * pixel_size;
148
149         return gbc;
150 }
151
152 #endif /* HAVE_LIBQRENCODE */
153
154
155
156 /*
157  * Local Variables:       -- emacs
158  * mode: C                -- emacs
159  * c-basic-offset: 8      -- emacs
160  * tab-width: 8           -- emacs
161  * indent-tabs-mode: nil  -- emacs
162  * End:                   -- emacs
163  */