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/i18n/icu_encoding_detection.h" | 9 #include "base/i18n/icu_encoding_detection.h" |
10 #include "base/i18n/icu_string_conversions.h" | 10 #include "base/i18n/icu_string_conversions.h" |
(...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
517 #endif | 517 #endif |
518 } | 518 } |
519 | 519 |
520 // Returns a VarDictionary (representing a bookmark), which in turn contains | 520 // Returns a VarDictionary (representing a bookmark), which in turn contains |
521 // child VarDictionaries (representing the child bookmarks). | 521 // child VarDictionaries (representing the child bookmarks). |
522 // If NULL is passed in as the bookmark then we traverse from the "root". | 522 // If NULL is passed in as the bookmark then we traverse from the "root". |
523 // Note that the "root" bookmark contains no useful information. | 523 // Note that the "root" bookmark contains no useful information. |
524 pp::VarDictionary TraverseBookmarks(FPDF_DOCUMENT doc, FPDF_BOOKMARK bookmark) { | 524 pp::VarDictionary TraverseBookmarks(FPDF_DOCUMENT doc, FPDF_BOOKMARK bookmark) { |
525 pp::VarDictionary dict; | 525 pp::VarDictionary dict; |
526 base::string16 title; | 526 base::string16 title; |
527 unsigned long buffer_size = FPDFBookmark_GetTitle(bookmark, NULL, 0); | 527 size_t buffer_size = FPDFBookmark_GetTitle(bookmark, NULL, 0); |
raymes
2015/08/26 06:33:15
Should we keep this as an unsigned long since this
Sam McNally
2015/08/26 08:23:10
Done.
| |
528 size_t title_length = base::checked_cast<size_t>(buffer_size) / | 528 if (buffer_size > 0) { |
529 sizeof(base::string16::value_type); | 529 PDFiumAPIStringBufferSizeInBytesAdapter<base::string16> api_string_adapter( |
530 if (title_length > 0) { | 530 &title, buffer_size, true); |
531 PDFiumAPIStringBufferAdapter<base::string16> api_string_adapter( | 531 api_string_adapter.Close(FPDFBookmark_GetTitle( |
532 &title, title_length, true); | 532 bookmark, api_string_adapter.GetData(), buffer_size)); |
533 void* data = api_string_adapter.GetData(); | |
534 FPDFBookmark_GetTitle(bookmark, data, buffer_size); | |
535 api_string_adapter.Close(title_length); | |
536 } | 533 } |
537 dict.Set(pp::Var("title"), pp::Var(base::UTF16ToUTF8(title))); | 534 dict.Set(pp::Var("title"), pp::Var(base::UTF16ToUTF8(title))); |
538 | 535 |
539 FPDF_DEST dest = FPDFBookmark_GetDest(doc, bookmark); | 536 FPDF_DEST dest = FPDFBookmark_GetDest(doc, bookmark); |
540 // Some bookmarks don't have a page to select. | 537 // Some bookmarks don't have a page to select. |
541 if (dest) { | 538 if (dest) { |
542 int page_index = FPDFDest_GetPageIndex(doc, dest); | 539 int page_index = FPDFDest_GetPageIndex(doc, dest); |
543 dict.Set(pp::Var("page"), pp::Var(page_index)); | 540 dict.Set(pp::Var("page"), pp::Var(page_index)); |
544 } | 541 } |
545 | 542 |
(...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1169 } | 1166 } |
1170 | 1167 |
1171 void PDFiumEngine::SetScrollPosition(const pp::Point& position) { | 1168 void PDFiumEngine::SetScrollPosition(const pp::Point& position) { |
1172 position_ = position; | 1169 position_ = position; |
1173 } | 1170 } |
1174 | 1171 |
1175 bool PDFiumEngine::IsProgressiveLoad() { | 1172 bool PDFiumEngine::IsProgressiveLoad() { |
1176 return doc_loader_.is_partial_document(); | 1173 return doc_loader_.is_partial_document(); |
1177 } | 1174 } |
1178 | 1175 |
1176 std::string PDFiumEngine::GetMetadata(const std::string& key) { | |
1177 size_t size = FPDF_GetMetaText(doc(), key.c_str(), nullptr, 0); | |
1178 if (size == 0) | |
1179 return std::string(); | |
1180 | |
1181 base::string16 value; | |
1182 PDFiumAPIStringBufferSizeInBytesAdapter<base::string16> string_adapter( | |
1183 &value, size, false); | |
1184 string_adapter.Close( | |
1185 FPDF_GetMetaText(doc(), key.c_str(), string_adapter.GetData(), size)); | |
1186 return base::UTF16ToUTF8(value); | |
1187 } | |
1188 | |
1179 void PDFiumEngine::OnPartialDocumentLoaded() { | 1189 void PDFiumEngine::OnPartialDocumentLoaded() { |
1180 file_access_.m_FileLen = doc_loader_.document_size(); | 1190 file_access_.m_FileLen = doc_loader_.document_size(); |
1181 fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_); | 1191 fpdf_availability_ = FPDFAvail_Create(&file_availability_, &file_access_); |
1182 DCHECK(fpdf_availability_); | 1192 DCHECK(fpdf_availability_); |
1183 | 1193 |
1184 // Currently engine does not deal efficiently with some non-linearized files. | 1194 // Currently engine does not deal efficiently with some non-linearized files. |
1185 // See http://code.google.com/p/chromium/issues/detail?id=59400 | 1195 // See http://code.google.com/p/chromium/issues/detail?id=59400 |
1186 // To improve user experience we download entire file for non-linearized PDF. | 1196 // To improve user experience we download entire file for non-linearized PDF. |
1187 if (!FPDFAvail_IsLinearized(fpdf_availability_)) { | 1197 if (!FPDFAvail_IsLinearized(fpdf_availability_)) { |
1188 doc_loader_.RequestData(0, doc_loader_.document_size()); | 1198 doc_loader_.RequestData(0, doc_loader_.document_size()); |
(...skipping 2735 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3924 IntersectClipRect(dc, settings.bounds.x(), settings.bounds.y(), | 3934 IntersectClipRect(dc, settings.bounds.x(), settings.bounds.y(), |
3925 settings.bounds.x() + settings.bounds.width(), | 3935 settings.bounds.x() + settings.bounds.width(), |
3926 settings.bounds.y() + settings.bounds.height()); | 3936 settings.bounds.y() + settings.bounds.height()); |
3927 | 3937 |
3928 // A temporary hack. PDFs generated by Cairo (used by Chrome OS to generate | 3938 // A temporary hack. PDFs generated by Cairo (used by Chrome OS to generate |
3929 // a PDF output from a webpage) result in very large metafiles and the | 3939 // a PDF output from a webpage) result in very large metafiles and the |
3930 // rendering using FPDF_RenderPage is incorrect. In this case, render as a | 3940 // rendering using FPDF_RenderPage is incorrect. In this case, render as a |
3931 // bitmap. Note that this code does not kick in for PDFs printed from Chrome | 3941 // bitmap. Note that this code does not kick in for PDFs printed from Chrome |
3932 // because in that case we create a temp PDF first before printing and this | 3942 // because in that case we create a temp PDF first before printing and this |
3933 // temp PDF does not have a creator string that starts with "cairo". | 3943 // temp PDF does not have a creator string that starts with "cairo". |
3934 base::string16 creator; | 3944 bool use_bitmap = false; |
3935 size_t buffer_bytes = FPDF_GetMetaText(doc, "Creator", NULL, 0); | 3945 if (base::StartsWith(GetMetadata("Creator"), "cairo", |
3936 if (buffer_bytes > 1) { | 3946 base::CompareCase::INSENSITIVE_ASCII)) { |
3937 FPDF_GetMetaText(doc, "Creator", | 3947 use_bitmap = true; |
3938 base::WriteInto(&creator, buffer_bytes + 1), buffer_bytes); | |
3939 } | 3948 } |
3940 bool use_bitmap = false; | |
3941 if (base::StartsWith(creator, L"cairo", base::CompareCase::INSENSITIVE_ASCII)) | |
3942 use_bitmap = true; | |
3943 | 3949 |
3944 // Another temporary hack. Some PDFs seems to render very slowly if | 3950 // Another temporary hack. Some PDFs seems to render very slowly if |
3945 // FPDF_RenderPage is directly used on a printer DC. I suspect it is | 3951 // FPDF_RenderPage is directly used on a printer DC. I suspect it is |
3946 // because of the code to talk Postscript directly to the printer if | 3952 // because of the code to talk Postscript directly to the printer if |
3947 // the printer supports this. Need to discuss this with PDFium. For now, | 3953 // the printer supports this. Need to discuss this with PDFium. For now, |
3948 // render to a bitmap and then blit the bitmap to the DC if we have been | 3954 // render to a bitmap and then blit the bitmap to the DC if we have been |
3949 // supplied a printer DC. | 3955 // supplied a printer DC. |
3950 int device_type = GetDeviceCaps(dc, TECHNOLOGY); | 3956 int device_type = GetDeviceCaps(dc, TECHNOLOGY); |
3951 if (use_bitmap || | 3957 if (use_bitmap || |
3952 (device_type == DT_RASPRINTER) || (device_type == DT_PLOTTER)) { | 3958 (device_type == DT_RASPRINTER) || (device_type == DT_PLOTTER)) { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4052 double* height) { | 4058 double* height) { |
4053 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); | 4059 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); |
4054 if (!doc) | 4060 if (!doc) |
4055 return false; | 4061 return false; |
4056 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; | 4062 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; |
4057 FPDF_CloseDocument(doc); | 4063 FPDF_CloseDocument(doc); |
4058 return success; | 4064 return success; |
4059 } | 4065 } |
4060 | 4066 |
4061 } // namespace chrome_pdf | 4067 } // namespace chrome_pdf |
OLD | NEW |