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"
39 void FontCache::platformInit()
41 // Not needed on Windows.
44 IMLangFontLink2* FontCache::getFontLinkInterface()
46 static IMultiLanguage *multiLanguage;
48 if (CoCreateInstance(CLSID_CMultiLanguage, 0, CLSCTX_ALL, IID_IMultiLanguage, (void**)&multiLanguage) != S_OK)
52 static IMLangFontLink2* langFontLink;
54 if (multiLanguage->QueryInterface(IID_IMLangFontLink2, (void**)&langFontLink) != S_OK)
61 const FontData* FontCache::getFontDataForCharacters(const Font& font, const UChar* characters, int length)
63 // IMLangFontLink::MapFont Method does what we want.
64 IMLangFontLink2* langFontLink = getFontLinkInterface();
68 FontData* fontData = 0;
71 langFontLink->GetFontCodePages(hdc, font.primaryFont()->m_font.hfont(), &fontCodePages);
73 DWORD actualCodePages;
75 langFontLink->GetStrCodePages(characters, length, fontCodePages, &actualCodePages, &cchActual);
78 if (langFontLink->MapFont(hdc, actualCodePages, characters[0], &result) == S_OK) {
79 fontData = new FontData(FontPlatformData(result, font.fontDescription().computedPixelSize()));
80 fontData->setIsMLangFont();
88 FontPlatformData* FontCache::getSimilarFontPlatformData(const Font& font)
93 FontPlatformData* FontCache::getLastResortFallbackFont(const Font& font)
95 // FIXME: Would be even better to somehow get the user's default font here. For now we'll pick
96 // the default that the user would get without changing any prefs.
97 static AtomicString timesStr("Times New Roman");
98 return getCachedFontPlatformData(font.fontDescription(), timesStr);
101 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family)
105 // The size here looks unusual. The negative number is intentional. The logical size constant is 32.
106 winfont.lfHeight = -fontDescription.computedPixelSize();
108 winfont.lfEscapement = 0;
109 winfont.lfOrientation = 0;
110 winfont.lfUnderline = false;
111 winfont.lfStrikeOut = false;
112 winfont.lfCharSet = DEFAULT_CHARSET;
113 winfont.lfOutPrecision = OUT_TT_PRECIS;
114 winfont.lfQuality = 5; // Force cleartype.
115 winfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
116 winfont.lfItalic = fontDescription.italic();
117 winfont.lfWeight = fontDescription.bold() ? 700 : 400; // FIXME: Support weights for real.
118 int len = min(family.length(), LF_FACESIZE - 1);
119 memcpy(winfont.lfFaceName, family.characters(), len * sizeof(WORD));
120 winfont.lfFaceName[len] = '\0';
122 HFONT hfont = CreateFontIndirect(&winfont);
123 // Windows will always give us a valid pointer here, even if the face name is non-existent. We have to double-check
124 // and see if the family name was really used.
125 HDC dc = GetDC((HWND)0);
127 SelectObject(dc, hfont);
128 WCHAR name[LF_FACESIZE];
129 unsigned resultLength = GetTextFace(dc, LF_FACESIZE, name);
130 if (resultLength > 0)
131 resultLength--; // ignore the null terminator
134 if (!equalIgnoringCase(family, String(name, resultLength))) {
139 return new FontPlatformData(hfont, fontDescription.computedPixelSize());