jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 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 | |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 7 | #include "fpdfsdk/cba_annotiterator.h" |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 8 | |
Dan Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame^] | 9 | #include <algorithm> |
| 10 | |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 11 | #include "core/fpdfapi/page/cpdf_page.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 12 | #include "fpdfsdk/cpdfsdk_annot.h" |
| 13 | #include "fpdfsdk/cpdfsdk_pageview.h" |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 14 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 15 | namespace { |
| 16 | |
| 17 | CFX_FloatRect GetAnnotRect(const CPDFSDK_Annot* pAnnot) { |
| 18 | return pAnnot->GetPDFAnnot()->GetRect(); |
| 19 | } |
| 20 | |
| 21 | bool CompareByLeftAscending(const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 22 | return GetAnnotRect(p1).left < GetAnnotRect(p2).left; |
| 23 | } |
| 24 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 25 | bool CompareByTopDescending(const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 26 | return GetAnnotRect(p1).top > GetAnnotRect(p2).top; |
| 27 | } |
| 28 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 29 | } // namespace |
| 30 | |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 31 | CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView, |
jaepark | 956553e | 2016-08-31 06:49:27 -0700 | [diff] [blame] | 32 | CPDF_Annot::Subtype nAnnotSubtype) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 33 | : m_eTabOrder(STRUCTURE), |
| 34 | m_pPageView(pPageView), |
jaepark | 956553e | 2016-08-31 06:49:27 -0700 | [diff] [blame] | 35 | m_nAnnotSubtype(nAnnotSubtype) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 36 | CPDF_Page* pPDFPage = m_pPageView->GetPDFPage(); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 37 | CFX_ByteString sTabs = pPDFPage->m_pFormDict->GetStringFor("Tabs"); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 38 | if (sTabs == "R") |
| 39 | m_eTabOrder = ROW; |
| 40 | else if (sTabs == "C") |
| 41 | m_eTabOrder = COLUMN; |
| 42 | |
| 43 | GenerateResults(); |
| 44 | } |
| 45 | |
| 46 | CBA_AnnotIterator::~CBA_AnnotIterator() {} |
| 47 | |
| 48 | CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot() { |
| 49 | return m_Annots.empty() ? nullptr : m_Annots.front(); |
| 50 | } |
| 51 | |
| 52 | CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot() { |
| 53 | return m_Annots.empty() ? nullptr : m_Annots.back(); |
| 54 | } |
| 55 | |
| 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 | |
| 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 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 103 | void CBA_AnnotIterator::GenerateResults() { |
| 104 | switch (m_eTabOrder) { |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 105 | case STRUCTURE: |
| 106 | CollectAnnots(&m_Annots); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 107 | break; |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 108 | |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 109 | case ROW: { |
| 110 | std::vector<CPDFSDK_Annot*> sa; |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 111 | CollectAnnots(&sa); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 112 | std::sort(sa.begin(), sa.end(), CompareByLeftAscending); |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 113 | |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 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 | } |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 124 | if (nLeftTopIndex < 0) |
| 125 | continue; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 126 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 127 | CFX_FloatRect rcLeftTop = AddToAnnotsList(&sa, nLeftTopIndex); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 128 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 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); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 135 | } |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 136 | AddSelectedToAnnots(&sa, &aSelect); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 137 | } |
| 138 | break; |
| 139 | } |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 140 | |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 141 | case COLUMN: { |
| 142 | std::vector<CPDFSDK_Annot*> sa; |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 143 | CollectAnnots(&sa); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 144 | std::sort(sa.begin(), sa.end(), CompareByTopDescending); |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 145 | |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 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 | } |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 159 | if (nLeftTopIndex < 0) |
| 160 | continue; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 161 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 162 | CFX_FloatRect rcLeftTop = AddToAnnotsList(&sa, nLeftTopIndex); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 163 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 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); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 170 | } |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 171 | AddSelectedToAnnots(&sa, &aSelect); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 172 | } |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | } |