2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
31 #include "FontCache.h"
33 #include "SimpleFontData.h"
34 #include "UnicodeRange.h"
37 #include <wtf/StdLibExtras.h>
38 #include <wtf/text/StringHash.h>
40 #include <ApplicationServices/ApplicationServices.h>
41 #include <WebKitSystemInterface/WebKitSystemInterface.h>
49 void FontCache::platformInit()
52 wkSetUpFontCache(1536 * 1024 * 4); // This size matches Mac.
56 IMLangFontLink2* FontCache::getFontLinkInterface()
58 static IMultiLanguage *multiLanguage;
60 if (CoCreateInstance(CLSID_CMultiLanguage, 0, CLSCTX_ALL, IID_IMultiLanguage, (void**)&multiLanguage) != S_OK)
64 static IMLangFontLink2* langFontLink;
66 if (multiLanguage->QueryInterface(&langFontLink) != S_OK)
73 static int CALLBACK metaFileEnumProc(HDC hdc, HANDLETABLE* table, CONST ENHMETARECORD* record, int tableEntries, LPARAM logFont)
75 if (record->iType == EMR_EXTCREATEFONTINDIRECTW) {
76 const EMREXTCREATEFONTINDIRECTW* createFontRecord = reinterpret_cast<const EMREXTCREATEFONTINDIRECTW*>(record);
77 *reinterpret_cast<LOGFONT*>(logFont) = createFontRecord->elfw.elfLogFont;
82 static int CALLBACK linkedFontEnumProc(CONST LOGFONT* logFont, CONST TEXTMETRIC* metrics, DWORD fontType, LPARAM hfont)
84 *reinterpret_cast<HFONT*>(hfont) = CreateFontIndirect(logFont);
88 static const Vector<String>* getLinkedFonts(String& family)
90 static HashMap<String, Vector<String>*> systemLinkMap;
91 Vector<String>* result = systemLinkMap.get(family);
95 result = new Vector<String>;
96 systemLinkMap.set(family, result);
98 if (FAILED(RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\FontLink\\SystemLink", 0, KEY_READ, &fontLinkKey)))
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))) {
106 unsigned length = linkedFontsBufferSize / sizeof(*linkedFonts);
108 while (i < length && linkedFonts[i] != ',')
112 while (j < length && linkedFonts[j])
114 result->append(String(linkedFonts + i, j - i));
119 RegCloseKey(fontLinkKey);
123 static const Vector<DWORD, 4>& getCJKCodePageMasks()
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[] = {
129 936, /* Simplified Chinese */
130 950, /* Traditional Chinese */
134 static Vector<DWORD, 4> codePageMasks;
135 static bool initialized;
138 IMLangFontLink2* langFontLink = fontCache()->getFontLinkInterface();
140 return codePageMasks;
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);
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]) {
152 langFontLink->CodePageToCodePages(CJKCodePages[i], &codePageMask);
153 codePageMasks.append(codePageMask);
157 return codePageMasks;
160 static bool currentFontContainsCharacter(HDC hdc, UChar character)
162 static Vector<char, 512> glyphsetBuffer;
163 glyphsetBuffer.resize(GetFontUnicodeRanges(hdc, 0));
164 GLYPHSET* glyphset = reinterpret_cast<GLYPHSET*>(glyphsetBuffer.data());
165 GetFontUnicodeRanges(hdc, glyphset);
167 // FIXME: Change this to a binary search.
169 while (i < glyphset->cRanges && glyphset->ranges[i].wcLow <= character)
172 return i && glyphset->ranges[i - 1].wcLow + glyphset->ranges[i - 1].cGlyphs > character;
175 static HFONT createMLangFont(IMLangFontLink2* langFontLink, HDC hdc, DWORD codePageMask, UChar character = 0)
179 if (SUCCEEDED(langFontLink->MapFont(hdc, codePageMask, character, &MLangFont)) && MLangFont) {
181 GetObject(MLangFont, sizeof(LOGFONT), &lf);
182 langFontLink->ReleaseFont(MLangFont);
183 hfont = CreateFontIndirect(&lf);
188 const SimpleFontData* FontCache::getFontDataForCharacters(const Font& font, const UChar* characters, int length)
190 UChar character = characters[0];
191 SimpleFontData* fontData = 0;
193 HFONT primaryFont = font.primaryFont()->fontDataForCharacter(character)->platformData().hfont();
194 HGDIOBJ oldFont = SelectObject(hdc, primaryFont);
197 if (IMLangFontLink2* langFontLink = getFontLinkInterface()) {
198 // Try MLang font linking first.
200 langFontLink->GetCharCodePages(character, &codePages);
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)) {
218 SelectObject(hdc, primaryFont);
222 hfont = createMLangFont(langFontLink, hdc, codePages, character);
225 // A font returned from MLang is trusted to contain the character.
226 bool containsCharacter = 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);
234 bool scriptStringOutSucceeded = false;
235 SCRIPT_STRING_ANALYSIS ssa;
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);
244 HENHMETAFILE metaFile = CloseEnhMetaFile(metaFileDc);
245 if (scriptStringOutSucceeded) {
247 logFont.lfFaceName[0] = 0;
248 EnumEnhMetaFile(0, metaFile, metaFileEnumProc, &logFont, NULL);
249 if (logFont.lfFaceName[0])
250 hfont = CreateFontIndirect(&logFont);
252 DeleteEnhMetaFile(metaFile);
256 const Vector<String>* linkedFonts = 0;
257 unsigned linkedFontIndex = 0;
259 SelectObject(hdc, hfont);
260 WCHAR name[LF_FACESIZE];
261 GetTextFace(hdc, LF_FACESIZE, name);
264 if (containsCharacter || currentFontContainsCharacter(hdc, character))
268 linkedFonts = getLinkedFonts(familyName);
269 SelectObject(hdc, oldFont);
273 if (linkedFonts->size() <= linkedFontIndex)
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);
285 if (!familyName.isEmpty()) {
286 FontPlatformData* result = getCachedFontPlatformData(font.fontDescription(), familyName);
288 fontData = getCachedFontData(result);
291 SelectObject(hdc, oldFont);
299 SimpleFontData* FontCache::getSimilarFontPlatformData(const Font& font)
304 static SimpleFontData* fontDataFromDescriptionAndLogFont(FontCache* fontCache, const FontDescription& fontDescription, const LOGFONT& font, AtomicString& outFontFamilyName)
306 AtomicString familyName = String(font.lfFaceName, wcsnlen(font.lfFaceName, LF_FACESIZE));
307 SimpleFontData* fontData = fontCache->getCachedFontData(fontDescription, familyName);
309 outFontFamilyName = familyName;
313 SimpleFontData* FontCache::getLastResortFallbackFont(const FontDescription& fontDescription)
315 DEFINE_STATIC_LOCAL(AtomicString, fallbackFontName, ());
316 if (!fallbackFontName.isEmpty())
317 return getCachedFontData(fontDescription, fallbackFontName);
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.
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")
332 SimpleFontData* simpleFont;
333 for (int i = 0; i < ARRAYSIZE(fallbackFonts); ++i) {
334 if (simpleFont = getCachedFontData(fontDescription, fallbackFonts[i])) {
335 fallbackFontName = fallbackFonts[i];
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))
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))
354 if (simpleFont = fontDataFromDescriptionAndLogFont(this, fontDescription, nonClientMetrics.lfMenuFont, fallbackFontName))
356 if (simpleFont = fontDataFromDescriptionAndLogFont(this, fontDescription, nonClientMetrics.lfStatusFont, fallbackFontName))
358 if (simpleFont = fontDataFromDescriptionAndLogFont(this, fontDescription, nonClientMetrics.lfCaptionFont, fallbackFontName))
360 if (simpleFont = fontDataFromDescriptionAndLogFont(this, fontDescription, nonClientMetrics.lfSmCaptionFont, fallbackFontName))
364 ASSERT_NOT_REACHED();
368 static LONG toGDIFontWeight(FontWeight fontWeight)
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
381 return gdiFontWeights[fontWeight];
384 static inline bool isGDIFontWeightBold(LONG gdiFontWeight)
386 return gdiFontWeight >= FW_SEMIBOLD;
389 static LONG adjustedGDIFontWeight(LONG gdiFontWeight, const String& family)
391 static AtomicString lucidaStr("Lucida Grande");
392 if (equalIgnoringCase(family, lucidaStr)) {
393 if (gdiFontWeight == FW_NORMAL)
395 if (gdiFontWeight == FW_BOLD)
398 return gdiFontWeight;
401 struct MatchImprovingProcData {
402 MatchImprovingProcData(LONG desiredWeight, bool desiredItalic)
403 : m_desiredWeight(desiredWeight)
404 , m_desiredItalic(desiredItalic)
405 , m_hasMatched(false)
409 LONG m_desiredWeight;
410 bool m_desiredItalic;
415 static int CALLBACK matchImprovingEnumProc(CONST LOGFONT* candidate, CONST TEXTMETRIC* metrics, DWORD fontType, LPARAM lParam)
417 MatchImprovingProcData* matchData = reinterpret_cast<MatchImprovingProcData*>(lParam);
419 if (!matchData->m_hasMatched) {
420 matchData->m_hasMatched = true;
421 matchData->m_chosen = *candidate;
425 if (!candidate->lfItalic != !matchData->m_chosen.lfItalic) {
426 if (!candidate->lfItalic == !matchData->m_desiredItalic)
427 matchData->m_chosen = *candidate;
432 unsigned chosenWeightDeltaMagnitude = abs(matchData->m_chosen.lfWeight - matchData->m_desiredWeight);
433 unsigned candidateWeightDeltaMagnitude = abs(candidate->lfWeight - matchData->m_desiredWeight);
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;
441 // Otherwise, prefer the one closer to the desired weight.
442 if (candidateWeightDeltaMagnitude < chosenWeightDeltaMagnitude)
443 matchData->m_chosen = *candidate;
448 static HFONT createGDIFont(const AtomicString& family, LONG desiredWeight, bool desiredItalic, int size, bool synthesizeItalic)
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;
459 MatchImprovingProcData matchData(desiredWeight, desiredItalic);
460 EnumFontFamiliesEx(hdc, &logFont, matchImprovingEnumProc, reinterpret_cast<LPARAM>(&matchData), 0);
464 if (!matchData.m_hasMatched)
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 PLATFORM(CG) || PLATFORM(CAIRO)
475 matchData.m_chosen.lfOutPrecision = OUT_TT_ONLY_PRECIS;
477 matchData.m_chosen.lfOutPrecision = OUT_TT_PRECIS;
479 matchData.m_chosen.lfQuality = DEFAULT_QUALITY;
480 matchData.m_chosen.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
482 if (desiredItalic && !matchData.m_chosen.lfItalic && synthesizeItalic)
483 matchData.m_chosen.lfItalic = 1;
485 HFONT result = CreateFontIndirect(&matchData.m_chosen);
491 SelectObject(dc, result);
492 WCHAR actualName[LF_FACESIZE];
493 GetTextFace(dc, LF_FACESIZE, actualName);
497 if (wcsicmp(matchData.m_chosen.lfFaceName, actualName)) {
498 DeleteObject(result);
505 struct TraitsInFamilyProcData {
506 TraitsInFamilyProcData(const AtomicString& familyName)
507 : m_familyName(familyName)
511 const AtomicString& m_familyName;
512 HashSet<unsigned> m_traitsMasks;
515 static int CALLBACK traitsInFamilyEnumProc(CONST LOGFONT* logFont, CONST TEXTMETRIC* metrics, DWORD fontType, LPARAM lParam)
517 TraitsInFamilyProcData* procData = reinterpret_cast<TraitsInFamilyProcData*>(lParam);
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 :
532 procData->m_traitsMasks.add(traitsMask);
535 void FontCache::getTraitsInFamily(const AtomicString& familyName, Vector<unsigned>& traitsMasks)
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;
546 TraitsInFamilyProcData procData(familyName);
547 EnumFontFamiliesEx(hdc, &logFont, traitsInFamilyEnumProc, reinterpret_cast<LPARAM>(&procData), 0);
548 copyToVector(procData.m_traitsMasks, traitsMasks);
553 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family)
555 bool isLucidaGrande = false;
556 static AtomicString lucidaStr("Lucida Grande");
557 if (equalIgnoringCase(family, lucidaStr))
558 isLucidaGrande = true;
560 bool useGDI = fontDescription.renderingMode() == AlternateRenderingMode && !isLucidaGrande;
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);
574 useGDI = false; // Never use GDI for Lucida Grande.
577 GetObject(hfont, sizeof(LOGFONT), &logFont);
579 bool synthesizeBold = isGDIFontWeightBold(weight) && !isGDIFontWeightBold(logFont.lfWeight);
580 bool synthesizeItalic = fontDescription.italic() && !logFont.lfItalic;
582 FontPlatformData* result = new FontPlatformData(hfont, fontDescription.computedPixelSize(), synthesizeBold, synthesizeItalic, useGDI);
585 bool fontCreationFailed = !result->cgFont();
586 #elif PLATFORM(CAIRO)
587 bool fontCreationFailed = !result->fontFace();
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