diff options
Diffstat (limited to 'fpdfsdk/cba_annotiterator.cpp')
-rw-r--r-- | fpdfsdk/cba_annotiterator.cpp | 133 |
1 files changed, 67 insertions, 66 deletions
diff --git a/fpdfsdk/cba_annotiterator.cpp b/fpdfsdk/cba_annotiterator.cpp index ef9ab2ad31..a447034cda 100644 --- a/fpdfsdk/cba_annotiterator.cpp +++ b/fpdfsdk/cba_annotiterator.cpp @@ -10,18 +10,22 @@ #include "fpdfsdk/cpdfsdk_annot.h" #include "fpdfsdk/cpdfsdk_pageview.h" -// static -bool CBA_AnnotIterator::CompareByLeftAscending(const CPDFSDK_Annot* p1, - const CPDFSDK_Annot* p2) { +namespace { + +CFX_FloatRect GetAnnotRect(const CPDFSDK_Annot* pAnnot) { + return pAnnot->GetPDFAnnot()->GetRect(); +} + +bool CompareByLeftAscending(const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) { return GetAnnotRect(p1).left < GetAnnotRect(p2).left; } -// static -bool CBA_AnnotIterator::CompareByTopDescending(const CPDFSDK_Annot* p1, - const CPDFSDK_Annot* p2) { +bool CompareByTopDescending(const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) { return GetAnnotRect(p1).top > GetAnnotRect(p2).top; } +} // namespace + CBA_AnnotIterator::CBA_AnnotIterator(CPDFSDK_PageView* pPageView, CPDF_Annot::Subtype nAnnotSubtype) : m_eTabOrder(STRUCTURE), @@ -66,27 +70,45 @@ CPDFSDK_Annot* CBA_AnnotIterator::GetPrevAnnot(CPDFSDK_Annot* pAnnot) { return *(--iter); } +void CBA_AnnotIterator::CollectAnnots(std::vector<CPDFSDK_Annot*>* pArray) { + for (auto pAnnot : m_pPageView->GetAnnotList()) { + if (pAnnot->GetAnnotSubtype() == m_nAnnotSubtype && + !pAnnot->IsSignatureWidget()) { + pArray->push_back(pAnnot); + } + } +} + +CFX_FloatRect CBA_AnnotIterator::AddToAnnotsList( + std::vector<CPDFSDK_Annot*>* sa, + size_t idx) { + CPDFSDK_Annot* pLeftTopAnnot = sa->at(idx); + CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot); + m_Annots.push_back(pLeftTopAnnot); + sa->erase(sa->begin() + idx); + return rcLeftTop; +} + +void CBA_AnnotIterator::AddSelectedToAnnots(std::vector<CPDFSDK_Annot*>* sa, + std::vector<size_t>* aSelect) { + for (size_t i = 0; i < aSelect->size(); ++i) + m_Annots.push_back(sa->at(aSelect->at(i))); + + for (int i = aSelect->size() - 1; i >= 0; --i) + sa->erase(sa->begin() + aSelect->at(i)); +} + void CBA_AnnotIterator::GenerateResults() { switch (m_eTabOrder) { - case STRUCTURE: { - for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { - CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); - if (pAnnot->GetAnnotSubtype() == m_nAnnotSubtype && - !pAnnot->IsSignatureWidget()) - m_Annots.push_back(pAnnot); - } + case STRUCTURE: + CollectAnnots(&m_Annots); break; - } + case ROW: { std::vector<CPDFSDK_Annot*> sa; - for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { - CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); - if (pAnnot->GetAnnotSubtype() == m_nAnnotSubtype && - !pAnnot->IsSignatureWidget()) - sa.push_back(pAnnot); - } - + CollectAnnots(&sa); std::sort(sa.begin(), sa.end(), CompareByLeftAscending); + while (!sa.empty()) { int nLeftTopIndex = -1; FX_FLOAT fTop = 0.0f; @@ -97,38 +119,28 @@ void CBA_AnnotIterator::GenerateResults() { fTop = rcAnnot.top; } } - if (nLeftTopIndex >= 0) { - CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex]; - CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot); - m_Annots.push_back(pLeftTopAnnot); - sa.erase(sa.begin() + nLeftTopIndex); - - std::vector<int> aSelect; - for (size_t i = 0; i < sa.size(); ++i) { - CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); - FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f; - if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top) - aSelect.push_back(i); - } - for (size_t i = 0; i < aSelect.size(); ++i) - m_Annots.push_back(sa[aSelect[i]]); + if (nLeftTopIndex < 0) + continue; + + CFX_FloatRect rcLeftTop = AddToAnnotsList(&sa, nLeftTopIndex); - for (int i = aSelect.size() - 1; i >= 0; --i) - sa.erase(sa.begin() + aSelect[i]); + std::vector<size_t> aSelect; + for (size_t i = 0; i < sa.size(); ++i) { + CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); + FX_FLOAT fCenterY = (rcAnnot.top + rcAnnot.bottom) / 2.0f; + if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top) + aSelect.push_back(i); } + AddSelectedToAnnots(&sa, &aSelect); } break; } + case COLUMN: { std::vector<CPDFSDK_Annot*> sa; - for (size_t i = 0; i < m_pPageView->CountAnnots(); ++i) { - CPDFSDK_Annot* pAnnot = m_pPageView->GetAnnot(i); - if (pAnnot->GetAnnotSubtype() == m_nAnnotSubtype && - !pAnnot->IsSignatureWidget()) - sa.push_back(pAnnot); - } - + CollectAnnots(&sa); std::sort(sa.begin(), sa.end(), CompareByTopDescending); + while (!sa.empty()) { int nLeftTopIndex = -1; FX_FLOAT fLeft = -1.0f; @@ -142,32 +154,21 @@ void CBA_AnnotIterator::GenerateResults() { fLeft = rcAnnot.left; } } + if (nLeftTopIndex < 0) + continue; - if (nLeftTopIndex >= 0) { - CPDFSDK_Annot* pLeftTopAnnot = sa[nLeftTopIndex]; - CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot); - m_Annots.push_back(pLeftTopAnnot); - sa.erase(sa.begin() + nLeftTopIndex); - - std::vector<int> aSelect; - for (size_t i = 0; i < sa.size(); ++i) { - CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); - FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f; - if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right) - aSelect.push_back(i); - } - for (size_t i = 0; i < aSelect.size(); ++i) - m_Annots.push_back(sa[aSelect[i]]); + CFX_FloatRect rcLeftTop = AddToAnnotsList(&sa, nLeftTopIndex); - for (int i = aSelect.size() - 1; i >= 0; --i) - sa.erase(sa.begin() + aSelect[i]); + std::vector<size_t> aSelect; + for (size_t i = 0; i < sa.size(); ++i) { + CFX_FloatRect rcAnnot = GetAnnotRect(sa[i]); + FX_FLOAT fCenterX = (rcAnnot.left + rcAnnot.right) / 2.0f; + if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right) + aSelect.push_back(i); } + AddSelectedToAnnots(&sa, &aSelect); } break; } } } - -CFX_FloatRect CBA_AnnotIterator::GetAnnotRect(const CPDFSDK_Annot* pAnnot) { - return pAnnot->GetPDFAnnot()->GetRect(); -} |