Skip to content

Commit

Permalink
Add support for custom box drawing and powerline glyphs (#16729)
Browse files Browse the repository at this point in the history
This adds support for drawing our own box drawing, block element,
and basic Powerline (U+E0Bx) glyphs in AtlasEngine.

This PR consists of 4 parts:
* AtlasEngine was refactored to simplify `_drawGlyph` because I've
  wanted to do that for ~1 year now and never got a chance.
  Well, now I'm doing it and you all will review it muahahaha.
  The good news is that it removes a goto usage that even for my
  standards was rather dangerous. Now it's gone and the risk with it.
* AtlasEngine was further refactored to properly parse out text that
  we want to handle different from regular text. Previously, we only
  did that for soft fonts, but now we want to do that for a lot more,
  so a refactor was in order. The new code is still extremely
  disgusting, because I now stuff `wchar_t`s into an array that's
  intended for glyph indices, but that's the best way to make it fast
  and not blow up the complexity of the code even further.
* Finally this adds a huge LUT for all the aforementioned glyphs.
  The LUT has 4 "drawing instruction" entries per glyph which describe
  the shape (rectangle, circle, lines, etc.) and the start/end coord.
  With a lot of bit packing each entry is only 4 bytes large.
* Finally-finally a `builtinGlyphs` setting was added to the font
  object and it defaults to `true`.

Closes #5897

## Validation Steps Performed
* RenderingTests with soft fonts ✅
* All the aforementioned glyphs ✅
* ...with color ✅
* `customGlyphs` setting can be toggled on and off ✅
  • Loading branch information
lhecker authored Feb 23, 2024
1 parent 6b29ef5 commit a6a0e44
Show file tree
Hide file tree
Showing 32 changed files with 1,775 additions and 460 deletions.
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation

const auto lock = _terminal->LockForWriting();

_builtinGlyphs = _settings->EnableBuiltinGlyphs();
_cellWidth = CSSLengthPercentage::FromString(_settings->CellWidth().c_str());
_cellHeight = CSSLengthPercentage::FromString(_settings->CellHeight().c_str());
_runtimeOpacity = std::nullopt;
Expand Down Expand Up @@ -1038,6 +1039,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_actualFont = { fontFace, 0, fontWeight.Weight, _desiredFont.GetEngineSize(), CP_UTF8, false };
_actualFontFaceName = { fontFace };

_desiredFont.SetEnableBuiltinGlyphs(_builtinGlyphs);
_desiredFont.SetCellSize(_cellWidth, _cellHeight);

const auto before = _actualFont.GetSize();
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
FontInfoDesired _desiredFont;
FontInfo _actualFont;
winrt::hstring _actualFontFaceName;
bool _builtinGlyphs = true;
CSSLengthPercentage _cellWidth;
CSSLengthPercentage _cellHeight;

Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/IControlSettings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace Microsoft.Terminal.Control
String Padding { get; };
Windows.Foundation.Collections.IMap<String, UInt32> FontFeatures { get; };
Windows.Foundation.Collections.IMap<String, Single> FontAxes { get; };
Boolean EnableBuiltinGlyphs { get; };
String CellWidth { get; };
String CellHeight { get; };

Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsEditor/Appearances.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
OBSERVABLE_PROJECTED_SETTING(_appearance.SourceProfile().FontInfo(), FontFace);
OBSERVABLE_PROJECTED_SETTING(_appearance.SourceProfile().FontInfo(), FontSize);
OBSERVABLE_PROJECTED_SETTING(_appearance.SourceProfile().FontInfo(), FontWeight);
OBSERVABLE_PROJECTED_SETTING(_appearance.SourceProfile().FontInfo(), EnableBuiltinGlyphs);

OBSERVABLE_PROJECTED_SETTING(_appearance, RetroTerminalEffect);
OBSERVABLE_PROJECTED_SETTING(_appearance, CursorShape);
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsEditor/Appearances.idl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace Microsoft.Terminal.Settings.Editor
OBSERVABLE_PROJECTED_APPEARANCE_SETTING(Single, FontSize);
OBSERVABLE_PROJECTED_APPEARANCE_SETTING(Double, LineHeight);
OBSERVABLE_PROJECTED_APPEARANCE_SETTING(Windows.UI.Text.FontWeight, FontWeight);
OBSERVABLE_PROJECTED_APPEARANCE_SETTING(Boolean, EnableBuiltinGlyphs);

OBSERVABLE_PROJECTED_APPEARANCE_SETTING(String, DarkColorSchemeName);
OBSERVABLE_PROJECTED_APPEARANCE_SETTING(String, LightColorSchemeName);
Expand Down
9 changes: 9 additions & 0 deletions src/cascadia/TerminalSettingsEditor/Appearances.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@
</StackPanel>
</local:SettingContainer>

<!-- Builtin Glyphs -->
<local:SettingContainer x:Uid="Profile_EnableBuiltinGlyphs"
ClearSettingValue="{x:Bind Appearance.ClearEnableBuiltinGlyphs}"
HasSettingValue="{x:Bind Appearance.HasEnableBuiltinGlyphs, Mode=OneWay}"
SettingOverrideSource="{x:Bind Appearance.EnableBuiltinGlyphsOverrideSource, Mode=OneWay}">
<ToggleSwitch IsOn="{x:Bind Appearance.EnableBuiltinGlyphs, Mode=TwoWay}"
Style="{StaticResource ToggleSwitchInExpanderStyle}" />
</local:SettingContainer>

<!-- Retro Terminal Effect -->
<local:SettingContainer x:Uid="Profile_RetroTerminalEffect"
ClearSettingValue="{x:Bind Appearance.ClearRetroTerminalEffect}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,14 @@
<value>1.2</value>
<comment>"1.2" is a decimal number.</comment>
</data>
<data name="Profile_EnableBuiltinGlyphs.Header" xml:space="preserve">
<value>Builtin Glyphs</value>
<comment>The main label of a toggle. When enabled, certain characters (glyphs) are replaced with better looking ones.</comment>
</data>
<data name="Profile_EnableBuiltinGlyphs.HelpText" xml:space="preserve">
<value>When enabled, the terminal draws custom glyphs for block element and box drawing characters instead of using the font. This feature only works when GPU Acceleration is available.</value>
<comment>A longer description of the "Profile_EnableBuiltinGlyphs" toggle. "glyphs", "block element" and "box drawing characters" are technical terms from the Unicode specification.</comment>
</data>
<data name="Profile_FontWeightComboBox.[using:Windows.UI.Xaml.Automation]AutomationProperties.Name" xml:space="preserve">
<value>Font weight</value>
<comment>Name for a control to select the weight (i.e. bold, thin, etc.) of the text in the app.</comment>
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/FontConfig.idl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Microsoft.Terminal.Settings.Model
INHERITABLE_FONT_SETTING(Windows.UI.Text.FontWeight, FontWeight);
INHERITABLE_FONT_SETTING(Windows.Foundation.Collections.IMap<String COMMA UInt32>, FontFeatures);
INHERITABLE_FONT_SETTING(Windows.Foundation.Collections.IMap<String COMMA Single>, FontAxes);
INHERITABLE_FONT_SETTING(Boolean, EnableBuiltinGlyphs);
INHERITABLE_FONT_SETTING(String, CellWidth);
INHERITABLE_FONT_SETTING(String, CellHeight);
}
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/MTSMSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Author(s):
X(winrt::Windows::UI::Text::FontWeight, FontWeight, "weight", DEFAULT_FONT_WEIGHT) \
X(IFontAxesMap, FontAxes, "axes") \
X(IFontFeatureMap, FontFeatures, "features") \
X(bool, EnableBuiltinGlyphs, "builtinGlyphs", true) \
X(winrt::hstring, CellWidth, "cellWidth") \
X(winrt::hstring, CellHeight, "cellHeight")

Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/TerminalSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
_FontWeight = fontInfo.FontWeight();
_FontFeatures = fontInfo.FontFeatures();
_FontAxes = fontInfo.FontAxes();
_EnableBuiltinGlyphs = fontInfo.EnableBuiltinGlyphs();
_CellWidth = fontInfo.CellWidth();
_CellHeight = fontInfo.CellHeight();
_Padding = profile.Padding();
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/TerminalSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
INHERITABLE_SETTING(Model::TerminalSettings, winrt::Windows::UI::Text::FontWeight, FontWeight);
INHERITABLE_SETTING(Model::TerminalSettings, IFontAxesMap, FontAxes);
INHERITABLE_SETTING(Model::TerminalSettings, IFontFeatureMap, FontFeatures);
INHERITABLE_SETTING(Model::TerminalSettings, bool, EnableBuiltinGlyphs, true);
INHERITABLE_SETTING(Model::TerminalSettings, hstring, CellWidth);
INHERITABLE_SETTING(Model::TerminalSettings, hstring, CellHeight);

