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

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: Fix pdfium engine and javascript 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 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
622 size_t size;
623 uint8_t* data;
624 };
625
626 int GetBlockForJpeg(void* param,
627 unsigned long pos,
628 unsigned char* buf,
629 unsigned long size) {
630 simple_buf* buf_p = static_cast<simple_buf*>(param);
631 if (pos + size < pos || pos + size > buf_p->size)
632 return 0;
633 memcpy(buf, buf_p->data + pos, size);
634 return 1;
635 }
636
620 } // namespace 637 } // namespace
621 638
622 bool InitializeSDK() { 639 bool InitializeSDK() {
623 SetUpV8(); 640 SetUpV8();
624 641
625 FPDF_LIBRARY_CONFIG config; 642 FPDF_LIBRARY_CONFIG config;
626 config.version = 2; 643 config.version = 2;
627 config.m_pUserFontPaths = nullptr; 644 config.m_pUserFontPaths = nullptr;
628 config.m_pIsolate = v8::Isolate::GetCurrent(); 645 config.m_pIsolate = v8::Isolate::GetCurrent();
629 config.m_v8EmbedderSlot = gin::kEmbedderPDFium; 646 config.m_v8EmbedderSlot = gin::kEmbedderPDFium;
(...skipping 716 matching lines...) Expand 10 before | Expand all | Expand 10 after
1346 1363
1347 DCHECK(defer_page_unload_); 1364 DCHECK(defer_page_unload_);
1348 defer_page_unload_ = false; 1365 defer_page_unload_ = false;
1349 for (int page_index : deferred_page_unloads_) 1366 for (int page_index : deferred_page_unloads_)
1350 pages_[page_index]->Unload(); 1367 pages_[page_index]->Unload();
1351 deferred_page_unloads_.clear(); 1368 deferred_page_unloads_.clear();
1352 return rv; 1369 return rv;
1353 } 1370 }
1354 1371
1355 uint32_t PDFiumEngine::QuerySupportedPrintOutputFormats() { 1372 uint32_t PDFiumEngine::QuerySupportedPrintOutputFormats() {
1356 if (!HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) 1373 if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY))
1357 return 0; 1374 return PP_PRINTOUTPUTFORMAT_PDF | PP_PRINTOUTPUTFORMAT_RASTER;
1358 return PP_PRINTOUTPUTFORMAT_PDF; 1375 if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY))
1376 return PP_PRINTOUTPUTFORMAT_RASTER;
1377 return 0;
1359 } 1378 }
1360 1379
1361 void PDFiumEngine::PrintBegin() { 1380 void PDFiumEngine::PrintBegin() {
1362 FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_WP); 1381 FORM_DoDocumentAAction(form_, FPDFDOC_AACTION_WP);
1363 } 1382 }
1364 1383
1365 pp::Resource PDFiumEngine::PrintPages( 1384 pp::Resource PDFiumEngine::PrintPages(
1366 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count, 1385 const PP_PrintPageNumberRange_Dev* page_ranges, uint32_t page_range_count,
1367 const PP_PrintSettings_Dev& print_settings) { 1386 const PP_PrintSettings_Dev& print_settings) {
1368 ScopedSubstFont scoped_subst_font(this); 1387 ScopedSubstFont scoped_subst_font(this);
1369 if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY)) 1388 if (HasPermission(PDFEngine::PERMISSION_PRINT_HIGH_QUALITY) &&
1389 (print_settings.format & PP_PRINTOUTPUTFORMAT_PDF)) {
1370 return PrintPagesAsPDF(page_ranges, page_range_count, print_settings); 1390 return PrintPagesAsPDF(page_ranges, page_range_count, print_settings);
1371 else if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) 1391 } else if (HasPermission(PDFEngine::PERMISSION_PRINT_LOW_QUALITY)) {
1372 return PrintPagesAsRasterPDF(page_ranges, page_range_count, print_settings); 1392 return PrintPagesAsRasterPDF(page_ranges, page_range_count, print_settings);
1393 }
1394
1373 return pp::Resource(); 1395 return pp::Resource();
1374 } 1396 }
1375 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)
(...skipping 22 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<uint8_t> compressed_bitmap_data;
1451 int quality = 40;
1452 if (!(print_settings.format & PP_PRINTOUTPUTFORMAT_PDF) &&
1453 (gfx::JPEGCodec::Encode(
1454 bitmap_data, gfx::JPEGCodec::FORMAT_BGRA, FPDFBitmap_GetWidth(bitmap),
1455 FPDFBitmap_GetHeight(bitmap), FPDFBitmap_GetStride(bitmap), quality,
1456 &compressed_bitmap_data))) {
1457 simple_buf buffer;
1458 buffer.size = compressed_bitmap_data.size();
1459 buffer.data = compressed_bitmap_data.data();
1460
1461 FPDF_FILEACCESS file_access;
1462 memset(&file_access, '\0', sizeof(file_access));
1463 file_access.m_FileLen =
1464 static_cast<unsigned long>(compressed_bitmap_data.size());
1465 file_access.m_GetBlock = &GetBlockForJpeg;
1466 file_access.m_Param = &buffer;
1467
1468 FPDFImageObj_LoadJpegFileInline(&temp_page, 1, temp_img, &file_access);
1469 } else {
1470 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap);
1471 }
1472
1426 FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0); 1473 FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0);
1427 FPDFPage_InsertObject(temp_page, temp_img); 1474 FPDFPage_InsertObject(temp_page, temp_img);
1428 FPDFPage_GenerateContent(temp_page); 1475 FPDFPage_GenerateContent(temp_page);
1429 FPDF_ClosePage(temp_page); 1476 FPDF_ClosePage(temp_page);
1430 1477
1431 page_to_print->ClosePrintPage(); 1478 page_to_print->ClosePrintPage();
1432 FPDFBitmap_Destroy(bitmap); 1479 FPDFBitmap_Destroy(bitmap);
1433 1480
1434 return temp_doc; 1481 return temp_doc;
1435 } 1482 }
(...skipping 2702 matching lines...) Expand 10 before | Expand all | Expand 10 after
4138 FPDF_DOCUMENT doc = 4185 FPDF_DOCUMENT doc =
4139 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); 4186 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr);
4140 if (!doc) 4187 if (!doc)
4141 return false; 4188 return false;
4142 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; 4189 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
4143 FPDF_CloseDocument(doc); 4190 FPDF_CloseDocument(doc);
4144 return success; 4191 return success;
4145 } 4192 }
4146 4193
4147 } // namespace chrome_pdf 4194 } // namespace chrome_pdf
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698