Bug 460926 A11y hierachy is broken on Ubuntu 8.10 (GNOME 2.24), r=Evan.Yan sr=roc
[wine-gecko.git] / gfx / thebes / public / gfxUserFontSet.h
blobd0c382b7193b8bca59a3a4ca070adfcb3c72d753
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
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/
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.
15 * The Original Code is Mozilla Foundation code.
17 * The Initial Developer of the Original Code is Mozilla Foundation.
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * John Daggett <jdaggett@mozilla.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef GFX_USER_FONT_SET_H
39 #define GFX_USER_FONT_SET_H
41 #include "gfxTypes.h"
42 #include "gfxFont.h"
43 #include "gfxFontUtils.h"
44 #include "nsRefPtrHashtable.h"
45 #include "nsAutoPtr.h"
46 #include "nsCOMPtr.h"
47 #include "nsIURI.h"
48 #include "nsIFile.h"
50 class nsIURI;
51 class gfxMixedFontFamily;
53 // parsed CSS @font-face rule information
54 // lifetime: from when @font-face rule processed until font is loaded
55 struct gfxFontFaceSrc {
56 PRPackedBool mIsLocal; // url or local
58 // if url, whether to use the origin principal or not
59 PRPackedBool mUseOriginPrincipal;
61 // format hint flags, union of all possible formats
62 // (e.g. TrueType, EOT, SVG, etc.)
63 // see FLAG_FORMAT_* enum values below
64 PRUint32 mFormatFlags;
66 nsString mLocalName; // full font name if local
67 nsCOMPtr<nsIURI> mURI; // uri if url
68 nsCOMPtr<nsIURI> mReferrer; // referrer url if url
69 nsCOMPtr<nsISupports> mOriginPrincipal; // principal if url
73 // subclassed to store platform-specific code cleaned out when font entry is deleted
74 // lifetime: from when platform font is created until it is deactivated
75 class gfxUserFontData {
76 public:
77 gfxUserFontData() { }
78 virtual ~gfxUserFontData() { }
81 // initially contains a set of proxy font entry objects, replaced with
82 // platform/user fonts as downloaded
84 class gfxMixedFontFamily : public gfxFontFamily {
86 public:
87 gfxMixedFontFamily(const nsAString& aName)
88 : gfxFontFamily(aName)
89 { }
91 virtual ~gfxMixedFontFamily() { }
93 void AddFontEntry(gfxFontEntry *aFontEntry) {
94 nsRefPtr<gfxFontEntry> fe = aFontEntry;
95 mAvailableFonts.AppendElement(fe);
98 void ReplaceFontEntry(gfxFontEntry *aOldFontEntry, gfxFontEntry *aNewFontEntry)
100 PRUint32 numFonts = mAvailableFonts.Length();
101 for (PRUint32 i = 0; i < numFonts; i++) {
102 gfxFontEntry *fe = mAvailableFonts[i];
103 if (fe == aOldFontEntry) {
104 mAvailableFonts[i] = aNewFontEntry;
105 return;
110 void RemoveFontEntry(gfxFontEntry *aFontEntry)
112 PRUint32 numFonts = mAvailableFonts.Length();
113 for (PRUint32 i = 0; i < numFonts; i++) {
114 gfxFontEntry *fe = mAvailableFonts[i];
115 if (fe == aFontEntry) {
116 mAvailableFonts.RemoveElementAt(i);
117 return;
122 // temp method to determine if all proxies are loaded
123 PRBool AllLoaded()
125 PRUint32 numFonts = mAvailableFonts.Length();
126 for (PRUint32 i = 0; i < numFonts; i++) {
127 gfxFontEntry *fe = mAvailableFonts[i];
128 if (fe->mIsProxy)
129 return PR_FALSE;
131 return PR_TRUE;
134 nsTArray<nsRefPtr<gfxFontEntry> > mAvailableFonts;
136 protected:
137 PRBool FindWeightsForStyle(gfxFontEntry* aFontsForWeights[],
138 const gfxFontStyle& aFontStyle);
142 class gfxProxyFontEntry;
144 class THEBES_API gfxUserFontSet {
146 public:
147 class LoaderContext;
148 typedef nsresult (*LoaderCallback) (gfxFontEntry *aFontToLoad,
149 const gfxFontFaceSrc *aFontFaceSrc,
150 LoaderContext *aContextData);
152 class LoaderContext {
153 public:
154 LoaderContext(LoaderCallback aLoader)
155 : mUserFontSet(nsnull), mLoaderProc(aLoader) { }
156 virtual ~LoaderContext() { }
158 gfxUserFontSet* mUserFontSet;
159 LoaderCallback mLoaderProc;
162 THEBES_INLINE_DECL_REFCOUNTING(gfxUserFontSet)
164 gfxUserFontSet(LoaderContext *aContext);
165 virtual ~gfxUserFontSet();
167 enum {
168 // no flags ==> unknown
169 FLAG_FORMAT_OPENTYPE = 1,
170 FLAG_FORMAT_TRUETYPE = 2,
171 FLAG_FORMAT_TRUETYPE_AAT = 4,
172 FLAG_FORMAT_EOT = 8,
173 FLAG_FORMAT_SVG = 16
176 enum LoadStatus {
177 STATUS_LOADING = 0,
178 STATUS_LOADED,
179 STATUS_FORMAT_NOT_SUPPORTED,
180 STATUS_ERROR,
181 STATUS_END_OF_LIST
185 // add in a font face
186 // weight, stretch - 0 == unknown, [1, 9] otherwise
187 // italic style = constants in gfxFont.h (e.g. FONT_STYLE_NORMAL)
188 // TODO: support for unicode ranges not yet implemented
189 void AddFontFace(const nsAString& aFamilyName,
190 const nsTArray<gfxFontFaceSrc>& aFontFaceSrcList,
191 PRUint32 aWeight = 0,
192 PRUint32 aStretch = 0,
193 PRUint32 aItalicStyle = 0,
194 gfxSparseBitSet *aUnicodeRanges = nsnull);
196 // Whether there is a face with this family name
197 PRBool HasFamily(const nsAString& aFamilyName) const
199 return GetFamily(aFamilyName) != nsnull;
202 // lookup a font entry for a given style, returns null if not loaded
203 gfxFontEntry *FindFontEntry(const nsAString& aName,
204 const gfxFontStyle& aFontStyle, PRBool& aNeedsBold);
206 // when download has been completed, pass back data here
207 // aDownloadStatus == NS_OK ==> download succeeded, error otherwise
208 // returns true if platform font creation sucessful (or local()
209 // reference was next in line)
210 PRBool OnLoadComplete(gfxFontEntry *aFontToLoad, nsISupports *aLoader,
211 const PRUint8 *aFontData, PRUint32 aLength,
212 nsresult aDownloadStatus);
214 // generation - each time a face is loaded, generation is
215 // incremented so that the change can be recognized
216 PRUint64 GetGeneration() { return mGeneration; }
218 protected:
219 // for a given proxy font entry, attempt to load the next resource
220 // in the src list
221 LoadStatus LoadNext(gfxProxyFontEntry *aProxyEntry);
223 // increment the generation on font load
224 void IncrementGeneration();
226 gfxMixedFontFamily *GetFamily(const nsAString& aName) const;
228 // remove family
229 void RemoveFamily(const nsAString& aFamilyName);
231 // font families defined by @font-face rules
232 nsRefPtrHashtable<nsStringHashKey, gfxMixedFontFamily> mFontFamilies;
234 PRUint64 mGeneration;
236 // owned by user font set obj, deleted within destructor
237 nsAutoPtr<LoaderContext> mLoaderContext;
240 // acts a placeholder until the real font is downloaded
242 class gfxProxyFontEntry : public gfxFontEntry {
244 public:
245 gfxProxyFontEntry(const nsTArray<gfxFontFaceSrc>& aFontFaceSrcList,
246 gfxMixedFontFamily *aFamily,
247 PRUint32 aWeight,
248 PRUint32 aStretch,
249 PRUint32 aItalicStyle,
250 gfxSparseBitSet *aUnicodeRanges);
252 virtual ~gfxProxyFontEntry();
254 PRPackedBool mIsLoading;
255 nsTArray<gfxFontFaceSrc> mSrcList;
256 PRUint32 mSrcIndex; // index of loading src item
257 gfxMixedFontFamily* mFamily;
261 #endif /* GFX_USER_FONT_SET_H */