Skip to content

Support rendering of underline style and color #16097

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

Merged
merged 24 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d2bea4c
initial commit for underline style and color
tusharsnx Oct 3, 2023
d341402
add new underline styles in UIA
tusharsnx Oct 9, 2023
a042401
draw curly line from the shader
tusharsnx Oct 12, 2023
e16055c
revert D2D antialias mode
tusharsnx Oct 12, 2023
1df5894
fix gridlines being drawn with underline color
tusharsnx Oct 14, 2023
0bdeb1a
add dotted underline to BackendD2D and Dx renderers
tusharsnx Oct 14, 2023
c179041
fix ScrollTest's MockScrollRenderEngine wasn't updated to receive und…
tusharsnx Oct 14, 2023
8684de8
make sine wave start with a crest
tusharsnx Oct 16, 2023
dc986c1
fix strikethrough being drawn with underline color
tusharsnx Oct 16, 2023
bea07a5
add support for underline styles in gdi renderer
tusharsnx Oct 24, 2023
4f13a3c
add spellings
tusharsnx Oct 24, 2023
1f3e535
fix signed/unsigned mismatch
tusharsnx Oct 25, 2023
2babe7b
make small improvements to Atlas curlyline rendering
tusharsnx Oct 25, 2023
e31d0fb
AtlasEngine: use cellBottomGap as the peak height for curly line
tusharsnx Oct 27, 2023
63ae6ce
GDIRenderer: use cellBottomGap as the peak height for curly line
tusharsnx Oct 27, 2023
b5574d0
initialize _curlyLineDrawPeakHeight
tusharsnx Oct 27, 2023
b8d3caa
send line rendition scale using texcoord
tusharsnx Oct 28, 2023
a2c4a36
underlines are mutually exclusive
tusharsnx Oct 28, 2023
6e4c65a
revert underline thickness scaling
tusharsnx Oct 28, 2023
b0be8b6
move curlyline peak height constants into the function
tusharsnx Nov 2, 2023
1ba1782
fix curlyline equation that was making a negative peak at the start
tusharsnx Nov 2, 2023
4f7ba10
BackendD2D: use `else if`
tusharsnx Nov 2, 2023
c7a8a55
shrink QuadInstance::ShadingType and add renditionScale member
tusharsnx Nov 3, 2023
3923fde
replace SelectObject with wil::SelectObject
tusharsnx Nov 4, 2023
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
13 changes: 10 additions & 3 deletions src/renderer/atlas/AtlasEngine.api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,12 +686,19 @@ void AtlasEngine::_resolveFontMetrics(const wchar_t* requestedFaceName, const Fo
adjustedWidth = std::max(1.0f, adjustedWidth);
adjustedHeight = std::max(1.0f, adjustedHeight);

// TODO: This helps in testing the underlines at different underline
// widths, particularly curly line, so they don't break if/when we get
// support for user customizable underline width. Remove it before merging.
// I'm using 1.5f as a hardcoded value below. In the future, we would
// get it from the user/config.
const auto underlineWidthScale = std::clamp(1.0f, 1.5f, 2.0f);

const auto baseline = std::roundf(ascent + (lineGap + adjustedHeight - advanceHeight) / 2.0f);
const auto underlinePos = std::roundf(baseline + underlinePosition);
const auto underlineWidth = std::max(1.0f, std::roundf(underlineThickness));
const auto underlineWidth = std::max(1.0f, std::roundf(underlineThickness * underlineWidthScale));
const auto strikethroughPos = std::roundf(baseline + strikethroughPosition);
const auto strikethroughWidth = std::max(1.0f, std::roundf(strikethroughThickness));
const auto thinLineWidth = std::max(1.0f, std::roundf(underlineThickness / 2.0f));
const auto strikethroughWidth = std::max(1.0f, std::roundf(strikethroughThickness * underlineWidthScale));
const auto thinLineWidth = std::max(1.0f, std::roundf(underlineThickness * underlineWidthScale / 2.0f));

// For double underlines we loosely follow what Word does:
// 1. The lines are half the width of an underline (= thinLineWidth)
Expand Down
28 changes: 24 additions & 4 deletions src/renderer/atlas/BackendD3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,16 @@ void BackendD3D::_updateFontDependents(const RenderingPayload& p)
{
const auto& font = *p.s->font;

// For curly line, we'll make room for the trough and crest (wave's peaks).
// The baseline for curly-line is kept at the baseline of singly underline.
const auto strokeWidthHalf = font.underline.height / 2.0f;
const auto curlyUnderlinePeakHeight = _curlyLineHeight * font.fontSize;
const auto curlyUnderlinePos = font.underline.position - curlyUnderlinePeakHeight;
const auto curlyUnderlineWidth = 2.0f * (curlyUnderlinePeakHeight + strokeWidthHalf);
const auto curlyUnderlinePosU16 = gsl::narrow_cast<u16>(lrintf(curlyUnderlinePos));
const auto curlyUnderlineWidthU16 = gsl::narrow_cast<u16>(lrintf(curlyUnderlineWidth));
_curlyUnderline = { curlyUnderlinePosU16, curlyUnderlineWidthU16 };

DWrite_GetRenderParams(p.dwriteFactory.get(), &_gamma, &_cleartypeEnhancedContrast, &_grayscaleEnhancedContrast, _textRenderingParams.put());
// Clearing the atlas requires BeginDraw(), which is expensive. Defer this until we need Direct2D anyways.
_fontChangedResetGlyphAtlas = true;
Expand Down Expand Up @@ -539,6 +549,8 @@ void BackendD3D::_recreateConstBuffer(const RenderingPayload& p) const
DWrite_GetGammaRatios(_gamma, data.gammaRatios);
data.enhancedContrast = p.s->font->antialiasingMode == AntialiasingMode::ClearType ? _cleartypeEnhancedContrast : _grayscaleEnhancedContrast;
data.underlineWidth = p.s->font->underline.height;
data.curlyLineHeight = p.s->font->fontSize * _curlyLineHeight;
data.underlineCellOffset = p.s->font->underline.position;
p.deviceContext->UpdateSubresource(_psConstantBuffer.get(), 0, nullptr, &data, 0, 0);
}
}
Expand Down Expand Up @@ -772,9 +784,7 @@ void BackendD3D::_resizeGlyphAtlas(const RenderingPayload& p, const u16 u, const
_d2dRenderTarget.try_query_to(_d2dRenderTarget4.addressof());

_d2dRenderTarget->SetUnitMode(D2D1_UNIT_MODE_PIXELS);
// We don't really use D2D for anything except DWrite, but it
// can't hurt to ensure that everything it does is pixel aligned.
_d2dRenderTarget->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
_d2dRenderTarget->SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
// Ensure that D2D uses the exact same gamma as our shader uses.
_d2dRenderTarget->SetTextRenderingParams(_textRenderingParams.get());

Expand Down Expand Up @@ -1644,6 +1654,8 @@ void BackendD3D::_drawGridlines(const RenderingPayload& p, u16 y)

const auto cellSize = p.s->font->cellSize;
const auto dottedLineType = horizontalShift ? ShadingType::DottedLineWide : ShadingType::DottedLine;
const auto dashedLineType = horizontalShift ? ShadingType::DashedLineWide : ShadingType::DashedLine;
const auto curlyLineType = horizontalShift ? ShadingType::CurlyLineWide : ShadingType::CurlyLine;

const auto rowTop = static_cast<i16>(cellSize.y * y);
const auto rowBottom = static_cast<i16>(rowTop + cellSize.y);
Expand Down Expand Up @@ -1724,10 +1736,18 @@ void BackendD3D::_drawGridlines(const RenderingPayload& p, u16 y)
{
appendHorizontalLine(r, p.s->font->underline, ShadingType::SolidLine);
}
if (r.lines.test(GridLines::HyperlinkUnderline))
if (r.lines.any(GridLines::DottedUnderline, GridLines::HyperlinkUnderline))
{
appendHorizontalLine(r, p.s->font->underline, dottedLineType);
}
if (r.lines.test(GridLines::DashedUnderline))
{
appendHorizontalLine(r, p.s->font->underline, dashedLineType);
}
if (r.lines.test(GridLines::CurlyUnderline))
{
appendHorizontalLine(r, _curlyUnderline, curlyLineType);
}
if (r.lines.test(GridLines::DoubleUnderline))
{
for (const auto pos : p.s->font->doubleUnderline)
Expand Down
15 changes: 12 additions & 3 deletions src/renderer/atlas/BackendD3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ namespace Microsoft::Console::Render::Atlas
alignas(sizeof(f32x4)) f32 gammaRatios[4]{};
alignas(sizeof(f32)) f32 enhancedContrast = 0;
alignas(sizeof(f32)) f32 underlineWidth = 0;
alignas(sizeof(f32)) f32 curlyLineHeight = 0;
alignas(sizeof(f32)) f32 underlineCellOffset = 0;
#pragma warning(suppress : 4324) // 'PSConstBuffer': structure was padded due to alignment specifier
};

Expand All @@ -67,11 +69,15 @@ namespace Microsoft::Console::Render::Atlas
TextPassthrough = 3,
DottedLine = 4,
DottedLineWide = 5,
DashedLine = 6,
DashedLineWide = 7,
CurlyLine = 8,
CurlyLineWide = 9,
// All items starting here will be drawing as a solid RGBA color
SolidLine = 6,
SolidLine = 10,

Cursor = 7,
Selection = 8,
Cursor = 11,
Selection = 12,

TextDrawingFirst = TextGrayscale,
TextDrawingLast = SolidLine,
Expand Down Expand Up @@ -291,6 +297,9 @@ namespace Microsoft::Console::Render::Atlas
// The bounding rect of _cursorRects in pixels.
til::rect _cursorPosition;

const f32 _curlyLineHeight = 0.1f; // in `em` units.
FontDecorationPosition _curlyUnderline;

bool _requiresContinuousRedraw = false;

#if ATLAS_DEBUG_SHOW_DIRTY
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/atlas/shader_common.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#define SHADING_TYPE_TEXT_PASSTHROUGH 3
#define SHADING_TYPE_DOTTED_LINE 4
#define SHADING_TYPE_DOTTED_LINE_WIDE 5
#define SHADING_TYPE_DASHED_LINE 6
#define SHADING_TYPE_DASHED_LINE_WIDE 7
#define SHADING_TYPE_CURLY_LINE 8
#define SHADING_TYPE_CURLY_LINE_WIDE 9
// clang-format on

struct VSData
Expand Down
56 changes: 56 additions & 0 deletions src/renderer/atlas/shader_ps.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ cbuffer ConstBuffer : register(b0)
float4 gammaRatios;
float enhancedContrast;
float underlineWidth;
float curlyLineHeight;
float underlineCellOffset;
}

