2 * Copyright (C) 2006, 2007 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"
34 #include "StringHash.h"
37 #include <ApplicationServices/ApplicationServices.h>
38 #include <WebKitSystemInterface/WebKitSystemInterface.h>
45 void FontCache::platformInit()
47 wkSetUpFontCache(1536 * 1024 * 4); // This size matches Mac.
50 IMLangFontLink2* FontCache::getFontLinkInterface()
52 static IMultiLanguage *multiLanguage;
54 if (CoCreateInstance(CLSID_CMultiLanguage, 0, CLSCTX_ALL, IID_IMultiLanguage, (void**)&multiLanguage) != S_OK)
58 static IMLangFontLink2* langFontLink;
60 if (multiLanguage->QueryInterface(&langFontLink) != S_OK)
67 static int CALLBACK metaFileEnumProc(HDC hdc, HANDLETABLE* table, CONST ENHMETARECORD* record, int tableEntries, LPARAM logFont)
69 if (record->iType == EMR_EXTCREATEFONTINDIRECTW) {
70 const EMREXTCREATEFONTINDIRECTW* createFontRecord = reinterpret_cast<const EMREXTCREATEFONTINDIRECTW*>(record);
71 *reinterpret_cast<LOGFONT*>(logFont) = createFontRecord->elfw.elfLogFont;
76 static int CALLBACK linkedFontEnumProc(CONST LOGFONT* logFont, CONST TEXTMETRIC* metrics, DWORD fontType, LPARAM hfont)
78 *reinterpret_cast<HFONT*>(hfont) = CreateFontIndirect(logFont);
82 static const Vector<String>* getLinkedFonts(String& family)
84 static HashMap<String, Vector<String>*> systemLinkMap;
85 Vector<String>* result = systemLinkMap.get(family);
89 result = new Vector<String>;
90 systemLinkMap.set(family, result);
92 if (FAILED(RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\FontLink\\SystemLink", 0, KEY_READ, &fontLinkKey)))
95 DWORD linkedFontsBufferSize = 0;
96 RegQueryValueEx(fontLinkKey, family.charactersWithNullTermination(), 0, NULL, NULL, &linkedFontsBufferSize);
97 WCHAR* linkedFonts = reinterpret_cast<WCHAR*>(malloc(linkedFontsBufferSize));
98 if (SUCCEEDED(RegQueryValueEx(fontLinkKey, family.charactersWithNullTermination(), 0, NULL, reinterpret_cast<BYTE*>(linkedFonts), &linkedFontsBufferSize))) {
100 unsigned length = linkedFontsBufferSize / sizeof(*linkedFonts);
102 while (i < linkedFontsBufferSize && linkedFonts[i] != ',')
106 while (j < linkedFontsBufferSize && linkedFonts[j])
108 result->append(String(linkedFonts + i, j - i));
113 RegCloseKey(fontLinkKey);
117 const FontData* FontCache::getFontDataForCharacters(const Font& font, const UChar* characters, int length)
119 FontData* fontData = 0;
121 HFONT primaryFont = font.primaryFont()->m_font.hfont();
122 HGDIOBJ oldFont = SelectObject(hdc, primaryFont);
125 // Special-case characters in the range U+2000..U+200F (spaces, zero width joiner and non-joiner,
126 // right to left and left to right marks). Uniscribe does not tell us what font it will use for these
127 // (presumably because it never renders their glyphs), so we use MapFont to find a font that contains
129 if (characters[0] >= 0x2000 && characters[0] <= 0x200F) {
130 IMLangFontLink2* langFontLink = getFontLinkInterface();
133 langFontLink->MapFont(hdc, 0, characters[0], &mLangFont);
135 GetObject(mLangFont, sizeof(LOGFONT), &lf);
136 langFontLink->ReleaseFont(mLangFont);
137 hfont = CreateFontIndirect(&lf);
140 // To find out what font Uniscribe would use, we make it draw into a metafile and intercept
141 // calls to CreateFontIndirect().
142 HDC metaFileDc = CreateEnhMetaFile(hdc, NULL, NULL, NULL);
143 SelectObject(metaFileDc, primaryFont);
145 bool scriptStringOutSucceeded = false;
146 SCRIPT_STRING_ANALYSIS ssa;
148 // FIXME: If length is greater than 1, we actually return the font for the last character.
149 // This function should be renamed getFontDataForCharacter and take a single 32-bit character.
150 if (SUCCEEDED(ScriptStringAnalyse(metaFileDc, characters, length, 0, -1, SSA_METAFILE | SSA_FALLBACK | SSA_GLYPHS | SSA_LINK,
151 0, NULL, NULL, NULL, NULL, NULL, &ssa))) {
152 scriptStringOutSucceeded = SUCCEEDED(ScriptStringOut(ssa, 0, 0, 0, NULL, 0, 0, FALSE));
153 ScriptStringFree(&ssa);
155 HENHMETAFILE metaFile = CloseEnhMetaFile(metaFileDc);
156 if (scriptStringOutSucceeded) {
158 logFont.lfFaceName[0] = 0;
159 EnumEnhMetaFile(0, metaFile, metaFileEnumProc, &logFont, NULL);
160 if (logFont.lfFaceName[0])
161 hfont = CreateFontIndirect(&logFont);
163 DeleteEnhMetaFile(metaFile);
167 const Vector<String>* linkedFonts = 0;
168 unsigned linkedFontIndex = 0;
170 SelectObject(hdc, hfont);
171 WCHAR name[LF_FACESIZE];
172 GetTextFace(hdc, LF_FACESIZE, name);
174 // Check if the font contains the desired character.
175 static Vector<char, 512> glyphsetBuffer;
176 glyphsetBuffer.resize(GetFontUnicodeRanges(hdc, NULL));
177 GLYPHSET* glyphset = reinterpret_cast<GLYPHSET*>(glyphsetBuffer.data());
178 GetFontUnicodeRanges(hdc, glyphset);
179 // FIXME: Change this to a binary search.
181 while (i < glyphset->cRanges && glyphset->ranges[i].wcLow <= characters[0])
183 if (i && glyphset->ranges[i - 1].wcLow + glyphset->ranges[i - 1].cGlyphs > characters[0])
187 linkedFonts = getLinkedFonts(familyName);
188 SelectObject(hdc, oldFont);
192 if (linkedFonts->size() <= linkedFontIndex)
196 logFont.lfCharSet = DEFAULT_CHARSET;
197 memcpy(logFont.lfFaceName, linkedFonts->at(linkedFontIndex).characters(), linkedFonts->at(linkedFontIndex).length() * sizeof(WCHAR));
198 logFont.lfFaceName[linkedFonts->at(linkedFontIndex).length()] = 0;
199 EnumFontFamiliesEx(hdc, &logFont, linkedFontEnumProc, reinterpret_cast<LPARAM>(&hfont), 0);
204 if (!familyName.isEmpty()) {
205 FontPlatformData* result = getCachedFontPlatformData(font.fontDescription(), familyName);
207 fontData = getCachedFontData(result);
210 SelectObject(hdc, oldFont);
218 FontPlatformData* FontCache::getSimilarFontPlatformData(const Font& font)
223 FontPlatformData* FontCache::getLastResortFallbackFont(const FontDescription& fontDescription)
225 // FIXME: Would be even better to somehow get the user's default font here. For now we'll pick
226 // the default that the user would get without changing any prefs.
227 static AtomicString timesStr("Times New Roman");
228 return getCachedFontPlatformData(fontDescription, timesStr);
231 bool FontCache::fontExists(const FontDescription& fontDescription, const AtomicString& family)
235 // The size here looks unusual. The negative number is intentional. The logical size constant is 32.
236 winfont.lfHeight = -fontDescription.computedPixelSize() * 32;
238 winfont.lfEscapement = 0;
239 winfont.lfOrientation = 0;
240 winfont.lfUnderline = false;
241 winfont.lfStrikeOut = false;
242 winfont.lfCharSet = DEFAULT_CHARSET;
244 winfont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
246 winfont.lfOutPrecision = OUT_TT_PRECIS;
248 winfont.lfQuality = 5; // Force cleartype.
249 winfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
250 winfont.lfItalic = fontDescription.italic();
252 // FIXME: Support weights for real. Do our own enumeration of the available weights.
253 // We can't rely on Windows here, since we need to follow the CSS2 algorithm for how to fill in
254 // gaps in the weight list.
255 // FIXME: Hardcoding Lucida Grande for now. It uses different weights than typical Win32 fonts
256 // (500/600 instead of 400/700).
257 static AtomicString lucidaStr("Lucida Grande");
258 if (equalIgnoringCase(family, lucidaStr))
259 winfont.lfWeight = fontDescription.bold() ? 600 : 500;
261 winfont.lfWeight = fontDescription.bold() ? 700 : 400;
262 int len = min(family.length(), (unsigned int)LF_FACESIZE - 1);
263 memcpy(winfont.lfFaceName, family.characters(), len * sizeof(WORD));
264 winfont.lfFaceName[len] = '\0';
266 HFONT hfont = CreateFontIndirect(&winfont);
267 // Windows will always give us a valid pointer here, even if the face name is non-existent. We have to double-check
268 // and see if the family name was really used.
271 SelectObject(dc, hfont);
272 WCHAR name[LF_FACESIZE];
273 GetTextFace(dc, LF_FACESIZE, name);
279 return !wcsicmp(winfont.lfFaceName, name);
282 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family)
284 bool isLucidaGrande = false;
285 static AtomicString lucidaStr("Lucida Grande");
286 if (equalIgnoringCase(family, lucidaStr))
287 isLucidaGrande = true;
289 bool useGDI = fontDescription.renderingMode() == AlternateRenderingMode && !isLucidaGrande;
293 // The size here looks unusual. The negative number is intentional. The logical size constant is 32. We do this
294 // for subpixel precision when rendering using Uniscribe. This masks rounding errors related to the HFONT metrics being
295 // different from the CGFont metrics.
296 // FIXME: We will eventually want subpixel precision for GDI mode, but the scaled rendering doesn't look as nice. That may be solvable though.
297 winfont.lfHeight = -fontDescription.computedPixelSize() * (useGDI ? 1 : 32);
299 winfont.lfEscapement = 0;
300 winfont.lfOrientation = 0;
301 winfont.lfUnderline = false;
302 winfont.lfStrikeOut = false;
303 winfont.lfCharSet = DEFAULT_CHARSET;
304 winfont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
305 winfont.lfQuality = DEFAULT_QUALITY;
306 winfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
307 winfont.lfItalic = fontDescription.italic();
309 // FIXME: Support weights for real. Do our own enumeration of the available weights.
310 // We can't rely on Windows here, since we need to follow the CSS2 algorithm for how to fill in
311 // gaps in the weight list.
312 // FIXME: Hardcoding Lucida Grande for now. It uses different weights than typical Win32 fonts
313 // (500/600 instead of 400/700).
314 if (isLucidaGrande) {
315 winfont.lfWeight = fontDescription.bold() ? 600 : 500;
316 useGDI = false; // Never use GDI for Lucida Grande.
318 winfont.lfWeight = fontDescription.bold() ? 700 : 400;
319 int len = min(family.length(), (unsigned int)LF_FACESIZE - 1);
320 memcpy(winfont.lfFaceName, family.characters(), len * sizeof(WORD));
321 winfont.lfFaceName[len] = '\0';
323 HFONT hfont = CreateFontIndirect(&winfont);
324 // Windows will always give us a valid pointer here, even if the face name is non-existent. We have to double-check
325 // and see if the family name was really used.
328 SelectObject(dc, hfont);
329 WCHAR name[LF_FACESIZE];
330 GetTextFace(dc, LF_FACESIZE, name);
334 if (_wcsicmp(winfont.lfFaceName, name)) {
339 FontPlatformData* result = new FontPlatformData(hfont, fontDescription.computedPixelSize(),
340 fontDescription.bold(), fontDescription.italic(), useGDI);
341 if (!result->cgFont()) {
342 // The creation of the CGFontRef failed for some reason. We already asserted in debug builds, but to make
343 // absolutely sure that we don't use this font, go ahead and return 0 so that we can fall back to the next