Chromium Code Reviews| Index: pdf/pdfium/pdfium_engine.cc |
| diff --git a/pdf/pdfium/pdfium_engine.cc b/pdf/pdfium/pdfium_engine.cc |
| index 35c28964955714d9612abb991075277b904480e0..6bfb7b5037ded6cdc57cfc87518b10fa908f3830 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" |
| @@ -617,6 +618,22 @@ void TearDownV8() { |
| g_isolate_holder = nullptr; |
| } |
| +struct simple_buf { |
|
Tom Sepez
2016/12/15 21:09:23
actually, can we avoid this altogether by passing
rbpotter
2016/12/16 00:06:31
Yes, done
|
| + size_t size; |
| + uint8_t* data; |
| +}; |
| + |
| +int GetBlockForJpeg(void* param, |
| + unsigned long pos, |
| + unsigned char* buf, |
| + unsigned long size) { |
| + simple_buf* buf_p = static_cast<simple_buf*>(param); |
| + if (pos + size < pos || pos + size > buf_p->size) |
| + return 0; |
| + memcpy(buf, buf_p->data + pos, size); |
| + return 1; |
| +} |
| + |
| } // namespace |
| bool InitializeSDK() { |
| @@ -1353,9 +1370,11 @@ 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_PDF | PP_PRINTOUTPUTFORMAT_RASTER; |
| + if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) |
| + return PP_PRINTOUTPUTFORMAT_RASTER; |
| + return 0; |
| } |
| void PDFiumEngine::PrintBegin() { |
| @@ -1366,10 +1385,13 @@ 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(); |
| } |
| @@ -1412,6 +1434,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); |
| @@ -1422,7 +1446,30 @@ 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<uint8_t> compressed_bitmap_data; |
| + int quality = 40; |
| + if (!(print_settings.format & PP_PRINTOUTPUTFORMAT_PDF) && |
| + (gfx::JPEGCodec::Encode( |
| + bitmap_data, gfx::JPEGCodec::FORMAT_BGRA, FPDFBitmap_GetWidth(bitmap), |
| + FPDFBitmap_GetHeight(bitmap), FPDFBitmap_GetStride(bitmap), quality, |
| + &compressed_bitmap_data))) { |
| + simple_buf buffer; |
| + buffer.size = compressed_bitmap_data.size(); |
| + buffer.data = compressed_bitmap_data.data(); |
| + |
| + 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_LoadJpegFileInline(&temp_page, 1, temp_img, &file_access); |
| + } 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); |