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

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: Change to match new Pdfium API 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
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 1276 matching lines...) Expand 10 before | Expand all | Expand 10 after
1346 1347
1347 DCHECK(defer_page_unload_); 1348 DCHECK(defer_page_unload_);
1348 defer_page_unload_ = false; 1349 defer_page_unload_ = false;
1349 for (int page_index : deferred_page_unloads_) 1350 for (int page_index : deferred_page_unloads_)
1350 pages_[page_index]->Unload(); 1351 pages_[page_index]->Unload();
1351 deferred_page_unloads_.clear(); 1352 deferred_page_unloads_.clear();
1352 return rv; 1353 return rv;
1353 } 1354 }
1354 1355
1355 uint32_t PDFiumEngine::QuerySupportedPrintOutputFormats() { 1356 uint32_t PDFiumEngine::QuerySupportedPrintOutputFormats() {
1356 if (!HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) 1357 if (!HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY))
Tom Sepez 2016/12/14 20:32:54 nit: Can a document have high_quality but not l
rbpotter 2016/12/15 00:50:59 Based on the logic in HasPermission, if a document
Tom Sepez 2016/12/15 21:09:23 Lots better, thanks.
1357 return 0; 1358 return 0;
1358 return PP_PRINTOUTPUTFORMAT_PDF; 1359 if (!HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY))
1360 return PP_PRINTOUTPUTFORMAT_RASTER;
1361 return PP_PRINTOUTPUTFORMAT_PDF | PP_PRINTOUTPUTFORMAT_RASTER;
1359 } 1362 }
1360 1363
1361 void PDFiumEngine::PrintBegin() { 1364 void PDFiumEngine::PrintBegin() {
1362 FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_WP); 1365 FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_WP);
1363 } 1366 }
1364 1367
1365 pp::Resource PDFiumEngine::PrintPages( 1368 pp::Resource PDFiumEngine::PrintPages(
1366 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, 1369 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count,
1367 const PP_PrintSettings_Dev& print_settings) { 1370 const PP_PrintSettings_Dev& print_settings) {
1368 ScopedSubstFont scoped_subst_font(this); 1371 ScopedSubstFont scoped_subst_font(this);
1369 if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY)) 1372 if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY) &&
1373 (print_settings.format & PP_PRINTOUTPUTFORMAT_PDF)) {
1370 return PrintPagesAsPDF(page_ranges, page_range_count, print_settings); 1374 return PrintPagesAsPDF(page_ranges, page_range_count, print_settings);
1371 else if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) 1375 } else if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) {
1372 return PrintPagesAsRasterPDF(page_ranges, page_range_count, print_settings); 1376 return PrintPagesAsRasterPDF(page_ranges, page_range_count, print_settings);
1377 }
1378
1373 return pp::Resource(); 1379 return pp::Resource();
1374 } 1380 }
1375 1381
1382 struct simple_buf {
1383 uint32_t size;
1384 uint8_t *data;
Tom Sepez 2016/12/14 20:32:54 nit: uint8_t* data; per style guide (several place
rbpotter 2016/12/15 00:50:58 Done.
1385 };
1386
1387 int GetBlockForJpeg(void *param,
Tom Sepez 2016/12/14 20:32:54 the return type should probably be unsigned long t
rbpotter 2016/12/15 00:50:59 Unfortunately the signature is determined by the F
1388 unsigned long pos,
1389 unsigned char* pBuf,
Tom Sepez 2016/12/14 20:32:55 nit: uint8_t consistently throughout, hungarian no
rbpotter 2016/12/15 00:50:59 The signature for this function is defined by the
1390 unsigned long size) {
1391 simple_buf * buf_p = static_cast<simple_buf *>(param);
1392 if (pos + size < pos || pos + size > buf_p->size)
dsinclair 2016/12/14 19:54:38 The pos + size < pos seems like it would always be
Tom Sepez 2016/12/14 20:32:54 This is checking for overflow, and is allowed in t
1393 return 0;
1394 memcpy(pBuf, buf_p->data, size);
dsinclair 2016/12/14 19:54:38 Should this be copying from buf_p->data + pos? Won
dsinclair 2016/12/14 19:54:38 Is it safe to assume pBuf is always big enough her
rbpotter 2016/12/15 00:50:58 Since the buffer is provided by the caller I don't
rbpotter 2016/12/15 00:50:58 Done.
Tom Sepez 2016/12/15 21:09:23 I think this is still an issue.
rbpotter 2016/12/16 00:06:31 This is now copying size bytes from buf_p->data.si
1395 return size;
1396 }
dsinclair 2016/12/14 19:54:38 Move up into the anonymous namespace.
rbpotter 2016/12/15 00:50:59 Done.
1397
1376 FPDF_DOCUMENT PDFiumEngine::CreateSinglePageRasterPdf( 1398 FPDF_DOCUMENT PDFiumEngine::CreateSinglePageRasterPdf(
1377 double source_page_width, 1399 double source_page_width,
1378 double source_page_height, 1400 double source_page_height,
1379 const PP_PrintSettings_Dev& print_settings, 1401 const PP_PrintSettings_Dev& print_settings,
1380 PDFiumPage* page_to_print) { 1402 PDFiumPage* page_to_print) {
1381 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument(); 1403 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
1382 if (!temp_doc) 1404 if (!temp_doc)
1383 return temp_doc; 1405 return temp_doc;
1384 1406
1385 const pp::Size& bitmap_size(page_to_print->rect().size()); 1407 const pp::Size& bitmap_size(page_to_print->rect().size());
(...skipping 19 matching lines...) Expand all
1405 pp::Rect page_rect = page_to_print->rect(); 1427 pp::Rect page_rect = page_to_print->rect();
1406 FPDF_RenderPageBitmap(bitmap, 1428 FPDF_RenderPageBitmap(bitmap,
1407 page_to_print->GetPrintPage(), 1429 page_to_print->GetPrintPage(),
1408 page_rect.x(), 1430 page_rect.x(),
1409 page_rect.y(), 1431 page_rect.y(),
1410 page_rect.width(), 1432 page_rect.width(),
1411 page_rect.height(), 1433 page_rect.height(),
1412 print_settings.orientation, 1434 print_settings.orientation,
1413 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); 1435 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
1414 1436
1437 unsigned char * bitmap_data =
1438 static_cast<unsigned char *>(FPDFBitmap_GetBuffer(bitmap));
1415 double ratio_x = ConvertUnitDouble(bitmap_size.width(), 1439 double ratio_x = ConvertUnitDouble(bitmap_size.width(),
1416 print_settings.dpi, 1440 print_settings.dpi,
1417 kPointsPerInch); 1441 kPointsPerInch);
1418 double ratio_y = ConvertUnitDouble(bitmap_size.height(), 1442 double ratio_y = ConvertUnitDouble(bitmap_size.height(),
1419 print_settings.dpi, 1443 print_settings.dpi,
1420 kPointsPerInch); 1444 kPointsPerInch);
1421 1445
1422 // Add the bitmap to an image object and add the image object to the output 1446 // Add the bitmap to an image object and add the image object to the output
1423 // page. 1447 // page.
1424 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImgeObj(temp_doc); 1448 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImgeObj(temp_doc);
1425 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap); 1449
1450 std::vector <unsigned char> compressed_bitmap_data;
Tom Sepez 2016/12/14 20:32:54 nit: std::vector<uint8_t> Did you "git cl format"
rbpotter 2016/12/15 00:50:58 Done.
1451 int quality = 40;
1452 bool encode_success = gfx::JPEGCodec::Encode(bitmap_data,
dsinclair 2016/12/14 19:54:38 Do we always want to do the JPEG encode, or only w
Tom Sepez 2016/12/14 20:32:54 nit: local not needed
rbpotter 2016/12/15 00:50:58 Not sure. The JPEG encode saves space for big/imag
1453 gfx::JPEGCodec::FORMAT_BGRA,
1454 FPDFBitmap_GetWidth(bitmap),
1455 FPDFBitmap_GetHeight(bitmap),
1456 FPDFBitmap_GetStride(bitmap),
1457 quality,
1458 &compressed_bitmap_data);
1459
1460 if (encode_success) {
1461 simple_buf buffer;
1462 buffer.size = compressed_bitmap_data.size();
Tom Sepez 2016/12/14 20:32:54 beware, this truncates a size_t to an uint32_t. Y
rbpotter 2016/12/15 00:50:58 Done.
1463 buffer.data = new uint8_t[buffer.size];
Tom Sepez 2016/12/14 20:36:28 actually, we can set buffer.data to compressed_bit
rbpotter 2016/12/15 00:50:58 Done.
1464 memcpy(buffer.data, &(compressed_bitmap_data[0]), buffer.size);
1465
1466 FPDF_FILEACCESS file_access;
1467 memset(&file_access, '\0', sizeof(file_access));
1468 file_access.m_FileLen =
1469 static_cast<unsigned long>(compressed_bitmap_data.size());
1470 file_access.m_GetBlock = &GetBlockForJpeg;
1471 file_access.m_Param = &buffer;
1472
1473 FPDFImageObj_LoadJpegFileInline(&temp_page, 1, temp_img, &file_access);
1474 delete buffer.data;
Tom Sepez 2016/12/14 20:32:54 delete[] buffer.data?
rbpotter 2016/12/15 00:50:59 N/A
1475 } else {
1476 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap);
1477 }
1478
1426 FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0); 1479 FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0);
1427 FPDFPage_InsertObject(temp_page, temp_img); 1480 FPDFPage_InsertObject(temp_page, temp_img);
1428 FPDFPage_GenerateContent(temp_page); 1481 FPDFPage_GenerateContent(temp_page);
1429 FPDF_ClosePage(temp_page); 1482 FPDF_ClosePage(temp_page);
1430 1483
1431 page_to_print->ClosePrintPage(); 1484 page_to_print->ClosePrintPage();
1432 FPDFBitmap_Destroy(bitmap); 1485 FPDFBitmap_Destroy(bitmap);
1433 1486
1434 return temp_doc; 1487 return temp_doc;
1435 } 1488 }
(...skipping 2702 matching lines...) Expand 10 before | Expand all | Expand 10 after
4138 FPDF_DOCUMENT doc = 4191 FPDF_DOCUMENT doc =
4139 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); 4192 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
4140 if (!doc) 4193 if (!doc)
4141 return false; 4194 return false;
4142 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; 4195 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
4143 FPDF_CloseDocument(doc); 4196 FPDF_CloseDocument(doc);
4144 return success; 4197 return success;
4145 } 4198 }
4146 4199
4147 } // namespace chrome_pdf 4200 } // namespace chrome_pdf
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698