Expand Down
1 change: 1 addition & 0 deletions src/cascadia/inc/ControlProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
X(winrt::Windows::UI::Text::FontWeight, FontWeight) \
X(IFontFeatureMap, FontFeatures) \
X(IFontAxesMap, FontAxes) \
X(bool, EnableBuiltinGlyphs, true) \
X(winrt::hstring, CellWidth) \
X(winrt::hstring, CellHeight) \
X(winrt::Microsoft::Terminal::Control::IKeyBindings, KeyBindings, nullptr) \
Expand Down
4 changes: 4 additions & 0 deletions src/host/screenInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ SCREEN_INFORMATION::SCREEN_INFORMATION(
{
OutputMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
}
_desiredFont.SetEnableBuiltinGlyphs(gci.GetEnableBuiltinGlyphs());
}

// Routine Description:
Expand Down Expand Up @@ -539,7 +540,10 @@ void SCREEN_INFORMATION::RefreshFontWithRenderer()

void SCREEN_INFORMATION::UpdateFont(const FontInfo* const pfiNewFont)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();

FontInfoDesired fiDesiredFont(*pfiNewFont);
fiDesiredFont.SetEnableBuiltinGlyphs(gci.GetEnableBuiltinGlyphs());

GetDesiredFont() = fiDesiredFont;

