1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
// Original code is licensed as follows:
/*
* Copyright 2006-2007 Jeremias Maerki.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h"
#include <limits>
#include <memory>
#include <vector>
#include "fxbarcode/BC_UtilCodingConvert.h"
#include "fxbarcode/common/BC_CommonBitMatrix.h"
#include "fxbarcode/datamatrix/BC_ASCIIEncoder.h"
#include "fxbarcode/datamatrix/BC_Base256Encoder.h"
#include "fxbarcode/datamatrix/BC_C40Encoder.h"
#include "fxbarcode/datamatrix/BC_EdifactEncoder.h"
#include "fxbarcode/datamatrix/BC_Encoder.h"
#include "fxbarcode/datamatrix/BC_EncoderContext.h"
#include "fxbarcode/datamatrix/BC_SymbolInfo.h"
#include "fxbarcode/datamatrix/BC_TextEncoder.h"
#include "fxbarcode/datamatrix/BC_X12Encoder.h"
#include "fxbarcode/utils.h"
#include "third_party/base/ptr_util.h"
const wchar_t CBC_HighLevelEncoder::LATCH_TO_C40 = 230;
const wchar_t CBC_HighLevelEncoder::LATCH_TO_BASE256 = 231;
const wchar_t CBC_HighLevelEncoder::UPPER_SHIFT = 235;
const wchar_t CBC_HighLevelEncoder::LATCH_TO_ANSIX12 = 238;
const wchar_t CBC_HighLevelEncoder::LATCH_TO_TEXT = 239;
const wchar_t CBC_HighLevelEncoder::LATCH_TO_EDIFACT = 240;
const wchar_t CBC_HighLevelEncoder::C40_UNLATCH = 254;
const wchar_t CBC_HighLevelEncoder::X12_UNLATCH = 254;
const wchar_t CBC_HighLevelEncoder::PAD = 129;
const wchar_t CBC_HighLevelEncoder::MACRO_05 = 236;
const wchar_t CBC_HighLevelEncoder::MACRO_06 = 237;
const wchar_t CBC_HighLevelEncoder::MACRO_05_HEADER[] = L"[)>05";
const wchar_t CBC_HighLevelEncoder::MACRO_06_HEADER[] = L"[)>06";
const wchar_t CBC_HighLevelEncoder::MACRO_TRAILER = 0x0004;
CBC_HighLevelEncoder::CBC_HighLevelEncoder() {}
CBC_HighLevelEncoder::~CBC_HighLevelEncoder() {}
std::vector<uint8_t>& CBC_HighLevelEncoder::getBytesForMessage(
CFX_WideString msg) {
CFX_ByteString bytestr;
CBC_UtilCodingConvert::UnicodeToUTF8(msg, bytestr);
m_bytearray.insert(m_bytearray.end(), bytestr.begin(), bytestr.end());
return m_bytearray;
}
// static
CFX_WideString CBC_HighLevelEncoder::encodeHighLevel(CFX_WideString msg,
CFX_WideString ecLevel,
bool allowRectangular,
int32_t& e) {
CBC_EncoderContext context(msg, ecLevel, e);
if (e != BCExceptionNO)
return CFX_WideString();
context.setAllowRectangular(allowRectangular);
if ((msg.Left(6) == MACRO_05_HEADER) && (msg.Last() == MACRO_TRAILER)) {
context.writeCodeword(MACRO_05);
context.setSkipAtEnd(2);
context.m_pos += 6;
} else if ((msg.Left(6) == MACRO_06_HEADER) &&
(msg.Last() == MACRO_TRAILER)) {
context.writeCodeword(MACRO_06);
context.setSkipAtEnd(2);
context.m_pos += 6;
}
std::vector<std::unique_ptr<CBC_Encoder>> encoders;
encoders.push_back(pdfium::MakeUnique<CBC_ASCIIEncoder>());
encoders.push_back(pdfium::MakeUnique<CBC_C40Encoder>());
encoders.push_back(pdfium::MakeUnique<CBC_TextEncoder>());
encoders.push_back(pdfium::MakeUnique<CBC_X12Encoder>());
encoders.push_back(pdfium::MakeUnique<CBC_EdifactEncoder>());
encoders.push_back(pdfium::MakeUnique<CBC_Base256Encoder>());
int32_t encodingMode = ASCII_ENCODATION;
while (context.hasMoreCharacters()) {
encoders[encodingMode]->Encode(context, e);
if (e != BCExceptionNO)
return L"";
if (context.m_newEncoding >= 0) {
encodingMode = context.m_newEncoding;
context.resetEncoderSignal();
}
}
int32_t len = context.m_codewords.GetLength();
context.updateSymbolInfo(e);
if (e != BCExceptionNO)
return L"";
int32_t capacity = context.m_symbolInfo->dataCapacity();
if (len < capacity) {
if (encodingMode != ASCII_ENCODATION &&
encodingMode != BASE256_ENCODATION) {
context.writeCodeword(0x00fe);
}
}
CFX_WideString codewords = context.m_codewords;
if (pdfium::base::checked_cast<int32_t>(codewords.GetLength()) < capacity) {
codewords += PAD;
}
while (pdfium::base::checked_cast<int32_t>(codewords.GetLength()) <
capacity) {
codewords += (randomize253State(
PAD, pdfium::base::checked_cast<int32_t>(codewords.GetLength()) + 1));
}
return codewords;
}
int32_t CBC_HighLevelEncoder::lookAheadTest(CFX_WideString msg,
int32_t startpos,
int32_t currentMode) {
if (startpos >= pdfium::base::checked_cast<int32_t>(msg.GetLength())) {
return currentMode;
}
std::vector<float> charCounts;
if (currentMode == ASCII_ENCODATION) {
charCounts.push_back(0);
charCounts.push_back(1);
charCounts.push_back(1);
charCounts.push_back(1);
charCounts.push_back(1);
charCounts.push_back(1.25f);
} else {
charCounts.push_back(1);
charCounts.push_back(2);
charCounts.push_back(2);
charCounts.push_back(2);
charCounts.push_back(2);
charCounts.push_back(2.25f);
charCounts[currentMode] = 0;
}
int32_t charsProcessed = 0;
while (true) {
if ((startpos + charsProcessed) ==
pdfium::base::checked_cast<int32_t>(msg.GetLength())) {
int32_t min = std::numeric_limits<int32_t>::max();
std::vector<uint8_t> mins(6);
std::vector<int32_t> intCharCounts(6);
min = findMinimums(charCounts, intCharCounts, min, mins);
int32_t minCount = getMinimumCount(mins);
if (intCharCounts[ASCII_ENCODATION] == min) {
return ASCII_ENCODATION;
}
if (minCount == 1 && mins[BASE256_ENCODATION] > 0) {
return BASE256_ENCODATION;
}
if (minCount == 1 && mins[EDIFACT_ENCODATION] > 0) {
return EDIFACT_ENCODATION;
}
if (minCount == 1 && mins[TEXT_ENCODATION] > 0) {
return TEXT_ENCODATION;
}
if (minCount == 1 && mins[X12_ENCODATION] > 0) {
return X12_ENCODATION;
}
return C40_ENCODATION;
}
wchar_t c = msg[startpos + charsProcessed];
charsProcessed++;
if (isDigit(c)) {
charCounts[ASCII_ENCODATION] += 0.5;
} else if (isExtendedASCII(c)) {
charCounts[ASCII_ENCODATION] = (float)ceil(charCounts[ASCII_ENCODATION]);
charCounts[ASCII_ENCODATION] += 2;
} else {
charCounts[ASCII_ENCODATION] = (float)ceil(charCounts[ASCII_ENCODATION]);
charCounts[ASCII_ENCODATION]++;
}
if (isNativeC40(c)) {
charCounts[C40_ENCODATION] += 2.0f / 3.0f;
} else if (isExtendedASCII(c)) {
charCounts[C40_ENCODATION] += 8.0f / 3.0f;
} else {
charCounts[C40_ENCODATION] += 4.0f / 3.0f;
}
if (isNativeText(c)) {
charCounts[TEXT_ENCODATION] += 2.0f / 3.0f;
} else if (isExtendedASCII(c)) {
charCounts[TEXT_ENCODATION] += 8.0f / 3.0f;
} else {
charCounts[TEXT_ENCODATION] += 4.0f / 3.0f;
}
if (isNativeX12(c)) {
charCounts[X12_ENCODATION] += 2.0f / 3.0f;
} else if (isExtendedASCII(c)) {
charCounts[X12_ENCODATION] += 13.0f / 3.0f;
} else {
charCounts[X12_ENCODATION] += 10.0f / 3.0f;
}
if (isNativeEDIFACT(c)) {
charCounts[EDIFACT_ENCODATION] += 3.0f / 4.0f;
} else if (isExtendedASCII(c)) {
charCounts[EDIFACT_ENCODATION] += 17.0f / 4.0f;
} else {
charCounts[EDIFACT_ENCODATION] += 13.0f / 4.0f;
}
charCounts[BASE256_ENCODATION]++;
if (charsProcessed >= 4) {
std::vector<int32_t> intCharCounts(6);
std::vector<uint8_t> mins(6);
findMinimums(charCounts, intCharCounts,
std::numeric_limits<int32_t>::max(), mins);
int32_t minCount = getMinimumCount(mins);
if (intCharCounts[ASCII_ENCODATION] < intCharCounts[BASE256_ENCODATION] &&
intCharCounts[ASCII_ENCODATION] < intCharCounts[C40_ENCODATION] &&
intCharCounts[ASCII_ENCODATION] < intCharCounts[TEXT_ENCODATION] &&
intCharCounts[ASCII_ENCODATION] < intCharCounts[X12_ENCODATION] &&
intCharCounts[ASCII_ENCODATION] < intCharCounts[EDIFACT_ENCODATION]) {
return ASCII_ENCODATION;
}
if (intCharCounts[BASE256_ENCODATION] < intCharCounts[ASCII_ENCODATION] ||
(mins[C40_ENCODATION] + mins[TEXT_ENCODATION] + mins[X12_ENCODATION] +
mins[EDIFACT_ENCODATION]) == 0) {
return BASE256_ENCODATION;
}
if (minCount == 1 && mins[EDIFACT_ENCODATION] > 0) {
return EDIFACT_ENCODATION;
}
if (minCount == 1 && mins[TEXT_ENCODATION] > 0) {
return TEXT_ENCODATION;
}
if (minCount == 1 && mins[X12_ENCODATION] > 0) {
return X12_ENCODATION;
}
if (intCharCounts[C40_ENCODATION] + 1 < intCharCounts[ASCII_ENCODATION] &&
intCharCounts[C40_ENCODATION] + 1 <
intCharCounts[BASE256_ENCODATION] &&
intCharCounts[C40_ENCODATION] + 1 <
intCharCounts[EDIFACT_ENCODATION] &&
intCharCounts[C40_ENCODATION] + 1 < intCharCounts[TEXT_ENCODATION]) {
if (intCharCounts[C40_ENCODATION] < intCharCounts[X12_ENCODATION]) {
return C40_ENCODATION;
}
if (intCharCounts[C40_ENCODATION] == intCharCounts[X12_ENCODATION]) {
int32_t p = startpos + charsProcessed + 1;
int32_t checked_length =
pdfium::base::checked_cast<int32_t>(msg.GetLength());
while (p < checked_length) {
wchar_t tc = msg[p];
if (isX12TermSep(tc)) {
return X12_ENCODATION;
}
if (!isNativeX12(tc)) {
break;
}
p++;
}
return C40_ENCODATION;
}
}
}
}
}
bool CBC_HighLevelEncoder::isDigit(wchar_t ch) {
return ch >= '0' && ch <= '9';
}
bool CBC_HighLevelEncoder::isExtendedASCII(wchar_t ch) {
return ch >= 128 && ch <= 255;
}
int32_t CBC_HighLevelEncoder::determineConsecutiveDigitCount(CFX_WideString msg,
int32_t startpos) {
int32_t count = 0;
int32_t len = msg.GetLength();
int32_t idx = startpos;
if (idx < len) {
wchar_t ch = msg[idx];
while (isDigit(ch) && idx < len) {
count++;
idx++;
if (idx < len) {
ch = msg[idx];
}
}
}
return count;
}
wchar_t CBC_HighLevelEncoder::randomize253State(wchar_t ch,
int32_t codewordPosition) {
int32_t pseudoRandom = ((149 * codewordPosition) % 253) + 1;
int32_t tempVariable = ch + pseudoRandom;
return tempVariable <= 254 ? (wchar_t)tempVariable
: (wchar_t)(tempVariable - 254);
}
int32_t CBC_HighLevelEncoder::findMinimums(std::vector<float>& charCounts,
std::vector<int32_t>& intCharCounts,
int32_t min,
std::vector<uint8_t>& mins) {
for (size_t l = 0; l < mins.size(); l++)
mins[l] = 0;
for (size_t i = 0; i < 6; i++) {
intCharCounts[i] = static_cast<int32_t>(ceil(charCounts[i]));
int32_t current = intCharCounts[i];
if (min > current) {
min = current;
for (size_t j = 0; j < mins.size(); j++)
mins[j] = 0;
}
if (min == current)
mins[i]++;
}
return min;
}
int32_t CBC_HighLevelEncoder::getMinimumCount(std::vector<uint8_t>& mins) {
int32_t minCount = 0;
for (int32_t i = 0; i < 6; i++) {
minCount += mins[i];
}
return minCount;
}
bool CBC_HighLevelEncoder::isNativeC40(wchar_t ch) {
return (ch == ' ') || (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z');
}
bool CBC_HighLevelEncoder::isNativeText(wchar_t ch) {
return (ch == ' ') || (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z');
}
bool CBC_HighLevelEncoder::isNativeX12(wchar_t ch) {
return isX12TermSep(ch) || (ch == ' ') || (ch >= '0' && ch <= '9') ||
(ch >= 'A' && ch <= 'Z');
}
bool CBC_HighLevelEncoder::isX12TermSep(wchar_t ch) {
return (ch == '\r') || (ch == '*') || (ch == '>');
}
bool CBC_HighLevelEncoder::isNativeEDIFACT(wchar_t ch) {
return ch >= ' ' && ch <= '^';
}
|