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
11#include "base/strings/string16.h"
12#include "ppapi/cpp/rect.h"
13#include "third_party/pdfium/fpdfsdk/include/fpdfdoc.h"
14#include "third_party/pdfium/fpdfsdk/include/fpdfformfill.h"
15#include "third_party/pdfium/fpdfsdk/include/fpdftext.h"
16
17namespace base {
18class Value;
19}
20
21namespace chrome_pdf {
22
23class PDFiumEngine;
24
25// Wrapper around a page from the document.
26class PDFiumPage {
27 public:
28  PDFiumPage(PDFiumEngine* engine,
29             int i,
30             const pp::Rect& r,
31             bool available);
32  ~PDFiumPage();
33  // Unloads the PDFium data for this page from memory.
34  void Unload();
35  // Gets the FPDF_PAGE for this page, loading and parsing it if necessary.
36  FPDF_PAGE GetPage();
37  //Get the FPDF_PAGE for printing.
38  FPDF_PAGE GetPrintPage();
39  //Close the printing page.
40  void ClosePrintPage();
41
42  // Returns FPDF_TEXTPAGE for the page, loading and parsing it if necessary.
43  FPDF_TEXTPAGE GetTextPage();
44
45  // Returns a DictionaryValue version of the page.
46  base::Value* GetAccessibleContentAsValue(int rotation);
47
48  enum Area {
49    NONSELECTABLE_AREA,
50    TEXT_AREA,
51    WEBLINK_AREA,  // Area is a hyperlink.
52    DOCLINK_AREA,  // Area is a link to a different part of the same document.
53  };
54
55  struct LinkTarget {
56    // We are using std::string here which have a copy contructor.
57    // That prevents us from using union here.
58    std::string url;  // Valid for WEBLINK_AREA only.
59    int page;         // Valid for DOCLINK_AREA only.
60  };
61
62  // Given a point in the document that's in this page, returns its character
63  // index if it's near a character, and also the type of text.
64  // Target is optional. It will be filled in for WEBLINK_AREA or
65  // DOCLINK_AREA only.
66  Area GetCharIndex(const pp::Point& point, int rotation, int* char_index,
67                    LinkTarget* target);
68
69  // Gets the character at the given index.
70  base::char16 GetCharAtIndex(int index);
71
72  // Gets the number of characters in the page.
73  int GetCharCount();
74
75  // Converts from page coordinates to screen coordinates.
76  pp::Rect PageToScreen(const pp::Point& offset,
77                        double zoom,
78                        double left,
79                        double top,
80                        double right,
81                        double bottom,
82                        int rotation);
83
84  int index() const { return index_; }
85  pp::Rect rect() const { return rect_; }
86  void set_rect(const pp::Rect& r) { rect_ = r; }
87  bool available() const { return available_; }
88  void set_available(bool available) { available_ = available; }
89  void set_calculated_links(bool calculated_links) {
90     calculated_links_ = calculated_links;
91  }
92
93 private:
94  // Returns a link index if the given character index is over a link, or -1
95  // otherwise.
96  int GetLink(int char_index, LinkTarget* target);
97  // Returns the link indices if the given rect intersects a link rect, or an
98  // empty vector otherwise.
99  std::vector<int> GetLinks(pp::Rect text_area,
100                            std::vector<LinkTarget>* targets);
101  // Calculate the locations of any links on the page.
102  void CalculateLinks();
103  // Returns link type and target associated with a link. Returns
104  // NONSELECTABLE_AREA if link detection failed.
105  Area GetLinkTarget(FPDF_LINK link, LinkTarget* target);
106  // Returns target associated with a destination.
107  Area GetDestinationTarget(FPDF_DEST destination, LinkTarget* target);
108  // Returns the text in the supplied box as a Value Node
109  base::Value* GetTextBoxAsValue(double page_height, double left, double top,
110                                 double right, double bottom, int rotation);
111  // Helper functions for JSON generation
112  base::Value* CreateTextNode(std::string text);
113  base::Value* CreateURLNode(std::string text, std::string url);
114
115  struct Link {
116    Link();
117    ~Link();
118
119    std::string url;
120    // Bounding rectangles of characters.
121    std::vector<pp::Rect> rects;
122  };
123
124  PDFiumEngine* engine_;
125  FPDF_PAGE page_;
126  FPDF_TEXTPAGE text_page_;
127  int index_;
128  pp::Rect rect_;
129  bool calculated_links_;
130  std::vector<Link> links_;
131  bool available_;
132};
133
134}  // namespace chrome_pdf
135
136#endif  // PDF_PDFIUM_PDFIUM_PAGE_H_
137