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 #ifndef MEDIAPROVIDER_PDF_JNI_PDFCLIENT_ANNOTATION_H_ 18 #define MEDIAPROVIDER_PDF_JNI_PDFCLIENT_ANNOTATION_H_ 19 20 #include <map> 21 22 #include "fpdf_annot.h" 23 #include "fpdfview.h" 24 #include "page_object.h" 25 #include "rect.h" 26 27 using pdfClient::Color; 28 using pdfClient::PageObject; 29 using pdfClient::Rectangle_f; 30 31 namespace pdfClient { 32 // Base class for different type of annotations 33 class Annotation { 34 public: 35 enum class Type { UNKNOWN = 0, FreeText = 1, Highlight = 2, Stamp = 3 }; 36 Annotation(Type type)37 Annotation(Type type) : type_(type) {} 38 virtual ~Annotation() = default; 39 GetType()40 Type GetType() const { return type_; } 41 42 virtual bool PopulateFromPdfiumInstance(FPDF_ANNOTATION fpdf_annot, FPDF_PAGE page) = 0; 43 virtual ScopedFPDFAnnotation CreatePdfiumInstance(FPDF_DOCUMENT document, FPDF_PAGE page) = 0; 44 virtual bool UpdatePdfiumInstance(FPDF_ANNOTATION fpdf_annot, FPDF_DOCUMENT document, 45 FPDF_PAGE page) = 0; 46 47 private: 48 Type type_; 49 }; 50 51 // This class represents a stamp annotation on a page of the pdf document. It doesn't take the 52 // ownership of the pdfium annotation. It takes the ownership of PdfPageObject inside it but not of 53 // underlying pdfium page objects 54 class StampAnnotation : public Annotation { 55 public: StampAnnotation(const Rectangle_f & bounds)56 StampAnnotation(const Rectangle_f& bounds) : Annotation(Type::Stamp) { bounds_ = bounds; } 57 GetBounds()58 Rectangle_f GetBounds() const { return bounds_; } SetBounds(Rectangle_f bounds)59 void SetBounds(Rectangle_f bounds) { bounds_ = bounds; } 60 61 // Return a const reference to the list 62 // Stamp annotation will have the ownership of the page objects inside it 63 std::vector<PageObject*> GetObjects() const; 64 AddObject(std::unique_ptr<PageObject> pageObject)65 void AddObject(std::unique_ptr<PageObject> pageObject) { 66 // Take ownership of the PageObject 67 pageObjects_.push_back(std::move(pageObject)); 68 } 69 RemoveObject(int index)70 void RemoveObject(int index) { 71 auto it = pageObjects_.begin(); 72 std::advance(it, index); 73 pageObjects_.erase(it); 74 } 75 76 bool PopulateFromPdfiumInstance(FPDF_ANNOTATION fpdf_annot, FPDF_PAGE page) override; 77 ScopedFPDFAnnotation CreatePdfiumInstance(FPDF_DOCUMENT document, FPDF_PAGE page) override; 78 bool UpdatePdfiumInstance(FPDF_ANNOTATION fpdf_annot, FPDF_DOCUMENT document, 79 FPDF_PAGE page) override; 80 81 private: 82 Rectangle_f bounds_; 83 std::vector<std::unique_ptr<PageObject>> pageObjects_; 84 }; 85 86 class HighlightAnnotation : public Annotation { 87 public: HighlightAnnotation(const std::vector<Rectangle_f> & bounds)88 HighlightAnnotation(const std::vector<Rectangle_f>& bounds) : Annotation(Type::Highlight) { 89 bounds_ = bounds; 90 } 91 GetBounds()92 std::vector<Rectangle_f> GetBounds() const { return bounds_; } SetBounds(std::vector<Rectangle_f> bounds)93 void SetBounds(std::vector<Rectangle_f> bounds) { bounds_ = bounds; } 94 GetColor()95 Color GetColor() const { return color_; } SetColor(Color color)96 void SetColor(Color color) { color_ = color; } 97 98 bool PopulateFromPdfiumInstance(FPDF_ANNOTATION fpdf_annot, FPDF_PAGE page) override; 99 ScopedFPDFAnnotation CreatePdfiumInstance(FPDF_DOCUMENT document, FPDF_PAGE page) override; 100 bool UpdatePdfiumInstance(FPDF_ANNOTATION fpdf_annot, FPDF_DOCUMENT document, 101 FPDF_PAGE page) override; 102 103 private: 104 std::vector<Rectangle_f> bounds_; 105 Color color_; 106 }; 107 108 class FreeTextAnnotation : public Annotation { 109 public: 110 static constexpr const char* kContents = "Contents"; FreeTextAnnotation(const Rectangle_f & bounds)111 FreeTextAnnotation(const Rectangle_f& bounds) : Annotation(Type::FreeText) { bounds_ = bounds; } 112 GetBounds()113 Rectangle_f GetBounds() const { return bounds_; } SetBounds(Rectangle_f bounds)114 void SetBounds(Rectangle_f bounds) { bounds_ = bounds; } 115 GetTextContent()116 std::wstring GetTextContent() const { return text_content_; } SetTextContent(std::wstring textContent)117 void SetTextContent(std::wstring textContent) { text_content_ = textContent; } 118 GetTextColor()119 Color GetTextColor() const { return text_color_; } SetTextColor(Color color)120 void SetTextColor(Color color) { text_color_ = color; } 121 GetBackgroundColor()122 Color GetBackgroundColor() const { return background_color_; } SetBackgroundColor(Color color)123 void SetBackgroundColor(Color color) { background_color_ = color; } 124 125 bool PopulateFromPdfiumInstance(FPDF_ANNOTATION fpdf_annot, FPDF_PAGE page) override; 126 ScopedFPDFAnnotation CreatePdfiumInstance(FPDF_DOCUMENT document, FPDF_PAGE page) override; 127 bool UpdatePdfiumInstance(FPDF_ANNOTATION fpdf_annot, FPDF_DOCUMENT document, 128 FPDF_PAGE page) override; 129 130 private: 131 Rectangle_f bounds_; 132 std::wstring text_content_; 133 Color text_color_; 134 Color background_color_; 135 static bool GetTextContentFromPdfium(FPDF_ANNOTATION fpdf_annot, unsigned long text_length, 136 std::wstring& text); 137 }; 138 139 } // namespace pdfClient 140 141 #endif