~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to libs/koreport/wrtembed/codeeanpaint.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * OpenRPT report writer and rendering engine
3
 
 * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com)
4
 
 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
5
 
 *
6
 
 * This library is free software; you can redistribute it and/or
7
 
 * modify it under the terms of the GNU Lesser General Public
8
 
 * License as published by the Free Software Foundation; either
9
 
 * version 2.1 of the License, or (at your option) any later version.
10
 
 *
11
 
 * This library is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
 
 * Lesser General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU Lesser General Public
17
 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
 
20
 
/*
21
 
 *     This file contains the implementation of the Code EAN and similar
22
 
 * formats for rendering purposes. All this code assumes a 100dpi
23
 
 * rendering surface for it's calculations.
24
 
 */
25
 
 
26
 
#include <QString>
27
 
#include <QVector>
28
 
#include <QRect>
29
 
#include <QPainter>
30
 
#include <QPen>
31
 
#include <QBrush>
32
 
 
33
 
#include "renderobjects.h"
34
 
 
35
 
static const int LEFTHAND_ODD = 0;
36
 
static const int LEFTHAND_EVEN = 1;
37
 
static const int RIGHTHAND = 2;
38
 
 
39
 
static int _encodings[10][3][7] = {
40
 
    /*   LEFTHAND_ODD   */  /*   LEFTHAND_EVEN   */  /*     RIGHTHAND     */
41
 
    { { 0, 0, 0, 1, 1, 0, 1}, { 0, 1, 0, 0, 1, 1, 1 }, { 1, 1, 1, 0, 0, 1, 0 } }, // 0
42
 
    { { 0, 0, 1, 1, 0, 0, 1}, { 0, 1, 1, 0, 0, 1, 1 }, { 1, 1, 0, 0, 1, 1, 0 } }, // 1
43
 
    { { 0, 0, 1, 0, 0, 1, 1}, { 0, 0, 1, 1, 0, 1, 1 }, { 1, 1, 0, 1, 1, 0, 0 } }, // 2
44
 
    { { 0, 1, 1, 1, 1, 0, 1}, { 0, 1, 0, 0, 0, 0, 1 }, { 1, 0, 0, 0, 0, 1, 0 } }, // 3
45
 
    { { 0, 1, 0, 0, 0, 1, 1}, { 0, 0, 1, 1, 1, 0, 1 }, { 1, 0, 1, 1, 1, 0, 0 } }, // 4
46
 
    { { 0, 1, 1, 0, 0, 0, 1}, { 0, 1, 1, 1, 0, 0, 1 }, { 1, 0, 0, 1, 1, 1, 0 } }, // 5
47
 
    { { 0, 1, 0, 1, 1, 1, 1}, { 0, 0, 0, 0, 1, 0, 1 }, { 1, 0, 1, 0, 0, 0, 0 } }, // 6
48
 
    { { 0, 1, 1, 1, 0, 1, 1}, { 0, 0, 1, 0, 0, 0, 1 }, { 1, 0, 0, 0, 1, 0, 0 } }, // 7
49
 
    { { 0, 1, 1, 0, 1, 1, 1}, { 0, 0, 0, 1, 0, 0, 1 }, { 1, 0, 0, 1, 0, 0, 0 } }, // 8
50
 
    { { 0, 0, 0, 1, 0, 1, 1}, { 0, 0, 1, 0, 1, 1, 1 }, { 1, 1, 1, 0, 1, 0, 0 } }  // 9
51
 
};
52
 
 
53
 
static const int odd = LEFTHAND_ODD;
54
 
static const int even = LEFTHAND_EVEN;
55
 
 
56
 
static int _parity[10][6] = {
57
 
    { odd,  odd,  odd,  odd,  odd,  odd }, // 0
58
 
    { odd,  odd, even,  odd, even, even }, // 1
59
 
    { odd,  odd, even, even,  odd, even }, // 2
60
 
    { odd,  odd, even, even, even,  odd }, // 3
61
 
    { odd, even,  odd,  odd, even, even }, // 4
62
 
    { odd, even, even,  odd,  odd, even }, // 5
63
 
    { odd, even, even, even,  odd,  odd }, // 6
64
 
    { odd, even,  odd, even,  odd, even }, // 7
65
 
    { odd, even,  odd, even, even,  odd }, // 8
66
 
    { odd, even, even,  odd, even,  odd }  // 9
67
 
};
68
 
 
69
 
