Chromium Code Reviews| 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 964 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 975 pp::Resource PDFiumEngine::PrintPages( | 975 pp::Resource PDFiumEngine::PrintPages( |
| 976 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, | 976 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, |
| 977 const PP_PrintSettings_Dev& print_settings) { | 977 const PP_PrintSettings_Dev& print_settings) { |
| 978 if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY)) | 978 if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY)) |
| 979 return PrintPagesAsPDF(page_ranges, page_range_count, print_settings); | 979 return PrintPagesAsPDF(page_ranges, page_range_count, print_settings); |
| 980 else if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) | 980 else if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) |
| 981 return PrintPagesAsRasterPDF(page_ranges, page_range_count, print_settings); | 981 return PrintPagesAsRasterPDF(page_ranges, page_range_count, print_settings); |
| 982 return pp::Resource(); | 982 return pp::Resource(); |
| 983 } | 983 } |
| 984 | 984 |
| 985 FPDF_DOCUMENT PDFiumEngine::CreateSinglePageRasterPdf( | |
| 986 double source_page_width, | |
| 987 double source_page_height, | |
| 988 const PP_PrintSettings_Dev& print_settings, | |
| 989 PDFiumPage* page_to_print) { | |
| 990 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument(); | |
| 991 if (!temp_doc) | |
| 992 return temp_doc; | |
| 993 | |
| 994 const pp::Size& bitmap_size(page_to_print->rect().size()); | |
| 995 | |
| 996 FPDF_PAGE temp_page = | |
| 997 FPDFPage_New(temp_doc, 0, source_page_width, source_page_height); | |
| 998 | |
| 999 pp::ImageData image = pp::ImageData(client_->GetPluginInstance(), | |
| 1000 PP_IMAGEDATAFORMAT_BGRA_PREMUL, | |
| 1001 bitmap_size, | |
| 1002 false); | |
| 1003 | |
| 1004 FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(bitmap_size.width(), | |
| 1005 bitmap_size.height(), | |
| 1006 FPDFBitmap_BGRx, | |
| 1007 image.data(), | |
| 1008 image.stride()); | |
| 1009 | |
| 1010 // Clear the bitmap | |
| 1011 FPDFBitmap_FillRect( | |
| 1012 bitmap, 0, 0, bitmap_size.width(), bitmap_size.height(), 0xFFFFFFFF); | |
| 1013 | |
| 1014 pp::Rect page_rect = page_to_print->rect(); | |
| 1015 FPDF_RenderPageBitmap(bitmap, | |
| 1016 page_to_print->GetPrintPage(), | |
| 1017 page_rect.x(), | |
| 1018 page_rect.y(), | |
| 1019 page_rect.width(), | |
| 1020 page_rect.height(), | |
| 1021 print_settings.orientation, | |
| 1022 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); | |
| 1023 | |
| 1024 double ratio_x = (static_cast<double>(bitmap_size.width()) * kPointsPerInch) / | |
| 1025 print_settings.dpi; | |
| 1026 double ratio_y = | |
| 1027 (static_cast<double>(bitmap_size.height()) * kPointsPerInch) / | |
| 1028 print_settings.dpi; | |
| 1029 | |
| 1030 // Add the bitmap to an image object and add the image object to the output | |
| 1031 // page. | |
| 1032 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImgeObj(temp_page); | |
|
Bo Xu
2014/08/20 00:50:48
here should be
FPDF_PAGEOBJECT temp_img = FPDFPa
Vitaly Buka (NO REVIEWS)
2014/08/20 01:43:40
Done. this also didn't crash :-)
| |
| 1033 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap); | |
| 1034 FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0); | |
| 1035 FPDFPage_InsertObject(temp_page, temp_img); | |
| 1036 FPDFPage_GenerateContent(temp_page); | |
| 1037 FPDF_ClosePage(temp_page); | |
| 1038 | |
| 1039 page_to_print->ClosePrintPage(); | |
| 1040 FPDFBitmap_Destroy(bitmap); | |
| 1041 | |
| 1042 pp::Buffer_Dev buffer = GetFlattenedPrintData(temp_doc); | |
| 1043 FPDF_CloseDocument(temp_doc); | |
| 1044 | |
| 1045 PDFiumMemBufferFileRead file_read(buffer.data(), buffer.size()); | |
| 1046 return FPDF_LoadCustomDocument(&file_read, NULL); | |
| 1047 } | |
| 1048 | |
| 985 pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF( | 1049 pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF( |
| 986 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, | 1050 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, |
| 987 const PP_PrintSettings_Dev& print_settings) { | 1051 const PP_PrintSettings_Dev& print_settings) { |
| 988 if (!page_range_count) | 1052 if (!page_range_count) |
| 989 return pp::Buffer_Dev(); | 1053 return pp::Buffer_Dev(); |
| 990 | 1054 |
| 991 // If document is not downloaded yet, disable printing. | 1055 // If document is not downloaded yet, disable printing. |
| 992 if (doc_ && !doc_loader_.IsDocumentComplete()) | 1056 if (doc_ && !doc_loader_.IsDocumentComplete()) |
| 993 return pp::Buffer_Dev(); | 1057 return pp::Buffer_Dev(); |
| 994 | 1058 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 1025 } | 1089 } |
| 1026 | 1090 |
| 1027 #if defined(OS_LINUX) | 1091 #if defined(OS_LINUX) |
| 1028 g_last_instance_id = client_->GetPluginInstance()->pp_instance(); | 1092 g_last_instance_id = client_->GetPluginInstance()->pp_instance(); |
| 1029 #endif | 1093 #endif |
| 1030 | 1094 |
| 1031 size_t i = 0; | 1095 size_t i = 0; |
| 1032 for (; i < pages_to_print.size(); ++i) { | 1096 for (; i < pages_to_print.size(); ++i) { |
| 1033 double source_page_width = source_page_sizes[i].first; | 1097 double source_page_width = source_page_sizes[i].first; |
| 1034 double source_page_height = source_page_sizes[i].second; | 1098 double source_page_height = source_page_sizes[i].second; |
| 1035 const pp::Size& bitmap_size(pages_to_print[i].rect().size()); | |
| 1036 | 1099 |
| 1037 // Use temp_doc to compress image by saving PDF to buffer. | 1100 // Use temp_doc to compress image by saving PDF to buffer. |
| 1038 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument(); | 1101 FPDF_DOCUMENT temp_doc = CreateSinglePageRasterPdf(source_page_width, |
| 1102 source_page_height, | |
| 1103 print_settings, | |
| 1104 &pages_to_print[i]); | |
| 1105 | |
| 1039 if (!temp_doc) | 1106 if (!temp_doc) |
| 1040 break; | 1107 break; |
| 1041 | 1108 |
| 1042 FPDF_PAGE output_page = FPDFPage_New(temp_doc, 0, source_page_width, | |
| 1043 source_page_height); | |
| 1044 | |
| 1045 pp::ImageData image = pp::ImageData(client_->GetPluginInstance(), | |
| 1046 PP_IMAGEDATAFORMAT_BGRA_PREMUL, | |
| 1047 bitmap_size, | |
| 1048 false); | |
| 1049 | |
| 1050 FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(bitmap_size.width(), | |
| 1051 bitmap_size.height(), | |
| 1052 FPDFBitmap_BGRx, | |
| 1053 image.data(), | |
| 1054 image.stride()); | |
| 1055 | |
| 1056 // Clear the bitmap | |
| 1057 FPDFBitmap_FillRect(bitmap, 0, 0, bitmap_size.width(), | |
| 1058 bitmap_size.height(), 0xFFFFFFFF); | |
| 1059 | |
| 1060 pp::Rect page_rect = pages_to_print[i].rect(); | |
| 1061 FPDF_RenderPageBitmap(bitmap, pages_to_print[i].GetPrintPage(), | |
| 1062 page_rect.x(), page_rect.y(), | |
| 1063 page_rect.width(), page_rect.height(), | |
| 1064 print_settings.orientation, | |
| 1065 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); | |
| 1066 | |
| 1067 double ratio_x = (static_cast<double>(bitmap_size.width()) * | |
| 1068 kPointsPerInch) / print_settings.dpi; | |
| 1069 double ratio_y = (static_cast<double>(bitmap_size.height()) * | |
| 1070 kPointsPerInch) / print_settings.dpi; | |
| 1071 | |
| 1072 // Add the bitmap to an image object and add the image object to the output | |
| 1073 // page. | |
| 1074 FPDF_PAGEOBJECT img_obj = FPDFPageObj_NewImgeObj(output_doc); | |
| 1075 FPDFImageObj_SetBitmap(&output_page, 1, img_obj, bitmap); | |
| 1076 FPDFImageObj_SetMatrix(img_obj, ratio_x, 0, 0, ratio_y, 0, 0); | |
| 1077 FPDFPage_InsertObject(output_page, img_obj); | |
| 1078 FPDFPage_GenerateContent(output_page); | |
| 1079 FPDF_ClosePage(output_page); | |
| 1080 | |
| 1081 pages_to_print[i].ClosePrintPage(); | |
| 1082 FPDFBitmap_Destroy(bitmap); | |
| 1083 | |
| 1084 pp::Buffer_Dev buffer = GetFlattenedPrintData(temp_doc); | |
| 1085 FPDF_CloseDocument(temp_doc); | |
| 1086 | |
| 1087 PDFiumMemBufferFileRead file_read(buffer.data(), buffer.size()); | |
| 1088 temp_doc = FPDF_LoadCustomDocument(&file_read, NULL); | |
| 1089 if (!temp_doc) | |
| 1090 break; | |
| 1091 | |
| 1092 FPDF_BOOL imported = FPDF_ImportPages(output_doc, temp_doc, "1", i); | 1109 FPDF_BOOL imported = FPDF_ImportPages(output_doc, temp_doc, "1", i); |
| 1093 FPDF_CloseDocument(temp_doc); | 1110 FPDF_CloseDocument(temp_doc); |
| 1094 if (!imported) | 1111 if (!imported) |
| 1095 break; | 1112 break; |
| 1096 } | 1113 } |
| 1097 | 1114 |
| 1098 pp::Buffer_Dev buffer; | 1115 pp::Buffer_Dev buffer; |
| 1099 if (i == pages_to_print.size()) { | 1116 if (i == pages_to_print.size()) { |
| 1100 FPDF_CopyViewerPreferences(output_doc, doc_); | 1117 FPDF_CopyViewerPreferences(output_doc, doc_); |
| 1101 FitContentsToPrintableAreaIfRequired(output_doc, print_settings); | 1118 FitContentsToPrintableAreaIfRequired(output_doc, print_settings); |
| (...skipping 2301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3403 double* height) { | 3420 double* height) { |
| 3404 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); | 3421 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); |
| 3405 if (!doc) | 3422 if (!doc) |
| 3406 return false; | 3423 return false; |
| 3407 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; | 3424 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; |
| 3408 FPDF_CloseDocument(doc); | 3425 FPDF_CloseDocument(doc); |
| 3409 return success; | 3426 return success; |
| 3410 } | 3427 } |
| 3411 | 3428 |
| 3412 } // namespace chrome_pdf | 3429 } // namespace chrome_pdf |
| OLD | NEW |