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"
36 #include <ApplicationServices/ApplicationServices.h>
37 #include <WebKitSystemInterface/WebKitSystemInterface.h>
44 void FontCache::platformInit()
46 wkSetUpFontCache(1536 * 1024 * 4); // This size matches Mac.
49 IMLangFontLink2* FontCache::getFontLinkInterface()
51 static IMultiLanguage *multiLanguage;
53 if (CoCreateInstance(CLSID_CMultiLanguage, 0, CLSCTX_ALL, IID_IMultiLanguage, (void**)&multiLanguage) != S_OK)
57 static IMLangFontLink2* langFontLink;
59 if (multiLanguage->QueryInterface(&langFontLink) != S_OK)
66 static int CALLBACK metaFileEnumProc(HDC hdc, HANDLETABLE* table, CONST ENHMETARECORD* record, int tableEntries, LPARAM hfontPtr)
68 if (record->iType == EMR_EXTCREATEFONTINDIRECTW) {
69 const EMREXTCREATEFONTINDIRECTW* createFontRecord = reinterpret_cast<const EMREXTCREATEFONTINDIRECTW*>(record);
70 *reinterpret_cast<HFONT*>(hfontPtr) = CreateFontIndirect(&createFontRecord->elfw.elfLogFont);
75 const FontData* FontCache::getFontDataForCharacters(const Font& font, const UChar* characters, int length)
77 // IMLangFontLink::MapFont Method does what we want.
78 IMLangFontLink2* langFontLink = getFontLinkInterface();
82 FontData* fontData = 0;
84 HFONT primaryFont = font.primaryFont()->m_font.hfont();
85 HGDIOBJ oldFont = SelectObject(hdc, primaryFont);
89 langFontLink->CodePageToCodePages(CP_ACP, &acpCodePages);
91 DWORD actualCodePages;
93 langFontLink->GetStrCodePages(characters, length, acpCodePages, &actualCodePages, &cchActual);
95 // If simplified Chinese is one of the actual code pages, make one call to MapFont() asking for
96 // simplified Chinese only (and ignore the result). This ensures that we get consistent answers
97 // for characters that are in the simplified Chinese code page as well as other code pages and
98 // characters that are exclusively in the simplified Chinese code page.
99 // FIXME: This needs to be done only once per primary font. We could set a bit in the FontPlatformData
100 // indicating that we have done this.
101 const UINT simplifiedChineseCP = 936;
104 if (actualCodePages && SUCCEEDED(langFontLink->CodePagesToCodePage(actualCodePages, simplifiedChineseCP, &codePage)) && codePage == simplifiedChineseCP) {
105 DWORD simplifiedChineseCodePages;
106 langFontLink->CodePageToCodePages(simplifiedChineseCP, &simplifiedChineseCodePages);
107 langFontLink->MapFont(hdc, simplifiedChineseCodePages, characters[0], &result);
108 langFontLink->ReleaseFont(result);
110 if (langFontLink->MapFont(hdc, actualCodePages, characters[0], &result) == S_OK) {
111 // Fill in a log font with the returned font from MLang, and then use that to create a new font.
113 GetObject(result, sizeof(LOGFONT), &lf);
114 langFontLink->ReleaseFont(result);
115 hfont = CreateFontIndirect(&lf);
120 // Font linking failed but Uniscribe might still be able to find a fallback font.
121 // To find out what Uniscribe would do, we make it draw into a metafile and intercept
122 // calls to CreateFontIndirect().
123 HDC metaFileDc = CreateEnhMetaFile(hdc, NULL, NULL, NULL);
124 SelectObject(metaFileDc, hfont ? hfont : primaryFont);
126 bool scriptStringOutSucceeded = false;
127 SCRIPT_STRING_ANALYSIS ssa;
129 // FIXME: If length is greater than 1, we actually return the font for the last character.
130 // This function should be renamed getFontDataForCharacter and take a single 32-bit character.
131 if (SUCCEEDED(ScriptStringAnalyse(metaFileDc, characters, length, 0, -1, SSA_METAFILE | SSA_FALLBACK | SSA_GLYPHS | SSA_LINK,
132 0, NULL, NULL, NULL, NULL, NULL, &ssa))) {
133 scriptStringOutSucceeded = SUCCEEDED(ScriptStringOut(ssa, 0, 0, 0, NULL, 0, 0, FALSE));
134 ScriptStringFree(&ssa);
136 HENHMETAFILE metaFile = CloseEnhMetaFile(metaFileDc);
137 if (scriptStringOutSucceeded)
138 EnumEnhMetaFile(0, metaFile, metaFileEnumProc, &hfont, NULL);
139 DeleteEnhMetaFile(metaFile);
143 SelectObject(hdc, hfont);
144 WCHAR name[LF_FACESIZE];
145 GetTextFace(hdc, LF_FACESIZE, name);
147 String familyName(name);
148 if (!familyName.isEmpty()) {
149 FontPlatformData* result = getCachedFontPlatformData(font.fontDescription(), familyName);
151 fontData = getCachedFontData(result);
154 SelectObject(hdc, oldFont);
162 FontPlatformData* FontCache::getSimilarFontPlatformData(const Font& font)
167 FontPlatformData* FontCache::getLastResortFallbackFont(const FontDescription& fontDescription)
169 // FIXME: Would be even better to somehow get the user's default font here. For now we'll pick
170 // the default that the user would get without changing any prefs.
171 static AtomicString timesStr("Times New Roman");
172 return getCachedFontPlatformData(fontDescription, timesStr);
175 bool FontCache::fontExists(const FontDescription& fontDescription, const AtomicString& family)
179 // The size here looks unusual. The negative number is intentional. The logical size constant is 32.
180 winfont.lfHeight = -fontDescription.computedPixelSize() * 32;
182 winfont.lfEscapement = 0;
183 winfont.lfOrientation = 0;
184 winfont.lfUnderline = false;
185 winfont.lfStrikeOut = false;
186 winfont.lfCharSet = DEFAULT_CHARSET;
188 winfont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
190 winfont.lfOutPrecision = OUT_TT_PRECIS;
192 winfont.lfQuality = 5; // Force cleartype.
193 winfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
194 winfont.lfItalic = fontDescription.italic();
196 // FIXME: Support weights for real. Do our own enumeration of the available weights.
197 // We can't rely on Windows here, since we need to follow the CSS2 algorithm for how to fill in
198 // gaps in the weight list.
199 // FIXME: Hardcoding Lucida Grande for now. It uses different weights than typical Win32 fonts
200 // (500/600 instead of 400/700).
201 static AtomicString lucidaStr("Lucida Grande");
202 if (equalIgnoringCase(family, lucidaStr))
203 winfont.lfWeight = fontDescription.bold() ? 600 : 500;
205 winfont.lfWeight = fontDescription.bold() ? 700 : 400;
206 int len = min(family.length(), (unsigned int)LF_FACESIZE - 1);
207 memcpy(winfont.lfFaceName, family.characters(), len * sizeof(WORD));
208 winfont.lfFaceName[len] = '\0';
210 HFONT hfont = CreateFontIndirect(&winfont);
211 // Windows will always give us a valid pointer here, even if the face name is non-existent. We have to double-check
212 // and see if the family name was really used.
213 HDC dc = GetDC((HWND)0);
215 SelectObject(dc, hfont);
216 WCHAR name[LF_FACESIZE];
217 GetTextFace(dc, LF_FACESIZE, name);
223 return !wcsicmp(winfont.lfFaceName, name);
226 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family)
230 // The size here looks unusual. The negative number is intentional. The logical size constant is 32.
231 winfont.lfHeight = -fontDescription.computedPixelSize() * 32;
233 winfont.lfEscapement = 0;
234 winfont.lfOrientation = 0;
235 winfont.lfUnderline = false;
236 winfont.lfStrikeOut = false;
237 winfont.lfCharSet = DEFAULT_CHARSET;
239 winfont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
241 winfont.lfOutPrecision = OUT_TT_PRECIS;
243 winfont.lfQuality = 5; // Force cleartype.
244 winfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
245 winfont.lfItalic = fontDescription.italic();
247 // FIXME: Support weights for real. Do our own enumeration of the available weights.
248 // We can't rely on Windows here, since we need to follow the CSS2 algorithm for how to fill in
249 // gaps in the weight list.
250 // FIXME: Hardcoding Lucida Grande for now. It uses different weights than typical Win32 fonts
251 // (500/600 instead of 400/700).
252 static AtomicString lucidaStr("Lucida Grande");
253 if (equalIgnoringCase(family, lucidaStr))
254 winfont.lfWeight = fontDescription.bold() ? 600 : 500;
256 winfont.lfWeight = fontDescription.bold() ? 700 : 400;
257 int len = min(family.length(), (unsigned int)LF_FACESIZE - 1);
258 memcpy(winfont.lfFaceName, family.characters(), len * sizeof(WORD));
259 winfont.lfFaceName[len] = '\0';
261 HFONT hfont = CreateFontIndirect(&winfont);
262 // Windows will always give us a valid pointer here, even if the face name is non-existent. We have to double-check
263 // and see if the family name was really used.
264 HDC dc = GetDC((HWND)0);
266 SelectObject(dc, hfont);
267 WCHAR name[LF_FACESIZE];
268 GetTextFace(dc, LF_FACESIZE, name);
272 if (_wcsicmp(winfont.lfFaceName, name)) {
277 FontPlatformData* result = new FontPlatformData(hfont, fontDescription.computedPixelSize(),
278 fontDescription.bold(), fontDescription.italic());
279 if (!result->cgFont()) {
280 // The creation of the CGFontRef failed for some reason. We already asserted in debug builds, but to make
281 // absolutely sure that we don't use this font, go ahead and return 0 so that we can fall back to the next