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