1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "WindowsEMF.h"
11 WindowsEMF::WindowsEMF() : mEmf(nullptr), mDC(nullptr) {}
13 WindowsEMF::~WindowsEMF() { ReleaseAllResource(); }
15 bool WindowsEMF::InitForDrawing(const wchar_t* aMetafilePath
/* = nullptr */) {
18 mDC
= ::CreateEnhMetaFile(nullptr, aMetafilePath
, nullptr, nullptr);
22 bool WindowsEMF::InitFromFileContents(const wchar_t* aMetafilePath
) {
23 MOZ_ASSERT(aMetafilePath
);
26 mEmf
= ::GetEnhMetaFileW(aMetafilePath
);
30 bool WindowsEMF::InitFromFileContents(LPBYTE aBytes
, UINT aSize
) {
31 MOZ_ASSERT(aBytes
&& aSize
!= 0);
34 mEmf
= SetEnhMetaFileBits(aSize
, aBytes
);
39 bool WindowsEMF::FinishDocument() {
41 mEmf
= ::CloseEnhMetaFile(mDC
);
47 void WindowsEMF::ReleaseEMFHandle() {
49 ::DeleteEnhMetaFile(mEmf
);
54 void WindowsEMF::ReleaseAllResource() {
59 bool WindowsEMF::Playback(HDC aDeviceContext
, const RECT
& aRect
) {
60 DebugOnly
<bool> result
= FinishDocument();
61 MOZ_ASSERT(result
, "This function should be used after InitXXX.");
63 return ::PlayEnhMetaFile(aDeviceContext
, mEmf
, &aRect
) != 0;
66 bool WindowsEMF::SaveToFile() {
67 DebugOnly
<bool> result
= FinishDocument();
68 MOZ_ASSERT(result
, "This function should be used after InitXXX.");
74 UINT
WindowsEMF::GetEMFContentSize() {
75 DebugOnly
<bool> result
= FinishDocument();
76 MOZ_ASSERT(result
, "This function should be used after InitXXX.");
78 return GetEnhMetaFileBits(mEmf
, 0, NULL
);
81 bool WindowsEMF::GetEMFContentBits(LPBYTE aBytes
) {
82 DebugOnly
<bool> result
= FinishDocument();
83 MOZ_ASSERT(result
, "This function should be used after InitXXX.");
85 UINT emfSize
= GetEMFContentSize();
86 if (GetEnhMetaFileBits(mEmf
, emfSize
, aBytes
) != emfSize
) {
94 } // namespace mozilla