blob: d1c9599cf66efbc28986a6cfa3d165f7919aa977 [file] [log] [blame]
jaepark611adb82016-08-17 11:34:36 -07001// 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
dsinclair114e46a2016-09-29 17:18:21 -07007#include "fpdfsdk/cba_annotiterator.h"
jaepark611adb82016-08-17 11:34:36 -07008
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05009#include <algorithm>
10
dsinclair41872fa2016-10-04 11:29:35 -070011#include "core/fpdfapi/page/cpdf_page.h"
dsinclair114e46a2016-09-29 17:18:21 -070012#include "fpdfsdk/cpdfsdk_annot.h"
13#include "fpdfsdk/cpdfsdk_pageview.h"
jaepark611adb82016-08-17 11:34:36 -070014
dsinclair8afe15a2016-10-05 12:00:34 -070015namespace {
16
17CFX_FloatRect GetAnnotRect(const CPDFSDK_Annot* pAnnot) {
18 return pAnnot->GetPDFAnnot()->GetRect();
19}
20
21bool CompareByLeftAscending(const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) {
jaepark611adb82016-08-17 11:34:36 -070022 return GetAnnotRect(p1).left < GetAnnotRect(p2).left;
23}
24
dsinclair8afe15a2016-10-05 12:00:34 -070025bool CompareByTopDescending(const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) {
jaepark611adb82016-08-17 11:34:36 -070026 return GetAnnotRect(p1).top > GetAnnotRect(p2).top;
27}
28
dsinclair8afe15a2016-10-05 12:00:34 -070029} // namespace
30
jaepark611adb82016-08-17 11:34:36 -070031CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView,
jaepark956553e2016-08-31 06:49:27 -070032 CPDF_Annot::Subtype nAnnotSubtype)
jaepark611adb82016-08-17 11:34:36 -070033 : m_eTabOrder(STRUCTURE),
34 m_pPageView(pPageView),
jaepark956553e2016-08-31 06:49:27 -070035 m_nAnnotSubtype(nAnnotSubtype) {
jaepark611adb82016-08-17 11:34:36 -070036 CPDF_Page* pPDFPage = m_pPageView->GetPDFPage();
Ryan Harrison275e2602017-09-18 14:23:18 -040037 ByteString sTabs = pPDFPage->m_pFormDict->GetStringFor("Tabs");
jaepark611adb82016-08-17 11:34:36 -070038 if (sTabs == "R")
39 m_eTabOrder = ROW;
40 else if (sTabs == "C")
41 m_eTabOrder = COLUMN;
42
43 GenerateResults();
44}
45
46CBA_AnnotIterator::~CBA_AnnotIterator() {}
47
48CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot() {
49 return m_Annots.empty() ? nullptr : m_Annots.front();
50}
51
52CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot() {
53 return m_Annots.empty() ? nullptr : m_Annots.back();
54}
55
56CPDFSDK_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
66CPDFSDK_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
dsinclair8afe15a2016-10-05 12:00:34 -070075void CBA_AnnotIterator::CollectAnnots(std::vector<CPDFSDK_Annot*>* pArray) {
Lei Zhang375c2762017-03-10 14:37:14 -080076 for (auto* pAnnot : m_pPageView->GetAnnotList()) {
dsinclair8afe15a2016-10-05 12:00:34 -070077 if (pAnnot->GetAnnotSubtype() == m_nAnnotSubtype &&
78 !pAnnot->IsSignatureWidget()) {
79 pArray->push_back(pAnnot);
80 }
81 }
82}
83
84CFX_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
94void 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
jaepark611adb82016-08-17 11:34:36 -0700103void CBA_AnnotIterator::GenerateResults() {
104 switch (m_eTabOrder) {
dsinclair8afe15a2016-10-05 12:00:34 -0700105 case STRUCTURE:
106 CollectAnnots(&m_Annots);
jaepark611adb82016-08-17 11:34:36 -0700107 break;
dsinclair8afe15a2016-10-05 12:00:34 -0700108
jaepark611adb82016-08-17 11:34:36 -0700109 case ROW: {
110 std::vector<CPDFSDK_Annot*> sa;
dsinclair8afe15a2016-10-05 12:00:34 -0700111 CollectAnnots(&sa);
jaepark611adb82016-08-17 11:34:36 -0700112 std::sort(sa.begin(), sa.end(), CompareByLeftAscending);
dsinclair8afe15a2016-10-05 12:00:34 -0700113
jaepark611adb82016-08-17 11:34:36 -0700114 while (!sa.empty()) {
115 int nLeftTopIndex = -1;
Dan Sinclair05df0752017-03-14 14:43:42 -0400116 float fTop = 0.0f;
jaepark611adb82016-08-17 11:34:36 -0700117 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 }
dsinclair8afe15a2016-10-05 12:00:34 -0700124 if (nLeftTopIndex < 0)
125 continue;
jaepark611adb82016-08-17 11:34:36 -0700126
dsinclair8afe15a2016-10-05 12:00:34 -0700127 CFX_FloatRect rcLeftTop = AddToAnnotsList(&sa, nLeftTopIndex);
jaepark611adb82016-08-17 11:34:36 -0700128
dsinclair8afe15a2016-10-05 12:00:34 -0700129 std::vector<size_t> aSelect;
130 for (size_t i = 0; i < sa.size(); ++i) {
131 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Dan Sinclair05df0752017-03-14 14:43:42 -0400132 float fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f;
dsinclair8afe15a2016-10-05 12:00:34 -0700133 if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top)
134 aSelect.push_back(i);
jaepark611adb82016-08-17 11:34:36 -0700135 }
dsinclair8afe15a2016-10-05 12:00:34 -0700136 AddSelectedToAnnots(&sa, &aSelect);
jaepark611adb82016-08-17 11:34:36 -0700137 }
138 break;
139 }
dsinclair8afe15a2016-10-05 12:00:34 -0700140
jaepark611adb82016-08-17 11:34:36 -0700141 case COLUMN: {
142 std::vector<CPDFSDK_Annot*> sa;
dsinclair8afe15a2016-10-05 12:00:34 -0700143 CollectAnnots(&sa);
jaepark611adb82016-08-17 11:34:36 -0700144 std::sort(sa.begin(), sa.end(), CompareByTopDescending);
dsinclair8afe15a2016-10-05 12:00:34 -0700145
jaepark611adb82016-08-17 11:34:36 -0700146 while (!sa.empty()) {
147 int nLeftTopIndex = -1;
Dan Sinclair05df0752017-03-14 14:43:42 -0400148 float fLeft = -1.0f;
jaepark611adb82016-08-17 11:34:36 -0700149 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 }
dsinclair8afe15a2016-10-05 12:00:34 -0700159 if (nLeftTopIndex < 0)
160 continue;
jaepark611adb82016-08-17 11:34:36 -0700161
dsinclair8afe15a2016-10-05 12:00:34 -0700162 CFX_FloatRect rcLeftTop = AddToAnnotsList(&sa, nLeftTopIndex);
jaepark611adb82016-08-17 11:34:36 -0700163
dsinclair8afe15a2016-10-05 12:00:34 -0700164 std::vector<size_t> aSelect;
165 for (size_t i = 0; i < sa.size(); ++i) {
166 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]);
Dan Sinclair05df0752017-03-14 14:43:42 -0400167 float fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f;
dsinclair8afe15a2016-10-05 12:00:34 -0700168 if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right)
169 aSelect.push_back(i);
jaepark611adb82016-08-17 11:34:36 -0700170 }
dsinclair8afe15a2016-10-05 12:00:34 -0700171 AddSelectedToAnnots(&sa, &aSelect);
jaepark611adb82016-08-17 11:34:36 -0700172 }
173 break;
174 }
175 }
176}