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_DOCPAGEDATA_H_
8 #define CORE_FPDFAPI_PAGE_CPDF_DOCPAGEDATA_H_
9 
10 #include <map>
11 #include <memory>
12 #include <set>
13 
14 #include "core/fpdfapi/font/cpdf_font.h"
15 #include "core/fpdfapi/page/cpdf_colorspace.h"
16 #include "core/fpdfapi/parser/cpdf_document.h"
17 #include "core/fxcrt/fx_coordinates.h"
18 #include "core/fxcrt/fx_string.h"
19 #include "core/fxcrt/observed_ptr.h"
20 #include "core/fxcrt/retain_ptr.h"
21 
22 class CFX_Font;
23 class CPDF_Dictionary;
24 class CPDF_FontEncoding;
25 class CPDF_IccProfile;
26 class CPDF_Image;
27 class CPDF_Object;
28 class CPDF_Pattern;
29 class CPDF_Stream;
30 class CPDF_StreamAcc;
31 
32 class CPDF_DocPageData : public CPDF_Document::PageDataIface,
33                          public CPDF_Font::FormFactoryIface {
34  public:
35   static CPDF_DocPageData* FromDocument(const CPDF_Document* pDoc);
36 
37   CPDF_DocPageData();
38   ~CPDF_DocPageData() override;
39 
40   // CPDF_Document::PageDataIface:
41   void ClearStockFont() override;
42   RetainPtr<CPDF_StreamAcc> GetFontFileStreamAcc(
43       const CPDF_Stream* pFontStream) override;
44   void MaybePurgeFontFileStreamAcc(const CPDF_Stream* pFontStream) override;
45 
46   // CPDF_Font::FormFactoryIFace:
47   std::unique_ptr<CPDF_Font::FormIface> CreateForm(
48       CPDF_Document* pDocument,
49       CPDF_Dictionary* pPageResources,
50       CPDF_Stream* pFormStream) override;
51 
IsForceClear()52   bool IsForceClear() const { return m_bForceClear; }
53 
54   RetainPtr<CPDF_Font> AddFont(std::unique_ptr<CFX_Font> pFont, int charset);
55   RetainPtr<CPDF_Font> GetFont(CPDF_Dictionary* pFontDict);
56   RetainPtr<CPDF_Font> AddStandardFont(const ByteString& fontName,
57                                        const CPDF_FontEncoding* pEncoding);
58   RetainPtr<CPDF_Font> GetStandardFont(const ByteString& fontName,
59                                        const CPDF_FontEncoding* pEncoding);
60 #if defined(OS_WIN)
61   RetainPtr<CPDF_Font> AddWindowsFont(LOGFONTA* pLogFont);
62 #endif
63 
64   // Loads a colorspace.
65   RetainPtr<CPDF_ColorSpace> GetColorSpace(const CPDF_Object* pCSObj,
66                                            const CPDF_Dictionary* pResources);
67 
68   // Loads a colorspace in a context that might be while loading another
69   // colorspace. |pVisited| is passed recursively to avoid circular calls
70   // involving CPDF_ColorSpace::Load().
71   RetainPtr<CPDF_ColorSpace> GetColorSpaceGuarded(
72       const CPDF_Object* pCSObj,
73       const CPDF_Dictionary* pResources,
74       std::set<const CPDF_Object*>* pVisited);
75 
76   RetainPtr<CPDF_Pattern> GetPattern(CPDF_Object* pPatternObj,
77                                      bool bShading,
78                                      const CFX_Matrix& matrix);
79 
80   RetainPtr<CPDF_Image> GetImage(uint32_t dwStreamObjNum);
81   void MaybePurgeImage(uint32_t dwStreamObjNum);
82 
83   RetainPtr<CPDF_IccProfile> GetIccProfile(const CPDF_Stream* pProfileStream);
84 
85  private:
86   // Loads a colorspace in a context that might be while loading another
87   // colorspace, or even in a recursive call from this method itself. |pVisited|
88   // is passed recursively to avoid circular calls involving
89   // CPDF_ColorSpace::Load() and |pVisitedInternal| is also passed recursively
90   // to avoid circular calls with this method calling itself.
91   RetainPtr<CPDF_ColorSpace> GetColorSpaceInternal(
92       const CPDF_Object* pCSObj,
93       const CPDF_Dictionary* pResources,
94       std::set<const CPDF_Object*>* pVisited,
95       std::set<const CPDF_Object*>* pVisitedInternal);
96 
97   size_t CalculateEncodingDict(int charset, CPDF_Dictionary* pBaseDict);
98   CPDF_Dictionary* ProcessbCJK(
99       CPDF_Dictionary* pBaseDict,
100       int charset,
101       ByteString basefont,
102       std::function<void(wchar_t, wchar_t, CPDF_Array*)> Insert);
103   void Clear(bool bForceRelease);
104 
105   bool m_bForceClear = false;
106 
107   // Specific destruction order may be required between maps.
108   std::map<ByteString, RetainPtr<const CPDF_Stream>> m_HashProfileMap;
109   std::map<const CPDF_Object*, ObservedPtr<CPDF_ColorSpace>> m_ColorSpaceMap;
110   std::map<const CPDF_Stream*, RetainPtr<CPDF_StreamAcc>> m_FontFileMap;
111   std::map<const CPDF_Stream*, ObservedPtr<CPDF_IccProfile>> m_IccProfileMap;
112   std::map<const CPDF_Object*, ObservedPtr<CPDF_Pattern>> m_PatternMap;
113   std::map<uint32_t, RetainPtr<CPDF_Image>> m_ImageMap;
114   std::map<const CPDF_Dictionary*, ObservedPtr<CPDF_Font>> m_FontMap;
115 };
116 
117 #endif  // CORE_FPDFAPI_PAGE_CPDF_DOCPAGEDATA_H_
118