OLD | NEW |
---|---|
(Empty) | |
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/include/cba_annotiterator.h" | |
8 | |
9 #include "core/fpdfapi/fpdf_page/include/cpdf_page.h" | |
10 #include "fpdfsdk/include/cpdfsdk_annot.h" | |
11 #include "fpdfsdk/include/fsdk_mgr.h" | |
12 | |
13 // static | |
14 bool CBA_AnnotIterator::CompareByLeftAscending(const CPDFSDK_Annot* p1, | |
15 const CPDFSDK_Annot* p2) { | |
16 return GetAnnotRect(p1).left < GetAnnotRect(p2).left; | |
17 } | |
18 | |
19 // static | |
20 bool CBA_AnnotIterator::CompareByTopDescending(const CPDFSDK_Annot* p1, | |
21 const CPDFSDK_Annot* p2) { | |
22 return GetAnnotRect(p1).top > GetAnnotRect(p2).top; | |
23 } | |
24 | |
jaepark
2016/08/16 20:21:31
Moved static methods to the top above the construc
| |
25 CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView, | |
26 const CFX_ByteString& sType, | |
27 const CFX_ByteString& sSubType) | |
28 : m_eTabOrder(STRUCTURE), | |
29 m_pPageView(pPageView), | |
30 m_sType(sType), | |
31 m_sSubType(sSubType) { | |
32 CPDF_Page* pPDFPage = m_pPageView->GetPDFPage(); | |
33 CFX_ByteString sTabs = pPDFPage->m_pFormDict->GetStringBy("Tabs"); | |
34 if (sTabs == "R") | |
35 m_eTabOrder = ROW; | |
36 else if (sTabs == "C") | |
37 m_eTabOrder = COLUMN; | |
38 | |
39 GenerateResults(); | |
40 } | |
41 | |
42 CBA_AnnotIterator::~CBA_AnnotIterator() {} | |
43 | |
44 CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot() { | |
45 return m_Annots.empty() ? nullptr : m_Annots.front(); | |
46 } | |
47 | |
48 CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot() { | |
49 return m_Annots.empty() ? nullptr : m_Annots.back(); | |
50 } | |
51 | |
52 CPDFSDK_Annot* CBA_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot) { | |
53 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot); | |
54 if (iter == m_Annots.end()) | |
55 return nullptr; | |
56 ++iter; | |
57 if (iter == m_Annots.end()) | |
58 iter = m_Annots.begin(); | |
59 return *iter; | |
60 } | |
61 | |
62 CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) { | |
63 auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot); | |
64 if (iter == m_Annots.end()) | |
65 return nullptr; | |
66 if (iter == m_Annots.begin()) | |
67 iter = m_Annots.end(); | |
68 return *(--iter); | |
69 } | |
70 | |
71 void CBA_AnnotIterator::GenerateResults() { | |
72 switch (m_eTabOrder) { | |
73 case STRUCTURE: { | |
74 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { | |
75 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); | |
76 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType) | |
77 m_Annots.push_back(pAnnot); | |
78 } | |
79 break; | |
80 } | |
81 case ROW: { | |
82 std::vector<CPDFSDK_Annot*> sa; | |
83 for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { | |
84 CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); | |
85 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType) | |
86 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); | |
126 if (pAnnot->GetType() == m_sType && pAnnot->GetSubType() == m_sSubType) | |
127 sa.push_back(pAnnot); | |
128 } | |
129 | |
130 std::sort(sa.begin(), sa.end(), CompareByTopDescending); | |
131 while (!sa.empty()) { | |
132 int nLeftTopIndex = -1; | |
133 FX_FLOAT fLeft = -1.0f; | |
134 for (int i = sa.size() - 1; i >= 0; --i) { | |
135 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); | |
136 if (fLeft < 0) { | |
137 nLeftTopIndex = 0; | |
138 fLeft = rcAnnot.left; | |
139 } else if (rcAnnot.left < fLeft) { | |
140 nLeftTopIndex = i; | |
141 fLeft = rcAnnot.left; | |
142 } | |
143 } | |
144 | |
145 if (nLeftTopIndex >= 0) { | |
146 CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex]; | |
147 CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot); | |
148 m_Annots.push_back(pLeftTopAnnot); | |
149 sa.erase(sa.begin() + nLeftTopIndex); | |
150 | |
151 std::vector<int> aSelect; | |
152 for (size_t i = 0; i < sa.size(); ++i) { | |
153 CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); | |
154 FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f; | |
155 if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right) | |
156 aSelect.push_back(i); | |
157 } | |
158 for (size_t i = 0; i < aSelect.size(); ++i) | |
159 m_Annots.push_back(sa[aSelect[i]]); | |
160 | |
161 for (int i = aSelect.size() - 1; i >= 0; --i) | |
162 sa.erase(sa.begin() + aSelect[i]); | |
163 } | |
164 } | |
165 break; | |
166 } | |
167 } | |
168 } | |
169 | |
170 CFX_FloatRect CBA_AnnotIterator::GetAnnotRect(const CPDFSDK_Annot* pAnnot) { | |
171 CFX_FloatRect rcAnnot; | |
172 pAnnot->GetPDFAnnot()->GetRect(rcAnnot); | |
173 return rcAnnot; | |
174 } | |
OLD | NEW |