1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "annot.h"
18 
19 #include <span>
20 #include <utility>
21 #include <vector>
22 
23 #include "fpdf_annot.h"
24 
25 namespace pdfClient_utils {
26 
GetVisibleAnnots(FPDF_PAGE page,const std::unordered_set<int> & types_to_exclude,std::vector<ScopedFPDFAnnotation> * annots)27 void GetVisibleAnnots(FPDF_PAGE page, const std::unordered_set<int>& types_to_exclude,
28                       std::vector<ScopedFPDFAnnotation>* annots) {
29     const int num_annots = FPDFPage_GetAnnotCount(page);
30     for (int i = 0; i < num_annots; ++i) {
31         ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
32         const FPDF_ANNOTATION_SUBTYPE subtype = FPDFAnnot_GetSubtype(annot.get());
33         // If the subtype is not present in types_to_exclude, add it to annots_ to hide it
34         if (types_to_exclude.find(subtype) == types_to_exclude.end()) {
35             const int annot_flags = FPDFAnnot_GetFlags(annot.get());
36             // Only worry about annots that aren't already hidden.
37             if ((annot_flags & FPDF_ANNOT_FLAG_HIDDEN) == FPDF_ANNOT_FLAG_NONE) {
38                 annots->push_back(std::move(annot));
39             }
40         }
41     }
42 }
43 
GetVisibleAnnotsOfType(FPDF_PAGE page,const std::unordered_set<int> & types,std::vector<ScopedFPDFAnnotation> * annots)44 void GetVisibleAnnotsOfType(FPDF_PAGE page, const std::unordered_set<int>& types,
45                             std::vector<ScopedFPDFAnnotation>* annots) {
46     const int num_annots = FPDFPage_GetAnnotCount(page);
47     for (int i = 0; i < num_annots; ++i) {
48         ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page, i));
49         const FPDF_ANNOTATION_SUBTYPE subtype = FPDFAnnot_GetSubtype(annot.get());
50         if (types.find(subtype) != types.end()) {
51             const int annot_flags = FPDFAnnot_GetFlags(annot.get());
52             // Only worry about annots that aren't already hidden.
53             if ((annot_flags & FPDF_ANNOT_FLAG_HIDDEN) == FPDF_ANNOT_FLAG_NONE) {
54                 annots->push_back(std::move(annot));
55             }
56         }
57     }
58 }
59 
HideAnnots(std::span<const ScopedFPDFAnnotation> annots)60 void HideAnnots(std::span<const ScopedFPDFAnnotation> annots) {
61     for (const auto& annot : annots) {
62         const int annot_flags = FPDFAnnot_GetFlags(annot.get());
63         FPDFAnnot_SetFlags(annot.get(), annot_flags | FPDF_ANNOT_FLAG_HIDDEN);
64     }
65 }
66 
UnhideAnnots(std::span<const ScopedFPDFAnnotation> annots)67 void UnhideAnnots(std::span<const ScopedFPDFAnnotation> annots) {
68     for (const auto& annot : annots) {
69         const int annot_flags = FPDFAnnot_GetFlags(annot.get());
70         FPDFAnnot_SetFlags(annot.get(), annot_flags & ~FPDF_ANNOT_FLAG_HIDDEN);
71     }
72 }
73 
74 }  // namespace pdfClient_utils