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_OnedCode128Writer.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_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
34namespace {
35
36constexpr size_t kPatternSize = 7;
37
38const 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, 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 1, 2}};
75
76const int32_t CODE_START_B = 104;
77const int32_t CODE_START_C = 105;
78const int32_t CODE_STOP = 106;
79
80bool 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
87CBC_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
93CBC_OnedCode128Writer::~CBC_OnedCode128Writer() = default;
94
95bool CBC_OnedCode128Writer::CheckContentValidity(WideStringView contents) {
96 return HasValidContentSize(contents) &&
97 std::all_of(contents.begin(), contents.end(), IsInOnedCode128Alphabet);
98}
99
100WideString 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
117void CBC_OnedCode128Writer::SetTextLocation(BC_TEXT_LOC location) {
118 m_locTextLoc = location;
119}
120
121DataVector<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
152int32_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
166int32_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}
BC_TEXT_LOC
Definition BC_Library.h:12
BC_TYPE
Definition BC_Library.h:25
BC_TEXT_LOC m_locTextLoc
static bool HasValidContentSize(WideStringView contents)
WideString FilterContents(WideStringView contents) override
bool CheckContentValidity(WideStringView contents) override
~CBC_OnedCode128Writer() override
DataVector< uint8_t > Encode(const ByteString &contents) override
void SetTextLocation(BC_TEXT_LOC location) override
WideString & operator+=(wchar_t ch)