1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

DirectWrite: Attempt to match font styles manually before falling back to system implementation

This allows for styles other than normal/bold/italic/bold-italic to be
selected, and more closely matches the behaviour of font selection on
other platforms.
This commit is contained in:
reuk 2024-10-01 15:16:03 +01:00
parent 1a1dc90a24
commit 14dc97a29e
No known key found for this signature in database
GPG key ID: FCB43929F012EE5C

View file

@ -114,6 +114,28 @@ public:
return stringArrayFromRange (strings);
}
static std::vector<ComSmartPtr<IDWriteFont>> getAllFontsInFamily (IDWriteFontFamily* fontFamily)
{
const auto fontFacesCount = fontFamily->GetFontCount();
std::vector<ComSmartPtr<IDWriteFont>> result;
result.reserve (fontFacesCount);
for (UINT32 i = 0; i < fontFacesCount; ++i)
{
ComSmartPtr<IDWriteFont> dwFont;
if (FAILED (fontFamily->GetFont (i, dwFont.resetAndGetPointerAddress())))
continue;
if (dwFont->GetSimulations() != DWRITE_FONT_SIMULATIONS_NONE)
continue;
result.push_back (dwFont);
}
return result;
}
StringArray findAllTypefaceStyles (const String& family)
{
const std::scoped_lock lock { mutex };
@ -131,19 +153,10 @@ public:
if (FAILED (collection->GetFontFamily (fontIndex, fontFamily.resetAndGetPointerAddress())) || fontFamily == nullptr)
continue;
// Get the font faces
const auto fontFacesCount = fontFamily->GetFontCount();
std::set<String> results;
for (uint32 i = 0; i < fontFacesCount; ++i)
{
ComSmartPtr<IDWriteFont> dwFont;
if (FAILED (fontFamily->GetFont (i, dwFont.resetAndGetPointerAddress())) || dwFont->GetSimulations() != DWRITE_FONT_SIMULATIONS_NONE)
continue;
results.insert (getFontFaceName (dwFont));
}
for (const auto& font : getAllFontsInFamily (fontFamily))
results.insert (getFontFaceName (font));
return stringArrayFromRange (results);
}
@ -567,6 +580,17 @@ public:
if (family == nullptr)
return getLastResortTypeface (f);
// Try matching the typeface style first
const auto fonts = AggregateFontCollection::getAllFontsInFamily (family);
const auto matchingStyle = std::find_if (fonts.begin(), fonts.end(), [&] (const auto& ptr)
{
return style.compareIgnoreCase (getFontFaceName (ptr)) == 0;
});
if (matchingStyle != fonts.end())
return fromFont (*matchingStyle, nullptr, &f, MetricsMechanism::dwriteOnly);
// No matching typeface style, so let dwrite try to find a reasonable substitute
const auto weight = f.isBold() ? DWRITE_FONT_WEIGHT_BOLD : DWRITE_FONT_WEIGHT_NORMAL;
const auto italic = f.isItalic() ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL;