From e647799f6a2f7f747c9f55d9f0ce08dcdfbd53f4 Mon Sep 17 00:00:00 2001 From: tsepez Date: Thu, 5 Jan 2017 12:57:00 -0800 Subject: Properly ref-count CFGAS_GEFont with CFX_RetainPtr. We worry about cyclical references, but no leaks found. Review-Url: https://codereview.chromium.org/2609423003 --- xfa/fxfa/app/xfa_ffwidgetacc.cpp | 11 +++--- xfa/fxfa/app/xfa_fontmgr.cpp | 83 +++++++++++++++++++--------------------- xfa/fxfa/app/xfa_fwltheme.cpp | 7 +--- xfa/fxfa/app/xfa_fwltheme.h | 5 ++- xfa/fxfa/app/xfa_textlayout.cpp | 6 ++- xfa/fxfa/app/xfa_textlayout.h | 6 +-- 6 files changed, 56 insertions(+), 62 deletions(-) (limited to 'xfa/fxfa/app') diff --git a/xfa/fxfa/app/xfa_ffwidgetacc.cpp b/xfa/fxfa/app/xfa_ffwidgetacc.cpp index 13145e6448..e62ebca302 100644 --- a/xfa/fxfa/app/xfa_ffwidgetacc.cpp +++ b/xfa/fxfa/app/xfa_ffwidgetacc.cpp @@ -1498,19 +1498,18 @@ CXFA_WidgetLayoutData* CXFA_WidgetAcc::GetWidgetLayoutData() { return m_pLayoutData.get(); } -CFGAS_GEFont* CXFA_WidgetAcc::GetFDEFont() { +CFX_RetainPtr CXFA_WidgetAcc::GetFDEFont() { CFX_WideStringC wsFontName = FX_WSTRC(L"Courier"); uint32_t dwFontStyle = 0; if (CXFA_Font font = GetFont()) { - if (font.IsBold()) { + if (font.IsBold()) dwFontStyle |= FX_FONTSTYLE_Bold; - } - if (font.IsItalic()) { + if (font.IsItalic()) dwFontStyle |= FX_FONTSTYLE_Italic; - } font.GetTypeface(wsFontName); } - CXFA_FFDoc* pDoc = GetDoc(); + + auto pDoc = GetDoc(); return pDoc->GetApp()->GetXFAFontMgr()->GetFont(pDoc, wsFontName, dwFontStyle); } diff --git a/xfa/fxfa/app/xfa_fontmgr.cpp b/xfa/fxfa/app/xfa_fontmgr.cpp index 636017d4e3..5db389663a 100644 --- a/xfa/fxfa/app/xfa_fontmgr.cpp +++ b/xfa/fxfa/app/xfa_fontmgr.cpp @@ -1740,18 +1740,16 @@ const XFA_FONTINFO* XFA_GetFontINFOByFontName( CXFA_DefFontMgr::CXFA_DefFontMgr() {} -CXFA_DefFontMgr::~CXFA_DefFontMgr() { - for (int32_t i = 0; i < m_CacheFonts.GetSize(); i++) - m_CacheFonts[i]->Release(); -} +CXFA_DefFontMgr::~CXFA_DefFontMgr() {} -CFGAS_GEFont* CXFA_DefFontMgr::GetFont(CXFA_FFDoc* hDoc, - const CFX_WideStringC& wsFontFamily, - uint32_t dwFontStyles, - uint16_t wCodePage) { +CFX_RetainPtr CXFA_DefFontMgr::GetFont( + CXFA_FFDoc* hDoc, + const CFX_WideStringC& wsFontFamily, + uint32_t dwFontStyles, + uint16_t wCodePage) { CFX_WideString wsFontName(wsFontFamily); CFGAS_FontMgr* pFDEFontMgr = hDoc->GetApp()->GetFDEFontMgr(); - CFGAS_GEFont* pFont = + CFX_RetainPtr pFont = pFDEFontMgr->LoadFont(wsFontName.c_str(), dwFontStyles, wCodePage); if (!pFont) { const XFA_FONTINFO* pCurFont = @@ -1785,56 +1783,50 @@ CFGAS_GEFont* CXFA_DefFontMgr::GetFont(CXFA_FFDoc* hDoc, } } if (pFont) - m_CacheFonts.Add(pFont); + m_CacheFonts.push_back(pFont); return pFont; } -CFGAS_GEFont* CXFA_DefFontMgr::GetDefaultFont( +CFX_RetainPtr CXFA_DefFontMgr::GetDefaultFont( CXFA_FFDoc* hDoc, const CFX_WideStringC& wsFontFamily, uint32_t dwFontStyles, uint16_t wCodePage) { CFGAS_FontMgr* pFDEFontMgr = hDoc->GetApp()->GetFDEFontMgr(); - CFGAS_GEFont* pFont = + CFX_RetainPtr pFont = pFDEFontMgr->LoadFont(L"Arial Narrow", dwFontStyles, wCodePage); if (!pFont) { pFont = pFDEFontMgr->LoadFont(static_cast(nullptr), dwFontStyles, wCodePage); } - - ASSERT(pFont); if (pFont) - m_CacheFonts.Add(pFont); + m_CacheFonts.push_back(pFont); return pFont; } CXFA_PDFFontMgr::CXFA_PDFFontMgr(CXFA_FFDoc* pDoc) : m_pDoc(pDoc) {} -CXFA_PDFFontMgr::~CXFA_PDFFontMgr() { - for (const auto& pair : m_FontMap) { - if (pair.second) - pair.second->Release(); - } -} +CXFA_PDFFontMgr::~CXFA_PDFFontMgr() {} -CFGAS_GEFont* CXFA_PDFFontMgr::FindFont(const CFX_ByteString& strPsName, - bool bBold, - bool bItalic, - CPDF_Font** pDstPDFFont, - bool bStrictMatch) { +CFX_RetainPtr CXFA_PDFFontMgr::FindFont( + const CFX_ByteString& strPsName, + bool bBold, + bool bItalic, + CPDF_Font** pDstPDFFont, + bool bStrictMatch) { CPDF_Document* pDoc = m_pDoc->GetPDFDoc(); - if (!pDoc) { + if (!pDoc) return nullptr; - } + CPDF_Dictionary* pFontSetDict = pDoc->GetRoot()->GetDictFor("AcroForm")->GetDictFor("DR"); - if (!pFontSetDict) { + if (!pFontSetDict) return nullptr; - } + pFontSetDict = pFontSetDict->GetDictFor("Font"); - if (!pFontSetDict) { + if (!pFontSetDict) return nullptr; - } + CFX_ByteString name = strPsName; name.Remove(' '); CFGAS_FontMgr* pFDEFontMgr = m_pDoc->GetApp()->GetFDEFontMgr(); @@ -1862,10 +1854,11 @@ CFGAS_GEFont* CXFA_PDFFontMgr::FindFont(const CFX_ByteString& strPsName, return nullptr; } -CFGAS_GEFont* CXFA_PDFFontMgr::GetFont(const CFX_WideStringC& wsFontFamily, - uint32_t dwFontStyles, - CPDF_Font** pPDFFont, - bool bStrictMatch) { +CFX_RetainPtr CXFA_PDFFontMgr::GetFont( + const CFX_WideStringC& wsFontFamily, + uint32_t dwFontStyles, + CPDF_Font** pPDFFont, + bool bStrictMatch) { uint32_t dwHashCode = FX_HashCode_GetW(wsFontFamily, false); CFX_ByteString strKey; strKey.Format("%u%u", dwHashCode, dwFontStyles); @@ -1877,7 +1870,7 @@ CFGAS_GEFont* CXFA_PDFFontMgr::GetFont(const CFX_WideStringC& wsFontFamily, bool bBold = (dwFontStyles & FX_FONTSTYLE_Bold) == FX_FONTSTYLE_Bold; bool bItalic = (dwFontStyles & FX_FONTSTYLE_Italic) == FX_FONTSTYLE_Italic; CFX_ByteString strFontName = PsNameToFontName(bsPsName, bBold, bItalic); - CFGAS_GEFont* pFont = + CFX_RetainPtr pFont = FindFont(strFontName, bBold, bItalic, pPDFFont, bStrictMatch); if (pFont) m_FontMap[strKey] = pFont; @@ -1975,7 +1968,7 @@ bool CXFA_PDFFontMgr::PsNameMatchDRFontName(const CFX_ByteStringC& bsPsName, return true; } -bool CXFA_PDFFontMgr::GetCharWidth(const CFGAS_GEFont* pFont, +bool CXFA_PDFFontMgr::GetCharWidth(const CFX_RetainPtr& pFont, FX_WCHAR wUnicode, bool bCharCode, int32_t* pWidth) { @@ -1991,7 +1984,8 @@ bool CXFA_PDFFontMgr::GetCharWidth(const CFGAS_GEFont* pFont, return true; } -void CXFA_PDFFontMgr::SetFont(const CFGAS_GEFont* pFont, CPDF_Font* pPDFFont) { +void CXFA_PDFFontMgr::SetFont(const CFX_RetainPtr& pFont, + CPDF_Font* pPDFFont) { m_FDE2PDFFont[pFont] = pPDFFont; } @@ -1999,10 +1993,11 @@ CXFA_FontMgr::CXFA_FontMgr() {} CXFA_FontMgr::~CXFA_FontMgr() {} -CFGAS_GEFont* CXFA_FontMgr::GetFont(CXFA_FFDoc* hDoc, - const CFX_WideStringC& wsFontFamily, - uint32_t dwFontStyles, - uint16_t wCodePage) { +CFX_RetainPtr CXFA_FontMgr::GetFont( + CXFA_FFDoc* hDoc, + const CFX_WideStringC& wsFontFamily, + uint32_t dwFontStyles, + uint16_t wCodePage) { uint32_t dwHash = FX_HashCode_GetW(wsFontFamily, false); CFX_ByteString bsKey; bsKey.Format("%u%u%u", dwHash, dwFontStyles, wCodePage); @@ -2015,7 +2010,7 @@ CFGAS_GEFont* CXFA_FontMgr::GetFont(CXFA_FFDoc* hDoc, CXFA_PDFFontMgr* pMgr = it != m_PDFFontMgrMap.end() ? it->second.get() : nullptr; CPDF_Font* pPDFFont = nullptr; - CFGAS_GEFont* pFont = nullptr; + CFX_RetainPtr pFont; if (pMgr) { pFont = pMgr->GetFont(wsEnglishName.AsStringC(), dwFontStyles, &pPDFFont, true); diff --git a/xfa/fxfa/app/xfa_fwltheme.cpp b/xfa/fxfa/app/xfa_fwltheme.cpp index d12678e51b..1de302e995 100644 --- a/xfa/fxfa/app/xfa_fwltheme.cpp +++ b/xfa/fxfa/app/xfa_fwltheme.cpp @@ -75,10 +75,6 @@ CXFA_FWLTheme::CXFA_FWLTheme(CXFA_FFApp* pApp) CXFA_FWLTheme::~CXFA_FWLTheme() { m_pTextOut.reset(); - if (m_pCalendarFont) { - m_pCalendarFont->Release(); - m_pCalendarFont = nullptr; - } FWLTHEME_Release(); } @@ -193,7 +189,8 @@ float CXFA_FWLTheme::GetFontSize(CFWL_ThemePart* pThemePart) const { return FWLTHEME_CAPACITY_FontSize; } -CFGAS_GEFont* CXFA_FWLTheme::GetFont(CFWL_ThemePart* pThemePart) const { +CFX_RetainPtr CXFA_FWLTheme::GetFont( + CFWL_ThemePart* pThemePart) const { if (CXFA_FFWidget* pWidget = XFA_ThemeGetOuterWidget(pThemePart->m_pWidget)) return pWidget->GetDataAcc()->GetFDEFont(); return GetTheme(pThemePart->m_pWidget)->GetFont(); diff --git a/xfa/fxfa/app/xfa_fwltheme.h b/xfa/fxfa/app/xfa_fwltheme.h index f8b804893a..a8efad0ccf 100644 --- a/xfa/fxfa/app/xfa_fwltheme.h +++ b/xfa/fxfa/app/xfa_fwltheme.h @@ -37,7 +37,8 @@ class CXFA_FWLTheme final : public IFWL_ThemeProvider { float GetCYBorderSize() const override; CFX_RectF GetUIMargin(CFWL_ThemePart* pThemePart) const override; float GetFontSize(CFWL_ThemePart* pThemePart) const override; - CFGAS_GEFont* GetFont(CFWL_ThemePart* pThemePart) const override; + CFX_RetainPtr GetFont( + CFWL_ThemePart* pThemePart) const override; float GetLineHeight(CFWL_ThemePart* pThemePart) const override; float GetScrollBarWidth() const override; FX_COLORREF GetTextColor(CFWL_ThemePart* pThemePart) const override; @@ -58,7 +59,7 @@ class CXFA_FWLTheme final : public IFWL_ThemeProvider { std::unique_ptr m_pCaretTP; std::unique_ptr m_pBarcodeTP; std::unique_ptr m_pTextOut; - CFGAS_GEFont* m_pCalendarFont; + CFX_RetainPtr m_pCalendarFont; CFX_WideString m_wsResource; CXFA_FFApp* const m_pApp; CFX_RectF m_Rect; diff --git a/xfa/fxfa/app/xfa_textlayout.cpp b/xfa/fxfa/app/xfa_textlayout.cpp index ea4ae4b2da..826fefcfbc 100644 --- a/xfa/fxfa/app/xfa_textlayout.cpp +++ b/xfa/fxfa/app/xfa_textlayout.cpp @@ -376,8 +376,10 @@ bool CXFA_TextParser::IsSpaceRun(IFDE_CSSComputedStyle* pStyle) const { } return false; } -CFGAS_GEFont* CXFA_TextParser::GetFont(CXFA_TextProvider* pTextProvider, - IFDE_CSSComputedStyle* pStyle) const { + +CFX_RetainPtr CXFA_TextParser::GetFont( + CXFA_TextProvider* pTextProvider, + IFDE_CSSComputedStyle* pStyle) const { CFX_WideStringC wsFamily = FX_WSTRC(L"Courier"); uint32_t dwStyle = 0; CXFA_Font font = pTextProvider->GetFontNode(); diff --git a/xfa/fxfa/app/xfa_textlayout.h b/xfa/fxfa/app/xfa_textlayout.h index f923c743ed..fa0ca668ca 100644 --- a/xfa/fxfa/app/xfa_textlayout.h +++ b/xfa/fxfa/app/xfa_textlayout.h @@ -92,8 +92,8 @@ class CXFA_TextParser { bool IsSpaceRun(IFDE_CSSComputedStyle* pStyle) const; bool GetTabstops(IFDE_CSSComputedStyle* pStyle, CXFA_TextTabstopsContext* pTabstopContext); - CFGAS_GEFont* GetFont(CXFA_TextProvider* pTextProvider, - IFDE_CSSComputedStyle* pStyle) const; + CFX_RetainPtr GetFont(CXFA_TextProvider* pTextProvider, + IFDE_CSSComputedStyle* pStyle) const; FX_FLOAT GetFontSize(CXFA_TextProvider* pTextProvider, IFDE_CSSComputedStyle* pStyle) const; int32_t GetHorScale(CXFA_TextProvider* pTextProvider, @@ -210,7 +210,7 @@ class XFA_TextPiece : public CFX_Target { int32_t iUnderline; int32_t iPeriod; int32_t iLineThrough; - CFGAS_GEFont* pFont; + CFX_RetainPtr pFont; FX_ARGB dwColor; FX_FLOAT fFontSize; CFX_RectF rtPiece; -- cgit v1.2.3