Use fxcrt::MaybeOwned in CFGAS_GEFont.
Change-Id: Ie1f4763e37832475a60c55b26263405e0bbf09b3
Reviewed-on: https://pdfium-review.googlesource.com/42510
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/xfa/fgas/font/cfgas_gefont.cpp b/xfa/fgas/font/cfgas_gefont.cpp
index 1c3d88b..6f33eb5 100644
--- a/xfa/fgas/font/cfgas_gefont.cpp
+++ b/xfa/fgas/font/cfgas_gefont.cpp
@@ -55,11 +55,7 @@
CFGAS_GEFont::CFGAS_GEFont(CFGAS_FontMgr* pFontMgr) : m_pFontMgr(pFontMgr) {}
-CFGAS_GEFont::~CFGAS_GEFont() {
- m_pFontEncoding.reset(); // Has an UnownedPtr to |m_pFont|.
- if (!m_bExternalFont)
- delete m_pFont;
-}
+CFGAS_GEFont::~CFGAS_GEFont() = default;
#if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
bool CFGAS_GEFont::LoadFontInternal(const wchar_t* pszFontFamily,
@@ -73,7 +69,7 @@
int32_t iWeight =
FontStyleIsBold(dwFontStyles) ? FXFONT_FW_BOLD : FXFONT_FW_NORMAL;
- m_pFont = new CFX_Font;
+ m_pFont = pdfium::MakeUnique<CFX_Font>();
if (FontStyleIsItalic(dwFontStyles) && FontStyleIsBold(dwFontStyles))
csFontFamily += ",BoldItalic";
else if (FontStyleIsBold(dwFontStyles))
@@ -83,9 +79,7 @@
m_pFont->LoadSubst(csFontFamily, true, dwFontStyles, iWeight, 0, wCodePage,
false);
- if (!m_pFont->GetFace())
- return false;
- return InitFont();
+ return m_pFont->GetFace() && InitFont();
}
#endif // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
@@ -94,7 +88,6 @@
return false;
m_pFont = pExternalFont;
- m_bExternalFont = true;
return InitFont();
}
@@ -102,8 +95,7 @@
if (m_pFont || !pInternalFont)
return false;
- m_pFont = pInternalFont.release();
- m_bExternalFont = false;
+ m_pFont = std::move(pInternalFont);
return InitFont();
}
@@ -114,7 +106,7 @@
if (m_pFontEncoding)
return true;
- m_pFontEncoding = FX_CreateFontEncodingEx(m_pFont, FXFM_ENCODING_NONE);
+ m_pFontEncoding = FX_CreateFontEncodingEx(m_pFont.Get(), FXFM_ENCODING_NONE);
return !!m_pFontEncoding;
}
diff --git a/xfa/fgas/font/cfgas_gefont.h b/xfa/fgas/font/cfgas_gefont.h
index 407a232..08164f7 100644
--- a/xfa/fgas/font/cfgas_gefont.h
+++ b/xfa/fgas/font/cfgas_gefont.h
@@ -13,8 +13,8 @@
#include <vector>
#include "core/fxcrt/fx_memory.h"
+#include "core/fxcrt/maybe_owned.h"
#include "core/fxcrt/retain_ptr.h"
-#include "core/fxcrt/unowned_ptr.h"
#include "xfa/fgas/font/cfgas_fontmgr.h"
#include "xfa/fgas/font/cfgas_pdffontmgr.h"
@@ -45,7 +45,7 @@
bool GetBBox(FX_RECT* bbox);
RetainPtr<CFGAS_GEFont> GetSubstFont(int32_t iGlyphIndex);
- CFX_Font* GetDevFont() const { return m_pFont; }
+ CFX_Font* GetDevFont() const { return m_pFont.Get(); }
void SetLogicalFontStyle(uint32_t dwLogFontStyle) {
m_bUseLogFontStyle = true;
@@ -71,9 +71,8 @@
WideString GetFamilyName() const;
bool m_bUseLogFontStyle = false;
- bool m_bExternalFont = false;
uint32_t m_dwLogFontStyle = 0;
- CFX_Font* m_pFont = nullptr;
+ MaybeOwned<CFX_Font> m_pFont; // Must come before |m_pFontEncoding|.
CFGAS_FontMgr::ObservedPtr const m_pFontMgr;
std::unique_ptr<CFX_UnicodeEncodingEx> m_pFontEncoding;
std::map<wchar_t, int32_t> m_CharWidthMap;