| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <fstream> | 8 #include <fstream> |
| 9 #include <iostream> | 9 #include <iostream> |
| 10 #include <iterator> | 10 #include <iterator> |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 // Need to check whether the save was successful. Ending the loop only | 328 // Need to check whether the save was successful. Ending the loop only |
| 329 // means the save was attempted. | 329 // means the save was attempted. |
| 330 base::File pdf_file( | 330 base::File pdf_file( |
| 331 pdf_file_save_path_, base::File::FLAG_OPEN | base::File::FLAG_READ); | 331 pdf_file_save_path_, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 332 ASSERT_TRUE(pdf_file.IsValid()); | 332 ASSERT_TRUE(pdf_file.IsValid()); |
| 333 } | 333 } |
| 334 | 334 |
| 335 // Converts the PDF to a PNG file so that the layout test can do an image | 335 // Converts the PDF to a PNG file so that the layout test can do an image |
| 336 // diff on this image and a reference image. | 336 // diff on this image and a reference image. |
| 337 void PdfToPng() { | 337 void PdfToPng() { |
| 338 std::string pdf_data; |
| 339 ASSERT_TRUE(base::ReadFileToString(pdf_file_save_path_, &pdf_data)); |
| 340 |
| 338 int num_pages; | 341 int num_pages; |
| 339 double max_width_in_points = 0; | 342 double max_width_in_points = 0; |
| 340 std::vector<uint8_t> bitmap_data; | 343 void* pdf_handle = nullptr; |
| 341 double total_height_in_pixels = 0; | 344 ASSERT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_data.data(), pdf_data.size(), |
| 342 std::string pdf_data; | 345 &num_pages, &max_width_in_points, |
| 343 | 346 &pdf_handle)); |
| 344 ASSERT_TRUE(base::ReadFileToString(pdf_file_save_path_, &pdf_data)); | |
| 345 ASSERT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_data.data(), | |
| 346 pdf_data.size(), | |
| 347 &num_pages, | |
| 348 &max_width_in_points)); | |
| 349 | |
| 350 ASSERT_GT(num_pages, 0); | 347 ASSERT_GT(num_pages, 0); |
| 351 double max_width_in_pixels = | 348 double max_width_in_pixels = |
| 352 ConvertUnitDouble(max_width_in_points, kPointsPerInch, kDpi); | 349 ConvertUnitDouble(max_width_in_points, kPointsPerInch, kDpi); |
| 353 | 350 |
| 351 std::vector<uint8_t> bitmap_data; |
| 352 double total_height_in_pixels = 0; |
| 354 for (int i = 0; i < num_pages; ++i) { | 353 for (int i = 0; i < num_pages; ++i) { |
| 355 double width_in_points, height_in_points; | 354 double width_in_points, height_in_points; |
| 356 ASSERT_TRUE(chrome_pdf::GetPDFPageSizeByIndex(pdf_data.data(), | 355 ASSERT_TRUE(chrome_pdf::GetPDFPageSizeByIndex( |
| 357 pdf_data.size(), | 356 pdf_handle, i, &width_in_points, &height_in_points)); |
| 358 i, | |
| 359 &width_in_points, | |
| 360 &height_in_points)); | |
| 361 | 357 |
| 362 double width_in_pixels = ConvertUnitDouble( | 358 double width_in_pixels = ConvertUnitDouble( |
| 363 width_in_points, kPointsPerInch, kDpi); | 359 width_in_points, kPointsPerInch, kDpi); |
| 364 double height_in_pixels = ConvertUnitDouble( | 360 double height_in_pixels = ConvertUnitDouble( |
| 365 height_in_points, kPointsPerInch, kDpi); | 361 height_in_points, kPointsPerInch, kDpi); |
| 366 | 362 |
| 367 // The image will be rotated if |width_in_pixels| is greater than | 363 // The image will be rotated if |width_in_pixels| is greater than |
| 368 // |height_in_pixels|. This is because the page will be rotated to fit | 364 // |height_in_pixels|. This is because the page will be rotated to fit |
| 369 // within a piece of paper. Therefore, |width_in_pixels| and | 365 // within a piece of paper. Therefore, |width_in_pixels| and |
| 370 // |height_in_pixels| have to be swapped or else they won't reflect the | 366 // |height_in_pixels| have to be swapped or else they won't reflect the |
| (...skipping 10 matching lines...) Expand all Loading... |
| 381 settings.area().height() > int_max / (kColorChannels * | 377 settings.area().height() > int_max / (kColorChannels * |
| 382 settings.area().width())) { | 378 settings.area().width())) { |
| 383 FAIL() << "The dimensions of the image are too large." | 379 FAIL() << "The dimensions of the image are too large." |
| 384 << "Decrease the DPI or the dimensions of the image."; | 380 << "Decrease the DPI or the dimensions of the image."; |
| 385 } | 381 } |
| 386 | 382 |
| 387 std::vector<uint8_t> page_bitmap_data( | 383 std::vector<uint8_t> page_bitmap_data( |
| 388 kColorChannels * settings.area().size().GetArea()); | 384 kColorChannels * settings.area().size().GetArea()); |
| 389 | 385 |
| 390 ASSERT_TRUE(chrome_pdf::RenderPDFPageToBitmap( | 386 ASSERT_TRUE(chrome_pdf::RenderPDFPageToBitmap( |
| 391 pdf_data.data(), | 387 pdf_handle, i, page_bitmap_data.data(), |
| 392 pdf_data.size(), | 388 settings.area().size().width(), settings.area().size().height(), |
| 393 i, | 389 settings.dpi(), true)); |
| 394 page_bitmap_data.data(), | |
| 395 settings.area().size().width(), | |
| 396 settings.area().size().height(), | |
| 397 settings.dpi(), | |
| 398 true)); | |
| 399 FillPng(&page_bitmap_data, | 390 FillPng(&page_bitmap_data, |
| 400 width_in_pixels, | 391 width_in_pixels, |
| 401 max_width_in_pixels, | 392 max_width_in_pixels, |
| 402 settings.area().size().height()); | 393 settings.area().size().height()); |
| 403 bitmap_data.insert(bitmap_data.end(), | 394 bitmap_data.insert(bitmap_data.end(), |
| 404 page_bitmap_data.begin(), | 395 page_bitmap_data.begin(), |
| 405 page_bitmap_data.end()); | 396 page_bitmap_data.end()); |
| 406 } | 397 } |
| 407 | 398 |
| 399 chrome_pdf::ReleasePDFHandle(pdf_handle); |
| 408 CreatePng(bitmap_data, max_width_in_pixels, total_height_in_pixels); | 400 CreatePng(bitmap_data, max_width_in_pixels, total_height_in_pixels); |
| 409 } | 401 } |
| 410 | 402 |
| 411 // Fills out a bitmap with whitespace so that the image will correctly fit | 403 // Fills out a bitmap with whitespace so that the image will correctly fit |
| 412 // within a PNG that is wider than the bitmap itself. | 404 // within a PNG that is wider than the bitmap itself. |
| 413 void FillPng(std::vector<uint8_t>* bitmap, | 405 void FillPng(std::vector<uint8_t>* bitmap, |
| 414 int current_width, | 406 int current_width, |
| 415 int desired_width, | 407 int desired_width, |
| 416 int height) { | 408 int height) { |
| 417 ASSERT_TRUE(bitmap); | 409 ASSERT_TRUE(bitmap); |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 // waiting for this message and start waiting for the image data. | 633 // waiting for this message and start waiting for the image data. |
| 642 std::cout << "#EOF\n"; | 634 std::cout << "#EOF\n"; |
| 643 std::cout.flush(); | 635 std::cout.flush(); |
| 644 | 636 |
| 645 SendPng(); | 637 SendPng(); |
| 646 Reset(); | 638 Reset(); |
| 647 } | 639 } |
| 648 } | 640 } |
| 649 | 641 |
| 650 } // namespace printing | 642 } // namespace printing |
| OLD | NEW |