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