static int _upcparenc[10][2][6] = {
70
 
    /*             PARITY 0             */  /*             PARITY 1             */
71
 
    { { even, even, even,  odd,  odd,  odd }, {  odd,  odd,  odd, even, even, even } }, // 0
72
 
    { { even, even,  odd, even,  odd,  odd }, {  odd,  odd, even,  odd, even, even } }, // 1
73
 
    { { even, even,  odd,  odd, even,  odd }, {  odd,  odd, even, even,  odd, even } }, // 2
74
 
    { { even, even,  odd,  odd,  odd, even }, {  odd,  odd, even, even, even,  odd } }, // 3
75
 
    { { even,  odd, even, even,  odd,  odd }, {  odd, even,  odd,  odd, even, even } }, // 4
76
 
    { { even,  odd,  odd, even, even,  odd }, {  odd, even, even,  odd,  odd, even } }, // 5
77
 
    { { even,  odd,  odd,  odd, even, even }, {  odd, even, even, even,  odd,  odd } }, // 6
78
 
    { { even,  odd, even,  odd, even,  odd }, {  odd, even,  odd, even,  odd, even } }, // 7
79
 
    { { even,  odd, even,  odd,  odd, even }, {  odd, even,  odd, even, even,  odd } }, // 8
80
 
    { { even,  odd,  odd, even,  odd, even }, {  odd, even, even,  odd, even,  odd } }  // 9
81
 
};
82
 
 
83
 
 
84
 
void renderCodeEAN13(const QRect & r, const QString & _str, int align, QPainter * pPainter)
85
 
