Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(509)

Side by Side Diff: pdf/pdfium/pdfium_engine.cc

Issue 485413002: Extracted method to CreateSinglePageRasterPdf() to simplify PrintPagesAsRasterPDF(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Tue Aug 19 18:41:30 PDT 2014 Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pdf/pdfium/pdfium_engine.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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_doc);
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 return temp_doc;
1043 }
1044
985 pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF( 1045 pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF(
986 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, 1046 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count,
987 const PP_PrintSettings_Dev& print_settings) { 1047 const PP_PrintSettings_Dev& print_settings) {
988 if (!page_range_count) 1048 if (!page_range_count)
989 return pp::Buffer_Dev(); 1049 return pp::Buffer_Dev();
990 1050
991 // If document is not downloaded yet, disable printing. 1051 // If document is not downloaded yet, disable printing.
992 if (doc_ && !doc_loader_.IsDocumentComplete()) 1052 if (doc_ && !doc_loader_.IsDocumentComplete())
993 return pp::Buffer_Dev(); 1053 return pp::Buffer_Dev();
994 1054
(...skipping 30 matching lines...) Expand all
1025 } 1085 }
1026 1086
1027 #if defined(OS_LINUX) 1087 #if defined(OS_LINUX)
1028 g_last_instance_id = client_->GetPluginInstance()->pp_instance(); 1088 g_last_instance_id = client_->GetPluginInstance()->pp_instance();
1029 #endif 1089 #endif
1030 1090
1031 size_t i = 0; 1091 size_t i = 0;
1032 for (; i < pages_to_print.size(); ++i) { 1092 for (; i < pages_to_print.size(); ++i) {
1033 double source_page_width = source_page_sizes[i].first; 1093 double source_page_width = source_page_sizes[i].first;
1034 double source_page_height = source_page_sizes[i].second; 1094 double source_page_height = source_page_sizes[i].second;
1035 const pp::Size& bitmap_size(pages_to_print[i].rect().size());
1036 1095
1037 // Use temp_doc to compress image by saving PDF to buffer. 1096 // Use temp_doc to compress image by saving PDF to buffer.
1038 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument(); 1097 FPDF_DOCUMENT temp_doc = CreateSinglePageRasterPdf(source_page_width,
1098 source_page_height,
1099 print_settings,
1100 &pages_to_print[i]);
1101
1039 if (!temp_doc) 1102 if (!temp_doc)
1040 break; 1103 break;
1041 1104
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); 1105 pp::Buffer_Dev buffer = GetFlattenedPrintData(temp_doc);
1085 FPDF_CloseDocument(temp_doc); 1106 FPDF_CloseDocument(temp_doc);
1086 1107
1087 PDFiumMemBufferFileRead file_read(buffer.data(), buffer.size()); 1108 PDFiumMemBufferFileRead file_read(buffer.data(), buffer.size());
Vitaly Buka (NO REVIEWS) 2014/08/20 01:43:40 Moved out flattening, because buffer must be alive
Bo Xu 2014/08/20 03:21:26 Just curious why buffer has to be alive here? Beca
Vitaly Buka (NO REVIEWS) 2014/08/20 05:39:01 yes
1088 temp_doc = FPDF_LoadCustomDocument(&file_read, NULL); 1109 temp_doc = FPDF_LoadCustomDocument(&file_read, NULL);
1089 if (!temp_doc)
1090 break;
1091 1110
1092 FPDF_BOOL imported = FPDF_ImportPages(output_doc, temp_doc, "1", i); 1111 FPDF_BOOL imported = FPDF_ImportPages(output_doc, temp_doc, "1", i);
1093 FPDF_CloseDocument(temp_doc); 1112 FPDF_CloseDocument(temp_doc);
1094 if (!imported) 1113 if (!imported)
1095 break; 1114 break;
1096 } 1115 }
1097 1116
1098 pp::Buffer_Dev buffer; 1117 pp::Buffer_Dev buffer;
1099 if (i == pages_to_print.size()) { 1118 if (i == pages_to_print.size()) {
1100 FPDF_CopyViewerPreferences(output_doc, doc_); 1119 FPDF_CopyViewerPreferences(output_doc, doc_);
(...skipping 2302 matching lines...) Expand 10 before | Expand all | Expand 10 after
3403 double* height) { 3422 double* height) {
3404 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); 3423 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL);
3405 if (!doc) 3424 if (!doc)
3406 return false; 3425 return false;
3407 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; 3426 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
3408 FPDF_CloseDocument(doc); 3427 FPDF_CloseDocument(doc);
3409 return success; 3428 return success;
3410 } 3429 }
3411 3430
3412 } // namespace chrome_pdf 3431 } // namespace chrome_pdf
OLDNEW
« no previous file with comments | « pdf/pdfium/pdfium_engine.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698