1 // Copyright 2017 The PDFium Authors
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>
10
11 #include "fpdfsdk/cpdfsdk_annot.h"
12 #include "fpdfsdk/cpdfsdk_pageview.h"
13
14 // static
CreateForDrawing(CPDFSDK_PageView * page_view)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
CPDFSDK_AnnotIteration(CPDFSDK_PageView * page_view)21 CPDFSDK_AnnotIteration::CPDFSDK_AnnotIteration(CPDFSDK_PageView* page_view)
22 : CPDFSDK_AnnotIteration(page_view, /*put_focused_annot_at_end=*/false) {}
23
CPDFSDK_AnnotIteration(CPDFSDK_PageView * page_view,bool put_focused_annot_at_end)24 CPDFSDK_AnnotIteration::CPDFSDK_AnnotIteration(CPDFSDK_PageView* page_view,
25 bool put_focused_annot_at_end) {
26 // Copying ObservedPtrs is expensive, so do it once at the end.
27 std::vector<CPDFSDK_Annot*> copied_list = page_view->GetAnnotList();
28 std::stable_sort(copied_list.begin(), copied_list.end(),
29 [](const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) {
30 return p1->GetLayoutOrder() < p2->GetLayoutOrder();
31 });
32
33 CPDFSDK_Annot* pTopMostAnnot = page_view->GetFocusAnnot();
34 if (pTopMostAnnot) {
35 auto it = std::find(copied_list.begin(), copied_list.end(), pTopMostAnnot);
36 if (it != copied_list.end()) {
37 copied_list.erase(it);
38 auto insert_it =
39 put_focused_annot_at_end ? copied_list.end() : copied_list.begin();
40 copied_list.insert(insert_it, pTopMostAnnot);
41 }
42 }
43
44 list_.reserve(copied_list.size());
45 for (auto* pAnnot : copied_list)
46 list_.emplace_back(pAnnot);
47 }
48
49 CPDFSDK_AnnotIteration::~CPDFSDK_AnnotIteration() = default;
50