30#include "KReportRenderObjects.h"
31#include "kreportplugin_debug.h"
33static const int SETA = 0;
34static const int SETB = 1;
35static const int SETC = 2;
37static const char FNC1 = (char)130;
38static const char FNC2 = (char)131;
39static const char FNC3 = (char)132;
40static const char FNC4 = (char)133;
41static const char SHIFT = (char)134;
42static const char CODEA = (char)135;
43static const char CODEB = (char)136;
44static const char CODEC = (char)137;
45static const char STARTA = (char)138;
46static const char STARTB = (char)139;
47static const char STARTC = (char)140;
60static const struct code128 _128codes[] = {
62 {
' ',
' ', 0, { 2, 1, 2, 2, 2, 2 },
false },
63 {
'!',
'!', 1, { 2, 2, 2, 1, 2, 2 },
false },
64 {
'"',
'"', 2, { 2, 2, 2, 2, 2, 1 },
false },
65 {
'#',
'#', 3, { 1, 2, 1, 2, 2, 3 },
false },
66 {
'$',
'$', 4, { 1, 2, 1, 3, 2, 2 },
false },
67 {
'%',
'%', 5, { 1, 3, 1, 2, 2, 2 },
false },
68 {
'&',
'&', 6, { 1, 2, 2, 2, 1, 3 },
false },
69 {
'\'',
'\'', 7, { 1, 2, 2, 3, 1, 2 },
false },
70 {
'(',
'(', 8, { 1, 3, 2, 2, 1, 2 },
false },
71 {
')',
')', 9, { 2, 2, 1, 2, 1, 3 },
false },
72 {
'*',
'*', 10, { 2, 2, 1, 3, 1, 2 },
false },
73 {
'+',
'+', 11, { 2, 3, 1, 2, 1, 2 },
false },
74 {
',',
',', 12, { 1, 1, 2, 2, 3, 2 },
false },
75 {
'-',
'-', 13, { 1, 2, 2, 1, 3, 2 },
false },
76 {
'.',
'.', 14, { 1, 2, 2, 2, 3, 1 },
false },
77 {
'/',
'/', 15, { 1, 1, 3, 2, 2, 2 },
false },
78 {
'0',
'0', 16, { 1, 2, 3, 1, 2, 2 },
false },
79 {
'1',
'1', 17, { 1, 2, 3, 2, 2, 1 },
false },
80 {
'2',
'2', 18, { 2, 2, 3, 2, 1, 1 },
false },
81 {
'3',
'3', 19, { 2, 2, 1, 1, 3, 2 },
false },
82 {
'4',
'4', 20, { 2, 2, 1, 2, 3, 1 },
false },
83 {
'5',
'5', 21, { 2, 1, 3, 2, 1, 2 },
false },
84 {
'6',
'6', 22, { 2, 2, 3, 1, 1, 2 },
false },
85 {
'7',
'7', 23, { 3, 1, 2, 1, 3, 1 },
false },
86 {
'8',
'8', 24, { 3, 1, 1, 2, 2, 2 },
false },
87 {
'9',
'9', 25, { 3, 2, 1, 1, 2, 2 },
false },
88 {
':',
':', 26, { 3, 2, 1, 2, 2, 1 },
false },
89 {
';',
';', 27, { 3, 1, 2, 2, 1, 2 },
false },
90 {
'<',
'<', 28, { 3, 2, 2, 1, 1, 2 },
false },
91 {
'=',
'=', 29, { 3, 2, 2, 2, 1, 1 },
false },
92 {
'>',
'>', 30, { 2, 1, 2, 1, 2, 3 },
false },
93 {
'?',
'?', 31, { 2, 1, 2, 3, 2, 1 },
false },
94 {
'@',
'@', 32, { 2, 3, 2, 1, 2, 1 },
false },
95 {
'A',
'A', 33, { 1, 1, 1, 3, 2, 3 },
false },
96 {
'B',
'B', 34, { 1, 3, 1, 1, 2, 3 },
false },
97 {
'C',
'C', 35, { 1, 3, 1, 3, 2, 1 },
false },
98 {
'D',
'D', 36, { 1, 1, 2, 3, 1, 3 },
false },
99 {
'E',
'E', 37, { 1, 3, 2, 1, 1, 3 },
false },
100 {
'F',
'F', 38, { 1, 3, 2, 3, 1, 1 },
false },
101 {
'G',
'G', 39, { 2, 1, 1, 3, 1, 3 },
false },
102 {
'H',
'H', 40, { 2, 3, 1, 1, 1, 3 },
false },
103 {
'I',
'I', 41, { 2, 3, 1, 3, 1, 1 },
false },
104 {
'J',
'J', 42, { 1, 1, 2, 1, 3, 3 },
false },
105 {
'K',
'K', 43, { 1, 1, 2, 3, 3, 1 },
false },
106 {
'L',
'L', 44, { 1, 3, 2, 1, 3, 1 },
false },
107 {
'M',
'M', 45, { 1, 1, 3, 1, 2, 3 },
false },
108 {
'N',
'N', 46, { 1, 1, 3, 3, 2, 1 },
false },
109 {
'O',
'O', 47, { 1, 3, 3, 1, 2, 1 },
false },
110 {
'P',
'P', 48, { 3, 1, 3, 1, 2, 1 },
false },
111 {
'Q',
'Q', 49, { 2, 1, 1, 3, 3, 1 },
false },
112 {
'R',
'R', 50, { 2, 3, 1, 1, 3, 1 },
false },
113 {
'S',
'S', 51, { 2, 1, 3, 1, 1, 3 },
false },
114 {
'T',
'T', 52, { 2, 1, 3, 3, 1, 1 },
false },
115 {
'U',
'U', 53, { 2, 1, 3, 1, 3, 1 },
false },
116 {
'V',
'V', 54, { 3, 1, 1, 1, 2, 3 },
false },
117 {
'W',
'W', 55, { 3, 1, 1, 3, 2, 1 },
false },
118 {
'X',
'X', 56, { 3, 3, 1, 1, 2, 1 },
false },
119 {
'Y',
'Y', 57, { 3, 1, 2, 1, 1, 3 },
false },
120 {
'Z',
'Z', 58, { 3, 1, 2, 3, 1, 1 },
false },
121 {
'[',
'[', 59, { 3, 3, 2, 1, 1, 1 },
false },
122 {
'\\',
'\\', 60, { 3, 1, 4, 1, 1, 1 },
false },
123 {
']',
']', 61, { 2, 2, 1, 4, 1, 1 },
false },
124 {
'^',
'^', 62, { 4, 3, 1, 1, 1, 1 },
false },
125 {
'_',
'_', 63, { 1, 1, 1, 2, 2, 4 },
false },
126 { 0x00,
'`', 64, { 1, 1, 1, 4, 2, 2 },
false },
127 { 0x01,
'a', 65, { 1, 2, 1, 1, 2, 4 },
false },
128 { 0x02,
'b', 66, { 1, 2, 1, 4, 2, 1 },
false },
129 { 0x03,
'c', 67, { 1, 4, 1, 1, 2, 2 },
false },
130 { 0x04,
'd', 68, { 1, 4, 1, 2, 2, 1 },
false },
131 { 0x05,
'e', 69, { 1, 1, 2, 2, 1, 4 },
false },
132 { 0x06,
'f', 70, { 1, 1, 2, 4, 1, 2 },
false },
133 { 0x07,
'g', 71, { 1, 2, 2, 1, 1, 4 },
false },
134 { 0x08,
'h', 72, { 1, 2, 2, 4, 1, 1 },
false },
135 { 0x09,
'i', 73, { 1, 4, 2, 1, 1, 2 },
false },
136 { 0x0A,
'j', 74, { 1, 4, 2, 2, 1, 1 },
false },
137 { 0x0B,
'k', 75, { 2, 4, 1, 2, 1, 1 },
false },
138 { 0x0C,
'l', 76, { 2, 2, 1, 1, 1, 4 },
false },
139 { 0x0D,
'm', 77, { 4, 1, 3, 1, 1, 1 },
false },
140 { 0x0E,
'n', 78, { 2, 4, 1, 1, 1, 2 },
false },
141 { 0x0F,
'o', 79, { 1, 3, 4, 1, 1, 1 },
false },
142 { 0x10,
'p', 80, { 1, 1, 1, 2, 4, 2 },
false },
143 { 0x11,
'q', 81, { 1, 2, 1, 1, 4, 2 },
false },
144 { 0x12,
'r', 82, { 1, 2, 1, 2, 4, 1 },
false },
145 { 0x13,
's', 83, { 1, 1, 4, 2, 1, 2 },
false },
146 { 0x14,
't', 84, { 1, 2, 4, 1, 1, 2 },
false },
147 { 0x15,
'u', 85, { 1, 2, 4, 2, 1, 1 },
false },
148 { 0x16,
'v', 86, { 4, 1, 1, 2, 1, 2 },
false },
149 { 0x17,
'w', 87, { 4, 2, 1, 1, 1, 2 },
false },
150 { 0x18,
'x', 88, { 4, 2, 1, 2, 1, 1 },
false },
151 { 0x19,
'y', 89, { 2, 1, 2, 1, 4, 1 },
false },
152 { 0x1A,
'z', 90, { 2, 1, 4, 1, 2, 1 },
false },
153 { 0x1B,
'{', 91, { 4, 1, 2, 1, 2, 1 },
false },
154 { 0x1C,
'|', 92, { 1, 1, 1, 1, 4, 3 },
false },
155 { 0x1D,
'}', 93, { 1, 1, 1, 3, 4, 1 },
false },
156 { 0x1E,
'~', 94, { 1, 3, 1, 1, 4, 1 },
false },
157 { 0x1F, 0x7F, 95, { 1, 1, 4, 1, 1, 3 },
false },
158 { FNC3, FNC3, 96, { 1, 1, 4, 3, 1, 1 },
false },
159 { FNC2, FNC2, 97, { 4, 1, 1, 1, 1, 3 },
false },
160 {
SHIFT,
SHIFT, 98, { 4, 1, 1, 3, 1, 1 },
false },
161 { CODEC, CODEC, 99, { 1, 1, 3, 1, 4, 1 },
false },
162 { CODEB, FNC4, CODEB, { 1, 1, 4, 1, 3, 1 },
false },
163 { FNC4, CODEA, CODEA, { 3, 1, 1, 1, 4, 1 },
false },
164 { FNC1, FNC1, FNC1, { 4, 1, 1, 1, 3, 1 },
false },
165 { STARTA, STARTA, STARTA, { 2, 1, 1, 4, 1, 2 },
false },
166 { STARTB, STARTB, STARTB, { 2, 1, 1, 2, 1, 4 },
false },
167 { STARTC, STARTC, STARTC, { 2, 1, 1, 2, 3, 2 },
false },
169 {
'\0',
'\0',
'\0', { 0, 0, 0, 0, 0, 0 },
true }
174int code128Index(
QChar code,
int set)
176 const char latin1Code = code.
toLatin1();
177 for (
int idx = 0; _128codes[idx]._null ==
false; ++idx) {
178 if (set == SETA && _128codes[idx].codea == latin1Code)
return idx;
179 if (set == SETB && _128codes[idx].codeb == latin1Code)
return idx;
180 if (set == SETC && _128codes[idx].codec == latin1Code)
return idx;
200 for (
int i = 0; i < _str.
length(); ++i) {
202 rank_a += (code128Index(c, SETA) != -1 ? 1 : 0);
203 rank_b += (code128Index(c, SETB) != -1 ? 1 : 0);
206 if (rank_c == _str.
length() && ((rank_c % 2) == 0 || rank_c > 4)) {
210 if ((rank_c % 2) == 1) {
221 for (; i < _str.
length(); i += 2) {
234 int set = (rank_a > rank_b ? SETA : SETB);
235 str.
push_back((rank_a > rank_b ? 103 : 104));
236 for (
int i = 0; i < _str.
length(); ++i) {
238 int v = code128Index(c, set);
240 v = code128Index(c, (set == SETA ? SETB : SETA));
252 int checksum = str.
at(0);
253 for (
int i = 1; i < str.
size(); ++i)
254 checksum += (str.
at(i) * i);
255 checksum = checksum % 103;
262 qreal quiet_zone = bar_width * 10;
263 if (quiet_zone < 0.1)
267 qreal draw_width = r.
width();
268 qreal draw_height = r.
height();
271 int val_length = str.
size() - 2;
280 qreal C = val_length;
283 L = (((11.0 * C) + 35.0) * X);
295 qreal nqz = (draw_width - L) / 2.0;
296 if (nqz > quiet_zone)
299 quiet_zone = draw_width - (L + quiet_zone);
303 qreal pos = r.
left() + quiet_zone;
310 for (
int i = 0; i < str.
size(); ++i) {
312 const int idx = str.
at(i);
313 if (idx < 0 || idx > 105) {
314 kreportpluginWarning() <<
"Encountered a non-compliant element while rendering a 3of9 barcode -- skipping";
318 for (
int b = 0; b < 6; ++b, space = !space) {
319 qreal w = _128codes[idx].values[b] * bar_width;
323 rect->setBrush(brush);
324 rect->setRect(
QRectF(pos, top, w, draw_height));
325 page->insertPrimitive(rect);
333 int STOP_CHARACTER[] = { 2, 3, 3, 1, 1, 1, 2 };
335 for (
int b = 0; b < 7; ++b, space = !space) {
336 qreal w = STOP_CHARACTER[b] * bar_width;
340 rect->setBrush(brush);
341 rect->setRect(
QRectF(pos, top, w, draw_height));
342 page->insertPrimitive(rect);
Represents a single page in a document and may contain zero or more OROPrimitive objects all of which...
char toLatin1() const const
const_reference at(qsizetype i) const const
void push_back(parameter_type value)
qsizetype size() const const
qreal height() const const
qreal width() const const
const QChar at(qsizetype position) const const
bool isEmpty() const const
qsizetype length() const const