From c84b59dee7fef73c372c6c185c612b0dbddd9ca0 Mon Sep 17 00:00:00 2001 From: John Daggett Date: Tue, 29 Sep 2015 10:51:28 +0900 Subject: [PATCH] Bug 1182361 p1 - move old generic lookup methods into gfxPangoFontGroup. r=heycam --- gfx/thebes/gfxFontconfigFonts.cpp | 144 ++++++++++++++++++++++++++++-- gfx/thebes/gfxFontconfigFonts.h | 24 ++++- gfx/thebes/gfxTextRun.cpp | 18 ++-- gfx/thebes/gfxTextRun.h | 9 +- 4 files changed, 170 insertions(+), 25 deletions(-) diff --git a/gfx/thebes/gfxFontconfigFonts.cpp b/gfx/thebes/gfxFontconfigFonts.cpp index 8e10548b2b45..f1ea2f0bdb8d 100644 --- a/gfx/thebes/gfxFontconfigFonts.cpp +++ b/gfx/thebes/gfxFontconfigFonts.cpp @@ -25,6 +25,9 @@ #include "gfxFontconfigUtils.h" #include "gfxUserFontSet.h" #include "gfxFontConstants.h" +#include "nsGkAtoms.h" +#include "nsILanguageAtomService.h" +#include "nsServiceManagerUtils.h" #include #include @@ -1281,9 +1284,140 @@ gfxPangoFontGroup::Copy(const gfxFontStyle *aStyle) } void -gfxPangoFontGroup::FindPlatformFont(const nsAString& fontName, - bool aUseFontSet, - void *aClosure) +gfxPangoFontGroup::FindGenericFontsPFG(FontFamilyType aGenericType, + nsIAtom *aLanguage, + void *aClosure) +{ + nsAutoTArray resolvedGenerics; + ResolveGenericFontNamesPFG(aGenericType, aLanguage, resolvedGenerics); + uint32_t g = 0, numGenerics = resolvedGenerics.Length(); + for (g = 0; g < numGenerics; g++) { + FindPlatformFontPFG(resolvedGenerics[g], false, aClosure); + } +} + +/* static */ void +gfxPangoFontGroup::ResolveGenericFontNamesPFG(FontFamilyType aGenericType, + nsIAtom *aLanguage, + nsTArray& aGenericFamilies) +{ + static const char kGeneric_serif[] = "serif"; + static const char kGeneric_sans_serif[] = "sans-serif"; + static const char kGeneric_monospace[] = "monospace"; + static const char kGeneric_cursive[] = "cursive"; + static const char kGeneric_fantasy[] = "fantasy"; + + // treat -moz-fixed as monospace + if (aGenericType == eFamily_moz_fixed) { + aGenericType = eFamily_monospace; + } + + // type should be standard generic type at this point + NS_ASSERTION(aGenericType >= eFamily_serif && + aGenericType <= eFamily_fantasy, + "standard generic font family type required"); + + // create the lang string + nsIAtom *langGroupAtom = nullptr; + nsAutoCString langGroupString; + if (aLanguage) { + if (!gLangService) { + CallGetService(NS_LANGUAGEATOMSERVICE_CONTRACTID, &gLangService); + } + if (gLangService) { + nsresult rv; + langGroupAtom = gLangService->GetLanguageGroup(aLanguage, &rv); + } + } + if (!langGroupAtom) { + langGroupAtom = nsGkAtoms::Unicode; + } + langGroupAtom->ToUTF8String(langGroupString); + + // map generic type to string + const char *generic = nullptr; + switch (aGenericType) { + case eFamily_serif: + generic = kGeneric_serif; + break; + case eFamily_sans_serif: + generic = kGeneric_sans_serif; + break; + case eFamily_monospace: + generic = kGeneric_monospace; + break; + case eFamily_cursive: + generic = kGeneric_cursive; + break; + case eFamily_fantasy: + generic = kGeneric_fantasy; + break; + default: + break; + } + + if (!generic) { + return; + } + + aGenericFamilies.Clear(); + + // load family for "font.name.generic.lang" + nsAutoCString prefFontName("font.name."); + prefFontName.Append(generic); + prefFontName.Append('.'); + prefFontName.Append(langGroupString); + gfxFontUtils::AppendPrefsFontList(prefFontName.get(), + aGenericFamilies); + + // if lang has pref fonts, also load fonts for "font.name-list.generic.lang" + if (!aGenericFamilies.IsEmpty()) { + nsAutoCString prefFontListName("font.name-list."); + prefFontListName.Append(generic); + prefFontListName.Append('.'); + prefFontListName.Append(langGroupString); + gfxFontUtils::AppendPrefsFontList(prefFontListName.get(), + aGenericFamilies); + } + +#if 0 // dump out generic mappings + printf("%s ===> ", prefFontName.get()); + for (uint32_t k = 0; k < aGenericFamilies.Length(); k++) { + if (k > 0) printf(", "); + printf("%s", NS_ConvertUTF16toUTF8(aGenericFamilies[k]).get()); + } + printf("\n"); +#endif +} + +void gfxPangoFontGroup::EnumerateFontListPFG(nsIAtom *aLanguage, void *aClosure) +{ + // initialize fonts in the font family list + const nsTArray& fontlist = mFamilyList.GetFontlist(); + + // lookup fonts in the fontlist + uint32_t i, numFonts = fontlist.Length(); + for (i = 0; i < numFonts; i++) { + const FontFamilyName& name = fontlist[i]; + if (name.IsNamed()) { + FindPlatformFontPFG(name.mName, true, aClosure); + } else { + FindGenericFontsPFG(name.mType, aLanguage, aClosure); + } + } + + // if necessary, append default generic onto the end + if (mFamilyList.GetDefaultFontType() != eFamily_none && + !mFamilyList.HasDefaultGeneric()) { + FindGenericFontsPFG(mFamilyList.GetDefaultFontType(), + aLanguage, aClosure); + } +} + +void +gfxPangoFontGroup::FindPlatformFontPFG(const nsAString& fontName, + bool aUseFontSet, + void *aClosure) { nsTArray *list = static_cast*>(aClosure); @@ -1360,8 +1494,8 @@ gfxPangoFontGroup::MakeFontSet(PangoLanguage *aLang, gfxFloat aSizeAdjustFactor, } nsAutoTArray fcFamilyList; - EnumerateFontList(langGroup ? langGroup.get() : mStyle.language.get(), - &fcFamilyList); + EnumerateFontListPFG(langGroup ? langGroup.get() : mStyle.language.get(), + &fcFamilyList); // To consider: A fontset cache here could be helpful. diff --git a/gfx/thebes/gfxFontconfigFonts.h b/gfx/thebes/gfxFontconfigFonts.h index 44d9a7c24875..1a7c1dbf3b4d 100644 --- a/gfx/thebes/gfxFontconfigFonts.h +++ b/gfx/thebes/gfxFontconfigFonts.h @@ -94,9 +94,27 @@ private: return mSizeAdjustFactor; } - virtual void FindPlatformFont(const nsAString& aName, - bool aUseFontSet, - void *aClosure); + // old helper methods from gfxFontGroup, moved here so that those methods + // can be revamped without affecting the legacy code here + + // iterate over the fontlist, lookup names and expand generics + void EnumerateFontListPFG(nsIAtom *aLanguage, void *aClosure); + + // expand a generic to a list of specific names based on prefs + void FindGenericFontsPFG(mozilla::FontFamilyType aGenericType, + nsIAtom *aLanguage, + void *aClosure); + + // lookup and add a font with a given name (i.e. *not* a generic!) + void FindPlatformFontPFG(const nsAString& aName, + bool aUseFontSet, + void *aClosure); + + static void + ResolveGenericFontNamesPFG(mozilla::FontFamilyType aGenericType, + nsIAtom *aLanguage, + nsTArray& aGenericFamilies); + friend class gfxSystemFcFontEntry; static FT_Library GetFTLibrary(); diff --git a/gfx/thebes/gfxTextRun.cpp b/gfx/thebes/gfxTextRun.cpp index 49aba62e1591..c78f5df7d41a 100644 --- a/gfx/thebes/gfxTextRun.cpp +++ b/gfx/thebes/gfxTextRun.cpp @@ -1559,14 +1559,13 @@ gfxFontGroup::~gfxFontGroup() void gfxFontGroup::FindGenericFonts(FontFamilyType aGenericType, - nsIAtom *aLanguage, - void *aClosure) + nsIAtom *aLanguage) { nsAutoTArray resolvedGenerics; ResolveGenericFontNames(aGenericType, aLanguage, resolvedGenerics); uint32_t g = 0, numGenerics = resolvedGenerics.Length(); for (g = 0; g < numGenerics; g++) { - FindPlatformFont(resolvedGenerics[g], false, aClosure); + FindPlatformFont(resolvedGenerics[g], false); } } @@ -1664,7 +1663,7 @@ gfxFontGroup::ResolveGenericFontNames(FontFamilyType aGenericType, #endif } -void gfxFontGroup::EnumerateFontList(nsIAtom *aLanguage, void *aClosure) +void gfxFontGroup::EnumerateFontList(nsIAtom *aLanguage) { // initialize fonts in the font family list const nsTArray& fontlist = mFamilyList.GetFontlist(); @@ -1674,9 +1673,9 @@ void gfxFontGroup::EnumerateFontList(nsIAtom *aLanguage, void *aClosure) for (i = 0; i < numFonts; i++) { const FontFamilyName& name = fontlist[i]; if (name.IsNamed()) { - FindPlatformFont(name.mName, true, aClosure); + FindPlatformFont(name.mName, true); } else { - FindGenericFonts(name.mType, aLanguage, aClosure); + FindGenericFonts(name.mType, aLanguage); } } @@ -1684,8 +1683,7 @@ void gfxFontGroup::EnumerateFontList(nsIAtom *aLanguage, void *aClosure) if (mFamilyList.GetDefaultFontType() != eFamily_none && !mFamilyList.HasDefaultGeneric()) { FindGenericFonts(mFamilyList.GetDefaultFontType(), - aLanguage, - aClosure); + aLanguage); } } @@ -1706,9 +1704,7 @@ gfxFontGroup::BuildFontList() } void -gfxFontGroup::FindPlatformFont(const nsAString& aName, - bool aUseFontSet, - void *aClosure) +gfxFontGroup::FindPlatformFont(const nsAString& aName, bool aUseFontSet) { bool needsBold; gfxFontFamily *family = nullptr; diff --git a/gfx/thebes/gfxTextRun.h b/gfx/thebes/gfxTextRun.h index 75f7e3b377c3..d893439e91de 100644 --- a/gfx/thebes/gfxTextRun.h +++ b/gfx/thebes/gfxTextRun.h @@ -1118,17 +1118,14 @@ protected: // helper methods for looking up fonts // iterate over the fontlist, lookup names and expand generics - void EnumerateFontList(nsIAtom *aLanguage, void *aClosure = nullptr); + void EnumerateFontList(nsIAtom *aLanguage); // expand a generic to a list of specific names based on prefs void FindGenericFonts(mozilla::FontFamilyType aGenericType, - nsIAtom *aLanguage, - void *aClosure); + nsIAtom *aLanguage); // lookup and add a font with a given name (i.e. *not* a generic!) - virtual void FindPlatformFont(const nsAString& aName, - bool aUseFontSet, - void *aClosure); + void FindPlatformFont(const nsAString& aName, bool aUseFontSet); static nsILanguageAtomService* gLangService; };