| 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 13 matching lines...) Expand all Loading... |
| 24 #include "ppapi/c/ppb_core.h" | 24 #include "ppapi/c/ppb_core.h" |
| 25 #include "ppapi/c/private/ppb_pdf.h" | 25 #include "ppapi/c/private/ppb_pdf.h" |
| 26 #include "ppapi/cpp/dev/memory_dev.h" | 26 #include "ppapi/cpp/dev/memory_dev.h" |
| 27 #include "ppapi/cpp/input_event.h" | 27 #include "ppapi/cpp/input_event.h" |
| 28 #include "ppapi/cpp/instance.h" | 28 #include "ppapi/cpp/instance.h" |
| 29 #include "ppapi/cpp/module.h" | 29 #include "ppapi/cpp/module.h" |
| 30 #include "ppapi/cpp/private/pdf.h" | 30 #include "ppapi/cpp/private/pdf.h" |
| 31 #include "ppapi/cpp/trusted/browser_font_trusted.h" | 31 #include "ppapi/cpp/trusted/browser_font_trusted.h" |
| 32 #include "ppapi/cpp/url_response_info.h" | 32 #include "ppapi/cpp/url_response_info.h" |
| 33 #include "ppapi/cpp/var.h" | 33 #include "ppapi/cpp/var.h" |
| 34 #include "ppapi/cpp/var_dictionary.h" |
| 34 #include "third_party/pdfium/fpdfsdk/include/fpdf_ext.h" | 35 #include "third_party/pdfium/fpdfsdk/include/fpdf_ext.h" |
| 35 #include "third_party/pdfium/fpdfsdk/include/fpdf_flatten.h" | 36 #include "third_party/pdfium/fpdfsdk/include/fpdf_flatten.h" |
| 36 #include "third_party/pdfium/fpdfsdk/include/fpdf_searchex.h" | 37 #include "third_party/pdfium/fpdfsdk/include/fpdf_searchex.h" |
| 37 #include "third_party/pdfium/fpdfsdk/include/fpdf_sysfontinfo.h" | 38 #include "third_party/pdfium/fpdfsdk/include/fpdf_sysfontinfo.h" |
| 38 #include "third_party/pdfium/fpdfsdk/include/fpdf_transformpage.h" | 39 #include "third_party/pdfium/fpdfsdk/include/fpdf_transformpage.h" |
| 39 #include "third_party/pdfium/fpdfsdk/include/fpdfedit.h" | 40 #include "third_party/pdfium/fpdfsdk/include/fpdfedit.h" |
| 40 #include "third_party/pdfium/fpdfsdk/include/fpdfppo.h" | 41 #include "third_party/pdfium/fpdfsdk/include/fpdfppo.h" |
| 41 #include "third_party/pdfium/fpdfsdk/include/fpdfsave.h" | 42 #include "third_party/pdfium/fpdfsdk/include/fpdfsave.h" |
| 42 #include "third_party/pdfium/fpdfsdk/include/pdfwindow/PDFWindow.h" | 43 #include "third_party/pdfium/fpdfsdk/include/pdfwindow/PDFWindow.h" |
| 43 #include "third_party/pdfium/fpdfsdk/include/pdfwindow/PWL_FontMap.h" | 44 #include "third_party/pdfium/fpdfsdk/include/pdfwindow/PWL_FontMap.h" |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 static const base::char16 kCr[] = {L'\r', L'\0'}; | 508 static const base::char16 kCr[] = {L'\r', L'\0'}; |
| 508 static const base::char16 kBlank[] = {L'\0'}; | 509 static const base::char16 kBlank[] = {L'\0'}; |
| 509 base::ReplaceChars(*text, kCr, kBlank, text); | 510 base::ReplaceChars(*text, kCr, kBlank, text); |
| 510 #elif defined(OS_WIN) | 511 #elif defined(OS_WIN) |
| 511 // Do nothing | 512 // Do nothing |
| 512 #else | 513 #else |
| 513 NOTIMPLEMENTED(); | 514 NOTIMPLEMENTED(); |
| 514 #endif | 515 #endif |
| 515 } | 516 } |
| 516 | 517 |
| 518 // Returns a VarDictionary (representing a bookmark), which in turn contains |
| 519 // child VarDictionaries (representing the child bookmarks). |
| 520 // If NULL is passed in as the bookmark then we traverse from the "root". |
| 521 // Note that the "root" bookmark contains no useful information. |
| 522 pp::VarDictionary TraverseBookmarks(FPDF_DOCUMENT doc, FPDF_BOOKMARK bookmark) { |
| 523 pp::VarDictionary dict; |
| 524 base::string16 title; |
| 525 unsigned long buffer_size = FPDFBookmark_GetTitle(bookmark, NULL, 0); |
| 526 size_t title_length = buffer_size / sizeof(base::string16::value_type); |
| 527 |
| 528 if (title_length > 0) { |
| 529 PDFiumAPIStringBufferAdapter<base::string16> api_string_adapter( |
| 530 &title, title_length, true); |
| 531 void* data = api_string_adapter.GetData(); |
| 532 FPDFBookmark_GetTitle(bookmark, data, buffer_size); |
| 533 api_string_adapter.Close(title_length); |
| 534 } |
| 535 dict.Set(pp::Var("title"), pp::Var(base::UTF16ToUTF8(title))); |
| 536 |
| 537 FPDF_DEST dest = FPDFBookmark_GetDest(doc, bookmark); |
| 538 // Some bookmarks don't have a page to select. |
| 539 if (dest) { |
| 540 int page_index = FPDFDest_GetPageIndex(doc, dest); |
| 541 dict.Set(pp::Var("page"), pp::Var(page_index)); |
| 542 } |
| 543 |
| 544 pp::VarArray children; |
| 545 int child_index = 0; |
| 546 for (FPDF_BOOKMARK child_bookmark = FPDFBookmark_GetFirstChild(doc, bookmark); |
| 547 child_bookmark != NULL; |
| 548 child_bookmark = FPDFBookmark_GetNextSibling(doc, child_bookmark)) { |
| 549 children.Set(child_index, TraverseBookmarks(doc, child_bookmark)); |
| 550 child_index++; |
| 551 } |
| 552 dict.Set(pp::Var("children"), children); |
| 553 return dict; |
| 554 } |
| 555 |
| 517 } // namespace | 556 } // namespace |
| 518 | 557 |
| 519 bool InitializeSDK() { | 558 bool InitializeSDK() { |
| 520 FPDF_InitLibrary(); | 559 FPDF_InitLibrary(); |
| 521 | 560 |
| 522 #if defined(OS_LINUX) | 561 #if defined(OS_LINUX) |
| 523 // Font loading doesn't work in the renderer sandbox in Linux. | 562 // Font loading doesn't work in the renderer sandbox in Linux. |
| 524 FPDF_SetSystemFontInfo(&g_font_info); | 563 FPDF_SetSystemFontInfo(&g_font_info); |
| 525 #endif | 564 #endif |
| 526 | 565 |
| (...skipping 1783 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2310 if (pages_[i]->available()) { | 2349 if (pages_[i]->available()) { |
| 2311 selection_.push_back(PDFiumRange(pages_[i], 0, | 2350 selection_.push_back(PDFiumRange(pages_[i], 0, |
| 2312 pages_[i]->GetCharCount())); | 2351 pages_[i]->GetCharCount())); |
| 2313 } | 2352 } |
| 2314 } | 2353 } |
| 2315 | 2354 |
| 2316 int PDFiumEngine::GetNumberOfPages() { | 2355 int PDFiumEngine::GetNumberOfPages() { |
| 2317 return pages_.size(); | 2356 return pages_.size(); |
| 2318 } | 2357 } |
| 2319 | 2358 |
| 2359 |
| 2360 pp::VarArray PDFiumEngine::GetBookmarks() { |
| 2361 pp::VarDictionary dict = TraverseBookmarks(doc_, NULL); |
| 2362 // The root bookmark contains no useful information. |
| 2363 return pp::VarArray(dict.Get(pp::Var("children"))); |
| 2364 } |
| 2365 |
| 2320 int PDFiumEngine::GetNamedDestinationPage(const std::string& destination) { | 2366 int PDFiumEngine::GetNamedDestinationPage(const std::string& destination) { |
| 2321 // Look for the destination. | 2367 // Look for the destination. |
| 2322 FPDF_DEST dest = FPDF_GetNamedDestByName(doc_, destination.c_str()); | 2368 FPDF_DEST dest = FPDF_GetNamedDestByName(doc_, destination.c_str()); |
| 2323 if (!dest) { | 2369 if (!dest) { |
| 2324 // Look for a bookmark with the same name. | 2370 // Look for a bookmark with the same name. |
| 2325 base::string16 destination_wide = base::UTF8ToUTF16(destination); | 2371 base::string16 destination_wide = base::UTF8ToUTF16(destination); |
| 2326 FPDF_WIDESTRING destination_pdf_wide = | 2372 FPDF_WIDESTRING destination_pdf_wide = |
| 2327 reinterpret_cast<FPDF_WIDESTRING>(destination_wide.c_str()); | 2373 reinterpret_cast<FPDF_WIDESTRING>(destination_wide.c_str()); |
| 2328 FPDF_BOOKMARK bookmark = FPDFBookmark_Find(doc_, destination_pdf_wide); | 2374 FPDF_BOOKMARK bookmark = FPDFBookmark_Find(doc_, destination_pdf_wide); |
| 2329 if (!bookmark) | 2375 if (!bookmark) |
| (...skipping 1585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3915 double* height) { | 3961 double* height) { |
| 3916 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); | 3962 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); |
| 3917 if (!doc) | 3963 if (!doc) |
| 3918 return false; | 3964 return false; |
| 3919 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; | 3965 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; |
| 3920 FPDF_CloseDocument(doc); | 3966 FPDF_CloseDocument(doc); |
| 3921 return success; | 3967 return success; |
| 3922 } | 3968 } |
| 3923 | 3969 |
| 3924 } // namespace chrome_pdf | 3970 } // namespace chrome_pdf |
| OLD | NEW |