xref: /aosp_15_r20/external/pdfium/fpdfsdk/cpdfsdk_annotiterator.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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 #include "fpdfsdk/cpdfsdk_annotiterator.h"
8 
9 #include <algorithm>
10 
11 #include "core/fpdfapi/page/cpdf_page.h"
12 #include "core/fpdfapi/parser/cpdf_dictionary.h"
13 #include "core/fxcrt/stl_util.h"
14 #include "fpdfsdk/cpdfsdk_annot.h"
15 #include "fpdfsdk/cpdfsdk_pageview.h"
16 #include "fpdfsdk/cpdfsdk_widget.h"
17 #include "third_party/base/containers/contains.h"
18 
19 namespace {
20 
GetAnnotRect(const CPDFSDK_Annot * pAnnot)21 CFX_FloatRect GetAnnotRect(const CPDFSDK_Annot* pAnnot) {
22   return pAnnot->GetPDFAnnot()->GetRect();
23 }
24 
CompareByLeftAscending(const CPDFSDK_Annot * p1,const CPDFSDK_Annot * p2)25 bool CompareByLeftAscending(const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) {
26   return GetAnnotRect(p1).left < GetAnnotRect(p2).left;
27 }
28 
CompareByTopDescending(const CPDFSDK_Annot * p1,const CPDFSDK_Annot * p2)29 bool CompareByTopDescending(const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) {
30   return GetAnnotRect(p1).top > GetAnnotRect(p2).top;
31 }
32 
33 }  // namespace
34 
CPDFSDK_AnnotIterator(CPDFSDK_PageView * pPageView,const std::vector<CPDF_Annot::Subtype> & subtypes_to_iterate)35 CPDFSDK_AnnotIterator::CPDFSDK_AnnotIterator(
36     CPDFSDK_PageView* pPageView,
37     const std::vector<CPDF_Annot::Subtype>& subtypes_to_iterate)
38     : m_pPageView(pPageView),
39       m_subtypes(subtypes_to_iterate),
40       m_eTabOrder(GetTabOrder(pPageView)) {
41   GenerateResults();
42 }
43 
44 CPDFSDK_AnnotIterator::~CPDFSDK_AnnotIterator() = default;
45 
GetFirstAnnot()46 CPDFSDK_Annot* CPDFSDK_AnnotIterator::GetFirstAnnot() {
47   return m_Annots.empty() ? nullptr : m_Annots.front();
48 }
49 
GetLastAnnot()50 CPDFSDK_Annot* CPDFSDK_AnnotIterator::GetLastAnnot() {
51   return m_Annots.empty() ? nullptr : m_Annots.back();
52 }
53 
GetNextAnnot(CPDFSDK_Annot * pAnnot)54 CPDFSDK_Annot* CPDFSDK_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot) {
55   auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot);
56   if (iter == m_Annots.end())
57     return nullptr;
58   ++iter;
59   if (iter == m_Annots.end())
60     return nullptr;
61   return *iter;
62 }
63 
GetPrevAnnot(CPDFSDK_Annot * pAnnot)64 CPDFSDK_Annot* CPDFSDK_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) {
65   auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot);
66   if (iter == m_Annots.begin() || iter == m_Annots.end())
67     return nullptr;
68   return *(--iter);
69 }
70 
CollectAnnots(std::vector<UnownedPtr<CPDFSDK_Annot>> * pArray)71 void CPDFSDK_AnnotIterator::CollectAnnots(
72     std::vector<UnownedPtr<CPDFSDK_Annot>>* pArray) {
73   for (auto* pAnnot : m_pPageView->GetAnnotList()) {
74     if (pdfium::Contains(m_subtypes, pAnnot->GetAnnotSubtype())) {
75       CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot);
76       if (!pWidget || !pWidget->IsSignatureWidget())
77         pArray->emplace_back(pAnnot);
78     }
79   }
80 }
81 
AddToAnnotsList(std::vector<UnownedPtr<CPDFSDK_Annot>> * sa,size_t idx)82 CFX_FloatRect CPDFSDK_AnnotIterator::AddToAnnotsList(
83     std::vector<UnownedPtr<CPDFSDK_Annot>>* sa,
84     size_t idx) {
85   CPDFSDK_Annot* pLeftTopAnnot = sa->at(idx);
86   CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
87   m_Annots.emplace_back(pLeftTopAnnot);
88   sa->erase(sa->begin() + idx);
89   return rcLeftTop;
90 }
91 
AddSelectedToAnnots(std::vector<UnownedPtr<CPDFSDK_Annot>> * sa,std::vector<size_t> * aSelect)92 void CPDFSDK_AnnotIterator::AddSelectedToAnnots(
93     std::vector<UnownedPtr<CPDFSDK_Annot>>* sa,
94     std::vector<size_t>* aSelect) {
95   for (size_t i = 0; i < aSelect->size(); ++i)
96     m_Annots.emplace_back(sa->at(aSelect->at(i)));
97 
98   for (size_t i = aSelect->size(); i > 0; --i)
99     sa->erase(sa->begin() + aSelect->at(i - 1));
100 }
101 
102 // static
GetTabOrder(CPDFSDK_PageView * pPageView)103 CPDFSDK_AnnotIterator::TabOrder CPDFSDK_AnnotIterator::GetTabOrder(
104     CPDFSDK_PageView* pPageView) {
105   CPDF_Page* pPDFPage = pPageView->GetPDFPage();
106   ByteString sTabs = pPDFPage->GetDict()->GetByteStringFor("Tabs");
107   if (sTabs == "R")
108     return kRow;
109   if (sTabs == "C")
110     return kColumn;
111   return kStructure;
112 }
113 
GenerateResults()114 void CPDFSDK_AnnotIterator::GenerateResults() {
115   switch (m_eTabOrder) {
116     case kStructure:
117       CollectAnnots(&m_Annots);
118       break;
119 
120     case kRow: {
121       std::vector<UnownedPtr<CPDFSDK_Annot>> sa;
122       CollectAnnots(&sa);
123       std::sort(sa.begin(), sa.end(), CompareByLeftAscending);
124 
125       while (!sa.empty()) {
126         int nLeftTopIndex = -1;
127         float fTop = 0.0f;
128         for (int i = fxcrt::CollectionSize<int>(sa) - 1; i >= 0; i--) {
129           CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
130           if (rcAnnot.top > fTop) {
131             nLeftTopIndex = i;
132             fTop = rcAnnot.top;
133           }
134         }
135         if (nLeftTopIndex < 0)
136           continue;
137 
138         CFX_FloatRect rcLeftTop = AddToAnnotsList(&sa, nLeftTopIndex);
139 
140         std::vector<size_t> aSelect;
141         for (size_t i = 0; i < sa.size(); ++i) {
142           CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
143           float fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f;
144           if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top)
145             aSelect.push_back(i);
146         }
147         AddSelectedToAnnots(&sa, &aSelect);
148       }
149       break;
150     }
151 
152     case kColumn: {
153       std::vector<UnownedPtr<CPDFSDK_Annot>> sa;
154       CollectAnnots(&sa);
155       std::sort(sa.begin(), sa.end(), CompareByTopDescending);
156 
157       while (!sa.empty()) {
158         int nLeftTopIndex = -1;
159         float fLeft = -1.0f;
160         for (int i = fxcrt::CollectionSize<int>(sa) - 1; i >= 0; --i) {
161           CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
162           if (fLeft < 0) {
163             nLeftTopIndex = 0;
164             fLeft = rcAnnot.left;
165           } else if (rcAnnot.left < fLeft) {
166             nLeftTopIndex = i;
167             fLeft = rcAnnot.left;
168           }
169         }
170         if (nLeftTopIndex < 0)
171           continue;
172 
173         CFX_FloatRect rcLeftTop = AddToAnnotsList(&sa, nLeftTopIndex);
174 
175         std::vector<size_t> aSelect;
176         for (size_t i = 0; i < sa.size(); ++i) {
177           CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
178           float fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f;
179           if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right)
180             aSelect.push_back(i);
181         }
182         AddSelectedToAnnots(&sa, &aSelect);
183       }
184       break;
185     }
186   }
187 }
188