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"
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
20 class DisableFontSmoothing
{
22 explicit DisableFontSmoothing() : enable_again_(false) {
24 if (SystemParametersInfo(SPI_GETFONTSMOOTHING
, 0, &enabled
, 0) &&
26 if (SystemParametersInfo(SPI_SETFONTSMOOTHING
, FALSE
, NULL
, 0))
31 ~DisableFontSmoothing() {
33 BOOL result
= SystemParametersInfo(SPI_SETFONTSMOOTHING
, TRUE
, NULL
, 0);
41 DISALLOW_COPY_AND_ASSIGN(DisableFontSmoothing
);
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
);
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)
64 gfx::CreateBitmapV4Header(rect
.width(), rect
.height(), &hdr
);
66 HBITMAP bitmap
= CreateDIBSection(hdc
,
67 reinterpret_cast<BITMAPINFO
*>(&hdr
), 0,
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();
81 memcpy(&*data_
.begin(), bits
, bytes
);
89 } // namespace printing