Bug 1401069 - Part 2. Implement ReleaseAllResource to make WindowsEMF::InitForDrawing reusable.
Currently, WindowsEMF::InitForDrawing can be used once. With the change in this
patch, we can call WindowsEMF::InitForDrawing and Playback/SaveToFile in pair
as many times as we want.
MozReview-Commit-ID: 4fbY4Q6i9v5
--- a/widget/windows/WindowsEMF.cpp
+++ b/widget/windows/WindowsEMF.cpp
@@ -12,36 +12,33 @@ namespace widget {
WindowsEMF::WindowsEMF()
: mDC(nullptr)
, mEmf(nullptr)
{
}
WindowsEMF::~WindowsEMF()
{
- FinishDocument();
- ReleaseEMFHandle();
+ ReleaseAllResource();
}
bool
WindowsEMF::InitForDrawing(const wchar_t* aMetafilePath /* = nullptr */)
{
- MOZ_ASSERT(!mDC && !mEmf, "InitForDrawing and InitFromFileContents is"
- " designed to be used either one at once.");
+ ReleaseAllResource();
mDC = ::CreateEnhMetaFile(nullptr, aMetafilePath, nullptr, nullptr);
return !!mDC;
}
bool
WindowsEMF::InitFromFileContents(const wchar_t* aMetafilePath)
{
MOZ_ASSERT(aMetafilePath);
- MOZ_ASSERT(!mDC && !mEmf, "InitForDrawing and InitFromFileContents is"
- " designed to be used either one at once.");
+ ReleaseAllResource();
mEmf = ::GetEnhMetaFileW(aMetafilePath);
return !!mEmf;
}
bool
WindowsEMF::FinishDocument()
{
@@ -56,16 +53,23 @@ void
WindowsEMF::ReleaseEMFHandle()
{
if (mEmf) {
::DeleteEnhMetaFile(mEmf);
mEmf = nullptr;
}
}
+void
+WindowsEMF::ReleaseAllResource()
+{
+ FinishDocument();
+ ReleaseEMFHandle();
+}
+
bool
WindowsEMF::Playback(HDC aDeviceContext, const RECT* aRect)
{
MOZ_ASSERT(aRect);
if (!FinishDocument()) {
return false;
}
--- a/widget/windows/WindowsEMF.h
+++ b/widget/windows/WindowsEMF.h
@@ -64,16 +64,17 @@ public:
*/
bool SaveToFile();
private:
WindowsEMF(const WindowsEMF& aEMF) = delete;
bool FinishDocument();
void ReleaseEMFHandle();
+ void ReleaseAllResource();
/* Compiled EMF data handle. */
HENHMETAFILE mEmf;
HDC mDC;
};
} // namespace widget
} // namespace mozilla