1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2008-2009
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Jonathan Kew <jfkthame@gmail.com>
23 : * John Daggett <jdaggett@mozilla.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef GFXPLATFORMFONTLIST_H_
40 : #define GFXPLATFORMFONTLIST_H_
41 :
42 : #include "nsDataHashtable.h"
43 : #include "nsRefPtrHashtable.h"
44 : #include "nsTHashtable.h"
45 :
46 : #include "gfxFontUtils.h"
47 : #include "gfxFont.h"
48 : #include "gfxPlatform.h"
49 :
50 : #include "mozilla/FunctionTimer.h"
51 :
52 : // gfxPlatformFontList is an abstract class for the global font list on the system;
53 : // concrete subclasses for each platform implement the actual interface to the system fonts.
54 : // This class exists because we cannot rely on the platform font-finding APIs to behave
55 : // in sensible/similar ways, particularly with rich, complex OpenType families,
56 : // so we do our own font family/style management here instead.
57 :
58 : // Much of this is based on the old gfxQuartzFontCache, but adapted for use on all platforms.
59 :
60 : class gfxPlatformFontList : protected gfxFontInfoLoader
61 : {
62 : public:
63 0 : static gfxPlatformFontList* PlatformFontList() {
64 0 : return sPlatformFontList;
65 : }
66 :
67 : static nsresult Init() {
68 : NS_TIME_FUNCTION;
69 :
70 : NS_ASSERTION(!sPlatformFontList, "What's this doing here?");
71 : gfxPlatform::GetPlatform()->CreatePlatformFontList();
72 : if (!sPlatformFontList) {
73 : return NS_ERROR_OUT_OF_MEMORY;
74 : }
75 : return NS_OK;
76 : }
77 :
78 : static void Shutdown() {
79 : delete sPlatformFontList;
80 : sPlatformFontList = nsnull;
81 : }
82 :
83 : virtual ~gfxPlatformFontList();
84 :
85 : // initialize font lists
86 : virtual nsresult InitFontList();
87 :
88 : void GetFontList (nsIAtom *aLangGroup,
89 : const nsACString& aGenericFamily,
90 : nsTArray<nsString>& aListOfFonts);
91 :
92 : virtual bool ResolveFontName(const nsAString& aFontName,
93 : nsAString& aResolvedFontName);
94 :
95 : void UpdateFontList() { InitFontList(); }
96 :
97 0 : void ClearPrefFonts() { mPrefFonts.Clear(); }
98 :
99 : virtual void GetFontFamilyList(nsTArray<nsRefPtr<gfxFontFamily> >& aFamilyArray);
100 :
101 : virtual gfxFontEntry*
102 : SystemFindFontForChar(const PRUint32 aCh,
103 : PRInt32 aRunScript,
104 : const gfxFontStyle* aStyle);
105 :
106 : // TODO: make this virtual, for lazily adding to the font list
107 : virtual gfxFontFamily* FindFamily(const nsAString& aFamily);
108 :
109 : gfxFontEntry* FindFontForFamily(const nsAString& aFamily, const gfxFontStyle* aStyle, bool& aNeedsBold);
110 :
111 : bool GetPrefFontFamilyEntries(eFontPrefLang aLangGroup, nsTArray<nsRefPtr<gfxFontFamily> > *array);
112 : void SetPrefFontFamilyEntries(eFontPrefLang aLangGroup, nsTArray<nsRefPtr<gfxFontFamily> >& array);
113 :
114 : // name lookup table methods
115 :
116 : void AddOtherFamilyName(gfxFontFamily *aFamilyEntry, nsAString& aOtherFamilyName);
117 :
118 : void AddFullname(gfxFontEntry *aFontEntry, nsAString& aFullname);
119 :
120 : void AddPostscriptName(gfxFontEntry *aFontEntry, nsAString& aPostscriptName);
121 :
122 0 : bool NeedFullnamePostscriptNames() { return mNeedFullnamePostscriptNames; }
123 :
124 : // pure virtual functions, to be provided by concrete subclasses
125 :
126 : // get the system default font
127 : virtual gfxFontEntry* GetDefaultFont(const gfxFontStyle* aStyle,
128 : bool& aNeedsBold) = 0;
129 :
130 : // look up a font by name on the host platform
131 : virtual gfxFontEntry* LookupLocalFont(const gfxProxyFontEntry *aProxyEntry,
132 : const nsAString& aFontName) = 0;
133 :
134 : // create a new platform font from downloaded data (@font-face)
135 : // this method is responsible to ensure aFontData is NS_Free()'d
136 : virtual gfxFontEntry* MakePlatformFont(const gfxProxyFontEntry *aProxyEntry,
137 : const PRUint8 *aFontData,
138 : PRUint32 aLength) = 0;
139 :
140 : // get the standard family name on the platform for a given font name
141 : // (platforms may override, eg Mac)
142 : virtual bool GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName);
143 :
144 : protected:
145 : gfxPlatformFontList(bool aNeedFullnamePostscriptNames = true);
146 :
147 : static gfxPlatformFontList *sPlatformFontList;
148 :
149 : static PLDHashOperator FindFontForCharProc(nsStringHashKey::KeyType aKey,
150 : nsRefPtr<gfxFontFamily>& aFamilyEntry,
151 : void* userArg);
152 :
153 : // returns default font for a given character, null otherwise
154 : virtual gfxFontEntry* CommonFontFallback(const PRUint32 aCh,
155 : PRInt32 aRunScript,
156 : const gfxFontStyle* aMatchStyle);
157 :
158 : // search fonts system-wide for a given character, null otherwise
159 : virtual gfxFontEntry* GlobalFontFallback(const PRUint32 aCh,
160 : PRInt32 aRunScript,
161 : const gfxFontStyle* aMatchStyle,
162 : PRUint32& aCmapCount);
163 :
164 : // whether system-based font fallback is used or not
165 : // if system fallback is used, no need to load all cmaps
166 0 : virtual bool UsesSystemFallback() { return false; }
167 :
168 : // separate initialization for reading in name tables, since this is expensive
169 : void InitOtherFamilyNames();
170 :
171 : static PLDHashOperator InitOtherFamilyNamesProc(nsStringHashKey::KeyType aKey,
172 : nsRefPtr<gfxFontFamily>& aFamilyEntry,
173 : void* userArg);
174 :
175 : // read in all fullname/Postscript names for all font faces
176 : void InitFaceNameLists();
177 :
178 : static PLDHashOperator InitFaceNameListsProc(nsStringHashKey::KeyType aKey,
179 : nsRefPtr<gfxFontFamily>& aFamilyEntry,
180 : void* userArg);
181 :
182 : // commonly used fonts for which the name table should be loaded at startup
183 : virtual void PreloadNamesList();
184 :
185 : // load the bad underline blacklist from pref.
186 : void LoadBadUnderlineList();
187 :
188 : // explicitly set fixed-pitch flag for all faces
189 : void SetFixedPitch(const nsAString& aFamilyName);
190 :
191 : void GenerateFontListKey(const nsAString& aKeyName, nsAString& aResult);
192 :
193 : static PLDHashOperator
194 : HashEnumFuncForFamilies(nsStringHashKey::KeyType aKey,
195 : nsRefPtr<gfxFontFamily>& aFamilyEntry,
196 : void* aUserArg);
197 :
198 : // gfxFontInfoLoader overrides, used to load in font cmaps
199 : virtual void InitLoader();
200 : virtual bool RunLoader();
201 : virtual void FinishLoader();
202 :
203 : // canonical family name ==> family entry (unique, one name per family entry)
204 : nsRefPtrHashtable<nsStringHashKey, gfxFontFamily> mFontFamilies;
205 :
206 : // other family name ==> family entry (not unique, can have multiple names per
207 : // family entry, only names *other* than the canonical names are stored here)
208 : nsRefPtrHashtable<nsStringHashKey, gfxFontFamily> mOtherFamilyNames;
209 :
210 : // flag set after InitOtherFamilyNames is called upon first name lookup miss
211 : bool mOtherFamilyNamesInitialized;
212 :
213 : // flag set after fullname and Postcript name lists are populated
214 : bool mFaceNamesInitialized;
215 :
216 : // whether these are needed for a given platform
217 : bool mNeedFullnamePostscriptNames;
218 :
219 : // fullname ==> font entry (unique, one name per font entry)
220 : nsRefPtrHashtable<nsStringHashKey, gfxFontEntry> mFullnames;
221 :
222 : // Postscript name ==> font entry (unique, one name per font entry)
223 : nsRefPtrHashtable<nsStringHashKey, gfxFontEntry> mPostscriptNames;
224 :
225 : // cached pref font lists
226 : // maps list of family names ==> array of family entries, one per lang group
227 : nsDataHashtable<nsUint32HashKey, nsTArray<nsRefPtr<gfxFontFamily> > > mPrefFonts;
228 :
229 : // when system-wide font lookup fails for a character, cache it to skip future searches
230 : gfxSparseBitSet mCodepointsWithNoFonts;
231 :
232 : // the family to use for U+FFFD fallback, to avoid expensive search every time
233 : // on pages with lots of problems
234 : nsString mReplacementCharFallbackFamily;
235 :
236 : nsTHashtable<nsStringHashKey> mBadUnderlineFamilyNames;
237 :
238 : // data used as part of the font cmap loading process
239 : nsTArray<nsRefPtr<gfxFontFamily> > mFontFamiliesToLoad;
240 : PRUint32 mStartIndex;
241 : PRUint32 mIncrement;
242 : PRUint32 mNumFamilies;
243 : };
244 :
245 : #endif /* GFXPLATFORMFONTLIST_H_ */
|