OLD | NEW |
---|---|
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 Loading... | |
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 Loading... | |
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 Loading... | |
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 Loading... | |
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; | |
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; | |
Vitaly Buka (NO REVIEWS)
2016/12/16 00:54:38
no need to do memset, just:
FPDF_FILEACCESS file_a
rbpotter
2016/12/16 01:14:33
Done.
| |
1453 memset(&file_access, '\0', sizeof(file_access)); | |
1454 file_access.m_FileLen = | |
1455 static_cast<unsigned long>(compressed_bitmap_data.size()); | |
1456 file_access.m_GetBlock = &GetBlockForJpeg; | |
1457 file_access.m_Param = &compressed_bitmap_data; | |
1458 | |
1459 FPDFImageObj_LoadJpegFileInline(&temp_page, 1, temp_img, &file_access); | |
1460 } else { | |
1461 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap); | |
1462 } | |
1463 | |
1426 FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0); | 1464 FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0); |
1427 FPDFPage_InsertObject(temp_page, temp_img); | 1465 FPDFPage_InsertObject(temp_page, temp_img); |
1428 FPDFPage_GenerateContent(temp_page); | 1466 FPDFPage_GenerateContent(temp_page); |
1429 FPDF_ClosePage(temp_page); | 1467 FPDF_ClosePage(temp_page); |
1430 | 1468 |
1431 page_to_print->ClosePrintPage(); | 1469 page_to_print->ClosePrintPage(); |
1432 FPDFBitmap_Destroy(bitmap); | 1470 FPDFBitmap_Destroy(bitmap); |
1433 | 1471 |
1434 return temp_doc; | 1472 return temp_doc; |
1435 } | 1473 } |
(...skipping 2702 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4138 FPDF_DOCUMENT doc = | 4176 FPDF_DOCUMENT doc = |
4139 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); | 4177 FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, nullptr); |
4140 if (!doc) | 4178 if (!doc) |
4141 return false; | 4179 return false; |
4142 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; | 4180 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; |
4143 FPDF_CloseDocument(doc); | 4181 FPDF_CloseDocument(doc); |
4144 return success; | 4182 return success; |
4145 } | 4183 } |
4146 | 4184 |
4147 } // namespace chrome_pdf | 4185 } // namespace chrome_pdf |
OLD | NEW |