8
9
10
11
12
13
14
15
16
17
18
19
20
21
23#include "fxbarcode/pdf417/BC_PDF417HighLevelEncoder.h"
27#include "core/fxcrt/fx_extension.h"
28#include "core/fxcrt/fx_string.h"
29#include "third_party/bigint/BigIntegerLibrary.hh"
33constexpr int16_t kLatchToText = 900;
34constexpr int16_t kLatchToBytePadded = 901;
35constexpr int16_t kLatchToNumeric = 902;
36constexpr int16_t kShiftToByte = 913;
37constexpr int16_t kLatchToByte = 924;
39constexpr std::array<
const int8_t, 128> kMixed = {
40 {-1, -1, -1, -1, -1, -1, -1, -1, -1, 12, -1, -1, -1, 11, -1, -1, -1, -1, -1,
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, -1, -1, 15, 18, 21,
42 10, -1, -1, -1, 22, 20, 13, 16, 17, 19, 0, 1, 2, 3, 4, 5, 6, 7, 8,
43 9, 14, -1, -1, 23, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 24,
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}};
48constexpr std::array<
const int8_t, 128> kPunctuation = {
49 {-1, -1, -1, -1, -1, -1, -1, -1, -1, 12, 15, -1, -1, 11, -1, -1, -1, -1, -1,
50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10, 20, -1, 18, -1,
51 -1, 28, 23, 24, 22, -1, 13, 16, 17, 19, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52 -1, 14, 0, 1, -1, 2, 25, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 4, 5, 6, -1,
54 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 21, 27, 9, -1}};
57bool IsAlphaUpperOrSpace(
wchar_t ch) {
61bool IsAlphaLowerOrSpace(
wchar_t ch) {
65bool IsMixed(
wchar_t ch) {
67 return !((ch & ~0x7F) || kMixed[ch] == -1);
70bool IsPunctuation(
wchar_t ch) {
72 return !((ch & ~0x7F) || kPunctuation[ch] == -1);
75bool IsText(
wchar_t ch) {
76 return (ch >= 32 && ch <= 126) || ch ==
'\t' || ch ==
'\n' || ch ==
'\r';
85 size_t len = bytes.GetLength();
88 for (size_t i = 0; i < len; i++) {
89 wchar_t ch = bytes[i] & 0xff;
90 if (ch ==
'?' && bytes[i] !=
'?')
95 len = result.GetLength();
99 SubMode textSubMode = SubMode::kAlpha;
100 EncodingMode encodingMode = EncodingMode::kUnknown;
102 size_t n = DetermineConsecutiveDigitCount(result, p);
104 sb
+= kLatchToNumeric;
105 encodingMode = EncodingMode::kNumeric;
106 textSubMode = SubMode::kAlpha;
107 EncodeNumeric(result, p, n, &sb);
110 size_t t = DetermineConsecutiveTextCount(result, p);
111 if (t >= 5 || n == len) {
112 if (encodingMode != EncodingMode::kText) {
114 encodingMode = EncodingMode::kText;
115 textSubMode = SubMode::kAlpha;
117 textSubMode = EncodeText(result, p, t, textSubMode, &sb);
120 std::optional<size_t> b =
121 DetermineConsecutiveBinaryCount(result, bytes.unsigned_span(), p);
125 size_t b_value = b.value();
128 if (b_value == 1 && encodingMode == EncodingMode::kText) {
129 EncodeBinary(bytes.unsigned_span(), p, 1, EncodingMode::kText, &sb);
131 EncodeBinary(bytes.unsigned_span(), p, b_value, encodingMode, &sb);
132 encodingMode = EncodingMode::kByte;
133 textSubMode = SubMode::kAlpha;
146 SubMode initialSubmode,
150 SubMode submode = initialSubmode;
152 while (idx < count) {
153 wchar_t ch = msg[startpos + idx];
155 case SubMode::kAlpha:
156 if (IsAlphaUpperOrSpace(ch)) {
163 if (IsAlphaLowerOrSpace(ch)) {
164 submode = SubMode::kLower;
169 submode = SubMode::kMixed;
173 if (IsPunctuation(ch)) {
175 tmp += kPunctuation[ch];
178 case SubMode::kLower:
179 if (IsAlphaLowerOrSpace(ch)) {
186 if (IsAlphaUpperOrSpace(ch)) {
192 submode = SubMode::kMixed;
196 if (IsPunctuation(ch)) {
198 tmp += kPunctuation[ch];
201 case SubMode::kMixed:
206 if (IsAlphaUpperOrSpace(ch)) {
207 submode = SubMode::kAlpha;
211 if (IsAlphaLowerOrSpace(ch)) {
212 submode = SubMode::kLower;
216 if (startpos + idx + 1 < count) {
217 wchar_t next = msg[startpos + idx + 1];
218 if (IsPunctuation(next)) {
219 submode = SubMode::kPunctuation;
224 if (IsPunctuation(ch)) {
226 tmp += kPunctuation[ch];
230 if (IsPunctuation(ch)) {
231 tmp += kPunctuation[ch];
234 submode = SubMode::kAlpha;
241 size_t len = tmp.GetLength();
242 for (size_t i = 0; i < len; i++) {
243 bool odd = (i % 2) != 0;
245 h = (h * 30) + tmp[i];
252 *sb
+= (h * 30) + 29;
259 EncodingMode startmode,
261 if (count == 1 && startmode == EncodingMode::kText)
264 size_t idx = startpos;
267 std::array<
wchar_t, 5> chars;
268 while ((startpos + count - idx) >= 6) {
270 for (size_t i = 0; i < 6; i++) {
272 t += bytes[idx + i] & 0xff;
274 for (size_t i = 0; i < 5; i++) {
275 chars[i] = (t % 900);
278 for (size_t i = 5; i >= 1; i--)
279 *sb += (chars[i - 1]);
283 if (idx < startpos + count)
284 *sb
+= kLatchToBytePadded;
285 for (size_t i = idx; i < startpos + count; i++) {
286 int32_t ch = bytes[i] & 0xff;
296 BigInteger num900 = 900;
297 while (idx < count) {
299 size_t len = 44 < count - idx ? 44 : count - idx;
300 ByteString part = (L'1' + msg.Substr(startpos + idx, len)).ToUTF8();
301 BigInteger bigint = stringToBigInteger(part.c_str());
303 int32_t c = (bigint % num900).toInt();
305 bigint = bigint / num900;
306 }
while (!bigint.isZero());
307 for (size_t i = tmp.GetLength(); i >= 1; i--)
317 size_t len = msg.GetLength();
318 size_t idx = startpos;
320 wchar_t ch = msg[idx];
334 size_t len = msg.GetLength();
335 size_t idx = startpos;
337 wchar_t ch = msg[idx];
338 size_t numericCount = 0;
345 if (numericCount >= 13)
346 return idx - startpos - numericCount;
347 if (numericCount > 0)
354 return idx - startpos;
360 pdfium::span<
const uint8_t> bytes,
362 size_t len = msg.GetLength();
363 size_t idx = startpos;
365 wchar_t ch = msg[idx];
366 size_t numericCount = 0;
369 size_t i = idx + numericCount;
374 if (numericCount >= 13)
375 return idx - startpos;
377 size_t textCount = 0;
378 while (textCount < 5 && IsText(ch)) {
380 size_t i = idx + textCount;
386 return idx - startpos;
388 if (bytes[idx] == 63 && ch !=
'?')
392 return idx - startpos;
fxcrt::ByteString ByteString
static std::optional< WideString > EncodeHighLevel(WideStringView msg)
WideString & operator+=(wchar_t ch)
bool FXSYS_IsDecimalDigit(wchar_t c)
bool FXSYS_IsUpperASCII(int32_t c)
bool FXSYS_IsLowerASCII(int32_t c)
fxcrt::WideStringView WideStringView
fxcrt::WideString WideString