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_C40Encoder.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 2006-2007 Jeremias Maerki.
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/datamatrix/BC_C40Encoder.h"
24
25#include <iterator>
26
27#include "core/fxcrt/fx_extension.h"
28#include "fxbarcode/common/BC_CommonBitMatrix.h"
29#include "fxbarcode/datamatrix/BC_Encoder.h"
30#include "fxbarcode/datamatrix/BC_EncoderContext.h"
31#include "fxbarcode/datamatrix/BC_HighLevelEncoder.h"
32#include "fxbarcode/datamatrix/BC_SymbolInfo.h"
33#include "third_party/base/check.h"
34
35namespace {
36
37WideString EncodeToC40Codewords(const WideString& sb) {
38 wchar_t c1 = sb[0];
39 wchar_t c2 = sb[1];
40 wchar_t c3 = sb[2];
41 int32_t v = (1600 * c1) + (40 * c2) + c3 + 1;
42 wchar_t cw[2];
43 cw[0] = static_cast<wchar_t>(v / 256);
44 cw[1] = static_cast<wchar_t>(v % 256);
45 return WideString(cw, std::size(cw));
46}
47
48} // namespace
49
51
52CBC_C40Encoder::~CBC_C40Encoder() = default;
53
57
59 WideString buffer;
60 while (context->hasMoreCharacters()) {
61 wchar_t c = context->getCurrentChar();
62 context->m_pos++;
63 int32_t lastCharSize = EncodeChar(c, &buffer);
64 if (lastCharSize <= 0)
65 return false;
66
67 size_t unwritten = (buffer.GetLength() / 3) * 2;
68 int32_t curCodewordCount = context->getCodewordCount() + unwritten;
69 if (!context->UpdateSymbolInfo(curCodewordCount))
70 return false;
71
72 int32_t available =
73 context->m_symbolInfo->data_capacity() - curCodewordCount;
74 if (!context->hasMoreCharacters()) {
75 if ((buffer.GetLength() % 3) == 2) {
76 if (available < 2 || available > 2) {
77 lastCharSize = BacktrackOneCharacter(context, &buffer, lastCharSize);
78 if (lastCharSize < 0)
79 return false;
80 }
81 }
82 while ((buffer.GetLength() % 3) == 1 &&
83 ((lastCharSize <= 3 && available != 1) || lastCharSize > 3)) {
84 lastCharSize = BacktrackOneCharacter(context, &buffer, lastCharSize);
85 if (lastCharSize < 0)
86 return false;
87 }
88 break;
89 }
90 size_t count = buffer.GetLength();
91 if ((count % 3) == 0) {
93 CBC_HighLevelEncoder::LookAheadTest(context->m_msg, context->m_pos,
95 if (newMode != GetEncodingMode()) {
96 context->SignalEncoderChange(newMode);
97 break;
98 }
99 }
100 }
101 return HandleEOD(context, &buffer);
102}
103
105 WideString* buffer) {
106 context->writeCodewords(EncodeToC40Codewords(*buffer));
107 buffer->Delete(0, 3);
108}
109
111 WideString* buffer) {
112 size_t unwritten = (buffer->GetLength() / 3) * 2;
113 size_t rest = buffer->GetLength() % 3;
114 int32_t curCodewordCount = context->getCodewordCount() + unwritten;
115 if (!context->UpdateSymbolInfo(curCodewordCount))
116 return false;
117
118 int32_t available = context->m_symbolInfo->data_capacity() - curCodewordCount;
119 if (rest == 2) {
120 *buffer += (wchar_t)'\0';
121 while (buffer->GetLength() >= 3)
122 WriteNextTriplet(context, buffer);
123 if (context->hasMoreCharacters()) {
125 }
126 } else if (available == 1 && rest == 1) {
127 while (buffer->GetLength() >= 3)
128 WriteNextTriplet(context, buffer);
129 if (context->hasMoreCharacters()) {
131 }
132 context->m_pos--;
133 } else if (rest == 0) {
134 while (buffer->GetLength() >= 3)
135 WriteNextTriplet(context, buffer);
136 if (available > 0 || context->hasMoreCharacters()) {
138 }
139 } else {
140 return false;
141 }
143 return true;
144}
145
146int32_t CBC_C40Encoder::EncodeChar(wchar_t c, WideString* sb) {
147 if (c == ' ') {
148 *sb += (wchar_t)'\3';
149 return 1;
150 }
152 *sb += (wchar_t)(c - 48 + 4);
153 return 1;
154 }
156 *sb += (wchar_t)(c - 65 + 14);
157 return 1;
158 }
159 if (c <= 0x1f) {
160 *sb += (wchar_t)'\0';
161 *sb += c;
162 return 2;
163 }
164 if ((c >= '!') && (c <= '/')) {
165 *sb += (wchar_t)'\1';
166 *sb += (wchar_t)(c - 33);
167 return 2;
168 }
169 if ((c >= ':') && (c <= '@')) {
170 *sb += (wchar_t)'\1';
171 *sb += (wchar_t)(c - 58 + 15);
172 return 2;
173 }
174 if ((c >= '[') && (c <= '_')) {
175 *sb += (wchar_t)'\1';
176 *sb += (wchar_t)(c - 91 + 22);
177 return 2;
178 }
179 if ((c >= 60) && (c <= 0x7f)) {
180 *sb += (wchar_t)'\2';
181 *sb += (wchar_t)(c - 96);
182 return 2;
183 }
184 if (c >= 80) {
185 *sb += (wchar_t)'\1';
186 *sb += (wchar_t)0x001e;
187 int32_t encode_result = EncodeChar(c - 128, sb);
188 return encode_result > 0 ? encode_result + 2 : 0;
189 }
190 return 0;
191}
192
193int32_t CBC_C40Encoder::BacktrackOneCharacter(CBC_EncoderContext* context,
194 WideString* buffer,
195 int32_t lastCharSize) {
196 DCHECK(lastCharSize >= 0);
197
198 if (context->m_pos < 1)
199 return -1;
200
201 size_t count = buffer->GetLength();
202 if (count < static_cast<size_t>(lastCharSize))
203 return -1;
204
205 buffer->Delete(count - lastCharSize, lastCharSize);
206 context->m_pos--;
207 wchar_t c = context->getCurrentChar();
208 WideString removed;
209 int32_t len = EncodeChar(c, &removed);
210 if (len <= 0)
211 return -1;
212
213 context->resetSymbolInfo();
214 return len;
215}
static void WriteNextTriplet(CBC_EncoderContext *context, WideString *buffer)
virtual int32_t EncodeChar(wchar_t c, WideString *sb)
CBC_HighLevelEncoder::Encoding GetEncodingMode() override
virtual bool HandleEOD(CBC_EncoderContext *context, WideString *buffer)
bool Encode(CBC_EncoderContext *context) override
~CBC_C40Encoder() override
void writeCodewords(const WideString &codewords)
void writeCodeword(wchar_t codeword)
void SignalEncoderChange(CBC_HighLevelEncoder::Encoding encoding)
static const wchar_t C40_UNLATCH
WideString & operator+=(wchar_t ch)
CharType operator[](const size_t index) const
Definition widestring.h:146
bool FXSYS_IsDecimalDigit(wchar_t c)
bool FXSYS_IsUpperASCII(int32_t c)