{
86
 
    int val[13];
87
 
    int i = 0;
88
 
 
89
 
    // initialize all the values just so we can be predictable
90
 
    for (i = 0; i < 13; ++i) {
91
 
        val[i] = -1;
92
 
    }
93
 
 
94
 
    // verify that the passed in string is valid
95
 
    // if it's not either twelve or thirteen characters
96
 
    // then it must be invalid to begin with
97
 
    if (_str.length() != 12 && _str.length() != 13) return;
98
 
    // loop through and convert each char to a digit.
99
 
    // if we can't convert all characters then this is
100
 
    // an invalid number
101
 
    for (i = 0; i < _str.length(); ++i) {
102
 
        val[i] = ((QChar) _str.at(i)).digitValue();
103
 
        if (val[i] == -1) return;
104
 
    }
105
 
 
106
 
    // calculate and append the checksum value
107
 
    int old_sum = val[12]; // get the old check sum value (-1 if none was set)
108
 
    int checksum = 0;
109
 
    for (i = 0; i < 12; ++i) {
110
 
        checksum += val[i] * (i % 2 ? 3 : 1);
111
 
    }
112
 
    checksum = (checksum % 10);
113
 
    if (checksum) checksum = 10 - checksum;
114
 
    val[12] = checksum;
115
 
 
116
 
    // if we had an old checksum value and if it doesn't match what we came
117
 
    // up with then the string must be invalid so we will bail
118
 
    if (old_sum != -1 && old_sum != checksum) return;
119
 
 
120
 
 
121
 
    // lets determine some core attributes about this barcode
122
 
    int bar_width = 1; // the width of the base unit bar
123
 
 
124
 
    // this is are mandatory minimum quiet zone
125
 
    int quiet_zone = bar_width * 10;
126
 
    if (quiet_zone < 10) quiet_zone = 10;
127
 
 
128
 
    // what kind of area do we have to work with
129
 
    int draw_width = r.width();
130
 
    int draw_height = r.height() - 2;
131
 
 
132
 
    // L = 95X
133
 
    // L length of barcode (excluding quite zone) in units same as X and I
134
 
    // X the width of a bar (pixels in our case)
135
 
    int L;
136
 
 
137
 
    int X = bar_width;
138
 
 
139
 
    L = (95 * X);
140
 
 
141
 
    // now we have the actual width the barcode will be so can determine the actual
142
 
    // size of the quiet zone (we assume we center the barcode in the given area
143
 
    // what should we do if the area is too small????
144
 
    // At the moment the way the code is written is we will always start at the minimum
145
 
    // required quiet zone if we don't have enough space.... I guess we'll just have over-run
146
 
    // to the right
147
 
    //
148
 
    // calculate the starting position based on the alignment option
149
 
    // for left align we don't need to do anything as the values are already setup for it
150
 
    if (align == 1) {   // center
151
 
        int nqz = (draw_width - L) / 2;
152
 
        if (nqz > quiet_zone) quiet_zone = nqz;
153
 
    } else if (align > 1) {  // right
154
 
        quiet_zone = draw_width - (L + quiet_zone);
155
 
    } // else if(align < 1) {} // left : do nothing
156
 
 
157
 
    int pos = r.left() + quiet_zone;
158
 
    int top = r.top();
159
 
 
160
 
    if (pPainter) {
161
 
        pPainter->save();
162
 
 
163
 
        QPen oneWide(pPainter->pen());
164
 
        oneWide.setWidth(1);
165
 
#ifndef Q_WS_WIN32
166
 
        oneWide.setJoinStyle(Qt::MiterJoin);
167
 
#endif
168
 
        pPainter->setPen(oneWide);
169
 
        pPainter->setBrush(pPainter->pen().color());
170
 
 
171
 
        int b = 0, w = 0;
172
 
 
173
 
        // render open guard
174
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
175
 
        pos += 2;
176
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
177
 
        pos ++;
178
 
 
179
 
        // render first set
180
 
        for (i = 0; i < 6; ++i) {
181
 
            b = val[i+1];
182
 
            for (w = 0; w < 7; ++w) {
183
 
                if (_encodings[b][_parity[val[0]][i]][w]) {
184
 
                    pPainter->fillRect(pos, top, 1, draw_height - 7, pPainter->pen().color());
185
 
                }
186
 
                pos++;
187
 
            }
188
 
        }
189
 
 
190
 
        // render center guard
191
 
        pos++;
192
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
193
 
        pos += 2;
194
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
195
 
        pos += 2;
196
 
 
197
 
        // render last set
198
 
        for (i = 0; i < 6; ++i) {
199
 
            b = val[i+7];
200
 
            for (w = 0; w < 7; ++w) {
201
 
                if (_encodings[b][RIGHTHAND][w]) {
202
 
                    pPainter->fillRect(pos, top, 1, draw_height - 7, pPainter->pen().color());
203
 
                }
204
 
                pos++;
205
 
            }
206
 
        }
207
 
 
208
 
        // render close guard
209
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
210
 
        pos += 2;
211
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
212
 
 
213
 
        QString parstr = QString("%1").arg(val[0]);
214
 
        QString leftstr = QString().sprintf("%d%d%d%d%d%d",
215
 
                                            val[1], val[2], val[3], val[4], val[5], val[6]);
216
 
        QString rightstr = QString().sprintf("%d%d%d%d%d%d",
217
 
                                             val[7], val[8], val[9], val[10], val[11], val[12]);
218
 
        pPainter->setFont(QFont("Arial", 6));
219
 
        pPainter->drawText(r.left(), r.top() + draw_height - 12,
220
 
                           quiet_zone - 2, 12, Qt::AlignRight | Qt::AlignTop,
221
 
                           parstr);
222
 
        pPainter->drawText(r.left() + quiet_zone + 3,
223
 
                           (r.top() + draw_height) - 7,
224
 
                           42, 10, Qt::AlignHCenter | Qt::AlignTop,
225
 
                           leftstr);
226
 
        pPainter->drawText(r.left() + quiet_zone + 50,
227
 
                           (r.top() + draw_height) - 7,
228
 
                           42, 10, Qt::AlignHCenter | Qt::AlignTop,
229
 
                           rightstr);
230
 
 
231
 
        pPainter->restore();
232
 
    }
233
 
}
234
 
 
235
 
void renderCodeUPCA(const QRect & r, const QString & _str, int align, QPainter * pPainter)
236
 
