1 /*
2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "config.h"
30 #include <winsock2.h>
31 #include "FontCache.h"
32 #include "Font.h"
33 #include "SimpleFontData.h"
34 #include "UnicodeRange.h"
35 #include <mlang.h>
36 #include <windows.h>
37 #include <wtf/StdLibExtras.h>
38 #include <wtf/text/StringHash.h>
39 #if USE(CG)
40 #include <ApplicationServices/ApplicationServices.h>
41 #include <WebKitSystemInterface/WebKitSystemInterface.h>
42 #endif
43
44 using std::min;
45
46 namespace WebCore
47 {
48
platformInit()49 void FontCache::platformInit()
50 {
51 #if USE(CG)
52 wkSetUpFontCache(1536 * 1024 * 4); // This size matches Mac.
53 #endif
54 }
55
getFontLinkInterface()56 IMLangFontLink2* FontCache::getFontLinkInterface()
57 {
58 static IMultiLanguage *multiLanguage;
59 if (!multiLanguage) {
60 if (CoCreateInstance(CLSID_CMultiLanguage, 0, CLSCTX_ALL, IID_IMultiLanguage, (void**)&multiLanguage) != S_OK)
61 return 0;
62 }
63
64 static IMLangFontLink2* langFontLink;
65 if (!langFontLink) {
66 if (multiLanguage->QueryInterface(&langFontLink) != S_OK)
67 return 0;
68 }
69
70 return langFontLink;
71 }
72
metaFileEnumProc(HDC hdc,HANDLETABLE * table,CONST ENHMETARECORD * record,int tableEntries,LPARAM logFont)73 static int CALLBACK metaFileEnumProc(HDC hdc, HANDLETABLE* table, CONST ENHMETARECORD* record, int tableEntries, LPARAM logFont)
74 {
75 if (record->iType == EMR_EXTCREATEFONTINDIRECTW) {
76 const EMREXTCREATEFONTINDIRECTW* createFontRecord = reinterpret_cast<const EMREXTCREATEFONTINDIRECTW*>(record);
77 *reinterpret_cast<LOGFONT*>(logFont) = createFontRecord->elfw.elfLogFont;
78 }
79 return true;
80 }
81
linkedFontEnumProc(CONST LOGFONT * logFont,CONST TEXTMETRIC * metrics,DWORD fontType,LPARAM hfont)82 static int CALLBACK linkedFontEnumProc(CONST LOGFONT* logFont, CONST TEXTMETRIC* metrics, DWORD fontType, LPARAM hfont)
83 {
84 *reinterpret_cast<HFONT*>(hfont) = CreateFontIndirect(logFont);
85 return false;
86 }
87
getLinkedFonts(String & family)88 static const Vector<String>* getLinkedFonts(String& family)
89 {
90 static HashMap<String, Vector<String>*> systemLinkMap;
91 Vector<String>* result = systemLinkMap.get(family);
92 if (result)
93 return result;
94
95 result = new Vector<String>;
96 systemLinkMap.set(family, result);
97 HKEY fontLinkKey;
98 if (FAILED(RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\FontLink\\SystemLink", 0, KEY_READ, &fontLinkKey)))
99 return result;
100
101 DWORD linkedFontsBufferSize = 0;
102 RegQueryValueEx(fontLinkKey, family.charactersWithNullTermination(), 0, NULL, NULL, &linkedFontsBufferSize);
103 WCHAR* linkedFonts = reinterpret_cast<WCHAR*>(malloc(linkedFontsBufferSize));
104 if (SUCCEEDED(RegQueryValueEx(fontLinkKey, family.charactersWithNullTermination(), 0, NULL, reinterpret_cast<BYTE*>(linkedFonts), &linkedFontsBufferSize))) {
105 unsigned i = 0;
106 unsigned length = linkedFontsBufferSize / sizeof(*linkedFonts);
107 while (i < length) {
108 while (i < length && linkedFonts[i] != ',')
109 i++;
110 i++;
111 unsigned j = i;
112 while (j < length && linkedFonts[j])
113 j++;
114 result->append(String(linkedFonts + i, j - i));
115 i = j + 1;
116 }
117 }
118 free(linkedFonts);
119 RegCloseKey(fontLinkKey);
120 return result;
121 }
122
getCJKCodePageMasks()123 static const Vector<DWORD, 4>& getCJKCodePageMasks()
124 {
125 // The default order in which we look for a font for a CJK character. If the user's default code page is
126 // one of these, we will use it first.
127 static const UINT CJKCodePages[] = {
128 932, /* Japanese */
129 936, /* Simplified Chinese */
130 950, /* Traditional Chinese */
131 949 /* Korean */
132 };
133
134 static Vector<DWORD, 4> codePageMasks;
135 static bool initialized;
136 if (!initialized) {
137 initialized = true;
138 IMLangFontLink2* langFontLink = fontCache()->getFontLinkInterface();
139 if (!langFontLink)
140 return codePageMasks;
141
142 UINT defaultCodePage;
143 DWORD defaultCodePageMask = 0;
144 if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_RETURN_NUMBER | LOCALE_IDEFAULTANSICODEPAGE, reinterpret_cast<LPWSTR>(&defaultCodePage), sizeof(defaultCodePage)))
145 langFontLink->CodePageToCodePages(defaultCodePage, &defaultCodePageMask);
146
147 if (defaultCodePage == CJKCodePages[0] || defaultCodePage == CJKCodePages[1] || defaultCodePage == CJKCodePages[2] || defaultCodePage == CJKCodePages[3])
148 codePageMasks.append(defaultCodePageMask);
149 for (unsigned i = 0; i < 4; ++i) {
150 if (defaultCodePage != CJKCodePages[i]) {
151 DWORD codePageMask;
152 langFontLink->CodePageToCodePages(CJKCodePages[i], &codePageMask);
153 codePageMasks.append(codePageMask);
154 }
155 }
156 }
157 return codePageMasks;
158 }
159
currentFontContainsCharacter(HDC hdc,UChar character)160 static bool currentFontContainsCharacter(HDC hdc, UChar character)
161 {
162 static Vector<char, 512> glyphsetBuffer;
163 glyphsetBuffer.resize(GetFontUnicodeRanges(hdc, 0));
164 GLYPHSET* glyphset = reinterpret_cast<GLYPHSET*>(glyphsetBuffer.data());
165 GetFontUnicodeRanges(hdc, glyphset);
166
167 // FIXME: Change this to a binary search.
168 unsigned i = 0;
169 while (i < glyphset->cRanges && glyphset->ranges[i].wcLow <= character)
170 i++;
171
172 return i && glyphset->ranges[i - 1].wcLow + glyphset->ranges[i - 1].cGlyphs > character;
173 }
174
createMLangFont(IMLangFontLink2 * langFontLink,HDC hdc,DWORD codePageMask,UChar character=0)175 static HFONT createMLangFont(IMLangFontLink2* langFontLink, HDC hdc, DWORD codePageMask, UChar character = 0)
176 {
177 HFONT MLangFont;
178 HFONT hfont = 0;
179 if (SUCCEEDED(langFontLink->MapFont(hdc, codePageMask, character, &MLangFont)) && MLangFont) {
180 LOGFONT lf;
181 GetObject(MLangFont, sizeof(LOGFONT), &lf);
182 langFontLink->ReleaseFont(MLangFont);
183 hfont = CreateFontIndirect(&lf);
184 }
185 return hfont;
186 }
187
getFontDataForCharacters(const Font & font,const UChar * characters,int length)188 const SimpleFontData* FontCache::getFontDataForCharacters(const Font& font, const UChar* characters, int length)
189 {
190 UChar character = characters[0];
191 SimpleFontData* fontData = 0;
192 HDC hdc = GetDC(0);
193 HFONT primaryFont = font.primaryFont()->fontDataForCharacter(character)->platformData().hfont();
194 HGDIOBJ oldFont = SelectObject(hdc, primaryFont);
195 HFONT hfont = 0;
196
197 if (IMLangFontLink2* langFontLink = getFontLinkInterface()) {
198 // Try MLang font linking first.
199 DWORD codePages = 0;
200 langFontLink->GetCharCodePages(character, &codePages);
201
202 if (codePages && findCharUnicodeRange(character) == cRangeSetCJK) {
203 // The CJK character may belong to multiple code pages. We want to
204 // do font linking against a single one of them, preferring the default
205 // code page for the user's locale.
206 const Vector<DWORD, 4>& CJKCodePageMasks = getCJKCodePageMasks();
207 unsigned numCodePages = CJKCodePageMasks.size();
208 for (unsigned i = 0; i < numCodePages && !hfont; ++i) {
209 hfont = createMLangFont(langFontLink, hdc, CJKCodePageMasks[i]);
210 if (hfont && !(codePages & CJKCodePageMasks[i])) {
211 // We asked about a code page that is not one of the code pages
212 // returned by MLang, so the font might not contain the character.
213 SelectObject(hdc, hfont);
214 if (!currentFontContainsCharacter(hdc, character)) {
215 DeleteObject(hfont);
216 hfont = 0;
217 }
218 SelectObject(hdc, primaryFont);
219 }
220 }
221 } else
222 hfont = createMLangFont(langFontLink, hdc, codePages, character);
223 }
224
225 // A font returned from MLang is trusted to contain the character.
226 bool containsCharacter = hfont;
227
228 if (!hfont) {
229 // To find out what font Uniscribe would use, we make it draw into a metafile and intercept
230 // calls to CreateFontIndirect().
231 HDC metaFileDc = CreateEnhMetaFile(hdc, NULL, NULL, NULL);
232 SelectObject(metaFileDc, primaryFont);
233
234 bool scriptStringOutSucceeded = false;
235 SCRIPT_STRING_ANALYSIS ssa;
236
237 // FIXME: If length is greater than 1, we actually return the font for the last character.
238 // This function should be renamed getFontDataForCharacter and take a single 32-bit character.
239 if (SUCCEEDED(ScriptStringAnalyse(metaFileDc, characters, length, 0, -1, SSA_METAFILE | SSA_FALLBACK | SSA_GLYPHS | SSA_LINK,
240 0, NULL, NULL, NULL, NULL, NULL, &ssa))) {
241 scriptStringOutSucceeded = SUCCEEDED(ScriptStringOut(ssa, 0, 0, 0, NULL, 0, 0, FALSE));
242 ScriptStringFree(&ssa);
243 }
244 HENHMETAFILE metaFile = CloseEnhMetaFile(metaFileDc);
245 if (scriptStringOutSucceeded) {
246 LOGFONT logFont;
247 logFont.lfFaceName[0] = 0;
248 EnumEnhMetaFile(0, metaFile, metaFileEnumProc, &logFont, NULL);
249 if (logFont.lfFaceName[0])
250 hfont = CreateFontIndirect(&logFont);
251 }
252 DeleteEnhMetaFile(metaFile);
253 }
254
255 String familyName;
256 const Vector<String>* linkedFonts = 0;
257 unsigned linkedFontIndex = 0;
258 while (hfont) {
259 SelectObject(hdc, hfont);
260 WCHAR name[LF_FACESIZE];
261 GetTextFace(hdc, LF_FACESIZE, name);
262 familyName = name;
263
264 if (containsCharacter || currentFontContainsCharacter(hdc, character))
265 break;
266
267 if (!linkedFonts)
268 linkedFonts = getLinkedFonts(familyName);
269 SelectObject(hdc, oldFont);
270 DeleteObject(hfont);
271 hfont = 0;
272
273 if (linkedFonts->size() <= linkedFontIndex)
274 break;
275
276 LOGFONT logFont;
277 logFont.lfCharSet = DEFAULT_CHARSET;
278 memcpy(logFont.lfFaceName, linkedFonts->at(linkedFontIndex).characters(), linkedFonts->at(linkedFontIndex).length() * sizeof(WCHAR));
279 logFont.lfFaceName[linkedFonts->at(linkedFontIndex).length()] = 0;
280 EnumFontFamiliesEx(hdc, &logFont, linkedFontEnumProc, reinterpret_cast<LPARAM>(&hfont), 0);
281 linkedFontIndex++;
282 }
283
284 if (hfont) {
285 if (!familyName.isEmpty()) {
286 FontPlatformData* result = getCachedFontPlatformData(font.fontDescription(), familyName);
287 if (result)
288 fontData = getCachedFontData(result);
289 }
290
291 SelectObject(hdc, oldFont);
292 DeleteObject(hfont);
293 }
294
295 ReleaseDC(0, hdc);
296 return fontData;
297 }
298
getSimilarFontPlatformData(const Font & font)299 SimpleFontData* FontCache::getSimilarFontPlatformData(const Font& font)
300 {
301 return 0;
302 }
303
fontDataFromDescriptionAndLogFont(FontCache * fontCache,const FontDescription & fontDescription,const LOGFONT & font,AtomicString & outFontFamilyName)304 static SimpleFontData* fontDataFromDescriptionAndLogFont(FontCache* fontCache, const FontDescription& fontDescription, const LOGFONT& font, AtomicString& outFontFamilyName)
305 {
306 AtomicString familyName = String(font.lfFaceName, wcsnlen(font.lfFaceName, LF_FACESIZE));
307 SimpleFontData* fontData = fontCache->getCachedFontData(fontDescription, familyName);
308 if (fontData)
309 outFontFamilyName = familyName;
310 return fontData;
311 }
312
getLastResortFallbackFont(const FontDescription & fontDescription)313 SimpleFontData* FontCache::getLastResortFallbackFont(const FontDescription& fontDescription)
314 {
315 DEFINE_STATIC_LOCAL(AtomicString, fallbackFontName, ());
316 if (!fallbackFontName.isEmpty())
317 return getCachedFontData(fontDescription, fallbackFontName);
318
319 // FIXME: Would be even better to somehow get the user's default font here. For now we'll pick
320 // the default that the user would get without changing any prefs.
321
322 // Search all typical Windows-installed full Unicode fonts.
323 // Sorted by most to least glyphs according to http://en.wikipedia.org/wiki/Unicode_typefaces
324 // Start with Times New Roman also since it is the default if the user doesn't change prefs.
325 static AtomicString fallbackFonts[] = {
326 AtomicString("Times New Roman"),
327 AtomicString("Microsoft Sans Serif"),
328 AtomicString("Tahoma"),
329 AtomicString("Lucida Sans Unicode"),
330 AtomicString("Arial")
331 };
332 SimpleFontData* simpleFont;
333 for (size_t i = 0; i < WTF_ARRAY_LENGTH(fallbackFonts); ++i) {
334 if (simpleFont = getCachedFontData(fontDescription, fallbackFonts[i])) {
335 fallbackFontName = fallbackFonts[i];
336 return simpleFont;
337 }
338 }
339
340 // Fall back to the DEFAULT_GUI_FONT if no known Unicode fonts are available.
341 if (HFONT defaultGUIFont = static_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT))) {
342 LOGFONT defaultGUILogFont;
343 GetObject(defaultGUIFont, sizeof(defaultGUILogFont), &defaultGUILogFont);
344 if (simpleFont = fontDataFromDescriptionAndLogFont(this, fontDescription, defaultGUILogFont, fallbackFontName))
345 return simpleFont;
346 }
347
348 // Fall back to Non-client metrics fonts.
349 NONCLIENTMETRICS nonClientMetrics = {0};
350 nonClientMetrics.cbSize = sizeof(nonClientMetrics);
351 if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(nonClientMetrics), &nonClientMetrics, 0)) {
352 if (simpleFont = fontDataFromDescriptionAndLogFont(this, fontDescription, nonClientMetrics.lfMessageFont, fallbackFontName))
353 return simpleFont;
354 if (simpleFont = fontDataFromDescriptionAndLogFont(this, fontDescription, nonClientMetrics.lfMenuFont, fallbackFontName))
355 return simpleFont;
356 if (simpleFont = fontDataFromDescriptionAndLogFont(this, fontDescription, nonClientMetrics.lfStatusFont, fallbackFontName))
357 return simpleFont;
358 if (simpleFont = fontDataFromDescriptionAndLogFont(this, fontDescription, nonClientMetrics.lfCaptionFont, fallbackFontName))
359 return simpleFont;
360 if (simpleFont = fontDataFromDescriptionAndLogFont(this, fontDescription, nonClientMetrics.lfSmCaptionFont, fallbackFontName))
361 return simpleFont;
362 }
363
364 ASSERT_NOT_REACHED();
365 return 0;
366 }
367
toGDIFontWeight(FontWeight fontWeight)368 static LONG toGDIFontWeight(FontWeight fontWeight)
369 {
370 static LONG gdiFontWeights[] = {
371 FW_THIN, // FontWeight100
372 FW_EXTRALIGHT, // FontWeight200
373 FW_LIGHT, // FontWeight300
374 FW_NORMAL, // FontWeight400
375 FW_MEDIUM, // FontWeight500
376 FW_SEMIBOLD, // FontWeight600
377 FW_BOLD, // FontWeight700
378 FW_EXTRABOLD, // FontWeight800
379 FW_HEAVY // FontWeight900
380 };
381 return gdiFontWeights[fontWeight];
382 }
383
isGDIFontWeightBold(LONG gdiFontWeight)384 static inline bool isGDIFontWeightBold(LONG gdiFontWeight)
385 {
386 return gdiFontWeight >= FW_SEMIBOLD;
387 }
388
adjustedGDIFontWeight(LONG gdiFontWeight,const String & family)389 static LONG adjustedGDIFontWeight(LONG gdiFontWeight, const String& family)
390 {
391 static AtomicString lucidaStr("Lucida Grande");
392 if (equalIgnoringCase(family, lucidaStr)) {
393 if (gdiFontWeight == FW_NORMAL)
394 return FW_MEDIUM;
395 if (gdiFontWeight == FW_BOLD)
396 return FW_SEMIBOLD;
397 }
398 return gdiFontWeight;
399 }
400
401 struct MatchImprovingProcData {
MatchImprovingProcDataWebCore::MatchImprovingProcData402 MatchImprovingProcData(LONG desiredWeight, bool desiredItalic)
403 : m_desiredWeight(desiredWeight)
404 , m_desiredItalic(desiredItalic)
405 , m_hasMatched(false)
406 {
407 }
408
409 LONG m_desiredWeight;
410 bool m_desiredItalic;
411 bool m_hasMatched;
412 LOGFONT m_chosen;
413 };
414
matchImprovingEnumProc(CONST LOGFONT * candidate,CONST TEXTMETRIC * metrics,DWORD fontType,LPARAM lParam)415 static int CALLBACK matchImprovingEnumProc(CONST LOGFONT* candidate, CONST TEXTMETRIC* metrics, DWORD fontType, LPARAM lParam)
416 {
417 MatchImprovingProcData* matchData = reinterpret_cast<MatchImprovingProcData*>(lParam);
418
419 if (!matchData->m_hasMatched) {
420 matchData->m_hasMatched = true;
421 matchData->m_chosen = *candidate;
422 return 1;
423 }
424
425 if (!candidate->lfItalic != !matchData->m_chosen.lfItalic) {
426 if (!candidate->lfItalic == !matchData->m_desiredItalic)
427 matchData->m_chosen = *candidate;
428
429 return 1;
430 }
431
432 unsigned chosenWeightDeltaMagnitude = abs(matchData->m_chosen.lfWeight - matchData->m_desiredWeight);
433 unsigned candidateWeightDeltaMagnitude = abs(candidate->lfWeight - matchData->m_desiredWeight);
434
435 // If both are the same distance from the desired weight, prefer the candidate if it is further from regular.
436 if (chosenWeightDeltaMagnitude == candidateWeightDeltaMagnitude && abs(candidate->lfWeight - FW_NORMAL) > abs(matchData->m_chosen.lfWeight - FW_NORMAL)) {
437 matchData->m_chosen = *candidate;
438 return 1;
439 }
440
441 // Otherwise, prefer the one closer to the desired weight.
442 if (candidateWeightDeltaMagnitude < chosenWeightDeltaMagnitude)
443 matchData->m_chosen = *candidate;
444
445 return 1;
446 }
447
createGDIFont(const AtomicString & family,LONG desiredWeight,bool desiredItalic,int size,bool synthesizeItalic)448 static HFONT createGDIFont(const AtomicString& family, LONG desiredWeight, bool desiredItalic, int size, bool synthesizeItalic)
449 {
450 HDC hdc = GetDC(0);
451
452 LOGFONT logFont;
453 logFont.lfCharSet = DEFAULT_CHARSET;
454 unsigned familyLength = min(family.length(), static_cast<unsigned>(LF_FACESIZE - 1));
455 memcpy(logFont.lfFaceName, family.characters(), familyLength * sizeof(UChar));
456 logFont.lfFaceName[familyLength] = 0;
457 logFont.lfPitchAndFamily = 0;
458
459 MatchImprovingProcData matchData(desiredWeight, desiredItalic);
460 EnumFontFamiliesEx(hdc, &logFont, matchImprovingEnumProc, reinterpret_cast<LPARAM>(&matchData), 0);
461
462 ReleaseDC(0, hdc);
463
464 if (!matchData.m_hasMatched)
465 return 0;
466
467 matchData.m_chosen.lfHeight = -size;
468 matchData.m_chosen.lfWidth = 0;
469 matchData.m_chosen.lfEscapement = 0;
470 matchData.m_chosen.lfOrientation = 0;
471 matchData.m_chosen.lfUnderline = false;
472 matchData.m_chosen.lfStrikeOut = false;
473 matchData.m_chosen.lfCharSet = DEFAULT_CHARSET;
474 #if USE(CG) || USE(CAIRO)
475 matchData.m_chosen.lfOutPrecision = OUT_TT_ONLY_PRECIS;
476 #else
477 matchData.m_chosen.lfOutPrecision = OUT_TT_PRECIS;
478 #endif
479 matchData.m_chosen.lfQuality = DEFAULT_QUALITY;
480 matchData.m_chosen.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
481
482 if (desiredItalic && !matchData.m_chosen.lfItalic && synthesizeItalic)
483 matchData.m_chosen.lfItalic = 1;
484
485 HFONT result = CreateFontIndirect(&matchData.m_chosen);
486 if (!result)
487 return 0;
488
489 HDC dc = GetDC(0);
490 SaveDC(dc);
491 SelectObject(dc, result);
492 WCHAR actualName[LF_FACESIZE];
493 GetTextFace(dc, LF_FACESIZE, actualName);
494 RestoreDC(dc, -1);
495 ReleaseDC(0, dc);
496
497 if (wcsicmp(matchData.m_chosen.lfFaceName, actualName)) {
498 DeleteObject(result);
499 result = 0;
500 }
501
502 return result;
503 }
504
505 struct TraitsInFamilyProcData {
TraitsInFamilyProcDataWebCore::TraitsInFamilyProcData506 TraitsInFamilyProcData(const AtomicString& familyName)
507 : m_familyName(familyName)
508 {
509 }
510
511 const AtomicString& m_familyName;
512 HashSet<unsigned> m_traitsMasks;
513 };
514
traitsInFamilyEnumProc(CONST LOGFONT * logFont,CONST TEXTMETRIC * metrics,DWORD fontType,LPARAM lParam)515 static int CALLBACK traitsInFamilyEnumProc(CONST LOGFONT* logFont, CONST TEXTMETRIC* metrics, DWORD fontType, LPARAM lParam)
516 {
517 TraitsInFamilyProcData* procData = reinterpret_cast<TraitsInFamilyProcData*>(lParam);
518
519 unsigned traitsMask = 0;
520 traitsMask |= logFont->lfItalic ? FontStyleItalicMask : FontStyleNormalMask;
521 traitsMask |= FontVariantNormalMask;
522 LONG weight = adjustedGDIFontWeight(logFont->lfWeight, procData->m_familyName);
523 traitsMask |= weight == FW_THIN ? FontWeight100Mask :
524 weight == FW_EXTRALIGHT ? FontWeight200Mask :
525 weight == FW_LIGHT ? FontWeight300Mask :
526 weight == FW_NORMAL ? FontWeight400Mask :
527 weight == FW_MEDIUM ? FontWeight500Mask :
528 weight == FW_SEMIBOLD ? FontWeight600Mask :
529 weight == FW_BOLD ? FontWeight700Mask :
530 weight == FW_EXTRABOLD ? FontWeight800Mask :
531 FontWeight900Mask;
532 procData->m_traitsMasks.add(traitsMask);
533 return 1;
534 }
getTraitsInFamily(const AtomicString & familyName,Vector<unsigned> & traitsMasks)535 void FontCache::getTraitsInFamily(const AtomicString& familyName, Vector<unsigned>& traitsMasks)
536 {
537 HDC hdc = GetDC(0);
538
539 LOGFONT logFont;
540 logFont.lfCharSet = DEFAULT_CHARSET;
541 unsigned familyLength = min(familyName.length(), static_cast<unsigned>(LF_FACESIZE - 1));
542 memcpy(logFont.lfFaceName, familyName.characters(), familyLength * sizeof(UChar));
543 logFont.lfFaceName[familyLength] = 0;
544 logFont.lfPitchAndFamily = 0;
545
546 TraitsInFamilyProcData procData(familyName);
547 EnumFontFamiliesEx(hdc, &logFont, traitsInFamilyEnumProc, reinterpret_cast<LPARAM>(&procData), 0);
548 copyToVector(procData.m_traitsMasks, traitsMasks);
549
550 ReleaseDC(0, hdc);
551 }
552
createFontPlatformData(const FontDescription & fontDescription,const AtomicString & family)553 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family)
554 {
555 bool isLucidaGrande = false;
556 static AtomicString lucidaStr("Lucida Grande");
557 if (equalIgnoringCase(family, lucidaStr))
558 isLucidaGrande = true;
559
560 bool useGDI = fontDescription.renderingMode() == AlternateRenderingMode && !isLucidaGrande;
561
562 // The logical size constant is 32. We do this for subpixel precision when rendering using Uniscribe.
563 // This masks rounding errors related to the HFONT metrics being different from the CGFont metrics.
564 // FIXME: We will eventually want subpixel precision for GDI mode, but the scaled rendering doesn't
565 // look as nice. That may be solvable though.
566 LONG weight = adjustedGDIFontWeight(toGDIFontWeight(fontDescription.weight()), family);
567 HFONT hfont = createGDIFont(family, weight, fontDescription.italic(),
568 fontDescription.computedPixelSize() * (useGDI ? 1 : 32), useGDI);
569
570 if (!hfont)
571 return 0;
572
573 if (isLucidaGrande)
574 useGDI = false; // Never use GDI for Lucida Grande.
575
576 LOGFONT logFont;
577 GetObject(hfont, sizeof(LOGFONT), &logFont);
578
579 bool synthesizeBold = isGDIFontWeightBold(weight) && !isGDIFontWeightBold(logFont.lfWeight);
580 bool synthesizeItalic = fontDescription.italic() && !logFont.lfItalic;
581
582 FontPlatformData* result = new FontPlatformData(hfont, fontDescription.computedPixelSize(), synthesizeBold, synthesizeItalic, useGDI);
583
584 #if USE(CG)
585 bool fontCreationFailed = !result->cgFont();
586 #elif USE(CAIRO)
587 bool fontCreationFailed = !result->scaledFont();
588 #endif
589
590 if (fontCreationFailed) {
591 // The creation of the CGFontRef failed for some reason. We already asserted in debug builds, but to make
592 // absolutely sure that we don't use this font, go ahead and return 0 so that we can fall back to the next
593 // font.
594 delete result;
595 DeleteObject(hfont);
596 return 0;
597 }
598
599 return result;
600 }
601
602 }
603
604