#include "config.h"
#include <winsock2.h>
#include "FontCache.h"
#include "FontData.h"
#include "Font.h"
#include <windows.h>
#include <mlang.h>
#include <ApplicationServices/ApplicationServices.h>
#include <WebKitSystemInterface/WebKitSystemInterface.h>
using std::min;
namespace WebCore
{
void FontCache::platformInit()
{
wkSetUpFontCache(1536 * 1024 * 4); }
IMLangFontLink2* FontCache::getFontLinkInterface()
{
static IMultiLanguage *multiLanguage;
if (!multiLanguage) {
if (CoCreateInstance(CLSID_CMultiLanguage, 0, CLSCTX_ALL, IID_IMultiLanguage, (void**)&multiLanguage) != S_OK)
return 0;
}
static IMLangFontLink2* langFontLink;
if (!langFontLink) {
if (multiLanguage->QueryInterface(&langFontLink) != S_OK)
return 0;
}
return langFontLink;
}
const FontData* FontCache::getFontDataForCharacters(const Font& font, const UChar* characters, int length)
{
IMLangFontLink2* langFontLink = getFontLinkInterface();
if (!langFontLink)
return 0;
FontData* fontData = 0;
HDC hdc = GetDC(0);
HFONT primaryFont = font.primaryFont()->m_font.hfont();
HGDIOBJ oldFont = SelectObject(hdc, primaryFont);
HFONT hfont = 0;
DWORD acpCodePages;
langFontLink->CodePageToCodePages(CP_ACP, &acpCodePages);
DWORD actualCodePages;
long cchActual;
langFontLink->GetStrCodePages(characters, length, acpCodePages, &actualCodePages, &cchActual);
if (cchActual) {
HFONT result;
if (langFontLink->MapFont(hdc, actualCodePages, characters[0], &result) == S_OK) {
LOGFONT lf;
GetObject(result, sizeof(LOGFONT), &lf);
langFontLink->ReleaseFont(result);
hfont = CreateFontIndirect(&lf);
SelectObject(hdc, hfont);
WCHAR name[LF_FACESIZE];
GetTextFace(hdc, LF_FACESIZE, name);
String familyName(name);
if (!familyName.isEmpty()) {
FontPlatformData* result = getCachedFontPlatformData(font.fontDescription(), familyName);
if (result)
fontData = getCachedFontData(result);
}
}
}
SelectObject(hdc, oldFont);
if (hfont)
DeleteObject(hfont);
ReleaseDC(0, hdc);
return fontData;
}
FontPlatformData* FontCache::getSimilarFontPlatformData(const Font& font)
{
return 0;
}
FontPlatformData* FontCache::getLastResortFallbackFont(const Font& font)
{
static AtomicString timesStr("Times New Roman");
return getCachedFontPlatformData(font.fontDescription(), timesStr);
}
FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family)
{
LOGFONT winfont;
winfont.lfHeight = -fontDescription.computedPixelSize() * 32;
winfont.lfWidth = 0;
winfont.lfEscapement = 0;
winfont.lfOrientation = 0;
winfont.lfUnderline = false;
winfont.lfStrikeOut = false;
winfont.lfCharSet = DEFAULT_CHARSET;
#if PLATFORM(CG)
winfont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
#else
winfont.lfOutPrecision = OUT_TT_PRECIS;
#endif
winfont.lfQuality = 5; winfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
winfont.lfItalic = fontDescription.italic();
static AtomicString lucidaStr("Lucida Grande");
if (equalIgnoringCase(family, lucidaStr))
winfont.lfWeight = fontDescription.bold() ? 600 : 500;
else
winfont.lfWeight = fontDescription.bold() ? 700 : 400;
int len = min(family.length(), (unsigned int)LF_FACESIZE - 1);
memcpy(winfont.lfFaceName, family.characters(), len * sizeof(WORD));
winfont.lfFaceName[len] = '\0';
HFONT hfont = CreateFontIndirect(&winfont);
HDC dc = GetDC((HWND)0);
SaveDC(dc);
SelectObject(dc, hfont);
WCHAR name[LF_FACESIZE];
GetTextFace(dc, LF_FACESIZE, name);
RestoreDC(dc, -1);
ReleaseDC(0, dc);
if (_wcsicmp(winfont.lfFaceName, name)) {
DeleteObject(hfont);
return 0;
}
FontPlatformData* result = new FontPlatformData(hfont, fontDescription.computedPixelSize(),
fontDescription.bold(), fontDescription.italic());
if (!result->cgFont()) {
delete result;
DeleteObject(hfont);
return 0;
}
return result;
}
}