{
237
 
    int val[13];
238
 
    int i = 0;
239
 
 
240
 
    // initialize all the values just so we can be predictable
241
 
    for (i = 0; i < 13; ++i) {
242
 
        val[i] = -1;
243
 
    }
244
 
 
245
 
    // verify that the passed in string is valid
246
 
    // if it's not either twelve or thirteen characters
247
 
    // then it must be invalid to begin with
248
 
    if (_str.length() != 11 && _str.length() != 12) return;
249
 
    // loop through and convert each char to a digit.
250
 
    // if we can't convert all characters then this is
251
 
    // an invalid number
252
 
    val[0] = 0;
253
 
    for (i = 0; i < _str.length(); ++i) {
254
 
        val[i+1] = ((QChar) _str.at(i)).digitValue();
255
 
        if (val[i+1] == -1) return;
256
 
    }
257
 
 
258
 
    // calculate and append the checksum value
259
 
    int old_sum = val[12]; // get the old check sum value (-1 if none was set)
260
 
    int checksum = 0;
261
 
    for (i = 0; i < 12; ++i) {
262
 
        checksum += val[i] * (i % 2 ? 3 : 1);
263
 
    }
264
 
    checksum = (checksum % 10);
265
 
    if (checksum) checksum = 10 - checksum;
266
 
    val[12] = checksum;
267
 
 
268
 
    // if we had an old checksum value and if it doesn't match what we came
269
 
    // up with then the string must be invalid so we will bail
270
 
    if (old_sum != -1 && old_sum != checksum) return;
271
 
 
272
 
 
273
 
    // lets determine some core attributes about this barcode
274
 
    int bar_width = 1; // the width of the base unit bar
275
 
 
276
 
    // this is are mandatory minimum quiet zone
277
 
    int quiet_zone = bar_width * 10;
278
 
    if (quiet_zone < 10) quiet_zone = 10;
279
 
 
280
 
    // what kind of area do we have to work with
281
 
    int draw_width = r.width();
282
 
    int draw_height = r.height() - 2;
283
 
 
284
 
    // L = 95X
285
 
    // L length of barcode (excluding quite zone) in units same as X and I
286
 
    // X the width of a bar (pixels in our case)
287
 
    int L;
288
 
 
289
 
    int X = bar_width;
290
 
 
291
 
    L = (95 * X);
292
 
 
293
 
    // now we have the actual width the barcode will be so can determine the actual
294
 
    // size of the quiet zone (we assume we center the barcode in the given area
295
 
    // what should we do if the area is too small????
296
 
    // At the moment the way the code is written is we will always start at the minimum
297
 
    // required quiet zone if we don't have enough space.... I guess we'll just have over-run
298
 
    // to the right
299
 
    //
300
 
    // calculate the starting position based on the alignment option
301
 
    // for left align we don't need to do anything as the values are already setup for it
302
 
    if (align == 1) {   // center
303
 
        int nqz = (draw_width - L) / 2;
304
 
        if (nqz > quiet_zone) quiet_zone = nqz;
305
 
    } else if (align > 1) {  // right
306
 
        quiet_zone = draw_width - (L + quiet_zone);
307
 
    } // else if(align < 1) {} // left : do nothing
308
 
 
309
 
    int pos = r.left() + quiet_zone;
310
 
    int top = r.top();
311
 
 
312
 
    if (pPainter) {
313
 
        pPainter->save();
314
 
 
315
 
        QPen oneWide(pPainter->pen());
316
 
        oneWide.setWidth(1);
317
 
#ifndef Q_WS_WIN32
318
 
        oneWide.setJoinStyle(Qt::MiterJoin);
319
 
#endif
320
 
        pPainter->setPen(oneWide);
321
 
        pPainter->setBrush(pPainter->pen().color());
322
 
 
323
 
        int b = 0, w = 0;
324
 
 
325
 
        // render open guard
326
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
327
 
        pos += 2;
328
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
329
 
        pos ++;
330
 
 
331
 
        // render first set
332
 
        for (i = 0; i < 6; ++i) {
333
 
            b = val[i+1];
334
 
            for (w = 0; w < 7; ++w) {
335
 
                if (_encodings[b][_parity[val[0]][i]][w]) {
336
 
                    pPainter->fillRect(pos, top, 1, draw_height - (i == 0 ? 0 : 7), pPainter->pen().color());
337
 
                }
338
 
                pos++;
339
 
            }
340
 
        }
341
 
 
342
 
        // render center guard
343
 
        pos++;
344
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
345
 
        pos += 2;
346
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
347
 
        pos += 2;
348
 
 
349
 
        // render last set
350
 
        for (i = 0; i < 6; ++i) {
351
 
            b = val[i+7];
352
 
            for (w = 0; w < 7; ++w) {
353
 
                if (_encodings[b][RIGHTHAND][w]) {
354
 
                    pPainter->fillRect(pos, top, 1, draw_height - (i == 5 ? 0 : 7), pPainter->pen().color());
355
 
                }
356
 
                pos++;
357
 
            }
358
 
        }
359
 
 
360
 
        // render close guard
361
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
362
 
        pos += 2;
363
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
364
 
 
365
 
        QString parstr = QString("%1").arg(val[1]);
366
 
        QString chkstr = QString("%1").arg(val[12]);
367
 
        QString leftstr = QString().sprintf("%d%d%d%d%d",
368
 
                                            val[2], val[3], val[4], val[5], val[6]);
369
 
        QString rightstr = QString().sprintf("%d%d%d%d%d",
370
 
                                             val[7], val[8], val[9], val[10], val[11]);
371
 
        pPainter->setFont(QFont("Arial", 6));
372
 
        pPainter->drawText(r.left(), r.top() + draw_height - 12,
373
 
                           quiet_zone - 2, 12, Qt::AlignRight | Qt::AlignTop,
374
 
                           parstr);
375
 
        pPainter->drawText(r.left() + quiet_zone + 10,
376
 
                           (r.top() + draw_height) - 7,
377
 
                           35, 10, Qt::AlignHCenter | Qt::AlignTop,
378
 
                           leftstr);
379
 
        pPainter->drawText(r.left() + quiet_zone + 50,
380
 
                           (r.top() + draw_height) - 7,
381
 
                           35, 10, Qt::AlignHCenter | Qt::AlignTop,
382
 
                           rightstr);
383
 
        pPainter->drawText(r.left() + quiet_zone + L + 2, r.top() + draw_height - 12,
384
 
                           8, 12, Qt::AlignLeft | Qt::AlignTop,
385
 
                           chkstr);
386
 
 
387
 
        pPainter->restore();
388
 
    }
389
 
}
390
 
 
391
 
