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