Expand Down
5 changes: 5 additions & 0 deletions src/host/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,3 +776,8 @@ bool Settings::GetCopyColor() const noexcept
{
return _fCopyColor;
}

bool Settings::GetEnableBuiltinGlyphs() const noexcept
{
return _fEnableBuiltinGlyphs;
}
2 changes: 2 additions & 0 deletions src/host/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class Settings

bool GetUseDx() const noexcept;
bool GetCopyColor() const noexcept;
bool GetEnableBuiltinGlyphs() const noexcept;

private:
RenderSettings _renderSettings;
Expand Down Expand Up @@ -214,6 +215,7 @@ class Settings
DWORD _dwVirtTermLevel;
bool _fUseDx;
bool _fCopyColor;
bool _fEnableBuiltinGlyphs = true;

// this is used for the special STARTF_USESIZE mode.
bool _fUseWindowSizePixels;
Expand Down
36 changes: 19 additions & 17 deletions src/inc/til/flat_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#pragma once

#pragma warning(push)
#pragma warning(disable : 26446) // Prefer to use gsl::at() instead of unchecked subscript operator (bounds.4).
#pragma warning(disable : 26409) // Avoid calling new and delete explicitly, use std::make_unique<T> instead (r.11).
#pragma warning(disable : 26432) // If you define or delete any default operation in the type '...', define or delete them all (c.21).
#pragma warning(disable : 26446) // Prefer to use gsl::at() instead of unchecked subscript operator (bounds.4).

namespace til
{
Expand Down Expand Up @@ -36,7 +37,7 @@ namespace til
// * small and cheap T
// * >= 50% successful lookups
// * <= 50% load factor (LoadFactor >= 2, which is the minimum anyways)
template<typename T, size_t LoadFactor = 2, size_t GrowthExponent = 1>
template<typename T, typename Traits, size_t LoadFactor = 2, size_t GrowthExponent = 1>
struct linear_flat_set
{
static_assert(LoadFactor >= 2);
Expand Down Expand Up @@ -98,27 +99,28 @@ namespace til
return nullptr;
}

const auto hash = ::std::hash<T>{}(key) >> _shift;
const auto hash = Traits::hash(key) >> _shift;

for (auto i = hash;; ++i)
{
auto& slot = _map[i & _mask];
if (!slot)
if (!Traits::occupied(slot))
{
return nullptr;
}
if (slot == key) [[likely]]
if (Traits::equals(slot, key)) [[likely]]
{
return &slot;
}
}
}