void renderCodeEAN8(const QRect & r, const QString & _str, int align, QPainter * pPainter)
392
 
{
393
 
    int val[8];
394
 
    int i = 0;
395
 
 
396
 
    // initialize all the values just so we can be predictable
397
 
    for (i = 0; i < 8; ++i) {
398
 
        val[i] = -1;
399
 
    }
400
 
 
401
 
    // verify that the passed in string is valid
402
 
    // if it's not either twelve or thirteen characters
403
 
    // then it must be invalid to begin with
404
 
    if (_str.length() != 7 && _str.length() != 8) return;
405
 
    // loop through and convert each char to a digit.
406
 
    // if we can't convert all characters then this is
407
 
    // an invalid number
408
 
    for (i = 0; i < _str.length(); ++i) {
409
 
        val[i] = ((QChar) _str.at(i)).digitValue();
410
 
        if (val[i] == -1) return;
411
 
    }
412
 
 
413
 
    // calculate and append the checksum value
414
 
    int old_sum = val[7]; // get the old check sum value (-1 if none was set)
415
 
    int checksum = 0;
416
 
    for (i = 0; i < 7; ++i) {
417
 
        checksum += val[i] * (i % 2 ? 1 : 3);
418
 
    }
419
 
    checksum = (checksum % 10);
420
 
    if (checksum) checksum = 10 - checksum;
421
 
    val[7] = checksum;
422
 
 
423
 
    // if we had an old checksum value and if it doesn't match what we came
424
 
    // up with then the string must be invalid so we will bail
425
 
    if (old_sum != -1 && old_sum != checksum) return;
426
 
 
427
 
 
428
 
    // lets determine some core attributes about this barcode
429
 
    int bar_width = 1; // the width of the base unit bar
430
 
 
431
 
    // this is are mandatory minimum quiet zone
432
 
    int quiet_zone = bar_width * 10;
433
 
    if (quiet_zone < 10) quiet_zone = 10;
434
 
 
435
 
    // what kind of area do we have to work with
436
 
    int draw_width = r.width();
437
 
    int draw_height = r.height() - 2;
438
 
 
439
 
    // L = 60X
440
 
    // L length of barcode (excluding quite zone) in units same as X and I
441
 
    // X the width of a bar (pixels in our case)
442
 
    int L;
443
 
 
444
 
    int X = bar_width;
445
 
 
446
 
    L = (67 * X);
447
 
 
448
 
    // now we have the actual width the barcode will be so can determine the actual
449
 
    // size of the quiet zone (we assume we center the barcode in the given area
450
 
    // what should we do if the area is too small????
451
 
    // At the moment the way the code is written is we will always start at the minimum
452
 
    // required quiet zone if we don't have enough space.... I guess we'll just have over-run
453
 
    // to the right
454
 
    //
455
 
    // calculate the starting position based on the alignment option
456
 
    // for left align we don't need to do anything as the values are already setup for it
457
 
    if (align == 1) {   // center
458
 
        int nqz = (draw_width - L) / 2;
459
 
        if (nqz > quiet_zone) quiet_zone = nqz;
460
 
    } else if (align > 1) {  // right
461
 
        quiet_zone = draw_width - (L + quiet_zone);
462
 
    } // else if(align < 1) {} // left : do nothing
463
 
 
464
 
    int pos = r.left() + quiet_zone;
465
 
    int top = r.top();
466
 
 
467
 
    if (pPainter) {
468
 
        pPainter->save();
469
 
 
470
 
        QPen oneWide(pPainter->pen());
471
 
        oneWide.setWidth(1);
472
 
#ifndef Q_WS_WIN32
473
 
        oneWide.setJoinStyle(Qt::MiterJoin);
474
 
#endif
475
 
        pPainter->setPen(oneWide);
476
 
        pPainter->setBrush(pPainter->pen().color());
477
 
 
478
 
        int b = 0, w = 0;
479
 
 
480
 
        // render open guard
481
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
482
 
        pos += 2;
483
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
484
 
        pos ++;
485
 
 
486
 
        // render first set
487
 
        for (i = 0; i < 4; ++i) {
488
 
            b = val[i];
489
 
            for (w = 0; w < 7; ++w) {
490
 
                if (_encodings[b][LEFTHAND_ODD][w]) {
491
 
                    pPainter->fillRect(pos, top, 1, draw_height - 6, pPainter->pen().color());
492
 
                }
493
 
                pos++;
494
 
            }
495
 
        }
496
 
 
497
 
        // render center guard
498
 
        pos++;
499
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
500
 
        pos += 2;
501
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
502
 
        pos += 2;
503
 
 
504
 
        // render last set
505
 
        for (i = 0; i < 4; ++i) {
506
 
            b = val[i+4];
507
 
            for (w = 0; w < 7; ++w) {
508
 
                if (_encodings[b][RIGHTHAND][w]) {
509
 
                    pPainter->fillRect(pos, top, 1, draw_height - 6, pPainter->pen().color());
510
 
                }
511
 
                pos++;
512
 
            }
513
 
        }
514
 
 
515
 
        // render close guard
516
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
517
 
        pos += 2;
518
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
519
 
 
520
 
        QString leftstr = QString().sprintf("%d%d%d%d",
521
 
                                            val[0], val[1], val[2], val[3]);
522
 
        QString rightstr = QString().sprintf("%d%d%d%d",
523
 
                                             val[4], val[5], val[6], val[7]);
524
 
        pPainter->setFont(QFont("Arial", 6));
525
 
        pPainter->drawText(r.left() + quiet_zone + 3,
526
 
                           (r.top() + draw_height) - 6,
527
 
                           28, 10, Qt::AlignHCenter | Qt::AlignTop,
528
 
                           leftstr);
529
 
        pPainter->drawText(r.left() + quiet_zone + 36,
530
 
                           (r.top() + draw_height) - 6,
531
 
                           28, 10, Qt::AlignHCenter | Qt::AlignTop,
532
 
                           rightstr);
533
 
 
534
 
        pPainter->restore();
535
 
    }
536
 
}
537
 
 
538
 
