Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef GFXPLATFORMFONTLIST_H_ |
michael@0 | 7 | #define GFXPLATFORMFONTLIST_H_ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsDataHashtable.h" |
michael@0 | 10 | #include "nsRefPtrHashtable.h" |
michael@0 | 11 | #include "nsTHashtable.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "gfxFontUtils.h" |
michael@0 | 14 | #include "gfxFontInfoLoader.h" |
michael@0 | 15 | #include "gfxFont.h" |
michael@0 | 16 | #include "gfxPlatform.h" |
michael@0 | 17 | |
michael@0 | 18 | #include "nsIMemoryReporter.h" |
michael@0 | 19 | #include "mozilla/Attributes.h" |
michael@0 | 20 | #include "mozilla/MemoryReporting.h" |
michael@0 | 21 | |
michael@0 | 22 | class CharMapHashKey : public PLDHashEntryHdr |
michael@0 | 23 | { |
michael@0 | 24 | public: |
michael@0 | 25 | typedef gfxCharacterMap* KeyType; |
michael@0 | 26 | typedef const gfxCharacterMap* KeyTypePointer; |
michael@0 | 27 | |
michael@0 | 28 | CharMapHashKey(const gfxCharacterMap *aCharMap) : |
michael@0 | 29 | mCharMap(const_cast<gfxCharacterMap*>(aCharMap)) |
michael@0 | 30 | { |
michael@0 | 31 | MOZ_COUNT_CTOR(CharMapHashKey); |
michael@0 | 32 | } |
michael@0 | 33 | CharMapHashKey(const CharMapHashKey& toCopy) : |
michael@0 | 34 | mCharMap(toCopy.mCharMap) |
michael@0 | 35 | { |
michael@0 | 36 | MOZ_COUNT_CTOR(CharMapHashKey); |
michael@0 | 37 | } |
michael@0 | 38 | ~CharMapHashKey() |
michael@0 | 39 | { |
michael@0 | 40 | MOZ_COUNT_DTOR(CharMapHashKey); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | gfxCharacterMap* GetKey() const { return mCharMap; } |
michael@0 | 44 | |
michael@0 | 45 | bool KeyEquals(const gfxCharacterMap *aCharMap) const { |
michael@0 | 46 | NS_ASSERTION(!aCharMap->mBuildOnTheFly && !mCharMap->mBuildOnTheFly, |
michael@0 | 47 | "custom cmap used in shared cmap hashtable"); |
michael@0 | 48 | // cmaps built on the fly never match |
michael@0 | 49 | if (aCharMap->mHash != mCharMap->mHash) |
michael@0 | 50 | { |
michael@0 | 51 | return false; |
michael@0 | 52 | } |
michael@0 | 53 | return mCharMap->Equals(aCharMap); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | static const gfxCharacterMap* KeyToPointer(gfxCharacterMap *aCharMap) { |
michael@0 | 57 | return aCharMap; |
michael@0 | 58 | } |
michael@0 | 59 | static PLDHashNumber HashKey(const gfxCharacterMap *aCharMap) { |
michael@0 | 60 | return aCharMap->mHash; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | enum { ALLOW_MEMMOVE = true }; |
michael@0 | 64 | |
michael@0 | 65 | protected: |
michael@0 | 66 | gfxCharacterMap *mCharMap; |
michael@0 | 67 | }; |
michael@0 | 68 | |
michael@0 | 69 | // gfxPlatformFontList is an abstract class for the global font list on the system; |
michael@0 | 70 | // concrete subclasses for each platform implement the actual interface to the system fonts. |
michael@0 | 71 | // This class exists because we cannot rely on the platform font-finding APIs to behave |
michael@0 | 72 | // in sensible/similar ways, particularly with rich, complex OpenType families, |
michael@0 | 73 | // so we do our own font family/style management here instead. |
michael@0 | 74 | |
michael@0 | 75 | // Much of this is based on the old gfxQuartzFontCache, but adapted for use on all platforms. |
michael@0 | 76 | |
michael@0 | 77 | struct FontListSizes { |
michael@0 | 78 | uint32_t mFontListSize; // size of the font list and dependent objects |
michael@0 | 79 | // (font family and face names, etc), but NOT |
michael@0 | 80 | // including the font table cache and the cmaps |
michael@0 | 81 | uint32_t mFontTableCacheSize; // memory used for the gfxFontEntry table caches |
michael@0 | 82 | uint32_t mCharMapsSize; // memory used for cmap coverage info |
michael@0 | 83 | }; |
michael@0 | 84 | |
michael@0 | 85 | class gfxUserFontSet; |
michael@0 | 86 | |
michael@0 | 87 | class gfxPlatformFontList : public gfxFontInfoLoader |
michael@0 | 88 | { |
michael@0 | 89 | public: |
michael@0 | 90 | static gfxPlatformFontList* PlatformFontList() { |
michael@0 | 91 | return sPlatformFontList; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | static nsresult Init() { |
michael@0 | 95 | NS_ASSERTION(!sPlatformFontList, "What's this doing here?"); |
michael@0 | 96 | gfxPlatform::GetPlatform()->CreatePlatformFontList(); |
michael@0 | 97 | if (!sPlatformFontList) { |
michael@0 | 98 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 99 | } |
michael@0 | 100 | return NS_OK; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | static void Shutdown() { |
michael@0 | 104 | delete sPlatformFontList; |
michael@0 | 105 | sPlatformFontList = nullptr; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | virtual ~gfxPlatformFontList(); |
michael@0 | 109 | |
michael@0 | 110 | // initialize font lists |
michael@0 | 111 | virtual nsresult InitFontList(); |
michael@0 | 112 | |
michael@0 | 113 | void GetFontList (nsIAtom *aLangGroup, |
michael@0 | 114 | const nsACString& aGenericFamily, |
michael@0 | 115 | nsTArray<nsString>& aListOfFonts); |
michael@0 | 116 | |
michael@0 | 117 | virtual bool ResolveFontName(const nsAString& aFontName, |
michael@0 | 118 | nsAString& aResolvedFontName); |
michael@0 | 119 | |
michael@0 | 120 | void UpdateFontList(); |
michael@0 | 121 | |
michael@0 | 122 | void ClearPrefFonts() { mPrefFonts.Clear(); } |
michael@0 | 123 | |
michael@0 | 124 | virtual void GetFontFamilyList(nsTArray<nsRefPtr<gfxFontFamily> >& aFamilyArray); |
michael@0 | 125 | |
michael@0 | 126 | virtual gfxFontEntry* |
michael@0 | 127 | SystemFindFontForChar(const uint32_t aCh, |
michael@0 | 128 | int32_t aRunScript, |
michael@0 | 129 | const gfxFontStyle* aStyle); |
michael@0 | 130 | |
michael@0 | 131 | // TODO: make this virtual, for lazily adding to the font list |
michael@0 | 132 | virtual gfxFontFamily* FindFamily(const nsAString& aFamily); |
michael@0 | 133 | |
michael@0 | 134 | gfxFontEntry* FindFontForFamily(const nsAString& aFamily, const gfxFontStyle* aStyle, bool& aNeedsBold); |
michael@0 | 135 | |
michael@0 | 136 | bool GetPrefFontFamilyEntries(eFontPrefLang aLangGroup, nsTArray<nsRefPtr<gfxFontFamily> > *array); |
michael@0 | 137 | void SetPrefFontFamilyEntries(eFontPrefLang aLangGroup, nsTArray<nsRefPtr<gfxFontFamily> >& array); |
michael@0 | 138 | |
michael@0 | 139 | // name lookup table methods |
michael@0 | 140 | |
michael@0 | 141 | void AddOtherFamilyName(gfxFontFamily *aFamilyEntry, nsAString& aOtherFamilyName); |
michael@0 | 142 | |
michael@0 | 143 | void AddFullname(gfxFontEntry *aFontEntry, nsAString& aFullname); |
michael@0 | 144 | |
michael@0 | 145 | void AddPostscriptName(gfxFontEntry *aFontEntry, nsAString& aPostscriptName); |
michael@0 | 146 | |
michael@0 | 147 | bool NeedFullnamePostscriptNames() { return mExtraNames != nullptr; } |
michael@0 | 148 | |
michael@0 | 149 | // pure virtual functions, to be provided by concrete subclasses |
michael@0 | 150 | |
michael@0 | 151 | // get the system default font family |
michael@0 | 152 | virtual gfxFontFamily* GetDefaultFont(const gfxFontStyle* aStyle) = 0; |
michael@0 | 153 | |
michael@0 | 154 | // look up a font by name on the host platform |
michael@0 | 155 | virtual gfxFontEntry* LookupLocalFont(const gfxProxyFontEntry *aProxyEntry, |
michael@0 | 156 | const nsAString& aFontName) = 0; |
michael@0 | 157 | |
michael@0 | 158 | // create a new platform font from downloaded data (@font-face) |
michael@0 | 159 | // this method is responsible to ensure aFontData is NS_Free()'d |
michael@0 | 160 | virtual gfxFontEntry* MakePlatformFont(const gfxProxyFontEntry *aProxyEntry, |
michael@0 | 161 | const uint8_t *aFontData, |
michael@0 | 162 | uint32_t aLength) = 0; |
michael@0 | 163 | |
michael@0 | 164 | // get the standard family name on the platform for a given font name |
michael@0 | 165 | // (platforms may override, eg Mac) |
michael@0 | 166 | virtual bool GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName); |
michael@0 | 167 | |
michael@0 | 168 | virtual void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf, |
michael@0 | 169 | FontListSizes* aSizes) const; |
michael@0 | 170 | virtual void AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf, |
michael@0 | 171 | FontListSizes* aSizes) const; |
michael@0 | 172 | |
michael@0 | 173 | // search for existing cmap that matches the input |
michael@0 | 174 | // return the input if no match is found |
michael@0 | 175 | gfxCharacterMap* FindCharMap(gfxCharacterMap *aCmap); |
michael@0 | 176 | |
michael@0 | 177 | // add a cmap to the shared cmap set |
michael@0 | 178 | gfxCharacterMap* AddCmap(const gfxCharacterMap *aCharMap); |
michael@0 | 179 | |
michael@0 | 180 | // remove the cmap from the shared cmap set |
michael@0 | 181 | void RemoveCmap(const gfxCharacterMap *aCharMap); |
michael@0 | 182 | |
michael@0 | 183 | // keep track of userfont sets to notify when global fontlist changes occur |
michael@0 | 184 | void AddUserFontSet(gfxUserFontSet *aUserFontSet) { |
michael@0 | 185 | mUserFontSetList.PutEntry(aUserFontSet); |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | void RemoveUserFontSet(gfxUserFontSet *aUserFontSet) { |
michael@0 | 189 | mUserFontSetList.RemoveEntry(aUserFontSet); |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | static const gfxFontEntry::ScriptRange sComplexScriptRanges[]; |
michael@0 | 193 | |
michael@0 | 194 | protected: |
michael@0 | 195 | class MemoryReporter MOZ_FINAL : public nsIMemoryReporter |
michael@0 | 196 | { |
michael@0 | 197 | public: |
michael@0 | 198 | NS_DECL_ISUPPORTS |
michael@0 | 199 | NS_DECL_NSIMEMORYREPORTER |
michael@0 | 200 | }; |
michael@0 | 201 | |
michael@0 | 202 | gfxPlatformFontList(bool aNeedFullnamePostscriptNames = true); |
michael@0 | 203 | |
michael@0 | 204 | static gfxPlatformFontList *sPlatformFontList; |
michael@0 | 205 | |
michael@0 | 206 | static PLDHashOperator FindFontForCharProc(nsStringHashKey::KeyType aKey, |
michael@0 | 207 | nsRefPtr<gfxFontFamily>& aFamilyEntry, |
michael@0 | 208 | void* userArg); |
michael@0 | 209 | |
michael@0 | 210 | // returns default font for a given character, null otherwise |
michael@0 | 211 | gfxFontEntry* CommonFontFallback(const uint32_t aCh, |
michael@0 | 212 | int32_t aRunScript, |
michael@0 | 213 | const gfxFontStyle* aMatchStyle, |
michael@0 | 214 | gfxFontFamily** aMatchedFamily); |
michael@0 | 215 | |
michael@0 | 216 | // search fonts system-wide for a given character, null otherwise |
michael@0 | 217 | virtual gfxFontEntry* GlobalFontFallback(const uint32_t aCh, |
michael@0 | 218 | int32_t aRunScript, |
michael@0 | 219 | const gfxFontStyle* aMatchStyle, |
michael@0 | 220 | uint32_t& aCmapCount, |
michael@0 | 221 | gfxFontFamily** aMatchedFamily); |
michael@0 | 222 | |
michael@0 | 223 | // whether system-based font fallback is used or not |
michael@0 | 224 | // if system fallback is used, no need to load all cmaps |
michael@0 | 225 | virtual bool UsesSystemFallback() { return false; } |
michael@0 | 226 | |
michael@0 | 227 | // verifies that a family contains a non-zero font count |
michael@0 | 228 | gfxFontFamily* CheckFamily(gfxFontFamily *aFamily); |
michael@0 | 229 | |
michael@0 | 230 | // initialize localized family names |
michael@0 | 231 | void InitOtherFamilyNames(); |
michael@0 | 232 | |
michael@0 | 233 | static PLDHashOperator |
michael@0 | 234 | InitOtherFamilyNamesProc(nsStringHashKey::KeyType aKey, |
michael@0 | 235 | nsRefPtr<gfxFontFamily>& aFamilyEntry, |
michael@0 | 236 | void* userArg); |
michael@0 | 237 | |
michael@0 | 238 | // search through font families, looking for a given name, initializing |
michael@0 | 239 | // facename lists along the way. first checks all families with names |
michael@0 | 240 | // close to face name, then searchs all families if not found. |
michael@0 | 241 | gfxFontEntry* SearchFamiliesForFaceName(const nsAString& aFaceName); |
michael@0 | 242 | |
michael@0 | 243 | static PLDHashOperator |
michael@0 | 244 | ReadFaceNamesProc(nsStringHashKey::KeyType aKey, |
michael@0 | 245 | nsRefPtr<gfxFontFamily>& aFamilyEntry, |
michael@0 | 246 | void* userArg); |
michael@0 | 247 | |
michael@0 | 248 | // helper method for finding fullname/postscript names in facename lists |
michael@0 | 249 | gfxFontEntry* FindFaceName(const nsAString& aFaceName); |
michael@0 | 250 | |
michael@0 | 251 | // look up a font by name, for cases where platform font list |
michael@0 | 252 | // maintains explicit mappings of fullname/psname ==> font |
michael@0 | 253 | virtual gfxFontEntry* LookupInFaceNameLists(const nsAString& aFontName); |
michael@0 | 254 | |
michael@0 | 255 | static PLDHashOperator LookupMissedFaceNamesProc(nsStringHashKey *aKey, |
michael@0 | 256 | void *aUserArg); |
michael@0 | 257 | |
michael@0 | 258 | static PLDHashOperator LookupMissedOtherNamesProc(nsStringHashKey *aKey, |
michael@0 | 259 | void *aUserArg); |
michael@0 | 260 | |
michael@0 | 261 | // commonly used fonts for which the name table should be loaded at startup |
michael@0 | 262 | virtual void PreloadNamesList(); |
michael@0 | 263 | |
michael@0 | 264 | // load the bad underline blacklist from pref. |
michael@0 | 265 | void LoadBadUnderlineList(); |
michael@0 | 266 | |
michael@0 | 267 | // explicitly set fixed-pitch flag for all faces |
michael@0 | 268 | void SetFixedPitch(const nsAString& aFamilyName); |
michael@0 | 269 | |
michael@0 | 270 | void GenerateFontListKey(const nsAString& aKeyName, nsAString& aResult); |
michael@0 | 271 | |
michael@0 | 272 | static PLDHashOperator |
michael@0 | 273 | HashEnumFuncForFamilies(nsStringHashKey::KeyType aKey, |
michael@0 | 274 | nsRefPtr<gfxFontFamily>& aFamilyEntry, |
michael@0 | 275 | void* aUserArg); |
michael@0 | 276 | |
michael@0 | 277 | virtual void GetFontFamilyNames(nsTArray<nsString>& aFontFamilyNames); |
michael@0 | 278 | |
michael@0 | 279 | // gfxFontInfoLoader overrides, used to load in font cmaps |
michael@0 | 280 | virtual void InitLoader(); |
michael@0 | 281 | virtual bool LoadFontInfo(); |
michael@0 | 282 | virtual void CleanupLoader(); |
michael@0 | 283 | |
michael@0 | 284 | // read the loader initialization prefs, and start it |
michael@0 | 285 | void GetPrefsAndStartLoader(); |
michael@0 | 286 | |
michael@0 | 287 | // for font list changes that affect all documents |
michael@0 | 288 | void ForceGlobalReflow(); |
michael@0 | 289 | |
michael@0 | 290 | // used by memory reporter to accumulate sizes of family names in the hash |
michael@0 | 291 | static size_t |
michael@0 | 292 | SizeOfFamilyNameEntryExcludingThis(const nsAString& aKey, |
michael@0 | 293 | const nsRefPtr<gfxFontFamily>& aFamily, |
michael@0 | 294 | mozilla::MallocSizeOf aMallocSizeOf, |
michael@0 | 295 | void* aUserArg); |
michael@0 | 296 | |
michael@0 | 297 | // canonical family name ==> family entry (unique, one name per family entry) |
michael@0 | 298 | nsRefPtrHashtable<nsStringHashKey, gfxFontFamily> mFontFamilies; |
michael@0 | 299 | |
michael@0 | 300 | // other family name ==> family entry (not unique, can have multiple names per |
michael@0 | 301 | // family entry, only names *other* than the canonical names are stored here) |
michael@0 | 302 | nsRefPtrHashtable<nsStringHashKey, gfxFontFamily> mOtherFamilyNames; |
michael@0 | 303 | |
michael@0 | 304 | // flag set after InitOtherFamilyNames is called upon first name lookup miss |
michael@0 | 305 | bool mOtherFamilyNamesInitialized; |
michael@0 | 306 | |
michael@0 | 307 | // flag set after fullname and Postcript name lists are populated |
michael@0 | 308 | bool mFaceNameListsInitialized; |
michael@0 | 309 | |
michael@0 | 310 | struct ExtraNames { |
michael@0 | 311 | ExtraNames() : mFullnames(100), mPostscriptNames(100) {} |
michael@0 | 312 | // fullname ==> font entry (unique, one name per font entry) |
michael@0 | 313 | nsRefPtrHashtable<nsStringHashKey, gfxFontEntry> mFullnames; |
michael@0 | 314 | // Postscript name ==> font entry (unique, one name per font entry) |
michael@0 | 315 | nsRefPtrHashtable<nsStringHashKey, gfxFontEntry> mPostscriptNames; |
michael@0 | 316 | }; |
michael@0 | 317 | nsAutoPtr<ExtraNames> mExtraNames; |
michael@0 | 318 | |
michael@0 | 319 | // face names missed when face name loading takes a long time |
michael@0 | 320 | nsAutoPtr<nsTHashtable<nsStringHashKey> > mFaceNamesMissed; |
michael@0 | 321 | |
michael@0 | 322 | // localized family names missed when face name loading takes a long time |
michael@0 | 323 | nsAutoPtr<nsTHashtable<nsStringHashKey> > mOtherNamesMissed; |
michael@0 | 324 | |
michael@0 | 325 | // cached pref font lists |
michael@0 | 326 | // maps list of family names ==> array of family entries, one per lang group |
michael@0 | 327 | nsDataHashtable<nsUint32HashKey, nsTArray<nsRefPtr<gfxFontFamily> > > mPrefFonts; |
michael@0 | 328 | |
michael@0 | 329 | // when system-wide font lookup fails for a character, cache it to skip future searches |
michael@0 | 330 | gfxSparseBitSet mCodepointsWithNoFonts; |
michael@0 | 331 | |
michael@0 | 332 | // the family to use for U+FFFD fallback, to avoid expensive search every time |
michael@0 | 333 | // on pages with lots of problems |
michael@0 | 334 | nsRefPtr<gfxFontFamily> mReplacementCharFallbackFamily; |
michael@0 | 335 | |
michael@0 | 336 | nsTHashtable<nsStringHashKey> mBadUnderlineFamilyNames; |
michael@0 | 337 | |
michael@0 | 338 | // character map data shared across families |
michael@0 | 339 | // contains weak ptrs to cmaps shared by font entry objects |
michael@0 | 340 | nsTHashtable<CharMapHashKey> mSharedCmaps; |
michael@0 | 341 | |
michael@0 | 342 | // data used as part of the font cmap loading process |
michael@0 | 343 | nsTArray<nsRefPtr<gfxFontFamily> > mFontFamiliesToLoad; |
michael@0 | 344 | uint32_t mStartIndex; |
michael@0 | 345 | uint32_t mIncrement; |
michael@0 | 346 | uint32_t mNumFamilies; |
michael@0 | 347 | |
michael@0 | 348 | nsTHashtable<nsPtrHashKey<gfxUserFontSet> > mUserFontSetList; |
michael@0 | 349 | }; |
michael@0 | 350 | |
michael@0 | 351 | #endif /* GFXPLATFORMFONTLIST_H_ */ |