From 5037839bb0b91792b81a3e455dead1314fcc7a6d Mon Sep 17 00:00:00 2001 From: tsepez Date: Mon, 9 Jan 2017 06:54:27 -0800 Subject: Tidy cfgas_fontmgr, remove custom sorting code. Review-Url: https://codereview.chromium.org/2610813010 --- core/fxcrt/fx_ext.h | 23 ------------ xfa/fgas/font/cfgas_fontmgr.cpp | 83 +++++++++++++++++------------------------ xfa/fgas/font/cfgas_fontmgr.h | 28 ++++++-------- 3 files changed, 47 insertions(+), 87 deletions(-) diff --git a/core/fxcrt/fx_ext.h b/core/fxcrt/fx_ext.h index 737ea849c9..20580760c6 100644 --- a/core/fxcrt/fx_ext.h +++ b/core/fxcrt/fx_ext.h @@ -99,27 +99,4 @@ void FX_GUID_ToString(FX_LPCGUID pGUID, bool bSeparator = true); #endif // PDF_ENABLE_XFA -template -class CFX_SSortTemplate { - public: - void ShellSort(baseType* pArray, int32_t iCount) { - ASSERT(pArray && iCount > 0); - int32_t i, j, gap; - baseType v1, v2; - gap = iCount >> 1; - while (gap > 0) { - for (i = gap; i < iCount; i++) { - j = i - gap; - v1 = pArray[i]; - while (j > -1 && (v2 = pArray[j]) > v1) { - pArray[j + gap] = v2; - j -= gap; - } - pArray[j + gap] = v1; - } - gap >>= 1; - } - } -}; - #endif // CORE_FXCRT_FX_EXT_H_ diff --git a/xfa/fgas/font/cfgas_fontmgr.cpp b/xfa/fgas/font/cfgas_fontmgr.cpp index 9d3954fb1e..72f7b88f95 100644 --- a/xfa/fgas/font/cfgas_fontmgr.cpp +++ b/xfa/fgas/font/cfgas_fontmgr.cpp @@ -6,6 +6,7 @@ #include "xfa/fgas/font/cfgas_fontmgr.h" +#include #include #include @@ -572,10 +573,7 @@ std::unique_ptr CFGAS_FontMgr::Create( CFGAS_FontMgr::CFGAS_FontMgr(CFX_FontSourceEnum_File* pFontEnum) : m_pFontSource(pFontEnum) {} -CFGAS_FontMgr::~CFGAS_FontMgr() { - for (int32_t i = 0; i < m_InstalledFonts.GetSize(); i++) - delete m_InstalledFonts[i]; -} +CFGAS_FontMgr::~CFGAS_FontMgr() {} bool CFGAS_FontMgr::EnumFontsFromFontMapper() { CFX_FontMapper* pFontMapper = @@ -598,7 +596,7 @@ bool CFGAS_FontMgr::EnumFontsFromFontMapper() { CFX_WideString::FromLocal(pFontMapper->GetFaceName(i).c_str()); RegisterFaces(pFontStream, &wsFaceName); } - return m_InstalledFonts.GetSize() != 0; + return !m_InstalledFonts.empty(); } bool CFGAS_FontMgr::EnumFontsFromFiles() { @@ -611,7 +609,7 @@ bool CFGAS_FontMgr::EnumFontsFromFiles() { if (pFontStream) RegisterFaces(pFontStream, nullptr); } - return m_InstalledFonts.GetSize() != 0; + return !m_InstalledFonts.empty(); } bool CFGAS_FontMgr::EnumFonts() { @@ -637,18 +635,19 @@ CFX_RetainPtr CFGAS_FontMgr::GetFontByCodePage( if (!pFontArray->empty()) return (*pFontArray)[0]; - CFX_FontDescriptorInfos* sortedFonts = m_Hash2CandidateList[dwHash].get(); - if (!sortedFonts) { - auto pNewFonts = pdfium::MakeUnique(); - sortedFonts = pNewFonts.get(); - MatchFonts(*sortedFonts, wCodePage, dwFontStyles, + std::vector* sortedFontInfos = + m_Hash2CandidateList[dwHash].get(); + if (!sortedFontInfos) { + auto pNewFonts = pdfium::MakeUnique>(); + sortedFontInfos = pNewFonts.get(); + MatchFonts(sortedFontInfos, wCodePage, dwFontStyles, CFX_WideString(pszFontFamily), 0); m_Hash2CandidateList[dwHash] = std::move(pNewFonts); } - if (sortedFonts->GetSize() == 0) + if (sortedFontInfos->empty()) return nullptr; - CFX_FontDescriptor* pDesc = sortedFonts->GetAt(0).pFont; + CFX_FontDescriptor* pDesc = (*sortedFontInfos)[0].pFont; CFX_RetainPtr pFont = LoadFont(pDesc->m_wsFaceName, pDesc->m_nFaceIndex, nullptr); if (!pFont) @@ -681,16 +680,17 @@ CFX_RetainPtr CFGAS_FontMgr::GetFontByUnicode( if (VerifyUnicode((*pFonts)[i], wUnicode)) return (*pFonts)[i]; } - CFX_FontDescriptorInfos* sortedFonts = m_Hash2CandidateList[dwHash].get(); - if (!sortedFonts) { - auto pNewFonts = pdfium::MakeUnique(); - sortedFonts = pNewFonts.get(); - MatchFonts(*sortedFonts, wCodePage, dwFontStyles, + std::vector* sortedFontInfos = + m_Hash2CandidateList[dwHash].get(); + if (!sortedFontInfos) { + auto pNewFonts = pdfium::MakeUnique>(); + sortedFontInfos = pNewFonts.get(); + MatchFonts(sortedFontInfos, wCodePage, dwFontStyles, CFX_WideString(pszFontFamily), wUnicode); m_Hash2CandidateList[dwHash] = std::move(pNewFonts); } - for (int32_t i = 0; i < sortedFonts->GetSize(); ++i) { - CFX_FontDescriptor* pDesc = sortedFonts->GetAt(i).pFont; + for (const auto& info : *sortedFontInfos) { + CFX_FontDescriptor* pDesc = info.pFont; if (!VerifyUnicode(pDesc, wUnicode)) continue; CFX_RetainPtr pFont = @@ -874,36 +874,23 @@ CFX_RetainPtr CFGAS_FontMgr::CreateFontStream( return nullptr; } -int32_t CFGAS_FontMgr::MatchFonts(CFX_FontDescriptorInfos& MatchedFonts, - uint16_t wCodePage, - uint32_t dwFontStyles, - const CFX_WideString& FontName, - FX_WCHAR wcUnicode) { - MatchedFonts.RemoveAll(); - CFX_WideString wsNormalizedFontName = FontName; - - CFX_FontDescriptor* pFont = nullptr; - int32_t nCount = m_InstalledFonts.GetSize(); - for (int32_t i = 0; i < nCount; i++) { - pFont = m_InstalledFonts[i]; - int32_t nPenalty = CalcPenalty(pFont, wCodePage, dwFontStyles, - wsNormalizedFontName, wcUnicode); +void CFGAS_FontMgr::MatchFonts( + std::vector* pMatchedFonts, + uint16_t wCodePage, + uint32_t dwFontStyles, + const CFX_WideString& FontName, + FX_WCHAR wcUnicode) { + pMatchedFonts->clear(); + for (const auto& pFont : m_InstalledFonts) { + int32_t nPenalty = + CalcPenalty(pFont.get(), wCodePage, dwFontStyles, FontName, wcUnicode); if (nPenalty >= 0xffff) continue; - - FX_FontDescriptorInfo FontInfo; - FontInfo.pFont = pFont; - FontInfo.nPenalty = nPenalty; - MatchedFonts.Add(FontInfo); - if (MatchedFonts.GetSize() == 0xffff) + pMatchedFonts->push_back({pFont.get(), nPenalty}); + if (pMatchedFonts->size() == 0xffff) break; } - if (MatchedFonts.GetSize() == 0) - return 0; - - CFX_SSortTemplate ssort; - ssort.ShellSort(MatchedFonts.GetData(), MatchedFonts.GetSize()); - return MatchedFonts.GetSize(); + std::sort(pMatchedFonts->begin(), pMatchedFonts->end()); } int32_t CFGAS_FontMgr::CalcPenalty(CFX_FontDescriptor* pInstalled, @@ -1003,7 +990,7 @@ void CFGAS_FontMgr::RegisterFace(FXFT_Face pFace, if ((pFace->face_flags & FT_FACE_FLAG_SCALABLE) == 0) return; - std::unique_ptr pFont(new CFX_FontDescriptor); + auto pFont = pdfium::MakeUnique(); pFont->m_dwFontStyles |= FXFT_Is_Face_Bold(pFace) ? FX_FONTSTYLE_Bold : 0; pFont->m_dwFontStyles |= FXFT_Is_Face_Italic(pFace) ? FX_FONTSTYLE_Italic : 0; pFont->m_dwFontStyles |= GetFlags(pFace); @@ -1029,7 +1016,7 @@ void CFGAS_FontMgr::RegisterFace(FXFT_Face pFace, pFaceName ? *pFaceName : CFX_WideString::FromLocal(FXFT_Get_Postscript_Name(pFace)); pFont->m_nFaceIndex = pFace->face_index; - m_InstalledFonts.Add(pFont.release()); + m_InstalledFonts.push_back(std::move(pFont)); } void CFGAS_FontMgr::RegisterFaces( diff --git a/xfa/fgas/font/cfgas_fontmgr.h b/xfa/fgas/font/cfgas_fontmgr.h index 20efa3296b..303d73404b 100644 --- a/xfa/fgas/font/cfgas_fontmgr.h +++ b/xfa/fgas/font/cfgas_fontmgr.h @@ -146,26 +146,22 @@ class CFX_FontDescriptor { uint32_t m_dwCsb[2]; }; -typedef CFX_ArrayTemplate CFX_FontDescriptors; - -struct FX_FontDescriptorInfo { +class CFX_FontDescriptorInfo { public: CFX_FontDescriptor* pFont; int32_t nPenalty; - bool operator>(const FX_FontDescriptorInfo& other) const { + bool operator>(const CFX_FontDescriptorInfo& other) const { return nPenalty > other.nPenalty; } - bool operator<(const FX_FontDescriptorInfo& other) const { + bool operator<(const CFX_FontDescriptorInfo& other) const { return nPenalty < other.nPenalty; } - bool operator==(const FX_FontDescriptorInfo& other) const { + bool operator==(const CFX_FontDescriptorInfo& other) const { return nPenalty == other.nPenalty; } }; -typedef CFX_ArrayTemplate CFX_FontDescriptorInfos; - struct FX_HandleParentPath { FX_HandleParentPath() {} FX_HandleParentPath(const FX_HandleParentPath& x) { @@ -226,11 +222,11 @@ class CFGAS_FontMgr { bool VerifyUnicode(const CFX_RetainPtr& pFont, FX_WCHAR wcUnicode); int32_t IsPartName(const CFX_WideString& Name1, const CFX_WideString& Name2); - int32_t MatchFonts(CFX_FontDescriptorInfos& MatchedFonts, - uint16_t wCodePage, - uint32_t dwFontStyles, - const CFX_WideString& FontName, - FX_WCHAR wcUnicode = 0xFFFE); + void MatchFonts(std::vector* MatchedFonts, + uint16_t wCodePage, + uint32_t dwFontStyles, + const CFX_WideString& FontName, + FX_WCHAR wcUnicode = 0xFFFE); int32_t CalcPenalty(CFX_FontDescriptor* pInstalled, uint16_t wCodePage, uint32_t dwFontStyles, @@ -248,14 +244,14 @@ class CFGAS_FontMgr { CFX_RetainPtr CreateFontStream( const CFX_ByteString& bsFaceName); - CFX_FontDescriptors m_InstalledFonts; - std::map> + CFX_FontSourceEnum_File* const m_pFontSource; + std::vector> m_InstalledFonts; + std::map>> m_Hash2CandidateList; std::map>> m_Hash2Fonts; std::map, CFX_RetainPtr> m_IFXFont2FileRead; std::set m_FailedUnicodesSet; - CFX_FontSourceEnum_File* const m_pFontSource; }; #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ -- cgit v1.2.3