[go: nahoru, domu]

blob: 7cc60e71fdd5053c7f654d22559940b28e4a988f [file] [log] [blame]
jam@chromium.org1b1e9eff2014-05-20 01:56:401// 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 Nakashima9d9e0632017-10-06 21:38:1811#include "base/optional.h"
jam@chromium.org1b1e9eff2014-05-20 01:56:4012#include "base/strings/string16.h"
13#include "ppapi/cpp/rect.h"
tsepez350243562015-05-12 01:08:4514#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.org1b1e9eff2014-05-20 01:56:4017
jam@chromium.org1b1e9eff2014-05-20 01:56:4018namespace chrome_pdf {
19
20class PDFiumEngine;
21
22// Wrapper around a page from the document.
23class PDFiumPage {
24 public:
thestig98913ba2017-04-21 19:03:2525 PDFiumPage(PDFiumEngine* engine, int i, const pp::Rect& r, bool available);
thestigfa6edbc72016-08-23 08:07:0026 PDFiumPage(const PDFiumPage& that);
jam@chromium.org1b1e9eff2014-05-20 01:56:4027 ~PDFiumPage();
thestigccb5fc8f2016-01-05 05:32:5128
jam@chromium.org1b1e9eff2014-05-20 01:56:4029 // 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();
thestig03ac42d2014-12-20 16:04:0033 // Get the FPDF_PAGE for printing.
jam@chromium.org1b1e9eff2014-05-20 01:56:4034 FPDF_PAGE GetPrintPage();
thestig03ac42d2014-12-20 16:04:0035 // Close the printing page.
jam@chromium.org1b1e9eff2014-05-20 01:56:4036 void ClosePrintPage();
37
38 // Returns FPDF_TEXTPAGE for the page, loading and parsing it if necessary.
39 FPDF_TEXTPAGE GetTextPage();
40
dmazzonic3547a32016-06-02 05:47:1541 // 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);
dmazzonid48d9322016-06-13 19:37:4250 // Get the bounds of a character in page pixels.
51 pp::FloatRect GetCharBounds(int char_index);
dmazzonic3547a32016-06-02 05:47:1552
jam@chromium.org1b1e9eff2014-05-20 01:56:4053 enum Area {
54 NONSELECTABLE_AREA,
drgagec32fae262017-06-24 00:17:4955 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.org1b1e9eff2014-05-20 01:56:4062 };
63
64 struct LinkTarget {
Henrique Nakashima9d9e0632017-10-06 21:38:1865 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.org1b1e9eff2014-05-20 01:56:4076 };
77
Henrique Nakashima9d9e0632017-10-06 21:38:1878 // Fills |y_in_pixels| of a destination into |target|.
79 // |target| is required.
80 void GetPageYTarget(FPDF_DEST destination, LinkTarget* target);
81
jam@chromium.org1b1e9eff2014-05-20 01:56:4082 // 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.
thestig98913ba2017-04-21 19:03:2586 Area GetCharIndex(const pp::Point& point,
87 int rotation,
88 int* char_index,
89 int* form_type,
90 LinkTarget* target);
jam@chromium.org1b1e9eff2014-05-20 01:56:4091
drgagec32fae262017-06-24 00:17:4992 // Converts a form type to its corresponding Area.
93 static Area FormTypeToArea(int form_type);
94
jam@chromium.org1b1e9eff2014-05-20 01:56:4095 // 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,
thestigccb5fc8f2016-01-05 05:32:51108 int rotation) const;
jam@chromium.org1b1e9eff2014-05-20 01:56:40109
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) {
thestig98913ba2017-04-21 19:03:25116 calculated_links_ = calculated_links;
jam@chromium.org1b1e9eff2014-05-20 01:56:40117 }
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 Nakashima9d9e0632017-10-06 21:38:18129 // Returns link type and fills target associated with a link. Returns
jam@chromium.org1b1e9eff2014-05-20 01:56:40130 // NONSELECTABLE_AREA if link detection failed.
Henrique Nakashima9d9e0632017-10-06 21:38:18131 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.
thestig139fa7482017-06-27 19:51:33137 Area GetURITarget(FPDF_ACTION uri_action, LinkTarget* target) const;
jam@chromium.org1b1e9eff2014-05-20 01:56:40138
thestig03ac42d2014-12-20 16:04:00139 class ScopedLoadCounter {
140 public:
141 explicit ScopedLoadCounter(PDFiumPage* page);
142 ~ScopedLoadCounter();
143
144 private:
145 PDFiumPage* const page_;
146 };
147
jam@chromium.org1b1e9eff2014-05-20 01:56:40148 struct Link {
149 Link();
thestigfa6edbc72016-08-23 08:07:00150 Link(const Link& that);
jam@chromium.org1b1e9eff2014-05-20 01:56:40151 ~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_;
thestig03ac42d2014-12-20 16:04:00162 int loading_count_;
jam@chromium.org1b1e9eff2014-05-20 01:56:40163 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_