Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / printing / image_win.cc
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.
4
5 #include "printing/image.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include "base/macros.h"
11 #include "base/win/scoped_gdi_object.h"
12 #include "base/win/scoped_hdc.h"
13 #include "base/win/scoped_select_object.h"
14 #include "printing/metafile.h"
15 #include "skia/ext/skia_utils_win.h"
16 #include "ui/gfx/gdi_util.h"  // EMF support
17 #include "ui/gfx/geometry/rect.h"
18
19
20 namespace {
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.
28 class DisableFontSmoothing {
29  public:
30   DisableFontSmoothing() : enable_again_(false) {
31     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
54 namespace printing {
55
56 bool Image::LoadMetafile(const Metafile& metafile) {
57     gfx::Rect rect(metafile.GetPageBounds(1));
58   DisableFontSmoothing disable_in_this_scope;
59
60   // Create a temporary HDC and bitmap to retrieve the rendered data.
61   base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL));
62   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);
73   unsigned char* bits = NULL;
74   base::win::ScopedBitmap bitmap(
75       ::CreateDIBSection(hdc.Get(), reinterpret_cast<BITMAPINFO*>(&hdr), 0,
76                          reinterpret_cast<void**>(&bits), NULL, 0));
77   DCHECK(bitmap.is_valid());
78   base::win::ScopedSelectObject select_object(hdc.Get(), bitmap.get());
79
80   skia::InitializeDC(hdc.Get());
81
82   bool success = metafile.Playback(hdc.Get(), NULL);
83
84   row_length_ = size_.width() * sizeof(uint32_t);
85   size_t bytes = row_length_ * size_.height();
86   DCHECK(bytes);
87
88   data_.assign(bits, bits + bytes);
89
90   return success;
91 }
92
93 }  // namespace printing