• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_OnedCode128Writer.h"
24 
25 #include <ctype.h>
26 
27 #include <memory>
28 
29 #include "core/fxcrt/fx_memory_wrappers.h"
30 #include "fxbarcode/BC_Writer.h"
31 #include "fxbarcode/oned/BC_OneDimWriter.h"
32 #include "third_party/base/check.h"
33 
34 namespace {
35 
36 constexpr size_t kPatternSize = 7;
37 
38 const uint8_t kCodePatterns[107][kPatternSize] = {
39     {2, 1, 2, 2, 2, 2, 0}, {2, 2, 2, 1, 2, 2, 0}, {2, 2, 2, 2, 2, 1, 0},
40     {1, 2, 1, 2, 2, 3, 0}, {1, 2, 1, 3, 2, 2, 0}, {1, 3, 1, 2, 2, 2, 0},
41     {1, 2, 2, 2, 1, 3, 0}, {1, 2, 2, 3, 1, 2, 0}, {1, 3, 2, 2, 1, 2, 0},
42     {2, 2, 1, 2, 1, 3, 0}, {2, 2, 1, 3, 1, 2, 0}, {2, 3, 1, 2, 1, 2, 0},
43     {1, 1, 2, 2, 3, 2, 0}, {1, 2, 2, 1, 3, 2, 0}, {1, 2, 2, 2, 3, 1, 0},
44     {1, 1, 3, 2, 2, 2, 0}, {1, 2, 3, 1, 2, 2, 0}, {1, 2, 3, 2, 2, 1, 0},
45     {2, 2, 3, 2, 1, 1, 0}, {2, 2, 1, 1, 3, 2, 0}, {2, 2, 1, 2, 3, 1, 0},
46     {2, 1, 3, 2, 1, 2, 0}, {2, 2, 3, 1, 1, 2, 0}, {3, 1, 2, 1, 3, 1, 0},
47     {3, 1, 1, 2, 2, 2, 0}, {3, 2, 1, 1, 2, 2, 0}, {3, 2, 1, 2, 2, 1, 0},
48     {3, 1, 2, 2, 1, 2, 0}, {3, 2, 2, 1, 1, 2, 0}, {3, 2, 2, 2, 1, 1, 0},
49     {2, 1, 2, 1, 2, 3, 0}, {2, 1, 2, 3, 2, 1, 0}, {2, 3, 2, 1, 2, 1, 0},
50     {1, 1, 1, 3, 2, 3, 0}, {1, 3, 1, 1, 2, 3, 0}, {1, 3, 1, 3, 2, 1, 0},
51     {1, 1, 2, 3, 1, 3, 0}, {1, 3, 2, 1, 1, 3, 0}, {1, 3, 2, 3, 1, 1, 0},
52     {2, 1, 1, 3, 1, 3, 0}, {2, 3, 1, 1, 1, 3, 0}, {2, 3, 1, 3, 1, 1, 0},
53     {1, 1, 2, 1, 3, 3, 0}, {1, 1, 2, 3, 3, 1, 0}, {1, 3, 2, 1, 3, 1, 0},
54     {1, 1, 3, 1, 2, 3, 0}, {1, 1, 3, 3, 2, 1, 0}, {1, 3, 3, 1, 2, 1, 0},
55     {3, 1, 3, 1, 2, 1, 0}, {2, 1, 1, 3, 3, 1, 0}, {2, 3, 1, 1, 3, 1, 0},
56     {2, 1, 3, 1, 1, 3, 0}, {2, 1, 3, 3, 1, 1, 0}, {2, 1, 3, 1, 3, 1, 0},
57     {3, 1, 1, 1, 2, 3, 0}, {3, 1, 1, 3, 2, 1, 0}, {3, 3, 1, 1, 2, 1, 0},
58     {3, 1, 2, 1, 1, 3, 0}, {3, 1, 2, 3, 1, 1, 0}, {3, 3, 2, 1, 1, 1, 0},
59     {3, 1, 4, 1, 1, 1, 0}, {2, 2, 1, 4, 1, 1, 0}, {4, 3, 1, 1, 1, 1, 0},
60     {1, 1, 1, 2, 2, 4, 0}, {1, 1, 1, 4, 2, 2, 0}, {1, 2, 1, 1, 2, 4, 0},
61     {1, 2, 1, 4, 2, 1, 0}, {1, 4, 1, 1, 2, 2, 0}, {1, 4, 1, 2, 2, 1, 0},
62     {1, 1, 2, 2, 1, 4, 0}, {1, 1, 2, 4, 1, 2, 0}, {1, 2, 2, 1, 1, 4, 0},
63     {1, 2, 2, 4, 1, 1, 0}, {1, 4, 2, 1, 1, 2, 0}, {1, 4, 2, 2, 1, 1, 0},
64     {2, 4, 1, 2, 1, 1, 0}, {2, 2, 1, 1, 1, 4, 0}, {4, 1, 3, 1, 1, 1, 0},
65     {2, 4, 1, 1, 1, 2, 0}, {1, 3, 4, 1, 1, 1, 0}, {1, 1, 1, 2, 4, 2, 0},
66     {1, 2, 1, 1, 4, 2, 0}, {1, 2, 1, 2, 4, 1, 0}, {1, 1, 4, 2, 1, 2, 0},
67     {1, 2, 4, 1, 1, 2, 0}, {1, 2, 4, 2, 1, 1, 0}, {4, 1, 1, 2, 1, 2, 0},
68     {4, 2, 1, 1, 1, 2, 0}, {4, 2, 1, 2, 1, 1, 0}, {2, 1, 2, 1, 4, 1, 0},
69     {2, 1, 4, 1, 2, 1, 0}, {4, 1, 2, 1, 2, 1, 0}, {1, 1, 1, 1, 4, 3, 0},
70     {1, 1, 1, 3, 4, 1, 0}, {1, 3, 1, 1, 4, 1, 0}, {1, 1, 4, 1, 1, 3, 0},
71     {1, 1, 4, 3, 1, 1, 0}, {4, 1, 1, 1, 1, 3, 0}, {4, 1, 1, 3, 1, 1, 0},
72     {1, 1, 3, 1, 4, 1, 0}, {1, 1, 4, 1, 3, 1, 0}, {3, 1, 1, 1, 4, 1, 0},
73     {4, 1, 1, 1, 3, 1, 0}, {2, 1, 1, 4, 1, 2, 0}, {2, 1, 1, 2, 1, 4, 0},
74     {2, 1, 1, 2, 3, 2, 0}, {2, 3, 3, 1, 1, 1, 2}};
75 
76 const int32_t CODE_START_B = 104;
77 const int32_t CODE_START_C = 105;
78 const int32_t CODE_STOP = 106;
79 
IsInOnedCode128Alphabet(wchar_t ch)80 bool IsInOnedCode128Alphabet(wchar_t ch) {
81   int32_t index = static_cast<int32_t>(ch);
82   return index >= 32 && index <= 126 && index != 34;
83 }
84 
85 }  // namespace
86 
CBC_OnedCode128Writer(BC_TYPE type)87 CBC_OnedCode128Writer::CBC_OnedCode128Writer(BC_TYPE type)
88     : m_codeFormat(type) {
89   DCHECK(m_codeFormat == BC_TYPE::kCode128B ||
90          m_codeFormat == BC_TYPE::kCode128C);
91 }
92 
93 CBC_OnedCode128Writer::~CBC_OnedCode128Writer() = default;
94 
CheckContentValidity(WideStringView contents)95 bool CBC_OnedCode128Writer::CheckContentValidity(WideStringView contents) {
96   return HasValidContentSize(contents) &&
97          std::all_of(contents.begin(), contents.end(), IsInOnedCode128Alphabet);
98 }
99 
FilterContents(WideStringView contents)100 WideString CBC_OnedCode128Writer::FilterContents(WideStringView contents) {
101   const wchar_t limit = m_codeFormat == BC_TYPE::kCode128B ? 126 : 106;
102 
103   WideString filtered;
104   filtered.Reserve(contents.GetLength());
105   for (size_t i = 0; i < contents.GetLength(); i++) {
106     wchar_t ch = contents[i];
107     if (ch > 175) {
108       i++;
109       continue;
110     }
111     if (ch >= 32 && ch <= limit)
112       filtered += ch;
113   }
114   return filtered;
115 }
116 
SetTextLocation(BC_TEXT_LOC location)117 void CBC_OnedCode128Writer::SetTextLocation(BC_TEXT_LOC location) {
118   m_locTextLoc = location;
119 }
120 
Encode(const ByteString & contents)121 DataVector<uint8_t> CBC_OnedCode128Writer::Encode(const ByteString& contents) {
122   if (contents.GetLength() < 1 || contents.GetLength() > 80)
123     return DataVector<uint8_t>();
124 
125   std::vector<int32_t> patterns;
126   int32_t checkSum = 0;
127   if (m_codeFormat == BC_TYPE::kCode128B)
128     checkSum = Encode128B(contents, &patterns);
129   else
130     checkSum = Encode128C(contents, &patterns);
131 
132   checkSum %= 103;
133   patterns.push_back(checkSum);
134   patterns.push_back(CODE_STOP);
135   m_iContentLen = contents.GetLength() + 3;
136   int32_t codeWidth = 0;
137   for (const auto& patternIndex : patterns) {
138     const uint8_t* pattern = kCodePatterns[patternIndex];
139     for (size_t i = 0; i < kPatternSize; ++i)
140       codeWidth += pattern[i];
141   }
142   DataVector<uint8_t> result(codeWidth);
143   auto result_span = pdfium::make_span(result);
144   for (const int32_t pattern_index : patterns) {
145     const uint8_t* pattern = kCodePatterns[pattern_index];
146     result_span = AppendPattern(result_span, {pattern, kPatternSize}, true);
147   }
148   return result;
149 }
150 
151 // static
Encode128B(const ByteString & contents,std::vector<int32_t> * patterns)152 int32_t CBC_OnedCode128Writer::Encode128B(const ByteString& contents,
153                                           std::vector<int32_t>* patterns) {
154   int32_t checkWeight = 1;
155   patterns->push_back(CODE_START_B);
156   int32_t checkSum = CODE_START_B * checkWeight;
157   for (size_t position = 0; position < contents.GetLength(); position++) {
158     int32_t patternIndex = contents[position] - ' ';
159     patterns->push_back(patternIndex);
160     checkSum += patternIndex * checkWeight++;
161   }
162   return checkSum;
163 }
164 
165 // static
Encode128C(const ByteString & contents,std::vector<int32_t> * patterns)166 int32_t CBC_OnedCode128Writer::Encode128C(const ByteString& contents,
167                                           std::vector<int32_t>* patterns) {
168   int32_t checkWeight = 1;
169   patterns->push_back(CODE_START_C);
170   int32_t checkSum = CODE_START_C * checkWeight;
171   size_t position = 0;
172   while (position < contents.GetLength()) {
173     int32_t patternIndex;
174     char ch = contents[position];
175     if (isdigit(ch)) {
176       patternIndex = FXSYS_atoi(
177           contents.Substr(position, contents.IsValidIndex(position + 1) ? 2 : 1)
178               .c_str());
179       ++position;
180       if (position < contents.GetLength() && isdigit(contents[position]))
181         ++position;
182     } else {
183       patternIndex = static_cast<int32_t>(ch);
184       ++position;
185     }
186     patterns->push_back(patternIndex);
187     checkSum += patternIndex * checkWeight++;
188   }
189   return checkSum;
190 }
191