OLD | NEW |
| (Empty) |
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/fpdf_page/cpdf_shadingpattern.h" | |
8 | |
9 #include "core/fpdfapi/fpdf_page/pageint.h" | |
10 #include "core/fpdfapi/fpdf_parser/cpdf_array.h" | |
11 #include "core/fpdfapi/fpdf_parser/cpdf_dictionary.h" | |
12 #include "core/fpdfapi/fpdf_parser/cpdf_document.h" | |
13 #include "core/fpdfapi/fpdf_parser/cpdf_object.h" | |
14 | |
15 namespace { | |
16 | |
17 ShadingType ToShadingType(int type) { | |
18 return (type > static_cast<int>(kInvalidShading) && | |
19 type < static_cast<int>(kMaxShading)) | |
20 ? static_cast<ShadingType>(type) | |
21 : kInvalidShading; | |
22 } | |
23 | |
24 } // namespace | |
25 | |
26 CPDF_ShadingPattern::CPDF_ShadingPattern(CPDF_Document* pDoc, | |
27 CPDF_Object* pPatternObj, | |
28 FX_BOOL bShading, | |
29 const CFX_Matrix& parentMatrix) | |
30 : CPDF_Pattern(SHADING, | |
31 pDoc, | |
32 bShading ? nullptr : pPatternObj, | |
33 parentMatrix), | |
34 m_ShadingType(kInvalidShading), | |
35 m_bShadingObj(bShading), | |
36 m_pShadingObj(pPatternObj), | |
37 m_pCS(nullptr), | |
38 m_pCountedCS(nullptr) { | |
39 if (!bShading) { | |
40 CPDF_Dictionary* pDict = m_pPatternObj->GetDict(); | |
41 m_Pattern2Form = pDict->GetMatrixFor("Matrix"); | |
42 m_pShadingObj = pDict->GetDirectObjectFor("Shading"); | |
43 m_Pattern2Form.Concat(parentMatrix); | |
44 } | |
45 } | |
46 | |
47 CPDF_ShadingPattern::~CPDF_ShadingPattern() { | |
48 CPDF_ColorSpace* pCS = m_pCountedCS ? m_pCountedCS->get() : nullptr; | |
49 if (pCS && m_pDocument) | |
50 m_pDocument->GetPageData()->ReleaseColorSpace(pCS->GetArray()); | |
51 } | |
52 | |
53 CPDF_TilingPattern* CPDF_ShadingPattern::AsTilingPattern() { | |
54 return nullptr; | |
55 } | |
56 | |
57 CPDF_ShadingPattern* CPDF_ShadingPattern::AsShadingPattern() { | |
58 return this; | |
59 } | |
60 | |
61 bool CPDF_ShadingPattern::Load() { | |
62 if (m_ShadingType != kInvalidShading) | |
63 return TRUE; | |
64 | |
65 CPDF_Dictionary* pShadingDict = | |
66 m_pShadingObj ? m_pShadingObj->GetDict() : nullptr; | |
67 if (!pShadingDict) | |
68 return FALSE; | |
69 | |
70 m_pFunctions.clear(); | |
71 CPDF_Object* pFunc = pShadingDict->GetDirectObjectFor("Function"); | |
72 if (pFunc) { | |
73 if (CPDF_Array* pArray = pFunc->AsArray()) { | |
74 m_pFunctions.resize(std::min<size_t>(pArray->GetCount(), 4)); | |
75 for (size_t i = 0; i < m_pFunctions.size(); ++i) | |
76 m_pFunctions[i] = CPDF_Function::Load(pArray->GetDirectObjectAt(i)); | |
77 } else { | |
78 m_pFunctions.push_back(CPDF_Function::Load(pFunc)); | |
79 } | |
80 } | |
81 CPDF_Object* pCSObj = pShadingDict->GetDirectObjectFor("ColorSpace"); | |
82 if (!pCSObj) | |
83 return FALSE; | |
84 | |
85 CPDF_DocPageData* pDocPageData = m_pDocument->GetPageData(); | |
86 m_pCS = pDocPageData->GetColorSpace(pCSObj, nullptr); | |
87 if (m_pCS) | |
88 m_pCountedCS = pDocPageData->FindColorSpacePtr(m_pCS->GetArray()); | |
89 | |
90 m_ShadingType = ToShadingType(pShadingDict->GetIntegerFor("ShadingType")); | |
91 | |
92 // We expect to have a stream if our shading type is a mesh. | |
93 if (IsMeshShading() && !ToStream(m_pShadingObj)) | |
94 return FALSE; | |
95 | |
96 return TRUE; | |
97 } | |
OLD | NEW |