Texture2D<float4> background : register(t0);
Expand Down Expand Up @@ -85,6 +87,60 @@ Output main(PSData data) : SV_Target
weights = color.aaaa;
break;
}
case SHADING_TYPE_DASHED_LINE:
{
const bool on = frac(data.position.x / backgroundCellSize.x) < 0.5f;
color = on * premultiplyColor(data.color);
weights = color.aaaa;
break;
}
case SHADING_TYPE_DASHED_LINE_WIDE:
{
const bool on = frac(data.position.x / (2.0f * backgroundCellSize.x)) < 0.5f;
color = on * premultiplyColor(data.color);
weights = color.aaaa;
break;
}
case SHADING_TYPE_CURLY_LINE:
{
const float strokeWidthHalf = underlineWidth / 2.0f;
const int cellIdxY = data.position.y / backgroundCellSize.y;
const float cellPosY = cellIdxY * backgroundCellSize.y;
const float centerY = cellPosY + underlineCellOffset + strokeWidthHalf;
const float Pi = radians(180);
const float freq = 2.0f * Pi / backgroundCellSize.x;
const float amp = curlyLineHeight - 1.0f; // -1.0f avoids clipping at the peak

const float s = sin(data.position.x * freq);
const float d = abs(centerY + s * amp - data.position.y);
const float a = 1 - saturate(d - strokeWidthHalf);
color = a * premultiplyColor(data.color);
weights = color.aaaa;
break;
}
case SHADING_TYPE_CURLY_LINE_WIDE:
{
float strokeWidthHalf = underlineWidth / 2.0f;
const int cellIdxY = data.position.y / backgroundCellSize.y;
const float cellPosY = cellIdxY * backgroundCellSize.y;
float centerY = cellPosY + underlineCellOffset + strokeWidthHalf;
const float Pi = radians(180);
float freq = 2.0f * Pi / backgroundCellSize.x;
float amp = curlyLineHeight - 1.0f; // -1.0f avoids clipping at the peak

// In 'Wide' case, we need to draw the same wave on an area twice as big.
strokeWidthHalf *= 2;
centerY -= curlyLineHeight + strokeWidthHalf;
freq /= 2;
amp *= 2;

const float s = sin(data.position.x * freq);
const float d = abs(centerY + s * amp - data.position.y);
const float a = 1 - saturate(d - strokeWidthHalf);
color = a * premultiplyColor(data.color);
weights = color.aaaa;
break;
}
default:
{
color = premultiplyColor(data.color);
Expand Down
41 changes: 41 additions & 0 deletions src/renderer/base/RenderSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,47 @@ std::pair<COLORREF, COLORREF> RenderSettings::GetAttributeColorsWithAlpha(const
return { fg, bg };
}

// Routine Description:
// - Calculates the RGB underline color of a given text attribute, using the
// current color table configuration and active render settings.
// - Returns the current foreground color when the underline color isn't set.
// Arguments:
// - attr - The TextAttribute to retrieve the underline color from.
// Return Value:
// - The color value of the attribute's underline.
COLORREF RenderSettings::GetAttributeUnderlineColor(const TextAttribute& attr) const noexcept
{
const auto [fg, bg] = GetAttributeColors(attr);
const auto ulTextColor = attr.GetUnderlineColor();
if (ulTextColor.IsDefault())
{
return fg;
}

const auto defaultUlIndex = GetColorAliasIndex(ColorAlias::DefaultForeground);
auto ul = ulTextColor.GetColor(_colorTable, defaultUlIndex, true);
if (attr.IsInvisible())
{
ul = bg;
}

// We intentionally aren't _only_ checking for attr.IsInvisible here, because we also want to
// catch the cases where the ul was intentionally set to be the same as the bg. In either case,
// don't adjust the underline color.
if constexpr (Feature_AdjustIndistinguishableText::IsEnabled())
{
if (
ul != bg &&
(_renderMode.test(Mode::AlwaysDistinguishableColors) ||
(_renderMode.test(Mode::IndexedDistinguishableColors) && ulTextColor.IsDefaultOrLegacy() && attr.GetBackground().IsDefaultOrLegacy())))
{
ul = ColorFix::GetPerceivableColor(ul, bg, 0.5f * 0.5f);
}
}

return ul;
}

// Routine Description:
// - Increments the position in the blink cycle, toggling the blink rendition
// state on every second call, potentially triggering a redraw of the given
Expand Down
14 changes: 11 additions & 3 deletions src/renderer/base/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -955,13 +955,21 @@ GridLineSet Renderer::s_GetGridlines(const TextAttribute& textAttribute) noexcep
{
case UnderlineStyle::NoUnderline:
break;
case UnderlineStyle::SinglyUnderlined:
lines.set(GridLines::Underline);
break;
case UnderlineStyle::DoublyUnderlined:
lines.set(GridLines::DoubleUnderline);
break;
case UnderlineStyle::SinglyUnderlined:
case UnderlineStyle::CurlyUnderlined:
lines.set(GridLines::CurlyUnderline);
break;
case UnderlineStyle::DottedUnderlined:
lines.set(GridLines::DottedUnderline);
break;
case UnderlineStyle::DashedUnderlined:
lines.set(GridLines::DashedUnderline);
break;
default:
lines.set(GridLines::Underline);
break;
Expand Down Expand Up @@ -1002,8 +1010,8 @@ void Renderer::_PaintBufferOutputGridLineHelper(_In_ IRenderEngine* const pEngin
// Return early if there are no lines to paint.
if (lines.any())
{
// Get the current foreground color to render the lines.
const auto rgb = _renderSettings.GetAttributeColors(textAttribute).first;
// Get the current underline color to render the lines.
const auto rgb = _renderSettings.GetAttributeUnderlineColor(textAttribute);
// Draw the lines
LOG_IF_FAILED(pEngine->PaintBufferGridLines(lines, rgb, cchLine, coordTarget));
}
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/inc/IRenderEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ namespace Microsoft::Console::Render
Right,
Underline,
DoubleUnderline,
CurlyUnderline,
DottedUnderline,
DashedUnderline,
Strikethrough,
HyperlinkUnderline
};
Expand Down
1 change: 1 addition & 0 deletions src/renderer/inc/RenderSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace Microsoft::Console::Render
size_t GetColorAliasIndex(const ColorAlias alias) const noexcept;
std::pair<COLORREF, COLORREF> GetAttributeColors(const TextAttribute& attr) const noexcept;
std::pair<COLORREF, COLORREF> GetAttributeColorsWithAlpha(const TextAttribute& attr) const noexcept;
COLORREF GetAttributeUnderlineColor(const TextAttribute& attr) const noexcept;
void ToggleBlinkRendition(class Renderer& renderer) noexcept;

private:
Expand Down
30 changes: 22 additions & 8 deletions src/types/UiaTextRangeBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,16 +405,22 @@ std::optional<bool> UiaTextRangeBase::_verifyAttr(TEXTATTRIBUTEID attributeId, V
THROW_HR_IF(E_INVALIDARG, val.vt != VT_I4);

// The underline style is stored as a TextDecorationLineStyle.
// However, The text buffer doesn't have that many different styles for being underlined.
// Instead, we only have single and double underlined.
// However, The text buffer doesn't have all the different styles for being underlined.
// Instead, we only use a subset of them.
switch (val.lVal)
{
case TextDecorationLineStyle_None:
return !attr.IsUnderlined();
case TextDecorationLineStyle_Single:
return attr.GetUnderlineStyle() == UnderlineStyle::SinglyUnderlined;
case TextDecorationLineStyle_Double:
return attr.GetUnderlineStyle() == UnderlineStyle::DoublyUnderlined;
case TextDecorationLineStyle_Single: // singly underlined and extended styles are treated the same
return attr.IsUnderlined() && attr.GetUnderlineStyle() != UnderlineStyle::DoublyUnderlined;
case TextDecorationLineStyle_Wavy:
return attr.GetUnderlineStyle() == UnderlineStyle::CurlyUnderlined;
case TextDecorationLineStyle_Dot:
return attr.GetUnderlineStyle() == UnderlineStyle::DottedUnderlined;
case TextDecorationLineStyle_Dash:
return attr.GetUnderlineStyle() == UnderlineStyle::DashedUnderlined;
default:
return std::nullopt;
}
Expand Down Expand Up @@ -697,18 +703,26 @@ bool UiaTextRangeBase::_initializeAttrQuery(TEXTATTRIBUTEID attributeId, VARIANT
const auto style = attr.GetUnderlineStyle();
switch (style)
{
case UnderlineStyle::NoUnderline:
pRetVal->lVal = TextDecorationLineStyle_None;
return true;
case UnderlineStyle::SinglyUnderlined:
pRetVal->lVal = TextDecorationLineStyle_Single;
return true;
case UnderlineStyle::DoublyUnderlined:
pRetVal->lVal = TextDecorationLineStyle_Double;
return true;
case UnderlineStyle::NoUnderline:
pRetVal->lVal = TextDecorationLineStyle_None;
case UnderlineStyle::CurlyUnderlined:
pRetVal->lVal = TextDecorationLineStyle_Wavy;
return true;
case UnderlineStyle::DottedUnderlined:
pRetVal->lVal = TextDecorationLineStyle_Dot;
return true;
case UnderlineStyle::DashedUnderlined:
pRetVal->lVal = TextDecorationLineStyle_Dash;
return true;
// Out of range styles are treated as singly underlined.
default:
// TODO: Handle other underline styles once they're supported in the graphic renderer.
// For now, extended styles are treated (and rendered) as single underline.
pRetVal->lVal = TextDecorationLineStyle_Single;
return true;
}
Expand Down