jaepark | 98e1019 | 2016-08-15 10:51:11 -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/cpdfsdk_annotiterator.h" |
| 8 | |
| 9 | #include <algorithm> |
| 10 | |
| 11 | #include "fpdfsdk/include/cpdfsdk_annot.h" |
| 12 | #include "fpdfsdk/include/fsdk_mgr.h" |
| 13 | |
| 14 | CPDFSDK_AnnotIterator::CPDFSDK_AnnotIterator(CPDFSDK_PageView* pPageView, |
| 15 | bool bReverse) |
| 16 | : m_bReverse(bReverse), m_pos(0) { |
| 17 | const std::vector<CPDFSDK_Annot*>& annots = pPageView->GetAnnotList(); |
| 18 | m_iteratorAnnotList.insert(m_iteratorAnnotList.begin(), annots.rbegin(), |
| 19 | annots.rend()); |
| 20 | std::stable_sort(m_iteratorAnnotList.begin(), m_iteratorAnnotList.end(), |
| 21 | [](CPDFSDK_Annot* p1, CPDFSDK_Annot* p2) { |
| 22 | return p1->GetLayoutOrder() < p2->GetLayoutOrder(); |
| 23 | }); |
| 24 | |
| 25 | CPDFSDK_Annot* pTopMostAnnot = pPageView->GetFocusAnnot(); |
| 26 | if (!pTopMostAnnot) |
| 27 | return; |
| 28 | |
| 29 | auto it = std::find(m_iteratorAnnotList.begin(), m_iteratorAnnotList.end(), |
| 30 | pTopMostAnnot); |
| 31 | if (it != m_iteratorAnnotList.end()) { |
| 32 | CPDFSDK_Annot* pReaderAnnot = *it; |
| 33 | m_iteratorAnnotList.erase(it); |
| 34 | m_iteratorAnnotList.insert(m_iteratorAnnotList.begin(), pReaderAnnot); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | CPDFSDK_AnnotIterator::~CPDFSDK_AnnotIterator() {} |
| 39 | |
| 40 | CPDFSDK_Annot* CPDFSDK_AnnotIterator::NextAnnot() { |
| 41 | if (m_pos < m_iteratorAnnotList.size()) |
| 42 | return m_iteratorAnnotList[m_pos++]; |
| 43 | return nullptr; |
| 44 | } |
| 45 | |
| 46 | CPDFSDK_Annot* CPDFSDK_AnnotIterator::PrevAnnot() { |
| 47 | if (m_pos < m_iteratorAnnotList.size()) |
| 48 | return m_iteratorAnnotList[m_iteratorAnnotList.size() - ++m_pos]; |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | CPDFSDK_Annot* CPDFSDK_AnnotIterator::Next() { |
| 53 | return m_bReverse ? PrevAnnot() : NextAnnot(); |
| 54 | } |