Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
BC_OnedCode39Writer.cpp
Go to the documentation of this file.
1// Copyright 2014 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6// Original code is licensed as follows:
7/*
8 * Copyright 2010 ZXing authors
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#include "fxbarcode/oned/BC_OnedCode39Writer.h"
24
25#include <algorithm>
26#include <iterator>
27#include <memory>
28
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"
34
35namespace {
36
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);
42
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");
48
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");
56
57bool IsInOnedCode39Alphabet(wchar_t ch) {
58 return FXSYS_IsDecimalDigit(ch) || (ch >= L'A' && ch <= L'Z') || ch == L'-' ||
59 ch == L'.' || ch == L' ' || ch == L'*' || ch == L'$' || ch == L'/' ||
60 ch == L'+' || ch == L'%';
61}
62
63char CalcCheckSum(const ByteString& contents) {
64 if (contents.GetLength() > 80)
65 return '*';
66
67 int32_t checksum = 0;
68 for (const auto& c : contents) {
69 size_t j = 0;
70 for (; j < kOnedCode39AlphabetLen; j++) {
71 if (kOnedCode39Alphabet[j] == c) {
72 if (c != '*')
73 checksum += j;
74 break;
75 }
76 }
77 if (j >= kOnedCode39AlphabetLen)
78 return '*';
79 }
80 return kOnedCode39Checksum[checksum % std::size(kOnedCode39Checksum)];
81}
82
83} // namespace
84
85CBC_OnedCode39Writer::CBC_OnedCode39Writer() = default;
86
87CBC_OnedCode39Writer::~CBC_OnedCode39Writer() = default;
88
89bool CBC_OnedCode39Writer::CheckContentValidity(WideStringView contents) {
90 return HasValidContentSize(contents) &&
91 std::all_of(contents.begin(), contents.end(), IsInOnedCode39Alphabet);
92}
93
94WideString CBC_OnedCode39Writer::FilterContents(WideStringView contents) {
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)) {
100 continue;
101 }
102 if (ch > 175) {
103 i++;
104 continue;
105 }
107 if (IsInOnedCode39Alphabet(ch))
108 filtercontents += ch;
109 }
110 return filtercontents;
111}
112
113WideString CBC_OnedCode39Writer::RenderTextContents(WideStringView contents) {
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)) {
118 continue;
119 }
120 if (ch > 175) {
121 i++;
122 continue;
123 }
124 if (IsInOnedCode39Alphabet(FXSYS_ToUpperASCII(ch)))
125 renderContents += ch;
126 }
127 return renderContents;
128}
129
130void CBC_OnedCode39Writer::SetTextLocation(BC_TEXT_LOC location) {
131 m_locTextLoc = location;
132}
133
134bool CBC_OnedCode39Writer::SetWideNarrowRatio(int8_t ratio) {
135 if (ratio < 2 || ratio > 3)
136 return false;
137
138 m_iWideNarrRatio = ratio;
139 return true;
140}
141
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;
146 }
147}
148
149DataVector<uint8_t> CBC_OnedCode39Writer::Encode(const ByteString& contents) {
150 char checksum = CalcCheckSum(contents);
151 if (checksum == '*')
152 return DataVector<uint8_t>();
153
154 uint8_t widths[kArraySize] = {0};
155 constexpr int32_t kWideStrideNum = 3;
156 constexpr int32_t kNarrowStrideNum = kArraySize - kWideStrideNum;
157 ByteString encodedContents = contents;
158 if (m_bCalcChecksum)
159 encodedContents += checksum;
160 m_iContentLen = encodedContents.GetLength();
161 size_t code_width =
162 (kWideStrideNum * m_iWideNarrRatio + kNarrowStrideNum) * 2 + 1 +
163 m_iContentLen;
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])
167 continue;
168
169 ToIntArray(kOnedCode39CharacterEncoding[i], widths);
170 for (size_t k = 0; k < kArraySize; k++)
171 code_width += widths[k];
172 }
173 }
174 DataVector<uint8_t> result(code_width);
175 auto result_span = pdfium::make_span(result);
176 ToIntArray(kOnedCode39CharacterEncoding[39], widths);
177 result_span = AppendPattern(result_span, widths, true);
178
179 static constexpr uint8_t kNarrowWhite[] = {1};
180 result_span = AppendPattern(result_span, kNarrowWhite, false);
181
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])
185 continue;
186
187 ToIntArray(kOnedCode39CharacterEncoding[i], widths);
188 result_span = AppendPattern(result_span, widths, true);
189 }
190 result_span = AppendPattern(result_span, kNarrowWhite, false);
191 }
192 ToIntArray(kOnedCode39CharacterEncoding[39], widths);
193 AppendPattern(result_span, widths, true);
194
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];
199 }
200 return result;
201}
202
203bool CBC_OnedCode39Writer::encodedContents(WideStringView contents,
204 WideString* result) {
205 *result = WideString(contents);
207 WideString checksumContent = FilterContents(contents);
208 ByteString str = checksumContent.ToUTF8();
209 char checksum;
210 checksum = CalcCheckSum(str);
211 if (checksum == '*')
212 return false;
213 str += checksum;
214 *result += checksum;
215 }
216 return true;
217}
218
219bool CBC_OnedCode39Writer::RenderResult(WideStringView contents,
220 pdfium::span<const uint8_t> code) {
221 WideString encodedCon;
222 if (!encodedContents(contents, &encodedCon))
223 return false;
224 return CBC_OneDimWriter::RenderResult(encodedCon.AsStringView(), code);
225}
BC_TEXT_LOC
Definition BC_Library.h:12
BC_TEXT_LOC m_locTextLoc
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
Definition bytestring.h:150
ByteString ToUTF8() const
WideString & operator=(WideString &&that) noexcept
WideString & operator+=(wchar_t ch)
bool FXSYS_IsDecimalDigit(wchar_t c)
char FXSYS_ToUpperASCII(char c)