summaryrefslogtreecommitdiffstats
path: root/gfx/thebes/gfxPlatformFontList.h
blob: d77c1205934a9a56b117b8d4925e9f98e257e9d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * 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/. */

#ifndef GFXPLATFORMFONTLIST_H_
#define GFXPLATFORMFONTLIST_H_

#include "nsDataHashtable.h"
#include "nsRefPtrHashtable.h"
#include "nsTHashtable.h"

#include "gfxFontUtils.h"
#include "gfxFontInfoLoader.h"
#include "gfxFont.h"
#include "gfxFontConstants.h"
#include "gfxPlatform.h"
#include "gfxFontFamilyList.h"

#include "nsIMemoryReporter.h"
#include "mozilla/Attributes.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/RangedArray.h"
#include "nsILanguageAtomService.h"

class CharMapHashKey : public PLDHashEntryHdr
{
public:
    typedef gfxCharacterMap* KeyType;
    typedef const gfxCharacterMap* KeyTypePointer;

    explicit CharMapHashKey(const gfxCharacterMap *aCharMap) :
        mCharMap(const_cast<gfxCharacterMap*>(aCharMap))
    {
        MOZ_COUNT_CTOR(CharMapHashKey);
    }
    CharMapHashKey(const CharMapHashKey& toCopy) :
        mCharMap(toCopy.mCharMap)
    {
        MOZ_COUNT_CTOR(CharMapHashKey);
    }
    ~CharMapHashKey()
    {
        MOZ_COUNT_DTOR(CharMapHashKey);
    }

    gfxCharacterMap* GetKey() const { return mCharMap; }

    bool KeyEquals(const gfxCharacterMap *aCharMap) const {
        NS_ASSERTION(!aCharMap->mBuildOnTheFly && !mCharMap->mBuildOnTheFly,
                     "custom cmap used in shared cmap hashtable");
        // cmaps built on the fly never match
        if (aCharMap->mHash != mCharMap->mHash)
        {
            return false;
        }
        return mCharMap->Equals(aCharMap);
    }

    static const gfxCharacterMap* KeyToPointer(gfxCharacterMap *aCharMap) {
        return aCharMap;
    }
    static PLDHashNumber HashKey(const gfxCharacterMap *aCharMap) {
        return aCharMap->mHash;
    }

    enum { ALLOW_MEMMOVE = true };

protected:
    gfxCharacterMap *mCharMap;
};

// gfxPlatformFontList is an abstract class for the global font list on the system;
// concrete subclasses for each platform implement the actual interface to the system fonts.
// This class exists because we cannot rely on the platform font-finding APIs to behave
// in sensible/similar ways, particularly with rich, complex OpenType families,
// so we do our own font family/style management here instead.

// Much of this is based on the old gfxQuartzFontCache, but adapted for use on all platforms.

struct FontListSizes {
    uint32_t mFontListSize; // size of the font list and dependent objects
                            // (font family and face names, etc), but NOT
                            // including the font table cache and the cmaps
    uint32_t mFontTableCacheSize; // memory used for the gfxFontEntry table caches
    uint32_t mCharMapsSize; // memory used for cmap coverage info
};

class gfxUserFontSet;

class gfxPlatformFontList : public gfxFontInfoLoader
{
public:
    typedef mozilla::unicode::Script Script;

    static gfxPlatformFontList* PlatformFontList() {
        return sPlatformFontList;
    }

    static nsresult Init() {
        NS_ASSERTION(!sPlatformFontList, "What's this doing here?");
        gfxPlatform::GetPlatform()->CreatePlatformFontList();
        if (!sPlatformFontList) {
            return NS_ERROR_OUT_OF_MEMORY;
        }
        return NS_OK;
    }

    static void Shutdown() {
        delete sPlatformFontList;
        sPlatformFontList = nullptr;
    }

    virtual ~gfxPlatformFontList();

    // initialize font lists
    nsresult InitFontList();

    virtual void GetFontList(nsIAtom *aLangGroup,
                             const nsACString& aGenericFamily,
                             nsTArray<nsString>& aListOfFonts);

    void UpdateFontList();

    virtual void ClearLangGroupPrefFonts();

    virtual void GetFontFamilyList(nsTArray<RefPtr<gfxFontFamily> >& aFamilyArray);

    gfxFontEntry*
    SystemFindFontForChar(uint32_t aCh, uint32_t aNextCh,
                          Script aRunScript,
                          const gfxFontStyle* aStyle);

    // Find family(ies) matching aFamily and append to the aOutput array
    // (there may be multiple results in the case of fontconfig aliases, etc).
    // Return true if any match was found and appended, false if none.
    virtual bool
    FindAndAddFamilies(const nsAString& aFamily,
                       nsTArray<gfxFontFamily*>* aOutput,
                       gfxFontStyle* aStyle = nullptr,
                       gfxFloat aDevToCssSize = 1.0);

