8
9
10
11
12
13
14
15
16
17
18
19
20
21
23#include "fxbarcode/oned/BC_OnedCode128Writer.h"
29#include "core/fxcrt/fx_memory_wrappers.h"
30#include "fxbarcode/BC_Writer.h"
31#include "fxbarcode/oned/BC_OneDimWriter.h"
32#include "third_party/base/check.h"
36constexpr size_t kPatternSize = 7;
38const uint8_t kCodePatterns[107][kPatternSize] = {
39 {2, 1, 2, 2, 2, 2, 0}, {2, 2, 2, 1, 2, 2, 0}, {2, 2, 2, 2, 2, 1, 0},
40 {1, 2, 1, 2, 2, 3, 0}, {1, 2, 1, 3, 2, 2, 0}, {1, 3, 1, 2, 2, 2, 0},
41 {1, 2, 2, 2, 1, 3, 0}, {1, 2, 2, 3, 1, 2, 0}, {1, 3, 2, 2, 1, 2, 0},
42 {2, 2, 1, 2, 1, 3, 0}, {2, 2, 1, 3, 1, 2, 0}, {2, 3, 1, 2, 1, 2, 0},
43 {1, 1, 2, 2, 3, 2, 0}, {1, 2, 2, 1, 3, 2, 0}, {1, 2, 2, 2, 3, 1, 0},
44 {1, 1, 3, 2, 2, 2, 0}, {1, 2, 3, 1, 2, 2, 0}, {1, 2, 3, 2, 2, 1, 0},
45 {2, 2, 3, 2, 1, 1, 0}, {2, 2, 1, 1, 3, 2, 0}, {2, 2, 1, 2, 3, 1, 0},
46 {2, 1, 3, 2, 1, 2, 0}, {2, 2, 3, 1, 1, 2, 0}, {3, 1, 2, 1, 3, 1, 0},
47 {3, 1, 1, 2, 2, 2, 0}, {3, 2, 1, 1, 2, 2, 0}, {3, 2, 1, 2, 2, 1, 0},
48 {3, 1, 2, 2, 1, 2, 0}, {3, 2, 2, 1, 1, 2, 0}, {3, 2, 2, 2, 1, 1, 0},
49 {2, 1, 2, 1, 2, 3, 0}, {2, 1, 2, 3, 2, 1, 0}, {2, 3, 2, 1, 2, 1, 0},
50 {1, 1, 1, 3, 2, 3, 0}, {1, 3, 1, 1, 2, 3, 0}, {1, 3, 1, 3, 2, 1, 0},
51 {1, 1, 2, 3,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2, 0}, {1, 2, 1, 2, 4, 1, 0}, {1, 1, 4, 2, 1, 2, 0},
67 {1, 2, 4, 1, 1, 2, 0}, {1, 2, 4, 2, 1, 1, 0}, {4, 1, 1, 2, 1, 2, 0},
68 {4, 2, 1, 1, 1, 2, 0}, {4, 2, 1, 2, 1, 1, 0}, {2, 1, 2, 1, 4, 1, 0},
69 {2, 1, 4, 1, 2, 1, 0}, {4, 1, 2, 1, 2, 1, 0}, {1, 1, 1, 1, 4, 3, 0},
70 {1, 1, 1, 3, 4, 1, 0}, {1, 3, 1, 1, 4, 1, 0}, {1, 1, 4, 1, 1, 3, 0},
71 {1, 1, 4, 3, 1, 1, 0}, {4, 1, 1, 1, 1, 3, 0}, {4, 1, 1, 3, 1, 1, 0},
72 {1, 1, 3, 1, 4, 1, 0}, {1, 1, 4, 1, 3, 1, 0}, {3, 1, 1, 1, 4, 1, 0},
73 {4, 1, 1, 1, 3, 1, 0}, {2, 1, 1, 4, 1, 2, 0}, {2, 1, 1, 2, 1, 4, 0},
74 {2, 1, 1, 2, 3, 2, 0}, {2, 3, 3, 1, 1, 1, 2}};
76const int32_t CODE_START_B = 104;
77const int32_t CODE_START_C = 105;
78const int32_t CODE_STOP = 106;
80bool IsInOnedCode128Alphabet(
wchar_t ch) {
81 int32_t index =
static_cast<int32_t>(ch);
82 return index >= 32 && index <= 126 && index != 34;
88 : m_codeFormat(type) {
89 DCHECK(m_codeFormat == BC_TYPE::kCode128B ||
90 m_codeFormat == BC_TYPE::kCode128C);
97 std::all_of(contents.begin(), contents.end(), IsInOnedCode128Alphabet);
104 filtered.Reserve(contents.GetLength());
105 for (size_t i = 0; i < contents.GetLength(); i++) {
106 wchar_t ch = contents[i];
111 if (ch >= 32 && ch <= limit)
122 if (contents.GetLength() < 1 || contents.GetLength() > 80)
123 return DataVector<uint8_t>();
125 std::vector<int32_t> patterns;
126 int32_t checkSum = 0;
128 checkSum = Encode128B(contents, &patterns);
130 checkSum = Encode128C(contents, &patterns);
133 patterns.push_back(checkSum);
134 patterns.push_back(CODE_STOP);
135 m_iContentLen = contents.GetLength() + 3;
136 int32_t codeWidth = 0;
137 for (
const auto& patternIndex : patterns) {
138 const uint8_t* pattern = kCodePatterns[patternIndex];
139 for (size_t i = 0; i < kPatternSize; ++i)
140 codeWidth += pattern[i];
142 DataVector<uint8_t> result(codeWidth);
143 auto result_span = pdfium::make_span(result);
144 for (
const int32_t pattern_index : patterns) {
145 const uint8_t* pattern = kCodePatterns[pattern_index];
146 result_span = AppendPattern(result_span, {pattern, kPatternSize},
true);
152int32_t CBC_OnedCode128Writer::Encode128B(
const ByteString& contents,
153 std::vector<int32_t>* patterns) {
154 int32_t checkWeight = 1;
155 patterns->push_back(CODE_START_B);
156 int32_t checkSum = CODE_START_B * checkWeight;
157 for (size_t position = 0; position < contents.GetLength(); position++) {
158 int32_t patternIndex = contents[position] -
' ';
159 patterns->push_back(patternIndex);
160 checkSum += patternIndex * checkWeight++;
166int32_t CBC_OnedCode128Writer::Encode128C(
const ByteString& contents,
167 std::vector<int32_t>* patterns) {
168 int32_t checkWeight = 1;
169 patterns->push_back(CODE_START_C);
170 int32_t checkSum = CODE_START_C * checkWeight;
172 while (position < contents.GetLength()) {
173 int32_t patternIndex;
174 char ch = contents[position];
176 patternIndex = FXSYS_atoi(
177 contents.Substr(position, contents.IsValidIndex(position + 1) ? 2 : 1)
180 if (position < contents.GetLength() && isdigit(contents[position]))
183 patternIndex =
static_cast<int32_t>(ch);
186 patterns->push_back(patternIndex);
187 checkSum += patternIndex * checkWeight++;
static bool HasValidContentSize(WideStringView contents)
WideString FilterContents(WideStringView contents) override
bool CheckContentValidity(WideStringView contents) override
~CBC_OnedCode128Writer() override
DataVector< uint8_t > Encode(const ByteString &contents) override
CBC_OnedCode128Writer(BC_TYPE type)
void SetTextLocation(BC_TEXT_LOC location) override
WideString & operator+=(wchar_t ch)