-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Fix bugs introduced in #16821 (custom font fallback) #16993
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,6 +187,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation | |
UpdateFontList(); | ||
} | ||
const auto& currentFontList{ CompleteFontList() }; | ||
fallbackFont = currentFontList.First().Current(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another temporary hack. |
||
for (const auto& font : currentFontList) | ||
{ | ||
if (font.LocalizedName() == name) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -482,32 +482,17 @@ void AtlasEngine::SetWarningCallback(std::function<void(HRESULT, wil::zwstring_v | |
// commit 9e86c98 (PR #16196), because it showed that it's definitely not due to FindFamilyName() failing. | ||
// | ||
// The workaround is to catch the exception and retry it with our nearby fonts manually loaded in. | ||
HRESULT hr = _updateFont(fontInfoDesired, fontInfo, features, axes); | ||
|
||
if constexpr (Feature_NearbyFontLoading::IsEnabled()) | ||
{ | ||
try | ||
{ | ||
_updateFont(fontInfoDesired, fontInfo, features, axes); | ||
return S_OK; | ||
} | ||
CATCH_LOG(); | ||
|
||
// _resolveFontMetrics() checks `_api.s->font->fontCollection` for a pre-existing font collection, | ||
// before falling back to using the system font collection. This way we can inject our custom one. | ||
// Doing it this way is a bit hacky, but it does have the benefit that we can cache a font collection | ||
// instance across font changes, like when zooming the font size rapidly using the scroll wheel. | ||
try | ||
if (FAILED(hr) && _updateWithNearbyFontCollection()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we technically need this at all anymore? the first failed font will automatically use the nearby collection, down in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried that at one point and it didn't work. My current leading theory is that DWrite's internal font cache thinks the font exists, tells us it exists, and only once it actually tries to use the font - for instance when we call |
||
{ | ||
_api.s.write()->font.write()->fontCollection = FontCache::GetCached(); | ||
hr = _updateFont(fontInfoDesired, fontInfo, features, axes); | ||
} | ||
CATCH_LOG(); | ||
} | ||
|
||
try | ||
{ | ||
_updateFont(fontInfoDesired, fontInfo, features, axes); | ||
return S_OK; | ||
} | ||
CATCH_RETURN(); | ||
return hr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HRESULT makes it simpler to write the |
||
} | ||
|
||
void AtlasEngine::UpdateHyperlinkHoveredId(const uint16_t hoveredId) noexcept | ||
|
@@ -536,7 +521,8 @@ void AtlasEngine::_resolveTransparencySettings() noexcept | |
} | ||
} | ||
|
||
void AtlasEngine::_updateFont(const FontInfoDesired& fontInfoDesired, FontInfo& fontInfo, const std::unordered_map<std::wstring_view, uint32_t>& features, const std::unordered_map<std::wstring_view, float>& axes) | ||
[[nodiscard]] HRESULT AtlasEngine::_updateFont(const FontInfoDesired& fontInfoDesired, FontInfo& fontInfo, const std::unordered_map<std::wstring_view, uint32_t>& features, const std::unordered_map<std::wstring_view, float>& axes) noexcept | ||
try | ||
{ | ||
std::vector<DWRITE_FONT_FEATURE> fontFeatures; | ||
if (!features.empty()) | ||
|
@@ -616,9 +602,12 @@ void AtlasEngine::_updateFont(const FontInfoDesired& fontInfoDesired, FontInfo& | |
_resolveFontMetrics(fontInfoDesired, fontInfo, font); | ||
font->fontFeatures = std::move(fontFeatures); | ||
font->fontAxisValues = std::move(fontAxisValues); | ||
|
||
return S_OK; | ||
} | ||
CATCH_RETURN() | ||
|
||
void AtlasEngine::_resolveFontMetrics(const FontInfoDesired& fontInfoDesired, FontInfo& fontInfo, FontSettings* fontMetrics) const | ||
void AtlasEngine::_resolveFontMetrics(const FontInfoDesired& fontInfoDesired, FontInfo& fontInfo, FontSettings* fontMetrics) | ||
{ | ||
const auto& faceName = fontInfoDesired.GetFaceName(); | ||
const auto requestedFamily = fontInfoDesired.GetFamily(); | ||
|
@@ -659,6 +648,16 @@ void AtlasEngine::_resolveFontMetrics(const FontInfoDesired& fontInfoDesired, Fo | |
BOOL exists = false; | ||
THROW_IF_FAILED(fontCollection->FindFamilyName(fontName.c_str(), &index, &exists)); | ||
|
||
// In case of a portable build, the given font may not be installed and instead be bundled next to our executable. | ||
if constexpr (Feature_NearbyFontLoading::IsEnabled()) | ||
{ | ||
if (!exists && _updateWithNearbyFontCollection()) | ||
{ | ||
fontCollection = _api.s->font->fontCollection; | ||
THROW_IF_FAILED(fontCollection->FindFamilyName(fontName.c_str(), &index, &exists)); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fix for the dialog. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay so now, on the first failed font in the list, we'll fall back to the nearby font collection for all the remaining ones |
||
|
||
if (!exists) | ||
{ | ||
if (!missingFontNames.empty()) | ||
|
@@ -869,3 +868,29 @@ void AtlasEngine::_resolveFontMetrics(const FontInfoDesired& fontInfoDesired, Fo | |
fontMetrics->colorGlyphs = fontInfoDesired.GetEnableColorGlyphs(); | ||
} | ||
} | ||
|
||
// Nearby fonts are described a couple times throughout the file. | ||
// This abstraction in particular helps us avoid retrying when it's pointless: | ||
// After all, if the font collection didn't change (no nearby fonts, loading failed, it's already loaded), | ||
// we don't need to try it again. It returns true if retrying is necessary. | ||
[[nodiscard]] bool AtlasEngine::_updateWithNearbyFontCollection() | ||
{ | ||
// _resolveFontMetrics() checks `_api.s->font->fontCollection` for a pre-existing font collection, | ||
// before falling back to using the system font collection. This way we can inject our custom one. | ||
// Doing it this way is a bit hacky, but it does have the benefit that we can cache a font collection | ||
// instance across font changes, like when zooming the font size rapidly using the scroll wheel. | ||
wil::com_ptr<IDWriteFontCollection> collection; | ||
try | ||
{ | ||
collection = FontCache::GetCached(); | ||
} | ||
CATCH_LOG(); | ||
|
||
if (!collection || _api.s->font->fontCollection == collection) | ||
{ | ||
return false; | ||
} | ||
|
||
_api.s.write()->font.write()->fontCollection = std::move(collection); | ||
return true; | ||
} | ||
lhecker marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A temporary hack.