From 9e79ba5209fda0aea57b7b6cd8c4303fea973e04 Mon Sep 17 00:00:00 2001 From: scheffle Date: Fri, 30 Aug 2024 16:50:38 +0200 Subject: [PATCH] initialize fonts on Windows when the resource base path is set fixes #330 --- .../lib/platform/win32/direct2d/d2dfont.cpp | 29 +++++++++---------- vstgui/lib/platform/win32/direct2d/d2dfont.h | 1 + vstgui/lib/platform/win32/win32factory.cpp | 1 + 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/vstgui/lib/platform/win32/direct2d/d2dfont.cpp b/vstgui/lib/platform/win32/direct2d/d2dfont.cpp index 6454512ba..02eeca7cc 100644 --- a/vstgui/lib/platform/win32/direct2d/d2dfont.cpp +++ b/vstgui/lib/platform/win32/direct2d/d2dfont.cpp @@ -49,16 +49,13 @@ struct CustomFonts return false; } - CustomFonts () + CustomFonts (const UTF8String& resourceBasePath) { - auto winFactory = getPlatformFactory ().asWin32Factory (); - if (!winFactory) + if (resourceBasePath.empty ()) return; - auto basePath = winFactory->getResourceBasePath (); - if (!basePath) - return; - *basePath += "Fonts\\*"; - auto files = getDirectoryContents (*basePath); + auto basePath = resourceBasePath; + basePath += "Fonts\\*"; + auto files = getDirectoryContents (basePath); if (files.empty ()) return; auto factory = getDWriteFactory (); @@ -106,6 +103,7 @@ struct CustomFonts return result; } #else + CustomFonts (const UTF8String& resourceBasePath) {} IDWriteFontCollection* getFontCollection () { return nullptr; } bool contains (const WCHAR*, DWRITE_FONT_WEIGHT, DWRITE_FONT_STRETCH, DWRITE_FONT_STYLE) { @@ -116,14 +114,9 @@ struct CustomFonts //----------------------------------------------------------------------------- static std::unique_ptr customFonts; -static std::once_flag customFontsOnceFlag; //----------------------------------------------------------------------------- -static CustomFonts* getCustomFonts () -{ - std::call_once (customFontsOnceFlag, [] () { customFonts = std::make_unique (); }); - return customFonts.get (); -} +static CustomFonts* getCustomFonts () { return customFonts.get (); } //----------------------------------------------------------------------------- static void gatherFonts (const FontFamilyCallback& callback, IDWriteFontCollection* collection) @@ -198,8 +191,14 @@ bool D2DFont::getAllFontFamilies (const FontFamilyCallback& callback) return true; } +//------------------------------------------------------------------------ +void D2DFont::initialize (const UTF8String& resourceBasePath) +{ + D2DFontPrivate::customFonts = std::make_unique (resourceBasePath); +} + //----------------------------------------------------------------------------- -void D2DFont::terminate () { D2DFontPrivate::customFonts = nullptr; } +void D2DFont::terminate () { D2DFontPrivate::customFonts.reset (); } //----------------------------------------------------------------------------- D2DFont::D2DFont (const UTF8String& name, const CCoord& size, const int32_t& style) diff --git a/vstgui/lib/platform/win32/direct2d/d2dfont.h b/vstgui/lib/platform/win32/direct2d/d2dfont.h index 044e7f8fb..55c128d49 100644 --- a/vstgui/lib/platform/win32/direct2d/d2dfont.h +++ b/vstgui/lib/platform/win32/direct2d/d2dfont.h @@ -28,6 +28,7 @@ class D2DFont final : public IPlatformFont, public IFontPainter static bool getAllFontFamilies (const FontFamilyCallback& callback); + static void initialize (const UTF8String& resourceBasePath); static void terminate (); protected: diff --git a/vstgui/lib/platform/win32/win32factory.cpp b/vstgui/lib/platform/win32/win32factory.cpp index 1b17040f1..c32be2e48 100644 --- a/vstgui/lib/platform/win32/win32factory.cpp +++ b/vstgui/lib/platform/win32/win32factory.cpp @@ -141,6 +141,7 @@ HINSTANCE Win32Factory::getInstance () const noexcept void Win32Factory::setResourceBasePath (const UTF8String& path) const noexcept { impl->setBasePath (path); + D2DFont::initialize (path); } //-----------------------------------------------------------------------------