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 size_t character_size = sizeof(base::string16::value_type); | |
526 unsigned long buffer_size = FPDFBookmark_GetTitle(bookmark, NULL, 0); | |
527 size_t title_length = buffer_size / character_size; | |
528 | |
529 if (title_length > 0) { | |
530 PDFiumAPIStringBufferAdapter<base::string16> api_string_adapter( | |
531 &title, title_length, true); | |
532 void* data = api_string_adapter.GetData(); | |
533 unsigned long bytes_written = FPDFBookmark_GetTitle( | |
534 bookmark, data, buffer_size); | |
535 api_string_adapter.Close(bytes_written / character_size); | |
raymes
2015/01/16 02:44:23
this should just be
api_string_adapter.Close(titl
Alexandre Carlton
2015/01/16 03:01:40
Done.
| |
536 } | |
537 | |
538 | |
raymes
2015/01/16 02:44:23
nit: remove the above 2 blank lines
Alexandre Carlton
2015/01/16 03:01:40
Done.
| |
539 dict.Set(pp::Var("title"), pp::Var(base::UTF16ToUTF8(title))); | |
540 | |
541 FPDF_DEST dest = FPDFBookmark_GetDest(doc, bookmark); | |
542 // Some bookmarks don't have a page to select. | |
543 if (dest) { | |
544 int page_index = FPDFDest_GetPageIndex(doc, dest); | |
545 dict.Set(pp::Var("page"), pp::Var(page_index)); | |
546 } | |
547 | |
548 pp::VarArray children; | |
549 int child_index = 0; | |
550 for (FPDF_BOOKMARK child_bookmark = FPDFBookmark_GetFirstChild(doc, bookmark); | |
551 child_bookmark != NULL; | |
552 child_bookmark = FPDFBookmark_GetNextSibling(doc, child_bookmark)) { | |
553 children.Set(child_index, TraverseBookmarks(doc, child_bookmark)); | |
554 child_index++; | |
555 } | |
556 dict.Set(pp::Var("children"), children); | |
557 return dict; | |
558 } | |
559 | |
517 } // namespace | 560 } // namespace |
518 | 561 |
519 bool InitializeSDK() { | 562 bool InitializeSDK() { |
520 FPDF_InitLibrary(); | 563 FPDF_InitLibrary(); |
521 | 564 |
522 #if defined(OS_LINUX) | 565 #if defined(OS_LINUX) |
523 // Font loading doesn't work in the renderer sandbox in Linux. | 566 // Font loading doesn't work in the renderer sandbox in Linux. |
524 FPDF_SetSystemFontInfo(&g_font_info); | 567 FPDF_SetSystemFontInfo(&g_font_info); |
525 #endif | 568 #endif |
526 | 569 |
(...skipping 1783 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2310 if (pages_[i]->available()) { | 2353 if (pages_[i]->available()) { |
2311 selection_.push_back(PDFiumRange(pages_[i], 0, | 2354 selection_.push_back(PDFiumRange(pages_[i], 0, |
2312 pages_[i]->GetCharCount())); | 2355 pages_[i]->GetCharCount())); |
2313 } | 2356 } |
2314 } | 2357 } |
2315 | 2358 |
2316 int PDFiumEngine::GetNumberOfPages() { | 2359 int PDFiumEngine::GetNumberOfPages() { |
2317 return pages_.size(); | 2360 return pages_.size(); |
2318 } | 2361 } |
2319 | 2362 |
2363 | |
2364 pp::VarArray PDFiumEngine::GetBookmarks() { | |
2365 pp::VarDictionary dict = TraverseBookmarks(doc_, NULL); | |
2366 // The root bookmark contains no useful information. | |
2367 return pp::VarArray(dict.Get(pp::Var("children"))); | |
2368 } | |
2369 | |
2320 int PDFiumEngine::GetNamedDestinationPage(const std::string& destination) { | 2370 int PDFiumEngine::GetNamedDestinationPage(const std::string& destination) { |
2321 // Look for the destination. | 2371 // Look for the destination. |
2322 FPDF_DEST dest = FPDF_GetNamedDestByName(doc_, destination.c_str()); | 2372 FPDF_DEST dest = FPDF_GetNamedDestByName(doc_, destination.c_str()); |
2323 if (!dest) { | 2373 if (!dest) { |
2324 // Look for a bookmark with the same name. | 2374 // Look for a bookmark with the same name. |
2325 base::string16 destination_wide = base::UTF8ToUTF16(destination); | 2375 base::string16 destination_wide = base::UTF8ToUTF16(destination); |
2326 FPDF_WIDESTRING destination_pdf_wide = | 2376 FPDF_WIDESTRING destination_pdf_wide = |
2327 reinterpret_cast<FPDF_WIDESTRING>(destination_wide.c_str()); | 2377 reinterpret_cast<FPDF_WIDESTRING>(destination_wide.c_str()); |
2328 FPDF_BOOKMARK bookmark = FPDFBookmark_Find(doc_, destination_pdf_wide); | 2378 FPDF_BOOKMARK bookmark = FPDFBookmark_Find(doc_, destination_pdf_wide); |
2329 if (!bookmark) | 2379 if (!bookmark) |
(...skipping 1585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3915 double* height) { | 3965 double* height) { |
3916 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); | 3966 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); |
3917 if (!doc) | 3967 if (!doc) |
3918 return false; | 3968 return false; |
3919 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; | 3969 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; |
3920 FPDF_CloseDocument(doc); | 3970 FPDF_CloseDocument(doc); |
3921 return success; | 3971 return success; |
3922 } | 3972 } |
3923 | 3973 |
3924 } // namespace chrome_pdf | 3974 } // namespace chrome_pdf |
OLD | NEW |