From 95d525e83f1f5b5fac15970f767410b21debbb81 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Thu, 18 Oct 2018 16:57:17 +0000 Subject: Shuffle platform-specifc code in CFGAS_FontMgr::GetFontByUnicode(). Move most of it into GetFontByUnicodeImpl(). Change-Id: Ibbfe458589d0d1aea8f52179110690ed83e40fac Reviewed-on: https://pdfium-review.googlesource.com/c/44250 Reviewed-by: Tom Sepez Commit-Queue: Lei Zhang --- xfa/fgas/font/cfgas_fontmgr.cpp | 116 +++++++++++++++++++++++----------------- xfa/fgas/font/cfgas_fontmgr.h | 7 +++ 2 files changed, 74 insertions(+), 49 deletions(-) diff --git a/xfa/fgas/font/cfgas_fontmgr.cpp b/xfa/fgas/font/cfgas_fontmgr.cpp index 8a51941627..8881be74f6 100644 --- a/xfa/fgas/font/cfgas_fontmgr.cpp +++ b/xfa/fgas/font/cfgas_fontmgr.cpp @@ -158,6 +158,34 @@ bool CFGAS_FontMgr::EnumFonts() { return true; } +RetainPtr CFGAS_FontMgr::GetFontByUnicodeImpl( + wchar_t wUnicode, + uint32_t dwFontStyles, + const wchar_t* pszFontFamily, + uint32_t dwHash, + uint16_t wCodePage, + uint16_t wBitField) { + const FX_FONTDESCRIPTOR* pFD = FindFont(pszFontFamily, dwFontStyles, false, + wCodePage, wBitField, wUnicode); + if (!pFD && pszFontFamily) { + pFD = + FindFont(nullptr, dwFontStyles, false, wCodePage, wBitField, wUnicode); + } + if (!pFD) + return nullptr; + + uint16_t newCodePage = FX_GetCodePageFromCharset(pFD->uCharSet); + const wchar_t* pFontFace = pFD->wsFontFace; + RetainPtr pFont = + CFGAS_GEFont::LoadFont(pFontFace, dwFontStyles, newCodePage, this); + if (!pFont) + return nullptr; + + pFont->SetLogicalFontStyle(dwFontStyles); + m_Hash2Fonts[dwHash].push_back(pFont); + return pFont; +} + const FX_FONTDESCRIPTOR* CFGAS_FontMgr::FindFont(const wchar_t* pszFontFamily, uint32_t dwFontStyles, bool matchParagraphStyle, @@ -619,6 +647,39 @@ bool CFGAS_FontMgr::EnumFonts() { return EnumFontsFromFontMapper() || EnumFontsFromFiles(); } +RetainPtr CFGAS_FontMgr::GetFontByUnicodeImpl( + wchar_t wUnicode, + uint32_t dwFontStyles, + const wchar_t* pszFontFamily, + uint32_t dwHash, + uint16_t wCodePage, + uint16_t /* wBitField */) { + std::vector* sortedFontInfos = + m_Hash2CandidateList[dwHash].get(); + if (!sortedFontInfos) { + auto pNewFonts = pdfium::MakeUnique>(); + sortedFontInfos = pNewFonts.get(); + MatchFonts(sortedFontInfos, wCodePage, dwFontStyles, + WideString(pszFontFamily), wUnicode); + m_Hash2CandidateList[dwHash] = std::move(pNewFonts); + } + for (const auto& info : *sortedFontInfos) { + CFX_FontDescriptor* pDesc = info.pFont; + if (!VerifyUnicodeForFontDescriptor(pDesc, wUnicode)) + continue; + RetainPtr pFont = + LoadFontInternal(pDesc->m_wsFaceName, pDesc->m_nFaceIndex); + if (!pFont) + continue; + pFont->SetLogicalFontStyle(dwFontStyles); + m_Hash2Fonts[dwHash].push_back(pFont); + return pFont; + } + if (!pszFontFamily) + m_FailedUnicodesSet.insert(wUnicode); + return nullptr; +} + RetainPtr CFGAS_FontMgr::LoadFontInternal( const WideString& wsFaceName, int32_t iFaceIndex) { @@ -813,57 +874,14 @@ RetainPtr CFGAS_FontMgr::GetFontByUnicode( } bsHash += FX_UTF8Encode(WideStringView(pszFontFamily)); uint32_t dwHash = FX_HashCode_GetA(bsHash.AsStringView(), false); - std::vector>* pFonts = &m_Hash2Fonts[dwHash]; - for (size_t i = 0; i < pFonts->size(); ++i) { - if (VerifyUnicode((*pFonts)[i], wUnicode)) - return (*pFonts)[i]; - } -#if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ - const FX_FONTDESCRIPTOR* pFD = FindFont(pszFontFamily, dwFontStyles, false, - wCodePage, wBitField, wUnicode); - if (!pFD && pszFontFamily) { - pFD = - FindFont(nullptr, dwFontStyles, false, wCodePage, wBitField, wUnicode); + std::vector>& fonts = m_Hash2Fonts[dwHash]; + for (auto& pFont : fonts) { + if (VerifyUnicode(pFont, wUnicode)) + return pFont; } - if (!pFD) - return nullptr; - - uint16_t newCodePage = FX_GetCodePageFromCharset(pFD->uCharSet); - const wchar_t* pFontFace = pFD->wsFontFace; - RetainPtr pFont = - CFGAS_GEFont::LoadFont(pFontFace, dwFontStyles, newCodePage, this); - if (!pFont) - return nullptr; - pFont->SetLogicalFontStyle(dwFontStyles); - pFonts->push_back(pFont); - return pFont; -#else // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ - std::vector* sortedFontInfos = - m_Hash2CandidateList[dwHash].get(); - if (!sortedFontInfos) { - auto pNewFonts = pdfium::MakeUnique>(); - sortedFontInfos = pNewFonts.get(); - MatchFonts(sortedFontInfos, wCodePage, dwFontStyles, - WideString(pszFontFamily), wUnicode); - m_Hash2CandidateList[dwHash] = std::move(pNewFonts); - } - for (const auto& info : *sortedFontInfos) { - CFX_FontDescriptor* pDesc = info.pFont; - if (!VerifyUnicodeForFontDescriptor(pDesc, wUnicode)) - continue; - RetainPtr pFont = - LoadFontInternal(pDesc->m_wsFaceName, pDesc->m_nFaceIndex); - if (!pFont) - continue; - pFont->SetLogicalFontStyle(dwFontStyles); - pFonts->push_back(pFont); - return pFont; - } - if (!pszFontFamily) - m_FailedUnicodesSet.insert(wUnicode); - return nullptr; -#endif // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ + return GetFontByUnicodeImpl(wUnicode, dwFontStyles, pszFontFamily, dwHash, + wCodePage, wBitField); } RetainPtr CFGAS_FontMgr::LoadFont(const wchar_t* pszFontFamily, diff --git a/xfa/fgas/font/cfgas_fontmgr.h b/xfa/fgas/font/cfgas_fontmgr.h index 2bda20ceb1..b784bedfe8 100644 --- a/xfa/fgas/font/cfgas_fontmgr.h +++ b/xfa/fgas/font/cfgas_fontmgr.h @@ -114,6 +114,13 @@ class CFGAS_FontMgr final : public Observable { bool EnumFonts(); private: + RetainPtr GetFontByUnicodeImpl(wchar_t wUnicode, + uint32_t dwFontStyles, + const wchar_t* pszFontFamily, + uint32_t dwHash, + uint16_t wCodePage, + uint16_t wBitField); + #if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ const FX_FONTDESCRIPTOR* FindFont(const wchar_t* pszFontFamily, uint32_t dwFontStyles, -- cgit v1.2.3