blob: 787c24aa52716951b01484d1a3a054cd35e9f706 [file] [log] [blame]
[email protected]7d7489902011-04-11 21:54:061// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]48091b02010-10-21 02:38:112// 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
avi126e93c2015-12-21 21:48:167#include <stddef.h>
8#include <stdint.h>
9
10#include "base/macros.h"
[email protected]47d14742013-01-26 04:09:1011#include "base/win/scoped_gdi_object.h"
12#include "base/win/scoped_hdc.h"
13#include "base/win/scoped_select_object.h"
thestig3109df12017-04-26 21:57:2514#include "printing/metafile.h"
tomhudson828dddb2015-12-04 14:34:1615#include "skia/ext/skia_utils_win.h"
[email protected]08397d52011-02-05 01:53:3816#include "ui/gfx/gdi_util.h" // EMF support
tfarina3b0452d2014-12-31 15:20:0917#include "ui/gfx/geometry/rect.h"
[email protected]48091b02010-10-21 02:38:1118
[email protected]47d14742013-01-26 04:09:1019
[email protected]48091b02010-10-21 02:38:1120namespace {
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.
28class DisableFontSmoothing {
29 public:
thestig707a24b22015-09-14 18:16:3330 DisableFontSmoothing() : enable_again_(false) {
[email protected]48091b02010-10-21 02:38:1131 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
54namespace printing {
55
thestig3109df12017-04-26 21:57:2556bool Image::LoadMetafile(const Metafile& metafile) {
57 gfx::Rect rect(metafile.GetPageBounds(1));
[email protected]48091b02010-10-21 02:38:1158 DisableFontSmoothing disable_in_this_scope;
59
60 // Create a temporary HDC and bitmap to retrieve the rendered data.
[email protected]47d14742013-01-26 04:09:1061 base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL));
[email protected]48091b02010-10-21 02:38:1162 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]47d14742013-01-26 04:09:1073 unsigned char* bits = NULL;
74 base::win::ScopedBitmap bitmap(
rvargas9b7c6b0f2014-09-25 19:22:4875 ::CreateDIBSection(hdc.Get(), reinterpret_cast<BITMAPINFO*>(&hdr), 0,
[email protected]47d14742013-01-26 04:09:1076 reinterpret_cast<void**>(&bits), NULL, 0));
anpol280746c2015-12-18 08:39:0377 DCHECK(bitmap.is_valid());
78 base::win::ScopedSelectObject select_object(hdc.Get(), bitmap.get());
[email protected]48091b02010-10-21 02:38:1179
rvargas9b7c6b0f2014-09-25 19:22:4880 skia::InitializeDC(hdc.Get());
[email protected]48091b02010-10-21 02:38:1181
rvargas9b7c6b0f2014-09-25 19:22:4882 bool success = metafile.Playback(hdc.Get(), NULL);
[email protected]48091b02010-10-21 02:38:1183
thestig707a24b22015-09-14 18:16:3384 row_length_ = size_.width() * sizeof(uint32_t);
[email protected]48091b02010-10-21 02:38:1185 size_t bytes = row_length_ * size_.height();
86 DCHECK(bytes);
87
[email protected]47d14742013-01-26 04:09:1088 data_.assign(bits, bits + bytes);
[email protected]48091b02010-10-21 02:38:1189
90 return success;
91}
92
93} // namespace printing