OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_engine.h" | 5 #include "pdf/pdfium/pdfium_engine.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 27 matching lines...) Expand all Loading... | |
38 #include "third_party/pdfium/fpdfsdk/include/fpdfedit.h" | 38 #include "third_party/pdfium/fpdfsdk/include/fpdfedit.h" |
39 #include "third_party/pdfium/fpdfsdk/include/fpdfoom.h" | 39 #include "third_party/pdfium/fpdfsdk/include/fpdfoom.h" |
40 #include "third_party/pdfium/fpdfsdk/include/fpdfppo.h" | 40 #include "third_party/pdfium/fpdfsdk/include/fpdfppo.h" |
41 #include "third_party/pdfium/fpdfsdk/include/fpdfsave.h" | 41 #include "third_party/pdfium/fpdfsdk/include/fpdfsave.h" |
42 #include "third_party/pdfium/fpdfsdk/include/pdfwindow/PDFWindow.h" | 42 #include "third_party/pdfium/fpdfsdk/include/pdfwindow/PDFWindow.h" |
43 #include "third_party/pdfium/fpdfsdk/include/pdfwindow/PWL_FontMap.h" | 43 #include "third_party/pdfium/fpdfsdk/include/pdfwindow/PWL_FontMap.h" |
44 #include "ui/events/keycodes/keyboard_codes.h" | 44 #include "ui/events/keycodes/keyboard_codes.h" |
45 | 45 |
46 namespace chrome_pdf { | 46 namespace chrome_pdf { |
47 | 47 |
48 namespace { | |
49 | |
48 #define kPageShadowTop 3 | 50 #define kPageShadowTop 3 |
49 #define kPageShadowBottom 7 | 51 #define kPageShadowBottom 7 |
50 #define kPageShadowLeft 5 | 52 #define kPageShadowLeft 5 |
51 #define kPageShadowRight 5 | 53 #define kPageShadowRight 5 |
52 | 54 |
53 #define kPageSeparatorThickness 4 | 55 #define kPageSeparatorThickness 4 |
54 #define kHighlightColorR 153 | 56 #define kHighlightColorR 153 |
55 #define kHighlightColorG 193 | 57 #define kHighlightColorG 193 |
56 #define kHighlightColorB 218 | 58 #define kHighlightColorB 218 |
57 | 59 |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
451 case 3: | 453 case 3: |
452 *offset_x = page_height - source_clip_box.right; | 454 *offset_x = page_height - source_clip_box.right; |
453 *offset_y = page_width - source_clip_box.top; | 455 *offset_y = page_width - source_clip_box.top; |
454 break; | 456 break; |
455 default: | 457 default: |
456 NOTREACHED(); | 458 NOTREACHED(); |
457 break; | 459 break; |
458 } | 460 } |
459 } | 461 } |
460 | 462 |
461 // Do an in-place transformation of objects on |page|. Translate all objects on | 463 // This formats a string with special 0xfffe end-of-line hyphens the same way |
462 // |page| in |source_clip_box| by (|offset_x|, |offset_y|) and scale them by | 464 // as Adobe Reader. When a hyphen is encountered, the next non-CR/LF whitespace |
463 // |scale_factor|. | 465 // becomes CR+LF and the hyphen is erased. If there is no whitespace between |
464 // | 466 // two hyphens, the latter hyphen is erased and ignored. |
465 // |page| Handle to the page. Returned by FPDF_LoadPage function. | 467 void FormatStringWithHyphens(base::string16* text) { |
466 // |source_clip_box| specifies the source clip box positions, relative to | 468 // First pass marks all the hyphen positions. |
467 // origin at left-bottom. | 469 struct HyphenPosition { |
468 // |scale_factor| specifies the scale factor that should be applied to page | 470 HyphenPosition() : position(0), next_whitespace_position(0) {} |
469 // objects. | 471 size_t position; |
470 // |offset_x| and |offset_y| specifies the translation offsets for the page | 472 size_t next_whitespace_position; // 0 for none |
471 // objects, relative to origin at left-bottom. | 473 }; |
474 std::vector<HyphenPosition> hyphen_positions; | |
475 HyphenPosition current_hyphen_position; | |
476 bool current_hyphen_position_is_valid = false; | |
477 const base::char16 kPdfiumHyphenEOL = 0xfffe; | |
472 | 478 |
473 void TransformPageObjects(FPDF_PAGE page, const ClipBox& source_clip_box, | 479 for (size_t i = 0; i < text->size(); ++i) { |
Lei Zhang
2014/09/11 23:13:10
This has been unused for some time. Putting this b
| |
474 const double scale_factor, double offset_x, | 480 const base::char16& current_char = (*text)[i]; |
475 double offset_y) { | 481 if (current_char == kPdfiumHyphenEOL) { |
476 const int obj_count = FPDFPage_CountObject(page); | 482 if (current_hyphen_position_is_valid) |
483 hyphen_positions.push_back(current_hyphen_position); | |
484 current_hyphen_position = HyphenPosition(); | |
485 current_hyphen_position.position = i; | |
486 current_hyphen_position_is_valid = true; | |
487 } else if (IsWhitespace(current_char)) { | |
488 if (current_hyphen_position_is_valid) { | |
489 if (current_char != L'\r' && current_char != L'\n') | |
490 current_hyphen_position.next_whitespace_position = i; | |
491 hyphen_positions.push_back(current_hyphen_position); | |
492 current_hyphen_position_is_valid = false; | |
493 } | |
494 } | |
495 } | |
496 if (current_hyphen_position_is_valid) | |
497 hyphen_positions.push_back(current_hyphen_position); | |
477 | 498 |
478 // Create a new clip path. | 499 // With all the hyphen positions, do the search and replace. |
479 FPDF_CLIPPATH clip_path = FPDF_CreateClipPath( | 500 while (!hyphen_positions.empty()) { |
480 source_clip_box.left + offset_x, source_clip_box.bottom + offset_y, | 501 static const base::char16 kCr[] = {L'\r', L'\0'}; |
481 source_clip_box.right + offset_x, source_clip_box.top + offset_y); | 502 const HyphenPosition& position = hyphen_positions.back(); |
503 if (position.next_whitespace_position != 0) { | |
504 (*text)[position.next_whitespace_position] = L'\n'; | |
505 text->insert(position.next_whitespace_position, kCr); | |
506 } | |
507 text->erase(position.position, 1); | |
508 hyphen_positions.pop_back(); | |
509 } | |
482 | 510 |
483 for (int obj_idx = 0; obj_idx < obj_count; ++obj_idx) { | 511 // Adobe Reader also get rid of trailing spaces right before a CRLF. |
484 FPDF_PAGEOBJECT page_obj = FPDFPage_GetObject(page, obj_idx); | 512 static const base::char16 kSpaceCrCn[] = {L' ', L'\r', L'\n', L'\0'}; |
485 FPDFPageObj_Transform(page_obj, scale_factor, 0, 0, scale_factor, | 513 static const base::char16 kCrCn[] = {L'\r', L'\n', L'\0'}; |
486 offset_x, offset_y); | 514 ReplaceSubstringsAfterOffset(text, 0, kSpaceCrCn, kCrCn); |
487 FPDFPageObj_TransformClipPath(page_obj, scale_factor, 0, 0, scale_factor, | 515 } |
488 offset_x, offset_y); | |
489 } | |
490 FPDFPage_TransformAnnots(page, scale_factor, 0, 0, scale_factor, | |
491 offset_x, offset_y); | |
492 FPDFPage_GenerateContent(page); | |
493 | 516 |
494 // Add a extra clip path to the new pdf page here. | 517 // Replace CR/LF with just LF on POSIX. |
495 FPDFPage_InsertClipPath(page, clip_path); | 518 void FormatStringForOS(base::string16* text) { |
519 #if defined(OS_POSIX) | |
520 static const base::char16 kCr[] = {L'\r', L'\0'}; | |
521 static const base::char16 kBlank[] = {L'\0'}; | |
522 base::ReplaceChars(*text, kCr, kBlank, text); | |
523 #elif defined(OS_WIN) | |
524 // Do nothing | |
525 #else | |
526 NOTIMPLEMENTED(); | |
527 #endif | |
528 } | |
496 | 529 |
497 // Destroy the clip path. | 530 } // namespace |
498 FPDF_DestroyClipPath(clip_path); | |
499 } | |
500 | 531 |
501 bool InitializeSDK(void* data) { | 532 bool InitializeSDK(void* data) { |
502 FPDF_InitLibrary(data); | 533 FPDF_InitLibrary(data); |
503 | 534 |
504 #if defined(OS_LINUX) | 535 #if defined(OS_LINUX) |
505 // Font loading doesn't work in the renderer sandbox in Linux. | 536 // Font loading doesn't work in the renderer sandbox in Linux. |
506 FPDF_SetSystemFontInfo(&g_font_info); | 537 FPDF_SetSystemFontInfo(&g_font_info); |
507 #endif | 538 #endif |
508 | 539 |
509 FSDK_SetOOMHandler(&g_oom_info); | 540 FSDK_SetOOMHandler(&g_oom_info); |
(...skipping 1344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1854 base::string16 result; | 1885 base::string16 result; |
1855 for (size_t i = 0; i < selection_.size(); ++i) { | 1886 for (size_t i = 0; i < selection_.size(); ++i) { |
1856 if (i > 0 && | 1887 if (i > 0 && |
1857 selection_[i - 1].page_index() > selection_[i].page_index()) { | 1888 selection_[i - 1].page_index() > selection_[i].page_index()) { |
1858 result = selection_[i].GetText() + result; | 1889 result = selection_[i].GetText() + result; |
1859 } else { | 1890 } else { |
1860 result.append(selection_[i].GetText()); | 1891 result.append(selection_[i].GetText()); |
1861 } | 1892 } |
1862 } | 1893 } |
1863 | 1894 |
1895 FormatStringWithHyphens(&result); | |
1896 FormatStringForOS(&result); | |
1864 return base::UTF16ToUTF8(result); | 1897 return base::UTF16ToUTF8(result); |
1865 } | 1898 } |
1866 | 1899 |
1867 std::string PDFiumEngine::GetLinkAtPosition(const pp::Point& point) { | 1900 std::string PDFiumEngine::GetLinkAtPosition(const pp::Point& point) { |
1868 int temp; | 1901 int temp; |
1869 PDFiumPage::LinkTarget target; | 1902 PDFiumPage::LinkTarget target; |
1870 pp::Point point_in_page( | 1903 pp::Point point_in_page( |
1871 static_cast<int>((point.x() + position_.x()) / current_zoom_), | 1904 static_cast<int>((point.x() + position_.x()) / current_zoom_), |
1872 static_cast<int>((point.y() + position_.y()) / current_zoom_)); | 1905 static_cast<int>((point.y() + position_.y()) / current_zoom_)); |
1873 PDFiumPage::Area area = GetCharIndex(point_in_page, &temp, &temp, &target); | 1906 PDFiumPage::Area area = GetCharIndex(point_in_page, &temp, &temp, &target); |
(...skipping 1555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3429 double* height) { | 3462 double* height) { |
3430 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); | 3463 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); |
3431 if (!doc) | 3464 if (!doc) |
3432 return false; | 3465 return false; |
3433 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; | 3466 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; |
3434 FPDF_CloseDocument(doc); | 3467 FPDF_CloseDocument(doc); |
3435 return success; | 3468 return success; |
3436 } | 3469 } |
3437 | 3470 |
3438 } // namespace chrome_pdf | 3471 } // namespace chrome_pdf |
OLD | NEW |