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

Unified Diff: pdf/pdfium/pdfium_engine.cc

Issue 2524143003: Print Preview: Add option to rasterize PDFs and add JPEG compression. (Closed)
Patch Set: Clean up JS Created 4 years 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 side-by-side diff with in-line comments
Download patch
Index: pdf/pdfium/pdfium_engine.cc
diff --git a/pdf/pdfium/pdfium_engine.cc b/pdf/pdfium/pdfium_engine.cc
index 35c28964955714d9612abb991075277b904480e0..63f497c83738aab6b163375ba9a19e28488d46b3 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,17 @@ void TearDownV8() {
g_isolate_holder = nullptr;
}
+int GetBlockForJpeg(void* param,
+ unsigned long pos,
+ unsigned char* buf,
+ unsigned long size) {
+ std::vector<uint8_t>* data_vector = static_cast<std::vector<uint8_t>*>(param);
+ if (pos + size < pos || pos + size > data_vector->size())
+ return 0;
+ memcpy(buf, data_vector->data() + pos, size);
+ return 1;
+}
+
} // namespace
bool InitializeSDK() {
@@ -1353,9 +1365,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 +1380,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 +1429,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 +1441,25 @@ 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;
Lei Zhang 2017/02/25 01:01:16 const + add comment to mention why we chose 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))) {
+ FPDF_FILEACCESS file_access = {};
+ file_access.m_FileLen =
+ static_cast<unsigned long>(compressed_bitmap_data.size());
+ file_access.m_GetBlock = &GetBlockForJpeg;
+ file_access.m_Param = &compressed_bitmap_data;
+
+ 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);

Powered by Google App Engine
This is Rietveld 408576698