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

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

Issue 980053002: Remove some duplicate code / constants in pdf/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months 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/DEPS ('k') | no next file » | 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 8
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 15 matching lines...) Expand all
26 #include "ppapi/c/private/ppb_pdf.h" 26 #include "ppapi/c/private/ppb_pdf.h"
27 #include "ppapi/cpp/dev/memory_dev.h" 27 #include "ppapi/cpp/dev/memory_dev.h"
28 #include "ppapi/cpp/input_event.h" 28 #include "ppapi/cpp/input_event.h"
29 #include "ppapi/cpp/instance.h" 29 #include "ppapi/cpp/instance.h"
30 #include "ppapi/cpp/module.h" 30 #include "ppapi/cpp/module.h"
31 #include "ppapi/cpp/private/pdf.h" 31 #include "ppapi/cpp/private/pdf.h"
32 #include "ppapi/cpp/trusted/browser_font_trusted.h" 32 #include "ppapi/cpp/trusted/browser_font_trusted.h"
33 #include "ppapi/cpp/url_response_info.h" 33 #include "ppapi/cpp/url_response_info.h"
34 #include "ppapi/cpp/var.h" 34 #include "ppapi/cpp/var.h"
35 #include "ppapi/cpp/var_dictionary.h" 35 #include "ppapi/cpp/var_dictionary.h"
36 #include "printing/units.h"
36 #include "third_party/pdfium/fpdfsdk/include/fpdf_ext.h" 37 #include "third_party/pdfium/fpdfsdk/include/fpdf_ext.h"
37 #include "third_party/pdfium/fpdfsdk/include/fpdf_flatten.h" 38 #include "third_party/pdfium/fpdfsdk/include/fpdf_flatten.h"
38 #include "third_party/pdfium/fpdfsdk/include/fpdf_searchex.h" 39 #include "third_party/pdfium/fpdfsdk/include/fpdf_searchex.h"
39 #include "third_party/pdfium/fpdfsdk/include/fpdf_sysfontinfo.h" 40 #include "third_party/pdfium/fpdfsdk/include/fpdf_sysfontinfo.h"
40 #include "third_party/pdfium/fpdfsdk/include/fpdf_transformpage.h" 41 #include "third_party/pdfium/fpdfsdk/include/fpdf_transformpage.h"
41 #include "third_party/pdfium/fpdfsdk/include/fpdfedit.h" 42 #include "third_party/pdfium/fpdfsdk/include/fpdfedit.h"
42 #include "third_party/pdfium/fpdfsdk/include/fpdfppo.h" 43 #include "third_party/pdfium/fpdfsdk/include/fpdfppo.h"
43 #include "third_party/pdfium/fpdfsdk/include/fpdfsave.h" 44 #include "third_party/pdfium/fpdfsdk/include/fpdfsave.h"
44 #include "third_party/pdfium/fpdfsdk/include/pdfwindow/PDFWindow.h" 45 #include "third_party/pdfium/fpdfsdk/include/pdfwindow/PDFWindow.h"
45 #include "third_party/pdfium/fpdfsdk/include/pdfwindow/PWL_FontMap.h" 46 #include "third_party/pdfium/fpdfsdk/include/pdfwindow/PWL_FontMap.h"
46 #include "ui/events/keycodes/keyboard_codes.h" 47 #include "ui/events/keycodes/keyboard_codes.h"
47 48
49 using printing::ConvertUnit;
50 using printing::ConvertUnitDouble;
51 using printing::kPointsPerInch;
52 using printing::kPixelsPerInch;
53
48 namespace chrome_pdf { 54 namespace chrome_pdf {
49 55
50 namespace { 56 namespace {
51 57
52 #define kPageShadowTop 3 58 #define kPageShadowTop 3
53 #define kPageShadowBottom 7 59 #define kPageShadowBottom 7
54 #define kPageShadowLeft 5 60 #define kPageShadowLeft 5
55 #define kPageShadowRight 5 61 #define kPageShadowRight 5
56 62
57 #define kPageSeparatorThickness 4 63 #define kPageSeparatorThickness 4
(...skipping 25 matching lines...) Expand all
83 // than the above to keep things smooth if the user is scrolling quickly. We 89 // than the above to keep things smooth if the user is scrolling quickly. We
84 // try painting a little because with accelerated compositing, we get flushes 90 // try painting a little because with accelerated compositing, we get flushes
85 // only every 16 ms. If we were to wait until the next flush to paint the rest 91 // only every 16 ms. If we were to wait until the next flush to paint the rest
86 // of the pdf, we would never get to draw the pdf and would only draw the 92 // of the pdf, we would never get to draw the pdf and would only draw the
87 // scrollbars. This value is picked to give enough time for gpu related code to 93 // scrollbars. This value is picked to give enough time for gpu related code to
88 // do its thing and still fit within the timelimit for 60Hz. For the 94 // do its thing and still fit within the timelimit for 60Hz. For the
89 // non-composited case, this doesn't make things worse since we're still 95 // non-composited case, this doesn't make things worse since we're still
90 // painting the scrollbars > 60 Hz. 96 // painting the scrollbars > 60 Hz.
91 #define kMaxInitialProgressivePaintTimeMs 10 97 #define kMaxInitialProgressivePaintTimeMs 10
92 98
93 // Copied from printing/units.cc because we don't want to depend on printing
94 // since it brings in libpng which causes duplicate symbols with PDFium.
95 const int kPointsPerInch = 72;
96 const int kPixelsPerInch = 96;
97
98 struct ClipBox { 99 struct ClipBox {
99 float left; 100 float left;
100 float right; 101 float right;
101 float top; 102 float top;
102 float bottom; 103 float bottom;
103 }; 104 };
104 105
105 int ConvertUnit(int value, int old_unit, int new_unit) {
106 // With integer arithmetic, to divide a value with correct rounding, you need
107 // to add half of the divisor value to the dividend value. You need to do the
108 // reverse with negative number.
109 if (value >= 0) {
110 return ((value * new_unit) + (old_unit / 2)) / old_unit;
111 } else {
112 return ((value * new_unit) - (old_unit / 2)) / old_unit;
113 }
114 }
115
116 std::vector<uint32_t> GetPageNumbersFromPrintPageNumberRange( 106 std::vector<uint32_t> GetPageNumbersFromPrintPageNumberRange(
117 const PP_PrintPageNumberRange_Dev* page_ranges, 107 const PP_PrintPageNumberRange_Dev* page_ranges,
118 uint32_t page_range_count) { 108 uint32_t page_range_count) {
119 std::vector<uint32_t> page_numbers; 109 std::vector<uint32_t> page_numbers;
120 for (uint32_t index = 0; index < page_range_count; ++index) { 110 for (uint32_t index = 0; index < page_range_count; ++index) {
121 for (uint32_t page_number = page_ranges[index].first_page_number; 111 for (uint32_t page_number = page_ranges[index].first_page_number;
122 page_number <= page_ranges[index].last_page_number; ++page_number) { 112 page_number <= page_ranges[index].last_page_number; ++page_number) {
123 page_numbers.push_back(page_number); 113 page_numbers.push_back(page_number);
124 } 114 }
125 } 115 }
(...skipping 1265 matching lines...) Expand 10 before | Expand all | Expand 10 after
1391 pp::Rect page_rect = page_to_print->rect(); 1381 pp::Rect page_rect = page_to_print->rect();
1392 FPDF_RenderPageBitmap(bitmap, 1382 FPDF_RenderPageBitmap(bitmap,
1393 page_to_print->GetPrintPage(), 1383 page_to_print->GetPrintPage(),
1394 page_rect.x(), 1384 page_rect.x(),
1395 page_rect.y(), 1385 page_rect.y(),
1396 page_rect.width(), 1386 page_rect.width(),
1397 page_rect.height(), 1387 page_rect.height(),
1398 print_settings.orientation, 1388 print_settings.orientation,
1399 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); 1389 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
1400 1390
1401 double ratio_x = (static_cast<double>(bitmap_size.width()) * kPointsPerInch) / 1391 double ratio_x = ConvertUnitDouble(bitmap_size.width(),
1402 print_settings.dpi; 1392 print_settings.dpi,
1403 double ratio_y = 1393 kPointsPerInch);
1404 (static_cast<double>(bitmap_size.height()) * kPointsPerInch) / 1394 double ratio_y = ConvertUnitDouble(bitmap_size.height(),
1405 print_settings.dpi; 1395 print_settings.dpi,
1396 kPointsPerInch);
1406 1397
1407 // Add the bitmap to an image object and add the image object to the output 1398 // Add the bitmap to an image object and add the image object to the output
1408 // page. 1399 // page.
1409 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImgeObj(temp_doc); 1400 FPDF_PAGEOBJECT temp_img = FPDFPageObj_NewImgeObj(temp_doc);
1410 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap); 1401 FPDFImageObj_SetBitmap(&temp_page, 1, temp_img, bitmap);
1411 FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0); 1402 FPDFImageObj_SetMatrix(temp_img, ratio_x, 0, 0, ratio_y, 0, 0);
1412 FPDFPage_InsertObject(temp_page, temp_img); 1403 FPDFPage_InsertObject(temp_page, temp_img);
1413 FPDFPage_GenerateContent(temp_page); 1404 FPDFPage_GenerateContent(temp_page);
1414 FPDF_ClosePage(temp_page); 1405 FPDF_ClosePage(temp_page);
1415 1406
(...skipping 27 matching lines...) Expand all
1443 GetPageNumbersFromPrintPageNumberRange(page_ranges, page_range_count); 1434 GetPageNumbersFromPrintPageNumberRange(page_ranges, page_range_count);
1444 for (size_t i = 0; i < page_numbers.size(); ++i) { 1435 for (size_t i = 0; i < page_numbers.size(); ++i) {
1445 uint32_t page_number = page_numbers[i]; 1436 uint32_t page_number = page_numbers[i];
1446 FPDF_PAGE pdf_page = FPDF_LoadPage(doc_, page_number); 1437 FPDF_PAGE pdf_page = FPDF_LoadPage(doc_, page_number);
1447 double source_page_width = FPDF_GetPageWidth(pdf_page); 1438 double source_page_width = FPDF_GetPageWidth(pdf_page);
1448 double source_page_height = FPDF_GetPageHeight(pdf_page); 1439 double source_page_height = FPDF_GetPageHeight(pdf_page);
1449 source_page_sizes.push_back(std::make_pair(source_page_width, 1440 source_page_sizes.push_back(std::make_pair(source_page_width,
1450 source_page_height)); 1441 source_page_height));
1451 1442
1452 int width_in_pixels = ConvertUnit(source_page_width, 1443 int width_in_pixels = ConvertUnit(source_page_width,
1453 static_cast<int>(kPointsPerInch), 1444 kPointsPerInch,
1454 print_settings.dpi); 1445 print_settings.dpi);
1455 int height_in_pixels = ConvertUnit(source_page_height, 1446 int height_in_pixels = ConvertUnit(source_page_height,
1456 static_cast<int>(kPointsPerInch), 1447 kPointsPerInch,
1457 print_settings.dpi); 1448 print_settings.dpi);
1458 1449
1459 pp::Rect rect(width_in_pixels, height_in_pixels); 1450 pp::Rect rect(width_in_pixels, height_in_pixels);
1460 pages_to_print.push_back(PDFiumPage(this, page_number, rect, true)); 1451 pages_to_print.push_back(PDFiumPage(this, page_number, rect, true));
1461 FPDF_ClosePage(pdf_page); 1452 FPDF_ClosePage(pdf_page);
1462 } 1453 }
1463 1454
1464 #if defined(OS_LINUX) 1455 #if defined(OS_LINUX)
1465 g_last_instance_id = client_->GetPluginInstance()->pp_instance(); 1456 g_last_instance_id = client_->GetPluginInstance()->pp_instance();
1466 #endif 1457 #endif
(...skipping 1032 matching lines...) Expand 10 before | Expand all | Expand 10 after
2499 page_rects.push_back(rect); 2490 page_rects.push_back(rect);
2500 2491
2501 document_size_.Enlarge(0, page_size.height()); 2492 document_size_.Enlarge(0, page_size.height());
2502 } 2493 }
2503 2494
2504 // Create blank pages. 2495 // Create blank pages.
2505 for (int i = 1; i < num_pages; ++i) { 2496 for (int i = 1; i < num_pages; ++i) {
2506 pp::Rect page_rect(page_rects[i]); 2497 pp::Rect page_rect(page_rects[i]);
2507 page_rect.Inset(kPageShadowLeft, kPageShadowTop, 2498 page_rect.Inset(kPageShadowLeft, kPageShadowTop,
2508 kPageShadowRight, kPageShadowBottom); 2499 kPageShadowRight, kPageShadowBottom);
2509 double width_in_points = 2500 double width_in_points = ConvertUnitDouble(page_rect.width(),
2510 page_rect.width() * kPointsPerInch / kPixelsPerInch; 2501 kPixelsPerInch,
2511 double height_in_points = 2502 kPointsPerInch);
2512 page_rect.height() * kPointsPerInch / kPixelsPerInch; 2503 double height_in_points = ConvertUnitDouble(page_rect.height(),
2504 kPixelsPerInch,
2505 kPointsPerInch);
2513 FPDFPage_New(doc_, i, width_in_points, height_in_points); 2506 FPDFPage_New(doc_, i, width_in_points, height_in_points);
2514 pages_.push_back(new PDFiumPage(this, i, page_rect, true)); 2507 pages_.push_back(new PDFiumPage(this, i, page_rect, true));
2515 } 2508 }
2516 2509
2517 CalculateVisiblePages(); 2510 CalculateVisiblePages();
2518 if (document_size_ != old_document_size) 2511 if (document_size_ != old_document_size)
2519 client_->DocumentSizeUpdated(document_size_); 2512 client_->DocumentSizeUpdated(document_size_);
2520 } 2513 }
2521 2514
2522 void PDFiumEngine::LoadDocument() { 2515 void PDFiumEngine::LoadDocument() {
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
2786 2779
2787 pp::Size PDFiumEngine::GetPageSize(int index) { 2780 pp::Size PDFiumEngine::GetPageSize(int index) {
2788 pp::Size size; 2781 pp::Size size;
2789 double width_in_points = 0; 2782 double width_in_points = 0;
2790 double height_in_points = 0; 2783 double height_in_points = 0;
2791 int rv = FPDF_GetPageSizeByIndex( 2784 int rv = FPDF_GetPageSizeByIndex(
2792 doc_, index, &width_in_points, &height_in_points); 2785 doc_, index, &width_in_points, &height_in_points);
2793 2786
2794 if (rv) { 2787 if (rv) {
2795 int width_in_pixels = static_cast<int>( 2788 int width_in_pixels = static_cast<int>(
2796 width_in_points * kPixelsPerInch / kPointsPerInch); 2789 ConvertUnitDouble(width_in_points, kPointsPerInch, kPixelsPerInch));
2797 int height_in_pixels = static_cast<int>( 2790 int height_in_pixels = static_cast<int>(
2798 height_in_points * kPixelsPerInch / kPointsPerInch); 2791 ConvertUnitDouble(height_in_points, kPointsPerInch, kPixelsPerInch));
2799 if (current_rotation_ % 2 == 1) 2792 if (current_rotation_ % 2 == 1)
2800 std::swap(width_in_pixels, height_in_pixels); 2793 std::swap(width_in_pixels, height_in_pixels);
2801 size = pp::Size(width_in_pixels, height_in_pixels); 2794 size = pp::Size(width_in_pixels, height_in_pixels);
2802 } 2795 }
2803 return size; 2796 return size;
2804 } 2797 }
2805 2798
2806 int PDFiumEngine::StartPaint(int page_index, const pp::Rect& dirty) { 2799 int PDFiumEngine::StartPaint(int page_index, const pp::Rect& dirty) {
2807 // For the first time we hit paint, do nothing and just record the paint for 2800 // For the first time we hit paint, do nothing and just record the paint for
2808 // the next callback. This keeps the UI responsive in case the user is doing 2801 // the next callback. This keeps the UI responsive in case the user is doing
(...skipping 964 matching lines...) Expand 10 before | Expand all | Expand 10 after
3773 3766
3774 PDFEngineExports* PDFEngineExports::Create() { 3767 PDFEngineExports* PDFEngineExports::Create() {
3775 return new PDFiumEngineExports; 3768 return new PDFiumEngineExports;
3776 } 3769 }
3777 3770
3778 namespace { 3771 namespace {
3779 3772
3780 int CalculatePosition(FPDF_PAGE page, 3773 int CalculatePosition(FPDF_PAGE page,
3781 const PDFiumEngineExports::RenderingSettings& settings, 3774 const PDFiumEngineExports::RenderingSettings& settings,
3782 pp::Rect* dest) { 3775 pp::Rect* dest) {
3783 int page_width = static_cast<int>( 3776 int page_width = static_cast<int>(ConvertUnitDouble(FPDF_GetPageWidth(page),
3784 FPDF_GetPageWidth(page) * settings.dpi_x / kPointsPerInch); 3777 kPointsPerInch,
3785 int page_height = static_cast<int>( 3778 settings.dpi_x));
3786 FPDF_GetPageHeight(page) * settings.dpi_y / kPointsPerInch); 3779 int page_height = static_cast<int>(ConvertUnitDouble(FPDF_GetPageHeight(page),
3780 kPointsPerInch,
3781 settings.dpi_y));
3787 3782
3788 // Start by assuming that we will draw exactly to the bounds rect 3783 // Start by assuming that we will draw exactly to the bounds rect
3789 // specified. 3784 // specified.
3790 *dest = settings.bounds; 3785 *dest = settings.bounds;
3791 3786
3792 int rotate = 0; // normal orientation. 3787 int rotate = 0; // normal orientation.
3793 3788
3794 // Auto-rotate landscape pages to print correctly. 3789 // Auto-rotate landscape pages to print correctly.
3795 if (settings.autorotate && 3790 if (settings.autorotate &&
3796 (dest->width() > dest->height()) != (page_width > page_height)) { 3791 (dest->width() > dest->height()) != (page_width > page_height)) {
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
3999 double* height) { 3994 double* height) {
4000 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); 3995 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL);
4001 if (!doc) 3996 if (!doc)
4002 return false; 3997 return false;
4003 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; 3998 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0;
4004 FPDF_CloseDocument(doc); 3999 FPDF_CloseDocument(doc);
4005 return success; 4000 return success;
4006 } 4001 }
4007 4002
4008 } // namespace chrome_pdf 4003 } // namespace chrome_pdf
OLDNEW
« no previous file with comments | « pdf/DEPS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698