• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 PDFium Authors. All rights reserved.
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 <memory>
27 
28 #include "core/fxcrt/fx_extension.h"
29 #include "core/fxcrt/fx_memory_wrappers.h"
30 #include "fxbarcode/BC_Writer.h"
31 #include "fxbarcode/common/BC_CommonBitMatrix.h"
32 #include "fxbarcode/oned/BC_OneDimWriter.h"
33 
34 namespace {
35 
36 const char kOnedCode39Alphabet[] = {
37     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
38     'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
39     'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '*', '$', '/', '+', '%'};
40 constexpr size_t kOnedCode39AlphabetLen = FX_ArraySize(kOnedCode39Alphabet);
41 
42 const char kOnedCode39Checksum[] = {
43     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
44     'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
45     'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%'};
46 static_assert(FX_ArraySize(kOnedCode39Checksum) == 43, "Wrong size");
47 
48 const int16_t kOnedCode39CharacterEncoding[] = {
49     0x0034, 0x0121, 0x0061, 0x0160, 0x0031, 0x0130, 0x0070, 0x0025, 0x0124,
50     0x0064, 0x0109, 0x0049, 0x0148, 0x0019, 0x0118, 0x0058, 0x000D, 0x010C,
51     0x004C, 0x001C, 0x0103, 0x0043, 0x0142, 0x0013, 0x0112, 0x0052, 0x0007,
52     0x0106, 0x0046, 0x0016, 0x0181, 0x00C1, 0x01C0, 0x0091, 0x0190, 0x00D0,
53     0x0085, 0x0184, 0x00C4, 0x0094, 0x00A8, 0x00A2, 0x008A, 0x002A};
54 static_assert(FX_ArraySize(kOnedCode39CharacterEncoding) == 44, "Wrong size");
55 
IsInOnedCode39Alphabet(wchar_t ch)56 bool IsInOnedCode39Alphabet(wchar_t ch) {
57   return FXSYS_IsDecimalDigit(ch) || (ch >= L'A' && ch <= L'Z') || ch == L'-' ||
58          ch == L'.' || ch == L' ' || ch == L'*' || ch == L'$' || ch == L'/' ||
59          ch == L'+' || ch == L'%';
60 }
61 
62 }  // namespace
63 
64 CBC_OnedCode39Writer::CBC_OnedCode39Writer() = default;
65 
66 CBC_OnedCode39Writer::~CBC_OnedCode39Writer() = default;
67 
CheckContentValidity(WideStringView contents)68 bool CBC_OnedCode39Writer::CheckContentValidity(WideStringView contents) {
69   return std::all_of(contents.begin(), contents.end(), IsInOnedCode39Alphabet);
70 }
71 
FilterContents(WideStringView contents)72 WideString CBC_OnedCode39Writer::FilterContents(WideStringView contents) {
73   WideString filtercontents;
74   filtercontents.Reserve(contents.GetLength());
75   for (size_t i = 0; i < contents.GetLength(); i++) {
76     wchar_t ch = contents[i];
77     if (ch == L'*' && (i == 0 || i == contents.GetLength() - 1)) {
78       continue;
79     }
80     if (ch > 175) {
81       i++;
82       continue;
83     }
84     ch = FXSYS_ToUpperASCII(ch);
85     if (IsInOnedCode39Alphabet(ch))
86       filtercontents += ch;
87   }
88   return filtercontents;
89 }
90 
RenderTextContents(WideStringView contents)91 WideString CBC_OnedCode39Writer::RenderTextContents(WideStringView contents) {
92   WideString renderContents;
93   for (size_t i = 0; i < contents.GetLength(); i++) {
94     wchar_t ch = contents[i];
95     if (ch == L'*' && (i == 0 || i == contents.GetLength() - 1)) {
96       continue;
97     }
98     if (ch > 175) {
99       i++;
100       continue;
101     }
102     if (IsInOnedCode39Alphabet(FXSYS_ToUpperASCII(ch)))
103       renderContents += ch;
104   }
105   return renderContents;
106 }
107 
SetTextLocation(BC_TEXT_LOC location)108 bool CBC_OnedCode39Writer::SetTextLocation(BC_TEXT_LOC location) {
109   if (location < BC_TEXT_LOC_NONE || location > BC_TEXT_LOC_BELOWEMBED) {
110     return false;
111   }
112   m_locTextLoc = location;
113   return true;
114 }
SetWideNarrowRatio(int8_t ratio)115 bool CBC_OnedCode39Writer::SetWideNarrowRatio(int8_t ratio) {
116   if (ratio < 2 || ratio > 3)
117     return false;
118 
119   m_iWideNarrRatio = ratio;
120   return true;
121 }
122 
EncodeWithHint(const ByteString & contents,BCFORMAT format,int32_t & outWidth,int32_t & outHeight,int32_t hints)123 uint8_t* CBC_OnedCode39Writer::EncodeWithHint(const ByteString& contents,
124                                               BCFORMAT format,
125                                               int32_t& outWidth,
126                                               int32_t& outHeight,
127                                               int32_t hints) {
128   if (format != BCFORMAT_CODE_39)
129     return nullptr;
130   return CBC_OneDimWriter::EncodeWithHint(contents, format, outWidth, outHeight,
131                                           hints);
132 }
133 
ToIntArray(int16_t a,int8_t * toReturn)134 void CBC_OnedCode39Writer::ToIntArray(int16_t a, int8_t* toReturn) {
135   for (int32_t i = 0; i < 9; i++) {
136     toReturn[i] = (a & (1 << i)) == 0 ? 1 : m_iWideNarrRatio;
137   }
138 }
139 
CalcCheckSum(const ByteString & contents)140 char CBC_OnedCode39Writer::CalcCheckSum(const ByteString& contents) {
141   if (contents.GetLength() > 80)
142     return '*';
143 
144   int32_t checksum = 0;
145   for (const auto& c : contents) {
146     size_t j = 0;
147     for (; j < kOnedCode39AlphabetLen; j++) {
148       if (kOnedCode39Alphabet[j] == c) {
149         if (c != '*')
150           checksum += j;
151         break;
152       }
153     }
154     if (j >= kOnedCode39AlphabetLen)
155       return '*';
156   }
157   return kOnedCode39Checksum[checksum % FX_ArraySize(kOnedCode39Checksum)];
158 }
159 
EncodeImpl(const ByteString & contents,int32_t & outlength)160 uint8_t* CBC_OnedCode39Writer::EncodeImpl(const ByteString& contents,
161                                           int32_t& outlength) {
162   char checksum = CalcCheckSum(contents);
163   if (checksum == '*')
164     return nullptr;
165 
166   int8_t widths[9] = {0};
167   int32_t wideStrideNum = 3;
168   int32_t narrStrideNum = 9 - wideStrideNum;
169   ByteString encodedContents = contents;
170   if (m_bCalcChecksum)
171     encodedContents += checksum;
172   m_iContentLen = encodedContents.GetLength();
173   int32_t codeWidth = (wideStrideNum * m_iWideNarrRatio + narrStrideNum) * 2 +
174                       1 + m_iContentLen;
175   for (size_t j = 0; j < m_iContentLen; j++) {
176     for (size_t i = 0; i < kOnedCode39AlphabetLen; i++) {
177       if (kOnedCode39Alphabet[i] != encodedContents[j])
178         continue;
179 
180       ToIntArray(kOnedCode39CharacterEncoding[i], widths);
181       for (size_t k = 0; k < 9; k++)
182         codeWidth += widths[k];
183     }
184   }
185   outlength = codeWidth;
186   std::unique_ptr<uint8_t, FxFreeDeleter> result(FX_Alloc(uint8_t, codeWidth));
187   ToIntArray(kOnedCode39CharacterEncoding[39], widths);
188   int32_t pos = AppendPattern(result.get(), 0, widths, 9, true);
189 
190   int8_t narrowWhite[] = {1};
191   pos += AppendPattern(result.get(), pos, narrowWhite, 1, false);
192 
193   for (int32_t l = m_iContentLen - 1; l >= 0; l--) {
194     for (size_t i = 0; i < kOnedCode39AlphabetLen; i++) {
195       if (kOnedCode39Alphabet[i] != encodedContents[l])
196         continue;
197 
198       ToIntArray(kOnedCode39CharacterEncoding[i], widths);
199       pos += AppendPattern(result.get(), pos, widths, 9, true);
200     }
201     pos += AppendPattern(result.get(), pos, narrowWhite, 1, false);
202   }
203   ToIntArray(kOnedCode39CharacterEncoding[39], widths);
204   pos += AppendPattern(result.get(), pos, widths, 9, true);
205 
206   auto* result_ptr = result.get();
207   for (int32_t i = 0; i < codeWidth / 2; i++) {
208     result_ptr[i] ^= result_ptr[codeWidth - 1 - i];
209     result_ptr[codeWidth - 1 - i] ^= result_ptr[i];
210     result_ptr[i] ^= result_ptr[codeWidth - 1 - i];
211   }
212   return result.release();
213 }
214 
encodedContents(WideStringView contents,WideString * result)215 bool CBC_OnedCode39Writer::encodedContents(WideStringView contents,
216                                            WideString* result) {
217   *result = WideString(contents);
218   if (m_bCalcChecksum && m_bPrintChecksum) {
219     WideString checksumContent = FilterContents(contents);
220     ByteString str = checksumContent.ToUTF8();
221     char checksum;
222     checksum = CalcCheckSum(str);
223     if (checksum == '*')
224       return false;
225     str += checksum;
226     *result += checksum;
227   }
228   return true;
229 }
230 
RenderResult(WideStringView contents,uint8_t * code,int32_t codeLength)231 bool CBC_OnedCode39Writer::RenderResult(WideStringView contents,
232                                         uint8_t* code,
233                                         int32_t codeLength) {
234   WideString encodedCon;
235   if (!encodedContents(contents, &encodedCon))
236     return false;
237   return CBC_OneDimWriter::RenderResult(encodedCon.AsStringView(), code,
238                                         codeLength);
239 }
240