Added some helpers for actually returning a font from the font button on Windows. Similar helpers are needed on other platforms.

This commit is contained in:
Pietro Gagliardi 2016-04-18 19:18:29 -04:00
parent 861b5f22df
commit a1d0e669c8
2 changed files with 27 additions and 7 deletions

View File

@ -55,6 +55,24 @@ struct uiDrawTextFont {
double size;
};
uiDrawTextFont *mkTextFont(IDWriteFont *df, WCHAR *family, BOOL copyFamily, double size)
{
uiDrawTextFont *font;
WCHAR *copy;
HRESULT hr;
font = uiNew(uiDrawTextFont);
font->f = df;
if (copyFamily) {
copy = (WCHAR *) uiAlloc((wcslen(family) + 1) * sizeof (WCHAR), "WCHAR[]");
wcscpy(copy, family);
font->family = copy;
} else
font->family = family;
font->size = size;
return font;
}
// We could use a C99-style array initializer like the other backends, but C++ doesn't support those.
// But it turns out we need to look up both by uival and dwval, so this loop method is fine...
// TODO consider moving these all to dwrite.cpp
@ -208,17 +226,17 @@ uiDrawTextFont *uiDrawLoadClosestFont(const uiDrawTextFontDescriptor *desc)
BOOL exists;
struct dwriteAttr attr;
IDWriteFontFamily *family;
WCHAR *wfamily;
IDWriteFont *match;
HRESULT hr;
font = uiNew(uiDrawTextFont);
// always get the latest available font information
hr = dwfactory->GetSystemFontCollection(&collection, TRUE);
if (hr != S_OK)
logHRESULT("error getting system font collection in uiDrawLoadClosestFont()", hr);
font->family = toUTF16(desc->Family);
hr = collection->FindFamilyName(font->family, &index, &exists);
wfamily = toUTF16(desc->Family);
hr = collection->FindFamilyName(wfamily, &index, &exists);
if (hr != S_OK)
logHRESULT("error finding font family in uiDrawLoadClosestFont()", hr);
if (!exists)
@ -231,16 +249,17 @@ uiDrawTextFont *uiDrawLoadClosestFont(const uiDrawTextFontDescriptor *desc)
attr.italic = desc->Italic;
attr.stretch = desc->Stretch;
attrToDWriteAttr(&attr);
hr = family->GetFirstMatchingFont(
attr.dweight,
attr.dstretch,
attr.ditalic,
&(font->f));
&match);
if (hr != S_OK)
logHRESULT("error loading font in uiDrawLoadClosestFont()", hr);
font->size = desc->Size;
font = mkTextFont(match,
wfamily, FALSE, // will be freed with font
desc->Size);
family->Release();
collection->Release();

View File

@ -161,6 +161,7 @@ extern WCHAR *fontCollectionCorrectString(fontCollection *fc, IDWriteLocalizedSt
// drawtext.cpp
#ifdef __cplusplus
extern uiDrawTextFont *mkTextFont(IDWriteFont *df, WCHAR *family, BOOL copyFamily, double size);
struct dwriteAttr {
uiDrawTextWeight weight;
uiDrawTextItalic italic;