// NOTE: It also does not initialize the returned slot.
// You must do that yourself in way that ensures that Traits::occupied(slot) now returns true.
// Use lookup() to check if the item already exists.
template<typename U>
std::pair<T&, bool> insert(U&& key)
std::pair<T*, bool> insert(U&& key)
{
// Putting this into the lookup path is a little pessimistic, but it
// allows us to default-construct this hashmap with a size of 0.
if (_load >= _capacity) [[unlikely]]
{
_bumpSize();
Expand All @@ -129,20 +131,20 @@ namespace til
// many times in literature that such a scheme performs the best on average.
// As such, we perform the divide here to get the topmost bits down.
// See flat_set_hash_integer.
const auto hash = ::std::hash<T>{}(key) >> _shift;
const auto hash = Traits::hash(key) >> _shift;

for (auto i = hash;; ++i)
{
auto& slot = _map[i & _mask];
if (!slot)
if (!Traits::occupied(slot))
{
slot = std::forward<U>(key);
_load += LoadFactor;
return { slot, true };
Traits::assign(slot, key);
return { &slot, true };
}
if (slot == key) [[likely]]
if (Traits::equals(slot, key)) [[likely]]
{
return { slot, false };
return { &slot, false };
}
}
}
Expand All @@ -166,17 +168,17 @@ namespace til
// This mirrors the insert() function, but without the lookup part.
for (auto& oldSlot : container())
{
if (!oldSlot)
if (!Traits::occupied(oldSlot))
{
continue;
}

const auto hash = ::std::hash<T>{}(oldSlot) >> newShift;
const auto hash = Traits::hash(oldSlot) >> newShift;

for (auto i = hash;; ++i)
{
auto& slot = newMap[i & newMask];
if (!slot)
if (!Traits::occupied(slot))
{
slot = std::move_if_noexcept(oldSlot);
break;
Expand Down
13 changes: 10 additions & 3 deletions src/inc/til/unicode.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@ namespace til
inline constexpr wchar_t UNICODE_REPLACEMENT = 0xFFFD;
}

static constexpr bool is_surrogate(const wchar_t wch) noexcept
constexpr bool is_surrogate(const auto wch) noexcept
{
return (wch & 0xF800) == 0xD800;
}

static constexpr bool is_leading_surrogate(const wchar_t wch) noexcept
constexpr bool is_leading_surrogate(const auto wch) noexcept
{
return (wch & 0xFC00) == 0xD800;
}

static constexpr bool is_trailing_surrogate(const wchar_t wch) noexcept
constexpr bool is_trailing_surrogate(const auto wch) noexcept
{
return (wch & 0xFC00) == 0xDC00;
}

constexpr char32_t combine_surrogates(const auto lead, const auto trail)
{
// Ah, I love these bracketed C-style casts. I use them in C all the time. Yep.
#pragma warning(suppress : 26493) // Don't use C-style casts (type.4).
return (char32_t{ lead } << 10) - 0x35FDC00 + char32_t{ trail };
}

// Verifies the beginning of the given UTF16 string and returns the first UTF16 sequence
// or U+FFFD otherwise. It's not really useful and at the time of writing only a
// single caller uses this. It's best to delete this if you read this comment.
Expand Down
5 changes: 4 additions & 1 deletion src/propslib/RegistrySerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ const RegistrySerialization::_RegPropertyMap RegistrySerialization::s_PropertyMa
{ _RegPropertyType::Boolean, CONSOLE_REGISTRY_INTERCEPTCOPYPASTE, SET_FIELD_AND_SIZE(_fInterceptCopyPaste) },
{ _RegPropertyType::Boolean, CONSOLE_REGISTRY_TERMINALSCROLLING, SET_FIELD_AND_SIZE(_TerminalScrolling) },
{ _RegPropertyType::Boolean, CONSOLE_REGISTRY_USEDX, SET_FIELD_AND_SIZE(_fUseDx) },
{ _RegPropertyType::Boolean, CONSOLE_REGISTRY_COPYCOLOR, SET_FIELD_AND_SIZE(_fCopyColor) }
{ _RegPropertyType::Boolean, CONSOLE_REGISTRY_COPYCOLOR, SET_FIELD_AND_SIZE(_fCopyColor) },
#if TIL_FEATURE_CONHOSTATLASENGINE_ENABLED
{ _RegPropertyType::Boolean, L"EnableBuiltinGlyphs", SET_FIELD_AND_SIZE(_fEnableBuiltinGlyphs) },
#endif

// Special cases that are handled manually in Registry::LoadFromRegistry:
// - CONSOLE_REGISTRY_WINDOWPOS
Expand Down
6 changes: 4 additions & 2 deletions src/renderer/atlas/AtlasEngine.api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ CATCH_RETURN()
/* fontStyle */ DWRITE_FONT_STYLE_NORMAL,
/* fontStretch */ DWRITE_FONT_STRETCH_NORMAL,
/* fontSize */ _api.s->font->fontSize,
/* localeName */ L"",
/* textFormat */ textFormat.put()));
/* localeName */ _p.userLocaleName.c_str(),
/* textFormat */ textFormat.addressof()));

wil::com_ptr<IDWriteTextLayout> textLayout;
RETURN_IF_FAILED(_p.dwriteFactory->CreateTextLayout(glyph.data(), gsl::narrow_cast<uint32_t>(glyph.size()), textFormat.get(), FLT_MAX, FLT_MAX, textLayout.addressof()));
Expand Down Expand Up @@ -774,5 +774,7 @@ void AtlasEngine::_resolveFontMetrics(const wchar_t* requestedFaceName, const Fo
fontMetrics->doubleUnderline[0] = { doubleUnderlinePosTopU16, thinLineWidthU16 };
fontMetrics->doubleUnderline[1] = { doubleUnderlinePosBottomU16, thinLineWidthU16 };
fontMetrics->overline = { 0, underlineWidthU16 };

fontMetrics->builtinGlyphs = fontInfoDesired.GetEnableBuiltinGlyphs();
}
}
Loading

0 comments on commit a6a0e44

Please sign in to comment.