Home | History | Annotate | Download | only in fpdfdoc
      1 // Copyright 2014 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_FPDFDOC_TAGGED_INT_H_
      8 #define CORE_FPDFDOC_TAGGED_INT_H_
      9 
     10 #include <map>
     11 #include <memory>
     12 #include <vector>
     13 
     14 #include "core/fpdfdoc/fpdf_tagged.h"
     15 #include "core/fxcrt/cfx_retain_ptr.h"
     16 #include "third_party/base/stl_util.h"
     17 
     18 class CPDF_StructElement;
     19 
     20 struct CPDF_StructKid {
     21   CPDF_StructKid();
     22   CPDF_StructKid(const CPDF_StructKid& that);
     23   ~CPDF_StructKid();
     24 
     25   enum { Invalid, Element, PageContent, StreamContent, Object } m_Type;
     26 
     27   CFX_RetainPtr<CPDF_StructElement> m_pElement;  // For Element.
     28   CPDF_Dictionary* m_pDict;                      // For Element.
     29   uint32_t m_PageObjNum;  // For PageContent, StreamContent, Object.
     30   uint32_t m_RefObjNum;   // For StreamContent, Object.
     31   uint32_t m_ContentId;   // For PageContent, StreamContent.
     32 };
     33 
     34 class CPDF_StructTree final : public IPDF_StructTree {
     35  public:
     36   explicit CPDF_StructTree(const CPDF_Document* pDoc);
     37   ~CPDF_StructTree() override;
     38 
     39   // IPDF_StructTree:
     40   int CountTopElements() const override;
     41   IPDF_StructElement* GetTopElement(int i) const override;
     42 
     43   void LoadPageTree(const CPDF_Dictionary* pPageDict);
     44   CFX_RetainPtr<CPDF_StructElement> AddPageNode(
     45       CPDF_Dictionary* pElement,
     46       std::map<CPDF_Dictionary*, CFX_RetainPtr<CPDF_StructElement>>* map,
     47       int nLevel = 0);
     48   bool AddTopLevelNode(CPDF_Dictionary* pDict,
     49                        const CFX_RetainPtr<CPDF_StructElement>& pElement);
     50 
     51  protected:
     52   const CPDF_Dictionary* const m_pTreeRoot;
     53   const CPDF_Dictionary* const m_pRoleMap;
     54   const CPDF_Dictionary* m_pPage;
     55   std::vector<CFX_RetainPtr<CPDF_StructElement>> m_Kids;
     56 
     57   friend class CPDF_StructElement;
     58 };
     59 
     60 class CPDF_StructElement final : public CFX_Retainable,
     61                                  public IPDF_StructElement {
     62  public:
     63   template <typename T, typename... Args>
     64   friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args);
     65 
     66   // IPDF_StructElement
     67   IPDF_StructTree* GetTree() const override;
     68   const CFX_ByteString& GetType() const override;
     69   IPDF_StructElement* GetParent() const override;
     70   CPDF_Dictionary* GetDict() const override;
     71   int CountKids() const override;
     72   IPDF_StructElement* GetKidIfElement(int index) const override;
     73   CPDF_Object* GetAttr(const CFX_ByteStringC& owner,
     74                        const CFX_ByteStringC& name,
     75                        bool bInheritable = false,
     76                        FX_FLOAT fLevel = 0.0F) override;
     77   CFX_ByteString GetName(const CFX_ByteStringC& owner,
     78                          const CFX_ByteStringC& name,
     79                          const CFX_ByteStringC& default_value,
     80                          bool bInheritable = false,
     81                          int subindex = -1) override;
     82   FX_ARGB GetColor(const CFX_ByteStringC& owner,
     83                    const CFX_ByteStringC& name,
     84                    FX_ARGB default_value,
     85                    bool bInheritable = false,
     86                    int subindex = -1) override;
     87   FX_FLOAT GetNumber(const CFX_ByteStringC& owner,
     88                      const CFX_ByteStringC& name,
     89                      FX_FLOAT default_value,
     90                      bool bInheritable = false,
     91                      int subindex = -1) override;
     92   int GetInteger(const CFX_ByteStringC& owner,
     93                  const CFX_ByteStringC& name,
     94                  int default_value,
     95                  bool bInheritable = false,
     96                  int subindex = -1) override;
     97 
     98   std::vector<CPDF_StructKid>* GetKids() { return &m_Kids; }
     99   void LoadKids(CPDF_Dictionary* pDict);
    100   void LoadKid(uint32_t PageObjNum, CPDF_Object* pObj, CPDF_StructKid* pKid);
    101   CPDF_Object* GetAttr(const CFX_ByteStringC& owner,
    102                        const CFX_ByteStringC& name,
    103                        bool bInheritable,
    104                        int subindex);
    105 
    106  private:
    107   CPDF_StructElement(CPDF_StructTree* pTree,
    108                      CPDF_StructElement* pParent,
    109                      CPDF_Dictionary* pDict);
    110   ~CPDF_StructElement() override;
    111 
    112   CPDF_StructTree* const m_pTree;
    113   CPDF_StructElement* const m_pParent;
    114   CPDF_Dictionary* const m_pDict;
    115   CFX_ByteString m_Type;
    116   std::vector<CPDF_StructKid> m_Kids;
    117 };
    118 
    119 #endif  // CORE_FPDFDOC_TAGGED_INT_H_
    120