Index: pdf/pdfium/pdfium_engine.cc |
diff --git a/pdf/pdfium/pdfium_engine.cc b/pdf/pdfium/pdfium_engine.cc |
index 0853d79beb294ae9a103eb0e065615401ec53148..d9701332c76f7469f1d93325715e3671b145c801 100644 |
--- a/pdf/pdfium/pdfium_engine.cc |
+++ b/pdf/pdfium/pdfium_engine.cc |
@@ -57,6 +57,7 @@ |
#include "third_party/pdfium/public/fpdf_transformpage.h" |
#include "third_party/pdfium/public/fpdfview.h" |
#include "ui/events/keycodes/keyboard_codes.h" |
+#include "ui/gfx/codec/jpeg_codec.h" |
#include "ui/gfx/geometry/rect.h" |
#include "v8/include/v8.h" |
@@ -1250,7 +1251,6 @@ void PDFiumEngine::CancelBrowserDownload() { |
void PDFiumEngine::FinishLoadingDocument() { |
DCHECK(doc_loader_->IsDocumentComplete() && doc_); |
- |
if (!form_) { |
int form_status = |
FPDFAvail_IsFormAvail(fpdf_availability_, &download_hints_); |
@@ -1390,7 +1390,9 @@ bool PDFiumEngine::HandleEvent(const pp::InputEvent& event) { |
uint32_t PDFiumEngine::QuerySupportedPrintOutputFormats() { |
if (!HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) |
return 0; |
- return PP_PRINTOUTPUTFORMAT_PDF; |
+ if (!HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY)) |
+ return PP_PRINTOUTPUTFORMAT_RASTER; |
+ return PP_PRINTOUTPUTFORMAT_PDF | PP_PRINTOUTPUTFORMAT_RASTER; |
} |
void PDFiumEngine::PrintBegin() { |
@@ -1401,13 +1403,32 @@ pp::Resource PDFiumEngine::PrintPages( |
const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, |
const PP_PrintSettings_Dev& print_settings) { |
ScopedSubstFont scoped_subst_font(this); |
- if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY)) |
+ if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY) && |
+ (print_settings.format & PP_PRINTOUTPUTFORMAT_PDF)) { |
return PrintPagesAsPDF(page_ranges, page_range_count, print_settings); |
- else if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) |
+ } else if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) { |
return PrintPagesAsRasterPDF(page_ranges, page_range_count, print_settings); |
+ } |
+ |
return pp::Resource(); |
} |
+struct simple_buf { |
+ uint32_t size; |
+ uint8_t *data; |
+}; |
+ |
+int GetBlockForJpeg(void *param, |
+ unsigned long pos, |
+ unsigned char* pBuf, |
+ unsigned long size) { |
+ simple_buf * buf_p = static_cast<simple_buf *>(param); |
+ if (pos + size < pos || pos + size > buf_p->size) |
+ return 0; |
+ memcpy(pBuf, buf_p->data, size); |
+ return size; |
+} |
+ |
FPDF_DOCUMENT PDFiumEngine::CreateSinglePageRasterPdf( |
double source_page_width, |
double source_page_height, |
@@ -1447,6 +1468,8 @@ FPDF_DOCUMENT PDFiumEngine::CreateSinglePageRasterPdf( |
print_settings.orientation, |
FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); |
+ unsigned char * bitmap_data = |
+ static_cast<unsigned char *>(FPDFBitmap_GetBuffer(bitmap)); |
double ratio_x = ConvertUnitDouble(bitmap_size.width(), |
print_settings.dpi, |
kPointsPerInch); |
@@ -1457,7 +1480,37 @@ FPDF_DOCUMENT PDFiumEngine::CreateSinglePageRasterPdf( |
// Add the bitmap to an image object and add the image object to the output |
// page. |
FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImgeObj(temp_doc); |
- FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap); |
+ |
+ std::vector <unsigned char> compressed_bitmap_data; |
+ int quality = 40; |
+ bool encode_success = gfx::JPEGCodec::Encode(bitmap_data, |
+ gfx::JPEGCodec::FORMAT_BGRA, |
+ FPDFBitmap_GetWidth(bitmap), |
+ FPDFBitmap_GetHeight(bitmap), |
+ FPDFBitmap_GetStride(bitmap), |
+ quality, |
+ &compressed_bitmap_data); |
+ |
+// encode_success = false; |
Vitaly Buka (NO REVIEWS)
2016/12/15 08:27:34
please remove
rbpotter
2016/12/16 00:06:30
This is already removed in the latest patchset.
|
+ if (encode_success) { |
+ simple_buf buffer; |
+ buffer.size = compressed_bitmap_data.size(); |
+ buffer.data = new uint8_t[buffer.size]; |
+ memcpy(buffer.data, &(compressed_bitmap_data[0]), buffer.size); |
+ |
+ FPDF_FILEACCESS file_access; |
+ memset(&file_access, '\0', sizeof(file_access)); |
+ file_access.m_FileLen = |
+ static_cast<unsigned long>(compressed_bitmap_data.size()); |
+ file_access.m_GetBlock = &GetBlockForJpeg; |
+ file_access.m_Param = &buffer; |
+ |
+ FPDFImageObj_LoadJpegFile(&temp_page, 1, temp_img, &file_access); |
+ delete buffer.data; |
+ } else { |
+ FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap); |
+ } |
+ |
FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0); |
FPDFPage_InsertObject(temp_page, temp_img); |
FPDFPage_GenerateContent(temp_page); |
@@ -1496,7 +1549,7 @@ pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF( |
double source_page_width = FPDF_GetPageWidth(pdf_page); |
double source_page_height = FPDF_GetPageHeight(pdf_page); |
source_page_sizes.push_back(std::make_pair(source_page_width, |
- source_page_height)); |
+ source_page_height)); |
int width_in_pixels = ConvertUnit(source_page_width, |
kPointsPerInch, |
@@ -1515,25 +1568,25 @@ pp::Buffer_Dev PDFiumEngine::PrintPagesAsRasterPDF( |
#endif |
size_t i = 0; |
+ unsigned long total_doc_size = 0; |
for (; i < pages_to_print.size(); ++i) { |
Vitaly Buka (NO REVIEWS)
2016/12/15 08:27:34
Seems total_doc_size is not used.
rbpotter
2016/12/16 00:06:30
This is already removed in the latest patchset.
|
double source_page_width = source_page_sizes[i].first; |
double source_page_height = source_page_sizes[i].second; |
// Use temp_doc to compress image by saving PDF to buffer. |
FPDF_DOCUMENT temp_doc = CreateSinglePageRasterPdf(source_page_width, |
- source_page_height, |
- print_settings, |
- &pages_to_print[i]); |
- |
+ source_page_height, |
+ print_settings, |
+ &pages_to_print[i]); |
if (!temp_doc) |
break; |
pp::Buffer_Dev buffer = GetFlattenedPrintData(temp_doc); |
+ total_doc_size += buffer.size(); |
FPDF_CloseDocument(temp_doc); |
PDFiumMemBufferFileRead file_read(buffer.data(), buffer.size()); |
temp_doc = FPDF_LoadCustomDocument(&file_read, nullptr); |
- |
FPDF_BOOL imported = FPDF_ImportPages(output_doc, temp_doc, "1", i); |
FPDF_CloseDocument(temp_doc); |
if (!imported) |