K. Moon | 832a694 | 2022-10-31 20:11:31 +0000 | [diff] [blame] | 1 | // Copyright 2017 The PDFium Authors |
tsepez | d805eec | 2017-01-11 14:03:54 -0800 | [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 | |
| 7 | #include "fpdfsdk/cpdfsdk_annotiteration.h" |
| 8 | |
| 9 | #include <algorithm> |
tsepez | d805eec | 2017-01-11 14:03:54 -0800 | [diff] [blame] | 10 | |
| 11 | #include "fpdfsdk/cpdfsdk_annot.h" |
| 12 | #include "fpdfsdk/cpdfsdk_pageview.h" |
| 13 | |
Lei Zhang | 9e4521d | 2022-10-10 18:14:58 +0000 | [diff] [blame] | 14 | // static |
| 15 | CPDFSDK_AnnotIteration CPDFSDK_AnnotIteration::CreateForDrawing( |
| 16 | CPDFSDK_PageView* page_view) { |
| 17 | CPDFSDK_AnnotIteration result(page_view); |
| 18 | return CPDFSDK_AnnotIteration(page_view, /*put_focused_annot_at_end=*/true); |
| 19 | } |
| 20 | |
| 21 | CPDFSDK_AnnotIteration::CPDFSDK_AnnotIteration(CPDFSDK_PageView* page_view) |
| 22 | : CPDFSDK_AnnotIteration(page_view, /*put_focused_annot_at_end=*/false) {} |
| 23 | |
| 24 | CPDFSDK_AnnotIteration::CPDFSDK_AnnotIteration(CPDFSDK_PageView* page_view, |
| 25 | bool put_focused_annot_at_end) { |
Lei Zhang | ad9ec04 | 2022-10-05 22:41:21 +0000 | [diff] [blame] | 26 | // Copying ObservedPtrs is expensive, so do it once at the end. |
Lei Zhang | 9e4521d | 2022-10-10 18:14:58 +0000 | [diff] [blame] | 27 | std::vector<CPDFSDK_Annot*> copied_list = page_view->GetAnnotList(); |
Lei Zhang | ad9ec04 | 2022-10-05 22:41:21 +0000 | [diff] [blame] | 28 | std::stable_sort(copied_list.begin(), copied_list.end(), |
tsepez | d805eec | 2017-01-11 14:03:54 -0800 | [diff] [blame] | 29 | [](const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) { |
| 30 | return p1->GetLayoutOrder() < p2->GetLayoutOrder(); |
| 31 | }); |
| 32 | |
Lei Zhang | 9e4521d | 2022-10-10 18:14:58 +0000 | [diff] [blame] | 33 | CPDFSDK_Annot* pTopMostAnnot = page_view->GetFocusAnnot(); |
tsepez | d805eec | 2017-01-11 14:03:54 -0800 | [diff] [blame] | 34 | if (pTopMostAnnot) { |
Lei Zhang | ad9ec04 | 2022-10-05 22:41:21 +0000 | [diff] [blame] | 35 | auto it = std::find(copied_list.begin(), copied_list.end(), pTopMostAnnot); |
| 36 | if (it != copied_list.end()) { |
| 37 | copied_list.erase(it); |
Lei Zhang | 9e4521d | 2022-10-10 18:14:58 +0000 | [diff] [blame] | 38 | auto insert_it = |
| 39 | put_focused_annot_at_end ? copied_list.end() : copied_list.begin(); |
| 40 | copied_list.insert(insert_it, pTopMostAnnot); |
tsepez | d805eec | 2017-01-11 14:03:54 -0800 | [diff] [blame] | 41 | } |
| 42 | } |
tsepez | d805eec | 2017-01-11 14:03:54 -0800 | [diff] [blame] | 43 | |
Lei Zhang | 9e4521d | 2022-10-10 18:14:58 +0000 | [diff] [blame] | 44 | list_.reserve(copied_list.size()); |
Lei Zhang | ad9ec04 | 2022-10-05 22:41:21 +0000 | [diff] [blame] | 45 | for (auto* pAnnot : copied_list) |
Lei Zhang | 9e4521d | 2022-10-10 18:14:58 +0000 | [diff] [blame] | 46 | list_.emplace_back(pAnnot); |
tsepez | d805eec | 2017-01-11 14:03:54 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Lei Zhang | 0e744a2 | 2020-06-02 00:44:28 +0000 | [diff] [blame] | 49 | CPDFSDK_AnnotIteration::~CPDFSDK_AnnotIteration() = default; |