1 // Copyright 2016 PDFium Authors. All rights reserved.
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/fpdfapi/page/cpdf_shadingpattern.h"
8
9 #include <algorithm>
10
11 #include "core/fpdfapi/page/cpdf_docpagedata.h"
12 #include "core/fpdfapi/page/cpdf_function.h"
13 #include "core/fpdfapi/parser/cpdf_array.h"
14 #include "core/fpdfapi/parser/cpdf_dictionary.h"
15 #include "core/fpdfapi/parser/cpdf_document.h"
16 #include "core/fpdfapi/parser/cpdf_object.h"
17 #include "core/fpdfapi/parser/cpdf_stream.h"
18 #include "core/fxcrt/fx_safe_types.h"
19
20 namespace {
21
ToShadingType(int type)22 ShadingType ToShadingType(int type) {
23 return (type > kInvalidShading && type < kMaxShading)
24 ? static_cast<ShadingType>(type)
25 : kInvalidShading;
26 }
27
28 } // namespace
29
CPDF_ShadingPattern(CPDF_Document * pDoc,CPDF_Object * pPatternObj,bool bShading,const CFX_Matrix & parentMatrix)30 CPDF_ShadingPattern::CPDF_ShadingPattern(CPDF_Document* pDoc,
31 CPDF_Object* pPatternObj,
32 bool bShading,
33 const CFX_Matrix& parentMatrix)
34 : CPDF_Pattern(pDoc, pPatternObj, parentMatrix), m_bShading(bShading) {
35 ASSERT(document());
36 if (!bShading)
37 SetPatternToFormMatrix();
38 }
39
40 CPDF_ShadingPattern::~CPDF_ShadingPattern() = default;
41
AsShadingPattern()42 CPDF_ShadingPattern* CPDF_ShadingPattern::AsShadingPattern() {
43 return this;
44 }
45
Load()46 bool CPDF_ShadingPattern::Load() {
47 if (m_ShadingType != kInvalidShading)
48 return true;
49
50 const CPDF_Object* pShadingObj = GetShadingObject();
51 const CPDF_Dictionary* pShadingDict =
52 pShadingObj ? pShadingObj->GetDict() : nullptr;
53 if (!pShadingDict)
54 return false;
55
56 m_pFunctions.clear();
57 const CPDF_Object* pFunc = pShadingDict->GetDirectObjectFor("Function");
58 if (pFunc) {
59 if (const CPDF_Array* pArray = pFunc->AsArray()) {
60 m_pFunctions.resize(std::min<size_t>(pArray->size(), 4));
61 for (size_t i = 0; i < m_pFunctions.size(); ++i)
62 m_pFunctions[i] = CPDF_Function::Load(pArray->GetDirectObjectAt(i));
63 } else {
64 m_pFunctions.push_back(CPDF_Function::Load(pFunc));
65 }
66 }
67 const CPDF_Object* pCSObj = pShadingDict->GetDirectObjectFor("ColorSpace");
68 if (!pCSObj)
69 return false;
70
71 auto* pDocPageData = CPDF_DocPageData::FromDocument(document());
72 m_pCS = pDocPageData->GetColorSpace(pCSObj, nullptr);
73
74 // The color space is required and cannot be a Pattern space, according to the
75 // PDF 1.7 spec, page 305.
76 if (!m_pCS || m_pCS->GetFamily() == PDFCS_PATTERN)
77 return false;
78
79 m_ShadingType = ToShadingType(pShadingDict->GetIntegerFor("ShadingType"));
80 return Validate();
81 }
82
GetShadingObject() const83 const CPDF_Object* CPDF_ShadingPattern::GetShadingObject() const {
84 return m_bShading ? pattern_obj()
85 : pattern_obj()->GetDict()->GetDirectObjectFor("Shading");
86 }
87
Validate() const88 bool CPDF_ShadingPattern::Validate() const {
89 if (m_ShadingType == kInvalidShading)
90 return false;
91
92 // We expect to have a stream if our shading type is a mesh.
93 if (IsMeshShading() && !ToStream(GetShadingObject()))
94 return false;
95
96 // Validate color space
97 switch (m_ShadingType) {
98 case kFunctionBasedShading:
99 case kAxialShading:
100 case kRadialShading: {
101 if (m_pCS->GetFamily() == PDFCS_INDEXED)
102 return false;
103 break;
104 }
105 case kFreeFormGouraudTriangleMeshShading:
106 case kLatticeFormGouraudTriangleMeshShading:
107 case kCoonsPatchMeshShading:
108 case kTensorProductPatchMeshShading: {
109 if (!m_pFunctions.empty() && m_pCS->GetFamily() == PDFCS_INDEXED)
110 return false;
111 break;
112 }
113 default: {
114 NOTREACHED();
115 return false;
116 }
117 }
118
119 uint32_t nNumColorSpaceComponents = m_pCS->CountComponents();
120 switch (m_ShadingType) {
121 case kFunctionBasedShading: {
122 // Either one 2-to-N function or N 2-to-1 functions.
123 return ValidateFunctions(1, 2, nNumColorSpaceComponents) ||
124 ValidateFunctions(nNumColorSpaceComponents, 2, 1);
125 }
126 case kAxialShading:
127 case kRadialShading: {
128 // Either one 1-to-N function or N 1-to-1 functions.
129 return ValidateFunctions(1, 1, nNumColorSpaceComponents) ||
130 ValidateFunctions(nNumColorSpaceComponents, 1, 1);
131 }
132 case kFreeFormGouraudTriangleMeshShading:
133 case kLatticeFormGouraudTriangleMeshShading:
134 case kCoonsPatchMeshShading:
135 case kTensorProductPatchMeshShading: {
136 // Either no function, one 1-to-N function, or N 1-to-1 functions.
137 return m_pFunctions.empty() ||
138 ValidateFunctions(1, 1, nNumColorSpaceComponents) ||
139 ValidateFunctions(nNumColorSpaceComponents, 1, 1);
140 }
141 default:
142 break;
143 }
144 NOTREACHED();
145 return false;
146 }
147
ValidateFunctions(uint32_t nExpectedNumFunctions,uint32_t nExpectedNumInputs,uint32_t nExpectedNumOutputs) const148 bool CPDF_ShadingPattern::ValidateFunctions(
149 uint32_t nExpectedNumFunctions,
150 uint32_t nExpectedNumInputs,
151 uint32_t nExpectedNumOutputs) const {
152 if (m_pFunctions.size() != nExpectedNumFunctions)
153 return false;
154
155 FX_SAFE_UINT32 nTotalOutputs = 0;
156 for (const auto& function : m_pFunctions) {
157 if (!function)
158 return false;
159
160 if (function->CountInputs() != nExpectedNumInputs ||
161 function->CountOutputs() != nExpectedNumOutputs) {
162 return false;
163 }
164
165 nTotalOutputs += function->CountOutputs();
166 }
167
168 return nTotalOutputs.IsValid();
169 }
170