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; 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. // 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... // 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 // TODO consider moving these all to dwrite.cpp
@ -208,17 +226,17 @@ uiDrawTextFont *uiDrawLoadClosestFont(const uiDrawTextFontDescriptor *desc)
BOOL exists; BOOL exists;
struct dwriteAttr attr; struct dwriteAttr attr;
IDWriteFontFamily *family; IDWriteFontFamily *family;
WCHAR *wfamily;
IDWriteFont *match;
HRESULT hr; HRESULT hr;
font = uiNew(uiDrawTextFont);
// always get the latest available font information // always get the latest available font information
hr = dwfactory->GetSystemFontCollection(&collection, TRUE); hr = dwfactory->GetSystemFontCollection(&collection, TRUE);
if (hr != S_OK) if (hr != S_OK)
logHRESULT("error getting system font collection in uiDrawLoadClosestFont()", hr); logHRESULT("error getting system font collection in uiDrawLoadClosestFont()", hr);
font->family = toUTF16(desc->Family); wfamily = toUTF16(desc->Family);
hr = collection->FindFamilyName(font->family, &index, &exists); hr = collection->FindFamilyName(wfamily, &index, &exists);
if (hr != S_OK) if (hr != S_OK)
logHRESULT("error finding font family in uiDrawLoadClosestFont()", hr); logHRESULT("error finding font family in uiDrawLoadClosestFont()", hr);
if (!exists) if (!exists)
@ -231,16 +249,17 @@ uiDrawTextFont *uiDrawLoadClosestFont(const uiDrawTextFontDescriptor *desc)
attr.italic = desc->Italic; attr.italic = desc->Italic;
attr.stretch = desc->Stretch; attr.stretch = desc->Stretch;
attrToDWriteAttr(&attr); attrToDWriteAttr(&attr);
hr = family->GetFirstMatchingFont( hr = family->GetFirstMatchingFont(
attr.dweight, attr.dweight,
attr.dstretch, attr.dstretch,
attr.ditalic, attr.ditalic,
&(font->f)); &match);
if (hr != S_OK) if (hr != S_OK)
logHRESULT("error loading font in uiDrawLoadClosestFont()", hr); 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(); family->Release();
collection->Release(); collection->Release();

View File

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