OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "pdf/pdfium/pdfium_page.h" | 5 #include "pdf/pdfium/pdfium_page.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 double left, right, bottom, top; | 62 double left, right, bottom, top; |
63 FPDFText_GetCharBox(text_page, index, &left, &right, &bottom, &top); | 63 FPDFText_GetCharBox(text_page, index, &left, &right, &bottom, &top); |
64 if (right < left) | 64 if (right < left) |
65 std::swap(left, right); | 65 std::swap(left, right); |
66 if (bottom < top) | 66 if (bottom < top) |
67 std::swap(top, bottom); | 67 std::swap(top, bottom); |
68 pp::FloatRect page_coords(left, top, right - left, bottom - top); | 68 pp::FloatRect page_coords(left, top, right - left, bottom - top); |
69 return FloatPageRectToPixelRect(page, page_coords); | 69 return FloatPageRectToPixelRect(page, page_coords); |
70 } | 70 } |
71 | 71 |
72 bool OverlapsOnYAxis(const pp::FloatRect &a, const pp::FloatRect& b) { | 72 bool OverlapsOnYAxis(const pp::FloatRect& a, const pp::FloatRect& b) { |
73 return !(a.IsEmpty() || b.IsEmpty() || | 73 return !(a.IsEmpty() || b.IsEmpty() || a.bottom() < b.y() || |
74 a.bottom() < b.y() || b.bottom() < a.y()); | 74 b.bottom() < a.y()); |
75 } | 75 } |
76 | 76 |
77 } // namespace | 77 } // namespace |
78 | 78 |
79 namespace chrome_pdf { | 79 namespace chrome_pdf { |
80 | 80 |
81 PDFiumPage::PDFiumPage(PDFiumEngine* engine, | 81 PDFiumPage::PDFiumPage(PDFiumEngine* engine, |
82 int i, | 82 int i, |
83 const pp::Rect& r, | 83 const pp::Rect& r, |
84 bool available) | 84 bool available) |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 pp::FloatRect text_run_bounds = | 181 pp::FloatRect text_run_bounds = |
182 GetFloatCharRectInPixels(page, text_page, char_index); | 182 GetFloatCharRectInPixels(page, text_page, char_index); |
183 if (char_index < chars_count) | 183 if (char_index < chars_count) |
184 char_index++; | 184 char_index++; |
185 while (char_index < chars_count) { | 185 while (char_index < chars_count) { |
186 unsigned int character = FPDFText_GetUnicode(text_page, char_index); | 186 unsigned int character = FPDFText_GetUnicode(text_page, char_index); |
187 | 187 |
188 if (!base::IsUnicodeWhitespace(character)) { | 188 if (!base::IsUnicodeWhitespace(character)) { |
189 // TODO(dmazzoni): this assumes horizontal text. | 189 // TODO(dmazzoni): this assumes horizontal text. |
190 // https://crbug.com/580311 | 190 // https://crbug.com/580311 |
191 pp::FloatRect char_rect = GetFloatCharRectInPixels( | 191 pp::FloatRect char_rect = |
192 page, text_page, char_index); | 192 GetFloatCharRectInPixels(page, text_page, char_index); |
193 if (!char_rect.IsEmpty() && !OverlapsOnYAxis(text_run_bounds, char_rect)) | 193 if (!char_rect.IsEmpty() && !OverlapsOnYAxis(text_run_bounds, char_rect)) |
194 break; | 194 break; |
195 | 195 |
196 int font_size = FPDFText_GetFontSize(text_page, char_index); | 196 int font_size = FPDFText_GetFontSize(text_page, char_index); |
197 if (font_size != text_run_font_size) | 197 if (font_size != text_run_font_size) |
198 break; | 198 break; |
199 | 199 |
200 // Heuristic: split a text run after a space longer than 3 average | 200 // Heuristic: split a text run after a space longer than 3 average |
201 // characters. | 201 // characters. |
202 double avg_char_width = | 202 double avg_char_width = |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 PDFiumPage::Area PDFiumPage::GetCharIndex(const pp::Point& point, | 237 PDFiumPage::Area PDFiumPage::GetCharIndex(const pp::Point& point, |
238 int rotation, | 238 int rotation, |
239 int* char_index, | 239 int* char_index, |
240 int* form_type, | 240 int* form_type, |
241 LinkTarget* target) { | 241 LinkTarget* target) { |
242 if (!available_) | 242 if (!available_) |
243 return NONSELECTABLE_AREA; | 243 return NONSELECTABLE_AREA; |
244 pp::Point point2 = point - rect_.point(); | 244 pp::Point point2 = point - rect_.point(); |
245 double new_x; | 245 double new_x; |
246 double new_y; | 246 double new_y; |
247 FPDF_DeviceToPage(GetPage(), 0, 0, rect_.width(), rect_.height(), | 247 FPDF_DeviceToPage(GetPage(), 0, 0, rect_.width(), rect_.height(), rotation, |
248 rotation, point2.x(), point2.y(), &new_x, &new_y); | 248 point2.x(), point2.y(), &new_x, &new_y); |
249 | 249 |
250 int rv = FPDFText_GetCharIndexAtPos( | 250 int rv = FPDFText_GetCharIndexAtPos(GetTextPage(), new_x, new_y, kTolerance, |
251 GetTextPage(), new_x, new_y, kTolerance, kTolerance); | 251 kTolerance); |
252 *char_index = rv; | 252 *char_index = rv; |
253 | 253 |
254 FPDF_LINK link = FPDFLink_GetLinkAtPoint(GetPage(), new_x, new_y); | 254 FPDF_LINK link = FPDFLink_GetLinkAtPoint(GetPage(), new_x, new_y); |
255 int control = | 255 int control = |
256 FPDFPage_HasFormFieldAtPoint(engine_->form(), GetPage(), new_x, new_y); | 256 FPDFPage_HasFormFieldAtPoint(engine_->form(), GetPage(), new_x, new_y); |
257 | 257 |
258 // If there is a control and link at the same point, figure out their z-order | 258 // If there is a control and link at the same point, figure out their z-order |
259 // to determine which is on top. | 259 // to determine which is on top. |
260 if (link && control > FPDF_FORMFIELD_UNKNOWN) { | 260 if (link && control > FPDF_FORMFIELD_UNKNOWN) { |
261 int control_z_order = FPDFPage_FormFieldZOrderAtPoint( | 261 int control_z_order = FPDFPage_FormFieldZOrderAtPoint( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 return static_cast<base::char16>(FPDFText_GetUnicode(GetTextPage(), index)); | 298 return static_cast<base::char16>(FPDFText_GetUnicode(GetTextPage(), index)); |
299 } | 299 } |
300 | 300 |
301 int PDFiumPage::GetCharCount() { | 301 int PDFiumPage::GetCharCount() { |
302 if (!available_) | 302 if (!available_) |
303 return 0; | 303 return 0; |
304 return FPDFText_CountChars(GetTextPage()); | 304 return FPDFText_CountChars(GetTextPage()); |
305 } | 305 } |
306 | 306 |
307 PDFiumPage::Area PDFiumPage::GetLinkTarget( | 307 PDFiumPage::Area PDFiumPage::GetLinkTarget( |
308 FPDF_LINK link, PDFiumPage::LinkTarget* target) const { | 308 FPDF_LINK link, |
| 309 PDFiumPage::LinkTarget* target) const { |
309 FPDF_DEST dest = FPDFLink_GetDest(engine_->doc(), link); | 310 FPDF_DEST dest = FPDFLink_GetDest(engine_->doc(), link); |
310 if (dest) | 311 if (dest) |
311 return GetDestinationTarget(dest, target); | 312 return GetDestinationTarget(dest, target); |
312 | 313 |
313 FPDF_ACTION action = FPDFLink_GetAction(link); | 314 FPDF_ACTION action = FPDFLink_GetAction(link); |
314 if (action) { | 315 if (action) { |
| 316 // TODO(gene): We don't support PDFACTION_REMOTEGOTO and |
| 317 // PDFACTION_LAUNCH at the moment. |
315 switch (FPDFAction_GetType(action)) { | 318 switch (FPDFAction_GetType(action)) { |
316 case PDFACTION_GOTO: { | 319 case PDFACTION_GOTO: { |
317 FPDF_DEST dest = FPDFAction_GetDest(engine_->doc(), action); | 320 FPDF_DEST dest = FPDFAction_GetDest(engine_->doc(), action); |
318 if (dest) | 321 if (dest) |
319 return GetDestinationTarget(dest, target); | 322 return GetDestinationTarget(dest, target); |
320 // TODO(gene): We don't fully support all types of the in-document | 323 // TODO(gene): We don't fully support all types of the in-document |
321 // links. Need to implement that. There is a bug to track that: | 324 // links. Need to implement that. There is a bug to track that: |
322 // http://code.google.com/p/chromium/issues/detail?id=55776 | 325 // http://code.google.com/p/chromium/issues/detail?id=55776 |
323 } break; | 326 break; |
| 327 } |
324 case PDFACTION_URI: { | 328 case PDFACTION_URI: { |
325 if (target) { | 329 if (target) { |
326 size_t buffer_size = | 330 size_t buffer_size = |
327 FPDFAction_GetURIPath(engine_->doc(), action, nullptr, 0); | 331 FPDFAction_GetURIPath(engine_->doc(), action, nullptr, 0); |
328 if (buffer_size > 0) { | 332 if (buffer_size > 0) { |
329 PDFiumAPIStringBufferAdapter<std::string> api_string_adapter( | 333 PDFiumAPIStringBufferAdapter<std::string> api_string_adapter( |
330 &target->url, buffer_size, true); | 334 &target->url, buffer_size, true); |
331 void* data = api_string_adapter.GetData(); | 335 void* data = api_string_adapter.GetData(); |
332 size_t bytes_written = FPDFAction_GetURIPath( | 336 size_t bytes_written = FPDFAction_GetURIPath(engine_->doc(), action, |
333 engine_->doc(), action, data, buffer_size); | 337 data, buffer_size); |
334 api_string_adapter.Close(bytes_written); | 338 api_string_adapter.Close(bytes_written); |
335 } | |
336 } | 339 } |
337 return WEBLINK_AREA; | 340 } |
338 } break; | 341 return WEBLINK_AREA; |
339 // TODO(gene): We don't support PDFACTION_REMOTEGOTO and PDFACTION_LAUNCH | 342 } |
340 // at the moment. | |
341 } | 343 } |
342 } | 344 } |
343 | 345 |
344 return NONSELECTABLE_AREA; | 346 return NONSELECTABLE_AREA; |
345 } | 347 } |
346 | 348 |
347 PDFiumPage::Area PDFiumPage::GetDestinationTarget( | 349 PDFiumPage::Area PDFiumPage::GetDestinationTarget( |
348 FPDF_DEST destination, PDFiumPage::LinkTarget* target) const { | 350 FPDF_DEST destination, |
| 351 PDFiumPage::LinkTarget* target) const { |
349 if (target) | 352 if (target) |
350 target->page = FPDFDest_GetPageIndex(engine_->doc(), destination); | 353 target->page = FPDFDest_GetPageIndex(engine_->doc(), destination); |
351 return DOCLINK_AREA; | 354 return DOCLINK_AREA; |
352 } | 355 } |
353 | 356 |
354 int PDFiumPage::GetLink(int char_index, PDFiumPage::LinkTarget* target) { | 357 int PDFiumPage::GetLink(int char_index, PDFiumPage::LinkTarget* target) { |
355 if (!available_) | 358 if (!available_) |
356 return -1; | 359 return -1; |
357 | 360 |
358 CalculateLinks(); | 361 CalculateLinks(); |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 page_->loading_count_--; | 522 page_->loading_count_--; |
520 } | 523 } |
521 | 524 |
522 PDFiumPage::Link::Link() = default; | 525 PDFiumPage::Link::Link() = default; |
523 | 526 |
524 PDFiumPage::Link::Link(const Link& that) = default; | 527 PDFiumPage::Link::Link(const Link& that) = default; |
525 | 528 |
526 PDFiumPage::Link::~Link() = default; | 529 PDFiumPage::Link::~Link() = default; |
527 | 530 |
528 } // namespace chrome_pdf | 531 } // namespace chrome_pdf |
OLD | NEW |