2 * Copyright (C) 2006 Apple Computer, 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.
30 #include "FontCache.h"
42 void FontCache::platformInit()
44 // Not needed on Windows.
47 IMLangFontLink2* FontCache::getFontLinkInterface()
49 static IMultiLanguage *multiLanguage;
51 if (CoCreateInstance(CLSID_CMultiLanguage, 0, CLSCTX_ALL, IID_IMultiLanguage, (void**)&multiLanguage) != S_OK)
55 static IMLangFontLink2* langFontLink;
57 if (multiLanguage->QueryInterface(IID_IMLangFontLink2, (void**)&langFontLink) != S_OK)
64 const FontData* FontCache::getFontDataForCharacters(const Font& font, const UChar* characters, int length)
66 // IMLangFontLink::MapFont Method does what we want.
67 IMLangFontLink2* langFontLink = getFontLinkInterface();
71 FontData* fontData = 0;
74 langFontLink->GetFontCodePages(hdc, font.primaryFont()->m_font.hfont(), &fontCodePages);
76 DWORD actualCodePages;
78 langFontLink->GetStrCodePages(characters, length, fontCodePages, &actualCodePages, &cchActual);
81 if (langFontLink->MapFont(hdc, actualCodePages, characters[0], &result) == S_OK) {
82 fontData = new FontData(FontPlatformData(result, font.fontDescription().computedPixelSize()));
83 fontData->setIsMLangFont();
91 FontPlatformData* FontCache::getSimilarFontPlatformData(const Font& font)
96 FontPlatformData* FontCache::getLastResortFallbackFont(const Font& font)
98 // FIXME: Would be even better to somehow get the user's default font here. For now we'll pick
99 // the default that the user would get without changing any prefs.
100 static AtomicString timesStr("Times New Roman");
101 return getCachedFontPlatformData(font.fontDescription(), timesStr);
104 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family)
108 // The size here looks unusual. The negative number is intentional. The logical size constant is 32.
109 winfont.lfHeight = -fontDescription.computedPixelSize();
111 winfont.lfEscapement = 0;
112 winfont.lfOrientation = 0;
113 winfont.lfUnderline = false;
114 winfont.lfStrikeOut = false;
115 winfont.lfCharSet = DEFAULT_CHARSET;
116 winfont.lfOutPrecision = OUT_TT_PRECIS;
117 winfont.lfQuality = 5; // Force cleartype.
118 winfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
119 winfont.lfItalic = fontDescription.italic();
120 winfont.lfWeight = fontDescription.bold() ? 700 : 400; // FIXME: Support weights for real.
121 int len = min(static_cast<int>(family.length()), LF_FACESIZE - 1);
122 memcpy(winfont.lfFaceName, family.characters(), len * sizeof(WORD));
123 winfont.lfFaceName[len] = '\0';
125 HFONT hfont = CreateFontIndirect(&winfont);
126 // Windows will always give us a valid pointer here, even if the face name is non-existent. We have to double-check
127 // and see if the family name was really used.
128 HDC dc = GetDC((HWND)0);
130 SelectObject(dc, hfont);
131 WCHAR name[LF_FACESIZE];
132 unsigned resultLength = GetTextFace(dc, LF_FACESIZE, name);
133 if (resultLength > 0)
134 resultLength--; // ignore the null terminator
137 if (!equalIgnoringCase(family, String(name, resultLength))) {
142 return new FontPlatformData(hfont, fontDescription.computedPixelSize());