Encapsulate CFX_FontMapper::m_InstalledTTFonts and m_LocalizedFonts
Change-Id: Ie8935e1bebc08904272825358e902a519e0b1f41
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/79872
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfdoc/cpdf_bafontmap.cpp b/core/fpdfdoc/cpdf_bafontmap.cpp
index 4ef2c11..e6d08b1 100644
--- a/core/fpdfdoc/cpdf_bafontmap.cpp
+++ b/core/fpdfdoc/cpdf_bafontmap.cpp
@@ -36,16 +36,8 @@
CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr();
CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper();
pFontMapper->LoadInstalledFonts();
-
- for (const auto& font : pFontMapper->m_InstalledTTFonts) {
- if (font == sFontFaceName)
- return true;
- }
- for (const auto& fontPair : pFontMapper->m_LocalizedTTFonts) {
- if (fontPair.first == sFontFaceName)
- return true;
- }
- return false;
+ return pFontMapper->HasInstalledFont(sFontFaceName) ||
+ pFontMapper->HasLocalizedFont(sFontFaceName);
}
RetainPtr<CPDF_Font> AddNativeTrueTypeFontToPDF(CPDF_Document* pDoc,
diff --git a/core/fxge/cfx_fontmapper.cpp b/core/fxge/cfx_fontmapper.cpp
index 1a09bf8..e605c55 100644
--- a/core/fxge/cfx_fontmapper.cpp
+++ b/core/fxge/cfx_fontmapper.cpp
@@ -687,6 +687,42 @@
return pdfium::CollectionSize<int>(m_FaceArray);
}
+bool CFX_FontMapper::HasInstalledFont(ByteStringView name) const {
+ for (const auto& font : m_InstalledTTFonts) {
+ if (font == name)
+ return true;
+ }
+ return false;
+}
+
+bool CFX_FontMapper::HasLocalizedFont(ByteStringView name) const {
+ for (const auto& fontPair : m_LocalizedTTFonts) {
+ if (fontPair.first == name)
+ return true;
+ }
+ return false;
+}
+
+#if defined(OS_WIN)
+Optional<ByteString> CFX_FontMapper::InstalledFontNameStartingWith(
+ const ByteString& name) const {
+ for (const auto& thisname : m_InstalledTTFonts) {
+ if (thisname.First(name.GetLength()) == name)
+ return thisname;
+ }
+ return pdfium::nullopt;
+}
+
+Optional<ByteString> CFX_FontMapper::LocalizedFontNameStartingWith(
+ const ByteString& name) const {
+ for (const auto& thispair : m_LocalizedTTFonts) {
+ if (thispair.first.First(name.GetLength()) == name)
+ return thispair.second;
+ }
+ return pdfium::nullopt;
+}
+#endif // defined(OS_WIN)
+
#ifdef PDF_ENABLE_XFA
std::unique_ptr<uint8_t, FxFreeDeleter> CFX_FontMapper::RawBytesForIndex(
uint32_t index,
diff --git a/core/fxge/cfx_fontmapper.h b/core/fxge/cfx_fontmapper.h
index 91a9d7d..e1897d4 100644
--- a/core/fxge/cfx_fontmapper.h
+++ b/core/fxge/cfx_fontmapper.h
@@ -11,6 +11,7 @@
#include <utility>
#include <vector>
+#include "build/build_config.h"
#include "core/fxcrt/fx_memory_wrappers.h"
#include "core/fxcrt/fx_string.h"
#include "core/fxcrt/retain_ptr.h"
@@ -69,6 +70,15 @@
bool IsBuiltinFace(const RetainPtr<CFX_Face>& face) const;
int GetFaceSize() const;
ByteString GetFaceName(int index) const { return m_FaceArray[index].name; }
+ bool HasInstalledFont(ByteStringView name) const;
+ bool HasLocalizedFont(ByteStringView name) const;
+
+#if defined(OS_WIN)
+ Optional<ByteString> InstalledFontNameStartingWith(
+ const ByteString& name) const;
+ Optional<ByteString> LocalizedFontNameStartingWith(
+ const ByteString& name) const;
+#endif // defined(OS_WIN)
#ifdef PDF_ENABLE_XFA
std::unique_ptr<uint8_t, FxFreeDeleter> RawBytesForIndex(
@@ -76,9 +86,6 @@
size_t* returned_length);
#endif // PDF_ENABLE_XFA
- std::vector<ByteString> m_InstalledTTFonts;
- std::vector<std::pair<ByteString, ByteString>> m_LocalizedTTFonts;
-
private:
static constexpr size_t MM_FACE_COUNT = 2;
static constexpr size_t FOXIT_FACE_COUNT = 14;
@@ -110,6 +117,8 @@
std::vector<FaceData> m_FaceArray;
std::unique_ptr<SystemFontInfoIface> m_pFontInfo;
UnownedPtr<CFX_FontMgr> const m_pFontMgr;
+ std::vector<ByteString> m_InstalledTTFonts;
+ std::vector<std::pair<ByteString, ByteString>> m_LocalizedTTFonts;
RetainPtr<CFX_Face> m_MMFaces[MM_FACE_COUNT];
RetainPtr<CFX_Face> m_FoxitFaces[FOXIT_FACE_COUNT];
};
diff --git a/core/fxge/win32/cwin32_platform.cpp b/core/fxge/win32/cwin32_platform.cpp
index 765050d..c45d426 100644
--- a/core/fxge/win32/cwin32_platform.cpp
+++ b/core/fxge/win32/cwin32_platform.cpp
@@ -204,16 +204,16 @@
if (!m_pMapper)
return name;
- for (size_t i = 0; i < m_pMapper->m_InstalledTTFonts.size(); ++i) {
- ByteString thisname = m_pMapper->m_InstalledTTFonts[i];
- if (thisname.First(name.GetLength()) == name)
- return m_pMapper->m_InstalledTTFonts[i];
- }
- for (size_t i = 0; i < m_pMapper->m_LocalizedTTFonts.size(); ++i) {
- ByteString thisname = m_pMapper->m_LocalizedTTFonts[i].first;
- if (thisname.First(name.GetLength()) == name)
- return m_pMapper->m_LocalizedTTFonts[i].second;
- }
+ Optional<ByteString> maybe_installed =
+ m_pMapper->InstalledFontNameStartingWith(name);
+ if (maybe_installed.has_value())
+ return maybe_installed.value();
+
+ Optional<ByteString> maybe_localized =
+ m_pMapper->LocalizedFontNameStartingWith(name);
+ if (maybe_localized.has_value())
+ return maybe_localized.value();
+
return ByteString();
}