blob: d4734326302ae0b67097b0ac304403d136908497 [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]48091b02010-10-21 02:38:1119namespace printing {
20
thestig3109df12017-04-26 21:57:2521bool Image::LoadMetafile(const Metafile& metafile) {
Etienne Bergeron1a8383b82019-04-17 19:03:1822 gfx::Rect rect(metafile.GetPageBounds(1));
[email protected]48091b02010-10-21 02:38:1123
24 // Create a temporary HDC and bitmap to retrieve the rendered data.
[email protected]47d14742013-01-26 04:09:1025 base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL));
[email protected]48091b02010-10-21 02:38:1126 BITMAPV4HEADER hdr;
27 DCHECK_EQ(rect.x(), 0);
28 DCHECK_EQ(rect.y(), 0);
29 DCHECK_GE(rect.width(), 0); // Metafile could be empty.
30 DCHECK_GE(rect.height(), 0);
31
32 if (rect.width() < 1 || rect.height() < 1)
33 return false;
34
35 size_ = rect.size();
36 gfx::CreateBitmapV4Header(rect.width(), rect.height(), &hdr);
[email protected]47d14742013-01-26 04:09:1037 unsigned char* bits = NULL;
38 base::win::ScopedBitmap bitmap(
rvargas9b7c6b0f2014-09-25 19:22:4839 ::CreateDIBSection(hdc.Get(), reinterpret_cast<BITMAPINFO*>(&hdr), 0,
[email protected]47d14742013-01-26 04:09:1040 reinterpret_cast<void**>(&bits), NULL, 0));
anpol280746c2015-12-18 08:39:0341 DCHECK(bitmap.is_valid());
42 base::win::ScopedSelectObject select_object(hdc.Get(), bitmap.get());
[email protected]48091b02010-10-21 02:38:1143
rvargas9b7c6b0f2014-09-25 19:22:4844 skia::InitializeDC(hdc.Get());
[email protected]48091b02010-10-21 02:38:1145
rvargas9b7c6b0f2014-09-25 19:22:4846 bool success = metafile.Playback(hdc.Get(), NULL);
[email protected]48091b02010-10-21 02:38:1147
thestig707a24b22015-09-14 18:16:3348 row_length_ = size_.width() * sizeof(uint32_t);
[email protected]48091b02010-10-21 02:38:1149 size_t bytes = row_length_ * size_.height();
50 DCHECK(bytes);
51
[email protected]47d14742013-01-26 04:09:1052 data_.assign(bits, bits + bytes);
[email protected]48091b02010-10-21 02:38:1153
54 return success;
55}
56
57} // namespace printing