Chromium Code Reviews| 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 | 8 |
| 9 #include "base/i18n/icu_encoding_detection.h" | 9 #include "base/i18n/icu_encoding_detection.h" |
| 10 #include "base/i18n/icu_string_conversions.h" | 10 #include "base/i18n/icu_string_conversions.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 // than the above to keep things smooth if the user is scrolling quickly. We | 91 // than the above to keep things smooth if the user is scrolling quickly. We |
| 92 // try painting a little because with accelerated compositing, we get flushes | 92 // try painting a little because with accelerated compositing, we get flushes |
| 93 // only every 16 ms. If we were to wait until the next flush to paint the rest | 93 // only every 16 ms. If we were to wait until the next flush to paint the rest |
| 94 // of the pdf, we would never get to draw the pdf and would only draw the | 94 // of the pdf, we would never get to draw the pdf and would only draw the |
| 95 // scrollbars. This value is picked to give enough time for gpu related code to | 95 // scrollbars. This value is picked to give enough time for gpu related code to |
| 96 // do its thing and still fit within the timelimit for 60Hz. For the | 96 // do its thing and still fit within the timelimit for 60Hz. For the |
| 97 // non-composited case, this doesn't make things worse since we're still | 97 // non-composited case, this doesn't make things worse since we're still |
| 98 // painting the scrollbars > 60 Hz. | 98 // painting the scrollbars > 60 Hz. |
| 99 #define kMaxInitialProgressivePaintTimeMs 10 | 99 #define kMaxInitialProgressivePaintTimeMs 10 |
| 100 | 100 |
| 101 struct ClipBox { | |
| 102 float left; | |
| 103 float right; | |
| 104 float top; | |
| 105 float bottom; | |
| 106 }; | |
| 107 | |
| 108 std::vector<uint32_t> GetPageNumbersFromPrintPageNumberRange( | 101 std::vector<uint32_t> GetPageNumbersFromPrintPageNumberRange( |
| 109 const PP_PrintPageNumberRange_Dev* page_ranges, | 102 const PP_PrintPageNumberRange_Dev* page_ranges, |
| 110 uint32_t page_range_count) { | 103 uint32_t page_range_count) { |
| 111 std::vector<uint32_t> page_numbers; | 104 std::vector<uint32_t> page_numbers; |
| 112 for (uint32_t index = 0; index < page_range_count; ++index) { | 105 for (uint32_t index = 0; index < page_range_count; ++index) { |
| 113 for (uint32_t page_number = page_ranges[index].first_page_number; | 106 for (uint32_t page_number = page_ranges[index].first_page_number; |
| 114 page_number <= page_ranges[index].last_page_number; ++page_number) { | 107 page_number <= page_ranges[index].last_page_number; ++page_number) { |
| 115 page_numbers.push_back(page_number); | 108 page_numbers.push_back(page_number); |
| 116 } | 109 } |
| 117 } | 110 } |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 349 | 342 |
| 350 double actual_source_page_width = rotated ? src_height : src_width; | 343 double actual_source_page_width = rotated ? src_height : src_width; |
| 351 double actual_source_page_height = rotated ? src_width : src_height; | 344 double actual_source_page_height = rotated ? src_width : src_height; |
| 352 double ratio_x = static_cast<double>(content_rect.width()) / | 345 double ratio_x = static_cast<double>(content_rect.width()) / |
| 353 actual_source_page_width; | 346 actual_source_page_width; |
| 354 double ratio_y = static_cast<double>(content_rect.height()) / | 347 double ratio_y = static_cast<double>(content_rect.height()) / |
| 355 actual_source_page_height; | 348 actual_source_page_height; |
| 356 return std::min(ratio_x, ratio_y); | 349 return std::min(ratio_x, ratio_y); |
| 357 } | 350 } |
| 358 | 351 |
| 352 // A rect struct for use with FPDF bounding box functions. | |
| 353 // Remember with PDFs, origin is bottom-left. | |
|
raymes
2015/10/01 04:19:48
nit: fill 80chars
Lei Zhang
2015/10/01 04:50:03
I did this on purpose since it takes 2 lines anywa
| |
| 354 struct ClipBox { | |
| 355 float left; | |
| 356 float right; | |
| 357 float top; | |
| 358 float bottom; | |
| 359 }; | |
| 360 | |
| 361 // Make the default size to be letter size (8.5" X 11"). We are just following | |
| 362 // the PDFium way of handling these corner cases. PDFium always consider | |
| 363 // US-Letter as the default page size. | |
| 364 void SetDefaultClipBox(bool rotated, ClipBox* clip_box) { | |
| 365 const int kDpi = 72; | |
| 366 const float kPaperWidth = 8.5 * kDpi; | |
| 367 const float kPaperHeight = 11 * kDpi; | |
| 368 clip_box->left = 0; | |
| 369 clip_box->bottom = 0; | |
| 370 clip_box->right = rotated ? kPaperHeight : kPaperWidth; | |
| 371 clip_box->top = rotated ? kPaperWidth : kPaperHeight; | |
| 372 } | |
| 373 | |
| 359 // Compute source clip box boundaries based on the crop box / media box of | 374 // Compute source clip box boundaries based on the crop box / media box of |
| 360 // source page and scale factor. | 375 // source page and scale factor. |
| 361 // | 376 // |
| 362 // |page| Handle to the source page. Returned by FPDF_LoadPage function. | 377 // |page| Handle to the source page. Returned by FPDF_LoadPage function. |
| 363 // |scale_factor| specifies the scale factor that should be applied to source | 378 // |scale_factor| specifies the scale factor that should be applied to source |
| 364 // clip box boundaries. | 379 // clip box boundaries. |
| 365 // |rotated| True if source page is rotated 90 degree or 270 degree. | 380 // |rotated| True if source page is rotated 90 degree or 270 degree. |
| 366 // |clip_box| out param to hold the computed source clip box values. | 381 // |clip_box| out param to hold the computed source clip box values. |
| 367 void CalculateClipBoxBoundary(FPDF_PAGE page, double scale_factor, bool rotated, | 382 void CalculateClipBoxBoundary(FPDF_PAGE page, double scale_factor, bool rotated, |
| 368 ClipBox* clip_box) { | 383 ClipBox* clip_box) { |
| 369 if (!FPDFPage_GetCropBox(page, &clip_box->left, &clip_box->bottom, | 384 ClipBox media_box; |
| 370 &clip_box->right, &clip_box->top)) { | 385 if (!FPDFPage_GetMediaBox(page, &media_box.left, &media_box.bottom, |
| 371 if (!FPDFPage_GetMediaBox(page, &clip_box->left, &clip_box->bottom, | 386 &media_box.right, &media_box.top)) { |
| 372 &clip_box->right, &clip_box->top)) { | 387 SetDefaultClipBox(rotated, &media_box); |
| 373 // Make the default size to be letter size (8.5" X 11"). We are just | |
| 374 // following the PDFium way of handling these corner cases. PDFium always | |
| 375 // consider US-Letter as the default page size. | |
| 376 float paper_width = 612; | |
| 377 float paper_height = 792; | |
| 378 clip_box->left = 0; | |
| 379 clip_box->bottom = 0; | |
| 380 clip_box->right = rotated ? paper_height : paper_width; | |
| 381 clip_box->top = rotated ? paper_width : paper_height; | |
| 382 } | |
| 383 } | 388 } |
| 389 | |
| 390 ClipBox crop_box; | |
| 391 if (!FPDFPage_GetCropBox(page, &crop_box.left, &crop_box.bottom, | |
| 392 &crop_box.right, &crop_box.top)) { | |
| 393 SetDefaultClipBox(rotated, &crop_box); | |
| 394 } | |
| 395 | |
| 396 // Clip |media_box| to the size of |crop_box|, but ignore |crop_box| if it is | |
| 397 // bigger than |media_box|. | |
| 398 clip_box->left = | |
| 399 (crop_box.left < media_box.left) ? media_box.left : crop_box.left; | |
| 400 clip_box->right = | |
| 401 (crop_box.right > media_box.right) ? media_box.right : crop_box.right; | |
| 402 clip_box->top = (crop_box.top > media_box.top) ? media_box.top : crop_box.top; | |
| 403 clip_box->bottom = | |
| 404 (crop_box.bottom < media_box.bottom) ? media_box.bottom : crop_box.bottom; | |
| 405 | |
| 406 // Finally, scale |clip_box|. | |
| 384 clip_box->left *= scale_factor; | 407 clip_box->left *= scale_factor; |
| 385 clip_box->right *= scale_factor; | 408 clip_box->right *= scale_factor; |
| 386 clip_box->bottom *= scale_factor; | 409 clip_box->bottom *= scale_factor; |
| 387 clip_box->top *= scale_factor; | 410 clip_box->top *= scale_factor; |
| 388 } | 411 } |
| 389 | 412 |
| 390 // Calculate the clip box translation offset for a page that does need to be | 413 // Calculate the clip box translation offset for a page that does need to be |
| 391 // scaled. All parameters are in points. | 414 // scaled. All parameters are in points. |
| 392 // | 415 // |
| 393 // |content_rect| specifies the printable area of the destination page, with | 416 // |content_rect| specifies the printable area of the destination page, with |
| (...skipping 3668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4062 double* height) { | 4085 double* height) { |
| 4063 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); | 4086 FPDF_DOCUMENT doc = FPDF_LoadMemDocument(pdf_buffer, pdf_buffer_size, NULL); |
| 4064 if (!doc) | 4087 if (!doc) |
| 4065 return false; | 4088 return false; |
| 4066 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; | 4089 bool success = FPDF_GetPageSizeByIndex(doc, page_number, width, height) != 0; |
| 4067 FPDF_CloseDocument(doc); | 4090 FPDF_CloseDocument(doc); |
| 4068 return success; | 4091 return success; |
| 4069 } | 4092 } |
| 4070 | 4093 |
| 4071 } // namespace chrome_pdf | 4094 } // namespace chrome_pdf |
| OLD | NEW |