blob: 6440193d1ebd0d0abe0a3ed8b1691243952471ec [file] [log] [blame]
K. Moon832a6942022-10-31 20:11:31 +00001// Copyright 2017 The PDFium Authors
tsepezd805eec2017-01-11 14:03:54 -08002// 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>
tsepezd805eec2017-01-11 14:03:54 -080010
11#include "fpdfsdk/cpdfsdk_annot.h"
12#include "fpdfsdk/cpdfsdk_pageview.h"
13
Lei Zhang9e4521d2022-10-10 18:14:58 +000014// static
15CPDFSDK_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
21CPDFSDK_AnnotIteration::CPDFSDK_AnnotIteration(CPDFSDK_PageView* page_view)
22 : CPDFSDK_AnnotIteration(page_view, /*put_focused_annot_at_end=*/false) {}
23
24CPDFSDK_AnnotIteration::CPDFSDK_AnnotIteration(CPDFSDK_PageView* page_view,
25 bool put_focused_annot_at_end) {
Lei Zhangad9ec042022-10-05 22:41:21 +000026 // Copying ObservedPtrs is expensive, so do it once at the end.
Lei Zhang9e4521d2022-10-10 18:14:58 +000027 std::vector<CPDFSDK_Annot*> copied_list = page_view->GetAnnotList();
Lei Zhangad9ec042022-10-05 22:41:21 +000028 std::stable_sort(copied_list.begin(), copied_list.end(),
tsepezd805eec2017-01-11 14:03:54 -080029 [](const CPDFSDK_Annot* p1, const CPDFSDK_Annot* p2) {
30 return p1->GetLayoutOrder() < p2->GetLayoutOrder();
31 });
32
Lei Zhang9e4521d2022-10-10 18:14:58 +000033 CPDFSDK_Annot* pTopMostAnnot = page_view->GetFocusAnnot();
tsepezd805eec2017-01-11 14:03:54 -080034 if (pTopMostAnnot) {
Lei Zhang02c94a32025-04-28 16:16:24 -070035 auto it = std::ranges::find(copied_list, pTopMostAnnot);
Lei Zhangad9ec042022-10-05 22:41:21 +000036 if (it != copied_list.end()) {
37 copied_list.erase(it);
Lei Zhang9e4521d2022-10-10 18:14:58 +000038 auto insert_it =
39 put_focused_annot_at_end ? copied_list.end() : copied_list.begin();
40 copied_list.insert(insert_it, pTopMostAnnot);
tsepezd805eec2017-01-11 14:03:54 -080041 }
42 }
tsepezd805eec2017-01-11 14:03:54 -080043
Lei Zhang9e4521d2022-10-10 18:14:58 +000044 list_.reserve(copied_list.size());
Lei Zhangd0dcedc2025-04-09 10:24:44 -070045 for (auto* pAnnot : copied_list) {
Lei Zhang9e4521d2022-10-10 18:14:58 +000046 list_.emplace_back(pAnnot);
Lei Zhangd0dcedc2025-04-09 10:24:44 -070047 }
tsepezd805eec2017-01-11 14:03:54 -080048}
49
Lei Zhang0e744a22020-06-02 00:44:28 +000050CPDFSDK_AnnotIteration::~CPDFSDK_AnnotIteration() = default;