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