Diff
Modified: trunk/Source/WebCore/ChangeLog (275363 => 275364)
--- trunk/Source/WebCore/ChangeLog 2021-04-01 16:07:08 UTC (rev 275363)
+++ trunk/Source/WebCore/ChangeLog 2021-04-01 16:10:44 UTC (rev 275364)
@@ -1,3 +1,45 @@
+2021-04-01 Chris Lord <[email protected]>
+
+ Make FontCache constructable and safe to use off the main thread
+ https://bugs.webkit.org/show_bug.cgi?id=223997
+
+ Reviewed by Darin Adler.
+
+ So that we can have a separate FontCache for use with OffscreenCanvas
+ on Worker threads, make FontCache constructable/destructable and
+ ref-counted.
+
+ This also changes some functions so that they no longer rely on static
+ AtomString variables, as these paths would not be safe to use off the
+ main thread, and changes main-thread checks to creation-thread checks.
+
+ No new tests, no change in behavior.
+
+ * platform/graphics/FontCache.cpp:
+ (WebCore::FontCache::alternateFamilyName):
+ (WebCore::FontCache::getCachedFontPlatformData):
+ (WebCore::FontCache::fontForFamily):
+ (WebCore::FontCache::similarFont):
+ * platform/graphics/FontCache.h:
+ (WebCore::FontCache::fontForFamily):
+ (WebCore::FontCache::getCachedFontPlatformData):
+ * platform/graphics/FontCascadeFonts.cpp:
+ (WebCore::FontCascadeFonts::FontCascadeFonts):
+ (WebCore::FontCascadeFonts::glyphDataForCharacter):
+ * platform/graphics/FontCascadeFonts.h:
+ (WebCore::FontCascadeFonts::primaryFont):
+ * platform/graphics/cocoa/FontCacheCoreText.cpp:
+ (WebCore::FontCache::similarFont):
+ (WebCore::FontCache::platformAlternateFamilyName):
+ * platform/graphics/freetype/FontCacheFreeType.cpp:
+ (WebCore::FontCache::lastResortFallbackFont):
+ (WebCore::getFamilyNameStringFromFamily):
+ (WebCore::FontCache::platformAlternateFamilyName):
+ * platform/graphics/win/FontCacheWin.cpp:
+ (WebCore::FontCache::fontFromDescriptionAndLogFont):
+ (WebCore::FontCache::lastResortFallbackFont):
+ (WebCore::FontCache::platformAlternateFamilyName):
+
2021-04-01 Mark Lam <[email protected]>
Fix some missing exception checks in HTMLMediaElement methods.
Modified: trunk/Source/WebCore/platform/graphics/FontCache.cpp (275363 => 275364)
--- trunk/Source/WebCore/platform/graphics/FontCache.cpp 2021-04-01 16:07:08 UTC (rev 275363)
+++ trunk/Source/WebCore/platform/graphics/FontCache.cpp 2021-04-01 16:10:44 UTC (rev 275364)
@@ -134,33 +134,25 @@
static const bool emptyValueIsZero = false;
};
-const AtomString& FontCache::alternateFamilyName(const AtomString& familyName)
+Optional<ASCIILiteral> FontCache::alternateFamilyName(const String& familyName)
{
- static MainThreadNeverDestroyed<const AtomString> arial("Arial", AtomString::ConstructFromLiteral);
- static MainThreadNeverDestroyed<const AtomString> courier("Courier", AtomString::ConstructFromLiteral);
- static MainThreadNeverDestroyed<const AtomString> courierNew("Courier New", AtomString::ConstructFromLiteral);
- static MainThreadNeverDestroyed<const AtomString> helvetica("Helvetica", AtomString::ConstructFromLiteral);
- static MainThreadNeverDestroyed<const AtomString> times("Times", AtomString::ConstructFromLiteral);
- static MainThreadNeverDestroyed<const AtomString> timesNewRoman("Times New Roman", AtomString::ConstructFromLiteral);
-
- const AtomString& platformSpecificAlternate = platformAlternateFamilyName(familyName);
- if (!platformSpecificAlternate.isNull())
+ if (auto platformSpecificAlternate = platformAlternateFamilyName(familyName))
return platformSpecificAlternate;
switch (familyName.length()) {
case 5:
if (equalLettersIgnoringASCIICase(familyName, "arial"))
- return helvetica;
+ return "Helvetica"_s;
if (equalLettersIgnoringASCIICase(familyName, "times"))
- return timesNewRoman;
+ return "Times New Roman"_s;
break;
case 7:
if (equalLettersIgnoringASCIICase(familyName, "courier"))
- return courierNew;
+ return "Courier New"_s;
break;
case 9:
if (equalLettersIgnoringASCIICase(familyName, "helvetica"))
- return arial;
+ return "Arial"_s;
break;
#if !OS(WINDOWS)
// On Windows, Courier New is a TrueType font that is always present and
@@ -170,19 +162,19 @@
// only be tried if Courier New is not found.
case 11:
if (equalLettersIgnoringASCIICase(familyName, "courier new"))
- return courier;
+ return "Courier"_s;
break;
#endif
case 15:
if (equalLettersIgnoringASCIICase(familyName, "times new roman"))
- return times;
+ return "Times"_s;
break;
}
- return nullAtom();
+ return WTF::nullopt;
}
-FontPlatformData* FontCache::getCachedFontPlatformData(const FontDescription& fontDescription, const AtomString& passedFamilyName,
+FontPlatformData* FontCache::getCachedFontPlatformData(const FontDescription& fontDescription, const String& passedFamilyName,
const FontFeatureSettings* fontFaceFeatures, FontSelectionSpecifiedCapabilities fontFaceCapabilities, bool checkingAlternateName)
{
#if PLATFORM(IOS_FAMILY)
@@ -192,18 +184,16 @@
#if OS(WINDOWS) && ENABLE(OPENTYPE_VERTICAL)
// Leading "@" in the font name enables Windows vertical flow flag for the font.
// Because we do vertical flow by ourselves, we don't want to use the Windows feature.
- // IE disregards "@" regardless of the orientatoin, so we follow the behavior.
- const AtomString& familyName = (passedFamilyName.isEmpty() || passedFamilyName[0] != '@') ?
- passedFamilyName : AtomString(passedFamilyName.impl()->substring(1));
+ // IE disregards "@" regardless of the orientation, so we follow the behavior.
+ const String& familyName = passedFamilyName.substring(passedFamilyName[0] == '@' ? 1 : 0);
#else
- const AtomString& familyName = passedFamilyName;
+ const String& familyName = passedFamilyName;
#endif
- static bool initialized;
- if (!initialized) {
+ static std::once_flag onceFlag;
+ std::call_once(onceFlag, [&]() {
platformInit();
- initialized = true;
- }
+ });
FontPlatformDataCacheKey key(familyName, fontDescription, fontFaceFeatures, fontFaceCapabilities);
@@ -215,9 +205,8 @@
if (!it->value && !checkingAlternateName) {
// We were unable to find a font. We have a small set of fonts that we alias to other names,
// e.g., Arial/Helvetica, Courier/Courier New, etc. Try looking up the font under the aliased name.
- const AtomString& alternateName = alternateFamilyName(familyName);
- if (!alternateName.isNull()) {
- FontPlatformData* fontPlatformDataForAlternateName = getCachedFontPlatformData(fontDescription, alternateName, fontFaceFeatures, fontFaceCapabilities, true);
+ if (auto alternateName = alternateFamilyName(familyName)) {
+ auto* fontPlatformDataForAlternateName = getCachedFontPlatformData(fontDescription, *alternateName, fontFaceFeatures, fontFaceCapabilities, true);
// Lookup the key in the hash table again as the previous iterator may have
// been invalidated by the recursive call to getCachedFontPlatformData().
it = m_fontPlatformDataCache->find(key);
@@ -283,7 +272,7 @@
const unsigned cMaxUnderMemoryPressureInactiveFontData = 50;
const unsigned cTargetUnderMemoryPressureInactiveFontData = 30;
-RefPtr<Font> FontCache::fontForFamily(const FontDescription& fontDescription, const AtomString& family, const FontFeatureSettings* fontFaceFeatures, FontSelectionSpecifiedCapabilities fontFaceCapabilities, bool checkingAlternateName)
+RefPtr<Font> FontCache::fontForFamily(const FontDescription& fontDescription, const String& family, const FontFeatureSettings* fontFaceFeatures, FontSelectionSpecifiedCapabilities fontFaceCapabilities, bool checkingAlternateName)
{
if (!m_purgeTimer.isActive())
m_purgeTimer.startOneShot(0_s);
@@ -534,7 +523,7 @@
{
}
-RefPtr<Font> FontCache::similarFont(const FontDescription&, const AtomString&)
+RefPtr<Font> FontCache::similarFont(const FontDescription&, const String&)
{
return nullptr;
}
Modified: trunk/Source/WebCore/platform/graphics/FontCache.h (275363 => 275364)
--- trunk/Source/WebCore/platform/graphics/FontCache.h 2021-04-01 16:07:08 UTC (rev 275363)
+++ trunk/Source/WebCore/platform/graphics/FontCache.h 2021-04-01 16:10:44 UTC (rev 275364)
@@ -224,11 +224,13 @@
typedef HashMap<FontPlatformData, RefPtr<OpenTypeVerticalData>, FontDataCacheKeyHash, FontDataCacheKeyTraits> FontVerticalDataCache;
#endif
-class FontCache {
+class FontCache : public RefCounted<FontCache> {
friend class WTF::NeverDestroyed<FontCache>;
WTF_MAKE_NONCOPYABLE(FontCache); WTF_MAKE_FAST_ALLOCATED;
public:
+ FontCache();
+
WEBCORE_EXPORT static FontCache& singleton();
// These methods are implemented by the platform.
@@ -253,10 +255,10 @@
// It comes into play when you create an @font-face which shares a family name as a preinstalled font.
Vector<FontSelectionCapabilities> getFontSelectionCapabilitiesInFamily(const AtomString&, AllowUserInstalledFonts);
- WEBCORE_EXPORT RefPtr<Font> fontForFamily(const FontDescription&, const AtomString&, const FontFeatureSettings* fontFaceFeatures = nullptr, FontSelectionSpecifiedCapabilities fontFaceCapabilities = { }, bool checkingAlternateName = false);
+ WEBCORE_EXPORT RefPtr<Font> fontForFamily(const FontDescription&, const String&, const FontFeatureSettings* fontFaceFeatures = nullptr, FontSelectionSpecifiedCapabilities fontFaceCapabilities = { }, bool checkingAlternateName = false);
WEBCORE_EXPORT Ref<Font> lastResortFallbackFont(const FontDescription&);
WEBCORE_EXPORT Ref<Font> fontForPlatformData(const FontPlatformData&);
- RefPtr<Font> similarFont(const FontDescription&, const AtomString& family);
+ RefPtr<Font> similarFont(const FontDescription&, const String& family);
void addClient(FontSelector&);
void removeClient(FontSelector&);
@@ -274,7 +276,7 @@
void clearWidthCaches();
#if PLATFORM(WIN)
- RefPtr<Font> fontFromDescriptionAndLogFont(const FontDescription&, const LOGFONT&, AtomString& outFontFamilyName);
+ RefPtr<Font> fontFromDescriptionAndLogFont(const FontDescription&, const LOGFONT&, String& outFontFamilyName);
#endif
#if ENABLE(OPENTYPE_VERTICAL)
@@ -301,9 +303,6 @@
void prewarmGlobally();
private:
- FontCache();
- ~FontCache() = delete;
-
WEBCORE_EXPORT void purgeInactiveFontDataIfNeeded();
void pruneUnreferencedEntriesFromFontCascadeCache();
void pruneSystemFallbackFonts();
@@ -310,13 +309,13 @@
Ref<FontCascadeFonts> retrieveOrAddCachedFonts(const FontCascadeDescription&, RefPtr<FontSelector>&&);
// FIXME: This method should eventually be removed.
- FontPlatformData* getCachedFontPlatformData(const FontDescription&, const AtomString& family, const FontFeatureSettings* fontFaceFeatures = nullptr, FontSelectionSpecifiedCapabilities fontFaceCapabilities = { }, bool checkingAlternateName = false);
+ FontPlatformData* getCachedFontPlatformData(const FontDescription&, const String& family, const FontFeatureSettings* fontFaceFeatures = nullptr, FontSelectionSpecifiedCapabilities fontFaceCapabilities = { }, bool checkingAlternateName = false);
// These methods are implemented by each platform.
WEBCORE_EXPORT std::unique_ptr<FontPlatformData> createFontPlatformData(const FontDescription&, const AtomString& family, const FontFeatureSettings* fontFaceFeatures, FontSelectionSpecifiedCapabilities fontFaceCapabilities);
- static const AtomString& alternateFamilyName(const AtomString&);
- static const AtomString& platformAlternateFamilyName(const AtomString&);
+ static Optional<ASCIILiteral> alternateFamilyName(const String&);
+ static Optional<ASCIILiteral> platformAlternateFamilyName(const String&);
Timer m_purgeTimer;
Modified: trunk/Source/WebCore/platform/graphics/FontCascadeFonts.cpp (275363 => 275364)
--- trunk/Source/WebCore/platform/graphics/FontCascadeFonts.cpp 2021-04-01 16:07:08 UTC (rev 275363)
+++ trunk/Source/WebCore/platform/graphics/FontCascadeFonts.cpp 2021-04-01 16:10:44 UTC (rev 275364)
@@ -104,6 +104,10 @@
, m_fontSelectorVersion(m_fontSelector ? m_fontSelector->version() : 0)
, m_generation(FontCache::singleton().generation())
{
+#if ASSERT_ENABLED
+ if (!isMainThread())
+ m_thread = makeRef(Thread::current());
+#endif
}
FontCascadeFonts::FontCascadeFonts(const FontPlatformData& platformData)
@@ -488,7 +492,7 @@
GlyphData FontCascadeFonts::glyphDataForCharacter(UChar32 c, const FontCascadeDescription& description, FontVariant variant)
{
- ASSERT(isMainThread());
+ ASSERT(m_thread ? m_thread->ptr() == &Thread::current() : isMainThread());
ASSERT(variant != AutoVariant);
if (variant != NormalVariant)
Modified: trunk/Source/WebCore/platform/graphics/FontCascadeFonts.h (275363 => 275364)
--- trunk/Source/WebCore/platform/graphics/FontCascadeFonts.h 2021-04-01 16:07:08 UTC (rev 275363)
+++ trunk/Source/WebCore/platform/graphics/FontCascadeFonts.h 2021-04-01 16:10:44 UTC (rev 275364)
@@ -114,6 +114,9 @@
unsigned short m_generation;
Pitch m_pitch { UnknownPitch };
bool m_isForPlatformFont { false };
+#if ASSERT_ENABLED
+ Optional<Ref<Thread>> m_thread;
+#endif
};
inline bool FontCascadeFonts::isFixedPitch(const FontCascadeDescription& description)
@@ -125,7 +128,7 @@
inline const Font& FontCascadeFonts::primaryFont(const FontCascadeDescription& description)
{
- ASSERT(isMainThread());
+ ASSERT(m_thread ? m_thread->ptr() == &Thread::current() : isMainThread());
if (!m_cachedPrimaryFont) {
auto& primaryRanges = realizeFallbackRangesAt(description, 0);
m_cachedPrimaryFont = primaryRanges.glyphDataForCharacter(' ', ExternalResourceDownloadPolicy::Allow).font;
Modified: trunk/Source/WebCore/platform/graphics/cocoa/FontCacheCoreText.cpp (275363 => 275364)
--- trunk/Source/WebCore/platform/graphics/cocoa/FontCacheCoreText.cpp 2021-04-01 16:07:08 UTC (rev 275363)
+++ trunk/Source/WebCore/platform/graphics/cocoa/FontCacheCoreText.cpp 2021-04-01 16:10:44 UTC (rev 275364)
@@ -619,7 +619,7 @@
return adoptCF(CTFontCreateCopyWithAttributes(originalFont, CTFontGetSize(originalFont), nullptr, descriptor.get()));
}
-RefPtr<Font> FontCache::similarFont(const FontDescription& description, const AtomString& family)
+RefPtr<Font> FontCache::similarFont(const FontDescription& description, const String& family)
{
// Attempt to find an appropriate font using a match based on the presence of keywords in
// the requested names. For example, we'll match any name that contains "Arabic" to Geeza Pro.
@@ -628,27 +628,19 @@
#if PLATFORM(IOS_FAMILY)
// Substitute the default monospace font for well-known monospace fonts.
- if (equalLettersIgnoringASCIICase(family, "monaco") || equalLettersIgnoringASCIICase(family, "menlo")) {
- static MainThreadNeverDestroyed<const AtomString> courier("courier", AtomString::ConstructFromLiteral);
- return fontForFamily(description, courier);
- }
+ if (equalLettersIgnoringASCIICase(family, "monaco") || equalLettersIgnoringASCIICase(family, "menlo"))
+ return fontForFamily(description, "courier"_s);
// Substitute Verdana for Lucida Grande.
- if (equalLettersIgnoringASCIICase(family, "lucida grande")) {
- static MainThreadNeverDestroyed<const AtomString> verdana("verdana", AtomString::ConstructFromLiteral);
- return fontForFamily(description, verdana);
- }
+ if (equalLettersIgnoringASCIICase(family, "lucida grande"))
+ return fontForFamily(description, "verdana"_s);
#endif
- static NeverDestroyed<String> arabic(MAKE_STATIC_STRING_IMPL("Arabic"));
- static NeverDestroyed<String> pashto(MAKE_STATIC_STRING_IMPL("Pashto"));
- static NeverDestroyed<String> urdu(MAKE_STATIC_STRING_IMPL("Urdu"));
- static String* matchWords[3] = { &arabic.get(), &pashto.get(), &urdu.get() };
- static MainThreadNeverDestroyed<const AtomString> geezaPlain("GeezaPro", AtomString::ConstructFromLiteral);
- static MainThreadNeverDestroyed<const AtomString> geezaBold("GeezaPro-Bold", AtomString::ConstructFromLiteral);
- for (String* matchWord : matchWords) {
- if (family.containsIgnoringASCIICase(*matchWord))
- return fontForFamily(description, isFontWeightBold(description.weight()) ? geezaBold : geezaPlain);
+ static constexpr ASCIILiteral matchWords[] = { "Arabic"_s, "Pashto"_s, "Urdu"_s };
+ auto familyMatcher = StringView(family);
+ for (auto matchWord : matchWords) {
+ if (equalIgnoringASCIICase(familyMatcher, StringView(matchWord)))
+ return fontForFamily(description, isFontWeightBold(description.weight()) ? "GeezaPro-Bold"_s : "GeezaPro"_s);
}
return nullptr;
}
@@ -1423,7 +1415,7 @@
return fontForPlatformData(alternateFont);
}
-const AtomString& FontCache::platformAlternateFamilyName(const AtomString& familyName)
+Optional<ASCIILiteral> FontCache::platformAlternateFamilyName(const String& familyName)
{
static const UChar heitiString[] = { 0x9ed1, 0x4f53 };
static const UChar songtiString[] = { 0x5b8b, 0x4f53 };
@@ -1431,10 +1423,10 @@
static const UChar weiruanYaHeiString[] = { 0x5fae, 0x8f6f, 0x96c5, 0x9ed1 };
static const UChar weiruanZhengHeitiString[] = { 0x5fae, 0x8edf, 0x6b63, 0x9ed1, 0x9ad4 };
- static MainThreadNeverDestroyed<const AtomString> songtiSC("Songti SC", AtomString::ConstructFromLiteral);
- static MainThreadNeverDestroyed<const AtomString> songtiTC("Songti TC", AtomString::ConstructFromLiteral);
- static MainThreadNeverDestroyed<const AtomString> heitiSCReplacement("PingFang SC", AtomString::ConstructFromLiteral);
- static MainThreadNeverDestroyed<const AtomString> heitiTCReplacement("PingFang TC", AtomString::ConstructFromLiteral);
+ static constexpr ASCIILiteral songtiSC = "Songti SC"_s;
+ static constexpr ASCIILiteral songtiTC = "Songti TC"_s;
+ static constexpr ASCIILiteral heitiSCReplacement = "PingFang SC"_s;
+ static constexpr ASCIILiteral heitiTCReplacement = "PingFang TC"_s;
switch (familyName.length()) {
case 2:
@@ -1469,7 +1461,7 @@
break;
}
- return nullAtom();
+ return WTF::nullopt;
}
void addAttributesForInstalledFonts(CFMutableDictionaryRef attributes, AllowUserInstalledFonts allowUserInstalledFonts)
Modified: trunk/Source/WebCore/platform/graphics/freetype/FontCacheFreeType.cpp (275363 => 275364)
--- trunk/Source/WebCore/platform/graphics/freetype/FontCacheFreeType.cpp 2021-04-01 16:07:08 UTC (rev 275363)
+++ trunk/Source/WebCore/platform/graphics/freetype/FontCacheFreeType.cpp 2021-04-01 16:10:44 UTC (rev 275364)
@@ -319,9 +319,8 @@
{
// We want to return a fallback font here, otherwise the logic preventing FontConfig
// matches for non-fallback fonts might return 0. See isFallbackFontAllowed.
- static AtomString timesStr("serif");
- if (RefPtr<Font> font = fontForFamily(fontDescription, timesStr))
- return *font;
+ if (RefPtr<Font> font = fontForFamily(fontDescription, "serif"_s))
+ return font.releaseNonNull();
// This could be reached due to improperly-installed or misconfigured fontconfig.
RELEASE_ASSERT_NOT_REACHED();
@@ -332,26 +331,26 @@
return { };
}
-static String getFamilyNameStringFromFamily(const AtomString& family)
+static String getFamilyNameStringFromFamily(const String& family)
{
// If we're creating a fallback font (e.g. "-webkit-monospace"), convert the name into
// the fallback name (like "monospace") that fontconfig understands.
if (family.length() && !family.startsWith("-webkit-"))
- return family.string();
+ return family;
- if (family == standardFamily || family == serifFamily)
+ if (family == familyNamesData->at(static_cast<unsigned>(FamilyNamesIndex::StandardFamily)) || family == familyNamesData->at(static_cast<unsigned>(FamilyNamesIndex::SerifFamily)))
return "serif";
- if (family == sansSerifFamily)
+ if (family == familyNamesData->at(static_cast<unsigned>(FamilyNamesIndex::SansSerifFamily)))
return "sans-serif";
- if (family == monospaceFamily)
+ if (family == familyNamesData->at(static_cast<unsigned>(FamilyNamesIndex::MonospaceFamily)))
return "monospace";
- if (family == cursiveFamily)
+ if (family == familyNamesData->at(static_cast<unsigned>(FamilyNamesIndex::CursiveFamily)))
return "cursive";
- if (family == fantasyFamily)
+ if (family == familyNamesData->at(static_cast<unsigned>(FamilyNamesIndex::FantasyFamily)))
return "fantasy";
#if PLATFORM(GTK)
- if (family == systemUiFamily || family == "-webkit-system-font")
+ if (family == familyNamesData->at(static_cast<unsigned>(FamilyNamesIndex::SystemUiFamily)) || family == "-webkit-system-font")
return defaultGtkSystemFont();
#endif
@@ -599,9 +598,9 @@
return platformData;
}
-const AtomString& FontCache::platformAlternateFamilyName(const AtomString&)
+Optional<ASCIILiteral> FontCache::platformAlternateFamilyName(const String&)
{
- return nullAtom();
+ return WTF::nullopt;
}
#if ENABLE(VARIATION_FONTS)
Modified: trunk/Source/WebCore/platform/graphics/win/FontCacheWin.cpp (275363 => 275364)
--- trunk/Source/WebCore/platform/graphics/win/FontCacheWin.cpp 2021-04-01 16:07:08 UTC (rev 275363)
+++ trunk/Source/WebCore/platform/graphics/win/FontCacheWin.cpp 2021-04-01 16:10:44 UTC (rev 275364)
@@ -362,9 +362,9 @@
return false;
}
-RefPtr<Font> FontCache::fontFromDescriptionAndLogFont(const FontDescription& fontDescription, const LOGFONT& font, AtomString& outFontFamilyName)
+RefPtr<Font> FontCache::fontFromDescriptionAndLogFont(const FontDescription& fontDescription, const LOGFONT& font, String& outFontFamilyName)
{
- AtomString familyName(font.lfFaceName, wcsnlen(font.lfFaceName, LF_FACESIZE));
+ String familyName(font.lfFaceName, wcsnlen(font.lfFaceName, LF_FACESIZE));
RefPtr<Font> fontData = fontForFamily(fontDescription, familyName);
if (fontData)
outFontFamilyName = familyName;
@@ -373,56 +373,57 @@
Ref<Font> FontCache::lastResortFallbackFont(const FontDescription& fontDescription)
{
- static MainThreadNeverDestroyed<AtomString> fallbackFontName;
+ static NeverDestroyed<String> fallbackFontName;
+ static std::once_flag onceFlag;
+ std::call_once(onceFlag, [&]() {
+ // FIXME: Would be even better to somehow get the user's default font here. For now we'll pick
+ // the default that the user would get without changing any prefs.
- if (!fallbackFontName.get().isEmpty())
- return *fontForFamily(fontDescription, fallbackFontName);
+ // Search all typical Windows-installed full Unicode fonts.
+ // Sorted by most to least glyphs according to http://en.wikipedia.org/wiki/Unicode_typefaces
+ // Start with Times New Roman also since it is the default if the user doesn't change prefs.
+ static NeverDestroyed<const String> fallbackFonts[] = {
+ String("Times New Roman", String::ConstructFromLiteral),
+ String("Microsoft Sans Serif", String::ConstructFromLiteral),
+ String("Tahoma", String::ConstructFromLiteral),
+ String("Lucida Sans Unicode", String::ConstructFromLiteral),
+ String("Arial", String::ConstructFromLiteral)
+ };
+ for (size_t i = 0; i < WTF_ARRAY_LENGTH(fallbackFonts); ++i) {
+ if (fontForFamily(fontDescription, fallbackFonts[i])) {
+ fallbackFontName.get() = fallbackFonts[i];
+ return;
+ }
+ }
- // FIXME: Would be even better to somehow get the user's default font here. For now we'll pick
- // the default that the user would get without changing any prefs.
+ // Fall back to the DEFAULT_GUI_FONT if no known Unicode fonts are available.
+ if (HFONT defaultGUIFont = static_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT))) {
+ LOGFONT defaultGUILogFont;
+ GetObject(defaultGUIFont, sizeof(defaultGUILogFont), &defaultGUILogFont);
+ if (fontFromDescriptionAndLogFont(fontDescription, defaultGUILogFont, fallbackFontName))
+ return;
+ }
- // Search all typical Windows-installed full Unicode fonts.
- // Sorted by most to least glyphs according to http://en.wikipedia.org/wiki/Unicode_typefaces
- // Start with Times New Roman also since it is the default if the user doesn't change prefs.
- static MainThreadNeverDestroyed<const AtomString> fallbackFonts[] = {
- AtomString("Times New Roman", AtomString::ConstructFromLiteral),
- AtomString("Microsoft Sans Serif", AtomString::ConstructFromLiteral),
- AtomString("Tahoma", AtomString::ConstructFromLiteral),
- AtomString("Lucida Sans Unicode", AtomString::ConstructFromLiteral),
- AtomString("Arial", AtomString::ConstructFromLiteral)
- };
- RefPtr<Font> simpleFont;
- for (size_t i = 0; i < WTF_ARRAY_LENGTH(fallbackFonts); ++i) {
- if (simpleFont = fontForFamily(fontDescription, fallbackFonts[i])) {
- fallbackFontName.get() = fallbackFonts[i];
- return *simpleFont;
+ // Fall back to Non-client metrics fonts.
+ NONCLIENTMETRICS nonClientMetrics { };
+ nonClientMetrics.cbSize = sizeof(nonClientMetrics);
+ if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(nonClientMetrics), &nonClientMetrics, 0)) {
+ if (fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfMessageFont, fallbackFontName))
+ return;
+ if (fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfMenuFont, fallbackFontName))
+ return;
+ if (fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfStatusFont, fallbackFontName))
+ return;
+ if (fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfCaptionFont, fallbackFontName))
+ return;
+ if (fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfSmCaptionFont, fallbackFontName))
+ return;
}
- }
+ });
- // Fall back to the DEFAULT_GUI_FONT if no known Unicode fonts are available.
- if (HFONT defaultGUIFont = static_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT))) {
- LOGFONT defaultGUILogFont;
- GetObject(defaultGUIFont, sizeof(defaultGUILogFont), &defaultGUILogFont);
- if (simpleFont = fontFromDescriptionAndLogFont(fontDescription, defaultGUILogFont, fallbackFontName))
- return *simpleFont;
- }
+ if (!fallbackFontName.get().isEmpty())
+ return *fontForFamily(fontDescription, fallbackFontName);
- // Fall back to Non-client metrics fonts.
- NONCLIENTMETRICS nonClientMetrics { };
- nonClientMetrics.cbSize = sizeof(nonClientMetrics);
- if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(nonClientMetrics), &nonClientMetrics, 0)) {
- if (simpleFont = fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfMessageFont, fallbackFontName))
- return *simpleFont;
- if (simpleFont = fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfMenuFont, fallbackFontName))
- return *simpleFont;
- if (simpleFont = fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfStatusFont, fallbackFontName))
- return *simpleFont;
- if (simpleFont = fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfCaptionFont, fallbackFontName))
- return *simpleFont;
- if (simpleFont = fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfSmCaptionFont, fallbackFontName))
- return *simpleFont;
- }
-
auto hFont = adoptGDIObject(static_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT)));
FontPlatformData platformData(WTFMove(hFont), fontDescription.computedPixelSize(), false, false, false);
return fontForPlatformData(platformData);
@@ -692,11 +693,8 @@
return result;
}
-const AtomString& FontCache::platformAlternateFamilyName(const AtomString& familyName)
+Optional<ASCIILiteral> FontCache::platformAlternateFamilyName(const String& familyName)
{
- static MainThreadNeverDestroyed<const AtomString> timesNewRoman("Times New Roman", AtomString::ConstructFromLiteral);
- static MainThreadNeverDestroyed<const AtomString> microsoftSansSerif("Microsoft Sans Serif", AtomString::ConstructFromLiteral);
-
switch (familyName.length()) {
// On Windows, we don't support bitmap fonts, but legacy content expects support.
// Thus we allow Times New Roman as an alternative for the bitmap font MS Serif,
@@ -705,7 +703,7 @@
// would need it on other platforms too.
case 8:
if (equalLettersIgnoringASCIICase(familyName, "ms serif"))
- return timesNewRoman;
+ return "Times New Roman"_s;
break;
// On Windows, we don't support bitmap fonts, but legacy content expects support.
// Thus we allow Microsoft Sans Serif as an alternative for the bitmap font MS Sans Serif,
@@ -714,10 +712,10 @@
// would need it on other platforms too.
case 13:
if (equalLettersIgnoringASCIICase(familyName, "ms sans serif"))
- return microsoftSansSerif;
+ return "Microsoft Sans Serif"_s;
break;
}
- return nullAtom();
+ return WTF::nullopt;
}
}