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 | |
341 int num_pages; | 338 int num_pages; |
342 double max_width_in_points = 0; | 339 double max_width_in_points = 0; |
343 void* pdf_handle = nullptr; | 340 std::vector<uint8_t> bitmap_data; |
| 341 double total_height_in_pixels = 0; |
| 342 std::string pdf_data; |
| 343 |
| 344 ASSERT_TRUE(base::ReadFileToString(pdf_file_save_path_, &pdf_data)); |
344 ASSERT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_data.data(), pdf_data.size(), | 345 ASSERT_TRUE(chrome_pdf::GetPDFDocInfo(pdf_data.data(), pdf_data.size(), |
345 &num_pages, &max_width_in_points, | 346 &num_pages, &max_width_in_points)); |
346 &pdf_handle)); | 347 |
347 ASSERT_GT(num_pages, 0); | 348 ASSERT_GT(num_pages, 0); |
348 double max_width_in_pixels = | 349 double max_width_in_pixels = |
349 ConvertUnitDouble(max_width_in_points, kPointsPerInch, kDpi); | 350 ConvertUnitDouble(max_width_in_points, kPointsPerInch, kDpi); |
350 | 351 |
351 std::vector<uint8_t> bitmap_data; | |
352 double total_height_in_pixels = 0; | |
353 for (int i = 0; i < num_pages; ++i) { | 352 for (int i = 0; i < num_pages; ++i) { |
354 double width_in_points, height_in_points; | 353 double width_in_points, height_in_points; |
355 ASSERT_TRUE(chrome_pdf::GetPDFPageSizeByIndex( | 354 ASSERT_TRUE(chrome_pdf::GetPDFPageSizeByIndex( |
356 pdf_handle, i, &width_in_points, &height_in_points)); | 355 pdf_data.data(), pdf_data.size(), i, &width_in_points, |
| 356 &height_in_points)); |
357 | 357 |
358 double width_in_pixels = ConvertUnitDouble( | 358 double width_in_pixels = ConvertUnitDouble( |
359 width_in_points, kPointsPerInch, kDpi); | 359 width_in_points, kPointsPerInch, kDpi); |
360 double height_in_pixels = ConvertUnitDouble( | 360 double height_in_pixels = ConvertUnitDouble( |
361 height_in_points, kPointsPerInch, kDpi); | 361 height_in_points, kPointsPerInch, kDpi); |
362 | 362 |
363 // 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 |
364 // |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 |
365 // within a piece of paper. Therefore, |width_in_pixels| and | 365 // within a piece of paper. Therefore, |width_in_pixels| and |
366 // |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... |
377 settings.area.height() > | 377 settings.area.height() > |
378 int_max / (kColorChannels * settings.area.width())) { | 378 int_max / (kColorChannels * settings.area.width())) { |
379 FAIL() << "The dimensions of the image are too large." | 379 FAIL() << "The dimensions of the image are too large." |
380 << "Decrease the DPI or the dimensions of the image."; | 380 << "Decrease the DPI or the dimensions of the image."; |
381 } | 381 } |
382 | 382 |
383 std::vector<uint8_t> page_bitmap_data(kColorChannels * | 383 std::vector<uint8_t> page_bitmap_data(kColorChannels * |
384 settings.area.size().GetArea()); | 384 settings.area.size().GetArea()); |
385 | 385 |
386 ASSERT_TRUE(chrome_pdf::RenderPDFPageToBitmap( | 386 ASSERT_TRUE(chrome_pdf::RenderPDFPageToBitmap( |
387 pdf_handle, i, page_bitmap_data.data(), settings.area.size().width(), | 387 pdf_data.data(), pdf_data.size(), i, page_bitmap_data.data(), |
388 settings.area.size().height(), settings.dpi, settings.autorotate)); | 388 settings.area.size().width(), settings.area.size().height(), |
| 389 settings.dpi, settings.autorotate)); |
389 FillPng(&page_bitmap_data, width_in_pixels, max_width_in_pixels, | 390 FillPng(&page_bitmap_data, width_in_pixels, max_width_in_pixels, |
390 settings.area.size().height()); | 391 settings.area.size().height()); |
391 bitmap_data.insert(bitmap_data.end(), | 392 bitmap_data.insert(bitmap_data.end(), |
392 page_bitmap_data.begin(), | 393 page_bitmap_data.begin(), |
393 page_bitmap_data.end()); | 394 page_bitmap_data.end()); |
394 } | 395 } |
395 | 396 |
396 chrome_pdf::ReleasePDFHandle(pdf_handle); | |
397 CreatePng(bitmap_data, max_width_in_pixels, total_height_in_pixels); | 397 CreatePng(bitmap_data, max_width_in_pixels, total_height_in_pixels); |
398 } | 398 } |
399 | 399 |
400 // Fills out a bitmap with whitespace so that the image will correctly fit | 400 // Fills out a bitmap with whitespace so that the image will correctly fit |
401 // within a PNG that is wider than the bitmap itself. | 401 // within a PNG that is wider than the bitmap itself. |
402 void FillPng(std::vector<uint8_t>* bitmap, | 402 void FillPng(std::vector<uint8_t>* bitmap, |
403 int current_width, | 403 int current_width, |
404 int desired_width, | 404 int desired_width, |
405 int height) { | 405 int height) { |
406 ASSERT_TRUE(bitmap); | 406 ASSERT_TRUE(bitmap); |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
630 // waiting for this message and start waiting for the image data. | 630 // waiting for this message and start waiting for the image data. |
631 std::cout << "#EOF\n"; | 631 std::cout << "#EOF\n"; |
632 std::cout.flush(); | 632 std::cout.flush(); |
633 | 633 |
634 SendPng(); | 634 SendPng(); |
635 Reset(); | 635 Reset(); |
636 } | 636 } |
637 } | 637 } |
638 | 638 |
639 } // namespace printing | 639 } // namespace printing |
OLD | NEW |