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_form.h"
8
9 #include <algorithm>
10
11 #include "core/fpdfapi/page/cpdf_contentparser.h"
12 #include "core/fpdfapi/page/cpdf_imageobject.h"
13 #include "core/fpdfapi/page/cpdf_pageobject.h"
14 #include "core/fpdfapi/page/cpdf_pageobjectholder.h"
15 #include "core/fpdfapi/parser/cpdf_dictionary.h"
16 #include "core/fpdfapi/parser/cpdf_stream.h"
17 #include "core/fxge/dib/cfx_dibitmap.h"
18 #include "third_party/base/ptr_util.h"
19
20 // static
ChooseResourcesDict(CPDF_Dictionary * pResources,CPDF_Dictionary * pParentResources,CPDF_Dictionary * pPageResources)21 CPDF_Dictionary* CPDF_Form::ChooseResourcesDict(
22 CPDF_Dictionary* pResources,
23 CPDF_Dictionary* pParentResources,
24 CPDF_Dictionary* pPageResources) {
25 if (pResources)
26 return pResources;
27 return pParentResources ? pParentResources : pPageResources;
28 }
29
CPDF_Form(CPDF_Document * pDoc,CPDF_Dictionary * pPageResources,CPDF_Stream * pFormStream)30 CPDF_Form::CPDF_Form(CPDF_Document* pDoc,
31 CPDF_Dictionary* pPageResources,
32 CPDF_Stream* pFormStream)
33 : CPDF_Form(pDoc, pPageResources, pFormStream, nullptr) {}
34
CPDF_Form(CPDF_Document * pDoc,CPDF_Dictionary * pPageResources,CPDF_Stream * pFormStream,CPDF_Dictionary * pParentResources)35 CPDF_Form::CPDF_Form(CPDF_Document* pDoc,
36 CPDF_Dictionary* pPageResources,
37 CPDF_Stream* pFormStream,
38 CPDF_Dictionary* pParentResources)
39 : CPDF_PageObjectHolder(
40 pDoc,
41 pFormStream->GetDict(),
42 pPageResources,
43 ChooseResourcesDict(pFormStream->GetDict()->GetDictFor("Resources"),
44 pParentResources,
45 pPageResources)),
46 m_pFormStream(pFormStream) {
47 LoadTransparencyInfo();
48 }
49
50 CPDF_Form::~CPDF_Form() = default;
51
ParseContent()52 void CPDF_Form::ParseContent() {
53 ParseContentInternal(nullptr, nullptr, nullptr, nullptr);
54 }
55
ParseContent(const CPDF_AllStates * pGraphicStates,const CFX_Matrix * pParentMatrix,std::set<const uint8_t * > * pParsedSet)56 void CPDF_Form::ParseContent(const CPDF_AllStates* pGraphicStates,
57 const CFX_Matrix* pParentMatrix,
58 std::set<const uint8_t*>* pParsedSet) {
59 ParseContentInternal(pGraphicStates, pParentMatrix, nullptr, pParsedSet);
60 }
61
ParseContentForType3Char(CPDF_Type3Char * pType3Char)62 void CPDF_Form::ParseContentForType3Char(CPDF_Type3Char* pType3Char) {
63 ParseContentInternal(nullptr, nullptr, pType3Char, nullptr);
64 }
65
ParseContentInternal(const CPDF_AllStates * pGraphicStates,const CFX_Matrix * pParentMatrix,CPDF_Type3Char * pType3Char,std::set<const uint8_t * > * pParsedSet)66 void CPDF_Form::ParseContentInternal(const CPDF_AllStates* pGraphicStates,
67 const CFX_Matrix* pParentMatrix,
68 CPDF_Type3Char* pType3Char,
69 std::set<const uint8_t*>* pParsedSet) {
70 if (GetParseState() == ParseState::kParsed)
71 return;
72
73 if (GetParseState() == ParseState::kNotParsed) {
74 if (!pParsedSet) {
75 if (!m_ParsedSet)
76 m_ParsedSet = pdfium::MakeUnique<std::set<const uint8_t*>>();
77 pParsedSet = m_ParsedSet.get();
78 }
79 StartParse(pdfium::MakeUnique<CPDF_ContentParser>(
80 this, pGraphicStates, pParentMatrix, pType3Char, pParsedSet));
81 }
82
83 ASSERT(GetParseState() == ParseState::kParsing);
84 ContinueParse(nullptr);
85 }
86
HasPageObjects() const87 bool CPDF_Form::HasPageObjects() const {
88 return GetPageObjectCount() != 0;
89 }
90
CalcBoundingBox() const91 CFX_FloatRect CPDF_Form::CalcBoundingBox() const {
92 if (GetPageObjectCount() == 0)
93 return CFX_FloatRect();
94
95 float left = 1000000.0f;
96 float right = -1000000.0f;
97 float bottom = 1000000.0f;
98 float top = -1000000.0f;
99 for (const auto& pObj : *this) {
100 const auto& rect = pObj->GetRect();
101 left = std::min(left, rect.left);
102 right = std::max(right, rect.right);
103 bottom = std::min(bottom, rect.bottom);
104 top = std::max(top, rect.top);
105 }
106 return CFX_FloatRect(left, bottom, right, top);
107 }
108
GetStream() const109 const CPDF_Stream* CPDF_Form::GetStream() const {
110 return m_pFormStream.Get();
111 }
112
113 Optional<std::pair<RetainPtr<CFX_DIBitmap>, CFX_Matrix>>
GetBitmapAndMatrixFromSoleImageOfForm() const114 CPDF_Form::GetBitmapAndMatrixFromSoleImageOfForm() const {
115 if (GetPageObjectCount() != 1)
116 return {};
117
118 CPDF_ImageObject* pImageObject = (*begin())->AsImage();
119 if (!pImageObject)
120 return {};
121
122 return {{pImageObject->GetIndependentBitmap(), pImageObject->matrix()}};
123 }
124