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 "StringHash.h"
35 #include "UnicodeRange.h"
36 #include <mlang.h>
37 #include <windows.h>
38 #include <wtf/StdLibExtras.h>
39 #if PLATFORM(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 PLATFORM(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
getLastResortFallbackFont(const FontDescription & fontDescription)304 SimpleFontData* FontCache::getLastResortFallbackFont(const FontDescription& fontDescription)
305 {
306 // FIXME: Would be even better to somehow get the user's default font here. For now we'll pick
307 // the default that the user would get without changing any prefs.
308 static AtomicString timesStr("Times New Roman");
309 if (SimpleFontData* simpleFont = getCachedFontData(fontDescription, timesStr))
310 return simpleFont;
311
312 DEFINE_STATIC_LOCAL(String, defaultGUIFontFamily, ());
313 if (defaultGUIFontFamily.isEmpty()) {
314 HFONT defaultGUIFont = static_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT));
315 LOGFONT logFont;
316 GetObject(defaultGUIFont, sizeof(logFont), &logFont);
317 defaultGUIFontFamily = String(logFont.lfFaceName, wcsnlen(logFont.lfFaceName, LF_FACESIZE));
318 }
319 return getCachedFontData(fontDescription, defaultGUIFontFamily);
320 }
321
toGDIFontWeight(FontWeight fontWeight)322 static LONG toGDIFontWeight(FontWeight fontWeight)
323 {
324 static LONG gdiFontWeights[] = {
325 FW_THIN, // FontWeight100
326 FW_EXTRALIGHT, // FontWeight200
327 FW_LIGHT, // FontWeight300
328 FW_NORMAL, // FontWeight400
329 FW_MEDIUM, // FontWeight500
330 FW_SEMIBOLD, // FontWeight600
331 FW_BOLD, // FontWeight700
332 FW_EXTRABOLD, // FontWeight800
333 FW_HEAVY // FontWeight900
334 };
335 return gdiFontWeights[fontWeight];
336 }
337
isGDIFontWeightBold(LONG gdiFontWeight)338 static inline bool isGDIFontWeightBold(LONG gdiFontWeight)
339 {
340 return gdiFontWeight >= FW_SEMIBOLD;
341 }
342
adjustedGDIFontWeight(LONG gdiFontWeight,const String & family)343 static LONG adjustedGDIFontWeight(LONG gdiFontWeight, const String& family)
344 {
345 static AtomicString lucidaStr("Lucida Grande");
346 if (equalIgnoringCase(family, lucidaStr)) {
347 if (gdiFontWeight == FW_NORMAL)
348 return FW_MEDIUM;
349 if (gdiFontWeight == FW_BOLD)
350 return FW_SEMIBOLD;
351 }
352 return gdiFontWeight;
353 }
354
355 struct MatchImprovingProcData {
MatchImprovingProcDataWebCore::MatchImprovingProcData356 MatchImprovingProcData(LONG desiredWeight, bool desiredItalic)
357 : m_desiredWeight(desiredWeight)
358 , m_desiredItalic(desiredItalic)
359 , m_hasMatched(false)
360 {
361 }
362
363 LONG m_desiredWeight;
364 bool m_desiredItalic;
365 bool m_hasMatched;
366 LOGFONT m_chosen;
367 };
368
matchImprovingEnumProc(CONST LOGFONT * candidate,CONST TEXTMETRIC * metrics,DWORD fontType,LPARAM lParam)369 static int CALLBACK matchImprovingEnumProc(CONST LOGFONT* candidate, CONST TEXTMETRIC* metrics, DWORD fontType, LPARAM lParam)
370 {
371 MatchImprovingProcData* matchData = reinterpret_cast<MatchImprovingProcData*>(lParam);
372
373 if (!matchData->m_hasMatched) {
374 matchData->m_hasMatched = true;
375 matchData->m_chosen = *candidate;
376 return 1;
377 }
378
379 if (!candidate->lfItalic != !matchData->m_chosen.lfItalic) {
380 if (!candidate->lfItalic == !matchData->m_desiredItalic)
381 matchData->m_chosen = *candidate;
382
383 return 1;
384 }
385
386 unsigned chosenWeightDeltaMagnitude = abs(matchData->m_chosen.lfWeight - matchData->m_desiredWeight);
387 unsigned candidateWeightDeltaMagnitude = abs(candidate->lfWeight - matchData->m_desiredWeight);
388
389 // If both are the same distance from the desired weight, prefer the candidate if it is further from regular.
390 if (chosenWeightDeltaMagnitude == candidateWeightDeltaMagnitude && abs(candidate->lfWeight - FW_NORMAL) > abs(matchData->m_chosen.lfWeight - FW_NORMAL)) {
391 matchData->m_chosen = *candidate;
392 return 1;
393 }
394
395 // Otherwise, prefer the one closer to the desired weight.
396 if (candidateWeightDeltaMagnitude < chosenWeightDeltaMagnitude)
397 matchData->m_chosen = *candidate;
398
399 return 1;
400 }
401
createGDIFont(const AtomicString & family,LONG desiredWeight,bool desiredItalic,int size,bool synthesizeItalic)402 static HFONT createGDIFont(const AtomicString& family, LONG desiredWeight, bool desiredItalic, int size, bool synthesizeItalic)
403 {
404 HDC hdc = GetDC(0);
405
406 LOGFONT logFont;
407 logFont.lfCharSet = DEFAULT_CHARSET;
408 unsigned familyLength = min(family.length(), static_cast<unsigned>(LF_FACESIZE - 1));
409 memcpy(logFont.lfFaceName, family.characters(), familyLength * sizeof(UChar));
410 logFont.lfFaceName[familyLength] = 0;
411 logFont.lfPitchAndFamily = 0;
412
413 MatchImprovingProcData matchData(desiredWeight, desiredItalic);
414 EnumFontFamiliesEx(hdc, &logFont, matchImprovingEnumProc, reinterpret_cast<LPARAM>(&matchData), 0);
415
416 ReleaseDC(0, hdc);
417
418 if (!matchData.m_hasMatched)
419 return 0;
420
421 matchData.m_chosen.lfHeight = -size;
422 matchData.m_chosen.lfWidth = 0;
423 matchData.m_chosen.lfEscapement = 0;
424 matchData.m_chosen.lfOrientation = 0;
425 matchData.m_chosen.lfUnderline = false;
426 matchData.m_chosen.lfStrikeOut = false;
427 matchData.m_chosen.lfCharSet = DEFAULT_CHARSET;
428 #if PLATFORM(CG) || PLATFORM(CAIRO)
429 matchData.m_chosen.lfOutPrecision = OUT_TT_ONLY_PRECIS;
430 #else
431 matchData.m_chosen.lfOutPrecision = OUT_TT_PRECIS;
432 #endif
433 matchData.m_chosen.lfQuality = DEFAULT_QUALITY;
434 matchData.m_chosen.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
435
436 if (desiredItalic && !matchData.m_chosen.lfItalic && synthesizeItalic)
437 matchData.m_chosen.lfItalic = 1;
438
439 HFONT result = CreateFontIndirect(&matchData.m_chosen);
440 if (!result)
441 return 0;
442
443 HDC dc = GetDC(0);
444 SaveDC(dc);
445 SelectObject(dc, result);
446 WCHAR actualName[LF_FACESIZE];
447 GetTextFace(dc, LF_FACESIZE, actualName);
448 RestoreDC(dc, -1);
449 ReleaseDC(0, dc);
450
451 if (wcsicmp(matchData.m_chosen.lfFaceName, actualName)) {
452 DeleteObject(result);
453 result = 0;
454 }
455
456 return result;
457 }
458
459 struct TraitsInFamilyProcData {
TraitsInFamilyProcDataWebCore::TraitsInFamilyProcData460 TraitsInFamilyProcData(const AtomicString& familyName)
461 : m_familyName(familyName)
462 {
463 }
464
465 const AtomicString& m_familyName;
466 HashSet<unsigned> m_traitsMasks;
467 };
468
traitsInFamilyEnumProc(CONST LOGFONT * logFont,CONST TEXTMETRIC * metrics,DWORD fontType,LPARAM lParam)469 static int CALLBACK traitsInFamilyEnumProc(CONST LOGFONT* logFont, CONST TEXTMETRIC* metrics, DWORD fontType, LPARAM lParam)
470 {
471 TraitsInFamilyProcData* procData = reinterpret_cast<TraitsInFamilyProcData*>(lParam);
472
473 unsigned traitsMask = 0;
474 traitsMask |= logFont->lfItalic ? FontStyleItalicMask : FontStyleNormalMask;
475 traitsMask |= FontVariantNormalMask;
476 LONG weight = adjustedGDIFontWeight(logFont->lfWeight, procData->m_familyName);
477 traitsMask |= weight == FW_THIN ? FontWeight100Mask :
478 weight == FW_EXTRALIGHT ? FontWeight200Mask :
479 weight == FW_LIGHT ? FontWeight300Mask :
480 weight == FW_NORMAL ? FontWeight400Mask :
481 weight == FW_MEDIUM ? FontWeight500Mask :
482 weight == FW_SEMIBOLD ? FontWeight600Mask :
483 weight == FW_BOLD ? FontWeight700Mask :
484 weight == FW_EXTRABOLD ? FontWeight800Mask :
485 FontWeight900Mask;
486 procData->m_traitsMasks.add(traitsMask);
487 return 1;
488 }
getTraitsInFamily(const AtomicString & familyName,Vector<unsigned> & traitsMasks)489 void FontCache::getTraitsInFamily(const AtomicString& familyName, Vector<unsigned>& traitsMasks)
490 {
491 HDC hdc = GetDC(0);
492
493 LOGFONT logFont;
494 logFont.lfCharSet = DEFAULT_CHARSET;
495 unsigned familyLength = min(familyName.length(), static_cast<unsigned>(LF_FACESIZE - 1));
496 memcpy(logFont.lfFaceName, familyName.characters(), familyLength * sizeof(UChar));
497 logFont.lfFaceName[familyLength] = 0;
498 logFont.lfPitchAndFamily = 0;
499
500 TraitsInFamilyProcData procData(familyName);
501 EnumFontFamiliesEx(hdc, &logFont, traitsInFamilyEnumProc, reinterpret_cast<LPARAM>(&procData), 0);
502 copyToVector(procData.m_traitsMasks, traitsMasks);
503
504 ReleaseDC(0, hdc);
505 }
506
createFontPlatformData(const FontDescription & fontDescription,const AtomicString & family)507 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family)
508 {
509 bool isLucidaGrande = false;
510 static AtomicString lucidaStr("Lucida Grande");
511 if (equalIgnoringCase(family, lucidaStr))
512 isLucidaGrande = true;
513
514 bool useGDI = fontDescription.renderingMode() == AlternateRenderingMode && !isLucidaGrande;
515
516 // The logical size constant is 32. We do this for subpixel precision when rendering using Uniscribe.
517 // This masks rounding errors related to the HFONT metrics being different from the CGFont metrics.
518 // FIXME: We will eventually want subpixel precision for GDI mode, but the scaled rendering doesn't
519 // look as nice. That may be solvable though.
520 #if PLATFORM(CG)
521 bool canCreateCGFontWithLOGFONT = wkCanCreateCGFontWithLOGFONT();
522 #else
523 bool canCreateCGFontWithLOGFONT = true;
524 #endif
525 LONG weight = adjustedGDIFontWeight(toGDIFontWeight(fontDescription.weight()), family);
526 HFONT hfont = createGDIFont(family, weight, fontDescription.italic(),
527 fontDescription.computedPixelSize() * (useGDI ? 1 : 32), useGDI && canCreateCGFontWithLOGFONT);
528
529 if (!hfont)
530 return 0;
531
532 if (isLucidaGrande)
533 useGDI = false; // Never use GDI for Lucida Grande.
534
535 LOGFONT logFont;
536 GetObject(hfont, sizeof(LOGFONT), &logFont);
537
538 bool synthesizeBold = isGDIFontWeightBold(weight) && !isGDIFontWeightBold(logFont.lfWeight);
539 bool synthesizeItalic = fontDescription.italic() && !logFont.lfItalic;
540
541 FontPlatformData* result = new FontPlatformData(hfont, fontDescription.computedPixelSize(), synthesizeBold, synthesizeItalic, useGDI);
542
543 #if PLATFORM(CG)
544 bool fontCreationFailed = !result->cgFont();
545 #elif PLATFORM(CAIRO)
546 bool fontCreationFailed = !result->fontFace();
547 #endif
548
549 if (fontCreationFailed) {
550 // The creation of the CGFontRef failed for some reason. We already asserted in debug builds, but to make
551 // absolutely sure that we don't use this font, go ahead and return 0 so that we can fall back to the next
552 // font.
553 delete result;
554 DeleteObject(hfont);
555 return 0;
556 }
557
558 return result;
559 }
560
561 }
562
563