1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef GFX_FT2FONTLIST_H
7 #define GFX_FT2FONTLIST_H
8 
9 #include "mozilla/MemoryReporting.h"
10 #include "gfxPlatformFontList.h"
11 #include "mozilla/gfx/UnscaledFontFreeType.h"
12 
13 namespace mozilla {
14 namespace dom {
15 class FontListEntry;
16 };
17 };  // namespace mozilla
18 using mozilla::dom::FontListEntry;
19 
20 class FontNameCache;
21 typedef struct FT_FaceRec_* FT_Face;
22 class nsZipArchive;
23 class WillShutdownObserver;
24 
25 class FT2FontEntry : public gfxFontEntry {
26  public:
FT2FontEntry(const nsAString & aFaceName)27   FT2FontEntry(const nsAString& aFaceName)
28       : gfxFontEntry(aFaceName),
29         mFTFace(nullptr),
30         mFontFace(nullptr),
31         mFTFontIndex(0) {}
32 
33   ~FT2FontEntry();
34 
35   gfxFontEntry* Clone() const override;
36 
GetName()37   const nsString& GetName() const { return Name(); }
38 
39   // create a font entry for a downloaded font
40   static FT2FontEntry* CreateFontEntry(const nsAString& aFontName,
41                                        uint16_t aWeight, int16_t aStretch,
42                                        uint8_t aStyle, const uint8_t* aFontData,
43                                        uint32_t aLength);
44 
45   // create a font entry representing an installed font, identified by
46   // a FontListEntry; the freetype and cairo faces will not be instantiated
47   // until actually needed
48   static FT2FontEntry* CreateFontEntry(const FontListEntry& aFLE);
49 
50   // Create a font entry for a given freetype face; if it is an installed font,
51   // also record the filename and index.
52   // aFontData (if non-nullptr) is NS_Malloc'ed data that aFace depends on,
53   // to be freed after the face is destroyed.
54   // aLength is the length of aFontData.
55   static FT2FontEntry* CreateFontEntry(FT_Face aFace, const char* aFilename,
56                                        uint8_t aIndex, const nsAString& aName,
57                                        const uint8_t* aFontData = nullptr,
58                                        uint32_t aLength = 0);
59 
60   virtual gfxFont* CreateFontInstance(const gfxFontStyle* aFontStyle,
61                                       bool aNeedsBold) override;
62 
63   // Create (if necessary) and return the cairo_font_face for this font.
64   // This may fail and return null, so caller must be prepared to handle this.
65   // If a style is passed, any variationSettings in the style will be applied
66   // to the resulting font face.
67   cairo_font_face_t* CairoFontFace(const gfxFontStyle* aStyle = nullptr);
68 
69   // Create a cairo_scaled_font for this face, with the given style.
70   // This may fail and return null, so caller must be prepared to handle this.
71   cairo_scaled_font_t* CreateScaledFont(const gfxFontStyle* aStyle);
72 
73   nsresult ReadCMAP(FontInfoData* aFontInfoData = nullptr) override;
74 
75   virtual hb_blob_t* GetFontTable(uint32_t aTableTag) override;
76 
77   virtual nsresult CopyFontTable(uint32_t aTableTag,
78                                  nsTArray<uint8_t>& aBuffer) override;
79 
80   // Check for various kinds of brokenness, and set flags on the entry
81   // accordingly so that we avoid using bad font tables
82   void CheckForBrokenFont(gfxFontFamily* aFamily);
83 
84   virtual void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf,
85                                       FontListSizes* aSizes) const override;
86   virtual void AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf,
87                                       FontListSizes* aSizes) const override;
88 
89   FT_Face mFTFace;
90   cairo_font_face_t* mFontFace;
91 
92   nsCString mFilename;
93   uint8_t mFTFontIndex;
94 
95   mozilla::ThreadSafeWeakPtr<mozilla::gfx::UnscaledFontFreeType> mUnscaledFont;
96 };
97 
98 class FT2FontFamily : public gfxFontFamily {
99  public:
FT2FontFamily(const nsAString & aName)100   FT2FontFamily(const nsAString& aName) : gfxFontFamily(aName) {}
101 
102   // Append this family's faces to the IPC fontlist
103   void AddFacesToFontList(InfallibleTArray<FontListEntry>* aFontList);
104 };
105 
106 class gfxFT2FontList : public gfxPlatformFontList {
107  public:
108   gfxFT2FontList();
109   virtual ~gfxFT2FontList();
110 
111   virtual gfxFontEntry* LookupLocalFont(const nsAString& aFontName,
112                                         uint16_t aWeight, int16_t aStretch,
113                                         uint8_t aStyle) override;
114 
115   virtual gfxFontEntry* MakePlatformFont(const nsAString& aFontName,
116                                          uint16_t aWeight, int16_t aStretch,
117                                          uint8_t aStyle,
118                                          const uint8_t* aFontData,
119                                          uint32_t aLength) override;
120 
121   void GetSystemFontList(InfallibleTArray<FontListEntry>* retValue);
122 
PlatformFontList()123   static gfxFT2FontList* PlatformFontList() {
124     return static_cast<gfxFT2FontList*>(
125         gfxPlatformFontList::PlatformFontList());
126   }
127 
128   virtual void GetFontFamilyList(
129       nsTArray<RefPtr<gfxFontFamily> >& aFamilyArray) override;
130 
131   gfxFontFamily* CreateFontFamily(const nsAString& aName) const override;
132 
133   void WillShutdown();
134 
135  protected:
136   typedef enum { kUnknown, kStandard } StandardFile;
137 
138   // initialize font lists
139   virtual nsresult InitFontListForPlatform() override;
140 
141   void AppendFaceFromFontListEntry(const FontListEntry& aFLE,
142                                    StandardFile aStdFile);
143 
144   void AppendFacesFromFontFile(const nsCString& aFileName,
145                                FontNameCache* aCache, StandardFile aStdFile);
146 
147   void AppendFacesFromOmnijarEntry(nsZipArchive* aReader,
148                                    const nsCString& aEntryName,
149                                    FontNameCache* aCache, bool aJarChanged);
150 
151   // the defaults here are suitable for reading bundled fonts from omnijar
152   void AppendFacesFromCachedFaceList(const nsCString& aFileName,
153                                      const nsCString& aFaceList,
154                                      StandardFile aStdFile = kStandard);
155 
156   void AddFaceToList(const nsCString& aEntryName, uint32_t aIndex,
157                      StandardFile aStdFile, FT_Face aFace,
158                      nsCString& aFaceList);
159 
160   void FindFonts();
161 
162   void FindFontsInOmnijar(FontNameCache* aCache);
163 
164   void FindFontsInDir(const nsCString& aDir, FontNameCache* aFNC);
165 
166   virtual gfxFontFamily* GetDefaultFontForPlatform(
167       const gfxFontStyle* aStyle) override;
168 
169   nsTHashtable<nsStringHashKey> mSkipSpaceLookupCheckFamilies;
170 
171  private:
172   mozilla::UniquePtr<FontNameCache> mFontNameCache;
173   int64_t mJarModifiedTime;
174   RefPtr<WillShutdownObserver> mObserver;
175 };
176 
177 #endif /* GFX_FT2FONTLIST_H */
178