1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
// Copyright 2016 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "core/fpdfapi/page/cpdf_shadingpattern.h"
#include <algorithm>
#include "core/fpdfapi/page/cpdf_docpagedata.h"
#include "core/fpdfapi/page/cpdf_function.h"
#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fpdfapi/parser/cpdf_dictionary.h"
#include "core/fpdfapi/parser/cpdf_document.h"
#include "core/fpdfapi/parser/cpdf_object.h"
#include "core/fpdfapi/parser/cpdf_stream.h"
namespace {
ShadingType ToShadingType(int type) {
return (type > static_cast<int>(kInvalidShading) &&
type < static_cast<int>(kMaxShading))
? static_cast<ShadingType>(type)
: kInvalidShading;
}
} // namespace
CPDF_ShadingPattern::CPDF_ShadingPattern(CPDF_Document* pDoc,
CPDF_Object* pPatternObj,
bool bShading,
const CFX_Matrix& parentMatrix)
: CPDF_Pattern(pDoc, bShading ? nullptr : pPatternObj, parentMatrix),
m_ShadingType(kInvalidShading),
m_bShadingObj(bShading),
m_pShadingObj(pPatternObj),
m_pCS(nullptr),
m_pCountedCS(nullptr) {
assert(document());
if (!bShading) {
m_pShadingObj = pattern_obj()->GetDict()->GetDirectObjectFor("Shading");
SetPatternToFormMatrix();
}
}
CPDF_ShadingPattern::~CPDF_ShadingPattern() {
CPDF_ColorSpace* pCountedCS = m_pCountedCS ? m_pCountedCS->get() : nullptr;
if (pCountedCS) {
auto* pPageData = document()->GetPageData();
if (pPageData) {
m_pCS.Release(); // Give up unowned reference first.
pPageData->ReleaseColorSpace(pCountedCS->GetArray());
}
}
}
CPDF_TilingPattern* CPDF_ShadingPattern::AsTilingPattern() {
return nullptr;
}
CPDF_ShadingPattern* CPDF_ShadingPattern::AsShadingPattern() {
return this;
}
bool CPDF_ShadingPattern::Load() {
if (m_ShadingType != kInvalidShading)
return true;
CPDF_Dictionary* pShadingDict =
m_pShadingObj ? m_pShadingObj->GetDict() : nullptr;
if (!pShadingDict)
return false;
m_pFunctions.clear();
CPDF_Object* pFunc = pShadingDict->GetDirectObjectFor("Function");
if (pFunc) {
if (CPDF_Array* pArray = pFunc->AsArray()) {
m_pFunctions.resize(std::min<size_t>(pArray->GetCount(), 4));
for (size_t i = 0; i < m_pFunctions.size(); ++i)
m_pFunctions[i] = CPDF_Function::Load(pArray->GetDirectObjectAt(i));
} else {
m_pFunctions.push_back(CPDF_Function::Load(pFunc));
}
}
CPDF_Object* pCSObj = pShadingDict->GetDirectObjectFor("ColorSpace");
if (!pCSObj)
return false;
CPDF_DocPageData* pDocPageData = document()->GetPageData();
m_pCS = pDocPageData->GetColorSpace(pCSObj, nullptr);
if (m_pCS) {
// The color space cannot be a Pattern space, according to the PDF 1.7 spec,
// page 305.
if (m_pCS->GetFamily() == PDFCS_PATTERN)
return false;
m_pCountedCS = pDocPageData->FindColorSpacePtr(m_pCS->GetArray());
}
m_ShadingType = ToShadingType(pShadingDict->GetIntegerFor("ShadingType"));
return Validate();
}
bool CPDF_ShadingPattern::Validate() const {
if (m_ShadingType == kInvalidShading)
return false;
// We expect to have a stream if our shading type is a mesh.
if (IsMeshShading() && !ToStream(m_pShadingObj.Get()))
return false;
// Validate color space
switch (m_ShadingType) {
case kFunctionBasedShading:
case kAxialShading:
case kRadialShading: {
if (m_pCS->GetFamily() == PDFCS_INDEXED)
return false;
break;
}
case kFreeFormGouraudTriangleMeshShading:
case kLatticeFormGouraudTriangleMeshShading:
case kCoonsPatchMeshShading:
case kTensorProductPatchMeshShading: {
if (!m_pFunctions.empty() && m_pCS->GetFamily() == PDFCS_INDEXED)
return false;
break;
}
default: {
NOTREACHED();
return false;
}
}
uint32_t nNumColorSpaceComponents = m_pCS->CountComponents();
switch (m_ShadingType) {
case kFunctionBasedShading: {
// Either one 2-to-N function or N 2-to-1 functions.
if (!ValidateFunctions(1, 2, nNumColorSpaceComponents) &&
!ValidateFunctions(nNumColorSpaceComponents, 2, 1)) {
return false;
}
break;
}
case kAxialShading:
case kRadialShading: {
// Either one 1-to-N function or N 1-to-1 functions.
if (!ValidateFunctions(1, 1, nNumColorSpaceComponents) &&
!ValidateFunctions(nNumColorSpaceComponents, 1, 1)) {
return false;
}
break;
}
case kFreeFormGouraudTriangleMeshShading:
case kLatticeFormGouraudTriangleMeshShading:
case kCoonsPatchMeshShading:
case kTensorProductPatchMeshShading: {
// Either no function, one 1-to-N function, or N 1-to-1 functions.
if (!m_pFunctions.empty() &&
!ValidateFunctions(1, 1, nNumColorSpaceComponents) &&
!ValidateFunctions(nNumColorSpaceComponents, 1, 1)) {
return false;
}
break;
}
default: {
NOTREACHED();
return false;
}
}
return true;
}
bool CPDF_ShadingPattern::ValidateFunctions(
uint32_t nExpectedNumFunctions,
uint32_t nExpectedNumInputs,
uint32_t nExpectedNumOutputs) const {
if (m_pFunctions.size() != nExpectedNumFunctions)
return false;
pdfium::base::CheckedNumeric<uint32_t> nTotalOutputs = 0;
for (const auto& function : m_pFunctions) {
if (!function)
return false;
if (function->CountInputs() != nExpectedNumInputs ||
function->CountOutputs() != nExpectedNumOutputs) {
return false;
}
nTotalOutputs += function->CountOutputs();
}
return nTotalOutputs.IsValid();
}
|