#include "config.h"
#include <winsock2.h>
#include "Font.h"
#include "FontCache.h"
#include "HWndDC.h"
#include <mlang.h>
#include <windows.h>
#include <wtf/HashSet.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/StdLibExtras.h>
#include <wtf/text/CString.h>
#include <wtf/text/StringHash.h>
#include <wtf/text/StringView.h>
#include <wtf/win/GDIObject.h>
#if USE(CG)
#include "CoreGraphicsSPI.h"
#include <ApplicationServices/ApplicationServices.h>
#include <WebKitSystemInterface/WebKitSystemInterface.h>
#endif
using std::min;
namespace WebCore
{
void FontCache::platformInit()
{
#if USE(CG)
size_t size = 1536 * 1024 * 4; CGFontSetShouldUseMulticache(true);
CGFontCache* fontCache = CGFontCacheGetLocalCache();
CGFontCacheSetShouldAutoExpire(fontCache, false);
CGFontCacheSetMaxSize(fontCache, size);
#endif
}
IMLangFontLinkType* FontCache::getFontLinkInterface()
{
static IMultiLanguage *multiLanguage;
if (!multiLanguage) {
if (CoCreateInstance(CLSID_CMultiLanguage, 0, CLSCTX_ALL, IID_IMultiLanguage, (void**)&multiLanguage) != S_OK)
return 0;
}
static IMLangFontLinkType* langFontLink;
if (!langFontLink) {
if (multiLanguage->QueryInterface(&langFontLink) != S_OK)
return 0;
}
return langFontLink;
}
static int CALLBACK metaFileEnumProc(HDC hdc, HANDLETABLE* table, CONST ENHMETARECORD* record, int tableEntries, LPARAM logFont)
{
if (record->iType == EMR_EXTCREATEFONTINDIRECTW) {
const EMREXTCREATEFONTINDIRECTW* createFontRecord = reinterpret_cast<const EMREXTCREATEFONTINDIRECTW*>(record);
*reinterpret_cast<LOGFONT*>(logFont) = createFontRecord->elfw.elfLogFont;
}
return true;
}
static int CALLBACK linkedFontEnumProc(CONST LOGFONT* logFont, CONST TEXTMETRIC* metrics, DWORD fontType, LPARAM hfont)
{
*reinterpret_cast<HFONT*>(hfont) = CreateFontIndirect(logFont);
return false;
}
static const Vector<String>* getLinkedFonts(String& family)
{
static HashMap<String, Vector<String>*> systemLinkMap;
Vector<String>* result = systemLinkMap.get(family);
if (result)
return result;
result = new Vector<String>;
systemLinkMap.set(family, result);
HKEY fontLinkKey = nullptr;
if (FAILED(RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\FontLink\\SystemLink", 0, KEY_READ, &fontLinkKey)))
return result;
DWORD linkedFontsBufferSize = 0;
if (::RegQueryValueEx(fontLinkKey, family.charactersWithNullTermination().data(), 0, nullptr, nullptr, &linkedFontsBufferSize) == ERROR_FILE_NOT_FOUND) {
WTFLogAlways("The font link key %s does not exist in the registry.", family.utf8().data());
return result;
}
WCHAR* linkedFonts = reinterpret_cast<WCHAR*>(malloc(linkedFontsBufferSize));
if (SUCCEEDED(RegQueryValueEx(fontLinkKey, family.charactersWithNullTermination().data(), 0, NULL, reinterpret_cast<BYTE*>(linkedFonts), &linkedFontsBufferSize))) {
unsigned i = 0;
unsigned length = linkedFontsBufferSize / sizeof(*linkedFonts);
while (i < length) {
while (i < length && linkedFonts[i] != ',')
i++;
i++;
unsigned j = i;
while (j < length && linkedFonts[j])
j++;
result->append(String(linkedFonts + i, j - i));
i = j + 1;
}
}
free(linkedFonts);
RegCloseKey(fontLinkKey);
return result;
}
static const Vector<DWORD, 4>& getCJKCodePageMasks()
{
static const UINT CJKCodePages[] = {
932,
936,
950,
949
};
static Vector<DWORD, 4> codePageMasks;
static bool initialized;
if (!initialized) {
initialized = true;
IMLangFontLinkType* langFontLink = FontCache::singleton().getFontLinkInterface();
if (!langFontLink)
return codePageMasks;
UINT defaultCodePage;
DWORD defaultCodePageMask = 0;
if (GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_RETURN_NUMBER | LOCALE_IDEFAULTANSICODEPAGE, reinterpret_cast<LPWSTR>(&defaultCodePage), sizeof(defaultCodePage)))
langFontLink->CodePageToCodePages(defaultCodePage, &defaultCodePageMask);
if (defaultCodePage == CJKCodePages[0] || defaultCodePage == CJKCodePages[1] || defaultCodePage == CJKCodePages[2] || defaultCodePage == CJKCodePages[3])
codePageMasks.append(defaultCodePageMask);
for (unsigned i = 0; i < 4; ++i) {
if (defaultCodePage != CJKCodePages[i]) {
DWORD codePageMask;
langFontLink->CodePageToCodePages(CJKCodePages[i], &codePageMask);
codePageMasks.append(codePageMask);
}
}
}
return codePageMasks;
}
static bool currentFontContainsCharacter(HDC hdc, UChar character)
{
static Vector<char, 512> glyphsetBuffer;
glyphsetBuffer.resize(GetFontUnicodeRanges(hdc, 0));
GLYPHSET* glyphset = reinterpret_cast<GLYPHSET*>(glyphsetBuffer.data());
GetFontUnicodeRanges(hdc, glyphset);
unsigned i = 0;
while (i < glyphset->cRanges && glyphset->ranges[i].wcLow <= character)
i++;
return i && glyphset->ranges[i - 1].wcLow + glyphset->ranges[i - 1].cGlyphs > character;
}
static HFONT createMLangFont(IMLangFontLinkType* langFontLink, HDC hdc, DWORD codePageMask, UChar character = 0)
{
HFONT MLangFont;
HFONT hfont = 0;
if (SUCCEEDED(langFontLink->MapFont(hdc, codePageMask, character, &MLangFont)) && MLangFont) {
LOGFONT lf;
GetObject(MLangFont, sizeof(LOGFONT), &lf);
langFontLink->ReleaseFont(MLangFont);
hfont = CreateFontIndirect(&lf);
}
return hfont;
}
RefPtr<Font> FontCache::systemFallbackForCharacters(const FontDescription& description, const Font* originalFontData, bool, const UChar* characters, unsigned length)
{
UChar character = characters[0];
RefPtr<Font> fontData;
HWndDC hdc(0);
HFONT primaryFont = originalFontData->platformData().hfont();
HGDIOBJ oldFont = SelectObject(hdc, primaryFont);
HFONT hfont = 0;
if (IMLangFontLinkType* langFontLink = getFontLinkInterface()) {
DWORD codePages = 0;
if (SUCCEEDED(langFontLink->GetCharCodePages(character, &codePages))) {
if (codePages && u_getIntPropertyValue(character, UCHAR_UNIFIED_IDEOGRAPH)) {
const Vector<DWORD, 4>& CJKCodePageMasks = getCJKCodePageMasks();
unsigned numCodePages = CJKCodePageMasks.size();
for (unsigned i = 0; i < numCodePages && !hfont; ++i) {
hfont = createMLangFont(langFontLink, hdc, CJKCodePageMasks[i]);
if (hfont && !(codePages & CJKCodePageMasks[i])) {
SelectObject(hdc, hfont);
if (!currentFontContainsCharacter(hdc, character)) {
DeleteObject(hfont);
hfont = 0;
}
SelectObject(hdc, primaryFont);
}
}
} else
hfont = createMLangFont(langFontLink, hdc, codePages, character);
}
}
bool containsCharacter = hfont;
if (!hfont) {
HDC metaFileDc = CreateEnhMetaFile(hdc, NULL, NULL, NULL);
SelectObject(metaFileDc, primaryFont);
bool scriptStringOutSucceeded = false;
SCRIPT_STRING_ANALYSIS ssa;
if (SUCCEEDED(ScriptStringAnalyse(metaFileDc, characters, length, 0, -1, SSA_METAFILE | SSA_FALLBACK | SSA_GLYPHS | SSA_LINK,
0, NULL, NULL, NULL, NULL, NULL, &ssa))) {
scriptStringOutSucceeded = SUCCEEDED(ScriptStringOut(ssa, 0, 0, 0, NULL, 0, 0, FALSE));
ScriptStringFree(&ssa);
}
HENHMETAFILE metaFile = CloseEnhMetaFile(metaFileDc);
if (scriptStringOutSucceeded) {
LOGFONT logFont;
logFont.lfFaceName[0] = 0;
EnumEnhMetaFile(0, metaFile, metaFileEnumProc, &logFont, NULL);
if (logFont.lfFaceName[0])
hfont = CreateFontIndirect(&logFont);
}
DeleteEnhMetaFile(metaFile);
}
String familyName;
const Vector<String>* linkedFonts = 0;
unsigned linkedFontIndex = 0;
while (hfont) {
SelectObject(hdc, hfont);
WCHAR name[LF_FACESIZE];
GetTextFace(hdc, LF_FACESIZE, name);
familyName = name;
if (containsCharacter || currentFontContainsCharacter(hdc, character))
break;
if (!linkedFonts)
linkedFonts = getLinkedFonts(familyName);
SelectObject(hdc, oldFont);
DeleteObject(hfont);
hfont = 0;
if (linkedFonts->size() <= linkedFontIndex)
break;
LOGFONT logFont;
logFont.lfCharSet = DEFAULT_CHARSET;
StringView(linkedFonts->at(linkedFontIndex)).getCharactersWithUpconvert(logFont.lfFaceName);
logFont.lfFaceName[linkedFonts->at(linkedFontIndex).length()] = 0;
EnumFontFamiliesEx(hdc, &logFont, linkedFontEnumProc, reinterpret_cast<LPARAM>(&hfont), 0);
linkedFontIndex++;
}
if (hfont) {
if (!familyName.isEmpty()) {
FontPlatformData* result = getCachedFontPlatformData(description, familyName);
if (result)
fontData = fontForPlatformData(*result);
}
SelectObject(hdc, oldFont);
DeleteObject(hfont);
}
return fontData.release();
}
Vector<String> FontCache::systemFontFamilies()
{
Vector<String> fontFamilies;
return fontFamilies;
}
RefPtr<Font> FontCache::fontFromDescriptionAndLogFont(const FontDescription& fontDescription, const LOGFONT& font, AtomicString& outFontFamilyName)
{
AtomicString familyName = String(font.lfFaceName, wcsnlen(font.lfFaceName, LF_FACESIZE));
RefPtr<Font> fontData = fontForFamily(fontDescription, familyName);
if (fontData)
outFontFamilyName = familyName;
return fontData;
}
Ref<Font> FontCache::lastResortFallbackFont(const FontDescription& fontDescription)
{
DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, fallbackFontName, ());
if (!fallbackFontName.isEmpty())
return *fontForFamily(fontDescription, fallbackFontName);
static AtomicString fallbackFonts[] = {
AtomicString("Times New Roman", AtomicString::ConstructFromLiteral),
AtomicString("Microsoft Sans Serif", AtomicString::ConstructFromLiteral),
AtomicString("Tahoma", AtomicString::ConstructFromLiteral),
AtomicString("Lucida Sans Unicode", AtomicString::ConstructFromLiteral),
AtomicString("Arial", AtomicString::ConstructFromLiteral)
};
RefPtr<Font> simpleFont;
for (size_t i = 0; i < WTF_ARRAY_LENGTH(fallbackFonts); ++i) {
if (simpleFont = fontForFamily(fontDescription, fallbackFonts[i])) {
fallbackFontName = fallbackFonts[i];
return *simpleFont;
}
}
if (HFONT defaultGUIFont = static_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT))) {
LOGFONT defaultGUILogFont;
GetObject(defaultGUIFont, sizeof(defaultGUILogFont), &defaultGUILogFont);
if (simpleFont = fontFromDescriptionAndLogFont(fontDescription, defaultGUILogFont, fallbackFontName))
return *simpleFont;
}
NONCLIENTMETRICS nonClientMetrics = {0};
nonClientMetrics.cbSize = sizeof(nonClientMetrics);
if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(nonClientMetrics), &nonClientMetrics, 0)) {
if (simpleFont = fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfMessageFont, fallbackFontName))
return *simpleFont;
if (simpleFont = fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfMenuFont, fallbackFontName))
return *simpleFont;
if (simpleFont = fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfStatusFont, fallbackFontName))
return *simpleFont;
if (simpleFont = fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfCaptionFont, fallbackFontName))
return *simpleFont;
if (simpleFont = fontFromDescriptionAndLogFont(fontDescription, nonClientMetrics.lfSmCaptionFont, fallbackFontName))
return *simpleFont;
}
ASSERT_NOT_REACHED();
return *simpleFont;
}
static LONG toGDIFontWeight(FontWeight fontWeight)
{
static LONG gdiFontWeights[] = {
FW_THIN, FW_EXTRALIGHT, FW_LIGHT, FW_NORMAL, FW_MEDIUM, FW_SEMIBOLD, FW_BOLD, FW_EXTRABOLD, FW_HEAVY };
return gdiFontWeights[fontWeight];
}
static inline bool isGDIFontWeightBold(LONG gdiFontWeight)
{
return gdiFontWeight >= FW_SEMIBOLD;
}
static LONG adjustedGDIFontWeight(LONG gdiFontWeight, const String& family)
{
if (equalLettersIgnoringASCIICase(family, "lucida grande")) {
if (gdiFontWeight == FW_NORMAL)
return FW_MEDIUM;
if (gdiFontWeight == FW_BOLD)
return FW_SEMIBOLD;
}
return gdiFontWeight;
}
struct MatchImprovingProcData {
MatchImprovingProcData(LONG desiredWeight, bool desiredItalic)
: m_desiredWeight(desiredWeight)
, m_desiredItalic(desiredItalic)
, m_hasMatched(false)
{
}
LONG m_desiredWeight;
bool m_desiredItalic;
bool m_hasMatched;
LOGFONT m_chosen;
};
static int CALLBACK matchImprovingEnumProc(CONST LOGFONT* candidate, CONST TEXTMETRIC* metrics, DWORD fontType, LPARAM lParam)
{
MatchImprovingProcData* matchData = reinterpret_cast<MatchImprovingProcData*>(lParam);
if (!matchData->m_hasMatched) {
matchData->m_hasMatched = true;
matchData->m_chosen = *candidate;
return 1;
}
if (!candidate->lfItalic != !matchData->m_chosen.lfItalic) {
if (!candidate->lfItalic == !matchData->m_desiredItalic)
matchData->m_chosen = *candidate;
return 1;
}
unsigned chosenWeightDeltaMagnitude = abs(matchData->m_chosen.lfWeight - matchData->m_desiredWeight);
unsigned candidateWeightDeltaMagnitude = abs(candidate->lfWeight - matchData->m_desiredWeight);
if (chosenWeightDeltaMagnitude == candidateWeightDeltaMagnitude && abs(candidate->lfWeight - FW_NORMAL) > abs(matchData->m_chosen.lfWeight - FW_NORMAL)) {
matchData->m_chosen = *candidate;
return 1;
}
if (candidateWeightDeltaMagnitude < chosenWeightDeltaMagnitude)
matchData->m_chosen = *candidate;
return 1;
}
static GDIObject<HFONT> createGDIFont(const AtomicString& family, LONG desiredWeight, bool desiredItalic, int size, bool synthesizeItalic)
{
HWndDC hdc(0);
LOGFONT logFont;
logFont.lfCharSet = DEFAULT_CHARSET;
StringView truncatedFamily = StringView(family).substring(0, static_cast<unsigned>(LF_FACESIZE - 1));
truncatedFamily.getCharactersWithUpconvert(logFont.lfFaceName);
logFont.lfFaceName[truncatedFamily.length()] = 0;
logFont.lfPitchAndFamily = 0;
MatchImprovingProcData matchData(desiredWeight, desiredItalic);
EnumFontFamiliesEx(hdc, &logFont, matchImprovingEnumProc, reinterpret_cast<LPARAM>(&matchData), 0);
if (!matchData.m_hasMatched)
return nullptr;
matchData.m_chosen.lfHeight = -size;
matchData.m_chosen.lfWidth = 0;
matchData.m_chosen.lfEscapement = 0;
matchData.m_chosen.lfOrientation = 0;
matchData.m_chosen.lfUnderline = false;
matchData.m_chosen.lfStrikeOut = false;
matchData.m_chosen.lfCharSet = DEFAULT_CHARSET;
#if USE(CG) || USE(CAIRO)
matchData.m_chosen.lfOutPrecision = OUT_TT_ONLY_PRECIS;
#else
matchData.m_chosen.lfOutPrecision = OUT_TT_PRECIS;
#endif
matchData.m_chosen.lfQuality = DEFAULT_QUALITY;
matchData.m_chosen.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
if (desiredItalic && !matchData.m_chosen.lfItalic && synthesizeItalic)
matchData.m_chosen.lfItalic = 1;
auto chosenFont = adoptGDIObject(::CreateFontIndirect(&matchData.m_chosen));
if (!chosenFont)
return nullptr;
HWndDC dc(0);
SaveDC(dc);
SelectObject(dc, chosenFont.get());
WCHAR actualName[LF_FACESIZE];
GetTextFace(dc, LF_FACESIZE, actualName);
RestoreDC(dc, -1);
if (wcsicmp(matchData.m_chosen.lfFaceName, actualName))
return nullptr;
return chosenFont;
}
struct TraitsInFamilyProcData {
TraitsInFamilyProcData(const AtomicString& familyName)
: m_familyName(familyName)
{
}
const AtomicString& m_familyName;
HashSet<unsigned> m_traitsMasks;
};
static int CALLBACK traitsInFamilyEnumProc(CONST LOGFONT* logFont, CONST TEXTMETRIC* metrics, DWORD fontType, LPARAM lParam)
{
TraitsInFamilyProcData* procData = reinterpret_cast<TraitsInFamilyProcData*>(lParam);
unsigned traitsMask = 0;
traitsMask |= logFont->lfItalic ? FontStyleItalicMask : FontStyleNormalMask;
LONG weight = adjustedGDIFontWeight(logFont->lfWeight, procData->m_familyName);
traitsMask |= weight == FW_THIN ? FontWeight100Mask :
weight == FW_EXTRALIGHT ? FontWeight200Mask :
weight == FW_LIGHT ? FontWeight300Mask :
weight == FW_NORMAL ? FontWeight400Mask :
weight == FW_MEDIUM ? FontWeight500Mask :
weight == FW_SEMIBOLD ? FontWeight600Mask :
weight == FW_BOLD ? FontWeight700Mask :
weight == FW_EXTRABOLD ? FontWeight800Mask :
FontWeight900Mask;
procData->m_traitsMasks.add(traitsMask);
return 1;
}
Vector<FontTraitsMask> FontCache::getTraitsInFamily(const AtomicString& familyName)
{
HWndDC hdc(0);
LOGFONT logFont;
logFont.lfCharSet = DEFAULT_CHARSET;
StringView truncatedFamily = StringView(familyName).substring(0, static_cast<unsigned>(LF_FACESIZE - 1));
truncatedFamily.getCharactersWithUpconvert(logFont.lfFaceName);
logFont.lfFaceName[truncatedFamily.length()] = 0;
logFont.lfPitchAndFamily = 0;
TraitsInFamilyProcData procData(familyName);
EnumFontFamiliesEx(hdc, &logFont, traitsInFamilyEnumProc, reinterpret_cast<LPARAM>(&procData), 0);
Vector<FontTraitsMask> result;
result.reserveInitialCapacity(procData.m_traitsMasks.size());
for (unsigned mask : procData.m_traitsMasks)
result.uncheckedAppend(static_cast<FontTraitsMask>(mask));
return result;
}
std::unique_ptr<FontPlatformData> FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& family, const FontFeatureSettings*, const FontVariantSettings*)
{
bool isLucidaGrande = equalLettersIgnoringASCIICase(family, "lucida grande");
bool useGDI = fontDescription.renderingMode() == FontRenderingMode::Alternate && !isLucidaGrande;
LONG weight = adjustedGDIFontWeight(toGDIFontWeight(fontDescription.weight()), family);
auto hfont = createGDIFont(family, weight, fontDescription.italic(),
fontDescription.computedPixelSize() * (useGDI ? 1 : 32), useGDI);
if (!hfont)
return nullptr;
if (isLucidaGrande)
useGDI = false;
LOGFONT logFont;
GetObject(hfont.get(), sizeof(LOGFONT), &logFont);
bool synthesizeBold = isGDIFontWeightBold(weight) && !isGDIFontWeightBold(logFont.lfWeight);
bool synthesizeItalic = fontDescription.italic() && !logFont.lfItalic;
auto result = std::make_unique<FontPlatformData>(WTFMove(hfont), fontDescription.computedPixelSize(), synthesizeBold, synthesizeItalic, useGDI);
#if USE(CG)
bool fontCreationFailed = !result->cgFont();
#elif USE(CAIRO)
bool fontCreationFailed = !result->scaledFont();
#endif
if (fontCreationFailed) {
return nullptr;
}
return result;
}
const AtomicString& FontCache::platformAlternateFamilyName(const AtomicString& familyName)
{
static NeverDestroyed<AtomicString> timesNewRoman("Times New Roman", AtomicString::ConstructFromLiteral);
static NeverDestroyed<AtomicString> microsoftSansSerif("Microsoft Sans Serif", AtomicString::ConstructFromLiteral);
switch (familyName.length()) {
case 8:
if (equalLettersIgnoringASCIICase(familyName, "ms serif"))
return timesNewRoman;
break;
case 13:
if (equalLettersIgnoringASCIICase(familyName, "ms sans serif"))
return microsoftSansSerif;
break;
}
return nullAtom;
}
}