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