• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_FPDFDOC_CPDF_ANNOT_H_
8 #define CORE_FPDFDOC_CPDF_ANNOT_H_
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 #include <map>
14 #include <memory>
15 
16 #include "core/fpdfapi/parser/cpdf_stream.h"
17 #include "core/fxcrt/bytestring.h"
18 #include "core/fxcrt/fx_coordinates.h"
19 #include "core/fxcrt/maybe_owned.h"
20 #include "core/fxcrt/retain_ptr.h"
21 #include "core/fxcrt/unowned_ptr.h"
22 #include "third_party/abseil-cpp/absl/types/optional.h"
23 
24 class CFX_RenderDevice;
25 class CPDF_Array;
26 class CPDF_Dictionary;
27 class CPDF_Document;
28 class CPDF_Form;
29 class CPDF_Page;
30 class CPDF_RenderContext;
31 
32 class CPDF_Annot {
33  public:
34   enum class AppearanceMode { kNormal, kRollover, kDown };
35   enum class Subtype : uint8_t {
36     UNKNOWN = 0,
37     TEXT,
38     LINK,
39     FREETEXT,
40     LINE,
41     SQUARE,
42     CIRCLE,
43     POLYGON,
44     POLYLINE,
45     HIGHLIGHT,
46     UNDERLINE,
47     SQUIGGLY,
48     STRIKEOUT,
49     STAMP,
50     CARET,
51     INK,
52     POPUP,
53     FILEATTACHMENT,
54     SOUND,
55     MOVIE,
56     WIDGET,
57     SCREEN,
58     PRINTERMARK,
59     TRAPNET,
60     WATERMARK,
61     THREED,
62     RICHMEDIA,
63     XFAWIDGET,
64     REDACT
65   };
66 
67   static Subtype StringToAnnotSubtype(const ByteString& sSubtype);
68   static ByteString AnnotSubtypeToString(Subtype nSubtype);
69   static CFX_FloatRect RectFromQuadPointsArray(const CPDF_Array* pArray,
70                                                size_t nIndex);
71   static CFX_FloatRect BoundingRectFromQuadPoints(
72       const CPDF_Dictionary* pAnnotDict);
73   static CFX_FloatRect RectFromQuadPoints(const CPDF_Dictionary* pAnnotDict,
74                                           size_t nIndex);
75   static size_t QuadPointCount(const CPDF_Array* pArray);
76 
77   CPDF_Annot(RetainPtr<CPDF_Dictionary> pDict, CPDF_Document* pDocument);
78   ~CPDF_Annot();
79 
80   Subtype GetSubtype() const;
81   uint32_t GetFlags() const;
82   CFX_FloatRect GetRect() const;
GetAnnotDict()83   const CPDF_Dictionary* GetAnnotDict() const { return m_pAnnotDict.Get(); }
GetMutableAnnotDict()84   RetainPtr<CPDF_Dictionary> GetMutableAnnotDict() { return m_pAnnotDict; }
85 
86   bool IsHidden() const;
87 
88   bool DrawAppearance(CPDF_Page* pPage,
89                       CFX_RenderDevice* pDevice,
90                       const CFX_Matrix& mtUser2Device,
91                       AppearanceMode mode);
92   bool DrawInContext(CPDF_Page* pPage,
93                      CPDF_RenderContext* pContext,
94                      const CFX_Matrix& mtUser2Device,
95                      AppearanceMode mode);
96 
97   void ClearCachedAP();
98   void DrawBorder(CFX_RenderDevice* pDevice, const CFX_Matrix* pUser2Device);
99   CPDF_Form* GetAPForm(CPDF_Page* pPage, AppearanceMode mode);
SetOpenState(bool bOpenState)100   void SetOpenState(bool bOpenState) { m_bOpenState = bOpenState; }
101   void SetPopupAnnotOpenState(bool bOpenState);
102   absl::optional<CFX_FloatRect> GetPopupAnnotRect() const;
SetPopupAnnot(CPDF_Annot * pAnnot)103   void SetPopupAnnot(CPDF_Annot* pAnnot) { m_pPopupAnnot = pAnnot; }
104 
105  private:
106   void GenerateAPIfNeeded();
107   bool ShouldGenerateAP() const;
108   bool ShouldDrawAnnotation() const;
109 
110   CFX_FloatRect RectForDrawing() const;
111 
112   RetainPtr<CPDF_Dictionary> const m_pAnnotDict;
113   UnownedPtr<CPDF_Document> const m_pDocument;
114   std::map<RetainPtr<CPDF_Stream>, std::unique_ptr<CPDF_Form>> m_APMap;
115   // If non-null, then this is not a popup annotation.
116   UnownedPtr<CPDF_Annot> m_pPopupAnnot;
117   const Subtype m_nSubtype;
118   const bool m_bIsTextMarkupAnnotation;
119   // |m_bOpenState| is only set for popup annotations.
120   bool m_bOpenState = false;
121   bool m_bHasGeneratedAP;
122 };
123 
124 // Get the AP in an annotation dict for a given appearance mode.
125 // If |eMode| is not Normal and there is not AP for that mode, falls back to
126 // the Normal AP.
127 RetainPtr<CPDF_Stream> GetAnnotAP(CPDF_Dictionary* pAnnotDict,
128                                   CPDF_Annot::AppearanceMode eMode);
129 
130 // Get the AP in an annotation dict for a given appearance mode.
131 // No fallbacks to Normal like in GetAnnotAP.
132 RetainPtr<CPDF_Stream> GetAnnotAPNoFallback(CPDF_Dictionary* pAnnotDict,
133                                             CPDF_Annot::AppearanceMode eMode);
134 
135 #endif  // CORE_FPDFDOC_CPDF_ANNOT_H_
136