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