Bumping manifests a=b2g-bump
[gecko.git] / gfx / thebes / gfxGDIFontList.h
blob9b1b86279961a088a1af46df39c98949bfd224a8
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/. */
6 #ifndef GFX_GDIFONTLIST_H
7 #define GFX_GDIFONTLIST_H
9 #include "mozilla/MemoryReporting.h"
10 #include "gfxWindowsPlatform.h"
11 #include "gfxPlatformFontList.h"
12 #include "nsGkAtoms.h"
14 #include <windows.h>
16 class AutoDC // get the global device context, and auto-release it on destruction
18 public:
19 AutoDC() {
20 mDC = ::GetDC(nullptr);
23 ~AutoDC() {
24 ::ReleaseDC(nullptr, mDC);
27 HDC GetDC() {
28 return mDC;
31 private:
32 HDC mDC;
35 class AutoSelectFont // select a font into the given DC, and auto-restore
37 public:
38 AutoSelectFont(HDC aDC, LOGFONTW *aLogFont)
39 : mOwnsFont(false)
41 mFont = ::CreateFontIndirectW(aLogFont);
42 if (mFont) {
43 mOwnsFont = true;
44 mDC = aDC;
45 mOldFont = (HFONT)::SelectObject(aDC, mFont);
46 } else {
47 mOldFont = nullptr;
51 AutoSelectFont(HDC aDC, HFONT aFont)
52 : mOwnsFont(false)
54 mDC = aDC;
55 mFont = aFont;
56 mOldFont = (HFONT)::SelectObject(aDC, aFont);
59 ~AutoSelectFont() {
60 if (mOldFont) {
61 ::SelectObject(mDC, mOldFont);
62 if (mOwnsFont) {
63 ::DeleteObject(mFont);
68 bool IsValid() const {
69 return mFont != nullptr;
72 HFONT GetFont() const {
73 return mFont;
76 private:
77 HDC mDC;
78 HFONT mFont;
79 HFONT mOldFont;
80 bool mOwnsFont;
83 /**
84 * List of different types of fonts we support on Windows.
85 * These can generally be lumped in to 3 categories where we have to
86 * do special things: Really old fonts bitmap and vector fonts (device
87 * and raster), Type 1 fonts, and TrueType/OpenType fonts.
89 * This list is sorted in order from least prefered to most prefered.
90 * We prefer Type1 fonts over OpenType fonts to avoid falling back to
91 * things like Arial (opentype) when you ask for Helvetica (type1)
92 **/
93 enum gfxWindowsFontType {
94 GFX_FONT_TYPE_UNKNOWN = 0,
95 GFX_FONT_TYPE_DEVICE,
96 GFX_FONT_TYPE_RASTER,
97 GFX_FONT_TYPE_TRUETYPE,
98 GFX_FONT_TYPE_PS_OPENTYPE,
99 GFX_FONT_TYPE_TT_OPENTYPE,
100 GFX_FONT_TYPE_TYPE1
103 // A single member of a font family (i.e. a single face, such as Times Italic)
104 // represented as a LOGFONT that will resolve to the correct face.
105 // This replaces FontEntry from gfxWindowsFonts.h/cpp.
106 class GDIFontEntry : public gfxFontEntry
108 public:
109 LPLOGFONTW GetLogFont() { return &mLogFont; }
111 nsresult ReadCMAP(FontInfoData *aFontInfoData = nullptr);
113 virtual bool IsSymbolFont();
115 void FillLogFont(LOGFONTW *aLogFont, uint16_t aWeight, gfxFloat aSize,
116 bool aUseCleartype);
118 static gfxWindowsFontType DetermineFontType(const NEWTEXTMETRICW& metrics,
119 DWORD fontType)
121 gfxWindowsFontType feType;
122 if (metrics.ntmFlags & NTM_TYPE1)
123 feType = GFX_FONT_TYPE_TYPE1;
124 else if (metrics.ntmFlags & NTM_PS_OPENTYPE)
125 feType = GFX_FONT_TYPE_PS_OPENTYPE;
126 else if (metrics.ntmFlags & NTM_TT_OPENTYPE)
127 feType = GFX_FONT_TYPE_TT_OPENTYPE;
128 else if (fontType == TRUETYPE_FONTTYPE)
129 feType = GFX_FONT_TYPE_TRUETYPE;
130 else if (fontType == RASTER_FONTTYPE)
131 feType = GFX_FONT_TYPE_RASTER;
132 else if (fontType == DEVICE_FONTTYPE)
133 feType = GFX_FONT_TYPE_DEVICE;
134 else
135 feType = GFX_FONT_TYPE_UNKNOWN;
137 return feType;
140 bool IsType1() const {
141 return (mFontType == GFX_FONT_TYPE_TYPE1);
144 bool IsTrueType() const {
145 return (mFontType == GFX_FONT_TYPE_TRUETYPE ||
146 mFontType == GFX_FONT_TYPE_PS_OPENTYPE ||
147 mFontType == GFX_FONT_TYPE_TT_OPENTYPE);
150 virtual bool MatchesGenericFamily(const nsACString& aGeneric) const {
151 if (aGeneric.IsEmpty()) {
152 return true;
155 // Japanese 'Mincho' fonts do not belong to FF_MODERN even if
156 // they are fixed pitch because they have variable stroke width.
157 if (mWindowsFamily == FF_ROMAN && mWindowsPitch & FIXED_PITCH) {
158 return aGeneric.EqualsLiteral("monospace");
161 // Japanese 'Gothic' fonts do not belong to FF_SWISS even if
162 // they are variable pitch because they have constant stroke width.
163 if (mWindowsFamily == FF_MODERN && mWindowsPitch & VARIABLE_PITCH) {
164 return aGeneric.EqualsLiteral("sans-serif");
167 // All other fonts will be grouped correctly using family...
168 switch (mWindowsFamily) {
169 case FF_DONTCARE:
170 return false;
171 case FF_ROMAN:
172 return aGeneric.EqualsLiteral("serif");
173 case FF_SWISS:
174 return aGeneric.EqualsLiteral("sans-serif");
175 case FF_MODERN:
176 return aGeneric.EqualsLiteral("monospace");
177 case FF_SCRIPT:
178 return aGeneric.EqualsLiteral("cursive");
179 case FF_DECORATIVE:
180 return aGeneric.EqualsLiteral("fantasy");
183 return false;
186 virtual bool SupportsLangGroup(nsIAtom* aLangGroup) const {
187 if (!aLangGroup || aLangGroup == nsGkAtoms::Unicode) {
188 return true;
191 int16_t bit = -1;
193 /* map our langgroup names in to Windows charset bits */
194 if (aLangGroup == nsGkAtoms::x_western) {
195 bit = ANSI_CHARSET;
196 } else if (aLangGroup == nsGkAtoms::Japanese) {
197 bit = SHIFTJIS_CHARSET;
198 } else if (aLangGroup == nsGkAtoms::ko) {
199 bit = HANGEUL_CHARSET;
200 } else if (aLangGroup == nsGkAtoms::ko_xxx) {
201 bit = JOHAB_CHARSET;
202 } else if (aLangGroup == nsGkAtoms::zh_cn) {
203 bit = GB2312_CHARSET;
204 } else if (aLangGroup == nsGkAtoms::zh_tw) {
205 bit = CHINESEBIG5_CHARSET;
206 } else if (aLangGroup == nsGkAtoms::el_) {
207 bit = GREEK_CHARSET;
208 } else if (aLangGroup == nsGkAtoms::he) {
209 bit = HEBREW_CHARSET;
210 } else if (aLangGroup == nsGkAtoms::ar) {
211 bit = ARABIC_CHARSET;
212 } else if (aLangGroup == nsGkAtoms::x_cyrillic) {
213 bit = RUSSIAN_CHARSET;
214 } else if (aLangGroup == nsGkAtoms::th) {
215 bit = THAI_CHARSET;
216 } else if (aLangGroup == nsGkAtoms::x_symbol) {
217 bit = SYMBOL_CHARSET;
220 if (bit != -1) {
221 return mCharset.test(bit);
224 return false;
227 virtual bool SupportsRange(uint8_t range) {
228 return mUnicodeRanges.test(range);
231 virtual bool SkipDuringSystemFallback() {
232 return !HasCmapTable(); // explicitly skip non-SFNT fonts
235 virtual bool TestCharacterMap(uint32_t aCh);
237 virtual void AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf,
238 FontListSizes* aSizes) const;
240 // create a font entry for a font with a given name
241 static GDIFontEntry* CreateFontEntry(const nsAString& aName,
242 gfxWindowsFontType aFontType,
243 bool aItalic,
244 uint16_t aWeight, int16_t aStretch,
245 gfxUserFontData* aUserFontData,
246 bool aFamilyHasItalicFace);
248 // create a font entry for a font referenced by its fullname
249 static GDIFontEntry* LoadLocalFont(const gfxProxyFontEntry &aProxyEntry,
250 const nsAString& aFullname);
252 uint8_t mWindowsFamily;
253 uint8_t mWindowsPitch;
255 gfxWindowsFontType mFontType;
256 bool mForceGDI : 1;
258 // For src:local user-fonts, we keep track of whether the platform family
259 // contains an italic face, because in this case we can't safely ask GDI
260 // to create synthetic italics (oblique) via the LOGFONT.
261 // (For other types of font, this is just set to false.)
262 bool mFamilyHasItalicFace : 1;
264 gfxSparseBitSet mCharset;
265 gfxSparseBitSet mUnicodeRanges;
267 protected:
268 friend class gfxWindowsFont;
270 GDIFontEntry(const nsAString& aFaceName, gfxWindowsFontType aFontType,
271 bool aItalic, uint16_t aWeight, int16_t aStretch,
272 gfxUserFontData *aUserFontData, bool aFamilyHasItalicFace);
274 void InitLogFont(const nsAString& aName, gfxWindowsFontType aFontType);
276 virtual gfxFont *CreateFontInstance(const gfxFontStyle *aFontStyle, bool aNeedsBold);
278 virtual nsresult CopyFontTable(uint32_t aTableTag,
279 FallibleTArray<uint8_t>& aBuffer) MOZ_OVERRIDE;
281 LOGFONTW mLogFont;
284 // a single font family, referencing one or more faces
285 class GDIFontFamily : public gfxFontFamily
287 public:
288 GDIFontFamily(nsAString &aName) :
289 gfxFontFamily(aName) {}
291 virtual void FindStyleVariations(FontInfoData *aFontInfoData = nullptr);
293 private:
294 static int CALLBACK FamilyAddStylesProc(const ENUMLOGFONTEXW *lpelfe,
295 const NEWTEXTMETRICEXW *nmetrics,
296 DWORD fontType, LPARAM data);
299 class gfxGDIFontList : public gfxPlatformFontList {
300 public:
301 static gfxGDIFontList* PlatformFontList() {
302 return static_cast<gfxGDIFontList*>(sPlatformFontList);
305 // initialize font lists
306 virtual nsresult InitFontList();
308 virtual gfxFontFamily* GetDefaultFont(const gfxFontStyle* aStyle);
310 virtual gfxFontFamily* FindFamily(const nsAString& aFamily,
311 bool aUseSystemFonts = false);
313 virtual gfxFontEntry* LookupLocalFont(const gfxProxyFontEntry *aProxyEntry,
314 const nsAString& aFontName);
316 virtual gfxFontEntry* MakePlatformFont(const gfxProxyFontEntry *aProxyEntry,
317 const uint8_t *aFontData, uint32_t aLength);
319 virtual void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf,
320 FontListSizes* aSizes) const;
321 virtual void AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf,
322 FontListSizes* aSizes) const;
324 private:
325 friend class gfxWindowsPlatform;
327 gfxGDIFontList();
329 nsresult GetFontSubstitutes();
331 static int CALLBACK EnumFontFamExProc(ENUMLOGFONTEXW *lpelfe,
332 NEWTEXTMETRICEXW *lpntme,
333 DWORD fontType,
334 LPARAM lParam);
336 virtual already_AddRefed<FontInfoData> CreateFontInfoData();
338 #ifdef MOZ_BUNDLED_FONTS
339 void ActivateBundledFonts();
340 #endif
342 typedef nsRefPtrHashtable<nsStringHashKey, gfxFontFamily> FontTable;
344 FontTable mFontSubstitutes;
345 nsTArray<nsString> mNonExistingFonts;
348 #endif /* GFX_GDIFONTLIST_H */