Add partial pre-read functionality to browser startup (Windows).
[chromium-blink-merge.git] / printing / image_win.cc
blobde731e06be2b8654b7b85bf0495a12b19d24c700
1 // Copyright (c) 2011 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.
5 #include "printing/image.h"
7 #include "printing/metafile.h"
8 #include "skia/ext/platform_device.h"
9 #include "ui/gfx/gdi_util.h" // EMF support
10 #include "ui/gfx/rect.h"
12 namespace {
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;
31 ~DisableFontSmoothing() {
32 if (enable_again_) {
33 BOOL result = SystemParametersInfo(SPI_SETFONTSMOOTHING, TRUE, NULL, 0);
34 DCHECK(result);
38 private:
39 bool enable_again_;
41 DISALLOW_COPY_AND_ASSIGN(DisableFontSmoothing);
44 } // namespace
46 namespace printing {
48 bool Image::LoadMetafile(const Metafile& metafile) {
49 gfx::Rect rect(metafile.GetPageBounds(1));
50 DisableFontSmoothing disable_in_this_scope;
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);
60 if (rect.width() < 1 || rect.height() < 1)
61 return false;
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));
72 skia::InitializeDC(hdc);
74 bool success = metafile.Playback(hdc, NULL);
76 row_length_ = size_.width() * sizeof(uint32);
77 size_t bytes = row_length_ * size_.height();
78 DCHECK(bytes);
80 data_.resize(bytes);
81 memcpy(&*data_.begin(), bits, bytes);
83 DeleteDC(hdc);
84 DeleteObject(bitmap);
86 return success;
89 } // namespace printing