Source code

Revision control

Other Tools

/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Logging.h"
#include "mozilla/gfx/Logging.h"
#include "mozilla/intl/LocaleService.h"
#include "mozilla/intl/MozLocale.h"
#include "mozilla/intl/OSPreferences.h"
#include "gfxPlatformFontList.h"
#include "gfxTextRun.h"
#include "gfxUserFontSet.h"
#include "SharedFontList-impl.h"
#include "GeckoProfiler.h"
#include "nsCRT.h"
#include "nsGkAtoms.h"
#include "nsServiceManagerUtils.h"
#include "nsUnicharUtils.h"
#include "nsUnicodeProperties.h"
#include "nsXULAppAPI.h"
#include "mozilla/AppShutdown.h"
#include "mozilla/Attributes.h"
#include "mozilla/Likely.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Mutex.h"
#include "mozilla/Preferences.h"
#include "mozilla/StaticPrefs_gfx.h"
#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/Telemetry.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/dom/BlobImpl.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/dom/ContentProcessMessageManager.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/ipc/FileDescriptorUtils.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/TextUtils.h"
#include "mozilla/Unused.h"
#include "base/eintr_wrapper.h"
#include <locale.h>
#include <numeric>
using namespace mozilla;
using mozilla::intl::Locale;
using mozilla::intl::LocaleService;
using mozilla::intl::OSPreferences;
#define LOG_FONTLIST(args) \
MOZ_LOG(gfxPlatform::GetLog(eGfxLog_fontlist), LogLevel::Debug, args)
#define LOG_FONTLIST_ENABLED() \
MOZ_LOG_TEST(gfxPlatform::GetLog(eGfxLog_fontlist), LogLevel::Debug)
#define LOG_FONTINIT(args) \
MOZ_LOG(gfxPlatform::GetLog(eGfxLog_fontinit), LogLevel::Debug, args)
#define LOG_FONTINIT_ENABLED() \
MOZ_LOG_TEST(gfxPlatform::GetLog(eGfxLog_fontinit), LogLevel::Debug)
gfxPlatformFontList* gfxPlatformFontList::sPlatformFontList = nullptr;
// Character ranges that require complex-script shaping support in the font,
// and so should be masked out by ReadCMAP if the necessary layout tables
// are not present.
// Currently used by the Mac and FT2 implementations only, but probably should
// be supported on Windows as well.
const gfxFontEntry::ScriptRange gfxPlatformFontList::sComplexScriptRanges[] = {
// Actually, now that harfbuzz supports presentation-forms shaping for
// Arabic, we can render it without layout tables. So maybe we don't
// want to mask the basic Arabic block here?
// This affects the arabic-fallback-*.html reftests, which rely on
// loading a font that *doesn't* have any GSUB table.
{0x0600, 0x06FF, 1, {TRUETYPE_TAG('a', 'r', 'a', 'b'), 0, 0}},
{0x0700, 0x074F, 1, {TRUETYPE_TAG('s', 'y', 'r', 'c'), 0, 0}},
{0x0750, 0x077F, 1, {TRUETYPE_TAG('a', 'r', 'a', 'b'), 0, 0}},
{0x08A0, 0x08FF, 1, {TRUETYPE_TAG('a', 'r', 'a', 'b'), 0, 0}},
{0x0900,
0x097F,
2,
{TRUETYPE_TAG('d', 'e', 'v', '2'), TRUETYPE_TAG('d', 'e', 'v', 'a'), 0}},
{0x0980,
0x09FF,
2,
{TRUETYPE_TAG('b', 'n', 'g', '2'), TRUETYPE_TAG('b', 'e', 'n', 'g'), 0}},
{0x0A00,
0x0A7F,
2,
{TRUETYPE_TAG('g', 'u', 'r', '2'), TRUETYPE_TAG('g', 'u', 'r', 'u'), 0}},
{0x0A80,
0x0AFF,
2,
{TRUETYPE_TAG('g', 'j', 'r', '2'), TRUETYPE_TAG('g', 'u', 'j', 'r'), 0}},
{0x0B00,
0x0B7F,
2,
{TRUETYPE_TAG('o', 'r', 'y', '2'), TRUETYPE_TAG('o', 'r', 'y', 'a'), 0}},
{0x0B80,
0x0BFF,
2,
{TRUETYPE_TAG('t', 'm', 'l', '2'), TRUETYPE_TAG('t', 'a', 'm', 'l'), 0}},
{0x0C00,
0x0C7F,
2,
{TRUETYPE_TAG('t', 'e', 'l', '2'), TRUETYPE_TAG('t', 'e', 'l', 'u'), 0}},
{0x0C80,
0x0CFF,
2,
{TRUETYPE_TAG('k', 'n', 'd', '2'), TRUETYPE_TAG('k', 'n', 'd', 'a'), 0}},
{0x0D00,
0x0D7F,
2,
{TRUETYPE_TAG('m', 'l', 'm', '2'), TRUETYPE_TAG('m', 'l', 'y', 'm'), 0}},
{0x0D80, 0x0DFF, 1, {TRUETYPE_TAG('s', 'i', 'n', 'h'), 0, 0}},
{0x0E80, 0x0EFF, 1, {TRUETYPE_TAG('l', 'a', 'o', ' '), 0, 0}},
{0x0F00, 0x0FFF, 1, {TRUETYPE_TAG('t', 'i', 'b', 't'), 0, 0}},
{0x1000,
0x109f,
2,
{TRUETYPE_TAG('m', 'y', 'm', 'r'), TRUETYPE_TAG('m', 'y', 'm', '2'), 0}},
{0x1780, 0x17ff, 1, {TRUETYPE_TAG('k', 'h', 'm', 'r'), 0, 0}},
// Khmer Symbols (19e0..19ff) don't seem to need any special shaping
{0xaa60,
0xaa7f,
2,
{TRUETYPE_TAG('m', 'y', 'm', 'r'), TRUETYPE_TAG('m', 'y', 'm', '2'), 0}},
// Thai seems to be "renderable" without AAT morphing tables
{0, 0, 0, {0, 0, 0}} // terminator
};
static const char* kObservedPrefs[] = {"font.",
"font.name-list.",
"intl.accept_languages", // hmmmm...
"layout.css.font-visibility.level",
"privacy.resistFingerprinting",
nullptr};
static const char kFontSystemWhitelistPref[] = "font.system.whitelist";
static const char kCJKFallbackOrderPref[] = "font.cjk_pref_fallback_order";
// xxx - this can probably be eliminated by reworking pref font handling code
static const char* gPrefLangNames[] = {
#define FONT_PREF_LANG(enum_id_, str_, atom_id_) str_
#include "gfxFontPrefLangList.h"
#undef FONT_PREF_LANG
};
static_assert(MOZ_ARRAY_LENGTH(gPrefLangNames) == uint32_t(eFontPrefLang_Count),
"size of pref lang name array doesn't match pref lang enum size");
class gfxFontListPrefObserver final : public nsIObserver {
~gfxFontListPrefObserver() = default;
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
};
static void FontListPrefChanged(const char* aPref, void* aData = nullptr) {
// XXX this could be made to only clear out the cache for the prefs that were
// changed but it probably isn't that big a deal.
gfxPlatformFontList::PlatformFontList()->ClearLangGroupPrefFonts();
gfxFontCache::GetCache()->AgeAllGenerations();
if (aPref && (!strcmp(aPref, "layout.css.font-visibility.level") ||
!strcmp(aPref, "privacy.resistFingerprinting"))) {
gfxPlatformFontList::PlatformFontList()->SetVisibilityLevel();
if (XRE_IsParentProcess()) {
gfxPlatform::ForceGlobalReflow();
}
}
}
static gfxFontListPrefObserver* gFontListPrefObserver = nullptr;
NS_IMPL_ISUPPORTS(gfxFontListPrefObserver, nsIObserver)
#define LOCALES_CHANGED_TOPIC "intl:system-locales-changed"
NS_IMETHODIMP
gfxFontListPrefObserver::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) {
NS_ASSERTION(!strcmp(aTopic, LOCALES_CHANGED_TOPIC), "invalid topic");
FontListPrefChanged(nullptr);
if (XRE_IsParentProcess()) {
gfxPlatform::ForceGlobalReflow();
}
return NS_OK;
}
MOZ_DEFINE_MALLOC_SIZE_OF(FontListMallocSizeOf)
NS_IMPL_ISUPPORTS(gfxPlatformFontList::MemoryReporter, nsIMemoryReporter)
NS_IMETHODIMP
gfxPlatformFontList::MemoryReporter::CollectReports(
nsIHandleReportCallback* aHandleReport, nsISupports* aData,
bool aAnonymize) {
FontListSizes sizes;
sizes.mFontListSize = 0;
sizes.mFontTableCacheSize = 0;
sizes.mCharMapsSize = 0;
sizes.mLoaderSize = 0;
sizes.mSharedSize = 0;
gfxPlatformFontList::PlatformFontList()->AddSizeOfIncludingThis(
&FontListMallocSizeOf, &sizes);
MOZ_COLLECT_REPORT(
"explicit/gfx/font-list", KIND_HEAP, UNITS_BYTES, sizes.mFontListSize,
"Memory used to manage the list of font families and faces.");
MOZ_COLLECT_REPORT(
"explicit/gfx/font-charmaps", KIND_HEAP, UNITS_BYTES, sizes.mCharMapsSize,
"Memory used to record the character coverage of individual fonts.");
if (sizes.mFontTableCacheSize) {
MOZ_COLLECT_REPORT(
"explicit/gfx/font-tables", KIND_HEAP, UNITS_BYTES,
sizes.mFontTableCacheSize,
"Memory used for cached font metrics and layout tables.");
}
if (sizes.mLoaderSize) {
MOZ_COLLECT_REPORT("explicit/gfx/font-loader", KIND_HEAP, UNITS_BYTES,
sizes.mLoaderSize,
"Memory used for (platform-specific) font loader.");
}
if (sizes.mSharedSize) {
MOZ_COLLECT_REPORT(
"font-list-shmem", KIND_NONHEAP, UNITS_BYTES, sizes.mSharedSize,
"Shared memory for system font list and character coverage data.");
}
return NS_OK;
}
PRThread* gfxPlatformFontList::sInitFontListThread = nullptr;
static void InitFontListCallback(void* aFontList) {
AUTO_PROFILER_REGISTER_THREAD("InitFontList");
PR_SetCurrentThreadName("InitFontList");
if (!static_cast<gfxPlatformFontList*>(aFontList)->InitFontList()) {
gfxPlatformFontList::Shutdown();
}
}
/* static */
bool gfxPlatformFontList::Initialize(gfxPlatformFontList* aList) {
sPlatformFontList = aList;
if (XRE_IsParentProcess() &&
StaticPrefs::gfx_font_list_omt_enabled_AtStartup() &&
!gfxPlatform::InSafeMode()) {
sInitFontListThread = PR_CreateThread(
PR_USER_THREAD, InitFontListCallback, aList, PR_PRIORITY_NORMAL,
PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
return true;
}
if (aList->InitFontList()) {
return true;
}
Shutdown();
return false;
}
gfxPlatformFontList::gfxPlatformFontList(bool aNeedFullnamePostscriptNames)
: mFontFamiliesMutex("gfxPlatformFontList::mFontFamiliesMutex"),
mFontFamilies(64),
mOtherFamilyNames(16),
mSharedCmaps(8) {
if (aNeedFullnamePostscriptNames) {
mExtraNames = MakeUnique<ExtraNames>();
}
mLangService = nsLanguageAtomService::GetService();
LoadBadUnderlineList();
gfxFontUtils::GetPrefsFontList(kFontSystemWhitelistPref, mEnabledFontsList);
// pref changes notification setup
NS_ASSERTION(!gFontListPrefObserver,
"There has been font list pref observer already");
gFontListPrefObserver = new gfxFontListPrefObserver();
NS_ADDREF(gFontListPrefObserver);
Preferences::RegisterPrefixCallbacks(FontListPrefChanged, kObservedPrefs);
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
if (obs) {
obs->AddObserver(gFontListPrefObserver, LOCALES_CHANGED_TOPIC, false);
}
// Only the parent process listens for whitelist changes; it will then
// notify its children to rebuild their font lists.
if (XRE_IsParentProcess()) {
Preferences::RegisterCallback(FontWhitelistPrefChanged,
kFontSystemWhitelistPref);
}
RegisterStrongMemoryReporter(new MemoryReporter());
}
gfxPlatformFontList::~gfxPlatformFontList() {
mSharedCmaps.Clear();
ClearLangGroupPrefFonts();
NS_ASSERTION(gFontListPrefObserver, "There is no font list pref observer");
Preferences::UnregisterPrefixCallbacks(FontListPrefChanged, kObservedPrefs);
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
if (obs) {
obs->RemoveObserver(gFontListPrefObserver, LOCALES_CHANGED_TOPIC);
}
if (XRE_IsParentProcess()) {
Preferences::UnregisterCallback(FontWhitelistPrefChanged,
kFontSystemWhitelistPref);
}
NS_RELEASE(gFontListPrefObserver);
}
/* static */
void gfxPlatformFontList::FontWhitelistPrefChanged(const char* aPref,
void* aClosure) {
MOZ_ASSERT(XRE_IsParentProcess());
auto* pfl = gfxPlatformFontList::PlatformFontList();
pfl->UpdateFontList(true);
dom::ContentParent::NotifyUpdatedFonts(true);
}
// number of CSS generic font families
const uint32_t kNumGenerics = 5;
void gfxPlatformFontList::ApplyWhitelist() {
uint32_t numFonts = mEnabledFontsList.Length();
mFontFamilyWhitelistActive = (numFonts > 0);
if (!mFontFamilyWhitelistActive) {
return;
}
nsTHashSet<nsCString> familyNamesWhitelist;
for (uint32_t i = 0; i < numFonts; i++) {
nsAutoCString key;
ToLowerCase(mEnabledFontsList[i], key);
familyNamesWhitelist.Insert(key);
}
AutoTArray<RefPtr<gfxFontFamily>, 128> accepted;
bool whitelistedFontFound = false;
for (const auto& entry : mFontFamilies) {
if (entry.GetData()->IsHidden()) {
// Hidden system fonts are exempt from whitelisting, but don't count
// towards determining whether we "kept" any (user-visible) fonts
accepted.AppendElement(entry.GetData());
continue;
}
nsAutoCString fontFamilyName(entry.GetKey());
ToLowerCase(fontFamilyName);
if (familyNamesWhitelist.Contains(fontFamilyName)) {
accepted.AppendElement(entry.GetData());
whitelistedFontFound = true;
}
}
if (!whitelistedFontFound) {
// No whitelisted fonts found! Ignore the whitelist.
return;
}
// Replace the original full list with the accepted subset.
mFontFamilies.Clear();
for (auto& f : accepted) {
nsAutoCString fontFamilyName(f->Name());
ToLowerCase(fontFamilyName);
mFontFamilies.InsertOrUpdate(fontFamilyName, std::move(f));
}
}
void gfxPlatformFontList::ApplyWhitelist(
nsTArray<fontlist::Family::InitData>& aFamilies) {
mFontFamilyWhitelistActive = !mEnabledFontsList.IsEmpty();
if (!mFontFamilyWhitelistActive) {
return;
}
nsTHashSet<nsCString> familyNamesWhitelist;
for (const auto& item : mEnabledFontsList) {
nsAutoCString key;
ToLowerCase(item, key);
familyNamesWhitelist.Insert(key);
}
AutoTArray<fontlist::Family::InitData, 128> accepted;
bool keptNonHidden = false;
for (auto& f : aFamilies) {
if (f.mVisibility == FontVisibility::Hidden ||
familyNamesWhitelist.Contains(f.mKey)) {
accepted.AppendElement(f);
if (f.mVisibility != FontVisibility::Hidden) {
keptNonHidden = true;
}
}
}
if (!keptNonHidden) {
// No (visible) families were whitelisted: ignore the whitelist
// and just leave the fontlist unchanged.
return;
}
aFamilies = std::move(accepted);
}
bool gfxPlatformFontList::FamilyInList(const nsACString& aName,
const char* aList[], size_t aCount) {
auto cmp = [&](const char* const aVal) -> int {
return nsCaseInsensitiveUTF8StringComparator(aName.BeginReading(), aVal,
aName.Length(), strlen(aVal));
};
size_t result;
return BinarySearchIf(aList, 0, aCount, cmp, &result);
}
void gfxPlatformFontList::CheckFamilyList(const char* aList[], size_t aCount) {
#ifdef DEBUG
MOZ_ASSERT(aCount > 0, "empty font family list?");
const char* a = aList[0];
uint32_t aLen = strlen(a);
for (size_t i = 1; i < aCount; ++i) {
const char* b = aList[i];
uint32_t bLen = strlen(b);
MOZ_ASSERT(nsCaseInsensitiveUTF8StringComparator(a, b, aLen, bLen) < 0,
"incorrectly sorted font family list!");
a = b;
aLen = bLen;
}
#endif
}
bool gfxPlatformFontList::AddWithLegacyFamilyName(const nsACString& aLegacyName,
gfxFontEntry* aFontEntry,
FontVisibility aVisibility) {
bool added = false;
nsAutoCString key;
ToLowerCase(aLegacyName, key);
mOtherFamilyNames
.LookupOrInsertWith(
key,
[&] {
RefPtr<gfxFontFamily> family =
CreateFontFamily(aLegacyName, aVisibility);
family->SetHasStyles(
true); // we don't want the family to search for
// faces, we're adding them directly here
added = true;
return family;
})
->AddFontEntry(aFontEntry->Clone());
return added;
}
bool gfxPlatformFontList::InitFontList() {
MutexAutoLock lock(mFontFamiliesMutex);
if (LOG_FONTINIT_ENABLED()) {
LOG_FONTINIT(("(fontinit) system fontlist initialization\n"));
}
if (IsInitialized()) {
// Font-list reinitialization always occurs on the main thread, in response
// to a change notification; it's only the initial creation during startup
// that may be on another thread.
MOZ_ASSERT(NS_IsMainThread());
// Rebuilding fontlist so clear out font/word caches.
gfxFontCache* fontCache = gfxFontCache::GetCache();
if (fontCache) {
fontCache->FlushShapedWordCaches();
fontCache->Flush();
}
gfxPlatform::PurgeSkiaFontCache();
if (NS_IsMainThread()) {
nsCOMPtr<nsIObserverService> obs =
mozilla::services::GetObserverService();
if (obs) {
// Notify any current presContexts that fonts are being updated, so
// existing caches will no longer be valid.
obs->NotifyObservers(nullptr, "font-info-updated", nullptr);
}
} else {
NS_DispatchToMainThread(
NS_NewRunnableFunction("font-info-updated notification callback", [] {
nsCOMPtr<nsIObserverService> obs =
mozilla::services::GetObserverService();
if (obs) {
obs->NotifyObservers(nullptr, "font-info-updated", nullptr);
}
}));
}
mAliasTable.Clear();
mLocalNameTable.Clear();
CancelLoadCmapsTask();
mStartedLoadingCmapsFrom = 0xffffffffu;
CancelInitOtherFamilyNamesTask();
mFontFamilies.Clear();
mOtherFamilyNames.Clear();
mOtherFamilyNamesInitialized = false;
if (mExtraNames) {
mExtraNames->mFullnames.Clear();
mExtraNames->mPostscriptNames.Clear();
}
mFaceNameListsInitialized = false;
ClearLangGroupPrefFonts();
CancelLoader();
gfxFontUtils::GetPrefsFontList(kFontSystemWhitelistPref, mEnabledFontsList);
// Ensure SetVisibilityLevel will clear the mCodepointsWithNoFonts set.
mVisibilityLevel = FontVisibility::Unknown;
}
SetVisibilityLevel();
// From here, gfxPlatformFontList::IsInitialized will return true,
// unless InitFontListForPlatform() fails and we reset it below.
mFontlistInitCount++;
// Try to initialize the cross-process shared font list if enabled by prefs,
// but not if we're running in Safe Mode.
if (StaticPrefs::gfx_e10s_font_list_shared_AtStartup() &&
!gfxPlatform::InSafeMode()) {
for (const auto& entry : mFontEntries.Values()) {
if (!entry) {
continue;
}
entry->mShmemCharacterMap = nullptr;
entry->mShmemFace = nullptr;
entry->mFamilyName.Truncate();
}
mFontEntries.Clear();
mShmemCharMaps.Clear();
bool oldSharedList = mSharedFontList != nullptr;
mSharedFontList.reset(new fontlist::FontList(mFontlistInitCount));
InitSharedFontListForPlatform();
if (mSharedFontList && mSharedFontList->Initialized()) {
if (mLocalNameTable.Count()) {
SharedFontList()->SetLocalNames(mLocalNameTable);
mLocalNameTable.Clear();
}
} else {
// something went wrong, fall back to in-process list
gfxCriticalNote << "Failed to initialize shared font list, "
"falling back to in-process list.";
mSharedFontList.reset(nullptr);
}
if (oldSharedList && XRE_IsParentProcess()) {
// notify all children of the change
if (NS_IsMainThread()) {
dom::ContentParent::NotifyUpdatedFonts(true);
} else {
NS_DispatchToMainThread(NS_NewRunnableFunction(
"NotifyUpdatedFonts callback",
[] { dom::ContentParent::NotifyUpdatedFonts(true); }));
}
}
}
if (!SharedFontList()) {
if (NS_FAILED(InitFontListForPlatform())) {
mFontlistInitCount = 0;
return false;
}
ApplyWhitelist();
}
// Set up mDefaultFontEntry as a "last resort" default that we can use
// to avoid crashing if the font list is otherwise unusable.
gfxFontStyle defStyle;
FontFamily fam = GetDefaultFont(&defStyle);
if (fam.mIsShared) {
auto face = fam.mShared->FindFaceForStyle(SharedFontList(), defStyle);
mDefaultFontEntry =
face ? GetOrCreateFontEntry(face, fam.mShared) : nullptr;
} else {
mDefaultFontEntry = fam.mUnshared->FindFontForStyle(defStyle);
}
return true;
}
void gfxPlatformFontList::InitializeCodepointsWithNoFonts() {
mCodepointsWithNoFonts.reset();
mCodepointsWithNoFonts.SetRange(0, 0x1f); // C0 controls
mCodepointsWithNoFonts.SetRange(0x7f, 0x9f); // C1 controls
mCodepointsWithNoFonts.SetRange(0xE000, 0xF8FF); // PUA
mCodepointsWithNoFonts.SetRange(0xF0000, 0x10FFFD); // Supplementary PUA
mCodepointsWithNoFonts.SetRange(0xfdd0, 0xfdef); // noncharacters
for (unsigned i = 0; i <= 0x100000; i += 0x10000) {
mCodepointsWithNoFonts.SetRange(i + 0xfffe, i + 0xffff); // noncharacters
}
}
void gfxPlatformFontList::SetVisibilityLevel() {
FontVisibility newLevel;
if (StaticPrefs::privacy_resistFingerprinting()) {
newLevel = FontVisibility::Base;
} else {
newLevel = FontVisibility(
std::min(int32_t(FontVisibility::User),
std::max(int32_t(FontVisibility::Base),
StaticPrefs::layout_css_font_visibility_level())));
}
if (newLevel != mVisibilityLevel) {
mVisibilityLevel = newLevel;
// (Re-)initialize ranges of characters for which system-wide font search
// should be skipped
InitializeCodepointsWithNoFonts();
// Forget any font family we previously chose for U+FFFD.
mReplacementCharFallbackFamily = FontFamily();
}
}
void gfxPlatformFontList::FontListChanged() {
MOZ_ASSERT(!XRE_IsParentProcess());
InitializeCodepointsWithNoFonts();
if (SharedFontList()) {
// If we're using a shared local face-name list, this may have changed
// such that existing font entries held by user font sets are no longer
// safe to use: ensure they all get flushed.
RebuildLocalFonts(/*aForgetLocalFaces*/ true);
}
ForceGlobalReflow();
}
void gfxPlatformFontList::GenerateFontListKey(const nsACString& aKeyName,
nsACString& aResult) {
aResult = aKeyName;
ToLowerCase(aResult);
}
// Used if a stylo thread wants to trigger InitOtherFamilyNames in the main
// process: we can't do IPC from the stylo thread so we post this to the main
// thread instead.
class InitOtherFamilyNamesForStylo : public mozilla::Runnable {
public:
explicit InitOtherFamilyNamesForStylo(bool aDeferOtherFamilyNamesLoading)
: Runnable("gfxPlatformFontList::InitOtherFamilyNamesForStylo"),
mDefer(aDeferOtherFamilyNamesLoading) {}
NS_IMETHOD Run() override {
auto pfl = gfxPlatformFontList::PlatformFontList();
auto list = pfl->SharedFontList();
if (!list) {
return NS_OK;
}
dom::ContentChild::GetSingleton()->SendInitOtherFamilyNames(
list->GetGeneration(), mDefer, &pfl->mOtherFamilyNamesInitialized);
return NS_OK;
}
private:
bool mDefer;
};
#define OTHERNAMES_TIMEOUT 200
bool gfxPlatformFontList::InitOtherFamilyNames(
bool aDeferOtherFamilyNamesLoading) {
if (mOtherFamilyNamesInitialized) {
return true;
}
if (SharedFontList() && !XRE_IsParentProcess()) {
if (NS_IsMainThread()) {
dom::ContentChild::GetSingleton()->SendInitOtherFamilyNames(
SharedFontList()->GetGeneration(), aDeferOtherFamilyNamesLoading,
&mOtherFamilyNamesInitialized);
} else {
NS_DispatchToMainThread(
new InitOtherFamilyNamesForStylo(aDeferOtherFamilyNamesLoading));
}
return mOtherFamilyNamesInitialized;
}
// If the font loader delay has been set to zero, we don't defer loading
// additional family names (regardless of the aDefer... parameter), as we
// take this to mean availability of font info is to be prioritized over
// potential startup perf or main-thread jank.
// (This is used so we can reliably run reftests that depend on localized
// font-family names being available.)
if (aDeferOtherFamilyNamesLoading &&
StaticPrefs::gfx_font_loader_delay_AtStartup() > 0) {
if (!mPendingOtherFamilyNameTask) {
RefPtr<mozilla::CancelableRunnable> task =
new InitOtherFamilyNamesRunnable();
mPendingOtherFamilyNameTask = task;
NS_DispatchToMainThreadQueue(task.forget(), EventQueuePriority::Idle);
}
} else {
InitOtherFamilyNamesInternal(false);
}
return mOtherFamilyNamesInitialized;
}
// time limit for loading facename lists (ms)
#define NAMELIST_TIMEOUT 200
gfxFontEntry* gfxPlatformFontList::SearchFamiliesForFaceName(
const nsACString& aFaceName) {
TimeStamp start = TimeStamp::Now();
bool timedOut = false;
// if mFirstChar is not 0, only load facenames for families
// that start with this character
char16_t firstChar = 0;
gfxFontEntry* lookup = nullptr;
// iterate over familes starting with the same letter
firstChar = ToLowerCase(aFaceName.CharAt(0));
for (const auto& entry : mFontFamilies) {
nsCStringHashKey::KeyType key = entry.GetKey();
const RefPtr<gfxFontFamily>& family = entry.GetData();
// when filtering, skip names that don't start with the filter character
if (firstChar && ToLowerCase(key.CharAt(0)) != firstChar) {
continue;
}
family->ReadFaceNames(this, NeedFullnamePostscriptNames());
TimeDuration elapsed = TimeStamp::Now() - start;
if (elapsed.ToMilliseconds() > NAMELIST_TIMEOUT) {
timedOut = true;
break;
}
}
lookup = FindFaceName(aFaceName);
TimeStamp end = TimeStamp::Now();
Telemetry::AccumulateTimeDelta(Telemetry::FONTLIST_INITFACENAMELISTS, start,
end);
if (LOG_FONTINIT_ENABLED()) {
TimeDuration elapsed = end - start;
LOG_FONTINIT(("(fontinit) SearchFamiliesForFaceName took %8.2f ms %s %s",
elapsed.ToMilliseconds(), (lookup ? "found name" : ""),
(timedOut ? "timeout" : "")));
}
return lookup;
}
gfxFontEntry* gfxPlatformFontList::FindFaceName(const nsACString& aFaceName) {
gfxFontEntry* lookup;
// lookup in name lookup tables, return null if not found
if (mExtraNames &&
((lookup = mExtraNames->mPostscriptNames.GetWeak(aFaceName)) ||
(lookup = mExtraNames->mFullnames.GetWeak(aFaceName)))) {
return lookup;
}
return nullptr;
}
gfxFontEntry* gfxPlatformFontList::LookupInFaceNameLists(
const nsACString& aFaceName) {
gfxFontEntry* lookup = nullptr;
// initialize facename lookup tables if needed
// note: this can terminate early or time out, in which case
// mFaceNameListsInitialized remains false
if (!mFaceNameListsInitialized) {
lookup = SearchFamiliesForFaceName(aFaceName);
if (lookup) {
return lookup;
}
}
// lookup in name lookup tables, return null if not found
if (!(lookup = FindFaceName(aFaceName))) {
// names not completely initialized, so keep track of lookup misses
if (!mFaceNameListsInitialized) {
if (!mFaceNamesMissed) {
mFaceNamesMissed = MakeUnique<nsTHashSet<nsCString>>(2);
}
mFaceNamesMissed->Insert(aFaceName);
}
}
return lookup;
}
gfxFontEntry* gfxPlatformFontList::LookupInSharedFaceNameList(
const nsACString& aFaceName, WeightRange aWeightForEntry,
StretchRange aStretchForEntry, SlantStyleRange aStyleForEntry) {
nsAutoCString keyName(aFaceName);
ToLowerCase(keyName);
fontlist::FontList* list = SharedFontList();
fontlist::Family* family = nullptr;
fontlist::Face* face = nullptr;
if (list->NumLocalFaces()) {
fontlist::LocalFaceRec* rec = list->FindLocalFace(keyName);
if (rec) {
auto* families = list->Families();
if (families) {
family = &families[rec->mFamilyIndex];
face = static_cast<fontlist::Face*>(
family->Faces(list)[rec->mFaceIndex].ToPtr(list));
}
}
} else {
list->SearchForLocalFace(keyName, &family, &face);
}
if (!face || !family) {
return nullptr;
}
gfxFontEntry* fe = CreateFontEntry(face, family);
if (fe) {
fe->mIsLocalUserFont = true;
fe->mWeightRange = aWeightForEntry;
fe->mStretchRange = aStretchForEntry;
fe->mStyleRange = aStyleForEntry;
}
return fe;
}
void gfxPlatformFontList::LoadBadUnderlineList() {
gfxFontUtils::GetPrefsFontList("font.blacklist.underline_offset",
mBadUnderlineFamilyNames);
for (auto& fam : mBadUnderlineFamilyNames) {
ToLowerCase(fam);
}
mBadUnderlineFamilyNames.Compact();
mBadUnderlineFamilyNames.Sort();
}
void gfxPlatformFontList::UpdateFontList(bool aFullRebuild) {
MOZ_ASSERT(NS_IsMainThread());
if (aFullRebuild) {
InitFontList();
RebuildLocalFonts();
} else {
// The font list isn't being fully rebuilt, we're just being notified that
// character maps have been updated and so font fallback needs to be re-
// done. We only care about this if we have previously encountered a
// fallback that required cmaps that were not yet available, and so we
// asked for the async cmap loader to run.
if (mStartedLoadingCmapsFrom != 0xffffffffu) {
InitializeCodepointsWithNoFonts();
mStartedLoadingCmapsFrom = 0xffffffffu;
gfxPlatform::ForceGlobalReflow();
}
}
}
bool gfxPlatformFontList::IsVisibleToCSS(const gfxFontFamily& aFamily) const {
return aFamily.Visibility() <= mVisibilityLevel ||
IsFontFamilyWhitelistActive();
}
bool gfxPlatformFontList::IsVisibleToCSS(
const fontlist::Family& aFamily) const {
return aFamily.Visibility() <= mVisibilityLevel ||
IsFontFamilyWhitelistActive();
}
void gfxPlatformFontList::GetFontList(nsAtom* aLangGroup,
const nsACString& aGenericFamily,
nsTArray<nsString>& aListOfFonts) {
if (SharedFontList()) {
fontlist::FontList* list = SharedFontList();
const fontlist::Family* families = list->Families();
if (families) {
for (uint32_t i = 0; i < list->NumFamilies(); i++) {
auto& f = families[i];
if (!IsVisibleToCSS(f) || f.IsAltLocaleFamily()) {
continue;
}
// XXX TODO: filter families for aGenericFamily, if supported by
// platform
aListOfFonts.AppendElement(
NS_ConvertUTF8toUTF16(list->LocalizedFamilyName(&f)));
}
}
return;
}
MutexAutoLock lock(mFontFamiliesMutex);
for (const RefPtr<gfxFontFamily>& family : mFontFamilies.Values()) {
if (!IsVisibleToCSS(*family)) {
continue;
}
if (family->FilterForFontList(aLangGroup, aGenericFamily)) {
nsAutoCString localizedFamilyName;
family->LocalizedName(localizedFamilyName);
aListOfFonts.AppendElement(NS_ConvertUTF8toUTF16(localizedFamilyName));
}
}
aListOfFonts.Sort();
aListOfFonts.Compact();
}
void gfxPlatformFontList::GetFontFamilyList(
nsTArray<RefPtr<gfxFontFamily>>& aFamilyArray) {
MOZ_ASSERT(aFamilyArray.IsEmpty());
// This doesn't use ToArray, because the caller passes an AutoTArray.
aFamilyArray.SetCapacity(mFontFamilies.Count());
for (const auto& family : mFontFamilies.Values()) {
aFamilyArray.AppendElement(family);
}
}
gfxFont* gfxPlatformFontList::SystemFindFontForChar(
uint32_t aCh, uint32_t aNextCh, Script aRunScript,
eFontPresentation aPresentation, const gfxFontStyle* aStyle,
FontVisibility* aVisibility, FontMatchingStats* aFontMatchingStats) {
MOZ_ASSERT(!mCodepointsWithNoFonts.test(aCh),
"don't call for codepoints already known to be unsupported");
// Try to short-circuit font fallback for U+FFFD, used to represent
// encoding errors: just use cached family from last time U+FFFD was seen.
// This helps speed up pages with lots of encoding errors, binary-as-text,
// etc.
if (aCh == 0xFFFD) {
gfxFontEntry* fontEntry = nullptr;
if (mReplacementCharFallbackFamily.mIsShared &&
mReplacementCharFallbackFamily.mShared) {
fontlist::Face* face =
mReplacementCharFallbackFamily.mShared->FindFaceForStyle(
SharedFontList(), *aStyle);
if (face) {
fontEntry =
GetOrCreateFontEntry(face, mReplacementCharFallbackFamily.mShared);
*aVisibility = mReplacementCharFallbackFamily.mShared->Visibility();
}
} else if (!mReplacementCharFallbackFamily.mIsShared &&
mReplacementCharFallbackFamily.mUnshared) {
fontEntry =
mReplacementCharFallbackFamily.mUnshared->FindFontForStyle(*aStyle);
*aVisibility = mReplacementCharFallbackFamily.mUnshared->Visibility();
}
// this should never fail, as we must have found U+FFFD in order to set
// mReplacementCharFallbackFamily at all, but better play it safe
if (fontEntry && fontEntry->HasCharacter(aCh)) {
return fontEntry->FindOrMakeFont(aStyle);
}
}
TimeStamp start = TimeStamp::Now();
// search commonly available fonts
bool common = true;
FontFamily fallbackFamily;
gfxFont* candidate = CommonFontFallback(
aCh, aNextCh, aRunScript, aPresentation, aStyle, fallbackFamily);
gfxFont* font = nullptr;
if (candidate) {
if (aPresentation == eFontPresentation::Any) {
font = candidate;
} else {
bool hasColorGlyph = candidate->HasColorGlyphFor(aCh, aNextCh);
if (hasColorGlyph == PrefersColor(aPresentation)) {
font = candidate;
}
}
}
// If we didn't find a common font, or it was not the preferred type (color
// or monochrome), do system-wide fallback (except for specials).
uint32_t cmapCount = 0;
if (!font) {
common = false;
font = GlobalFontFallback(aCh, aNextCh, aRunScript, aPresentation, aStyle,
cmapCount, fallbackFamily, aFontMatchingStats);
// If the font we found doesn't match the requested type, and we also found
// a candidate above, prefer that one.
if (font && aPresentation != eFontPresentation::Any && candidate) {
bool hasColorGlyph = font->HasColorGlyphFor(aCh, aNextCh);
if (hasColorGlyph != PrefersColor(aPresentation)) {
// We're discarding `font` and using `candidate` instead, so ensure
// `font` is known to the global cache expiration tracker.
RefPtr<gfxFont> autoRefDeref(font);
font = candidate;
}
}
}
TimeDuration elapsed = TimeStamp::Now() - start;
LogModule* log = gfxPlatform::GetLog(eGfxLog_textrun);
if (MOZ_UNLIKELY(MOZ_LOG_TEST(log, LogLevel::Warning))) {
Script script = mozilla::unicode::GetScriptCode(aCh);
MOZ_LOG(log, LogLevel::Warning,
("(textrun-systemfallback-%s) char: u+%6.6x "
"script: %d match: [%s]"
" time: %dus cmaps: %d\n",
(common ? "common" : "global"), aCh, static_cast<int>(script),
(font ? font->GetFontEntry()->Name().get() : "<none>"),
int32_t(elapsed.ToMicroseconds()), cmapCount));
}
// no match? add to set of non-matching codepoints
if (!font) {
mCodepointsWithNoFonts.set(aCh);
} else {
*aVisibility = fallbackFamily.mIsShared
? fallbackFamily.mShared->Visibility()
: fallbackFamily.mUnshared->Visibility();
if (aCh == 0xFFFD) {
mReplacementCharFallbackFamily = fallbackFamily;
}
}
// track system fallback time
static bool first = true;
int32_t intElapsed =
int32_t(first ? elapsed.ToMilliseconds() : elapsed.ToMicroseconds());
Telemetry::Accumulate((first ? Telemetry::SYSTEM_FONT_FALLBACK_FIRST
: Telemetry::SYSTEM_FONT_FALLBACK),
intElapsed);
first = false;
// track the script for which fallback occurred (incremented one make it
// 1-based)
Telemetry::Accumulate(Telemetry::SYSTEM_FONT_FALLBACK_SCRIPT,
int(aRunScript) + 1);
return font;
}
#define NUM_FALLBACK_FONTS 8
gfxFont* gfxPlatformFontList::CommonFontFallback(
uint32_t aCh, uint32_t aNextCh, Script aRunScript,
eFontPresentation aPresentation, const gfxFontStyle* aMatchStyle,
FontFamily& aMatchedFamily) {
AutoTArray<const char*, NUM_FALLBACK_FONTS> defaultFallbacks;
gfxPlatform::GetPlatform()->GetCommonFallbackFonts(
aCh, aRunScript, aPresentation, defaultFallbacks);
GlobalFontMatch data(aCh, aNextCh, *aMatchStyle, aPresentation);
if (SharedFontList()) {
for (const auto name : defaultFallbacks) {
fontlist::Family* family = FindSharedFamily(nsDependentCString(name));
if (!family || !IsVisibleToCSS(*family)) {
continue;
}
// XXX(jfkthame) Should we fire the async cmap-loader here, or let it
// always do a potential sync initialization of the family?
family->SearchAllFontsForChar(SharedFontList(), &data);
if (data.mBestMatch) {
aMatchedFamily = FontFamily(family);
return data.mBestMatch->FindOrMakeFont(aMatchStyle);
}
}
} else {
for (const auto name : defaultFallbacks) {
gfxFontFamily* fallback =
FindFamilyByCanonicalName(nsDependentCString(name));
if (!fallback || !IsVisibleToCSS(*fallback)) {
continue;
}
fallback->FindFontForChar(&data);
if (data.mBestMatch) {
aMatchedFamily = FontFamily(fallback);
return data.mBestMatch->FindOrMakeFont(aMatchStyle);
}
}
}
return nullptr;
}
gfxFont* gfxPlatformFontList::GlobalFontFallback(
uint32_t aCh, uint32_t aNextCh, Script aRunScript,
eFontPresentation aPresentation, const gfxFontStyle* aMatchStyle,
uint32_t& aCmapCount, FontFamily& aMatchedFamily,
FontMatchingStats* aFontMatchingStats) {
bool useCmaps = IsFontFamilyWhitelistActive() ||
gfxPlatform::GetPlatform()->UseCmapsDuringSystemFallback();
FontVisibility rejectedFallbackVisibility = FontVisibility::Unknown;
if (!useCmaps) {
// Allow platform-specific fallback code to try and find a usable font
gfxFontEntry* fe = PlatformGlobalFontFallback(aCh, aRunScript, aMatchStyle,
aMatchedFamily);
if (fe) {
if (aMatchedFamily.mIsShared) {
if (IsVisibleToCSS(*aMatchedFamily.mShared)) {
gfxFont* font = fe->FindOrMakeFont(aMatchStyle);
if (font) {
if (aPresentation == eFontPresentation::Any) {
return font;
}
bool hasColorGlyph = font->HasColorGlyphFor(aCh, aNextCh);
if (hasColorGlyph == PrefersColor(aPresentation)) {
return font;
}
// If we don't use this font, we need to touch its refcount
// to trigger gfxFontCache expiration tracking.
RefPtr<gfxFont> autoRefDeref(font);
}
}
rejectedFallbackVisibility = aMatchedFamily.mShared->Visibility();
} else {
if (IsVisibleToCSS(*aMatchedFamily.mUnshared)) {
gfxFont* font = fe->FindOrMakeFont(aMatchStyle);
if (font) {
if (aPresentation == eFontPresentation::Any) {
return font;
}
bool hasColorGlyph = font->HasColorGlyphFor(aCh, aNextCh);
if (hasColorGlyph == PrefersColor(aPresentation)) {
return font;
}
RefPtr<gfxFont> autoRefDeref(font);
}
}
rejectedFallbackVisibility = aMatchedFamily.mUnshared->Visibility();
}
}
}
// otherwise, try to find it among local fonts
GlobalFontMatch data(aCh, aNextCh, *aMatchStyle, aPresentation);
if (SharedFontList()) {
fontlist::Family* families = SharedFontList()->Families();
if (families) {
for (uint32_t i = 0; i < SharedFontList()->NumFamilies(); i++) {
fontlist::Family& family = families[i];
if (!IsVisibleToCSS(family)) {
continue;
}
if (!family.IsFullyInitialized() &&
StaticPrefs::gfx_font_rendering_fallback_async() &&
!XRE_IsParentProcess()) {
// Start loading all the missing charmaps; but this is async,
// so for now we just continue, ignoring this family.
StartCmapLoadingFromFamily(i);
} else {
family.SearchAllFontsForChar(SharedFontList(), &data);
if (data.mMatchDistance == 0.0) {
// no better style match is possible, so stop searching
break;
}
}
}
if (data.mBestMatch) {
aMatchedFamily = FontFamily(data.mMatchedSharedFamily);
return data.mBestMatch->FindOrMakeFont(aMatchStyle);
}
}
} else {
// iterate over all font families to find a font that support the
// character
for (const RefPtr<gfxFontFamily>& family : mFontFamilies.Values()) {
if (!IsVisibleToCSS(*family)) {
continue;
}
// evaluate all fonts in this family for a match
family->FindFontForChar(&data);
if (data.mMatchDistance == 0.0) {
// no better style match is possible, so stop searching
break;
}
}
aCmapCount = data.mCmapsTested;
if (data.mBestMatch) {
aMatchedFamily = FontFamily(data.mMatchedFamily);
return data.mBestMatch->FindOrMakeFont(aMatchStyle);
}
}
if (aFontMatchingStats) {
if (rejectedFallbackVisibility == FontVisibility::LangPack) {
aFontMatchingStats->mFallbacks |= FallbackTypes::MissingFontLangPack;
} else if (rejectedFallbackVisibility == FontVisibility::User) {
aFontMatchingStats->mFallbacks |= FallbackTypes::MissingFontUser;
}
}
return nullptr;
}
class StartCmapLoadingRunnable : public mozilla::Runnable {
public:
explicit StartCmapLoadingRunnable(uint32_t aStartIndex)
: Runnable("gfxPlatformFontList::StartCmapLoadingRunnable"),
mStartIndex(aStartIndex) {}
NS_IMETHOD Run() override {
auto* pfl = gfxPlatformFontList::PlatformFontList();
auto* list = pfl->SharedFontList();
if (!list) {
return NS_OK;
}
if (mStartIndex >= list->NumFamilies()) {
return NS_OK;
}
if (XRE_IsParentProcess()) {
pfl->StartCmapLoading(list->GetGeneration(), mStartIndex);
} else {
dom::ContentChild::GetSingleton()->SendStartCmapLoading(
list->GetGeneration(), mStartIndex);
}
return NS_OK;
}
private:
uint32_t mStartIndex;
};
void gfxPlatformFontList::StartCmapLoadingFromFamily(uint32_t aStartIndex) {
if (aStartIndex > mStartedLoadingCmapsFrom) {
// We already initiated cmap-loading from somewhere earlier in the list;
// no need to do it again here.
return;
}
mStartedLoadingCmapsFrom = aStartIndex;
// If we're already on the main thread, don't bother dispatching a runnable
// here to kick off the loading process, just do it directly.
if (NS_IsMainThread()) {
auto* list = SharedFontList();
if (XRE_IsParentProcess()) {
StartCmapLoading(list->GetGeneration(), aStartIndex);
} else {
dom::ContentChild::GetSingleton()->SendStartCmapLoading(
list->GetGeneration(), aStartIndex);
}
} else {
NS_DispatchToMainThread(new StartCmapLoadingRunnable(aStartIndex));
}
}
class LoadCmapsRunnable : public CancelableRunnable {
class WillShutdownObserver : public nsIObserver {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
explicit WillShutdownObserver(LoadCmapsRunnable* aRunnable)
: mRunnable(aRunnable) {}
void Remove() {
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
if (obs) {
obs->RemoveObserver(this, NS_XPCOM_WILL_SHUTDOWN_OBSERVER_ID);
}
mRunnable = nullptr;
}
protected:
virtual ~WillShutdownObserver() = default;
LoadCmapsRunnable* mRunnable;
};
public:
explicit LoadCmapsRunnable(uint32_t aGeneration, uint32_t aFamilyIndex)
: CancelableRunnable("gfxPlatformFontList::LoadCmapsRunnable"),
mGeneration(aGeneration),
mStartIndex(aFamilyIndex),
mIndex(aFamilyIndex) {
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
if (obs) {
mObserver = new WillShutdownObserver(this);
obs->AddObserver(mObserver, NS_XPCOM_WILL_SHUTDOWN_OBSERVER_ID, false);
}
}
virtual ~LoadCmapsRunnable() {
if (mObserver) {
mObserver->Remove();
}
}
// Reset the current family index, if the value passed is earlier than our
// original starting position. We don't "reset" if it would move the current
// position forward, or back into the already-scanned range.
// We could optimize further by remembering the current position reached,
// and then skipping ahead from the original start, but it doesn't seem worth
// extra complexity for a task that usually only happens once, and already-
// processed families will be skipped pretty quickly in Run() anyhow.
void MaybeResetIndex(uint32_t aFamilyIndex) {
if (aFamilyIndex < mStartIndex) {
mStartIndex = aFamilyIndex;
mIndex = aFamilyIndex;
}
}
nsresult Cancel() override {
mIsCanceled = true;
return NS_OK;
}
NS_IMETHOD Run() override {
if (mIsCanceled) {
return NS_OK;
}
auto* pfl = gfxPlatformFontList::PlatformFontList();
auto* list = pfl->SharedFontList();
MOZ_ASSERT(list);
if (!list) {
return NS_OK;
}
if (mGeneration != list->GetGeneration()) {
return NS_OK;
}
uint32_t numFamilies = list->NumFamilies();
if (mIndex >= numFamilies) {
return NS_OK;
}
auto* families = list->Families();
// Skip any families that are already initialized.
while (mIndex < numFamilies && families[mIndex].IsFullyInitialized()) {
++mIndex;
}
// Fully process one family, and advance index.
if (mIndex < numFamilies) {
Unused << pfl->InitializeFamily(&families[mIndex], true);
++mIndex;
}
// If there are more families to initialize, post ourselves back to the
// idle queue to handle the next one; otherwise we're finished and we need
// to notify content processes to update their rendering.
if (mIndex < numFamilies) {
RefPtr<CancelableRunnable> task = this;
NS_DispatchToMainThreadQueue(task.forget(), EventQueuePriority::Idle);
} else {
pfl->CancelLoadCmapsTask();
pfl->InitializeCodepointsWithNoFonts();
dom::ContentParent::NotifyUpdatedFonts(false);
}
return NS_OK;
}
private:
uint32_t mGeneration;
uint32_t mStartIndex;
uint32_t mIndex;
bool mIsCanceled = false;
RefPtr<WillShutdownObserver> mObserver;
};
NS_IMPL_ISUPPORTS(LoadCmapsRunnable::WillShutdownObserver, nsIObserver)
NS_IMETHODIMP
LoadCmapsRunnable::WillShutdownObserver::Observe(nsISupports* aSubject,
const char* aTopic,
const char16_t* aData) {
if (!nsCRT::strcmp(aTopic, NS_XPCOM_WILL_SHUTDOWN_OBSERVER_ID)) {
if (mRunnable) {
mRunnable->Cancel();
}
} else {
MOZ_ASSERT_UNREACHABLE("unexpected notification topic");
}
return NS_OK;
}
void gfxPlatformFontList::StartCmapLoading(uint32_t aGeneration,
uint32_t aStartIndex) {
MOZ_RELEASE_ASSERT(XRE_IsParentProcess());
if (aGeneration != SharedFontList()->GetGeneration()) {
return;
}
if (AppShutdown::IsShuttingDown()) {
return;
}
if (mLoadCmapsRunnable) {
// We already have a runnable; just make sure it covers the full range of
// families needed.
static_cast<LoadCmapsRunnable*>(mLoadCmapsRunnable.get())
->MaybeResetIndex(aStartIndex);
return;
}
mLoadCmapsRunnable = new LoadCmapsRunnable(aGeneration, aStartIndex);
RefPtr<CancelableRunnable> task = mLoadCmapsRunnable;
NS_DispatchToMainThreadQueue(task.forget(), EventQueuePriority::Idle);
}
gfxFontFamily* gfxPlatformFontList::CheckFamily(gfxFontFamily* aFamily) {
if (aFamily && !aFamily->HasStyles()) {
aFamily->FindStyleVariations();
}
if (aFamily && aFamily->GetFontList().Length() == 0) {
// failed to load any faces for this family, so discard it
nsAutoCString key;
GenerateFontListKey(aFamily->Name(), key);
mFontFamilies.Remove(key);
return nullptr;
}
return aFamily;
}
bool gfxPlatformFontList::FindAndAddFamilies(
StyleGenericFontFamily aGeneric, const nsACString& aFamily,
nsTArray<FamilyAndGeneric>* aOutput, FindFamiliesFlags aFlags,
gfxFontStyle* aStyle, nsAtom* aLanguage, gfxFloat aDevToCssSize) {
nsAutoCString key;
GenerateFontListKey(aFamily, key);
bool allowHidden = bool(aFlags & FindFamiliesFlags::eSearchHiddenFamilies);
if (SharedFontList()) {
fontlist::Family* family = SharedFontList()->FindFamily(key);
// If not found, and other family names have not yet been initialized,
// initialize the rest of the list and try again. This is done lazily
// since reading name table entries is expensive.
// Although ASCII localized family names are possible they don't occur
// in practice, so avoid pulling in names at startup.
if (!family && !mOtherFamilyNamesInitialized) {
bool triggerLoading = true;
bool mayDefer =
!(aFlags & FindFamiliesFlags::eForceOtherFamilyNamesLoading);
if (IsAscii(key)) {
// If `key` is an ASCII name, only trigger loading if it includes a
// space, and the "base" name (up to the last space) exists as a known
// family, so that this might be a legacy styled-family name.
const char* data = key.BeginReading();
int32_t index = key.Length();
while (--index > 0) {
if (data[index] == ' ') {
break;
}
}
if (index <= 0 ||
!SharedFontList()->FindFamily(nsAutoCString(key.get(), index))) {
triggerLoading = false;
}
}
if (triggerLoading) {
if (InitOtherFamilyNames(mayDefer)) {
family = SharedFontList()->FindFamily(key);
}
}
if (!family && !mOtherFamilyNamesInitialized &&
!(aFlags & FindFamiliesFlags::eNoAddToNamesMissedWhenSearching)) {
// localized family names load timed out, add name to list of
// names to check after localized names are loaded
if (!mOtherNamesMissed) {
mOtherNamesMissed = MakeUnique<nsTHashSet<nsCString>>(2);
}
mOtherNamesMissed->Insert(key);
}
}
// Check whether the family we found is actually allowed to be looked up,
// according to current font-visibility prefs.
if (family &&
(IsVisibleToCSS(*family) || (allowHidden && family->IsHidden()))) {
aOutput->AppendElement(FamilyAndGeneric(family, aGeneric));
return true;
}
return false;
}
NS_ASSERTION(mFontFamilies.Count() != 0,
"system font list was not initialized correctly");
// lookup in canonical (i.e. English) family name list
gfxFontFamily* familyEntry = mFontFamilies.GetWeak(key);
if (familyEntry && !IsVisibleToCSS(*familyEntry) &&
!(allowHidden && familyEntry->IsHidden())) {
return false;
}
// if not found, lookup in other family names list (mostly localized names)
if (!familyEntry) {
familyEntry = mOtherFamilyNames.GetWeak(key);
}
if (familyEntry && !IsVisibleToCSS(*familyEntry) &&
!(allowHidden && familyEntry->IsHidden())) {
return false;
}
// if still not found and other family names not yet fully initialized,
// initialize the rest of the list and try again. this is done lazily
// since reading name table entries is expensive.
// although ASCII localized family names are possible they don't occur
// in practice so avoid pulling in names at startup
if (!familyEntry && !mOtherFamilyNamesInitialized && !IsAscii(aFamily)) {
InitOtherFamilyNames(
!(aFlags & FindFamiliesFlags::eForceOtherFamilyNamesLoading));
familyEntry = mOtherFamilyNames.GetWeak(key);
if (!familyEntry && !mOtherFamilyNamesInitialized &&
!(aFlags & FindFamiliesFlags::eNoAddToNamesMissedWhenSearching)) {
// localized family names load timed out, add name to list of
// names to check after localized names are loaded
if (!mOtherNamesMissed) {
mOtherNamesMissed = MakeUnique<nsTHashSet<nsCString>>(2);
}
mOtherNamesMissed->Insert(key);
}
if (familyEntry && !IsVisibleToCSS(*familyEntry) &&
!(allowHidden && familyEntry->IsHidden())) {
return false;
}
}
familyEntry = CheckFamily(familyEntry);
// If we failed to find the requested family, check for a space in the
// name; if found, and if the "base" name (up to the last space) exists
// as a family, then this might be a legacy GDI-style family name for
// an additional weight/width. Try searching the faces of the base family
// and create any corresponding legacy families.
if (!familyEntry &&
!(aFlags & FindFamiliesFlags::eNoSearchForLegacyFamilyNames)) {
// We don't have nsAString::RFindChar, so look for a space manually
const char* data = aFamily.BeginReading();
int32_t index = aFamily.Length();
while (--index > 0) {
if (data[index] == ' ') {
break;
}
}
if (index > 0) {
gfxFontFamily* base =
FindUnsharedFamily(Substring(aFamily, 0, index),
FindFamiliesFlags::eNoSearchForLegacyFamilyNames);
// If we found the "base" family name, and if it has members with
// legacy names, this will add corresponding font-family entries to
// the mOtherFamilyNames list; then retry the legacy-family search.
if (base && base->CheckForLegacyFamilyNames(this)) {
familyEntry = mOtherFamilyNames.GetWeak(key);
}
if (familyEntry && !IsVisibleToCSS(*familyEntry) &&
!(allowHidden && familyEntry->IsHidden())) {
return false;
}
}
}