#include "uipriv_windows.hpp"
#include "attrstr.hpp"
IDWriteFactory *dwfactory = NULL;
HRESULT uiprivInitDrawText(void)
{
return DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED,
__uuidof (IDWriteFactory),
(IUnknown **) (&dwfactory));
}
void uiprivUninitDrawText(void)
{
dwfactory->Release();
}
fontCollection *uiprivLoadFontCollection(void)
{
fontCollection *fc;
HRESULT hr;
fc = uiprivNew(fontCollection);
hr = dwfactory->GetSystemFontCollection(&(fc->fonts), TRUE);
if (hr != S_OK)
logHRESULT(L"error getting system font collection", hr);
fc->userLocaleSuccess = GetUserDefaultLocaleName(fc->userLocale, LOCALE_NAME_MAX_LENGTH);
return fc;
}
void uiprivFontCollectionFree(fontCollection *fc)
{
fc->fonts->Release();
uiprivFree(fc);
}
WCHAR *uiprivFontCollectionFamilyName(fontCollection *fc, IDWriteFontFamily *family)
{
IDWriteLocalizedStrings *names;
WCHAR *str;
HRESULT hr;
hr = family->GetFamilyNames(&names);
if (hr != S_OK)
logHRESULT(L"error getting names of font out", hr);
str = uiprivFontCollectionCorrectString(fc, names);
names->Release();
return str;
}
WCHAR *uiprivFontCollectionCorrectString(fontCollection *fc, IDWriteLocalizedStrings *names)
{
UINT32 index;
BOOL exists;
UINT32 length;
WCHAR *wname;
HRESULT hr;
hr = S_OK;
exists = FALSE;
if (fc->userLocaleSuccess != 0)
hr = names->FindLocaleName(fc->userLocale, &index, &exists);
if (hr != S_OK || (hr == S_OK && !exists))
hr = names->FindLocaleName(L"en-us", &index, &exists);
if (!exists)
index = 0;
hr = names->GetStringLength(index, &length);
if (hr != S_OK)
logHRESULT(L"error getting length of font name", hr);
wname = (WCHAR *) uiprivAlloc((length + 1) * sizeof (WCHAR), "WCHAR[]");
hr = names->GetString(index, wname, length + 1);
if (hr != S_OK)
logHRESULT(L"error getting font name", hr);
return wname;
}