From a1d0e669c8d9a05eef92a5f15eba70dcb6fecc77 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 18 Apr 2016 19:18:29 -0400 Subject: [PATCH] Added some helpers for actually returning a font from the font button on Windows. Similar helpers are needed on other platforms. --- windows/drawtext.cpp | 33 ++++++++++++++++++++++++++------- windows/uipriv_windows.h | 1 + 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/windows/drawtext.cpp b/windows/drawtext.cpp index e7dbe801..5ce4a0e7 100644 --- a/windows/drawtext.cpp +++ b/windows/drawtext.cpp @@ -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(); diff --git a/windows/uipriv_windows.h b/windows/uipriv_windows.h index be278c0a..d84a3af5 100644 --- a/windows/uipriv_windows.h +++ b/windows/uipriv_windows.h @@ -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;