1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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/. */
5
6 #include "WindowsEMF.h"
7
8 namespace mozilla {
9 namespace widget {
10
WindowsEMF()11 WindowsEMF::WindowsEMF() : mEmf(nullptr), mDC(nullptr) {}
12
~WindowsEMF()13 WindowsEMF::~WindowsEMF() { ReleaseAllResource(); }
14
InitForDrawing(const wchar_t * aMetafilePath)15 bool WindowsEMF::InitForDrawing(const wchar_t* aMetafilePath /* = nullptr */) {
16 ReleaseAllResource();
17
18 mDC = ::CreateEnhMetaFile(nullptr, aMetafilePath, nullptr, nullptr);
19 return !!mDC;
20 }
21
InitFromFileContents(const wchar_t * aMetafilePath)22 bool WindowsEMF::InitFromFileContents(const wchar_t* aMetafilePath) {
23 MOZ_ASSERT(aMetafilePath);
24 ReleaseAllResource();
25
26 mEmf = ::GetEnhMetaFileW(aMetafilePath);
27 return !!mEmf;
28 }
29
InitFromFileContents(LPBYTE aBytes,UINT aSize)30 bool WindowsEMF::InitFromFileContents(LPBYTE aBytes, UINT aSize) {
31 MOZ_ASSERT(aBytes && aSize != 0);
32 ReleaseAllResource();
33
34 mEmf = SetEnhMetaFileBits(aSize, aBytes);
35
36 return !!mEmf;
37 }
38
FinishDocument()39 bool WindowsEMF::FinishDocument() {
40 if (mDC) {
41 mEmf = ::CloseEnhMetaFile(mDC);
42 mDC = nullptr;
43 }
44 return !!mEmf;
45 }
46
ReleaseEMFHandle()47 void WindowsEMF::ReleaseEMFHandle() {
48 if (mEmf) {
49 ::DeleteEnhMetaFile(mEmf);
50 mEmf = nullptr;
51 }
52 }
53
ReleaseAllResource()54 void WindowsEMF::ReleaseAllResource() {
55 FinishDocument();
56 ReleaseEMFHandle();
57 }
58
Playback(HDC aDeviceContext,const RECT & aRect)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.");
62
63 return ::PlayEnhMetaFile(aDeviceContext, mEmf, &aRect) != 0;
64 }
65
SaveToFile()66 bool WindowsEMF::SaveToFile() {
67 DebugOnly<bool> result = FinishDocument();
68 MOZ_ASSERT(result, "This function should be used after InitXXX.");
69
70 ReleaseEMFHandle();
71 return true;
72 }
73
GetEMFContentSize()74 UINT WindowsEMF::GetEMFContentSize() {
75 DebugOnly<bool> result = FinishDocument();
76 MOZ_ASSERT(result, "This function should be used after InitXXX.");
77
78 return GetEnhMetaFileBits(mEmf, 0, NULL);
79 }
80
GetEMFContentBits(LPBYTE aBytes)81 bool WindowsEMF::GetEMFContentBits(LPBYTE aBytes) {
82 DebugOnly<bool> result = FinishDocument();
83 MOZ_ASSERT(result, "This function should be used after InitXXX.");
84
85 UINT emfSize = GetEMFContentSize();
86 if (GetEnhMetaFileBits(mEmf, emfSize, aBytes) != emfSize) {
87 return false;
88 }
89
90 return true;
91 }
92
93 } // namespace widget
94 } // namespace mozilla