    gfxFontEntry* FindFontForFamily(const nsAString& aFamily, const gfxFontStyle* aStyle, bool& aNeedsBold);

    // name lookup table methods

    void AddOtherFamilyName(gfxFontFamily *aFamilyEntry, nsAString& aOtherFamilyName);

    void AddFullname(gfxFontEntry *aFontEntry, nsAString& aFullname);

    void AddPostscriptName(gfxFontEntry *aFontEntry, nsAString& aPostscriptName);

    bool NeedFullnamePostscriptNames() { return mExtraNames != nullptr; }

    // pure virtual functions, to be provided by concrete subclasses

    // get the system default font family
    gfxFontFamily* GetDefaultFont(const gfxFontStyle* aStyle);

    // look up a font by name on the host platform
    virtual gfxFontEntry* LookupLocalFont(const nsAString& aFontName,
                                          uint16_t aWeight,
                                          int16_t aStretch,
                                          uint8_t aStyle) = 0;

    // create a new platform font from downloaded data (@font-face)
    // this method is responsible to ensure aFontData is free()'d
    virtual gfxFontEntry* MakePlatformFont(const nsAString& aFontName,
                                           uint16_t aWeight,
                                           int16_t aStretch,
                                           uint8_t aStyle,
                                           const uint8_t* aFontData,
                                           uint32_t aLength) = 0;

    // get the standard family name on the platform for a given font name
    // (platforms may override, eg Mac)
    virtual bool GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName);

    virtual void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf,
                                        FontListSizes* aSizes) const;
    virtual void AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf,
                                        FontListSizes* aSizes) const;

    // search for existing cmap that matches the input
    // return the input if no match is found
    gfxCharacterMap* FindCharMap(gfxCharacterMap *aCmap);

    // add a cmap to the shared cmap set
    gfxCharacterMap* AddCmap(const gfxCharacterMap *aCharMap);

    // remove the cmap from the shared cmap set
    void RemoveCmap(const gfxCharacterMap *aCharMap);

    // keep track of userfont sets to notify when global fontlist changes occur
    void AddUserFontSet(gfxUserFontSet *aUserFontSet) {
        mUserFontSetList.PutEntry(aUserFontSet);
    }

    void RemoveUserFontSet(gfxUserFontSet *aUserFontSet) {
        mUserFontSetList.RemoveEntry(aUserFontSet);
    }

    static const gfxFontEntry::ScriptRange sComplexScriptRanges[];

    void GetFontlistInitInfo(uint32_t& aNumInits, uint32_t& aLoaderState) {
        aNumInits = mFontlistInitCount;
        aLoaderState = (uint32_t) mState;
    }

    virtual void
    AddGenericFonts(mozilla::FontFamilyType aGenericType,
                    nsIAtom* aLanguage,
                    nsTArray<gfxFontFamily*>& aFamilyList);

    nsTArray<RefPtr<gfxFontFamily>>*
    GetPrefFontsLangGroup(mozilla::FontFamilyType aGenericType,
                          eFontPrefLang aPrefLang);

    // in some situations, need to make decisions about ambiguous characters, may need to look at multiple pref langs
    void GetLangPrefs(eFontPrefLang aPrefLangs[], uint32_t &aLen, eFontPrefLang aCharLang, eFontPrefLang aPageLang);

    // convert a lang group to enum constant (i.e. "zh-TW" ==> eFontPrefLang_ChineseTW)
    static eFontPrefLang GetFontPrefLangFor(const char* aLang);

    // convert a lang group atom to enum constant
    static eFontPrefLang GetFontPrefLangFor(nsIAtom *aLang);

    // convert an enum constant to a lang group atom
    static nsIAtom* GetLangGroupForPrefLang(eFontPrefLang aLang);

    // convert a enum constant to lang group string (i.e. eFontPrefLang_ChineseTW ==> "zh-TW")
    static const char* GetPrefLangName(eFontPrefLang aLang);

    // map a Unicode range (based on char code) to a font language for Preferences
    static eFontPrefLang GetFontPrefLangFor(uint8_t aUnicodeRange);

    // returns true if a pref lang is CJK
    static bool IsLangCJK(eFontPrefLang aLang);

    // helper method to add a pref lang to an array, if not already in array
    static void AppendPrefLang(eFontPrefLang aPrefLangs[], uint32_t& aLen, eFontPrefLang aAddLang);

    // default serif/sans-serif choice based on font.default.xxx prefs
    mozilla::FontFamilyType
    GetDefaultGeneric(eFontPrefLang aLang);

    // map lang group ==> lang string
    void GetSampleLangForGroup(nsIAtom* aLanguage, nsACString& aLangStr,
                               bool aCheckEnvironment = true);

    // Returns true if the font family whitelist is not empty.
    bool IsFontFamilyWhitelistActive();

    static void FontWhitelistPrefChanged(const char *aPref, void *aClosure) {
        gfxPlatformFontList::PlatformFontList()->UpdateFontList();
    }

