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_OnedCodaBarWriter.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 2011 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_OnedCodaBarWriter.h"
24
25#include <iterator>
26
27#include "core/fxcrt/fx_2d_size.h"
28#include "core/fxcrt/fx_extension.h"
29#include "fxbarcode/BC_Writer.h"
30#include "fxbarcode/common/BC_CommonBitMatrix.h"
31#include "fxbarcode/oned/BC_OneDimWriter.h"
32#include "third_party/base/containers/contains.h"
33
34namespace {
35
36const char kOnedCodaAlphabet[] = {'0', '1', '2', '3', '4', '5', '6', '7',
37 '8', '9', '-', '$', ':', '/', '.', '+',
38 'A', 'B', 'C', 'D', 'T', 'N'};
39
40const int8_t kOnedCodaCharacterEncoding[] = {
41 0x03, 0x06, 0x09, 0x60, 0x12, 0x42, 0x21, 0x24, 0x30, 0x48, 0x0c,
42 0x18, 0x45, 0x51, 0x54, 0x15, 0x1A, 0x29, 0x0B, 0x0E, 0x1A, 0x29};
43static_assert(std::size(kOnedCodaCharacterEncoding) == 22, "Wrong size");
44static_assert(std::size(kOnedCodaCharacterEncoding) ==
45 std::size(kOnedCodaAlphabet),
46 "Wrong size");
47
48const char kStartEndChars[] = {'A', 'B', 'C', 'D', 'T', 'N', '*', 'E',
49 'a', 'b', 'c', 'd', 't', 'n', 'e'};
50const char kContentChars[] = {'0', '1', '2', '3', '4', '5', '6', '7',
51 '8', '9', '-', '$', '/', ':', '+', '.'};
52
53bool IsValidChar(wchar_t ch, bool isContent) {
54 if (ch > 0x7F)
55 return false;
56
57 char narrow_ch = static_cast<char>(ch);
58 return pdfium::Contains(kContentChars, narrow_ch) ||
59 (isContent && pdfium::Contains(kStartEndChars, narrow_ch));
60}
61
62} // namespace
63
64CBC_OnedCodaBarWriter::CBC_OnedCodaBarWriter() = default;
65
66CBC_OnedCodaBarWriter::~CBC_OnedCodaBarWriter() = default;
67
68bool CBC_OnedCodaBarWriter::SetStartChar(char start) {
69 if (!pdfium::Contains(kStartEndChars, start))
70 return false;
71
72 m_chStart = start;
73 return true;
74}
75
76bool CBC_OnedCodaBarWriter::SetEndChar(char end) {
77 if (!pdfium::Contains(kStartEndChars, end))
78 return false;
79
80 m_chEnd = end;
81 return true;
82}
83
84void CBC_OnedCodaBarWriter::SetDataLength(int32_t length) {
85 m_iDataLenth = length + 2;
86}
87
88void CBC_OnedCodaBarWriter::SetTextLocation(BC_TEXT_LOC location) {
89 m_locTextLoc = location;
90}
91
92bool CBC_OnedCodaBarWriter::SetWideNarrowRatio(int8_t ratio) {
93 if (ratio < 2 || ratio > 3)
94 return false;
95
96 m_iWideNarrRatio = ratio;
97 return true;
98}
99
100bool CBC_OnedCodaBarWriter::CheckContentValidity(WideStringView contents) {
101 return HasValidContentSize(contents) &&
102 std::all_of(contents.begin(), contents.end(),
103 [](const wchar_t& ch) { return IsValidChar(ch, false); });
104}
105
106WideString CBC_OnedCodaBarWriter::FilterContents(WideStringView contents) {
107 WideString filtercontents;
108 filtercontents.Reserve(contents.GetLength());
109 wchar_t ch;
110 for (size_t index = 0; index < contents.GetLength(); index++) {
111 ch = contents[index];
112 if (ch > 175) {
113 index++;
114 continue;
115 }
116 if (!IsValidChar(ch, true))
117 continue;
118 filtercontents += ch;
119 }
120 return filtercontents;
121}
122
123DataVector<uint8_t> CBC_OnedCodaBarWriter::Encode(const ByteString& contents) {
124 ByteString data = m_chStart + contents + m_chEnd;
125 m_iContentLen = data.GetLength();
126 DataVector<uint8_t> result(
127 Fx2DSizeOrDie(m_iWideNarrRatio * 7, data.GetLength()));
128 char ch;
129 int32_t position = 0;
130 for (size_t index = 0; index < data.GetLength(); index++) {
131 ch = FXSYS_ToUpperASCII(data[index]);
132 switch (ch) {
133 case 'T':
134 ch = 'A';
135 break;
136 case 'N':
137 ch = 'B';
138 break;
139 case '*':
140 ch = 'C';
141 break;
142 case 'E':
143 ch = 'D';
144 break;
145 default:
146 break;
147 }
148 int8_t code = 0;
149 for (size_t i = 0; i < std::size(kOnedCodaAlphabet); i++) {
150 if (ch == kOnedCodaAlphabet[i]) {
151 code = kOnedCodaCharacterEncoding[i];
152 break;
153 }
154 }
155 uint8_t color = 1;
156 int32_t counter = 0;
157 int32_t bit = 0;
158 while (bit < 7) {
159 result[position] = color;
160 position++;
161 if (((code >> (6 - bit)) & 1) == 0 || counter == m_iWideNarrRatio - 1) {
162 color = !color;
163 bit++;
164 counter = 0;
165 } else {
166 counter++;
167 }
168 }
169 if (index < data.GetLength() - 1) {
170 result[position] = 0;
171 position++;
172 }
173 }
174 result.resize(position);
175 return result;
176}
177
178WideString CBC_OnedCodaBarWriter::encodedContents(WideStringView contents) {
179 WideString strStart(static_cast<wchar_t>(m_chStart));
180 WideString strEnd(static_cast<wchar_t>(m_chEnd));
181 return strStart + contents + strEnd;
182}
183
184bool CBC_OnedCodaBarWriter::RenderResult(WideStringView contents,
185 pdfium::span<const uint8_t> code) {
187 encodedContents(contents).AsStringView(), code);
188}
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)
bool SetEndChar(char end) override
WideString encodedContents(WideStringView contents)
void SetTextLocation(BC_TEXT_LOC location) override
void SetDataLength(int32_t length) override
bool SetWideNarrowRatio(int8_t ratio) override
bool RenderResult(WideStringView contents, pdfium::span< const uint8_t > code) override
DataVector< uint8_t > Encode(const ByteString &contents) override
bool CheckContentValidity(WideStringView contents) override
bool SetStartChar(char start) override
~CBC_OnedCodaBarWriter() override
WideString FilterContents(WideStringView contents) override
WideString(wchar_t ch)
WideString & operator+=(wchar_t ch)
ByteString operator+(const ByteString &str1, char ch)
Definition bytestring.h:273
ByteString operator+(char ch, const ByteString &str2)
Definition bytestring.h:276