8
9
10
11
12
13
14
15
16
17
18
19
20
21
23#include "fxbarcode/oned/BC_OnedCode39Writer.h"
29#include "core/fxcrt/fx_extension.h"
30#include "core/fxcrt/fx_memory_wrappers.h"
31#include "fxbarcode/BC_Writer.h"
32#include "fxbarcode/common/BC_CommonBitMatrix.h"
33#include "fxbarcode/oned/BC_OneDimWriter.h"
37const char kOnedCode39Alphabet[] = {
38 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
39 'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
40 'U',
'V',
'W',
'X',
'Y',
'Z',
'-',
'.',
' ',
'*',
'$',
'/',
'+',
'%'};
41constexpr size_t kOnedCode39AlphabetLen = std::size(kOnedCode39Alphabet);
43const char kOnedCode39Checksum[] = {
44 '0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
45 'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
46 'U',
'V',
'W',
'X',
'Y',
'Z',
'-',
'.',
' ',
'$',
'/',
'+',
'%'};
47static_assert(
std::size(kOnedCode39Checksum) == 43,
"Wrong size");
49const int16_t kOnedCode39CharacterEncoding[] = {
50 0x0034, 0x0121, 0x0061, 0x0160, 0x0031, 0x0130, 0x0070, 0x0025, 0x0124,
51 0x0064, 0x0109, 0x0049, 0x0148, 0x0019, 0x0118, 0x0058, 0x000D, 0x010C,
52 0x004C, 0x001C, 0x0103, 0x0043, 0x0142, 0x0013, 0x0112, 0x0052, 0x0007,
53 0x0106, 0x0046, 0x0016, 0x0181, 0x00C1, 0x01C0, 0x0091, 0x0190, 0x00D0,
54 0x0085, 0x0184, 0x00C4, 0x0094, 0x00A8, 0x00A2, 0x008A, 0x002A};
55static_assert(
std::size(kOnedCode39CharacterEncoding) == 44,
"Wrong size");
57bool IsInOnedCode39Alphabet(
wchar_t ch) {
59 ch == L'.' || ch == L' ' || ch == L'*' || ch == L'$' || ch == L'/' ||
60 ch == L'+' || ch == L'%';
63char CalcCheckSum(
const ByteString& contents) {
64 if (contents.GetLength() > 80)
68 for (
const auto& c : contents) {
70 for (; j < kOnedCode39AlphabetLen; j++) {
71 if (kOnedCode39Alphabet[j] == c) {
77 if (j >= kOnedCode39AlphabetLen)
80 return kOnedCode39Checksum[checksum %
std::size(kOnedCode39Checksum)];
91 std::all_of(contents.begin(), contents.end(), IsInOnedCode39Alphabet);
95 WideString filtercontents;
96 filtercontents.Reserve(contents.GetLength());
97 for (size_t i = 0; i < contents.GetLength(); i++) {
98 wchar_t ch = contents[i];
99 if (ch == L'*' && (i == 0 || i == contents.GetLength() - 1)) {
107 if (IsInOnedCode39Alphabet(ch))
108 filtercontents
+= ch;
110 return filtercontents;
114 WideString renderContents;
115 for (size_t i = 0; i < contents.GetLength(); i++) {
116 wchar_t ch = contents[i];
117 if (ch == L'*' && (i == 0 || i == contents.GetLength() - 1)) {
125 renderContents
+= ch;
127 return renderContents;
135 if (ratio < 2 || ratio > 3)
138 m_iWideNarrRatio = ratio;
142void CBC_OnedCode39Writer::ToIntArray(int16_t value,
143 uint8_t array[kArraySize])
const {
144 for (size_t i = 0; i < kArraySize; i++) {
145 array[i] = (value & (1 << i)) == 0 ? 1 : m_iWideNarrRatio;
150 char checksum = CalcCheckSum(contents);
152 return DataVector<uint8_t>();
154 uint8_t widths[kArraySize] = {0};
155 constexpr int32_t kWideStrideNum = 3;
156 constexpr int32_t kNarrowStrideNum = kArraySize - kWideStrideNum;
157 ByteString encodedContents = contents;
159 encodedContents
+= checksum;
160 m_iContentLen = encodedContents.GetLength();
162 (kWideStrideNum * m_iWideNarrRatio + kNarrowStrideNum) * 2 + 1 +
164 for (size_t j = 0; j < m_iContentLen; j++) {
165 for (size_t i = 0; i < kOnedCode39AlphabetLen; i++) {
166 if (kOnedCode39Alphabet[i] != encodedContents[j])
169 ToIntArray(kOnedCode39CharacterEncoding[i], widths);
170 for (size_t k = 0; k < kArraySize; k++)
171 code_width += widths[k];
174 DataVector<uint8_t> result(code_width);
175 auto result_span = pdfium::make_span(result);
176 ToIntArray(kOnedCode39CharacterEncoding[39], widths);
179 static constexpr uint8_t kNarrowWhite[] = {1};
182 for (int32_t l = m_iContentLen - 1; l >= 0; l--) {
183 for (size_t i = 0; i < kOnedCode39AlphabetLen; i++) {
184 if (kOnedCode39Alphabet[i] != encodedContents
[l
])
187 ToIntArray(kOnedCode39CharacterEncoding[i], widths);
192 ToIntArray(kOnedCode39CharacterEncoding[39], widths);
195 for (size_t i = 0; i < code_width / 2; i++) {
196 result[i] ^= result[code_width - 1 - i];
197 result[code_width - 1 - i] ^= result[i];
198 result[i] ^= result[code_width - 1 - i];
204 WideString* result) {
205 *result
= WideString(contents);
208 ByteString str = checksumContent
.ToUTF8();
210 checksum = CalcCheckSum(str);
220 pdfium::span<
const uint8_t> code) {
221 WideString encodedCon;
static bool HasValidContentSize(WideStringView contents)
virtual bool RenderResult(WideStringView contents, pdfium::span< const uint8_t > code)
pdfium::span< uint8_t > AppendPattern(pdfium::span< uint8_t > target, pdfium::span< const uint8_t > pattern, bool startColor)
void SetTextLocation(BC_TEXT_LOC location) override
bool RenderResult(WideStringView contents, pdfium::span< const uint8_t > code) override
bool CheckContentValidity(WideStringView contents) override
~CBC_OnedCode39Writer() override
WideString RenderTextContents(WideStringView contents)
bool encodedContents(WideStringView contents, WideString *result)
DataVector< uint8_t > Encode(const ByteString &contents) override
bool SetWideNarrowRatio(int8_t ratio) override
WideString FilterContents(WideStringView contents) override
ByteString & operator+=(char ch)
CharType operator[](const size_t index) const
ByteString ToUTF8() const
WideString & operator=(WideString &&that) noexcept
WideString & operator+=(wchar_t ch)
bool FXSYS_IsDecimalDigit(wchar_t c)
char FXSYS_ToUpperASCII(char c)