1 // Copyright 2016 The PDFium Authors 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/bytestring.h" 18 #include "core/fxcrt/data_vector.h" 19 #include "core/fxcrt/fx_codepage_forward.h" 20 #include "core/fxcrt/fx_coordinates.h" 21 #include "core/fxcrt/retain_ptr.h" 22 23 class CFX_Font; 24 class CPDF_Dictionary; 25 class CPDF_FontEncoding; 26 class CPDF_IccProfile; 27 class CPDF_Image; 28 class CPDF_Object; 29 class CPDF_Pattern; 30 class CPDF_Stream; 31 class CPDF_StreamAcc; 32 33 class CPDF_DocPageData final : public CPDF_Document::PageDataIface, 34 public CPDF_Font::FormFactoryIface { 35 public: 36 static CPDF_DocPageData* FromDocument(const CPDF_Document* pDoc); 37 38 CPDF_DocPageData(); 39 ~CPDF_DocPageData() override; 40 41 // CPDF_Document::PageDataIface: 42 void ClearStockFont() override; 43 RetainPtr<CPDF_StreamAcc> GetFontFileStreamAcc( 44 RetainPtr<const CPDF_Stream> pFontStream) override; 45 void MaybePurgeFontFileStreamAcc( 46 RetainPtr<CPDF_StreamAcc>&& pStreamAcc) override; 47 void MaybePurgeImage(uint32_t dwStreamObjNum) override; 48 49 // CPDF_Font::FormFactoryIFace: 50 std::unique_ptr<CPDF_Font::FormIface> CreateForm( 51 CPDF_Document* pDocument, 52 RetainPtr<CPDF_Dictionary> pPageResources, 53 RetainPtr<CPDF_Stream> pFormStream) override; 54 IsForceClear()55 bool IsForceClear() const { return m_bForceClear; } 56 57 RetainPtr<CPDF_Font> AddFont(std::unique_ptr<CFX_Font> pFont, 58 FX_Charset charset); 59 RetainPtr<CPDF_Font> GetFont(RetainPtr<CPDF_Dictionary> pFontDict); 60 RetainPtr<CPDF_Font> AddStandardFont(const ByteString& fontName, 61 const CPDF_FontEncoding* pEncoding); 62 RetainPtr<CPDF_Font> GetStandardFont(const ByteString& fontName, 63 const CPDF_FontEncoding* pEncoding); 64 #if BUILDFLAG(IS_WIN) 65 RetainPtr<CPDF_Font> AddWindowsFont(LOGFONTA* pLogFont); 66 #endif 67 68 // Loads a colorspace. 69 RetainPtr<CPDF_ColorSpace> GetColorSpace(const CPDF_Object* pCSObj, 70 const CPDF_Dictionary* pResources); 71 72 // Loads a colorspace in a context that might be while loading another 73 // colorspace. |pVisited| is passed recursively to avoid circular calls 74 // involving CPDF_ColorSpace::Load(). 75 RetainPtr<CPDF_ColorSpace> GetColorSpaceGuarded( 76 const CPDF_Object* pCSObj, 77 const CPDF_Dictionary* pResources, 78 std::set<const CPDF_Object*>* pVisited); 79 80 RetainPtr<CPDF_Pattern> GetPattern(RetainPtr<CPDF_Object> pPatternObj, 81 const CFX_Matrix& matrix); 82 RetainPtr<CPDF_ShadingPattern> GetShading(RetainPtr<CPDF_Object> pPatternObj, 83 const CFX_Matrix& matrix); 84 85 RetainPtr<CPDF_Image> GetImage(uint32_t dwStreamObjNum); 86 87 RetainPtr<CPDF_IccProfile> GetIccProfile( 88 RetainPtr<const CPDF_Stream> pProfileStream); 89 90 private: 91 struct HashIccProfileKey { 92 HashIccProfileKey(DataVector<uint8_t> digest, uint32_t components); 93 HashIccProfileKey(const HashIccProfileKey& that); 94 ~HashIccProfileKey(); 95 96 bool operator<(const HashIccProfileKey& other) const; 97 98 DataVector<uint8_t> digest; 99 uint32_t components; 100 }; 101 102 // Loads a colorspace in a context that might be while loading another 103 // colorspace, or even in a recursive call from this method itself. |pVisited| 104 // is passed recursively to avoid circular calls involving 105 // CPDF_ColorSpace::Load() and |pVisitedInternal| is also passed recursively 106 // to avoid circular calls with this method calling itself. 107 RetainPtr<CPDF_ColorSpace> GetColorSpaceInternal( 108 const CPDF_Object* pCSObj, 109 const CPDF_Dictionary* pResources, 110 std::set<const CPDF_Object*>* pVisited, 111 std::set<const CPDF_Object*>* pVisitedInternal); 112 113 size_t CalculateEncodingDict(FX_Charset charset, CPDF_Dictionary* pBaseDict); 114 RetainPtr<CPDF_Dictionary> ProcessbCJK( 115 RetainPtr<CPDF_Dictionary> pBaseDict, 116 FX_Charset charset, 117 ByteString basefont, 118 std::function<void(wchar_t, wchar_t, CPDF_Array*)> Insert); 119 120 bool m_bForceClear = false; 121 122 // Specific destruction order may be required between maps. 123 std::map<HashIccProfileKey, RetainPtr<const CPDF_Stream>> m_HashIccProfileMap; 124 std::map<RetainPtr<const CPDF_Array>, RetainPtr<CPDF_ColorSpace>> 125 m_ColorSpaceMap; 126 std::map<RetainPtr<const CPDF_Stream>, RetainPtr<CPDF_StreamAcc>> 127 m_FontFileMap; 128 std::map<RetainPtr<const CPDF_Stream>, RetainPtr<CPDF_IccProfile>> 129 m_IccProfileMap; 130 std::map<RetainPtr<const CPDF_Object>, RetainPtr<CPDF_Pattern>> m_PatternMap; 131 std::map<uint32_t, RetainPtr<CPDF_Image>> m_ImageMap; 132 std::map<RetainPtr<const CPDF_Dictionary>, RetainPtr<CPDF_Font>> m_FontMap; 133 }; 134 135 #endif // CORE_FPDFAPI_PAGE_CPDF_DOCPAGEDATA_H_ 136