Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialize fonts on Windows when the resource base path is set #331

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions vstgui/lib/platform/win32/direct2d/d2dfont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down Expand Up @@ -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)
{
Expand All @@ -116,14 +114,9 @@ struct CustomFonts

//-----------------------------------------------------------------------------
static std::unique_ptr<CustomFonts> customFonts;
static std::once_flag customFontsOnceFlag;

//-----------------------------------------------------------------------------
static CustomFonts* getCustomFonts ()
{
std::call_once (customFontsOnceFlag, [] () { customFonts = std::make_unique<CustomFonts> (); });
return customFonts.get ();
}
static CustomFonts* getCustomFonts () { return customFonts.get (); }

//-----------------------------------------------------------------------------
static void gatherFonts (const FontFamilyCallback& callback, IDWriteFontCollection* collection)
Expand Down Expand Up @@ -198,8 +191,14 @@ bool D2DFont::getAllFontFamilies (const FontFamilyCallback& callback)
return true;
}

//------------------------------------------------------------------------
void D2DFont::initialize (const UTF8String& resourceBasePath)
{
D2DFontPrivate::customFonts = std::make_unique<D2DFontPrivate::CustomFonts> (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)
Expand Down
1 change: 1 addition & 0 deletions vstgui/lib/platform/win32/direct2d/d2dfont.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions vstgui/lib/platform/win32/win32factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ HINSTANCE Win32Factory::getInstance () const noexcept
void Win32Factory::setResourceBasePath (const UTF8String& path) const noexcept
{
impl->setBasePath (path);
D2DFont::initialize (path);
}

//-----------------------------------------------------------------------------
Expand Down