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_ASCIIEncoder.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_ASCIIEncoder.h"
24
25
#
include
<
optional
>
26
27
#
include
"core/fxcrt/compiler_specific.h"
28
#
include
"core/fxcrt/fx_extension.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
34
namespace
{
35
36
std
::optional<
wchar_t
> EncodeASCIIDigits(
wchar_t
digit1,
wchar_t
digit2) {
37
if
(!
FXSYS_IsDecimalDigit
(
digit1
)
|| !
FXSYS_IsDecimalDigit
(
digit2
)
) {
38
// This could potentially return 0 as a sentinel value. Then this function
39
// can just return wchar_t instead of std::optional<wchar_t>.
40
return
std
::nullopt;
41
}
42
return
static_cast
<
wchar_t
>((digit1 - 48) * 10 + (digit2 - 48) + 130);
43
}
44
45
size_t DetermineConsecutiveDigitCount(
const
WideString
& msg, size_t startpos) {
46
// This is faster in debug builds and helpful for fuzzers.
47
// Access |data| with care.
48
size_t count = 0;
49
const
size_t size = msg.GetLength();
50
const
wchar_t
* data = msg.c_str();
51
52
// SAFETY: performance-sensitive.
53
UNSAFE_BUFFERS
({
54
for
(size_t i = startpos; i < size; ++i) {
55
if
(!FXSYS_IsDecimalDigit(data[i])) {
56
break
;
57
}
58
++count;
59
}
60
});
61
62
return
count;
63
}
64
65
}
// namespace
66
67
CBC_ASCIIEncoder::
CBC_ASCIIEncoder
() =
default
;
68
69
CBC_ASCIIEncoder::~
CBC_ASCIIEncoder
() =
default
;
70
71
CBC_HighLevelEncoder
::
Encoding
CBC_ASCIIEncoder::
GetEncodingMode
() {
72
return
CBC_HighLevelEncoder
::
Encoding
::
ASCII
;
73
}
74
75
bool
CBC_ASCIIEncoder::
Encode
(
CBC_EncoderContext
* context) {
76
size_t n = DetermineConsecutiveDigitCount(context->m_msg, context->m_pos);
77
if
(n >= 2) {
78
std
::optional<
wchar_t
> code = EncodeASCIIDigits(
79
context->m_msg[context->m_pos], context->m_msg[context->m_pos + 1]);
80
if
(!code.has_value())
81
return
false
;
82
83
context
->
writeCodeword
(
code.value()
)
;
84
context->m_pos += 2;
85
return
true
;
86
}
87
88
wchar_t
c = context
->
getCurrentChar
(
)
;
89
CBC_HighLevelEncoder
::
Encoding
newMode = CBC_HighLevelEncoder::LookAheadTest(
90
context->m_msg, context->m_pos,
GetEncodingMode
(
)
);
91
if
(newMode !=
GetEncodingMode
(
)
) {
92
switch
(newMode) {
93
case
CBC_HighLevelEncoder
::
Encoding
::
BASE256
:
94
context
->
writeCodeword
(
CBC_HighLevelEncoder
::
LATCH_TO_BASE256
)
;
95
break
;
96
case
CBC_HighLevelEncoder
::
Encoding
::
C40
:
97
context
->
writeCodeword
(
CBC_HighLevelEncoder
::
LATCH_TO_C40
)
;
98
break
;
99
case
CBC_HighLevelEncoder
::
Encoding
::
X12
:
100
context
->
writeCodeword
(
CBC_HighLevelEncoder
::
LATCH_TO_ANSIX12
)
;
101
break
;
102
case
CBC_HighLevelEncoder
::
Encoding
::
TEXT
:
103
context
->
writeCodeword
(
CBC_HighLevelEncoder
::
LATCH_TO_TEXT
)
;
104
break
;
105
case
CBC_HighLevelEncoder
::
Encoding
::
EDIFACT
:
106
context
->
writeCodeword
(
CBC_HighLevelEncoder
::
LATCH_TO_EDIFACT
)
;
107
break
;
108
default
:
109
return
false
;
110
}
111
context
->
SignalEncoderChange
(
newMode
)
;
112
return
true
;
113
}
114
115
if
(
CBC_HighLevelEncoder
::
IsExtendedASCII
(
c
)
) {
116
context
->
writeCodeword
(
CBC_HighLevelEncoder
::
UPPER_SHIFT
)
;
117
context
->
writeCodeword
(
static_cast
<
wchar_t
>(c - 128 + 1)
)
;
118
}
else
{
119
context
->
writeCodeword
(
static_cast
<
wchar_t
>(c + 1)
)
;
120
}
121
context->m_pos++;
122
return
true
;
123
}
CBC_ASCIIEncoder::Encode
bool Encode(CBC_EncoderContext *context) override
Definition
BC_ASCIIEncoder.cpp:75
CBC_ASCIIEncoder::~CBC_ASCIIEncoder
~CBC_ASCIIEncoder() override
CBC_ASCIIEncoder::GetEncodingMode
CBC_HighLevelEncoder::Encoding GetEncodingMode() override
Definition
BC_ASCIIEncoder.cpp:71
CBC_ASCIIEncoder::CBC_ASCIIEncoder
CBC_ASCIIEncoder()
CBC_EncoderContext
Definition
BC_EncoderContext.h:16
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::SignalEncoderChange
void SignalEncoderChange(CBC_HighLevelEncoder::Encoding encoding)
Definition
BC_EncoderContext.cpp:71
CBC_HighLevelEncoder
Definition
BC_HighLevelEncoder.h:12
CBC_HighLevelEncoder::LATCH_TO_TEXT
static const wchar_t LATCH_TO_TEXT
Definition
BC_HighLevelEncoder.h:40
CBC_HighLevelEncoder::LATCH_TO_ANSIX12
static const wchar_t LATCH_TO_ANSIX12
Definition
BC_HighLevelEncoder.h:39
CBC_HighLevelEncoder::LATCH_TO_BASE256
static const wchar_t LATCH_TO_BASE256
Definition
BC_HighLevelEncoder.h:37
CBC_HighLevelEncoder::IsExtendedASCII
static bool IsExtendedASCII(wchar_t ch)
Definition
BC_HighLevelEncoder.cpp:327
CBC_HighLevelEncoder::LATCH_TO_C40
static const wchar_t LATCH_TO_C40
Definition
BC_HighLevelEncoder.h:36
CBC_HighLevelEncoder::UPPER_SHIFT
static const wchar_t UPPER_SHIFT
Definition
BC_HighLevelEncoder.h:38
CBC_HighLevelEncoder::Encoding
Encoding
Definition
BC_HighLevelEncoder.h:14
CBC_HighLevelEncoder::Encoding::BASE256
@ BASE256
Definition
BC_HighLevelEncoder.h:21
CBC_HighLevelEncoder::Encoding::TEXT
@ TEXT
Definition
BC_HighLevelEncoder.h:18
CBC_HighLevelEncoder::Encoding::C40
@ C40
Definition
BC_HighLevelEncoder.h:17
CBC_HighLevelEncoder::Encoding::X12
@ X12
Definition
BC_HighLevelEncoder.h:19
CBC_HighLevelEncoder::Encoding::EDIFACT
@ EDIFACT
Definition
BC_HighLevelEncoder.h:20
CBC_HighLevelEncoder::Encoding::ASCII
@ ASCII
Definition
BC_HighLevelEncoder.h:16
CBC_HighLevelEncoder::LATCH_TO_EDIFACT
static const wchar_t LATCH_TO_EDIFACT
Definition
BC_HighLevelEncoder.h:41
UNSAFE_BUFFERS
#define UNSAFE_BUFFERS(...)
Definition
compiler_specific.h:95
FXSYS_IsDecimalDigit
bool FXSYS_IsDecimalDigit(wchar_t c)
Definition
fx_extension.h:105
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
WideString
fxcrt::WideString WideString
Definition
widestring.h:207
qtwebengine
src
3rdparty
chromium
third_party
pdfium
fxbarcode
datamatrix
BC_ASCIIEncoder.cpp
Generated on
for Qt by
1.14.0