Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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/check.h"
28
#
include
"core/fxcrt/compiler_specific.h"
29
#
include
"core/fxcrt/fx_extension.h"
30
#
include
"fxbarcode/datamatrix/BC_Encoder.h"
31
#
include
"fxbarcode/datamatrix/BC_EncoderContext.h"
32
#
include
"fxbarcode/datamatrix/BC_HighLevelEncoder.h"
33
#
include
"fxbarcode/datamatrix/BC_SymbolInfo.h"
34
35
namespace
{
36
37
WideString
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
(
WideStringView
(
pdfium
::make_span(cw)));
46
}
47
48
}
// namespace
49
50
CBC_C40Encoder
::
CBC_C40Encoder
() =
default
;
51
52
CBC_C40Encoder
::~
CBC_C40Encoder
() =
default
;
53
54
CBC_HighLevelEncoder
::
Encoding
CBC_C40Encoder
::
GetEncodingMode
() {
55
return
CBC_HighLevelEncoder
::
Encoding
::
C40
;
56
}
57
58
bool
CBC_C40Encoder
::
Encode
(
CBC_EncoderContext
* context) {
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) {
92
CBC_HighLevelEncoder
::
Encoding
newMode =
93
CBC_HighLevelEncoder::LookAheadTest(context->m_msg, context->m_pos,
94
GetEncodingMode
(
)
);
95
if
(newMode !=
GetEncodingMode
(
)
) {
96
context
->
SignalEncoderChange
(
newMode
)
;
97
break
;
98
}
99
}
100
}
101
return
HandleEOD
(
context
,
&buffer
)
;
102
}
103
104
void
CBC_C40Encoder
::
WriteNextTriplet
(
CBC_EncoderContext
* context,
105
WideString
* buffer) {
106
context
->
writeCodewords
(
EncodeToC40Codewords(*buffer)
)
;
107
buffer->Delete(0, 3);
108
}
109
110
bool
CBC_C40Encoder
::
HandleEOD
(
CBC_EncoderContext
* context,
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
(
)
) {
124
context
->
writeCodeword
(
CBC_HighLevelEncoder
::
C40_UNLATCH
)
;
125
}
126
}
else
if
(available == 1 && rest == 1) {
127
while
(buffer->GetLength() >= 3)
128
WriteNextTriplet
(
context
,
buffer
)
;
129
if
(context
->
hasMoreCharacters
(
)
) {
130
context
->
writeCodeword
(
CBC_HighLevelEncoder
::
C40_UNLATCH
)
;
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
(
)
) {
137
context
->
writeCodeword
(
CBC_HighLevelEncoder
::
C40_UNLATCH
)
;
138
}
139
}
else
{
140
return
false
;
141
}
142
context
->
SignalEncoderChange
(
CBC_HighLevelEncoder
::
Encoding
::
ASCII
)
;
143
return
true
;
144
}
145
146
int32_t
CBC_C40Encoder
::
EncodeChar
(
wchar_t
c,
WideString
* sb) {
147
if
(c ==
' '
) {
148
*sb
+=
(
wchar_t
)
'\3'
;
149
return
1;
150
}
151
if
(
FXSYS_IsDecimalDigit
(
c
)
) {
152
*sb
+=
(
wchar_t
)(c - 48 + 4);
153
return
1;
154
}
155
if
(
FXSYS_IsUpperASCII
(
c
)
) {
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
193
int32_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
}
DCHECK
#define DCHECK
Definition
check.h:33
CBC_C40Encoder
Definition
BC_C40Encoder.h:13
CBC_C40Encoder::WriteNextTriplet
static void WriteNextTriplet(CBC_EncoderContext *context, WideString *buffer)
Definition
BC_C40Encoder.cpp:104
CBC_C40Encoder::EncodeChar
virtual int32_t EncodeChar(wchar_t c, WideString *sb)
Definition
BC_C40Encoder.cpp:146
CBC_C40Encoder::GetEncodingMode
CBC_HighLevelEncoder::Encoding GetEncodingMode() override
Definition
BC_C40Encoder.cpp:54
CBC_C40Encoder::HandleEOD
virtual bool HandleEOD(CBC_EncoderContext *context, WideString *buffer)
Definition
BC_C40Encoder.cpp:110
CBC_C40Encoder::CBC_C40Encoder
CBC_C40Encoder()
CBC_C40Encoder::Encode
bool Encode(CBC_EncoderContext *context) override
Definition
BC_C40Encoder.cpp:58
CBC_C40Encoder::~CBC_C40Encoder
~CBC_C40Encoder() override
CBC_EncoderContext
Definition
BC_EncoderContext.h:16
CBC_EncoderContext::writeCodewords
void writeCodewords(const WideString &codewords)
Definition
BC_EncoderContext.cpp:59
CBC_EncoderContext::writeCodeword
void writeCodeword(wchar_t codeword)
Definition
BC_EncoderContext.cpp:63
CBC_EncoderContext::getCurrentChar
wchar_t getCurrentChar()
Definition
BC_EncoderContext.cpp:52
CBC_EncoderContext::resetSymbolInfo
void resetSymbolInfo()
Definition
BC_EncoderContext.cpp:101
CBC_EncoderContext::UpdateSymbolInfo
bool UpdateSymbolInfo()
Definition
BC_EncoderContext.cpp:88
CBC_EncoderContext::hasMoreCharacters
bool hasMoreCharacters()
Definition
BC_EncoderContext.cpp:80
CBC_EncoderContext::SignalEncoderChange
void SignalEncoderChange(CBC_HighLevelEncoder::Encoding encoding)
Definition
BC_EncoderContext.cpp:71
CBC_HighLevelEncoder
Definition
BC_HighLevelEncoder.h:12
CBC_HighLevelEncoder::C40_UNLATCH
static const wchar_t C40_UNLATCH
Definition
BC_HighLevelEncoder.h:42
CBC_HighLevelEncoder::Encoding
Encoding
Definition
BC_HighLevelEncoder.h:14
CBC_HighLevelEncoder::Encoding::C40
@ C40
Definition
BC_HighLevelEncoder.h:17
CBC_HighLevelEncoder::Encoding::ASCII
@ ASCII
Definition
BC_HighLevelEncoder.h:16
fxcrt::WideString::operator+=
WideString & operator+=(wchar_t ch)
Definition
widestring.cpp:486
pdfium::CFDETextOutLargeBitmapTest
Definition
cfde_textout_unittest.cpp:117
FXSYS_IsDecimalDigit
bool FXSYS_IsDecimalDigit(wchar_t c)
Definition
fx_extension.h:105
FXSYS_IsUpperASCII
bool FXSYS_IsUpperASCII(int32_t c)
Definition
fx_extension.h:55
WideStringView
fxcrt::WideStringView WideStringView
Definition
string_view_template.h:326
WideString
fxcrt::WideString WideString
Definition
widestring.h:207
qtwebengine
src
3rdparty
chromium
third_party
pdfium
fxbarcode
datamatrix
BC_C40Encoder.cpp
Generated on
for Qt by
1.18.0