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
cpdf_defaultappearance.cpp
Go to the documentation of this file.
1
// Copyright 2016 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
7
#
include
"core/fpdfdoc/cpdf_defaultappearance.h"
8
9
#
include
<
algorithm
>
10
#
include
<
vector
>
11
12
#
include
"core/fpdfapi/parser/cpdf_simple_parser.h"
13
#
include
"core/fpdfapi/parser/fpdf_parser_utility.h"
14
#
include
"core/fxcrt/fx_string.h"
15
#
include
"core/fxcrt/notreached.h"
16
#
include
"core/fxge/cfx_color.h"
17
18
namespace
{
19
20
// Find the token and its |nParams| parameters from the start of data,
21
// and move the current position to the start of those parameters.
22
bool
FindTagParamFromStart(
CPDF_SimpleParser
* parser,
23
ByteStringView
token,
24
int
nParams) {
25
nParams++;
26
27
std::vector<uint32_t> pBuf(nParams);
28
int
buf_index = 0;
29
int
buf_count = 0;
30
31
parser
->
SetCurrentPosition
(
0
)
;
32
while
(
true
) {
33
pBuf[buf_index++] = parser
->
GetCurrentPosition
(
)
;
34
if
(buf_index == nParams)
35
buf_index = 0;
36
37
buf_count++;
38
if
(buf_count > nParams)
39
buf_count = nParams;
40
41
ByteStringView
word = parser->GetWord();
42
if
(word.IsEmpty())
43
return
false
;
44
45
if
(word == token) {
46
if
(buf_count < nParams)
47
continue
;
48
49
parser
->
SetCurrentPosition
(
pBuf[buf_index]
)
;
50
return
true
;
51
}
52
}
53
}
54
55
}
// namespace
56
57
CPDF_DefaultAppearance
::
CPDF_DefaultAppearance
(
const
ByteString
& csDA)
58
:
m_csDA
(
csDA
) {}
59
60
CPDF_DefaultAppearance
::
CPDF_DefaultAppearance
(
61
const
CPDF_DefaultAppearance
& cDA) =
default
;
62
63
CPDF_DefaultAppearance
::~
CPDF_DefaultAppearance
() =
default
;
64
65
std
::optional<
ByteString
>
CPDF_DefaultAppearance
::
GetFont
(
66
float
* fFontSize)
const
{
67
*fFontSize = 0.0f;
68
if
(m_csDA.IsEmpty())
69
return
std
::nullopt;
70
71
ByteString
csFontNameTag;
72
CPDF_SimpleParser
syntax(m_csDA.AsStringView().unsigned_span());
73
if
(FindTagParamFromStart(&syntax,
"Tf"
, 2)) {
74
csFontNameTag
=
ByteString
(syntax
.
GetWord
());
75
csFontNameTag.Delete(0, 1);
76
*fFontSize = StringToFloat(syntax
.
GetWord
());
77
}
78
return
PDF_NameDecode(csFontNameTag.AsStringView());
79
}
80
81
std
::optional<
CFX_Color
>
CPDF_DefaultAppearance
::
GetColor
()
const
{
82
if
(m_csDA.IsEmpty())
83
return
std
::nullopt;
84
85
CPDF_SimpleParser
syntax(m_csDA.AsStringView().unsigned_span());
86
if
(FindTagParamFromStart(&syntax,
"g"
, 1)) {
87
float
gray = StringToFloat(syntax
.
GetWord
());
88
return
CFX_Color
(
CFX_Color
::
Type
::
kGray
,
gray
)
;
89
}
90
if
(FindTagParamFromStart(&syntax,
"rg"
, 3)) {
91
float
r = StringToFloat(syntax
.
GetWord
());
92
float
g = StringToFloat(syntax
.
GetWord
());
93
float
b = StringToFloat(syntax
.
GetWord
());
94
return
CFX_Color
(
CFX_Color
::
Type
::
kRGB
,
r
,
g
,
b
)
;
95
}
96
if
(FindTagParamFromStart(&syntax,
"k"
, 4)) {
97
float
c = StringToFloat(syntax
.
GetWord
());
98
float
m = StringToFloat(syntax
.
GetWord
());
99
float
y = StringToFloat(syntax
.
GetWord
());
100
float
k = StringToFloat(syntax
.
GetWord
());
101
return
CFX_Color
(
CFX_Color
::
Type
::
kCMYK
,
c
,
m
,
y
,
k
)
;
102
}
103
return
std
::nullopt;
104
}
105
106
std
::optional<
CFX_Color
::
TypeAndARGB
>
CPDF_DefaultAppearance
::
GetColorARGB
()
107
const
{
108
std
::optional<
CFX_Color
> maybe_color = GetColor();
109
if
(!maybe_color.has_value())
110
return
std
::nullopt;
111
112
const
CFX_Color
& color = maybe_color.value();
113
if
(color
.
nColorType
==
CFX_Color
::
Type
::
kGray
) {
114
int
g =
static_cast
<
int
>(color
.
fColor1
* 255 + 0.5f);
115
return
CFX_Color
::
TypeAndARGB
(
CFX_Color
::
Type
::
kGray
,
116
ArgbEncode
(
255
,
g
,
g
,
g
)
)
;
117
}
118
if
(color
.
nColorType
==
CFX_Color
::
Type
::
kRGB
) {
119
int
r =
static_cast
<
int
>(color
.
fColor1
* 255 + 0.5f);
120
int
g =
static_cast
<
int
>(color
.
fColor2
* 255 + 0.5f);
121
int
b =
static_cast
<
int
>(color
.
fColor3
* 255 + 0.5f);
122
return
CFX_Color
::
TypeAndARGB
(
CFX_Color
::
Type
::
kRGB
,
123
ArgbEncode
(
255
,
r
,
g
,
b
)
)
;
124
}
125
if
(color
.
nColorType
==
CFX_Color
::
Type
::
kCMYK
) {
126
float
r = 1.0f -
std
::min(1.0f, color
.
fColor1
+ color
.
fColor4
);
127
float
g = 1.0f -
std
::min(1.0f, color
.
fColor2
+ color
.
fColor4
);
128
float
b = 1.0f -
std
::min(1.0f, color
.
fColor3
+ color
.
fColor4
);
129
return
CFX_Color
::
TypeAndARGB
(
130
CFX_Color
::
Type
::
kCMYK
,
131
ArgbEncode
(
255
,
static_cast
<
int
>(r * 255 + 0.5f)
,
132
static_cast
<
int
>(g * 255 + 0.5f)
,
133
static_cast
<
int
>(b * 255 + 0.5f)
)
)
;
134
}
135
NOTREACHED_NORETURN
();
136
}
137
138
// static
139
bool
CPDF_DefaultAppearance
::
FindTagParamFromStartForTesting
(
140
CPDF_SimpleParser
* parser,
141
ByteStringView
token,
142
int
nParams) {
143
return
FindTagParamFromStart(parser, token, nParams);
144
}
ByteString
fxcrt::ByteString ByteString
Definition
bytestring.h:180
CPDF_DefaultAppearance
Definition
cpdf_defaultappearance.h:17
CPDF_DefaultAppearance::GetFont
std::optional< ByteString > GetFont(float *fFontSize) const
Definition
cpdf_defaultappearance.cpp:65
CPDF_DefaultAppearance::~CPDF_DefaultAppearance
~CPDF_DefaultAppearance()
CPDF_DefaultAppearance::CPDF_DefaultAppearance
CPDF_DefaultAppearance(const ByteString &csDA)
Definition
cpdf_defaultappearance.cpp:57
CPDF_DefaultAppearance::FindTagParamFromStartForTesting
static bool FindTagParamFromStartForTesting(CPDF_SimpleParser *parser, ByteStringView token, int nParams)
Definition
cpdf_defaultappearance.cpp:139
CPDF_DefaultAppearance::GetColor
std::optional< CFX_Color > GetColor() const
Definition
cpdf_defaultappearance.cpp:81
CPDF_DefaultAppearance::GetColorARGB
std::optional< CFX_Color::TypeAndARGB > GetColorARGB() const
Definition
cpdf_defaultappearance.cpp:106
CPDF_DefaultAppearance::CPDF_DefaultAppearance
CPDF_DefaultAppearance(const CPDF_DefaultAppearance &cDA)
CPDF_SimpleParser
Definition
cpdf_simple_parser.h:17
CPDF_SimpleParser::GetCurrentPosition
uint32_t GetCurrentPosition() const
Definition
cpdf_simple_parser.h:25
CPDF_SimpleParser::SetCurrentPosition
void SetCurrentPosition(uint32_t position)
Definition
cpdf_simple_parser.h:24
CPDF_SimpleParser::GetWord
ByteStringView GetWord()
Definition
cpdf_simple_parser.cpp:21
fxcrt::ByteString::operator=
ByteString & operator=(ByteString &&that) noexcept
Definition
bytestring.cpp:175
ArgbEncode
constexpr FX_ARGB ArgbEncode(uint32_t a, uint32_t r, uint32_t g, uint32_t b)
Definition
fx_dib.h:188
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
NOTREACHED_NORETURN
#define NOTREACHED_NORETURN()
Definition
notreached.h:22
ByteStringView
fxcrt::ByteStringView ByteStringView
Definition
string_view_template.h:325
CFX_Color::TypeAndARGB
Definition
cfx_color.h:16
CFX_Color::TypeAndARGB::TypeAndARGB
TypeAndARGB(CFX_Color::Type type_in, FX_ARGB argb_in)
Definition
cfx_color.h:17
CFX_Color
Definition
cfx_color.h:12
CFX_Color::fColor4
float fColor4
Definition
cfx_color.h:58
CFX_Color::fColor3
float fColor3
Definition
cfx_color.h:57
CFX_Color::Type
Type
Definition
cfx_color.h:14
CFX_Color::Type::kCMYK
@ kCMYK
Definition
cfx_color.h:14
CFX_Color::Type::kGray
@ kGray
Definition
cfx_color.h:14
CFX_Color::Type::kRGB
@ kRGB
Definition
cfx_color.h:14
CFX_Color::nColorType
Type nColorType
Definition
cfx_color.h:54
CFX_Color::CFX_Color
constexpr CFX_Color(Type type=CFX_Color::Type::kTransparent, float color1=0.0f, float color2=0.0f, float color3=0.0f, float color4=0.0f)
Definition
cfx_color.h:27
CFX_Color::fColor2
float fColor2
Definition
cfx_color.h:56
CFX_Color::fColor1
float fColor1
Definition
cfx_color.h:55
qtwebengine
src
3rdparty
chromium
third_party
pdfium
core
fpdfdoc
cpdf_defaultappearance.cpp
Generated on
for Qt by
1.18.0