Bug 1829659 - Remove `import.py` because it's not used anymore r=glandium,bwc
[gecko.git] / widget / windows / WindowsEMF.cpp
blob71e3631beaefa6f14f98c34be3fc76af0db436e7
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"
8 namespace mozilla {
9 namespace widget {
11 WindowsEMF::WindowsEMF() : mEmf(nullptr), mDC(nullptr) {}
13 WindowsEMF::~WindowsEMF() { ReleaseAllResource(); }
15 bool WindowsEMF::InitForDrawing(const wchar_t* aMetafilePath /* = nullptr */) {
16 ReleaseAllResource();
18 mDC = ::CreateEnhMetaFile(nullptr, aMetafilePath, nullptr, nullptr);
19 return !!mDC;
22 bool WindowsEMF::InitFromFileContents(const wchar_t* aMetafilePath) {
23 MOZ_ASSERT(aMetafilePath);
24 ReleaseAllResource();
26 mEmf = ::GetEnhMetaFileW(aMetafilePath);
27 return !!mEmf;
30 bool WindowsEMF::InitFromFileContents(LPBYTE aBytes, UINT aSize) {
31 MOZ_ASSERT(aBytes && aSize != 0);
32 ReleaseAllResource();
34 mEmf = SetEnhMetaFileBits(aSize, aBytes);
36 return !!mEmf;
39 bool WindowsEMF::FinishDocument() {
40 if (mDC) {
41 mEmf = ::CloseEnhMetaFile(mDC);
42 mDC = nullptr;
44 return !!mEmf;
47 void WindowsEMF::ReleaseEMFHandle() {
48 if (mEmf) {
49 ::DeleteEnhMetaFile(mEmf);
50 mEmf = nullptr;
54 void WindowsEMF::ReleaseAllResource() {
55 FinishDocument();
56 ReleaseEMFHandle();
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.");
70 ReleaseEMFHandle();
71 return true;
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) {
87 return false;
90 return true;
93 } // namespace widget
94 } // namespace mozilla