jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef PDF_PDFIUM_PDFIUM_PAGE_H_ |
| 6 | #define PDF_PDFIUM_PDFIUM_PAGE_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame^] | 11 | #include "base/optional.h" |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 12 | #include "base/strings/string16.h" |
| 13 | #include "ppapi/cpp/rect.h" |
tsepez | 35024356 | 2015-05-12 01:08:45 | [diff] [blame] | 14 | #include "third_party/pdfium/public/fpdf_doc.h" |
| 15 | #include "third_party/pdfium/public/fpdf_formfill.h" |
| 16 | #include "third_party/pdfium/public/fpdf_text.h" |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 17 | |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 18 | namespace chrome_pdf { |
| 19 | |
| 20 | class PDFiumEngine; |
| 21 | |
| 22 | // Wrapper around a page from the document. |
| 23 | class PDFiumPage { |
| 24 | public: |
thestig | 98913ba | 2017-04-21 19:03:25 | [diff] [blame] | 25 | PDFiumPage(PDFiumEngine* engine, int i, const pp::Rect& r, bool available); |
thestig | fa6edbc7 | 2016-08-23 08:07:00 | [diff] [blame] | 26 | PDFiumPage(const PDFiumPage& that); |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 27 | ~PDFiumPage(); |
thestig | ccb5fc8f | 2016-01-05 05:32:51 | [diff] [blame] | 28 | |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 29 | // Unloads the PDFium data for this page from memory. |
| 30 | void Unload(); |
| 31 | // Gets the FPDF_PAGE for this page, loading and parsing it if necessary. |
| 32 | FPDF_PAGE GetPage(); |
thestig | 03ac42d | 2014-12-20 16:04:00 | [diff] [blame] | 33 | // Get the FPDF_PAGE for printing. |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 34 | FPDF_PAGE GetPrintPage(); |
thestig | 03ac42d | 2014-12-20 16:04:00 | [diff] [blame] | 35 | // Close the printing page. |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 36 | void ClosePrintPage(); |
| 37 | |
| 38 | // Returns FPDF_TEXTPAGE for the page, loading and parsing it if necessary. |
| 39 | FPDF_TEXTPAGE GetTextPage(); |
| 40 | |
dmazzoni | c3547a3 | 2016-06-02 05:47:15 | [diff] [blame] | 41 | // Given a start char index, find the longest continuous run of text that's |
| 42 | // in a single direction and with the same style and font size. Return the |
| 43 | // length of that sequence and its font size and bounding box. |
| 44 | void GetTextRunInfo(int start_char_index, |
| 45 | uint32_t* out_len, |
| 46 | double* out_font_size, |
| 47 | pp::FloatRect* out_bounds); |
| 48 | // Get a unicode character from the page. |
| 49 | uint32_t GetCharUnicode(int char_index); |
dmazzoni | d48d932 | 2016-06-13 19:37:42 | [diff] [blame] | 50 | // Get the bounds of a character in page pixels. |
| 51 | pp::FloatRect GetCharBounds(int char_index); |
dmazzoni | c3547a3 | 2016-06-02 05:47:15 | [diff] [blame] | 52 | |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 53 | enum Area { |
| 54 | NONSELECTABLE_AREA, |
drgage | c32fae26 | 2017-06-24 00:17:49 | [diff] [blame] | 55 | TEXT_AREA, // Area contains regular, selectable text not |
| 56 | // within form fields. |
| 57 | WEBLINK_AREA, // Area is a hyperlink. |
| 58 | DOCLINK_AREA, // Area is a link to a different part of the same |
| 59 | // document. |
| 60 | FORM_TEXT_AREA, // Area is a form text field or form combobox text |
| 61 | // field. |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | struct LinkTarget { |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame^] | 65 | LinkTarget(); |
| 66 | LinkTarget(const LinkTarget& other); |
| 67 | ~LinkTarget(); |
| 68 | |
| 69 | // Valid for WEBLINK_AREA only. |
| 70 | std::string url; |
| 71 | |
| 72 | // Valid for DOCLINK_AREA only. |
| 73 | int page; |
| 74 | // Valid for DOCLINK_AREA only. From the top of the page. |
| 75 | base::Optional<int> y_in_pixels; |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 76 | }; |
| 77 | |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame^] | 78 | // Fills |y_in_pixels| of a destination into |target|. |
| 79 | // |target| is required. |
| 80 | void GetPageYTarget(FPDF_DEST destination, LinkTarget* target); |
| 81 | |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 82 | // Given a point in the document that's in this page, returns its character |
| 83 | // index if it's near a character, and also the type of text. |
| 84 | // Target is optional. It will be filled in for WEBLINK_AREA or |
| 85 | // DOCLINK_AREA only. |
thestig | 98913ba | 2017-04-21 19:03:25 | [diff] [blame] | 86 | Area GetCharIndex(const pp::Point& point, |
| 87 | int rotation, |
| 88 | int* char_index, |
| 89 | int* form_type, |
| 90 | LinkTarget* target); |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 91 | |
drgage | c32fae26 | 2017-06-24 00:17:49 | [diff] [blame] | 92 | // Converts a form type to its corresponding Area. |
| 93 | static Area FormTypeToArea(int form_type); |
| 94 | |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 95 | // Gets the character at the given index. |
| 96 | base::char16 GetCharAtIndex(int index); |
| 97 | |
| 98 | // Gets the number of characters in the page. |
| 99 | int GetCharCount(); |
| 100 | |
| 101 | // Converts from page coordinates to screen coordinates. |
| 102 | pp::Rect PageToScreen(const pp::Point& offset, |
| 103 | double zoom, |
| 104 | double left, |
| 105 | double top, |
| 106 | double right, |
| 107 | double bottom, |
thestig | ccb5fc8f | 2016-01-05 05:32:51 | [diff] [blame] | 108 | int rotation) const; |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 109 | |
| 110 | int index() const { return index_; } |
| 111 | pp::Rect rect() const { return rect_; } |
| 112 | void set_rect(const pp::Rect& r) { rect_ = r; } |
| 113 | bool available() const { return available_; } |
| 114 | void set_available(bool available) { available_ = available; } |
| 115 | void set_calculated_links(bool calculated_links) { |
thestig | 98913ba | 2017-04-21 19:03:25 | [diff] [blame] | 116 | calculated_links_ = calculated_links; |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | private: |
| 120 | // Returns a link index if the given character index is over a link, or -1 |
| 121 | // otherwise. |
| 122 | int GetLink(int char_index, LinkTarget* target); |
| 123 | // Returns the link indices if the given rect intersects a link rect, or an |
| 124 | // empty vector otherwise. |
| 125 | std::vector<int> GetLinks(pp::Rect text_area, |
| 126 | std::vector<LinkTarget>* targets); |
| 127 | // Calculate the locations of any links on the page. |
| 128 | void CalculateLinks(); |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame^] | 129 | // Returns link type and fills target associated with a link. Returns |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 130 | // NONSELECTABLE_AREA if link detection failed. |
Henrique Nakashima | 9d9e063 | 2017-10-06 21:38:18 | [diff] [blame^] | 131 | Area GetLinkTarget(FPDF_LINK link, LinkTarget* target); |
| 132 | // Returns link type and fills target associated with a destination. Returns |
| 133 | // NONSELECTABLE_AREA if detection failed. |
| 134 | Area GetDestinationTarget(FPDF_DEST destination, LinkTarget* target); |
| 135 | // Returns link type and fills target associated with a URI action. Returns |
| 136 | // NONSELECTABLE_AREA if detection failed. |
thestig | 139fa748 | 2017-06-27 19:51:33 | [diff] [blame] | 137 | Area GetURITarget(FPDF_ACTION uri_action, LinkTarget* target) const; |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 138 | |
thestig | 03ac42d | 2014-12-20 16:04:00 | [diff] [blame] | 139 | class ScopedLoadCounter { |
| 140 | public: |
| 141 | explicit ScopedLoadCounter(PDFiumPage* page); |
| 142 | ~ScopedLoadCounter(); |
| 143 | |
| 144 | private: |
| 145 | PDFiumPage* const page_; |
| 146 | }; |
| 147 | |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 148 | struct Link { |
| 149 | Link(); |
thestig | fa6edbc7 | 2016-08-23 08:07:00 | [diff] [blame] | 150 | Link(const Link& that); |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 151 | ~Link(); |
| 152 | |
| 153 | std::string url; |
| 154 | // Bounding rectangles of characters. |
| 155 | std::vector<pp::Rect> rects; |
| 156 | }; |
| 157 | |
| 158 | PDFiumEngine* engine_; |
| 159 | FPDF_PAGE page_; |
| 160 | FPDF_TEXTPAGE text_page_; |
| 161 | int index_; |
thestig | 03ac42d | 2014-12-20 16:04:00 | [diff] [blame] | 162 | int loading_count_; |
jam@chromium.org | 1b1e9eff | 2014-05-20 01:56:40 | [diff] [blame] | 163 | pp::Rect rect_; |
| 164 | bool calculated_links_; |
| 165 | std::vector<Link> links_; |
| 166 | bool available_; |
| 167 | }; |
| 168 | |
| 169 | } // namespace chrome_pdf |
| 170 | |
| 171 | #endif // PDF_PDFIUM_PDFIUM_PAGE_H_ |