1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
6 #ifndef GFX_FT2FONTLIST_H
7 #define GFX_FT2FONTLIST_H
9 #include "mozilla/MemoryReporting.h"
10 #include "gfxFT2FontBase.h"
11 #include "gfxPlatformFontList.h"
12 #include "nsTHashSet.h"
16 class SystemFontListEntry
;
21 }; // namespace mozilla
24 typedef struct FT_FaceRec_
* FT_Face
;
26 class WillShutdownObserver
;
28 class FT2FontEntry final
: public gfxFT2FontEntryBase
{
29 friend class gfxFT2FontList
;
31 using FontListEntry
= mozilla::dom::SystemFontListEntry
;
32 using FTUserFontData
= mozilla::gfx::FTUserFontData
;
35 explicit FT2FontEntry(const nsACString
& aFaceName
)
36 : gfxFT2FontEntryBase(aFaceName
), mFTFontIndex(0) {}
40 gfxFontEntry
* Clone() const override
;
42 const nsCString
& GetName() const { return Name(); }
44 // create a font entry for a downloaded font
45 static FT2FontEntry
* CreateFontEntry(
46 const nsACString
& aFontName
, WeightRange aWeight
, StretchRange aStretch
,
47 SlantStyleRange aStyle
, const uint8_t* aFontData
, uint32_t aLength
);
49 // create a font entry representing an installed font, identified by
50 // a FontListEntry; the freetype and cairo faces will not be instantiated
51 // until actually needed
52 static FT2FontEntry
* CreateFontEntry(const FontListEntry
& aFLE
);
54 // Create a font entry with the given name; if it is an installed font,
55 // also record the filename and index.
56 // If a non-null harfbuzz face is passed, also set style/weight/stretch
57 // properties of the entry from the values in the face.
58 static FT2FontEntry
* CreateFontEntry(const nsACString
& aName
,
59 const char* aFilename
, uint8_t aIndex
,
60 const hb_face_t
* aFace
);
62 gfxFont
* CreateFontInstance(const gfxFontStyle
* aStyle
) override
;
64 nsresult
ReadCMAP(FontInfoData
* aFontInfoData
= nullptr) override
;
66 hb_blob_t
* GetFontTable(uint32_t aTableTag
) override
;
68 bool HasFontTable(uint32_t aTableTag
) override
;
69 nsresult
CopyFontTable(uint32_t aTableTag
, nsTArray
<uint8_t>&) override
;
71 bool HasVariations() override
;
72 void GetVariationAxes(
73 nsTArray
<gfxFontVariationAxis
>& aVariationAxes
) override
;
74 void GetVariationInstances(
75 nsTArray
<gfxFontVariationInstance
>& aInstances
) override
;
77 // Check for various kinds of brokenness, and set flags on the entry
78 // accordingly so that we avoid using bad font tables
79 void CheckForBrokenFont(gfxFontFamily
* aFamily
);
80 void CheckForBrokenFont(const nsACString
& aFamilyKey
);
82 already_AddRefed
<mozilla::gfx::SharedFTFace
> GetFTFace(bool aCommit
= false);
83 FTUserFontData
* GetUserFontData();
85 FT_MM_Var
* GetMMVar() override
;
87 // Get a harfbuzz face for this font, if possible. The caller is responsible
88 // to destroy the face when no longer needed.
89 // This may be a bit expensive, so avoid calling multiple times if the same
90 // face can be re-used for several purposes instead.
91 hb_face_t
* CreateHBFace() const;
94 * Append this face's metadata to aFaceList for storage in the FontNameCache
95 * (for faster startup).
96 * The aPSName and aFullName parameters here can in principle be empty,
97 * but if they are missing for a given face then src:local() lookups will
98 * not be able to find it when the shared font list is in use.
100 void AppendToFaceList(nsCString
& aFaceList
, const nsACString
& aFamilyName
,
101 const nsACString
& aPSName
, const nsACString
& aFullName
,
102 FontVisibility aVisibility
);
104 void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf
,
105 FontListSizes
* aSizes
) const override
;
106 void AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
,
107 FontListSizes
* aSizes
) const override
;
109 // Strong reference (addref'd), but held in an atomic ptr rather than a
111 mozilla::Atomic
<mozilla::gfx::SharedFTFace
*> mFTFace
;
113 FT_MM_Var
* mMMVar
= nullptr;
116 uint8_t mFTFontIndex
;
118 mozilla::ThreadSafeWeakPtr
<mozilla::gfx::UnscaledFontFreeType
> mUnscaledFont
;
120 nsTHashSet
<uint32_t> mAvailableTables
;
122 enum class HasVariationsState
: int8_t {
127 std::atomic
<HasVariationsState
> mHasVariations
=
128 HasVariationsState::Uninitialized
;
130 bool mMMVarInitialized
= false;
133 class FT2FontFamily final
: public gfxFontFamily
{
134 using FontListEntry
= mozilla::dom::SystemFontListEntry
;
137 explicit FT2FontFamily(const nsACString
& aName
, FontVisibility aVisibility
)
138 : gfxFontFamily(aName
, aVisibility
) {}
140 // Append this family's faces to the IPC fontlist
141 void AddFacesToFontList(nsTArray
<FontListEntry
>* aFontList
);
143 void FinalizeMemberList(bool aSortFaces
);
146 class gfxFT2FontList final
: public gfxPlatformFontList
{
147 using FontListEntry
= mozilla::dom::SystemFontListEntry
;
151 virtual ~gfxFT2FontList();
153 gfxFontEntry
* CreateFontEntry(
154 mozilla::fontlist::Face
* aFace
,
155 const mozilla::fontlist::Family
* aFamily
) override
;
157 gfxFontEntry
* LookupLocalFont(nsPresContext
* aPresContext
,
158 const nsACString
& aFontName
,
159 WeightRange aWeightForEntry
,
160 StretchRange aStretchForEntry
,
161 SlantStyleRange aStyleForEntry
) override
;
163 gfxFontEntry
* MakePlatformFont(const nsACString
& aFontName
,
164 WeightRange aWeightForEntry
,
165 StretchRange aStretchForEntry
,
166 SlantStyleRange aStyleForEntry
,
167 const uint8_t* aFontData
,
168 uint32_t aLength
) override
;
172 void ReadSystemFontList(mozilla::dom::SystemFontList
*);
174 static gfxFT2FontList
* PlatformFontList() {
175 return static_cast<gfxFT2FontList
*>(
176 gfxPlatformFontList::PlatformFontList());
179 gfxFontFamily
* CreateFontFamily(const nsACString
& aName
,
180 FontVisibility aVisibility
) const override
;
185 typedef enum { kUnknown
, kStandard
} StandardFile
;
187 // initialize font lists
188 nsresult
InitFontListForPlatform() MOZ_REQUIRES(mLock
) override
;
190 FontVisibility
GetVisibilityForFamily(const nsACString
& aName
) const;
192 void AppendFaceFromFontListEntry(const FontListEntry
& aFLE
,
193 StandardFile aStdFile
) MOZ_REQUIRES(mLock
);
195 void AppendFacesFromBlob(const nsCString
& aFileName
, StandardFile aStdFile
,
196 hb_blob_t
* aBlob
, FontNameCache
* aCache
,
197 uint32_t aTimestamp
, uint32_t aFilesize
)
200 void AppendFacesFromFontFile(const nsCString
& aFileName
,
201 FontNameCache
* aCache
, StandardFile aStdFile
)
204 void AppendFacesFromOmnijarEntry(nsZipArchive
* aReader
,
205 const nsCString
& aEntryName
,
206 FontNameCache
* aCache
, bool aJarChanged
)
209 void InitSharedFontListForPlatform() MOZ_REQUIRES(mLock
) override
;
210 void CollectInitData(const FontListEntry
& aFLE
, const nsCString
& aPSName
,
211 const nsCString
& aFullName
, StandardFile aStdFile
);
214 * Callback passed to AppendFacesFromCachedFaceList to collect family/face
215 * information in either the unshared or shared list we're building.
217 typedef void (*CollectFunc
)(const FontListEntry
& aFLE
,
218 const nsCString
& aPSName
,
219 const nsCString
& aFullName
,
220 StandardFile aStdFile
);
223 * Append faces from the face-list record for a specific file.
224 * aCollectFace is a callback that will store the face(s) in either the
225 * unshared mFontFamilies list or the mFamilyInitData/mFaceInitData tables
226 * that will be used to initialize the shared list.
227 * Returns true if it is able to read at least one face entry; false if no
228 * usable face entry was found.
230 bool AppendFacesFromCachedFaceList(CollectFunc aCollectFace
,
231 const nsCString
& aFileName
,
232 const nsCString
& aFaceList
,
233 StandardFile aStdFile
) MOZ_REQUIRES(mLock
);
235 void AddFaceToList(const nsCString
& aEntryName
, uint32_t aIndex
,
236 StandardFile aStdFile
, hb_face_t
* aFace
,
237 nsCString
& aFaceList
) MOZ_REQUIRES(mLock
);
239 void FindFonts() MOZ_REQUIRES(mLock
);
241 void FindFontsInOmnijar(FontNameCache
* aCache
) MOZ_REQUIRES(mLock
);
243 void FindFontsInDir(const nsCString
& aDir
, FontNameCache
* aFNC
)
246 FontFamily
GetDefaultFontForPlatform(nsPresContext
* aPresContext
,
247 const gfxFontStyle
* aStyle
,
248 nsAtom
* aLanguage
= nullptr)
249 MOZ_REQUIRES(mLock
) override
;
251 nsTHashSet
<nsCString
> mSkipSpaceLookupCheckFamilies
;
254 mozilla::UniquePtr
<FontNameCache
> mFontNameCache
;
255 int64_t mJarModifiedTime
;
256 RefPtr
<WillShutdownObserver
> mObserver
;
258 nsTArray
<mozilla::fontlist::Family::InitData
> mFamilyInitData
;
259 nsClassHashtable
<nsCStringHashKey
,
260 nsTArray
<mozilla::fontlist::Face::InitData
>>
264 #endif /* GFX_FT2FONTLIST_H */