[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 | |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 7 | #include "printing/metafile.h" |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 8 | #include "skia/ext/platform_device.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 9 | #include "ui/gfx/gdi_util.h" // EMF support |
| 10 | #include "ui/gfx/rect.h" |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 11 | |
| 12 | namespace { |
| 13 | |
| 14 | // A simple class which temporarily overrides system settings. |
| 15 | // The bitmap image rendered via the PlayEnhMetaFile() function depends on |
| 16 | // some system settings. |
| 17 | // As a workaround for such dependency, this class saves the system settings |
| 18 | // and changes them. This class also restore the saved settings in its |
| 19 | // destructor. |
| 20 | class DisableFontSmoothing { |
| 21 | public: |
| 22 | explicit DisableFontSmoothing() : enable_again_(false) { |
| 23 | BOOL enabled; |
| 24 | if (SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &enabled, 0) && |
| 25 | enabled) { |
| 26 | if (SystemParametersInfo(SPI_SETFONTSMOOTHING, FALSE, NULL, 0)) |
| 27 | enable_again_ = true; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | ~DisableFontSmoothing() { |
| 32 | if (enable_again_) { |
| 33 | BOOL result = SystemParametersInfo(SPI_SETFONTSMOOTHING, TRUE, NULL, 0); |
| 34 | DCHECK(result); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | private: |
| 39 | bool enable_again_; |
| 40 | |
| 41 | DISALLOW_COPY_AND_ASSIGN(DisableFontSmoothing); |
| 42 | }; |
| 43 | |
| 44 | } // namespace |
| 45 | |
| 46 | namespace printing { |
| 47 | |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 48 | bool Image::LoadMetafile(const Metafile& metafile) { |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 49 | gfx::Rect rect(metafile.GetPageBounds(1)); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 50 | DisableFontSmoothing disable_in_this_scope; |
| 51 | |
| 52 | // Create a temporary HDC and bitmap to retrieve the rendered data. |
| 53 | HDC hdc = CreateCompatibleDC(NULL); |
| 54 | BITMAPV4HEADER hdr; |
| 55 | DCHECK_EQ(rect.x(), 0); |
| 56 | DCHECK_EQ(rect.y(), 0); |
| 57 | DCHECK_GE(rect.width(), 0); // Metafile could be empty. |
| 58 | DCHECK_GE(rect.height(), 0); |
| 59 | |
| 60 | if (rect.width() < 1 || rect.height() < 1) |
| 61 | return false; |
| 62 | |
| 63 | size_ = rect.size(); |
| 64 | gfx::CreateBitmapV4Header(rect.width(), rect.height(), &hdr); |
| 65 | void* bits; |
| 66 | HBITMAP bitmap = CreateDIBSection(hdc, |
| 67 | reinterpret_cast<BITMAPINFO*>(&hdr), 0, |
| 68 | &bits, NULL, 0); |
| 69 | DCHECK(bitmap); |
| 70 | DCHECK(SelectObject(hdc, bitmap)); |
| 71 | |
[email protected] | 62f2e80 | 2011-05-26 14:28:35 | [diff] [blame] | 72 | skia::InitializeDC(hdc); |
[email protected] | 48091b0 | 2010-10-21 02:38:11 | [diff] [blame] | 73 | |
| 74 | bool success = metafile.Playback(hdc, NULL); |
| 75 | |
| 76 | row_length_ = size_.width() * sizeof(uint32); |
| 77 | size_t bytes = row_length_ * size_.height(); |
| 78 | DCHECK(bytes); |
| 79 | |
| 80 | data_.resize(bytes); |
| 81 | memcpy(&*data_.begin(), bits, bytes); |
| 82 | |
| 83 | DeleteDC(hdc); |
| 84 | DeleteObject(bitmap); |
| 85 | |
| 86 | return success; |
| 87 | } |
| 88 | |
| 89 | } // namespace printing |