31#include "KReportRenderObjects.h"
32#include "kreportplugin_debug.h"
34static const int SETA = 0;
35static const int SETB = 1;
36static const int SETC = 2;
38static const char FNC1 = (char)130;
39static const char FNC2 = (char)131;
40static const char FNC3 = (char)132;
41static const char FNC4 = (char)133;
42static const char SHIFT = (char)134;
43static const char CODEA = (char)135;
44static const char CODEB = (char)136;
45static const char CODEC = (char)137;
46static const char STARTA = (char)138;
47static const char STARTB = (char)139;
48static const char STARTC = (char)140;
61static const struct code128 _128codes[] = {
63 {
' ',
' ', 0, { 2, 1, 2, 2, 2, 2 },
false },
64 {
'!',
'!', 1, { 2, 2, 2, 1, 2, 2 },
false },
65 {
'"',
'"', 2, { 2, 2, 2, 2, 2, 1 },
false },
66 {
'#',
'#', 3, { 1, 2, 1, 2, 2, 3 },
false },
67 {
'$',
'$', 4, { 1, 2, 1, 3, 2, 2 },
false },
68 {
'%',
'%', 5, { 1, 3, 1, 2, 2, 2 },
false },
69 {
'&',
'&', 6, { 1, 2, 2, 2, 1, 3 },
false },
70 {
'\'',
'\'', 7, { 1, 2, 2, 3, 1, 2 },
false },
71 {
'(',
'(', 8, { 1, 3, 2, 2, 1, 2 },
false },
72 {
')',
')', 9, { 2, 2, 1, 2, 1, 3 },
false },
73 {
'*',
'*', 10, { 2, 2, 1, 3, 1, 2 },
false },
74 {
'+',
'+', 11, { 2, 3, 1, 2, 1, 2 },
false },
75 {
',',
',', 12, { 1, 1, 2, 2, 3, 2 },
false },
76 {
'-',
'-', 13, { 1, 2, 2, 1, 3, 2 },
false },
77 {
'.',
'.', 14, { 1, 2, 2, 2, 3, 1 },
false },
78 {
'/',
'/', 15, { 1, 1, 3, 2, 2, 2 },
false },
79 {
'0',
'0', 16, { 1, 2, 3, 1, 2, 2 },
false },
80 {
'1',
'1', 17, { 1, 2, 3, 2, 2, 1 },
false },
81 {
'2',
'2', 18, { 2, 2, 3, 2, 1, 1 },
false },
82 {
'3',
'3', 19, { 2, 2, 1, 1, 3, 2 },
false },
83 {
'4',
'4', 20, { 2, 2, 1, 2, 3, 1 },
false },
84 {
'5',
'5', 21, { 2, 1, 3, 2, 1, 2 },
false },
85 {
'6',
'6', 22, { 2, 2, 3, 1, 1, 2 },
false },
86 {
'7',
'7', 23, { 3, 1, 2, 1, 3, 1 },
false },
87 {
'8',
'8', 24, { 3, 1, 1, 2, 2, 2 },
false },
88 {
'9',
'9', 25, { 3, 2, 1, 1, 2, 2 },
false },
89 {
':',
':', 26, { 3, 2, 1, 2, 2, 1 },
false },
90 {
';',
';', 27, { 3, 1, 2, 2, 1, 2 },
false },
91 {
'<',
'<', 28, { 3, 2, 2, 1, 1, 2 },
false },
92 {
'=',
'=', 29, { 3, 2, 2, 2, 1, 1 },
false },
93 {
'>',
'>', 30, { 2, 1, 2, 1, 2, 3 },
false },
94 {
'?',
'?', 31, { 2, 1, 2, 3, 2, 1 },
false },
95 {
'@',
'@', 32, { 2, 3, 2, 1, 2, 1 },
false },
96 {
'A',
'A', 33, { 1, 1, 1, 3, 2, 3 },
false },
97 {
'B',
'B', 34, { 1, 3, 1, 1, 2, 3 },
false },
98 {
'C',
'C', 35, { 1, 3, 1, 3, 2, 1 },
false },
99 {
'D',
'D', 36, { 1, 1, 2, 3, 1, 3 },
false },
100 {
'E',
'E', 37, { 1, 3, 2, 1, 1, 3 },
false },
101 {
'F',
'F', 38, { 1, 3, 2, 3, 1, 1 },
false },
102 {
'G',
'G', 39, { 2, 1, 1, 3, 1, 3 },
false },
103 {
'H',
'H', 40, { 2, 3, 1, 1, 1, 3 },
false },
104 {
'I',
'I', 41, { 2, 3, 1, 3, 1, 1 },
false },
105 {
'J',
'J', 42, { 1, 1, 2, 1, 3, 3 },
false },
106 {
'K',
'K', 43, { 1, 1, 2, 3, 3, 1 },
false },
107 {
'L',
'L', 44, { 1, 3, 2, 1, 3, 1 },
false },
108 {
'M',
'M', 45, { 1, 1, 3, 1, 2, 3 },
false },
109 {
'N',
'N', 46, { 1, 1, 3, 3, 2, 1 },
false },
110 {
'O',
'O', 47, { 1, 3, 3, 1, 2, 1 },
false },
111 {
'P',
'P', 48, { 3, 1, 3, 1, 2, 1 },
false },
112 {
'Q',
'Q', 49, { 2, 1, 1, 3, 3, 1 },
false },
113 {
'R',
'R', 50, { 2, 3, 1, 1, 3, 1 },
false },
114 {
'S',
'S', 51, { 2, 1, 3, 1, 1, 3 },
false },
115 {
'T',
'T', 52, { 2, 1, 3, 3, 1, 1 },
false },
116 {
'U',
'U', 53, { 2, 1, 3, 1, 3, 1 },
false },
117 {
'V',
'V', 54, { 3, 1, 1, 1, 2, 3 },
false },
118 {
'W',
'W', 55, { 3, 1, 1, 3, 2, 1 },
false },
119 {
'X',
'X', 56, { 3, 3, 1, 1, 2, 1 },
false },
120 {
'Y',
'Y', 57, { 3, 1, 2, 1, 1, 3 },
false },
121 {
'Z',
'Z', 58, { 3, 1, 2, 3, 1, 1 },
false },
122 {
'[',
'[', 59, { 3, 3, 2, 1, 1, 1 },
false },
123 {
'\\',
'\\', 60, { 3, 1, 4, 1, 1, 1 },
false },
124 {
']',
']', 61, { 2, 2, 1, 4, 1, 1 },
false },
125 {
'^',
'^', 62, { 4, 3, 1, 1, 1, 1 },
false },
126 {
'_',
'_', 63, { 1, 1, 1, 2, 2, 4 },
false },
127 { 0x00,
'`', 64, { 1, 1, 1, 4, 2, 2 },
false },
128 { 0x01,
'a', 65, { 1, 2, 1, 1, 2, 4 },
false },
129 { 0x02,
'b', 66, { 1, 2, 1, 4, 2, 1 },
false },
130 { 0x03,
'c', 67, { 1, 4, 1, 1, 2, 2 },
false },
131 { 0x04,
'd', 68, { 1, 4, 1, 2, 2, 1 },
false },
132 { 0x05,
'e', 69, { 1, 1, 2, 2, 1, 4 },
false },
133 { 0x06,
'f', 70, { 1, 1, 2, 4, 1, 2 },
false },
134 { 0x07,
'g', 71, { 1, 2, 2, 1, 1, 4 },
false },
135 { 0x08,
'h', 72, { 1, 2, 2, 4, 1, 1 },
false },
136 { 0x09,
'i', 73, { 1, 4, 2, 1, 1, 2 },
false },
137 { 0x0A,
'j', 74, { 1, 4, 2, 2, 1, 1 },
false },
138 { 0x0B,
'k', 75, { 2, 4, 1, 2, 1, 1 },
false },
139 { 0x0C,
'l', 76, { 2, 2, 1, 1, 1, 4 },
false },
140 { 0x0D,
'm', 77, { 4, 1, 3, 1, 1, 1 },
false },
141 { 0x0E,
'n', 78, { 2, 4, 1, 1, 1, 2 },
false },
142 { 0x0F,
'o', 79, { 1, 3, 4, 1, 1, 1 },
false },
143 { 0x10,
'p', 80, { 1, 1, 1, 2, 4, 2 },
false },
144 { 0x11,
'q', 81, { 1, 2, 1, 1, 4, 2 },
false },
145 { 0x12,
'r', 82, { 1, 2, 1, 2, 4, 1 },
false },
146 { 0x13,
's', 83, { 1, 1, 4, 2, 1, 2 },
false },
147 { 0x14,
't', 84, { 1, 2, 4, 1, 1, 2 },
false },
148 { 0x15,
'u', 85, { 1, 2, 4, 2, 1, 1 },
false },
149 { 0x16,
'v', 86, { 4, 1, 1, 2, 1, 2 },
false },
150 { 0x17,
'w', 87, { 4, 2, 1, 1, 1, 2 },
false },
151 { 0x18,
'x', 88, { 4, 2, 1, 2, 1, 1 },
false },
152 { 0x19,
'y', 89, { 2, 1, 2, 1, 4, 1 },
false },
153 { 0x1A,
'z', 90, { 2, 1, 4, 1, 2, 1 },
false },
154 { 0x1B,
'{', 91, { 4, 1, 2, 1, 2, 1 },
false },
155 { 0x1C,
'|', 92, { 1, 1, 1, 1, 4, 3 },
false },
156 { 0x1D,
'}', 93, { 1, 1, 1, 3, 4, 1 },
false },
157 { 0x1E,
'~', 94, { 1, 3, 1, 1, 4, 1 },
false },
158 { 0x1F, 0x7F, 95, { 1, 1, 4, 1, 1, 3 },
false },
159 { FNC3, FNC3, 96, { 1, 1, 4, 3, 1, 1 },
false },
160 { FNC2, FNC2, 97, { 4, 1, 1, 1, 1, 3 },
false },
161 {
SHIFT,
SHIFT, 98, { 4, 1, 1, 3, 1, 1 },
false },
162 { CODEC, CODEC, 99, { 1, 1, 3, 1, 4, 1 },
false },
163 { CODEB, FNC4, CODEB, { 1, 1, 4, 1, 3, 1 },
false },
164 { FNC4, CODEA, CODEA, { 3, 1, 1, 1, 4, 1 },
false },
165 { FNC1, FNC1, FNC1, { 4, 1, 1, 1, 3, 1 },
false },
166 { STARTA, STARTA, STARTA, { 2, 1, 1, 4, 1, 2 },
false },
167 { STARTB, STARTB, STARTB, { 2, 1, 1, 2, 1, 4 },
false },
168 { STARTC, STARTC, STARTC, { 2, 1, 1, 2, 3, 2 },
false },
170 {
'\0',
'\0',
'\0', { 0, 0, 0, 0, 0, 0 },
true }
175int code128IndexP(
QChar code,
int set)
177 const char latin1Code = code.
toLatin1();
178 for (
int idx = 0; _128codes[idx]._null ==
false; idx++) {
179 if (set == SETA && _128codes[idx].codea == latin1Code)
return idx;
180 if (set == SETB && _128codes[idx].codeb == latin1Code)
return idx;
181 if (set == SETC && _128codes[idx].codec == latin1Code)
return idx;
199 for (
int i = 0; i < _str.
length(); ++i) {
201 rank_a += (code128IndexP(c, SETA) != -1 ? 1 : 0);
202 rank_b += (code128IndexP(c, SETB) != -1 ? 1 : 0);
205 if (rank_c == _str.
length() && ((rank_c % 2) == 0 || rank_c > 4)) {
209 if ((rank_c % 2) == 1) {
219 for (; i < _str.
length(); i += 2) {
232 int set = (rank_a > rank_b ? SETA : SETB);
233 str.
push_back((rank_a > rank_b ? 103 : 104));
234 for (
int i = 0; i < _str.
length(); ++i) {
236 int v = code128IndexP(c, set);
238 v = code128IndexP(c, (set == SETA ? SETB : SETA));
251 int checksum = str.
at(0);
252 for (
int i = 1; i < str.
size(); ++i) {
253 checksum += (str.
at(i) * i);
255 checksum = checksum % 103;
262 int quiet_zone = bar_width * 10;
266 int draw_width = r.
width();
267 int draw_height = r.
height();
270 int val_length = str.
size() - 2;
282 L = (((11 * C) + 35) * X);
294 int nqz = (draw_width - L) / 2;
295 if (nqz > quiet_zone) quiet_zone = nqz;
297 quiet_zone = draw_width - (L + quiet_zone);
301 int pos = r.
left() + quiet_zone;
312 pPainter->
setPen(oneWide);
318 for (
int i = 0; i < str.
size(); ++i) {
321 if (idx < 0 || idx > 105) {
322 kreportpluginWarning() <<
"Encountered a non-compliant element while rendering a 3of9 barcode -- skipping";
326 for (
int b = 0; b < 6; b++, space = !space) {
327 int w = _128codes[idx].values[b] * bar_width;
328 if (!space && pPainter) {
337 int STOP_CHARACTER[] = { 2, 3, 3, 1, 1, 1, 2 };
339 for (
int b = 0; b < 7; b++, space = !space) {
340 int w = STOP_CHARACTER[b] * bar_width;
341 if (!space && pPainter) {
char toLatin1() const const
const_reference at(qsizetype i) const const
void push_back(parameter_type value)
qsizetype size() const const
void fillRect(const QRect &rectangle, QGradient::Preset preset)
const QPen & pen() const const
void setBrush(Qt::BrushStyle style)
void setPen(Qt::PenStyle style)
QColor color() const const
const QChar at(qsizetype position) const const
bool isEmpty() const const
qsizetype length() const const