mirror of
https://github.com/microsoft/terminal.git
synced 2026-04-23 06:31:29 +00:00
This PR reimplements the VT rendering engines to do a better job of preserving the original color types when propagating attributes over ConPTY. For the 16-color renderers it provides better support for default colors and improves the efficiency of the color narrowing conversions. It also fixes problems with the ordering of character renditions that could result in attributes being dropped. Originally the base renderer would calculate the RGB color values and legacy/extended attributes up front, passing that data on to the active engine's `UpdateDrawingBrushes` method. With this new implementation, the renderer now just passes through the original `TextAttribute` along with an `IRenderData` interface, and leaves it to the engines to extract the information they need. The GDI and DirectX engines now have to lookup the RGB colors themselves (via simple `IRenderData` calls), but have no need for the other attributes. The VT engines extract the information that they need from the `TextAttribute`, instead of having to reverse engineer it from `COLORREF`s. The process for the 256-color Xterm engine starts with a check for default colors. If both foreground and background are default, it outputs a SGR 0 reset, and clears the `_lastTextAttribute` completely to make sure any reset state is reapplied. With that out the way, the foreground and background are updated (if changed) in one of 4 ways. They can either be a default value (SGR 39 and 49), a 16-color index (using ANSI or AIX sequences), a 256-color index, or a 24-bit RGB value (both using SGR 38 and 48 sequences). Then once the colors are accounted for, there is a separate step that handles the character rendition attributes (bold, italics, underline, etc.) This step must come _after_ the color sequences, in case a SGR reset is required, which would otherwise have cleared any character rendition attributes if it came last (which is what happened in the original implementation). The process for the 16-color engines is a little different. The target client in this case (Windows telnet) is incapable of setting default colors individually, so we need to output an SGR 0 reset if _either_ color has changed to default. With that out the way, we use the `TextColor::GetLegacyIndex` method to obtain an approximate 16-color index for each color, and apply the bold attribute by brightening the foreground index (setting bit 8) if the color type permits that. However, since Windows telnet only supports the 8 basic ANSI colors, the best we can do for bright colors is to output an SGR 1 attribute to get a bright foreground. There is nothing we can do about a bright background, so after that we just have to drop the high bit from the colors. If the resulting index values have changed from what they were before, we then output ANSI 8-color SGR sequences to update them. As with the 256-color engine, there is also a final step to handle the character rendition attributes. But in this case, the only supported attributes are underline and reversed video. Since the VT engines no longer depend on the active color table and default color values, there was quite a lot of code that could now be removed. This included the `IDefaultColorProvider` interface and implementations, the `Find(Nearest)TableIndex` functions, and also the associated HLS conversion and difference calculations. VALIDATION Other than simple API parameter changes, the majority of updates required in the unit tests were to correct assumptions about the way the colors should be rendered, which were the source of the narrowing bugs this PR was trying to fix. Like passing white on black to the `UpdateDrawingBrushes` API, and expecting it to output the default `SGR 0` sequence, or passing an RGB color and expecting an indexed SGR sequence. In addition to that, I've added some VT renderer tests to make sure the rendition attributes (bold, underline, etc) are correctly retained when a default color update causes an `SGR 0` sequence to be generated (the source of bug #3076). And I've extended the VT renderer color tests (both 256-color and 16-color) to make sure we're covering all of the different color types (default, RGB, and both forms of indexed colors). I've also tried to manually verify that all of the test cases in the linked bug reports (and their associated duplicates) are now fixed when this PR is applied. Closes #2661 Closes #3076 Closes #3717 Closes #5384 Closes #5864 This is only a partial fix for #293, but I suspect the remaining cases are unfixable.
312 lines
9.3 KiB
C++
312 lines
9.3 KiB
C++
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT license.
|
|
|
|
#include "precomp.h"
|
|
#include "TextAttribute.hpp"
|
|
#include "../../inc/conattrs.hpp"
|
|
|
|
BYTE TextAttribute::s_legacyDefaultForeground = 7;
|
|
BYTE TextAttribute::s_legacyDefaultBackground = 0;
|
|
|
|
// Routine Description:
|
|
// - Sets the legacy attributes which map to and from the default colors.
|
|
// Parameters:
|
|
// - defaultAttributes: the attribute values to be used for default colors.
|
|
// Return value:
|
|
// - None
|
|
void TextAttribute::SetLegacyDefaultAttributes(const WORD defaultAttributes) noexcept
|
|
{
|
|
s_legacyDefaultForeground = defaultAttributes & FG_ATTRS;
|
|
s_legacyDefaultBackground = (defaultAttributes & BG_ATTRS) >> 4;
|
|
}
|
|
|
|
// Routine Description:
|
|
// - Returns a WORD with legacy-style attributes for this textattribute.
|
|
// Parameters:
|
|
// - None
|
|
// Return value:
|
|
// - a WORD with legacy-style attributes for this textattribute.
|
|
WORD TextAttribute::GetLegacyAttributes() const noexcept
|
|
{
|
|
const BYTE fgIndex = _foreground.GetLegacyIndex(s_legacyDefaultForeground);
|
|
const BYTE bgIndex = _background.GetLegacyIndex(s_legacyDefaultBackground);
|
|
const WORD metaAttrs = _wAttrLegacy & META_ATTRS;
|
|
const bool brighten = IsBold() && _foreground.CanBeBrightened();
|
|
return fgIndex | (bgIndex << 4) | metaAttrs | (brighten ? FOREGROUND_INTENSITY : 0);
|
|
}
|
|
|
|
bool TextAttribute::IsLegacy() const noexcept
|
|
{
|
|
return _foreground.IsLegacy() && _background.IsLegacy();
|
|
}
|
|
|
|
// Arguments:
|
|
// - None
|
|
// Return Value:
|
|
// - color that should be displayed as the foreground color
|
|
COLORREF TextAttribute::CalculateRgbForeground(std::basic_string_view<COLORREF> colorTable,
|
|
COLORREF defaultFgColor,
|
|
COLORREF defaultBgColor) const noexcept
|
|
{
|
|
return IsReverseVideo() ? _GetRgbBackground(colorTable, defaultBgColor) : _GetRgbForeground(colorTable, defaultFgColor);
|
|
}
|
|
|
|
// Routine Description:
|
|
// - Calculates rgb background color based off of current color table and active modification attributes
|
|
// Arguments:
|
|
// - None
|
|
// Return Value:
|
|
// - color that should be displayed as the background color
|
|
COLORREF TextAttribute::CalculateRgbBackground(std::basic_string_view<COLORREF> colorTable,
|
|
COLORREF defaultFgColor,
|
|
COLORREF defaultBgColor) const noexcept
|
|
{
|
|
return IsReverseVideo() ? _GetRgbForeground(colorTable, defaultFgColor) : _GetRgbBackground(colorTable, defaultBgColor);
|
|
}
|
|
|
|
// Routine Description:
|
|
// - gets rgb foreground color, possibly based off of current color table. Does not take active modification
|
|
// attributes into account
|
|
// Arguments:
|
|
// - None
|
|
// Return Value:
|
|
// - color that is stored as the foreground color
|
|
COLORREF TextAttribute::_GetRgbForeground(std::basic_string_view<COLORREF> colorTable,
|
|
COLORREF defaultColor) const noexcept
|
|
{
|
|
return _foreground.GetColor(colorTable, defaultColor, IsBold());
|
|
}
|
|
|
|
// Routine Description:
|
|
// - gets rgb background color, possibly based off of current color table. Does not take active modification
|
|
// attributes into account
|
|
// Arguments:
|
|
// - None
|
|
// Return Value:
|
|
// - color that is stored as the background color
|
|
COLORREF TextAttribute::_GetRgbBackground(std::basic_string_view<COLORREF> colorTable,
|
|
COLORREF defaultColor) const noexcept
|
|
{
|
|
return _background.GetColor(colorTable, defaultColor, false);
|
|
}
|
|
|
|
TextColor TextAttribute::GetForeground() const noexcept
|
|
{
|
|
return _foreground;
|
|
}
|
|
|
|
TextColor TextAttribute::GetBackground() const noexcept
|
|
{
|
|
return _background;
|
|
}
|
|
|
|
void TextAttribute::SetForeground(const TextColor foreground) noexcept
|
|
{
|
|
_foreground = foreground;
|
|
}
|
|
|
|
void TextAttribute::SetBackground(const TextColor background) noexcept
|
|
{
|
|
_background = background;
|
|
}
|
|
|
|
void TextAttribute::SetForeground(const COLORREF rgbForeground) noexcept
|
|
{
|
|
_foreground = TextColor(rgbForeground);
|
|
}
|
|
|
|
void TextAttribute::SetBackground(const COLORREF rgbBackground) noexcept
|
|
{
|
|
_background = TextColor(rgbBackground);
|
|
}
|
|
|
|
void TextAttribute::SetIndexedForeground(const BYTE fgIndex) noexcept
|
|
{
|
|
_foreground = TextColor(fgIndex, false);
|
|
}
|
|
|
|
void TextAttribute::SetIndexedBackground(const BYTE bgIndex) noexcept
|
|
{
|
|
_background = TextColor(bgIndex, false);
|
|
}
|
|
|
|
void TextAttribute::SetIndexedForeground256(const BYTE fgIndex) noexcept
|
|
{
|
|
_foreground = TextColor(fgIndex, true);
|
|
}
|
|
|
|
void TextAttribute::SetIndexedBackground256(const BYTE bgIndex) noexcept
|
|
{
|
|
_background = TextColor(bgIndex, true);
|
|
}
|
|
|
|
void TextAttribute::SetColor(const COLORREF rgbColor, const bool fIsForeground) noexcept
|
|
{
|
|
if (fIsForeground)
|
|
{
|
|
SetForeground(rgbColor);
|
|
}
|
|
else
|
|
{
|
|
SetBackground(rgbColor);
|
|
}
|
|
}
|
|
|
|
bool TextAttribute::IsLeadingByte() const noexcept
|
|
{
|
|
return WI_IsFlagSet(_wAttrLegacy, COMMON_LVB_LEADING_BYTE);
|
|
}
|
|
|
|
bool TextAttribute::IsTrailingByte() const noexcept
|
|
{
|
|
return WI_IsFlagSet(_wAttrLegacy, COMMON_LVB_LEADING_BYTE);
|
|
}
|
|
|
|
bool TextAttribute::IsTopHorizontalDisplayed() const noexcept
|
|
{
|
|
return WI_IsFlagSet(_wAttrLegacy, COMMON_LVB_GRID_HORIZONTAL);
|
|
}
|
|
|
|
bool TextAttribute::IsBottomHorizontalDisplayed() const noexcept
|
|
{
|
|
return WI_IsFlagSet(_wAttrLegacy, COMMON_LVB_UNDERSCORE);
|
|
}
|
|
|
|
bool TextAttribute::IsLeftVerticalDisplayed() const noexcept
|
|
{
|
|
return WI_IsFlagSet(_wAttrLegacy, COMMON_LVB_GRID_LVERTICAL);
|
|
}
|
|
|
|
bool TextAttribute::IsRightVerticalDisplayed() const noexcept
|
|
{
|
|
return WI_IsFlagSet(_wAttrLegacy, COMMON_LVB_GRID_RVERTICAL);
|
|
}
|
|
|
|
void TextAttribute::SetLeftVerticalDisplayed(const bool isDisplayed) noexcept
|
|
{
|
|
WI_UpdateFlag(_wAttrLegacy, COMMON_LVB_GRID_LVERTICAL, isDisplayed);
|
|
}
|
|
|
|
void TextAttribute::SetRightVerticalDisplayed(const bool isDisplayed) noexcept
|
|
{
|
|
WI_UpdateFlag(_wAttrLegacy, COMMON_LVB_GRID_RVERTICAL, isDisplayed);
|
|
}
|
|
|
|
bool TextAttribute::IsBold() const noexcept
|
|
{
|
|
return WI_IsFlagSet(_extendedAttrs, ExtendedAttributes::Bold);
|
|
}
|
|
|
|
bool TextAttribute::IsItalic() const noexcept
|
|
{
|
|
return WI_IsFlagSet(_extendedAttrs, ExtendedAttributes::Italics);
|
|
}
|
|
|
|
bool TextAttribute::IsBlinking() const noexcept
|
|
{
|
|
return WI_IsFlagSet(_extendedAttrs, ExtendedAttributes::Blinking);
|
|
}
|
|
|
|
bool TextAttribute::IsInvisible() const noexcept
|
|
{
|
|
return WI_IsFlagSet(_extendedAttrs, ExtendedAttributes::Invisible);
|
|
}
|
|
|
|
bool TextAttribute::IsCrossedOut() const noexcept
|
|
{
|
|
return WI_IsFlagSet(_extendedAttrs, ExtendedAttributes::CrossedOut);
|
|
}
|
|
|
|
bool TextAttribute::IsUnderlined() const noexcept
|
|
{
|
|
// TODO:GH#2915 Treat underline separately from LVB_UNDERSCORE
|
|
return WI_IsFlagSet(_wAttrLegacy, COMMON_LVB_UNDERSCORE);
|
|
}
|
|
|
|
bool TextAttribute::IsReverseVideo() const noexcept
|
|
{
|
|
return WI_IsFlagSet(_wAttrLegacy, COMMON_LVB_REVERSE_VIDEO);
|
|
}
|
|
|
|
void TextAttribute::SetBold(bool isBold) noexcept
|
|
{
|
|
WI_UpdateFlag(_extendedAttrs, ExtendedAttributes::Bold, isBold);
|
|
}
|
|
|
|
void TextAttribute::SetItalics(bool isItalic) noexcept
|
|
{
|
|
WI_UpdateFlag(_extendedAttrs, ExtendedAttributes::Italics, isItalic);
|
|
}
|
|
|
|
void TextAttribute::SetBlinking(bool isBlinking) noexcept
|
|
{
|
|
WI_UpdateFlag(_extendedAttrs, ExtendedAttributes::Blinking, isBlinking);
|
|
}
|
|
|
|
void TextAttribute::SetInvisible(bool isInvisible) noexcept
|
|
{
|
|
WI_UpdateFlag(_extendedAttrs, ExtendedAttributes::Invisible, isInvisible);
|
|
}
|
|
|
|
void TextAttribute::SetCrossedOut(bool isCrossedOut) noexcept
|
|
{
|
|
WI_UpdateFlag(_extendedAttrs, ExtendedAttributes::CrossedOut, isCrossedOut);
|
|
}
|
|
|
|
void TextAttribute::SetUnderline(bool isUnderlined) noexcept
|
|
{
|
|
// TODO:GH#2915 Treat underline separately from LVB_UNDERSCORE
|
|
WI_UpdateFlag(_wAttrLegacy, COMMON_LVB_UNDERSCORE, isUnderlined);
|
|
}
|
|
|
|
void TextAttribute::SetReverseVideo(bool isReversed) noexcept
|
|
{
|
|
WI_UpdateFlag(_wAttrLegacy, COMMON_LVB_REVERSE_VIDEO, isReversed);
|
|
}
|
|
|
|
ExtendedAttributes TextAttribute::GetExtendedAttributes() const noexcept
|
|
{
|
|
return _extendedAttrs;
|
|
}
|
|
|
|
// Routine Description:
|
|
// - swaps foreground and background color
|
|
void TextAttribute::Invert() noexcept
|
|
{
|
|
WI_ToggleFlag(_wAttrLegacy, COMMON_LVB_REVERSE_VIDEO);
|
|
}
|
|
|
|
void TextAttribute::SetDefaultForeground() noexcept
|
|
{
|
|
_foreground = TextColor();
|
|
}
|
|
|
|
void TextAttribute::SetDefaultBackground() noexcept
|
|
{
|
|
_background = TextColor();
|
|
}
|
|
|
|
// Method Description:
|
|
// - Returns true if this attribute indicates its background is the "default"
|
|
// background. Its _rgbBackground will contain the actual value of the
|
|
// default background. If the default colors are ever changed, this method
|
|
// should be used to identify attributes with the default bg value, and
|
|
// update them accordingly.
|
|
// Arguments:
|
|
// - <none>
|
|
// Return Value:
|
|
// - true iff this attribute indicates it's the "default" background color.
|
|
bool TextAttribute::BackgroundIsDefault() const noexcept
|
|
{
|
|
return _background.IsDefault();
|
|
}
|
|
|
|
// Routine Description:
|
|
// - Resets the meta and extended attributes, which is what the VT standard
|
|
// requires for most erasing and filling operations.
|
|
void TextAttribute::SetStandardErase() noexcept
|
|
{
|
|
_extendedAttrs = ExtendedAttributes::Normal;
|
|
_wAttrLegacy = 0;
|
|
}
|