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 | |
| 7 | #include "fpdfsdk/include/cba_annotiterator.h" |
| 8 | |
dsinclair | 8a752ab | 2016-09-29 11:59:54 -0700 | [diff] [blame^] | 9 | #include "core/fpdfapi/fpdf_page/cpdf_page.h" |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 10 | #include "fpdfsdk/include/cpdfsdk_annot.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 11 | #include "fpdfsdk/include/cpdfsdk_pageview.h" |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 12 | |
| 13 | // static |
| 14 | bool CBA_AnnotIterator::CompareByLeftAscending(const CPDFSDK_Annot* p1, |
| 15 | const CPDFSDK_Annot* p2) { |
| 16 | return GetAnnotRect(p1).left < GetAnnotRect(p2).left; |
| 17 | } |
| 18 | |
| 19 | // static |
| 20 | bool CBA_AnnotIterator::CompareByTopDescending(const CPDFSDK_Annot* p1, |
| 21 | const CPDFSDK_Annot* p2) { |
| 22 | return GetAnnotRect(p1).top > GetAnnotRect(p2).top; |
| 23 | } |
| 24 | |
| 25 | CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView, |
jaepark | 956553e | 2016-08-31 06:49:27 -0700 | [diff] [blame] | 26 | CPDF_Annot::Subtype nAnnotSubtype) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 27 | : m_eTabOrder(STRUCTURE), |
| 28 | m_pPageView(pPageView), |
jaepark | 956553e | 2016-08-31 06:49:27 -0700 | [diff] [blame] | 29 | m_nAnnotSubtype(nAnnotSubtype) { |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 30 | CPDF_Page* pPDFPage = m_pPageView->GetPDFPage(); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 31 | CFX_ByteString sTabs = pPDFPage->m_pFormDict->GetStringFor("Tabs"); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 32 | if (sTabs == "R") |
| 33 | m_eTabOrder = ROW; |
| 34 | else if (sTabs == "C") |
| 35 | m_eTabOrder = COLUMN; |
| 36 | |
| 37 | GenerateResults(); |
| 38 | } |
| 39 | |
| 40 | CBA_AnnotIterator::~CBA_AnnotIterator() {} |
| 41 | |
| 42 | CPDFSDK_Annot* CBA_AnnotIterator::GetFirstAnnot() { |
| 43 | return m_Annots.empty() ? nullptr : m_Annots.front(); |
| 44 | } |
| 45 | |
| 46 | CPDFSDK_Annot* CBA_AnnotIterator::GetLastAnnot() { |
| 47 | return m_Annots.empty() ? nullptr : m_Annots.back(); |
| 48 | } |
| 49 | |
| 50 | CPDFSDK_Annot* CBA_AnnotIterator::GetNextAnnot(CPDFSDK_Annot* pAnnot) { |
| 51 | auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot); |
| 52 | if (iter == m_Annots.end()) |
| 53 | return nullptr; |
| 54 | ++iter; |
| 55 | if (iter == m_Annots.end()) |
| 56 | iter = m_Annots.begin(); |
| 57 | return *iter; |
| 58 | } |
| 59 | |
| 60 | CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) { |
| 61 | auto iter = std::find(m_Annots.begin(), m_Annots.end(), pAnnot); |
| 62 | if (iter == m_Annots.end()) |
| 63 | return nullptr; |
| 64 | if (iter == m_Annots.begin()) |
| 65 | iter = m_Annots.end(); |
| 66 | return *(--iter); |
| 67 | } |
| 68 | |
| 69 | void CBA_AnnotIterator::GenerateResults() { |
| 70 | switch (m_eTabOrder) { |
| 71 | case STRUCTURE: { |
| 72 | for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { |
| 73 | CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); |
jaepark | 956553e | 2016-08-31 06:49:27 -0700 | [diff] [blame] | 74 | if (pAnnot->GetAnnotSubtype() == m_nAnnotSubtype && |
jaepark | 9ed9137 | 2016-08-26 16:16:10 -0700 | [diff] [blame] | 75 | !pAnnot->IsSignatureWidget()) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 76 | m_Annots.push_back(pAnnot); |
| 77 | } |
| 78 | break; |
| 79 | } |
| 80 | case ROW: { |
| 81 | std::vector<CPDFSDK_Annot*> sa; |
| 82 | for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { |
| 83 | CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); |
jaepark | 956553e | 2016-08-31 06:49:27 -0700 | [diff] [blame] | 84 | if (pAnnot->GetAnnotSubtype() == m_nAnnotSubtype && |
jaepark | 9ed9137 | 2016-08-26 16:16:10 -0700 | [diff] [blame] | 85 | !pAnnot->IsSignatureWidget()) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 86 | sa.push_back(pAnnot); |
| 87 | } |
| 88 | |
| 89 | std::sort(sa.begin(), sa.end(), CompareByLeftAscending); |
| 90 | while (!sa.empty()) { |
| 91 | int nLeftTopIndex = -1; |
| 92 | FX_FLOAT fTop = 0.0f; |
| 93 | for (int i = sa.size() - 1; i >= 0; i--) { |
| 94 | CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); |
| 95 | if (rcAnnot.top > fTop) { |
| 96 | nLeftTopIndex = i; |
| 97 | fTop = rcAnnot.top; |
| 98 | } |
| 99 | } |
| 100 | if (nLeftTopIndex >= 0) { |
| 101 | CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex]; |
| 102 | CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot); |
| 103 | m_Annots.push_back(pLeftTopAnnot); |
| 104 | sa.erase(sa.begin() + nLeftTopIndex); |
| 105 | |
| 106 | std::vector<int> aSelect; |
| 107 | for (size_t i = 0; i < sa.size(); ++i) { |
| 108 | CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); |
| 109 | FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f; |
| 110 | if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top) |
| 111 | aSelect.push_back(i); |
| 112 | } |
| 113 | for (size_t i = 0; i < aSelect.size(); ++i) |
| 114 | m_Annots.push_back(sa[aSelect[i]]); |
| 115 | |
| 116 | for (int i = aSelect.size() - 1; i >= 0; --i) |
| 117 | sa.erase(sa.begin() + aSelect[i]); |
| 118 | } |
| 119 | } |
| 120 | break; |
| 121 | } |
| 122 | case COLUMN: { |
| 123 | std::vector<CPDFSDK_Annot*> sa; |
| 124 | for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { |
| 125 | CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); |
jaepark | 956553e | 2016-08-31 06:49:27 -0700 | [diff] [blame] | 126 | if (pAnnot->GetAnnotSubtype() == m_nAnnotSubtype && |
jaepark | 9ed9137 | 2016-08-26 16:16:10 -0700 | [diff] [blame] | 127 | !pAnnot->IsSignatureWidget()) |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 128 | sa.push_back(pAnnot); |
| 129 | } |
| 130 | |
| 131 | std::sort(sa.begin(), sa.end(), CompareByTopDescending); |
| 132 | while (!sa.empty()) { |
| 133 | int nLeftTopIndex = -1; |
| 134 | FX_FLOAT fLeft = -1.0f; |
| 135 | for (int i = sa.size() - 1; i >= 0; --i) { |
| 136 | CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); |
| 137 | if (fLeft < 0) { |
| 138 | nLeftTopIndex = 0; |
| 139 | fLeft = rcAnnot.left; |
| 140 | } else if (rcAnnot.left < fLeft) { |
| 141 | nLeftTopIndex = i; |
| 142 | fLeft = rcAnnot.left; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if (nLeftTopIndex >= 0) { |
| 147 | CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex]; |
| 148 | CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot); |
| 149 | m_Annots.push_back(pLeftTopAnnot); |
| 150 | sa.erase(sa.begin() + nLeftTopIndex); |
| 151 | |
| 152 | std::vector<int> aSelect; |
| 153 | for (size_t i = 0; i < sa.size(); ++i) { |
| 154 | CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); |
| 155 | FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f; |
| 156 | if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right) |
| 157 | aSelect.push_back(i); |
| 158 | } |
| 159 | for (size_t i = 0; i < aSelect.size(); ++i) |
| 160 | m_Annots.push_back(sa[aSelect[i]]); |
| 161 | |
| 162 | for (int i = aSelect.size() - 1; i >= 0; --i) |
| 163 | sa.erase(sa.begin() + aSelect[i]); |
| 164 | } |
| 165 | } |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | CFX_FloatRect CBA_AnnotIterator::GetAnnotRect(const CPDFSDK_Annot* pAnnot) { |
jaepark | a1d2111 | 2016-08-25 13:33:34 -0700 | [diff] [blame] | 172 | return pAnnot->GetPDFAnnot()->GetRect(); |
jaepark | 611adb8 | 2016-08-17 11:34:36 -0700 | [diff] [blame] | 173 | } |