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_FPDF_PARSER_INCLUDE_CPDF_DOCUMENT_H_
8 #define CORE_FPDFAPI_FPDF_PARSER_INCLUDE_CPDF_DOCUMENT_H_
9 
10 #include <memory>
11 
12 #include "core/fpdfapi/fpdf_parser/include/cpdf_indirect_object_holder.h"
13 #include "core/fpdfapi/fpdf_parser/include/cpdf_object.h"
14 #include "core/fpdfdoc/include/fpdf_doc.h"
15 #include "core/fxcrt/include/fx_basic.h"
16 
17 class CFX_Font;
18 class CFX_Matrix;
19 class CPDF_ColorSpace;
20 class CPDF_DocPageData;
21 class CPDF_DocRenderData;
22 class CPDF_Font;
23 class CPDF_FontEncoding;
24 class CPDF_IccProfile;
25 class CPDF_Image;
26 class CPDF_Pattern;
27 class CPDF_StreamAcc;
28 
29 #define FPDFPERM_PRINT 0x0004
30 #define FPDFPERM_MODIFY 0x0008
31 #define FPDFPERM_EXTRACT 0x0010
32 #define FPDFPERM_ANNOT_FORM 0x0020
33 #define FPDFPERM_FILL_FORM 0x0100
34 #define FPDFPERM_EXTRACT_ACCESS 0x0200
35 #define FPDFPERM_ASSEMBLE 0x0400
36 #define FPDFPERM_PRINT_HIGH 0x0800
37 #define FPDF_PAGE_MAX_NUM 0xFFFFF
38 
39 class CPDF_Document : public CPDF_IndirectObjectHolder {
40  public:
41   explicit CPDF_Document(CPDF_Parser* pParser);
42   ~CPDF_Document();
43 
GetParser()44   CPDF_Parser* GetParser() const { return m_pParser; }
GetRoot()45   CPDF_Dictionary* GetRoot() const { return m_pRootDict; }
GetInfo()46   CPDF_Dictionary* GetInfo() const { return m_pInfoDict; }
47 
GetID(CFX_ByteString & id1,CFX_ByteString & id2)48   void GetID(CFX_ByteString& id1, CFX_ByteString& id2) const {
49     id1 = m_ID1;
50     id2 = m_ID2;
51   }
52 
53   int GetPageCount() const;
54   CPDF_Dictionary* GetPage(int iPage);
55   int GetPageIndex(uint32_t objnum);
56   uint32_t GetUserPermissions() const;
GetPageData()57   CPDF_DocPageData* GetPageData() const { return m_pDocPage; }
58   void ClearPageData();
59   void RemoveColorSpaceFromPageData(CPDF_Object* pObject);
60 
CodecContext()61   std::unique_ptr<CFX_Deletable>* CodecContext() { return &m_pCodecContext; }
LinksContext()62   std::unique_ptr<CPDF_LinkList>* LinksContext() { return &m_pLinksContext; }
63 
GetRenderData()64   CPDF_DocRenderData* GetRenderData() const { return m_pDocRender.get(); }
65   void ClearRenderData();
66   void ClearRenderFont();
67 
68   FX_BOOL IsFormStream(uint32_t objnum, FX_BOOL& bForm) const;
69 
70   // |pFontDict| must not be null.
71   CPDF_Font* LoadFont(CPDF_Dictionary* pFontDict);
72   CPDF_ColorSpace* LoadColorSpace(CPDF_Object* pCSObj,
73                                   CPDF_Dictionary* pResources = nullptr);
74 
75   CPDF_Pattern* LoadPattern(CPDF_Object* pObj,
76                             FX_BOOL bShading,
77                             const CFX_Matrix& matrix);
78 
79   CPDF_Image* LoadImageF(CPDF_Object* pObj);
80   CPDF_StreamAcc* LoadFontFile(CPDF_Stream* pStream);
81   CPDF_IccProfile* LoadIccProfile(CPDF_Stream* pStream);
82 
83   void LoadDoc();
84   void LoadAsynDoc(CPDF_Dictionary* pLinearized);
85   void LoadPages();
86 
87   // Editing methods.
88   void CreateNewDoc();
89   CPDF_Dictionary* CreateNewPage(int iPage);
90   void DeletePage(int iPage);
91   CPDF_Font* AddStandardFont(const FX_CHAR* font, CPDF_FontEncoding* pEncoding);
92   CPDF_Font* AddFont(CFX_Font* pFont, int charset, FX_BOOL bVert);
93 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
94   CPDF_Font* AddWindowsFont(LOGFONTA* pLogFont,
95                             FX_BOOL bVert,
96                             FX_BOOL bTranslateName = FALSE);
97   CPDF_Font* AddWindowsFont(LOGFONTW* pLogFont,
98                             FX_BOOL bVert,
99                             FX_BOOL bTranslateName = FALSE);
100 #endif
101 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
102   CPDF_Font* AddMacFont(CTFontRef pFont,
103                         FX_BOOL bVert,
104                         FX_BOOL bTranslateName = FALSE);
105 #endif
106 
107  protected:
108   friend class CPDF_Creator;
109   friend class CPDF_Parser;
110   friend class CPDF_DataAvail;
111   friend class CPDF_OCContext;
112 
113   // Retrieve page count information by getting count value from the tree nodes
114   int RetrievePageCount() const;
115   CPDF_Dictionary* FindPDFPage(CPDF_Dictionary* pPages,
116                                int iPage,
117                                int nPagesToGo,
118                                int level);
119   int FindPageIndex(CPDF_Dictionary* pNode,
120                     uint32_t& skip_count,
121                     uint32_t objnum,
122                     int& index,
123                     int level = 0);
124   FX_BOOL CheckOCGVisible(CPDF_Dictionary* pOCG, FX_BOOL bPrinting);
125 
126   CPDF_Dictionary* m_pRootDict;
127   CPDF_Dictionary* m_pInfoDict;
128   CFX_ByteString m_ID1;
129   CFX_ByteString m_ID2;
130   bool m_bLinearized;
131   int m_iFirstPageNo;
132   uint32_t m_dwFirstPageObjNum;
133   CFX_ArrayTemplate<uint32_t> m_PageList;
134   // TODO(thestig): Figure out why this cannot be a std::unique_ptr.
135   CPDF_DocPageData* m_pDocPage;
136   std::unique_ptr<CPDF_DocRenderData> m_pDocRender;
137   std::unique_ptr<CFX_Deletable> m_pCodecContext;
138   std::unique_ptr<CPDF_LinkList> m_pLinksContext;
139 };
140 
141 #endif  // CORE_FPDFAPI_FPDF_PARSER_INCLUDE_CPDF_DOCUMENT_H_
142