Home | History | Annotate | Download | only in parser
      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/page/cpdf_image.h"
     17 #include "core/fpdfapi/parser/cpdf_indirect_object_holder.h"
     18 #include "core/fpdfapi/parser/cpdf_object.h"
     19 #include "core/fpdfdoc/cpdf_linklist.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_LinearizedHeader;
     30 class CPDF_Parser;
     31 class CPDF_Pattern;
     32 class CPDF_StreamAcc;
     33 class JBig2_DocumentContext;
     34 
     35 #define FPDFPERM_MODIFY 0x0008
     36 #define FPDFPERM_ANNOT_FORM 0x0020
     37 #define FPDFPERM_FILL_FORM 0x0100
     38 #define FPDFPERM_EXTRACT_ACCESS 0x0200
     39 
     40 #define FPDF_PAGE_MAX_NUM 0xFFFFF
     41 
     42 class CPDF_Document : public CPDF_IndirectObjectHolder {
     43  public:
     44   explicit CPDF_Document(std::unique_ptr<CPDF_Parser> pParser);
     45   ~CPDF_Document() override;
     46 
     47   CPDF_Parser* GetParser() const { return m_pParser.get(); }
     48   const CPDF_Dictionary* GetRoot() const { return m_pRootDict; }
     49   CPDF_Dictionary* GetRoot() { return m_pRootDict; }
     50   const CPDF_Dictionary* GetInfo() const { return m_pInfoDict.Get(); }
     51   CPDF_Dictionary* GetInfo() { return m_pInfoDict.Get(); }
     52 
     53   void DeletePage(int iPage);
     54   int GetPageCount() const;
     55   bool IsPageLoaded(int iPage) const;
     56   CPDF_Dictionary* GetPage(int iPage);
     57   int GetPageIndex(uint32_t objnum);
     58   uint32_t GetUserPermissions() const;
     59 
     60   // Returns a valid pointer, unless it is called during destruction.
     61   CPDF_DocPageData* GetPageData() const { return m_pDocPage.get(); }
     62 
     63   void SetPageObjNum(int iPage, uint32_t objNum);
     64 
     65   std::unique_ptr<JBig2_DocumentContext>* CodecContext() {
     66     return &m_pCodecContext;
     67   }
     68   std::unique_ptr<CPDF_LinkList>* LinksContext() { return &m_pLinksContext; }
     69 
     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   RetainPtr<CPDF_Image> LoadImageFromPageData(uint32_t dwStreamObjNum);
     82   RetainPtr<CPDF_StreamAcc> LoadFontFile(CPDF_Stream* pStream);
     83   RetainPtr<CPDF_IccProfile> LoadIccProfile(CPDF_Stream* pStream);
     84 
     85   void LoadDoc();
     86   void LoadLinearizedDoc(const CPDF_LinearizedHeader* pLinearizationParams);
     87   void LoadPages();
     88   void LoadDocumentInfo();
     89 
     90   void CreateNewDoc();
     91   CPDF_Dictionary* CreateNewPage(int iPage);
     92 
     93   CPDF_Font* AddStandardFont(const char* font, CPDF_FontEncoding* pEncoding);
     94   CPDF_Font* AddFont(CFX_Font* pFont, int charset, bool bVert);
     95 #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
     96   CPDF_Font* AddWindowsFont(LOGFONTA* pLogFont,
     97                             bool bVert,
     98                             bool bTranslateName = false);
     99   CPDF_Font* AddWindowsFont(LOGFONTW* pLogFont,
    100                             bool bVert,
    101                             bool bTranslateName = false);
    102 #endif
    103 
    104  protected:
    105   // Retrieve page count information by getting count value from the tree nodes
    106   int RetrievePageCount() const;
    107   // When this method is called, m_pTreeTraversal[level] exists.
    108   CPDF_Dictionary* TraversePDFPages(int iPage, int* nPagesToGo, size_t level);
    109   int FindPageIndex(CPDF_Dictionary* pNode,
    110                     uint32_t* skip_count,
    111                     uint32_t objnum,
    112                     int* index,
    113                     int level = 0) const;
    114   std::unique_ptr<CPDF_Object> ParseIndirectObject(uint32_t objnum) override;
    115   void LoadDocInternal();
    116   size_t CalculateEncodingDict(int charset, CPDF_Dictionary* pBaseDict);
    117   CPDF_Dictionary* GetPagesDict() const;
    118   CPDF_Dictionary* ProcessbCJK(
    119       CPDF_Dictionary* pBaseDict,
    120       int charset,
    121       bool bVert,
    122       ByteString basefont,
    123       std::function<void(wchar_t, wchar_t, CPDF_Array*)> Insert);
    124   bool InsertDeletePDFPage(CPDF_Dictionary* pPages,
    125                            int nPagesToGo,
    126                            CPDF_Dictionary* pPageDict,
    127                            bool bInsert,
    128                            std::set<CPDF_Dictionary*>* pVisited);
    129   bool InsertNewPage(int iPage, CPDF_Dictionary* pPageDict);
    130   void ResetTraversal();
    131 
    132   std::unique_ptr<CPDF_Parser> m_pParser;
    133 
    134   // TODO(tsepez): figure out why tests break if this is an UnownedPtr.
    135   CPDF_Dictionary* m_pRootDict;  // Not owned.
    136 
    137   UnownedPtr<CPDF_Dictionary> m_pInfoDict;
    138 
    139   // Vector of pairs to know current position in the page tree. The index in the
    140   // vector corresponds to the level being described. The pair contains a
    141   // pointer to the dictionary being processed at the level, and an index of the
    142   // of the child being processed within the dictionary's /Kids array.
    143   std::vector<std::pair<CPDF_Dictionary*, size_t>> m_pTreeTraversal;
    144 
    145   // Index of the next page that will be traversed from the page tree.
    146   int m_iNextPageToTraverse;
    147   bool m_bReachedMaxPageLevel;
    148   bool m_bLinearized;
    149   int m_iFirstPageNo;
    150   uint32_t m_dwFirstPageObjNum;
    151   std::unique_ptr<CPDF_DocPageData> m_pDocPage;
    152   std::unique_ptr<CPDF_DocRenderData> m_pDocRender;
    153   std::unique_ptr<JBig2_DocumentContext> m_pCodecContext;
    154   std::unique_ptr<CPDF_LinkList> m_pLinksContext;
    155   std::vector<uint32_t> m_PageList;
    156 };
    157 
    158 #endif  // CORE_FPDFAPI_PARSER_CPDF_DOCUMENT_H_
    159