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_PARSER_CPDF_DOCUMENT_H_ 8 #define CORE_FPDFAPI_PARSER_CPDF_DOCUMENT_H_ 9 10 #include <functional> 11 #include <memory> 12 #include <set> 13 #include <utility> 14 #include <vector> 15 16 #include "core/fpdfapi/parser/cpdf_indirect_object_holder.h" 17 #include "core/fpdfapi/parser/cpdf_object.h" 18 #include "core/fpdfdoc/cpdf_linklist.h" 19 #include "core/fxcrt/fx_basic.h" 20 21 class CFX_Font; 22 class CFX_Matrix; 23 class CPDF_ColorSpace; 24 class CPDF_DocPageData; 25 class CPDF_DocRenderData; 26 class CPDF_Font; 27 class CPDF_FontEncoding; 28 class CPDF_IccProfile; 29 class CPDF_Image; 30 class CPDF_LinearizedHeader; 31 class CPDF_Parser; 32 class CPDF_Pattern; 33 class CPDF_StreamAcc; 34 class JBig2_DocumentContext; 35 36 #define FPDFPERM_PRINT 0x0004 37 #define FPDFPERM_MODIFY 0x0008 38 #define FPDFPERM_EXTRACT 0x0010 39 #define FPDFPERM_ANNOT_FORM 0x0020 40 #define FPDFPERM_FILL_FORM 0x0100 41 #define FPDFPERM_EXTRACT_ACCESS 0x0200 42 #define FPDFPERM_ASSEMBLE 0x0400 43 #define FPDFPERM_PRINT_HIGH 0x0800 44 #define FPDF_PAGE_MAX_NUM 0xFFFFF 45 46 class CPDF_Document : public CPDF_IndirectObjectHolder { 47 public: 48 explicit CPDF_Document(std::unique_ptr<CPDF_Parser> pParser); 49 ~CPDF_Document() override; 50 GetParser()51 CPDF_Parser* GetParser() const { return m_pParser.get(); } GetRoot()52 CPDF_Dictionary* GetRoot() const { return m_pRootDict; } GetInfo()53 CPDF_Dictionary* GetInfo() const { return m_pInfoDict; } 54 55 void DeletePage(int iPage); 56 int GetPageCount() const; 57 bool IsPageLoaded(int iPage) const; 58 CPDF_Dictionary* GetPage(int iPage); 59 int GetPageIndex(uint32_t objnum); 60 uint32_t GetUserPermissions() const; GetPageData()61 CPDF_DocPageData* GetPageData() const { return m_pDocPage; } 62 63 void SetPageObjNum(int iPage, uint32_t objNum); 64 CodecContext()65 std::unique_ptr<JBig2_DocumentContext>* CodecContext() { 66 return &m_pCodecContext; 67 } LinksContext()68 std::unique_ptr<CPDF_LinkList>* LinksContext() { return &m_pLinksContext; } 69 GetRenderData()70 CPDF_DocRenderData* GetRenderData() const { return m_pDocRender.get(); } 71 72 // |pFontDict| must not be null. 73 CPDF_Font* LoadFont(CPDF_Dictionary* pFontDict); 74 CPDF_ColorSpace* LoadColorSpace(CPDF_Object* pCSObj, 75 CPDF_Dictionary* pResources = nullptr); 76 77 CPDF_Pattern* LoadPattern(CPDF_Object* pObj, 78 bool bShading, 79 const CFX_Matrix& matrix); 80 81 CPDF_Image* LoadImageFromPageData(uint32_t dwStreamObjNum); 82 CPDF_StreamAcc* LoadFontFile(CPDF_Stream* pStream); 83 CPDF_IccProfile* LoadIccProfile(CPDF_Stream* pStream); 84 85 void LoadDoc(); 86 void LoadLinearizedDoc(const CPDF_LinearizedHeader* pLinearizationParams); 87 void LoadPages(); 88 89 void CreateNewDoc(); 90 CPDF_Dictionary* CreateNewPage(int iPage); 91 92 CPDF_Font* AddStandardFont(const FX_CHAR* font, CPDF_FontEncoding* pEncoding); 93 CPDF_Font* AddFont(CFX_Font* pFont, int charset, bool bVert); 94 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ 95 CPDF_Font* AddWindowsFont(LOGFONTA* pLogFont, 96 bool bVert, 97 bool bTranslateName = false); 98 CPDF_Font* AddWindowsFont(LOGFONTW* pLogFont, 99 bool bVert, 100 bool bTranslateName = false); 101 #endif 102 103 protected: 104 // Retrieve page count information by getting count value from the tree nodes 105 int RetrievePageCount() const; 106 // When this method is called, m_pTreeTraversal[level] exists. 107 CPDF_Dictionary* TraversePDFPages(int iPage, int* nPagesToGo, size_t level); 108 int FindPageIndex(CPDF_Dictionary* pNode, 109 uint32_t* skip_count, 110 uint32_t objnum, 111 int* index, 112 int level = 0); 113 std::unique_ptr<CPDF_Object> ParseIndirectObject(uint32_t objnum) override; 114 void LoadDocInternal(); 115 size_t CalculateEncodingDict(int charset, CPDF_Dictionary* pBaseDict); 116 CPDF_Dictionary* GetPagesDict() const; 117 CPDF_Dictionary* ProcessbCJK( 118 CPDF_Dictionary* pBaseDict, 119 int charset, 120 bool bVert, 121 CFX_ByteString basefont, 122 std::function<void(FX_WCHAR, FX_WCHAR, CPDF_Array*)> Insert); 123 bool InsertDeletePDFPage(CPDF_Dictionary* pPages, 124 int nPagesToGo, 125 CPDF_Dictionary* pPageDict, 126 bool bInsert, 127 std::set<CPDF_Dictionary*>* pVisited); 128 bool InsertNewPage(int iPage, CPDF_Dictionary* pPageDict); 129 void ResetTraversal(); 130 131 std::unique_ptr<CPDF_Parser> m_pParser; 132 CPDF_Dictionary* m_pRootDict; 133 CPDF_Dictionary* m_pInfoDict; 134 // Vector of pairs to know current position in the page tree. The index in the 135 // vector corresponds to the level being described. The pair contains a 136 // pointer to the dictionary being processed at the level, and an index of the 137 // of the child being processed within the dictionary's /Kids array. 138 std::vector<std::pair<CPDF_Dictionary*, size_t>> m_pTreeTraversal; 139 // Index of the next page that will be traversed from the page tree. 140 int m_iNextPageToTraverse; 141 bool m_bReachedMaxPageLevel; 142 bool m_bLinearized; 143 int m_iFirstPageNo; 144 uint32_t m_dwFirstPageObjNum; 145 // TODO(thestig): Figure out why this cannot be a std::unique_ptr. 146 CPDF_DocPageData* m_pDocPage; 147 std::unique_ptr<CPDF_DocRenderData> m_pDocRender; 148 std::unique_ptr<JBig2_DocumentContext> m_pCodecContext; 149 std::unique_ptr<CPDF_LinkList> m_pLinksContext; 150 std::vector<uint32_t> m_PageList; 151 }; 152 153 #endif // CORE_FPDFAPI_PARSER_CPDF_DOCUMENT_H_ 154