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 | |
Dan Sinclair | cbf76e6 | 2018-03-28 21:00:35 +0000 | [diff] [blame] | 7 | #include "fpdfsdk/cpdfsdk_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" |
Lei Zhang | 8153561 | 2018-10-09 21:15:17 +0000 | [diff] [blame] | 12 | #include "core/fpdfapi/parser/cpdf_dictionary.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 13 | #include "fpdfsdk/cpdfsdk_annot.h" |
| 14 | #include "fpdfsdk/cpdfsdk_pageview.h" |
Lei Zhang | 87e3d7a | 2021-06-17 19:40:28 +0000 | [diff] [blame^] | 15 | #include "third_party/base/containers/contains.h" |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 16 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | CFX_FloatRect GetAnnotRect(const CPDFSDK_Annot* pAnnot) { |
| 20 | return pAnnot->GetPDFAnnot()->GetRect(); |
| 21 | } |
| 22 | |
| 23 | bool CompareByLeftAscending(const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 24 | return GetAnnotRect(p1).left < GetAnnotRect(p2).left; |
| 25 | } |
| 26 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 27 | bool CompareByTopDescending(const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 28 | return GetAnnotRect(p1).top > GetAnnotRect(p2).top; |
| 29 | } |
| 30 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 31 | } // namespace |
| 32 | |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 33 | CPDFSDK_AnnotIterator::CPDFSDK_AnnotIterator( |
| 34 | CPDFSDK_PageView* pPageView, |
| 35 | const std::vector<CPDF_Annot::Subtype>& subtypes_to_iterate) |
Lei Zhang | 7fb895f | 2018-10-09 19:02:24 +0000 | [diff] [blame] | 36 | : m_pPageView(pPageView), |
Neha Gupta | 1eb6ddd | 2020-03-19 08:37:15 +0000 | [diff] [blame] | 37 | m_subtypes(subtypes_to_iterate), |
Lei Zhang | 7fb895f | 2018-10-09 19:02:24 +0000 | [diff] [blame] | 38 | m_eTabOrder(GetTabOrder(pPageView)) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 39 | GenerateResults(); |
| 40 | } |
| 41 | |
Lei Zhang | 8153561 | 2018-10-09 21:15:17 +0000 | [diff] [blame] | 42 | CPDFSDK_AnnotIterator::~CPDFSDK_AnnotIterator() = default; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 43 | |
Dan Sinclair | cbf76e6 | 2018-03-28 21:00:35 +0000 | [diff] [blame] | 44 | CPDFSDK_Annot* CPDFSDK_AnnotIterator::GetFirstAnnot() { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 45 | return m_Annots.empty() ? nullptr : m_Annots.front(); |
| 46 | } |
| 47 | |
Dan Sinclair | cbf76e6 | 2018-03-28 21:00:35 +0000 | [diff] [blame] | 48 | CPDFSDK_Annot* CPDFSDK_AnnotIterator::GetLastAnnot() { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 49 | return m_Annots.empty() ? nullptr : m_Annots.back(); |
| 50 | } |
| 51 | |
Dan Sinclair | cbf76e6 | 2018-03-28 21:00:35 +0000 | [diff] [blame] | 52 | CPDFSDK_Annot* CPDFSDK_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 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()) |
Ankit Kumar | afc869e | 2020-04-07 21:21:51 +0000 | [diff] [blame] | 58 | return nullptr; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 59 | return *iter; |
| 60 | } |
| 61 | |
Dan Sinclair | cbf76e6 | 2018-03-28 21:00:35 +0000 | [diff] [blame] | 62 | CPDFSDK_Annot* CPDFSDK_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 63 | auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot); |
Ankit Kumar | afc869e | 2020-04-07 21:21:51 +0000 | [diff] [blame] | 64 | if (iter == m_Annots.begin() || iter == m_Annots.end()) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 65 | return nullptr; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 66 | return *(--iter); |
| 67 | } |
| 68 | |
Tom Sepez | 00c19bf | 2021-04-05 17:38:15 +0000 | [diff] [blame] | 69 | void CPDFSDK_AnnotIterator::CollectAnnots( |
| 70 | std::vector<UnownedPtr<CPDFSDK_Annot>>* pArray) { |
Lei Zhang | 375c276 | 2017-03-10 14:37:14 -0800 | [diff] [blame] | 71 | for (auto* pAnnot : m_pPageView->GetAnnotList()) { |
Lei Zhang | f245ae6 | 2020-05-15 21:49:46 +0000 | [diff] [blame] | 72 | if (pdfium::Contains(m_subtypes, pAnnot->GetAnnotSubtype()) && |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 73 | !pAnnot->IsSignatureWidget()) { |
Tom Sepez | 00c19bf | 2021-04-05 17:38:15 +0000 | [diff] [blame] | 74 | pArray->emplace_back(pAnnot); |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
Dan Sinclair | cbf76e6 | 2018-03-28 21:00:35 +0000 | [diff] [blame] | 79 | CFX_FloatRect CPDFSDK_AnnotIterator::AddToAnnotsList( |
Tom Sepez | 00c19bf | 2021-04-05 17:38:15 +0000 | [diff] [blame] | 80 | std::vector<UnownedPtr<CPDFSDK_Annot>>* sa, |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 81 | size_t idx) { |
| 82 | CPDFSDK_Annot* pLeftTopAnnot = sa->at(idx); |
| 83 | CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot); |
Tom Sepez | 00c19bf | 2021-04-05 17:38:15 +0000 | [diff] [blame] | 84 | m_Annots.emplace_back(pLeftTopAnnot); |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 85 | sa->erase(sa->begin() + idx); |
| 86 | return rcLeftTop; |
| 87 | } |
| 88 | |
Tom Sepez | 00c19bf | 2021-04-05 17:38:15 +0000 | [diff] [blame] | 89 | void CPDFSDK_AnnotIterator::AddSelectedToAnnots( |
| 90 | std::vector<UnownedPtr<CPDFSDK_Annot>>* sa, |
| 91 | std::vector<size_t>* aSelect) { |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 92 | for (size_t i = 0; i < aSelect->size(); ++i) |
Tom Sepez | 00c19bf | 2021-04-05 17:38:15 +0000 | [diff] [blame] | 93 | m_Annots.emplace_back(sa->at(aSelect->at(i))); |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 94 | |
| 95 | for (int i = aSelect->size() - 1; i >= 0; --i) |
| 96 | sa->erase(sa->begin() + aSelect->at(i)); |
| 97 | } |
| 98 | |
Lei Zhang | b05abe4 | 2021-06-07 19:20:55 +0000 | [diff] [blame] | 99 | // static |
| 100 | CPDFSDK_AnnotIterator::TabOrder CPDFSDK_AnnotIterator::GetTabOrder( |
| 101 | CPDFSDK_PageView* pPageView) { |
| 102 | CPDF_Page* pPDFPage = pPageView->GetPDFPage(); |
| 103 | ByteString sTabs = pPDFPage->GetDict()->GetStringFor("Tabs"); |
| 104 | if (sTabs == "R") |
| 105 | return kRow; |
| 106 | if (sTabs == "C") |
| 107 | return kColumn; |
| 108 | return kStructure; |
| 109 | } |
| 110 | |
Dan Sinclair | cbf76e6 | 2018-03-28 21:00:35 +0000 | [diff] [blame] | 111 | void CPDFSDK_AnnotIterator::GenerateResults() { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 112 | switch (m_eTabOrder) { |
Lei Zhang | b05abe4 | 2021-06-07 19:20:55 +0000 | [diff] [blame] | 113 | case kStructure: |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 114 | CollectAnnots(&m_Annots); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 115 | break; |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 116 | |
Lei Zhang | b05abe4 | 2021-06-07 19:20:55 +0000 | [diff] [blame] | 117 | case kRow: { |
Tom Sepez | 00c19bf | 2021-04-05 17:38:15 +0000 | [diff] [blame] | 118 | std::vector<UnownedPtr<CPDFSDK_Annot>> sa; |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 119 | CollectAnnots(&sa); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 120 | std::sort(sa.begin(), sa.end(), CompareByLeftAscending); |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 121 | |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 122 | while (!sa.empty()) { |
| 123 | int nLeftTopIndex = -1; |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 124 | float fTop = 0.0f; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 125 | for (int i = sa.size() - 1; i >= 0; i--) { |
| 126 | CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); |
| 127 | if (rcAnnot.top > fTop) { |
| 128 | nLeftTopIndex = i; |
| 129 | fTop = rcAnnot.top; |
| 130 | } |
| 131 | } |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 132 | if (nLeftTopIndex < 0) |
| 133 | continue; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 134 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 135 | CFX_FloatRect rcLeftTop = AddToAnnotsList(&sa, nLeftTopIndex); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 136 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 137 | std::vector<size_t> aSelect; |
| 138 | for (size_t i = 0; i < sa.size(); ++i) { |
| 139 | CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 140 | float fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f; |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 141 | if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top) |
| 142 | aSelect.push_back(i); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 143 | } |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 144 | AddSelectedToAnnots(&sa, &aSelect); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 145 | } |
| 146 | break; |
| 147 | } |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 148 | |
Lei Zhang | b05abe4 | 2021-06-07 19:20:55 +0000 | [diff] [blame] | 149 | case kColumn: { |
Tom Sepez | 00c19bf | 2021-04-05 17:38:15 +0000 | [diff] [blame] | 150 | std::vector<UnownedPtr<CPDFSDK_Annot>> sa; |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 151 | CollectAnnots(&sa); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 152 | std::sort(sa.begin(), sa.end(), CompareByTopDescending); |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 153 | |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 154 | while (!sa.empty()) { |
| 155 | int nLeftTopIndex = -1; |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 156 | float fLeft = -1.0f; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 157 | for (int i = sa.size() - 1; i >= 0; --i) { |
| 158 | CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); |
| 159 | if (fLeft < 0) { |
| 160 | nLeftTopIndex = 0; |
| 161 | fLeft = rcAnnot.left; |
| 162 | } else if (rcAnnot.left < fLeft) { |
| 163 | nLeftTopIndex = i; |
| 164 | fLeft = rcAnnot.left; |
| 165 | } |
| 166 | } |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 167 | if (nLeftTopIndex < 0) |
| 168 | continue; |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 169 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 170 | CFX_FloatRect rcLeftTop = AddToAnnotsList(&sa, nLeftTopIndex); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 171 | |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 172 | std::vector<size_t> aSelect; |
| 173 | for (size_t i = 0; i < sa.size(); ++i) { |
| 174 | CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 175 | float fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f; |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 176 | if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right) |
| 177 | aSelect.push_back(i); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 178 | } |
dsinclair | 8afe15a | 2016-10-05 12:00:34 -0700 | [diff] [blame] | 179 | AddSelectedToAnnots(&sa, &aSelect); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 180 | } |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | } |