| 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 "printing/emf_win.h" | 5 #include "printing/emf_win.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> |
| 10 |
| 9 #include "base/files/file.h" | 11 #include "base/files/file.h" |
| 10 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 11 #include "base/logging.h" | 13 #include "base/logging.h" |
| 12 #include "base/macros.h" | 14 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/win/scoped_gdi_object.h" | 15 #include "base/win/scoped_gdi_object.h" |
| 15 #include "base/win/scoped_hdc.h" | 16 #include "base/win/scoped_hdc.h" |
| 16 #include "base/win/scoped_select_object.h" | 17 #include "base/win/scoped_select_object.h" |
| 17 #include "skia/ext/skia_utils_win.h" | 18 #include "skia/ext/skia_utils_win.h" |
| 18 #include "third_party/skia/include/core/SkBitmap.h" | 19 #include "third_party/skia/include/core/SkBitmap.h" |
| 19 #include "ui/gfx/codec/jpeg_codec.h" | 20 #include "ui/gfx/codec/jpeg_codec.h" |
| 20 #include "ui/gfx/codec/png_codec.h" | 21 #include "ui/gfx/codec/png_codec.h" |
| 21 #include "ui/gfx/gdi_util.h" | 22 #include "ui/gfx/gdi_util.h" |
| 22 #include "ui/gfx/geometry/rect.h" | 23 #include "ui/gfx/geometry/rect.h" |
| 23 #include "ui/gfx/geometry/size.h" | 24 #include "ui/gfx/geometry/size.h" |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 const EMRSTRETCHDIBITS * sdib_record = | 354 const EMRSTRETCHDIBITS * sdib_record = |
| 354 reinterpret_cast<const EMRSTRETCHDIBITS*>(record()); | 355 reinterpret_cast<const EMRSTRETCHDIBITS*>(record()); |
| 355 const BYTE* record_start = reinterpret_cast<const BYTE *>(record()); | 356 const BYTE* record_start = reinterpret_cast<const BYTE *>(record()); |
| 356 const BITMAPINFOHEADER *bmih = | 357 const BITMAPINFOHEADER *bmih = |
| 357 reinterpret_cast<const BITMAPINFOHEADER *>(record_start + | 358 reinterpret_cast<const BITMAPINFOHEADER *>(record_start + |
| 358 sdib_record->offBmiSrc); | 359 sdib_record->offBmiSrc); |
| 359 const BYTE* bits = record_start + sdib_record->offBitsSrc; | 360 const BYTE* bits = record_start + sdib_record->offBitsSrc; |
| 360 bool play_normally = true; | 361 bool play_normally = true; |
| 361 res = false; | 362 res = false; |
| 362 HDC hdc = context->hdc; | 363 HDC hdc = context->hdc; |
| 363 scoped_ptr<SkBitmap> bitmap; | 364 std::unique_ptr<SkBitmap> bitmap; |
| 364 if (bmih->biCompression == BI_JPEG) { | 365 if (bmih->biCompression == BI_JPEG) { |
| 365 if (!DIBFormatNativelySupported(hdc, CHECKJPEGFORMAT, bits, | 366 if (!DIBFormatNativelySupported(hdc, CHECKJPEGFORMAT, bits, |
| 366 bmih->biSizeImage)) { | 367 bmih->biSizeImage)) { |
| 367 play_normally = false; | 368 play_normally = false; |
| 368 bitmap.reset(gfx::JPEGCodec::Decode(bits, bmih->biSizeImage)); | 369 bitmap.reset(gfx::JPEGCodec::Decode(bits, bmih->biSizeImage)); |
| 369 } | 370 } |
| 370 } else if (bmih->biCompression == BI_PNG) { | 371 } else if (bmih->biCompression == BI_PNG) { |
| 371 if (!DIBFormatNativelySupported(hdc, CHECKPNGFORMAT, bits, | 372 if (!DIBFormatNativelySupported(hdc, CHECKPNGFORMAT, bits, |
| 372 bmih->biSizeImage)) { | 373 bmih->biSizeImage)) { |
| 373 play_normally = false; | 374 play_normally = false; |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 bool Emf::IsAlphaBlendUsed() const { | 518 bool Emf::IsAlphaBlendUsed() const { |
| 518 bool result = false; | 519 bool result = false; |
| 519 ::EnumEnhMetaFile(NULL, | 520 ::EnumEnhMetaFile(NULL, |
| 520 emf(), | 521 emf(), |
| 521 &IsAlphaBlendUsedEnumProc, | 522 &IsAlphaBlendUsedEnumProc, |
| 522 &result, | 523 &result, |
| 523 NULL); | 524 NULL); |
| 524 return result; | 525 return result; |
| 525 } | 526 } |
| 526 | 527 |
| 527 scoped_ptr<Emf> Emf::RasterizeMetafile(int raster_area_in_pixels) const { | 528 std::unique_ptr<Emf> Emf::RasterizeMetafile(int raster_area_in_pixels) const { |
| 528 gfx::Rect page_bounds = GetPageBounds(1); | 529 gfx::Rect page_bounds = GetPageBounds(1); |
| 529 gfx::Size page_size(page_bounds.size()); | 530 gfx::Size page_size(page_bounds.size()); |
| 530 if (page_size.GetArea() <= 0) { | 531 if (page_size.GetArea() <= 0) { |
| 531 NOTREACHED() << "Metafile is empty"; | 532 NOTREACHED() << "Metafile is empty"; |
| 532 page_bounds = gfx::Rect(1, 1); | 533 page_bounds = gfx::Rect(1, 1); |
| 533 } | 534 } |
| 534 | 535 |
| 535 float scale = sqrt( | 536 float scale = sqrt( |
| 536 static_cast<float>(raster_area_in_pixels) / page_size.GetArea()); | 537 static_cast<float>(raster_area_in_pixels) / page_size.GetArea()); |
| 537 page_size.set_width(std::max<int>(1, page_size.width() * scale)); | 538 page_size.set_width(std::max<int>(1, page_size.width() * scale)); |
| 538 page_size.set_height(std::max<int>(1, page_size.height() * scale)); | 539 page_size.set_height(std::max<int>(1, page_size.height() * scale)); |
| 539 | 540 |
| 540 | 541 |
| 541 RasterBitmap bitmap(page_size); | 542 RasterBitmap bitmap(page_size); |
| 542 | 543 |
| 543 gfx::Rect bitmap_rect(page_size); | 544 gfx::Rect bitmap_rect(page_size); |
| 544 RECT rect = bitmap_rect.ToRECT(); | 545 RECT rect = bitmap_rect.ToRECT(); |
| 545 Playback(bitmap.context(), &rect); | 546 Playback(bitmap.context(), &rect); |
| 546 | 547 |
| 547 scoped_ptr<Emf> result(new Emf); | 548 std::unique_ptr<Emf> result(new Emf); |
| 548 result->Init(); | 549 result->Init(); |
| 549 HDC hdc = result->context(); | 550 HDC hdc = result->context(); |
| 550 DCHECK(hdc); | 551 DCHECK(hdc); |
| 551 skia::InitializeDC(hdc); | 552 skia::InitializeDC(hdc); |
| 552 | 553 |
| 553 // Params are ignored. | 554 // Params are ignored. |
| 554 result->StartPage(page_bounds.size(), page_bounds, 1); | 555 result->StartPage(page_bounds.size(), page_bounds, 1); |
| 555 | 556 |
| 556 ::ModifyWorldTransform(hdc, NULL, MWT_IDENTITY); | 557 ::ModifyWorldTransform(hdc, NULL, MWT_IDENTITY); |
| 557 XFORM xform = { | 558 XFORM xform = { |
| 558 static_cast<float>(page_bounds.width()) / bitmap_rect.width(), | 559 static_cast<float>(page_bounds.width()) / bitmap_rect.width(), |
| 559 0, | 560 0, |
| 560 0, | 561 0, |
| 561 static_cast<float>(page_bounds.height()) / bitmap_rect.height(), | 562 static_cast<float>(page_bounds.height()) / bitmap_rect.height(), |
| 562 static_cast<float>(page_bounds.x()), | 563 static_cast<float>(page_bounds.x()), |
| 563 static_cast<float>(page_bounds.y()), | 564 static_cast<float>(page_bounds.y()), |
| 564 }; | 565 }; |
| 565 ::SetWorldTransform(hdc, &xform); | 566 ::SetWorldTransform(hdc, &xform); |
| 566 ::BitBlt(hdc, 0, 0, bitmap_rect.width(), bitmap_rect.height(), | 567 ::BitBlt(hdc, 0, 0, bitmap_rect.width(), bitmap_rect.height(), |
| 567 bitmap.context(), bitmap_rect.x(), bitmap_rect.y(), SRCCOPY); | 568 bitmap.context(), bitmap_rect.x(), bitmap_rect.y(), SRCCOPY); |
| 568 | 569 |
| 569 result->FinishPage(); | 570 result->FinishPage(); |
| 570 result->FinishDocument(); | 571 result->FinishDocument(); |
| 571 | 572 |
| 572 return result; | 573 return result; |
| 573 } | 574 } |
| 574 | 575 |
| 575 scoped_ptr<Emf> Emf::RasterizeAlphaBlend() const { | 576 std::unique_ptr<Emf> Emf::RasterizeAlphaBlend() const { |
| 576 gfx::Rect page_bounds = GetPageBounds(1); | 577 gfx::Rect page_bounds = GetPageBounds(1); |
| 577 if (page_bounds.size().GetArea() <= 0) { | 578 if (page_bounds.size().GetArea() <= 0) { |
| 578 NOTREACHED() << "Metafile is empty"; | 579 NOTREACHED() << "Metafile is empty"; |
| 579 page_bounds = gfx::Rect(1, 1); | 580 page_bounds = gfx::Rect(1, 1); |
| 580 } | 581 } |
| 581 | 582 |
| 582 RasterBitmap bitmap(page_bounds.size()); | 583 RasterBitmap bitmap(page_bounds.size()); |
| 583 | 584 |
| 584 // Map metafile page_bounds.x(), page_bounds.y() to bitmap 0, 0. | 585 // Map metafile page_bounds.x(), page_bounds.y() to bitmap 0, 0. |
| 585 XFORM xform = {1, | 586 XFORM xform = {1, |
| 586 0, | 587 0, |
| 587 0, | 588 0, |
| 588 1, | 589 1, |
| 589 static_cast<float>(-page_bounds.x()), | 590 static_cast<float>(-page_bounds.x()), |
| 590 static_cast<float>(-page_bounds.y())}; | 591 static_cast<float>(-page_bounds.y())}; |
| 591 ::SetWorldTransform(bitmap.context(), &xform); | 592 ::SetWorldTransform(bitmap.context(), &xform); |
| 592 | 593 |
| 593 scoped_ptr<Emf> result(new Emf); | 594 std::unique_ptr<Emf> result(new Emf); |
| 594 result->Init(); | 595 result->Init(); |
| 595 HDC hdc = result->context(); | 596 HDC hdc = result->context(); |
| 596 DCHECK(hdc); | 597 DCHECK(hdc); |
| 597 skia::InitializeDC(hdc); | 598 skia::InitializeDC(hdc); |
| 598 | 599 |
| 599 HDC bitmap_dc = bitmap.context(); | 600 HDC bitmap_dc = bitmap.context(); |
| 600 RECT rect = page_bounds.ToRECT(); | 601 RECT rect = page_bounds.ToRECT(); |
| 601 ::EnumEnhMetaFile(hdc, emf(), &RasterizeAlphaBlendProc, &bitmap_dc, &rect); | 602 ::EnumEnhMetaFile(hdc, emf(), &RasterizeAlphaBlendProc, &bitmap_dc, &rect); |
| 602 | 603 |
| 603 result->FinishDocument(); | 604 result->FinishDocument(); |
| 604 | 605 |
| 605 return result; | 606 return result; |
| 606 } | 607 } |
| 607 | 608 |
| 608 | 609 |
| 609 } // namespace printing | 610 } // namespace printing |
| OLD | NEW |