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

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

Issue 2524143003: Print Preview: Add option to rasterize PDFs and add JPEG compression. (Closed)
Patch Set: 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 unified diff | Download patch
« no previous file with comments | « pdf/pdfium/DEPS ('k') | printing/print_job_constants.h » ('j') | 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 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 #include "third_party/pdfium/public/fpdf_edit.h" 50 #include "third_party/pdfium/public/fpdf_edit.h"
51 #include "third_party/pdfium/public/fpdf_ext.h" 51 #include "third_party/pdfium/public/fpdf_ext.h"
52 #include "third_party/pdfium/public/fpdf_flatten.h" 52 #include "third_party/pdfium/public/fpdf_flatten.h"
53 #include "third_party/pdfium/public/fpdf_ppo.h" 53 #include "third_party/pdfium/public/fpdf_ppo.h"
54 #include "third_party/pdfium/public/fpdf_save.h" 54 #include "third_party/pdfium/public/fpdf_save.h"
55 #include "third_party/pdfium/public/fpdf_searchex.h" 55 #include "third_party/pdfium/public/fpdf_searchex.h"
56 #include "third_party/pdfium/public/fpdf_sysfontinfo.h" 56 #include "third_party/pdfium/public/fpdf_sysfontinfo.h"
57 #include "third_party/pdfium/public/fpdf_transformpage.h" 57 #include "third_party/pdfium/public/fpdf_transformpage.h"
58 #include "third_party/pdfium/public/fpdfview.h" 58 #include "third_party/pdfium/public/fpdfview.h"
59 #include "ui/events/keycodes/keyboard_codes.h" 59 #include "ui/events/keycodes/keyboard_codes.h"
60 #include "ui/gfx/codec/jpeg_codec.h"
60 #include "ui/gfx/geometry/rect.h" 61 #include "ui/gfx/geometry/rect.h"
61 #include "v8/include/v8.h" 62 #include "v8/include/v8.h"
62 63
63 using printing::ConvertUnit; 64 using printing::ConvertUnit;
64 using printing::ConvertUnitDouble; 65 using printing::ConvertUnitDouble;
65 using printing::kPointsPerInch; 66 using printing::kPointsPerInch;
66 using printing::kPixelsPerInch; 67 using printing::kPixelsPerInch;
67 68
68 namespace chrome_pdf { 69 namespace chrome_pdf {
69 70
(...skipping 1173 matching lines...) Expand 10 before | Expand all | Expand 10 after
1243 else 1244 else
1244 OnDocumentComplete(); 1245 OnDocumentComplete();
1245 } 1246 }
1246 1247
1247 void PDFiumEngine::CancelBrowserDownload() { 1248 void PDFiumEngine::CancelBrowserDownload() {
1248 client_->CancelBrowserDownload(); 1249 client_->CancelBrowserDownload();
1249 } 1250 }
1250 1251
1251 void PDFiumEngine::FinishLoadingDocument() { 1252 void PDFiumEngine::FinishLoadingDocument() {
1252 DCHECK(doc_loader_->IsDocumentComplete() && doc_); 1253 DCHECK(doc_loader_->IsDocumentComplete() && doc_);
1253
1254 if (!form_) { 1254 if (!form_) {
1255 int form_status = 1255 int form_status =
1256 FPDFAvail_IsFormAvail(fpdf_availability_, &download_hints_); 1256 FPDFAvail_IsFormAvail(fpdf_availability_, &download_hints_);
1257 if (form_status != PDF_FORM_NOTAVAIL) { 1257 if (form_status != PDF_FORM_NOTAVAIL) {
1258 form_ = FPDFDOC_InitFormFillEnvironment( 1258 form_ = FPDFDOC_InitFormFillEnvironment(
1259 doc_, static_cast<FPDF_FORMFILLINFO*>(this)); 1259 doc_, static_cast<FPDF_FORMFILLINFO*>(this));
1260 #if defined(PDF_ENABLE_XFA) 1260 #if defined(PDF_ENABLE_XFA)
1261 FPDF_LoadXFA(doc_); 1261 FPDF_LoadXFA(doc_);
1262 #endif 1262 #endif
1263 1263
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
1383 defer_page_unload_ = false; 1383 defer_page_unload_ = false;
1384 for (int page_index : deferred_page_unloads_) 1384 for (int page_index : deferred_page_unloads_)
1385 pages_[page_index]->Unload(); 1385 pages_[page_index]->Unload();
1386 deferred_page_unloads_.clear(); 1386 deferred_page_unloads_.clear();
1387 return rv; 1387 return rv;
1388 } 1388 }
1389 1389
1390 uint32_t PDFiumEngine::QuerySupportedPrintOutputFormats() { 1390 uint32_t PDFiumEngine::QuerySupportedPrintOutputFormats() {
1391 if (!HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) 1391 if (!HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY))
1392 return 0; 1392 return 0;
1393 return PP_PRINTOUTPUTFORMAT_PDF; 1393 if (!HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY))
1394 return PP_PRINTOUTPUTFORMAT_RASTER;
1395 return PP_PRINTOUTPUTFORMAT_PDF | PP_PRINTOUTPUTFORMAT_RASTER;
1394 } 1396 }
1395 1397
1396 void PDFiumEngine::PrintBegin() { 1398 void PDFiumEngine::PrintBegin() {
1397 FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_WP); 1399 FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_WP);
1398 } 1400 }
1399 1401
1400 pp::Resource PDFiumEngine::PrintPages( 1402 pp::Resource PDFiumEngine::PrintPages(
1401 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, 1403 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count,
1402 const PP_PrintSettings_Dev& print_settings) { 1404 const PP_PrintSettings_Dev& print_settings) {
1403 ScopedSubstFont scoped_subst_font(this); 1405 ScopedSubstFont scoped_subst_font(this);
1404 if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY)) 1406 if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY) &&
1407 (print_settings.format & PP_PRINTOUTPUTFORMAT_PDF)) {
1405 return PrintPagesAsPDF(page_ranges, page_range_count, print_settings); 1408 return PrintPagesAsPDF(page_ranges, page_range_count, print_settings);
1406 else if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) 1409 } else if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) {
1407 return PrintPagesAsRasterPDF(page_ranges, page_range_count, print_settings); 1410 return PrintPagesAsRasterPDF(page_ranges, page_range_count, print_settings);
1411 }
1412
1408 return pp::Resource(); 1413 return pp::Resource();
1409 } 1414 }
1410 1415
1416 struct simple_buf {
1417 uint32_t size;
1418 uint8_t *data;
1419 };
1420
1421 int GetBlockForJpeg(void *param,
1422 unsigned long pos,
1423 unsigned char* pBuf,
1424 unsigned long size) {
1425 simple_buf * buf_p = static_cast<simple_buf *>(param);
1426 if (pos + size < pos || pos + size > buf_p->size)
1427 return 0;
1428 memcpy(pBuf, buf_p->data, size);
1429 return size;
1430 }
1431
1411 FPDF_DOCUMENT PDFiumEngine::CreateSinglePageRasterPdf( 1432 FPDF_DOCUMENT PDFiumEngine::CreateSinglePageRasterPdf(
1412 double source_page_width, 1433 double source_page_width,
1413 double source_page_height, 1434 double source_page_height,
1414 const PP_PrintSettings_Dev& print_settings, 1435 const PP_PrintSettings_Dev& print_settings,
1415 PDFiumPage* page_to_print) { 1436 PDFiumPage* page_to_print) {
1416 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument(); 1437 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
1417 if (!temp_doc) 1438 if (!temp_doc)
1418 return temp_doc; 1439 return temp_doc;
1419 1440
1420 const pp::Size& bitmap_size(page_to_print->rect().size()); 1441 const pp::Size& bitmap_size(page_to_print->rect().size());
(...skipping 19 matching lines...) Expand all
1440 pp::Rect page_rect = page_to_print->rect(); 1461 pp::Rect page_rect = page_to_print->rect();
1441 FPDF_RenderPageBitmap(bitmap, 1462 FPDF_RenderPageBitmap(bitmap,
1442 page_to_print->GetPrintPage(), 1463 page_to_print->GetPrintPage(),
1443 page_rect.x(), 1464 page_rect.x(),
1444 page_rect.y(), 1465 page_rect.y(),
1445 page_rect.width(), 1466 page_rect.width(),
1446 page_rect.height(), 1467 page_rect.height(),
1447 print_settings.orientation, 1468 print_settings.orientation,
1448 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); 1469 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
1449 1470
1471 unsigned char * bitmap_data =
1472 static_cast<unsigned char *>(FPDFBitmap_GetBuffer(bitmap));
1450 double ratio_x = ConvertUnitDouble(bitmap_size.width(), 1473 double ratio_x = ConvertUnitDouble(bitmap_size.width(),
1451 print_settings.dpi, 1474 print_settings.dpi,
1452 kPointsPerInch); 1475 kPointsPerInch);
1453 double ratio_y = ConvertUnitDouble(bitmap_size.height(), 1476 double ratio_y = ConvertUnitDouble(bitmap_size.height(),
1454 print_settings.dpi, 1477 print_settings.dpi,
1455 kPointsPerInch); 1478 kPointsPerInch);
1456 1479
1457 // Add the bitmap to an image object and add the image object to the output 1480 // Add the bitmap to an image object and add the image object to the output
1458 // page. 1481 // page.
1459 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImgeObj(temp_doc); 1482 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImgeObj(temp_doc);
1460 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap); 1483
1484 std::vector <unsigned char> compressed_bitmap_data;
1485 int quality = 40;
1486 bool encode_success = gfx::JPEGCodec::Encode(bitmap_data,
1487 gfx::JPEGCodec::FORMAT_BGRA,
1488 FPDFBitmap_GetWidth(bitmap),
1489 FPDFBitmap_GetHeight(bitmap),
1490 FPDFBitmap_GetStride(bitmap),
1491 quality,
1492 &compressed_bitmap_data);
1493
1494 // 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.
1495 if (encode_success) {
1496 simple_buf buffer;
1497 buffer.size = compressed_bitmap_data.size();
1498 buffer.data = new uint8_t[buffer.size];
1499 memcpy(buffer.data, &(compressed_bitmap_data[0]), buffer.size);
1500
1501 FPDF_FILEACCESS file_access;
1502 memset(&file_access, '\0', sizeof(file_access));
1503 file_access.m_FileLen =
1504 static_cast<unsigned long>(compressed_bitmap_data.size());
1505 file_access.m_GetBlock = &GetBlockForJpeg;
1506 file_access.m_Param = &buffer;
1507
1508 FPDFImageObj_LoadJpegFile(&temp_page, 1, temp_img, &file_access);
1509 delete buffer.data;
1510 } else {
1511 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap);
1512 }
1513
1461 FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0); 1514 FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0);
1462 FPDFPage_InsertObject(temp_page, temp_img); 1515 FPDFPage_InsertObject(temp_page, temp_img);
1463 FPDFPage_GenerateContent(temp_page); 1516 FPDFPage_GenerateContent(temp_page);
1464 FPDF_ClosePage(temp_page); 1517 FPDF_ClosePage(temp_page);
1465 1518
1466 page_to_print->ClosePrintPage(); 1519 page_to_print->ClosePrintPage();
1467 FPDFBitmap_Destroy(bitmap); 1520 FPDFBitmap_Destroy(bitmap);
1468 1521
1469 return temp_doc; 1522 return temp_doc;
1470 } 1523 }
(...skipping 18 matching lines...) Expand all
1489 // width and height of source PDF pages. 1542 // width and height of source PDF pages.
1490 std::vector<std::pair<double, double> > source_page_sizes; 1543 std::vector<std::pair<double, double> > source_page_sizes;
1491 // Collect pages to print and sizes of source pages. 1544 // Collect pages to print and sizes of source pages.
1492 std::vector<uint32_t> page_numbers = 1545 std::vector<uint32_t> page_numbers =
1493 GetPageNumbersFromPrintPageNumberRange(page_ranges, page_range_count); 1546 GetPageNumbersFromPrintPageNumberRange(page_ranges, page_range_count);
1494 for (uint32_t page_number : page_numbers) { 1547 for (uint32_t page_number : page_numbers) {
1495 FPDF_PAGE pdf_page = FPDF_LoadPage(doc_, page_number); 1548 FPDF_PAGE pdf_page = FPDF_LoadPage(doc_, page_number);
1496 double source_page_width = FPDF_GetPageWidth(pdf_page); 1549 double source_page_width = FPDF_GetPageWidth(pdf_page);
1497 double source_page_height = FPDF_GetPageHeight(pdf_page); 1550 double source_page_height = FPDF_GetPageHeight(pdf_page);
1498 source_page_sizes.push_back(std::make_pair(source_page_width, 1551 source_page_sizes.push_back(std::make_pair(source_page_width,
1499 source_page_height)); 1552 source_page_height));
1500 1553
1501 int width_in_pixels = ConvertUnit(source_page_width, 1554 int width_in_pixels = ConvertUnit(source_page_width,
1502 kPointsPerInch, 1555 kPointsPerInch,
1503 print_settings.dpi); 1556 print_settings.dpi);
1504 int height_in_pixels = ConvertUnit(source_page_height, 1557 int height_in_pixels = ConvertUnit(source_page_height,
1505 kPointsPerInch, 1558 kPointsPerInch,
1506 print_settings.dpi); 1559 print_settings.dpi);
1507 1560
1508 pp::Rect rect(width_in_pixels, height_in_pixels); 1561 pp::Rect rect(width_in_pixels, height_in_pixels);
1509 pages_to_print.push_back(PDFiumPage(this, page_number, rect, true)); 1562 pages_to_print.push_back(PDFiumPage(this, page_number, rect, true));
1510 FPDF_ClosePage(pdf_page); 1563 FPDF_ClosePage(pdf_page);
1511 } 1564 }
1512 1565
1513 #if defined(OS_LINUX) 1566 #if defined(OS_LINUX)
1514 g_last_instance_id = client_->GetPluginInstance()->pp_instance(); 1567 g_last_instance_id = client_->GetPluginInstance()->pp_instance();
1515 #endif 1568 #endif
1516 1569
1517 size_t i = 0; 1570 size_t i = 0;
1571 unsigned long total_doc_size = 0;
1518 for (; i < pages_to_print.size(); ++i) { 1572 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.
1519 double source_page_width = source_page_sizes[i].first; 1573 double source_page_width = source_page_sizes[i].first;
1520 double source_page_height = source_page_sizes[i].second; 1574 double source_page_height = source_page_sizes[i].second;
1521 1575
1522 // Use temp_doc to compress image by saving PDF to buffer. 1576 // Use temp_doc to compress image by saving PDF to buffer.
1523 FPDF_DOCUMENT temp_doc = CreateSinglePageRasterPdf(source_page_width, 1577 FPDF_DOCUMENT temp_doc = CreateSinglePageRasterPdf(source_page_width,
1524 source_page_height, 1578 source_page_height,
1525 print_settings, 1579 print_settings,
1526 &pages_to_print[i]); 1580 &pages_to_print[i]);
1527
1528 if (!temp_doc) 1581 if (!temp_doc)
1529 break; 1582 break;
1530 1583
1531 pp::Buffer_Dev buffer = GetFlattenedPrintData(temp_doc); 1584 pp::Buffer_Dev buffer = GetFlattenedPrintData(temp_doc);
1585 total_doc_size += buffer.size();
1532 FPDF_CloseDocument(temp_doc); 1586 FPDF_CloseDocument(temp_doc);
1533 1587
1534 PDFiumMemBufferFileRead file_read(buffer.data(), buffer.size()); 1588 PDFiumMemBufferFileRead file_read(buffer.data(), buffer.size());
1535 temp_doc = FPDF_LoadCustomDocument(&file_read, nullptr); 1589 temp_doc = FPDF_LoadCustomDocument(&file_read, nullptr);
1536
1537 FPDF_BOOL imported = FPDF_ImportPages(output_doc, temp_doc, "1", i); 1590 FPDF_BOOL imported = FPDF_ImportPages(output_doc, temp_doc, "1", i);
1538 FPDF_CloseDocument(temp_doc); 1591 FPDF_CloseDocument(temp_doc);
1539 if (!imported) 1592 if (!imported)
1540 break; 1593 break;
1541 } 1594 }
1542 1595
1543 pp::Buffer_Dev buffer; 1596 pp::Buffer_Dev buffer;
1544 if (i == pages_to_print.size()) { 1597 if (i == pages_to_print.size()) {
1545 FPDF_CopyViewerPreferences(output_doc, doc_); 1598 FPDF_CopyViewerPreferences(output_doc, doc_);
1546 FitContentsToPrintableAreaIfRequired(output_doc, print_settings); 1599 FitContentsToPrintableAreaIfRequired(output_doc, print_settings);
(...skipping 2606 matching lines...) Expand 10 before | Expand all | Expand 10 after
4153 4206
4154 bool PDFiumEngineExports::GetPDFPageSizeByIndex(void* pdf_handle, 4207 bool PDFiumEngineExports::GetPDFPageSizeByIndex(void* pdf_handle,
4155 int page_number, 4208 int page_number,
4156 double* width, 4209 double* width,
4157 double* height) { 4210 double* height) {
4158 FPDF_DOCUMENT doc = pdf_handle; 4211 FPDF_DOCUMENT doc = pdf_handle;
4159 return doc && FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; 4212 return doc && FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
4160 } 4213 }
4161 4214
4162 } // namespace chrome_pdf 4215 } // namespace chrome_pdf
OLDNEW
« no previous file with comments | « pdf/pdfium/DEPS ('k') | printing/print_job_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698