void renderCodeUPCE(const QRect & r, const QString & _str, int align, QPainter * pPainter)
539
 
{
540
 
    int val[8];
541
 
    int i = 0;
542
 
 
543
 
    // initialize all the values just so we can be predictable
544
 
    for (i = 0; i < 8; ++i) {
545
 
        val[i] = -1;
546
 
    }
547
 
 
548
 
    // verify that the passed in string is valid
549
 
    // if it's not either twelve or thirteen characters
550
 
    // then it must be invalid to begin with
551
 
    if (_str.length() != 8) return;
552
 
    // loop through and convert each char to a digit.
553
 
    // if we can't convert all characters then this is
554
 
    // an invalid number
555
 
    for (i = 0; i < _str.length(); ++i) {
556
 
        val[i] = ((QChar) _str.at(i)).digitValue();
557
 
        if (val[i] == -1) return;
558
 
    }
559
 
 
560
 
    // calculate and append the checksum value
561
 
    // because everything is so messed up we don't calculate
562
 
    // the checksum and require that it be passed in already
563
 
    // however we do have to verify that the first digit is
564
 
    // either 0 or 1 as that is our parity
565
 
    if (val[0] != 0 && val[0] != 1) return;
566
 
 
567
 
    // lets determine some core attributes about this barcode
568
 
    int bar_width = 1; // the width of the base unit bar
569
 
 
570
 
    // this is are mandatory minimum quiet zone
571
 
    int quiet_zone = bar_width * 10;
572
 
    if (quiet_zone < 10) quiet_zone = 10;
573
 
 
574
 
    // what kind of area do we have to work with
575
 
    int draw_width = r.width();
576
 
    int draw_height = r.height() - 2;
577
 
 
578
 
    // L = 51X
579
 
    // L length of barcode (excluding quite zone) in units same as X and I
580
 
    // X the width of a bar (pixels in our case)
581
 
    int L;
582
 
 
583
 
    int X = bar_width;
584
 
 
585
 
    L = (51 * X);
586
 
 
587
 
    // now we have the actual width the barcode will be so can determine the actual
588
 
    // size of the quiet zone (we assume we center the barcode in the given area
589
 
    // what should we do if the area is too small????
590
 
    // At the moment the way the code is written is we will always start at the minimum
591
 
    // required quiet zone if we don't have enough space.... I guess we'll just have over-run
592
 
    // to the right
593
 
    //
594
 
    // calculate the starting position based on the alignment option
595
 
    // for left align we don't need to do anything as the values are already setup for it
596
 
    if (align == 1) {   // center
597
 
        int nqz = (draw_width - L) / 2;
598
 
        if (nqz > quiet_zone) quiet_zone = nqz;
599
 
    } else if (align > 1) {  // right
600
 
        quiet_zone = draw_width - (L + quiet_zone);
601
 
    } // else if(align < 1) {} // left : do nothing
602
 
 
603
 
    int pos = r.left() + quiet_zone;
604
 
    int top = r.top();
605
 
 
606
 
    if (pPainter) {
607
 
        pPainter->save();
608
 
 
609
 
        QPen oneWide(pPainter->pen());
610
 
        oneWide.setWidth(1);
611
 
#ifndef Q_WS_WIN32
612
 
        oneWide.setJoinStyle(Qt::MiterJoin);
613
 
#endif
614
 
        pPainter->setPen(oneWide);
615
 
        pPainter->setBrush(pPainter->pen().color());
616
 
 
617
 
        int b = 0, w = 0;
618
 
 
619
 
        // render open guard
620
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
621
 
        pos += 2;
622
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
623
 
        pos ++;
624
 
 
625
 
        // render first set
626
 
        for (i = 0; i < 6; ++i) {
627
 
            b = val[i+1];
628
 
            for (w = 0; w < 7; ++w) {
629
 
                if (_encodings[b][_upcparenc[val[7]][val[0]][i]][w]) {
630
 
                    pPainter->fillRect(pos, top, 1, draw_height - 7, pPainter->pen().color());
631
 
                }
632
 
                pos++;
633
 
            }
634
 
        }
635
 
 
636
 
        // render center guard
637
 
        pos++;
638
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
639
 
        pos += 2;
640
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
641
 
        pos += 2;
642
 
 
643
 
        // render close guard
644
 
        pPainter->fillRect(pos, top, 1, draw_height, pPainter->pen().color());
645
 
 
646
 
        QString parstr = QString("%1").arg(val[0]);
647
 
        QString chkstr = QString("%1").arg(val[7]);
648
 
        QString leftstr = QString().sprintf("%d%d%d%d%d%d",
649
 
                                            val[1], val[2], val[3], val[4], val[5], val[6]);
650
 
        pPainter->setFont(QFont("Arial", 6));
651
 
        pPainter->drawText(r.left(), r.top() + draw_height - 12,
652
 
                           quiet_zone - 2, 12, Qt::AlignRight | Qt::AlignTop,
653
 
                           parstr);
654
 
        pPainter->drawText(r.left() + quiet_zone + 3,
655
 
                           (r.top() + draw_height) - 7,
656
 
                           42, 10, Qt::AlignHCenter | Qt::AlignTop,
657
 
                           leftstr);
658
 
        pPainter->drawText(r.left() + quiet_zone + L + 2, r.top() + draw_height - 12,
659
 
                           8, 12, Qt::AlignLeft | Qt::AlignTop,
660
 
                           chkstr);
661
 
 
662
 
        pPainter->restore();
663
 
    }
664
 
}
665