protected:
    class MemoryReporter final : public nsIMemoryReporter
    {
        ~MemoryReporter() {}
    public:
        NS_DECL_ISUPPORTS
        NS_DECL_NSIMEMORYREPORTER
    };

    explicit gfxPlatformFontList(bool aNeedFullnamePostscriptNames = true);

    static gfxPlatformFontList *sPlatformFontList;

    // Convenience method to return the first matching family (if any) as found
    // by FindAndAddFamilies().
    gfxFontFamily*
    FindFamily(const nsAString& aFamily, gfxFontStyle* aStyle = nullptr,
               gfxFloat aDevToCssSize = 1.0)
    {
        AutoTArray<gfxFontFamily*,1> families;
        return FindAndAddFamilies(aFamily, &families, aStyle, aDevToCssSize)
               ? families[0] : nullptr;
    }

    // Lookup family name in global family list without substitutions or
    // localized family name lookup. Used for common font fallback families.
    gfxFontFamily* FindFamilyByCanonicalName(const nsAString& aFamily) {
        nsAutoString key;
        gfxFontFamily *familyEntry;
        GenerateFontListKey(aFamily, key);
        if ((familyEntry = mFontFamilies.GetWeak(key))) {
            return CheckFamily(familyEntry);
        }
        return nullptr;
    }

    // returns default font for a given character, null otherwise
    gfxFontEntry* CommonFontFallback(uint32_t aCh, uint32_t aNextCh,
                                     Script aRunScript,
                                     const gfxFontStyle* aMatchStyle,
                                     gfxFontFamily** aMatchedFamily);

    // Search fonts system-wide for a given character, null if not found.
    gfxFontEntry* GlobalFontFallback(const uint32_t aCh,
                                     Script aRunScript,
                                     const gfxFontStyle* aMatchStyle,
                                     uint32_t& aCmapCount,
                                     gfxFontFamily** aMatchedFamily);

    // Platform-specific implementation of global font fallback, if any;
    // this may return nullptr in which case the default cmap-based fallback
    // will be performed.
    virtual gfxFontEntry*
    PlatformGlobalFontFallback(const uint32_t aCh,
                               Script aRunScript,
                               const gfxFontStyle* aMatchStyle,
                               gfxFontFamily** aMatchedFamily)
    {
        return nullptr;
    }

    // whether system-based font fallback is used or not
    // if system fallback is used, no need to load all cmaps
    virtual bool UsesSystemFallback() { return false; }

    void AppendCJKPrefLangs(eFontPrefLang aPrefLangs[], uint32_t &aLen,
                            eFontPrefLang aCharLang, eFontPrefLang aPageLang);

    // verifies that a family contains a non-zero font count
    gfxFontFamily* CheckFamily(gfxFontFamily *aFamily);

    // initialize localized family names
    void InitOtherFamilyNames();

    // search through font families, looking for a given name, initializing
    // facename lists along the way. first checks all families with names
    // close to face name, then searchs all families if not found.
    gfxFontEntry* SearchFamiliesForFaceName(const nsAString& aFaceName);

    // helper method for finding fullname/postscript names in facename lists
    gfxFontEntry* FindFaceName(const nsAString& aFaceName);

    // look up a font by name, for cases where platform font list
    // maintains explicit mappings of fullname/psname ==> font
    virtual gfxFontEntry* LookupInFaceNameLists(const nsAString& aFontName);

    // commonly used fonts for which the name table should be loaded at startup
    virtual void PreloadNamesList();

    // load the bad underline blacklist from pref.
    void LoadBadUnderlineList();

    void GenerateFontListKey(const nsAString& aKeyName, nsAString& aResult);

    virtual void GetFontFamilyNames(nsTArray<nsString>& aFontFamilyNames);

    nsILanguageAtomService* GetLangService();

    // helper function to map lang to lang group
    nsIAtom* GetLangGroup(nsIAtom* aLanguage);

    // helper method for finding an appropriate lang string
    bool TryLangForGroup(const nsACString& aOSLang, nsIAtom* aLangGroup,
                         nsACString& aLang);

    static const char* GetGenericName(mozilla::FontFamilyType aGenericType);

    // gfxFontInfoLoader overrides, used to load in font cmaps
    virtual void InitLoader();
    virtual bool LoadFontInfo();
    virtual void CleanupLoader();

    // read the loader initialization prefs, and start it
    void GetPrefsAndStartLoader();

    // for font list changes that affect all documents
    void ForceGlobalReflow();

    void RebuildLocalFonts();

    void
    ResolveGenericFontNames(mozilla::FontFamilyType aGenericType,
                            eFontPrefLang aPrefLang,
                            nsTArray<RefPtr<gfxFontFamily>>* aGenericFamilies);

    void
    ResolveEmojiFontNames(nsTArray<RefPtr<gfxFontFamily>>* aGenericFamilies);

    void
    GetFontFamiliesFromGenericFamilies(
        nsTArray<nsString>& aGenericFamilies,
        nsIAtom* aLangGroup,
        nsTArray<RefPtr<gfxFontFamily>>* aFontFamilies);

    virtual nsresult InitFontListForPlatform() = 0;

    void ApplyWhitelist();

    typedef nsRefPtrHashtable<nsStringHashKey, gfxFontFamily> FontFamilyTable;
    typedef nsRefPtrHashtable<nsStringHashKey, gfxFontEntry> FontEntryTable;

    // used by memory reporter to accumulate sizes of family names in the table
    static size_t
    SizeOfFontFamilyTableExcludingThis(const FontFamilyTable& aTable,
                                       mozilla::MallocSizeOf aMallocSizeOf);
    static size_t
    SizeOfFontEntryTableExcludingThis(const FontEntryTable& aTable,
                                      mozilla::MallocSizeOf aMallocSizeOf);

    // Platform-specific helper for GetDefaultFont(...).
    virtual gfxFontFamily*
    GetDefaultFontForPlatform(const gfxFontStyle* aStyle) = 0;

    // canonical family name ==> family entry (unique, one name per family entry)
    FontFamilyTable mFontFamilies;

    // other family name ==> family entry (not unique, can have multiple names per
    // family entry, only names *other* than the canonical names are stored here)
    FontFamilyTable mOtherFamilyNames;

    // flag set after InitOtherFamilyNames is called upon first name lookup miss
    bool mOtherFamilyNamesInitialized;

    // flag set after fullname and Postcript name lists are populated
    bool mFaceNameListsInitialized;

    struct ExtraNames {
      ExtraNames() : mFullnames(64), mPostscriptNames(64) {}

      // fullname ==> font entry (unique, one name per font entry)
      FontEntryTable mFullnames;
      // Postscript name ==> font entry (unique, one name per font entry)
      FontEntryTable mPostscriptNames;
    };
    mozilla::UniquePtr<ExtraNames> mExtraNames;

    // face names missed when face name loading takes a long time
    mozilla::UniquePtr<nsTHashtable<nsStringHashKey> > mFaceNamesMissed;

    // localized family names missed when face name loading takes a long time
    mozilla::UniquePtr<nsTHashtable<nsStringHashKey> > mOtherNamesMissed;

    typedef nsTArray<RefPtr<gfxFontFamily>> PrefFontList;
    typedef mozilla::RangedArray<mozilla::UniquePtr<PrefFontList>,
                                 mozilla::eFamily_generic_first,
                                 mozilla::eFamily_generic_count> PrefFontsForLangGroup;
    mozilla::RangedArray<PrefFontsForLangGroup,
                         eFontPrefLang_First,
                         eFontPrefLang_Count> mLangGroupPrefFonts;

    mozilla::UniquePtr<PrefFontList> mEmojiPrefFont;
    
    // when system-wide font lookup fails for a character, cache it to skip future searches
    gfxSparseBitSet mCodepointsWithNoFonts;

    // the family to use for U+FFFD fallback, to avoid expensive search every time
    // on pages with lots of problems
    RefPtr<gfxFontFamily> mReplacementCharFallbackFamily;

    nsTHashtable<nsStringHashKey> mBadUnderlineFamilyNames;

    // character map data shared across families
    // contains weak ptrs to cmaps shared by font entry objects
    nsTHashtable<CharMapHashKey> mSharedCmaps;

    // data used as part of the font cmap loading process
    nsTArray<RefPtr<gfxFontFamily> > mFontFamiliesToLoad;
    uint32_t mStartIndex;
    uint32_t mIncrement;
    uint32_t mNumFamilies;

    // xxx - info for diagnosing no default font aborts
    // see bugs 636957, 1070983, 1189129
    uint32_t mFontlistInitCount; // num times InitFontList called

    nsTHashtable<nsPtrHashKey<gfxUserFontSet> > mUserFontSetList;

    nsCOMPtr<nsILanguageAtomService> mLangService;
    nsTArray<uint32_t> mCJKPrefLangs;
    nsTArray<mozilla::FontFamilyType> mDefaultGenericsLangGroup;

    bool mFontFamilyWhitelistActive;
};

#endif /* GFXPLATFORMFONTLIST_H_ */