8
9
10
11
12
13
14
15
16
17
18
19
20
21
23#include "fxbarcode/oned/BC_OnedCode39Writer.h"
30#include "core/fxcrt/compiler_specific.h"
31#include "core/fxcrt/fx_extension.h"
32#include "core/fxcrt/stl_util.h"
33#include "fxbarcode/BC_Writer.h"
34#include "fxbarcode/oned/BC_OneDimWriter.h"
38constexpr auto kOnedCode39Alphabet =
fxcrt::ToArray<
const char>(
39 {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
40 'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
41 'U',
'V',
'W',
'X',
'Y',
'Z',
'-',
'.',
' ',
'*',
'$',
'/',
'+',
'%'});
42constexpr size_t kOnedCode39AlphabetLen =
std::size(kOnedCode39Alphabet);
44constexpr auto kOnedCode39Checksum =
fxcrt::ToArray<
const char>(
45 {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
46 'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
47 'U',
'V',
'W',
'X',
'Y',
'Z',
'-',
'.',
' ',
'$',
'/',
'+',
'%'});
48static_assert(
std::size(kOnedCode39Checksum) == 43,
"Wrong size");
50constexpr auto kOnedCode39CharacterEncoding =
fxcrt::ToArray<
const uint16_t>(
51 {0x0034, 0x0121, 0x0061, 0x0160, 0x0031, 0x0130, 0x0070, 0x0025, 0x0124,
52 0x0064, 0x0109, 0x0049, 0x0148, 0x0019, 0x0118, 0x0058, 0x000D, 0x010C,
53 0x004C, 0x001C, 0x0103, 0x0043, 0x0142, 0x0013, 0x0112, 0x0052, 0x0007,
54 0x0106, 0x0046, 0x0016, 0x0181, 0x00C1, 0x01C0, 0x0091, 0x0190, 0x00D0,
55 0x0085, 0x0184, 0x00C4, 0x0094, 0x00A8, 0x00A2, 0x008A, 0x002A});
56static_assert(
std::size(kOnedCode39CharacterEncoding) == 44,
"Wrong size");
58bool IsInOnedCode39Alphabet(
wchar_t ch) {
60 ch == L'.' || ch == L' ' || ch == L'*' || ch == L'$' || ch == L'/' ||
61 ch == L'+' || ch == L'%';
65 if (contents.GetLength() > 80)
69 for (
const auto& c : contents) {
71 for (; j < kOnedCode39AlphabetLen; j++) {
72 if (kOnedCode39Alphabet[j] == c) {
78 if (j >= kOnedCode39AlphabetLen)
81 return kOnedCode39Checksum[checksum %
std::size(kOnedCode39Checksum)];
84void ToIntArray(int16_t value, uint8_t ratio, pdfium::span<uint8_t> span) {
85 for (size_t i = 0; i < span.size(); i++) {
86 span[i] = (value & (1 << i)) == 0 ? 1 : ratio;
98 std::all_of(contents.begin(), contents.end(), IsInOnedCode39Alphabet);
103 filtercontents.Reserve(contents.GetLength());
104 for (size_t i = 0; i < contents.GetLength(); i++) {
105 wchar_t ch = contents[i];
106 if (ch == L'*' && (i == 0 || i == contents.GetLength() - 1)) {
114 if (IsInOnedCode39Alphabet(ch))
115 filtercontents
+= ch;
117 return filtercontents;
122 for (size_t i = 0; i < contents.GetLength(); i++) {
123 wchar_t ch = contents[i];
124 if (ch == L'*' && (i == 0 || i == contents.GetLength() - 1)) {
132 renderContents
+= ch;
134 return renderContents;
142 if (ratio < 2 || ratio > 3)
145 m_iWideNarrRatio = ratio;
150 char checksum = CalcCheckSum(contents);
152 return DataVector<uint8_t>();
154 std::array<uint8_t, kArraySize> widths = {};
155 constexpr int32_t kWideStrideNum = 3;
156 constexpr int32_t kNarrowStrideNum = kArraySize - kWideStrideNum;
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], m_iWideNarrRatio, 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], m_iWideNarrRatio, widths);
177 result_span = AppendPattern(result_span, widths,
true);
179 static constexpr uint8_t kNarrowWhite[] = {1};
180 result_span = AppendPattern(result_span, kNarrowWhite,
false);
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], m_iWideNarrRatio, widths);
188 result_span = AppendPattern(result_span, widths,
true);
190 result_span = AppendPattern(result_span, kNarrowWhite,
false);
192 ToIntArray(kOnedCode39CharacterEncoding[39], m_iWideNarrRatio, widths);
193 AppendPattern(result_span, widths,
true);
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];
210 checksum = CalcCheckSum(str);
220 pdfium::span<
const uint8_t> code) {
224 return CBC_OneDimWriter::RenderResult(encodedCon.AsStringView(), code);
fxcrt::ByteString ByteString
static bool HasValidContentSize(WideStringView contents)
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)
ByteString ToUTF8() const
WideString & operator=(WideString &&that) noexcept
WideString & operator+=(wchar_t ch)
bool FXSYS_IsDecimalDigit(wchar_t c)
char FXSYS_ToUpperASCII(char c)
fxcrt::WideStringView WideStringView
fxcrt::WideString WideString