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

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: 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 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 540 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 new gin::IsolateHolder(gin::IsolateHolder::kSingleThread); 611 new gin::IsolateHolder(gin::IsolateHolder::kSingleThread);
611 g_isolate_holder->isolate()->Enter(); 612 g_isolate_holder->isolate()->Enter();
612 } 613 }
613 614
614 void TearDownV8() { 615 void TearDownV8() {
615 g_isolate_holder->isolate()->Exit(); 616 g_isolate_holder->isolate()->Exit();
616 delete g_isolate_holder; 617 delete g_isolate_holder;
617 g_isolate_holder = nullptr; 618 g_isolate_holder = nullptr;
618 } 619 }
619 620
621 int GetBlockForJpeg(void* param,
622 unsigned long pos,
623 unsigned char* buf,
624 unsigned long size) {
625 std::vector<uint8_t>* data_vector = static_cast<std::vector<uint8_t>*>(param);
626 if (pos + size < pos || pos + size > data_vector->size())
627 return 0;
628 memcpy(buf, data_vector->data() + pos, size);
629 return 1;
630 }
631
620 } // namespace 632 } // namespace
621 633
622 bool InitializeSDK() { 634 bool InitializeSDK() {
623 SetUpV8(); 635 SetUpV8();
624 636
625 FPDF_LIBRARY_CONFIG config; 637 FPDF_LIBRARY_CONFIG config;
626 config.version = 2; 638 config.version = 2;
627 config.m_pUserFontPaths = nullptr; 639 config.m_pUserFontPaths = nullptr;
628 config.m_pIsolate = v8::Isolate::GetCurrent(); 640 config.m_pIsolate = v8::Isolate::GetCurrent();
629 config.m_v8EmbedderSlot = gin::kEmbedderPDFium; 641 config.m_v8EmbedderSlot = gin::kEmbedderPDFium;
(...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after
1346 1358
1347 DCHECK(defer_page_unload_); 1359 DCHECK(defer_page_unload_);
1348 defer_page_unload_ = false; 1360 defer_page_unload_ = false;
1349 for (int page_index : deferred_page_unloads_) 1361 for (int page_index : deferred_page_unloads_)
1350 pages_[page_index]->Unload(); 1362 pages_[page_index]->Unload();
1351 deferred_page_unloads_.clear(); 1363 deferred_page_unloads_.clear();
1352 return rv; 1364 return rv;
1353 } 1365 }
1354 1366
1355 uint32_t PDFiumEngine::QuerySupportedPrintOutputFormats() { 1367 uint32_t PDFiumEngine::QuerySupportedPrintOutputFormats() {
1356 if (!HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) 1368 if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY))
1357 return 0; 1369 return PP_PRINTOUTPUTFORMAT_PDF | PP_PRINTOUTPUTFORMAT_RASTER;
1358 return PP_PRINTOUTPUTFORMAT_PDF; 1370 if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY))
1371 return PP_PRINTOUTPUTFORMAT_RASTER;
1372 return 0;
1359 } 1373 }
1360 1374
1361 void PDFiumEngine::PrintBegin() { 1375 void PDFiumEngine::PrintBegin() {
1362 FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_WP); 1376 FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_WP);
1363 } 1377 }
1364 1378
1365 pp::Resource PDFiumEngine::PrintPages( 1379 pp::Resource PDFiumEngine::PrintPages(
1366 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, 1380 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count,
1367 const PP_PrintSettings_Dev& print_settings) { 1381 const PP_PrintSettings_Dev& print_settings) {
1368 ScopedSubstFont scoped_subst_font(this); 1382 ScopedSubstFont scoped_subst_font(this);
1369 if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY)) 1383 if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY) &&
1384 (print_settings.format & PP_PRINTOUTPUTFORMAT_PDF)) {
1370 return PrintPagesAsPDF(page_ranges, page_range_count, print_settings); 1385 return PrintPagesAsPDF(page_ranges, page_range_count, print_settings);
1371 else if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) 1386 } else if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) {
1372 return PrintPagesAsRasterPDF(page_ranges, page_range_count, print_settings); 1387 return PrintPagesAsRasterPDF(page_ranges, page_range_count, print_settings);
1388 }
1389
1373 return pp::Resource(); 1390 return pp::Resource();
1374 } 1391 }
1375 1392
1376 FPDF_DOCUMENT PDFiumEngine::CreateSinglePageRasterPdf( 1393 FPDF_DOCUMENT PDFiumEngine::CreateSinglePageRasterPdf(
1377 double source_page_width, 1394 double source_page_width,
1378 double source_page_height, 1395 double source_page_height,
1379 const PP_PrintSettings_Dev& print_settings, 1396 const PP_PrintSettings_Dev& print_settings,
1380 PDFiumPage* page_to_print) { 1397 PDFiumPage* page_to_print) {
1381 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument(); 1398 FPDF_DOCUMENT temp_doc = FPDF_CreateNewDocument();
1382 if (!temp_doc) 1399 if (!temp_doc)
(...skipping 22 matching lines...) Expand all
1405 pp::Rect page_rect = page_to_print->rect(); 1422 pp::Rect page_rect = page_to_print->rect();
1406 FPDF_RenderPageBitmap(bitmap, 1423 FPDF_RenderPageBitmap(bitmap,
1407 page_to_print->GetPrintPage(), 1424 page_to_print->GetPrintPage(),
1408 page_rect.x(), 1425 page_rect.x(),
1409 page_rect.y(), 1426 page_rect.y(),
1410 page_rect.width(), 1427 page_rect.width(),
1411 page_rect.height(), 1428 page_rect.height(),
1412 print_settings.orientation, 1429 print_settings.orientation,
1413 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); 1430 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
1414 1431
1432 unsigned char* bitmap_data =
1433 static_cast<unsigned char*>(FPDFBitmap_GetBuffer(bitmap));
1415 double ratio_x = ConvertUnitDouble(bitmap_size.width(), 1434 double ratio_x = ConvertUnitDouble(bitmap_size.width(),
1416 print_settings.dpi, 1435 print_settings.dpi,
1417 kPointsPerInch); 1436 kPointsPerInch);
1418 double ratio_y = ConvertUnitDouble(bitmap_size.height(), 1437 double ratio_y = ConvertUnitDouble(bitmap_size.height(),
1419 print_settings.dpi, 1438 print_settings.dpi,
1420 kPointsPerInch); 1439 kPointsPerInch);
1421 1440
1422 // Add the bitmap to an image object and add the image object to the output 1441 // Add the bitmap to an image object and add the image object to the output
1423 // page. 1442 // page.
1424 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImgeObj(temp_doc); 1443 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImgeObj(temp_doc);
1425 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap); 1444
1445 std::vector<uint8_t> compressed_bitmap_data;
1446 int quality = 40;
Lei Zhang 2017/02/25 01:01:16 const + add comment to mention why we chose 40.
1447 if (!(print_settings.format & PP_PRINTOUTPUTFORMAT_PDF) &&
1448 (gfx::JPEGCodec::Encode(
1449 bitmap_data, gfx::JPEGCodec::FORMAT_BGRA, FPDFBitmap_GetWidth(bitmap),
1450 FPDFBitmap_GetHeight(bitmap), FPDFBitmap_GetStride(bitmap), quality,
1451 &compressed_bitmap_data))) {
1452 FPDF_FILEACCESS file_access = {};
1453 file_access.m_FileLen =
1454 static_cast<unsigned long>(compressed_bitmap_data.size());
1455 file_access.m_GetBlock = &GetBlockForJpeg;
1456 file_access.m_Param = &compressed_bitmap_data;
1457
1458 FPDFImageObj_LoadJpegFileInline(&temp_page, 1, temp_img, &file_access);
1459 } else {
1460 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap);
1461 }
1462
1426 FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0); 1463 FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0);
1427 FPDFPage_InsertObject(temp_page, temp_img); 1464 FPDFPage_InsertObject(temp_page, temp_img);
1428 FPDFPage_GenerateContent(temp_page); 1465 FPDFPage_GenerateContent(temp_page);
1429 FPDF_ClosePage(temp_page); 1466 FPDF_ClosePage(temp_page);
1430 1467
1431 page_to_print->ClosePrintPage(); 1468 page_to_print->ClosePrintPage();
1432 FPDFBitmap_Destroy(bitmap); 1469 FPDFBitmap_Destroy(bitmap);
1433 1470
1434 return temp_doc; 1471 return temp_doc;
1435 } 1472 }
(...skipping 2702 matching lines...) Expand 10 before | Expand all | Expand 10 after
4138 FPDF_DOCUMENT doc = 4175 FPDF_DOCUMENT doc =
4139 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); 4176 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
4140 if (!doc) 4177 if (!doc)
4141 return false; 4178 return false;
4142 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; 4179 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
4143 FPDF_CloseDocument(doc); 4180 FPDF_CloseDocument(doc);
4144 return success; 4181 return success;
4145 } 4182 }
4146 4183
4147 } // namespace chrome_pdf 4184 } // namespace chrome_pdf
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698