Sort unlaunched apps on app list start page by apps grid order.
[chromium-blink-merge.git] / pdf / pdfium / pdfium_page.cc
blob1335f0780cf211ceb08d4c237eab05c6ed2c2d79
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.
5 #include "pdf/pdfium/pdfium_page.h"
7 #include <math.h>
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "pdf/pdfium/pdfium_api_string_buffer_adapter.h"
15 #include "pdf/pdfium/pdfium_engine.h"
17 // Used when doing hit detection.
18 #define kTolerance 20.0
20 namespace {
22 // Dictionary Value key names for returning the accessible page content as JSON.
23 const char kPageWidth[] = "width";
24 const char kPageHeight[] = "height";
25 const char kPageTextBox[] = "textBox";
26 const char kTextBoxLeft[] = "left";
27 const char kTextBoxTop[] = "top";
28 const char kTextBoxWidth[] = "width";
29 const char kTextBoxHeight[] = "height";
30 const char kTextBoxFontSize[] = "fontSize";
31 const char kTextBoxNodes[] = "textNodes";
32 const char kTextNodeType[] = "type";
33 const char kTextNodeText[] = "text";
34 const char kTextNodeURL[] = "url";
35 const char kTextNodeTypeText[] = "text";
36 const char kTextNodeTypeURL[] = "url";
37 const char kDocLinkURLPrefix[] = "#page";
39 } // namespace
41 namespace chrome_pdf {
43 PDFiumPage::PDFiumPage(PDFiumEngine* engine,
44 int i,
45 const pp::Rect& r,
46 bool available)
47 : engine_(engine),
48 page_(NULL),
49 text_page_(NULL),
50 index_(i),
51 loading_count_(0),
52 rect_(r),
53 calculated_links_(false),
54 available_(available) {
57 PDFiumPage::~PDFiumPage() {
58 DCHECK_EQ(0, loading_count_);
61 void PDFiumPage::Unload() {
62 // Do not unload while in the middle of a load.
63 if (loading_count_)
64 return;
66 if (text_page_) {
67 FPDFText_ClosePage(text_page_);
68 text_page_ = NULL;
71 if (page_) {
72 if (engine_->form()) {
73 FORM_OnBeforeClosePage(page_, engine_->form());
75 FPDF_ClosePage(page_);
76 page_ = NULL;
80 FPDF_PAGE PDFiumPage::GetPage() {
81 ScopedUnsupportedFeature scoped_unsupported_feature(engine_);
82 if (!available_)
83 return NULL;
84 if (!page_) {
85 ScopedLoadCounter scoped_load(this);
86 page_ = FPDF_LoadPage(engine_->doc(), index_);
87 if (page_ && engine_->form()) {
88 FORM_OnAfterLoadPage(page_, engine_->form());
91 return page_;
94 FPDF_PAGE PDFiumPage::GetPrintPage() {
95 ScopedUnsupportedFeature scoped_unsupported_feature(engine_);
96 if (!available_)
97 return NULL;
98 if (!page_) {
99 ScopedLoadCounter scoped_load(this);
100 page_ = FPDF_LoadPage(engine_->doc(), index_);
102 return page_;
105 void PDFiumPage::ClosePrintPage() {
106 // Do not close |page_| while in the middle of a load.
107 if (loading_count_)
108 return;
110 if (page_) {
111 FPDF_ClosePage(page_);
112 page_ = NULL;
116 FPDF_TEXTPAGE PDFiumPage::GetTextPage() {
117 if (!available_)
118 return NULL;
119 if (!text_page_) {
120 ScopedLoadCounter scoped_load(this);
121 text_page_ = FPDFText_LoadPage(GetPage());
123 return text_page_;
126 base::Value* PDFiumPage::GetAccessibleContentAsValue(int rotation) {
127 base::DictionaryValue* node = new base::DictionaryValue();
129 if (!available_)
130 return node;
132 double width = FPDF_GetPageWidth(GetPage());
133 double height = FPDF_GetPageHeight(GetPage());
135 base::ListValue* text = new base::ListValue();
136 int box_count = FPDFText_CountRects(GetTextPage(), 0, GetCharCount());
137 for (int i = 0; i < box_count; i++) {
138 double left, top, right, bottom;
139 FPDFText_GetRect(GetTextPage(), i, &left, &top, &right, &bottom);
140 text->Append(
141 GetTextBoxAsValue(height, left, top, right, bottom, rotation));
144 node->SetDouble(kPageWidth, width);
145 node->SetDouble(kPageHeight, height);
146 node->Set(kPageTextBox, text); // Takes ownership of |text|
148 return node;
151 base::Value* PDFiumPage::GetTextBoxAsValue(double page_height,
152 double left, double top,
153 double right, double bottom,
154 int rotation) {
155 base::string16 text_utf16;
156 int char_count =
157 FPDFText_GetBoundedText(GetTextPage(), left, top, right, bottom, NULL, 0);
158 if (char_count > 0) {
159 unsigned short* data = reinterpret_cast<unsigned short*>(
160 WriteInto(&text_utf16, char_count + 1));
161 FPDFText_GetBoundedText(GetTextPage(),
162 left, top, right, bottom,
163 data, char_count);
165 std::string text_utf8 = base::UTF16ToUTF8(text_utf16);
167 FPDF_LINK link = FPDFLink_GetLinkAtPoint(GetPage(), left, top);
168 Area area;
169 std::vector<LinkTarget> targets;
170 if (link) {
171 targets.push_back(LinkTarget());
172 area = GetLinkTarget(link, &targets[0]);
173 } else {
174 pp::Rect rect(
175 PageToScreen(pp::Point(), 1.0, left, top, right, bottom, rotation));
176 GetLinks(rect, &targets);
177 area = targets.size() == 0 ? TEXT_AREA : WEBLINK_AREA;
180 int char_index = FPDFText_GetCharIndexAtPos(GetTextPage(), left, top,
181 kTolerance, kTolerance);
182 double font_size = FPDFText_GetFontSize(GetTextPage(), char_index);
184 base::DictionaryValue* node = new base::DictionaryValue();
185 node->SetDouble(kTextBoxLeft, left);
186 node->SetDouble(kTextBoxTop, page_height - top);
187 node->SetDouble(kTextBoxWidth, right - left);
188 node->SetDouble(kTextBoxHeight, top - bottom);
189 node->SetDouble(kTextBoxFontSize, font_size);
191 base::ListValue* text_nodes = new base::ListValue();
193 if (area == DOCLINK_AREA) {
194 std::string url = kDocLinkURLPrefix + base::IntToString(targets[0].page);
195 text_nodes->Append(CreateURLNode(text_utf8, url));
196 } else if (area == WEBLINK_AREA && link) {
197 text_nodes->Append(CreateURLNode(text_utf8, targets[0].url));
198 } else if (area == WEBLINK_AREA && !link) {
199 size_t start = 0;
200 for (size_t i = 0; i < targets.size(); ++i) {
201 // If there is an extra NULL character at end, find() will not return any
202 // matches. There should not be any though.
203 if (!targets[i].url.empty())
204 DCHECK(targets[i].url[targets[i].url.size() - 1] != '\0');
206 // PDFium may change the case of generated links.
207 std::string lowerCaseURL = base::StringToLowerASCII(targets[i].url);
208 std::string lowerCaseText = base::StringToLowerASCII(text_utf8);
209 size_t pos = lowerCaseText.find(lowerCaseURL, start);
210 size_t length = targets[i].url.size();
211 if (pos == std::string::npos) {
212 // Check if the link is a "mailto:" URL
213 if (lowerCaseURL.compare(0, 7, "mailto:") == 0) {
214 pos = lowerCaseText.find(lowerCaseURL.substr(7), start);
215 length -= 7;
218 if (pos == std::string::npos) {
219 // No match has been found. This should never happen.
220 continue;
224 std::string before_text = text_utf8.substr(start, pos - start);
225 if (before_text.size() > 0)
226 text_nodes->Append(CreateTextNode(before_text));
227 std::string link_text = text_utf8.substr(pos, length);
228 text_nodes->Append(CreateURLNode(link_text, targets[i].url));
230 start = pos + length;
232 std::string before_text = text_utf8.substr(start);
233 if (before_text.size() > 0)
234 text_nodes->Append(CreateTextNode(before_text));
235 } else {
236 text_nodes->Append(CreateTextNode(text_utf8));
239 node->Set(kTextBoxNodes, text_nodes); // Takes ownership of |text_nodes|.
240 return node;
243 base::Value* PDFiumPage::CreateTextNode(std::string text) {
244 base::DictionaryValue* node = new base::DictionaryValue();
245 node->SetString(kTextNodeType, kTextNodeTypeText);
246 node->SetString(kTextNodeText, text);
247 return node;
250 base::Value* PDFiumPage::CreateURLNode(std::string text, std::string url) {
251 base::DictionaryValue* node = new base::DictionaryValue();
252 node->SetString(kTextNodeType, kTextNodeTypeURL);
253 node->SetString(kTextNodeText, text);
254 node->SetString(kTextNodeURL, url);
255 return node;
258 PDFiumPage::Area PDFiumPage::GetCharIndex(const pp::Point& point,
259 int rotation,
260 int* char_index,
261 LinkTarget* target) {
262 if (!available_)
263 return NONSELECTABLE_AREA;
264 pp::Point point2 = point - rect_.point();
265 double new_x, new_y;
266 FPDF_DeviceToPage(GetPage(), 0, 0, rect_.width(), rect_.height(),
267 rotation, point2.x(), point2.y(), &new_x, &new_y);
269 int rv = FPDFText_GetCharIndexAtPos(
270 GetTextPage(), new_x, new_y, kTolerance, kTolerance);
271 *char_index = rv;
273 FPDF_LINK link = FPDFLink_GetLinkAtPoint(GetPage(), new_x, new_y);
274 if (link) {
275 // We don't handle all possible link types of the PDF. For example,
276 // launch actions, cross-document links, etc.
277 // In that case, GetLinkTarget() will return NONSELECTABLE_AREA
278 // and we should proceed with area detection.
279 PDFiumPage::Area area = GetLinkTarget(link, target);
280 if (area != PDFiumPage::NONSELECTABLE_AREA)
281 return area;
284 if (rv < 0)
285 return NONSELECTABLE_AREA;
287 return GetLink(*char_index, target) != -1 ? WEBLINK_AREA : TEXT_AREA;
290 base::char16 PDFiumPage::GetCharAtIndex(int index) {
291 if (!available_)
292 return L'\0';
293 return static_cast<base::char16>(FPDFText_GetUnicode(GetTextPage(), index));
296 int PDFiumPage::GetCharCount() {
297 if (!available_)
298 return 0;
299 return FPDFText_CountChars(GetTextPage());
302 PDFiumPage::Area PDFiumPage::GetLinkTarget(
303 FPDF_LINK link, PDFiumPage::LinkTarget* target) {
304 FPDF_DEST dest = FPDFLink_GetDest(engine_->doc(), link);
305 if (dest != NULL)
306 return GetDestinationTarget(dest, target);
308 FPDF_ACTION action = FPDFLink_GetAction(link);
309 if (action) {
310 switch (FPDFAction_GetType(action)) {
311 case PDFACTION_GOTO: {
312 FPDF_DEST dest = FPDFAction_GetDest(engine_->doc(), action);
313 if (dest)
314 return GetDestinationTarget(dest, target);
315 // TODO(gene): We don't fully support all types of the in-document
316 // links. Need to implement that. There is a bug to track that:
317 // http://code.google.com/p/chromium/issues/detail?id=55776
318 } break;
319 case PDFACTION_URI: {
320 if (target) {
321 size_t buffer_size =
322 FPDFAction_GetURIPath(engine_->doc(), action, NULL, 0);
323 if (buffer_size > 0) {
324 PDFiumAPIStringBufferAdapter<std::string> api_string_adapter(
325 &target->url, buffer_size, true);
326 void* data = api_string_adapter.GetData();
327 size_t bytes_written = FPDFAction_GetURIPath(
328 engine_->doc(), action, data, buffer_size);
329 api_string_adapter.Close(bytes_written);
332 return WEBLINK_AREA;
333 } break;
334 // TODO(gene): We don't support PDFACTION_REMOTEGOTO and PDFACTION_LAUNCH
335 // at the moment.
339 return NONSELECTABLE_AREA;
342 PDFiumPage::Area PDFiumPage::GetDestinationTarget(
343 FPDF_DEST destination, PDFiumPage::LinkTarget* target) {
344 int page_index = FPDFDest_GetPageIndex(engine_->doc(), destination);
345 if (target) {
346 target->page = page_index;
348 return DOCLINK_AREA;
351 int PDFiumPage::GetLink(int char_index, PDFiumPage::LinkTarget* target) {
352 if (!available_)
353 return -1;
355 CalculateLinks();
357 // Get the bounding box of the rect again, since it might have moved because
358 // of the tolerance above.
359 double left, right, bottom, top;
360 FPDFText_GetCharBox(GetTextPage(), char_index, &left, &right, &bottom, &top);
362 pp::Point origin(
363 PageToScreen(pp::Point(), 1.0, left, top, right, bottom, 0).point());
364 for (size_t i = 0; i < links_.size(); ++i) {
365 for (size_t j = 0; j < links_[i].rects.size(); ++j) {
366 if (links_[i].rects[j].Contains(origin)) {
367 if (target)
368 target->url = links_[i].url;
369 return i;
373 return -1;
376 std::vector<int> PDFiumPage::GetLinks(pp::Rect text_area,
377 std::vector<LinkTarget>* targets) {
378 if (!available_)
379 return std::vector<int>();
381 CalculateLinks();
383 std::vector<int> links;
385 for (size_t i = 0; i < links_.size(); ++i) {
386 for (size_t j = 0; j < links_[i].rects.size(); ++j) {
387 if (links_[i].rects[j].Intersects(text_area)) {
388 if (targets) {
389 LinkTarget target;
390 target.url = links_[i].url;
391 targets->push_back(target);
393 links.push_back(i);
397 return links;
400 void PDFiumPage::CalculateLinks() {
401 if (calculated_links_)
402 return;
404 calculated_links_ = true;
405 FPDF_PAGELINK links = FPDFLink_LoadWebLinks(GetTextPage());
406 int count = FPDFLink_CountWebLinks(links);
407 for (int i = 0; i < count; ++i) {
408 base::string16 url;
409 int url_length = FPDFLink_GetURL(links, i, NULL, 0);
410 if (url_length > 0) {
411 PDFiumAPIStringBufferAdapter<base::string16> api_string_adapter(
412 &url, url_length, true);
413 unsigned short* data =
414 reinterpret_cast<unsigned short*>(api_string_adapter.GetData());
415 int actual_length = FPDFLink_GetURL(links, i, data, url_length);
416 api_string_adapter.Close(actual_length);
418 Link link;
419 link.url = base::UTF16ToUTF8(url);
421 // If the link cannot be converted to a pp::Var, then it is not possible to
422 // pass it to JS. In this case, ignore the link like other PDF viewers.
423 // See http://crbug.com/312882 for an example.
424 pp::Var link_var(link.url);
425 if (!link_var.is_string())
426 continue;
428 // Make sure all the characters in the URL are valid per RFC 1738.
429 // http://crbug.com/340326 has a sample bad PDF.
430 // GURL does not work correctly, e.g. it just strips \t \r \n.
431 bool is_invalid_url = false;
432 for (size_t j = 0; j < link.url.length(); ++j) {
433 // Control characters are not allowed.
434 // 0x7F is also a control character.
435 // 0x80 and above are not in US-ASCII.
436 if (link.url[j] < ' ' || link.url[j] >= '\x7F') {
437 is_invalid_url = true;
438 break;
441 if (is_invalid_url)
442 continue;
444 int rect_count = FPDFLink_CountRects(links, i);
445 for (int j = 0; j < rect_count; ++j) {
446 double left, top, right, bottom;
447 FPDFLink_GetRect(links, i, j, &left, &top, &right, &bottom);
448 link.rects.push_back(
449 PageToScreen(pp::Point(), 1.0, left, top, right, bottom, 0));
451 links_.push_back(link);
453 FPDFLink_CloseWebLinks(links);
456 pp::Rect PDFiumPage::PageToScreen(const pp::Point& offset,
457 double zoom,
458 double left,
459 double top,
460 double right,
461 double bottom,
462 int rotation) {
463 if (!available_)
464 return pp::Rect();
466 int new_left, new_top, new_right, new_bottom;
467 FPDF_PageToDevice(
468 page_,
469 static_cast<int>((rect_.x() - offset.x()) * zoom),
470 static_cast<int>((rect_.y() - offset.y()) * zoom),
471 static_cast<int>(ceil(rect_.width() * zoom)),
472 static_cast<int>(ceil(rect_.height() * zoom)),
473 rotation, left, top, &new_left, &new_top);
474 FPDF_PageToDevice(
475 page_,
476 static_cast<int>((rect_.x() - offset.x()) * zoom),
477 static_cast<int>((rect_.y() - offset.y()) * zoom),
478 static_cast<int>(ceil(rect_.width() * zoom)),
479 static_cast<int>(ceil(rect_.height() * zoom)),
480 rotation, right, bottom, &new_right, &new_bottom);
482 // If the PDF is rotated, the horizontal/vertical coordinates could be
483 // flipped. See
484 // http://www.netl.doe.gov/publications/proceedings/03/ubc/presentations/Goeckner-pres.pdf
485 if (new_right < new_left)
486 std::swap(new_right, new_left);
487 if (new_bottom < new_top)
488 std::swap(new_bottom, new_top);
490 return pp::Rect(
491 new_left, new_top, new_right - new_left + 1, new_bottom - new_top + 1);
494 PDFiumPage::ScopedLoadCounter::ScopedLoadCounter(PDFiumPage* page)
495 : page_(page) {
496 page_->loading_count_++;
499 PDFiumPage::ScopedLoadCounter::~ScopedLoadCounter() {
500 page_->loading_count_--;
503 PDFiumPage::Link::Link() {
506 PDFiumPage::Link::~Link() {
509 } // namespace chrome_pdf