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 #ifndef CORE_FPDFAPI_PAGE_CPDF_PAGEOBJECTHOLDER_H_
8 #define CORE_FPDFAPI_PAGE_CPDF_PAGEOBJECTHOLDER_H_
9 
10 #include <deque>
11 #include <map>
12 #include <memory>
13 #include <set>
14 #include <vector>
15 
16 #include "core/fpdfapi/page/cpdf_transparency.h"
17 #include "core/fxcrt/fx_coordinates.h"
18 #include "core/fxcrt/fx_string.h"
19 #include "core/fxcrt/fx_system.h"
20 #include "core/fxcrt/unowned_ptr.h"
21 #include "core/fxge/fx_dib.h"
22 
23 class CPDF_ContentParser;
24 class CPDF_Dictionary;
25 class CPDF_Document;
26 class CPDF_PageObject;
27 class CPDF_Stream;
28 class PauseIndicatorIface;
29 
30 // These structs are used to keep track of resources that have already been
31 // generated in the page object holder.
32 struct GraphicsData {
33   float fillAlpha;
34   float strokeAlpha;
35   BlendMode blendType;
36 
37   bool operator<(const GraphicsData& other) const;
38 };
39 
40 struct FontData {
41   ByteString baseFont;
42   ByteString type;
43 
44   bool operator<(const FontData& other) const;
45 };
46 
47 class CPDF_PageObjectHolder {
48  public:
49   enum class ParseState : uint8_t { kNotParsed, kParsing, kParsed };
50 
51   using iterator = std::deque<std::unique_ptr<CPDF_PageObject>>::iterator;
52   using const_iterator =
53       std::deque<std::unique_ptr<CPDF_PageObject>>::const_iterator;
54 
55   CPDF_PageObjectHolder(CPDF_Document* pDoc,
56                         CPDF_Dictionary* pDict,
57                         CPDF_Dictionary* pPageResources,
58                         CPDF_Dictionary* pResources);
59   virtual ~CPDF_PageObjectHolder();
60 
61   virtual bool IsPage() const;
62 
63   void StartParse(std::unique_ptr<CPDF_ContentParser> pParser);
64   void ContinueParse(PauseIndicatorIface* pPause);
GetParseState()65   ParseState GetParseState() const { return m_ParseState; }
66 
GetDocument()67   CPDF_Document* GetDocument() const { return m_pDocument.Get(); }
68 
GetDict()69   CPDF_Dictionary* GetDict() const { return m_pDict.Get(); }
GetPageObjectCount()70   size_t GetPageObjectCount() const { return m_PageObjectList.size(); }
71   CPDF_PageObject* GetPageObjectByIndex(size_t index) const;
72   void AppendPageObject(std::unique_ptr<CPDF_PageObject> pPageObj);
73   bool RemovePageObject(CPDF_PageObject* pPageObj);
74   bool ErasePageObjectAtIndex(size_t index);
75 
begin()76   iterator begin() { return m_PageObjectList.begin(); }
begin()77   const_iterator begin() const { return m_PageObjectList.begin(); }
78 
end()79   iterator end() { return m_PageObjectList.end(); }
end()80   const_iterator end() const { return m_PageObjectList.end(); }
81 
GetLastCTM()82   const CFX_Matrix& GetLastCTM() const { return m_LastCTM; }
GetBBox()83   const CFX_FloatRect& GetBBox() const { return m_BBox; }
84 
GetTransparency()85   const CPDF_Transparency& GetTransparency() const { return m_Transparency; }
BackgroundAlphaNeeded()86   bool BackgroundAlphaNeeded() const { return m_bBackgroundAlphaNeeded; }
SetBackgroundAlphaNeeded(bool needed)87   void SetBackgroundAlphaNeeded(bool needed) {
88     m_bBackgroundAlphaNeeded = needed;
89   }
90 
HasImageMask()91   bool HasImageMask() const { return !m_MaskBoundingBoxes.empty(); }
GetMaskBoundingBoxes()92   const std::vector<CFX_FloatRect>& GetMaskBoundingBoxes() const {
93     return m_MaskBoundingBoxes;
94   }
95   void AddImageMaskBoundingBox(const CFX_FloatRect& box);
HasDirtyStreams()96   bool HasDirtyStreams() const { return !m_DirtyStreams.empty(); }
97   std::set<int32_t> TakeDirtyStreams();
98 
99   RetainPtr<CPDF_Dictionary> m_pPageResources;
100   RetainPtr<CPDF_Dictionary> m_pResources;
101   std::map<GraphicsData, ByteString> m_GraphicsMap;
102   std::map<FontData, ByteString> m_FontsMap;
103 
104  protected:
105   void LoadTransparencyInfo();
106 
107   CFX_FloatRect m_BBox;
108   CPDF_Transparency m_Transparency;
109 
110  private:
111   bool m_bBackgroundAlphaNeeded = false;
112   ParseState m_ParseState = ParseState::kNotParsed;
113   RetainPtr<CPDF_Dictionary> const m_pDict;
114   UnownedPtr<CPDF_Document> m_pDocument;
115   std::vector<CFX_FloatRect> m_MaskBoundingBoxes;
116   std::unique_ptr<CPDF_ContentParser> m_pParser;
117   std::deque<std::unique_ptr<CPDF_PageObject>> m_PageObjectList;
118   CFX_Matrix m_LastCTM;
119 
120   // The indexes of Content streams that are dirty and need to be regenerated.
121   std::set<int32_t> m_DirtyStreams;
122 };
123 
124 #endif  // CORE_FPDFAPI_PAGE_CPDF_PAGEOBJECTHOLDER_H_
125