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"
16 class AutoDC
// get the global device context, and auto-release it on destruction
20 mDC
= ::GetDC(nullptr);
24 ::ReleaseDC(nullptr, mDC
);
35 class AutoSelectFont
// select a font into the given DC, and auto-restore
38 AutoSelectFont(HDC aDC
, LOGFONTW
*aLogFont
)
41 mFont
= ::CreateFontIndirectW(aLogFont
);
45 mOldFont
= (HFONT
)::SelectObject(aDC
, mFont
);
51 AutoSelectFont(HDC aDC
, HFONT aFont
)
56 mOldFont
= (HFONT
)::SelectObject(aDC
, aFont
);
61 ::SelectObject(mDC
, mOldFont
);
63 ::DeleteObject(mFont
);
68 bool IsValid() const {
69 return mFont
!= nullptr;
72 HFONT
GetFont() const {
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)
93 enum gfxWindowsFontType
{
94 GFX_FONT_TYPE_UNKNOWN
= 0,
97 GFX_FONT_TYPE_TRUETYPE
,
98 GFX_FONT_TYPE_PS_OPENTYPE
,
99 GFX_FONT_TYPE_TT_OPENTYPE
,
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
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
,
118 static gfxWindowsFontType
DetermineFontType(const NEWTEXTMETRICW
& metrics
,
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
;
135 feType
= GFX_FONT_TYPE_UNKNOWN
;
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()) {
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
) {
172 return aGeneric
.EqualsLiteral("serif");
174 return aGeneric
.EqualsLiteral("sans-serif");
176 return aGeneric
.EqualsLiteral("monospace");
178 return aGeneric
.EqualsLiteral("cursive");
180 return aGeneric
.EqualsLiteral("fantasy");
186 virtual bool SupportsLangGroup(nsIAtom
* aLangGroup
) const {
187 if (!aLangGroup
|| aLangGroup
== nsGkAtoms::Unicode
) {
193 /* map our langgroup names in to Windows charset bits */
194 if (aLangGroup
== nsGkAtoms::x_western
) {
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
) {
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_
) {
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
) {
216 } else if (aLangGroup
== nsGkAtoms::x_symbol
) {
217 bit
= SYMBOL_CHARSET
;
221 return mCharset
.test(bit
);
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
,
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
;
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
;
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
;
284 // a single font family, referencing one or more faces
285 class GDIFontFamily
: public gfxFontFamily
288 GDIFontFamily(nsAString
&aName
) :
289 gfxFontFamily(aName
) {}
291 virtual void FindStyleVariations(FontInfoData
*aFontInfoData
= nullptr);
294 static int CALLBACK
FamilyAddStylesProc(const ENUMLOGFONTEXW
*lpelfe
,
295 const NEWTEXTMETRICEXW
*nmetrics
,
296 DWORD fontType
, LPARAM data
);
299 class gfxGDIFontList
: public gfxPlatformFontList
{
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;
325 friend class gfxWindowsPlatform
;
329 nsresult
GetFontSubstitutes();
331 static int CALLBACK
EnumFontFamExProc(ENUMLOGFONTEXW
*lpelfe
,
332 NEWTEXTMETRICEXW
*lpntme
,
336 virtual already_AddRefed
<FontInfoData
> CreateFontInfoData();
338 #ifdef MOZ_BUNDLED_FONTS
339 void ActivateBundledFonts();
342 typedef nsRefPtrHashtable
<nsStringHashKey
, gfxFontFamily
> FontTable
;
344 FontTable mFontSubstitutes
;
345 nsTArray
<nsString
> mNonExistingFonts
;
348 #endif /* GFX_GDIFONTLIST_H */