PoDoFo 1.2.0
Loading...
Searching...
No Matches
PdfAnnotationCollection.h
1// SPDX-FileCopyrightText: 2022 Francesco Pretto <ceztko@gmail.com>
2// SPDX-License-Identifier: LGPL-2.0-or-later OR MPL-2.0
3
4#ifndef PDF_ANNOTATION_COLLECTION_H
5#define PDF_ANNOTATION_COLLECTION_H
6
7#include "PdfAnnotation.h"
8#include "PdfArray.h"
9
10namespace PoDoFo
11{
12 class PdfPage;
13
14 class PODOFO_API PdfAnnotationCollection final
15 {
16 friend class PdfPage;
17 friend class PdfAnnotation;
18
19 private:
20 PdfAnnotationCollection(PdfPage& page);
21
22 public:
23 template <typename TAnnotation>
24 TAnnotation& CreateAnnot(const Rect& rect);
25
26 PdfAnnotation& CreateAnnot(PdfAnnotationType annotType, const Rect& rect);
27
28 PdfAnnotation& GetAnnotAt(unsigned index);
29
30 const PdfAnnotation& GetAnnotAt(unsigned index) const;
31
32 PdfAnnotation& GetAnnot(const PdfReference& ref);
33
34 const PdfAnnotation& GetAnnot(const PdfReference& ref) const;
35
36 void RemoveAnnotAt(unsigned index);
37
38 void RemoveAnnot(const PdfReference& ref);
39
40 unsigned GetCount() const;
41
42 public:
43 using AnnotationList = std::vector<std::unique_ptr<PdfAnnotation>>;
44
45 template <typename TObject, typename TListIterator>
46 class Iterator final
47 {
48 friend class PdfAnnotationCollection;
49 public:
50 using difference_type = void;
51 using value_type = TObject*;
52 using pointer = void;
53 using reference = void;
54 using iterator_category = std::forward_iterator_tag;
55 public:
56 Iterator() { }
57 private:
58 Iterator(const TListIterator& iterator) : m_iterator(iterator) { }
59 public:
60 Iterator(const Iterator&) = default;
61 Iterator& operator=(const Iterator&) = default;
62 bool operator==(const Iterator& rhs) const
63 {
64 return m_iterator == rhs.m_iterator;
65 }
66 bool operator!=(const Iterator& rhs) const
67 {
68 return m_iterator != rhs.m_iterator;
69 }
70 Iterator& operator++()
71 {
72 m_iterator++;
73 return *this;
74 }
75 Iterator operator++(int)
76 {
77 auto copy = *this;
78 m_iterator++;
79 return copy;
80 }
81 value_type operator*()
82 {
83 return m_iterator->get();
84 }
85 value_type operator->()
86 {
87 return m_iterator->get();
88 }
89 private:
90 TListIterator m_iterator;
91 };
92
93 using iterator = Iterator<PdfAnnotation, AnnotationList::iterator>;
94 using const_iterator = Iterator<const PdfAnnotation, AnnotationList::const_iterator>;
95
96 public:
97 iterator begin();
98 iterator end();
99 const_iterator begin() const;
100 const_iterator end() const;
101
102 private:
103 PdfAnnotation& addAnnotation(std::unique_ptr<PdfAnnotation>&& annot);
104 PdfArray* getAnnotationsArray() const;
105 void initAnnotations();
106 PdfAnnotation& getAnnotAt(unsigned index) const;
107 PdfAnnotation& getAnnot(const PdfReference& ref) const;
108 void fixIndices(unsigned index);
109
110 private:
111 using AnnotationMap = std::map<PdfReference, unsigned>;
112
113 private:
114 AnnotationList m_Annots;
115 std::unique_ptr<AnnotationMap> m_annotMap;
116 PdfPage* m_Page;
117 PdfArray* m_annotArray;
118 };
119
120 template<typename TAnnotation>
121 TAnnotation& PdfAnnotationCollection::CreateAnnot(const Rect& rect)
122 {
123 return static_cast<TAnnotation&>(CreateAnnot(PdfAnnotation::GetAnnotationType<TAnnotation>(), rect));
124 }
125}
126
127#endif // PDF_ANNOTATION_COLLECTION_H
All classes, functions, types and enums of PoDoFo are members of these namespace.
Definition basetypes.h:13