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 <array>
27 #include <iterator>
28 #include <memory>
29
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"
35
36 namespace {
37
38 constexpr 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', '-', '.', ' ', '*', '$', '/', '+', '%'});
42 constexpr size_t kOnedCode39AlphabetLen = std::size(kOnedCode39Alphabet);
43
44 constexpr 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', '-', '.', ' ', '$', '/', '+', '%'});
48 static_assert(std::size(kOnedCode39Checksum) == 43, "Wrong size");
49
50 constexpr 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});
56 static_assert(std::size(kOnedCode39CharacterEncoding) == 44, "Wrong size");
57
IsInOnedCode39Alphabet(wchar_t ch)58 bool IsInOnedCode39Alphabet(wchar_t ch) {
59 return FXSYS_IsDecimalDigit(ch) || (ch >= L'A' && ch <= L'Z') || ch == L'-' ||
60 ch == L'.' || ch == L' ' || ch == L'*' || ch == L'$' || ch == L'/' ||
61 ch == L'+' || ch == L'%';
62 }
63
CalcCheckSum(const ByteString & contents)64 char CalcCheckSum(const ByteString& contents) {
65 if (contents.GetLength() > 80)
66 return '*';
67
68 int32_t checksum = 0;
69 for (const auto& c : contents) {
70 size_t j = 0;
71 for (; j < kOnedCode39AlphabetLen; j++) {
72 if (kOnedCode39Alphabet[j] == c) {
73 if (c != '*')
74 checksum += j;
75 break;
76 }
77 }
78 if (j >= kOnedCode39AlphabetLen)
79 return '*';
80 }
81 return kOnedCode39Checksum[checksum % std::size(kOnedCode39Checksum)];
82 }
83
ToIntArray(int16_t value,uint8_t ratio,pdfium::span<uint8_t> span)84 void 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;
87 }
88 }
89
90 } // namespace
91
92 CBC_OnedCode39Writer::CBC_OnedCode39Writer() = default;
93
94 CBC_OnedCode39Writer::~CBC_OnedCode39Writer() = default;
95
CheckContentValidity(WideStringView contents)96 bool CBC_OnedCode39Writer::CheckContentValidity(WideStringView contents) {
97 return HasValidContentSize(contents) &&
98 std::all_of(contents.begin(), contents.end(), IsInOnedCode39Alphabet);
99 }
100
FilterContents(WideStringView contents)101 WideString CBC_OnedCode39Writer::FilterContents(WideStringView contents) {
102 WideString filtercontents;
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)) {
107 continue;
108 }
109 if (ch > 175) {
110 i++;
111 continue;
112 }
113 ch = FXSYS_ToUpperASCII(ch);
114 if (IsInOnedCode39Alphabet(ch))
115 filtercontents += ch;
116 }
117 return filtercontents;
118 }
119
RenderTextContents(WideStringView contents)120 WideString CBC_OnedCode39Writer::RenderTextContents(WideStringView contents) {
121 WideString renderContents;
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)) {
125 continue;
126 }
127 if (ch > 175) {
128 i++;
129 continue;
130 }
131 if (IsInOnedCode39Alphabet(FXSYS_ToUpperASCII(ch)))
132 renderContents += ch;
133 }
134 return renderContents;
135 }
136
SetTextLocation(BC_TEXT_LOC location)137 void CBC_OnedCode39Writer::SetTextLocation(BC_TEXT_LOC location) {
138 m_locTextLoc = location;
139 }
140
SetWideNarrowRatio(int8_t ratio)141 bool CBC_OnedCode39Writer::SetWideNarrowRatio(int8_t ratio) {
142 if (ratio < 2 || ratio > 3)
143 return false;
144
145 m_iWideNarrRatio = ratio;
146 return true;
147 }
148
Encode(const ByteString & contents)149 DataVector<uint8_t> CBC_OnedCode39Writer::Encode(const ByteString& contents) {
150 char checksum = CalcCheckSum(contents);
151 if (checksum == '*')
152 return DataVector<uint8_t>();
153
154 std::array<uint8_t, kArraySize> widths = {};
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], m_iWideNarrRatio, 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], m_iWideNarrRatio, 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], m_iWideNarrRatio, widths);
188 result_span = AppendPattern(result_span, widths, true);
189 }
190 result_span = AppendPattern(result_span, kNarrowWhite, false);
191 }
192 ToIntArray(kOnedCode39CharacterEncoding[39], m_iWideNarrRatio, 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
encodedContents(WideStringView contents,WideString * result)203 bool CBC_OnedCode39Writer::encodedContents(WideStringView contents,
204 WideString* result) {
205 *result = WideString(contents);
206 if (m_bCalcChecksum && m_bPrintChecksum) {
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
RenderResult(WideStringView contents,pdfium::span<const uint8_t> code)219 bool 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 }
226