Bug 1874684 - Part 28: Return DateDuration from DifferenceISODateTime. r=mgaudet
[gecko.git] / gfx / thebes / gfxFT2FontList.h
blob0e92d31944cc22c34ca8de421dc6224ffa982dd2
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"
14 namespace mozilla {
15 namespace dom {
16 class SystemFontListEntry;
18 namespace gfx {
19 class FTUserFontData;
21 }; // namespace mozilla
23 class FontNameCache;
24 typedef struct FT_FaceRec_* FT_Face;
25 class nsZipArchive;
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;
34 public:
35 explicit FT2FontEntry(const nsACString& aFaceName)
36 : gfxFT2FontEntryBase(aFaceName), mFTFontIndex(0) {}
38 ~FT2FontEntry();
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;
93 /**
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
110 // normal RefPtr.
111 mozilla::Atomic<mozilla::gfx::SharedFTFace*> mFTFace;
113 FT_MM_Var* mMMVar = nullptr;
115 nsCString mFilename;
116 uint8_t mFTFontIndex;
118 mozilla::ThreadSafeWeakPtr<mozilla::gfx::UnscaledFontFreeType> mUnscaledFont;
120 nsTHashSet<uint32_t> mAvailableTables;
122 enum class HasVariationsState : int8_t {
123 Uninitialized = -1,
124 No = 0,
125 Yes = 1,
127 std::atomic<HasVariationsState> mHasVariations =
128 HasVariationsState::Uninitialized;
130 bool mMMVarInitialized = false;
133 class FT2FontFamily final : public gfxFontFamily {
134 using FontListEntry = mozilla::dom::SystemFontListEntry;
136 public:
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;
149 public:
150 gfxFT2FontList();
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;
170 void WriteCache();
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;
182 void WillShutdown();
184 protected:
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)
198 MOZ_REQUIRES(mLock);
200 void AppendFacesFromFontFile(const nsCString& aFileName,
201 FontNameCache* aCache, StandardFile aStdFile)
202 MOZ_REQUIRES(mLock);
204 void AppendFacesFromOmnijarEntry(nsZipArchive* aReader,
205 const nsCString& aEntryName,
206 FontNameCache* aCache, bool aJarChanged)
207 MOZ_REQUIRES(mLock);
209 void InitSharedFontListForPlatform() MOZ_REQUIRES(mLock) override;
210 void CollectInitData(const FontListEntry& aFLE, const nsCString& aPSName,
211 const nsCString& aFullName, StandardFile aStdFile);
213 nsTArray<std::pair<const char**, uint32_t>> GetFilteredPlatformFontLists()
214 override;
217 * Callback passed to AppendFacesFromCachedFaceList to collect family/face
218 * information in either the unshared or shared list we're building.
220 typedef void (*CollectFunc)(const FontListEntry& aFLE,
221 const nsCString& aPSName,
222 const nsCString& aFullName,
223 StandardFile aStdFile);
226 * Append faces from the face-list record for a specific file.
227 * aCollectFace is a callback that will store the face(s) in either the
228 * unshared mFontFamilies list or the mFamilyInitData/mFaceInitData tables
229 * that will be used to initialize the shared list.
230 * Returns true if it is able to read at least one face entry; false if no
231 * usable face entry was found.
233 bool AppendFacesFromCachedFaceList(CollectFunc aCollectFace,
234 const nsCString& aFileName,
235 const nsCString& aFaceList,
236 StandardFile aStdFile) MOZ_REQUIRES(mLock);
238 void AddFaceToList(const nsCString& aEntryName, uint32_t aIndex,
239 StandardFile aStdFile, hb_face_t* aFace,
240 nsCString& aFaceList) MOZ_REQUIRES(mLock);
242 void FindFonts() MOZ_REQUIRES(mLock);
244 void FindFontsInOmnijar(FontNameCache* aCache) MOZ_REQUIRES(mLock);
246 void FindFontsInDir(const nsCString& aDir, FontNameCache* aFNC)
247 MOZ_REQUIRES(mLock);
249 FontFamily GetDefaultFontForPlatform(nsPresContext* aPresContext,
250 const gfxFontStyle* aStyle,
251 nsAtom* aLanguage = nullptr)
252 MOZ_REQUIRES(mLock) override;
254 nsTHashSet<nsCString> mSkipSpaceLookupCheckFamilies;
256 private:
257 mozilla::UniquePtr<FontNameCache> mFontNameCache;
258 int64_t mJarModifiedTime;
259 RefPtr<WillShutdownObserver> mObserver;
261 nsTArray<mozilla::fontlist::Family::InitData> mFamilyInitData;
262 nsClassHashtable<nsCStringHashKey,
263 nsTArray<mozilla::fontlist::Face::InitData>>
264 mFaceInitData;
267 #endif /* GFX_FT2FONTLIST_H */