Prefer Optional<vector> to than unique_ptr<vector>
The use of the unique_ptr in this case is just to provide a no-value
case by means of nullptr. We can avoid the additional indirection
via Optional.
-- Return results by value while we're at it.
Change-Id: I2fae135aa72ae7b6a539effe8647ce1ef44c8346
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/78714
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/xfa/fgas/font/cfgas_fontmgr.cpp b/xfa/fgas/font/cfgas_fontmgr.cpp
index 93f5b32..9a45efc 100644
--- a/xfa/fgas/font/cfgas_fontmgr.cpp
+++ b/xfa/fgas/font/cfgas_fontmgr.cpp
@@ -637,17 +637,12 @@
const wchar_t* pszFontFamily,
uint32_t dwHash,
uint16_t wCodePage,
- uint16_t /* wBitField */) {
- std::vector<CFGAS_FontDescriptorInfo>* sortedFontInfos =
- m_Hash2CandidateList[dwHash].get();
- if (!sortedFontInfos) {
- auto pNewFonts = std::make_unique<std::vector<CFGAS_FontDescriptorInfo>>();
- sortedFontInfos = pNewFonts.get();
- MatchFonts(sortedFontInfos, wCodePage, dwFontStyles,
- WideString(pszFontFamily), wUnicode);
- m_Hash2CandidateList[dwHash] = std::move(pNewFonts);
+ uint16_t /* wBitField*/) {
+ if (!m_Hash2CandidateList[dwHash].has_value()) {
+ m_Hash2CandidateList[dwHash] =
+ MatchFonts(wCodePage, dwFontStyles, pszFontFamily, wUnicode);
}
- for (const auto& info : *sortedFontInfos) {
+ for (const auto& info : m_Hash2CandidateList[dwHash].value()) {
CFGAS_FontDescriptor* pDesc = info.pFont;
if (!VerifyUnicodeForFontDescriptor(pDesc, wUnicode))
continue;
@@ -679,23 +674,23 @@
return CFGAS_GEFont::LoadFont(std::move(pInternalFont));
}
-void CFGAS_FontMgr::MatchFonts(
- std::vector<CFGAS_FontDescriptorInfo>* pMatchedFonts,
+std::vector<CFGAS_FontDescriptorInfo> CFGAS_FontMgr::MatchFonts(
uint16_t wCodePage,
uint32_t dwFontStyles,
const WideString& FontName,
wchar_t wcUnicode) {
- pMatchedFonts->clear();
+ std::vector<CFGAS_FontDescriptorInfo> matched_fonts;
for (const auto& pFont : m_InstalledFonts) {
int32_t nPenalty =
CalcPenalty(pFont.get(), wCodePage, dwFontStyles, FontName, wcUnicode);
if (nPenalty >= 0xffff)
continue;
- pMatchedFonts->push_back({pFont.get(), nPenalty});
- if (pMatchedFonts->size() == 0xffff)
+ matched_fonts.push_back({pFont.get(), nPenalty});
+ if (matched_fonts.size() == 0xffff)
break;
}
- std::sort(pMatchedFonts->begin(), pMatchedFonts->end());
+ std::sort(matched_fonts.begin(), matched_fonts.end());
+ return matched_fonts;
}
void CFGAS_FontMgr::RegisterFace(RetainPtr<CFX_Face> pFace,
@@ -780,15 +775,12 @@
RetainPtr<CFGAS_GEFont> pFont =
CFGAS_GEFont::LoadFont(pFD->wsFontFace, dwFontStyles, wCodePage);
#else // defined(OS_WIN)
- std::vector<CFGAS_FontDescriptorInfo>* sortedFontInfos =
- m_Hash2CandidateList[dwHash].get();
- if (!sortedFontInfos) {
- auto pNewFonts = std::make_unique<std::vector<CFGAS_FontDescriptorInfo>>();
- sortedFontInfos = pNewFonts.get();
- MatchFonts(sortedFontInfos, wCodePage, dwFontStyles,
- WideString(pszFontFamily), 0);
- m_Hash2CandidateList[dwHash] = std::move(pNewFonts);
+ if (!m_Hash2CandidateList[dwHash].has_value()) {
+ m_Hash2CandidateList[dwHash] =
+ MatchFonts(wCodePage, dwFontStyles, WideString(pszFontFamily), 0);
}
+ std::vector<CFGAS_FontDescriptorInfo>* sortedFontInfos =
+ &m_Hash2CandidateList[dwHash].value();
if (sortedFontInfos->empty())
return nullptr;
diff --git a/xfa/fgas/font/cfgas_fontmgr.h b/xfa/fgas/font/cfgas_fontmgr.h
index 995ca95..4b5d59e 100644
--- a/xfa/fgas/font/cfgas_fontmgr.h
+++ b/xfa/fgas/font/cfgas_fontmgr.h
@@ -19,6 +19,7 @@
#include "core/fxcrt/retain_ptr.h"
#include "core/fxge/cfx_face.h"
#include "core/fxge/fx_freetype.h"
+#include "third_party/base/optional.h"
class CFGAS_GEFont;
class IFX_SeekableReadStream;
@@ -131,11 +132,10 @@
void RegisterFace(RetainPtr<CFX_Face> pFace, const WideString* pFaceName);
void RegisterFaces(const RetainPtr<IFX_SeekableReadStream>& pFontStream,
const WideString* pFaceName);
- void MatchFonts(std::vector<CFGAS_FontDescriptorInfo>* MatchedFonts,
- uint16_t wCodePage,
- uint32_t dwFontStyles,
- const WideString& FontName,
- wchar_t wcUnicode);
+ std::vector<CFGAS_FontDescriptorInfo> MatchFonts(uint16_t wCodePage,
+ uint32_t dwFontStyles,
+ const WideString& FontName,
+ wchar_t wcUnicode);
RetainPtr<CFGAS_GEFont> LoadFontInternal(const WideString& wsFaceName,
int32_t iFaceIndex);
#endif // defined(OS_WIN)
@@ -147,7 +147,7 @@
std::deque<FX_FONTDESCRIPTOR> m_FontFaces;
#else
std::vector<std::unique_ptr<CFGAS_FontDescriptor>> m_InstalledFonts;
- std::map<uint32_t, std::unique_ptr<std::vector<CFGAS_FontDescriptorInfo>>>
+ std::map<uint32_t, Optional<std::vector<CFGAS_FontDescriptorInfo>>>
m_Hash2CandidateList;
#endif // defined(OS_WIN)
};