[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "printing/image.h" |
| 6 | |
avi | 126e93c | 2015-12-21 21:48:16 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | #include "base/macros.h" |
[email protected] | 47d1474 | 2013-01-26 04:09:10 | [diff] [blame] | 11 | #include "base/win/scoped_gdi_object.h" |
| 12 | #include "base/win/scoped_hdc.h" |
| 13 | #include "base/win/scoped_select_object.h" |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame^] | 14 | #include "printing/metafile.h" |
tomhudson | 828dddb | 2015-12-04 14:34:16 | [diff] [blame] | 15 | #include "skia/ext/skia_utils_win.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 16 | #include "ui/gfx/gdi_util.h" // EMF support |
tfarina | 3b0452d | 2014-12-31 15:20:09 | [diff] [blame] | 17 | #include "ui/gfx/geometry/rect.h" |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 18 | |
[email protected] | 47d1474 | 2013-01-26 04:09:10 | [diff] [blame] | 19 | |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | // A simple class which temporarily overrides system settings. |
| 23 | // The bitmap image rendered via the PlayEnhMetaFile() function depends on |
| 24 | // some system settings. |
| 25 | // As a workaround for such dependency, this class saves the system settings |
| 26 | // and changes them. This class also restore the saved settings in its |
| 27 | // destructor. |
| 28 | class DisableFontSmoothing { |
| 29 | public: |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 30 | DisableFontSmoothing() : enable_again_(false) { |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 31 | BOOL enabled; |
| 32 | if (SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &enabled, 0) && |
| 33 | enabled) { |
| 34 | if (SystemParametersInfo(SPI_SETFONTSMOOTHING, FALSE, NULL, 0)) |
| 35 | enable_again_ = true; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | ~DisableFontSmoothing() { |
| 40 | if (enable_again_) { |
| 41 | BOOL result = SystemParametersInfo(SPI_SETFONTSMOOTHING, TRUE, NULL, 0); |
| 42 | DCHECK(result); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | bool enable_again_; |
| 48 | |
| 49 | DISALLOW_COPY_AND_ASSIGN(DisableFontSmoothing); |
| 50 | }; |
| 51 | |
| 52 | } // namespace |
| 53 | |
| 54 | namespace printing { |
| 55 | |
thestig | 3109df1 | 2017-04-26 21:57:25 | [diff] [blame^] | 56 | bool Image::LoadMetafile(const Metafile& metafile) { |
| 57 | gfx::Rect rect(metafile.GetPageBounds(1)); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 58 | DisableFontSmoothing disable_in_this_scope; |
| 59 | |
| 60 | // Create a temporary HDC and bitmap to retrieve the rendered data. |
[email protected] | 47d1474 | 2013-01-26 04:09:10 | [diff] [blame] | 61 | base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL)); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 62 | BITMAPV4HEADER hdr; |
| 63 | DCHECK_EQ(rect.x(), 0); |
| 64 | DCHECK_EQ(rect.y(), 0); |
| 65 | DCHECK_GE(rect.width(), 0); // Metafile could be empty. |
| 66 | DCHECK_GE(rect.height(), 0); |
| 67 | |
| 68 | if (rect.width() < 1 || rect.height() < 1) |
| 69 | return false; |
| 70 | |
| 71 | size_ = rect.size(); |
| 72 | gfx::CreateBitmapV4Header(rect.width(), rect.height(), &hdr); |
[email protected] | 47d1474 | 2013-01-26 04:09:10 | [diff] [blame] | 73 | unsigned char* bits = NULL; |
| 74 | base::win::ScopedBitmap bitmap( |
rvargas | 9b7c6b0f | 2014-09-25 19:22:48 | [diff] [blame] | 75 | ::CreateDIBSection(hdc.Get(), reinterpret_cast<BITMAPINFO*>(&hdr), 0, |
[email protected] | 47d1474 | 2013-01-26 04:09:10 | [diff] [blame] | 76 | reinterpret_cast<void**>(&bits), NULL, 0)); |
anpol | 280746c | 2015-12-18 08:39:03 | [diff] [blame] | 77 | DCHECK(bitmap.is_valid()); |
| 78 | base::win::ScopedSelectObject select_object(hdc.Get(), bitmap.get()); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 79 | |
rvargas | 9b7c6b0f | 2014-09-25 19:22:48 | [diff] [blame] | 80 | skia::InitializeDC(hdc.Get()); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 81 | |
rvargas | 9b7c6b0f | 2014-09-25 19:22:48 | [diff] [blame] | 82 | bool success = metafile.Playback(hdc.Get(), NULL); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 83 | |
thestig | 707a24b2 | 2015-09-14 18:16:33 | [diff] [blame] | 84 | row_length_ = size_.width() * sizeof(uint32_t); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 85 | size_t bytes = row_length_ * size_.height(); |
| 86 | DCHECK(bytes); |
| 87 | |
[email protected] | 47d1474 | 2013-01-26 04:09:10 | [diff] [blame] | 88 | data_.assign(bits, bits + bytes); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 89 | |
| 90 | return success; |
| 91 | } |
| 92 | |
| 93 | } // namespace printing |