Skip to content
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

Add support for hyperlinks to AtlasEngine #12225

Merged
1 commit merged into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion src/renderer/atlas/AtlasEngine.api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ CATCH_RETURN()

void AtlasEngine::UpdateHyperlinkHoveredId(const uint16_t hoveredId) noexcept
{
_api.hyperlinkHoveredId = hoveredId;
}

#pragma endregion
Expand Down Expand Up @@ -580,7 +581,7 @@ void AtlasEngine::_resolveFontMetrics(const FontInfoDesired& fontInfoDesired, Fo
const auto strikethroughOffsetInPx = static_cast<double>(-metrics.strikethroughPosition) * designUnitsPerPx;
const auto strikethroughThicknessInPx = static_cast<double>(metrics.strikethroughThickness) * designUnitsPerPx;
const auto lineThickness = gsl::narrow<u16>(std::round(std::min(underlineThicknessInPx, strikethroughThicknessInPx)));
const auto underlinePos = gsl::narrow<u16>(std::round(baseline + underlineOffsetInPx - lineThickness / 2.0));
const auto underlinePos = gsl::narrow<u16>(std::ceil(baseline + underlineOffsetInPx - lineThickness / 2.0));
Copy link
Member Author

@lhecker lhecker Jan 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change prevents the underline from overlapping characters, in case it rounded down before this change.

const auto strikethroughPos = gsl::narrow<u16>(std::round(baseline + strikethroughOffsetInPx - lineThickness / 2.0));

auto fontName = wil::make_process_heap_string(requestedFaceName);
Expand Down
29 changes: 25 additions & 4 deletions src/renderer/atlas/AtlasEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,13 @@ try
const auto x = gsl::narrow_cast<u16>(clamp<int>(coord.X, 0, _api.cellCount.x));
const auto y = gsl::narrow_cast<u16>(clamp<int>(coord.Y, 0, _api.cellCount.y));

if (_api.currentRow != y)
if (_api.lastPaintBufferLineCoord.y != y)
{
_flushBufferLine();
}

_api.currentRow = y;
_api.lastPaintBufferLineCoord = { x, y };
_api.bufferLineWasHyperlinked = false;

// Due to the current IRenderEngine interface (that wasn't refactored yet) we need to assemble
// the current buffer line first as the remaining function operates on whole lines of text.
Expand Down Expand Up @@ -503,9 +504,21 @@ try
CATCH_RETURN()

[[nodiscard]] HRESULT AtlasEngine::PaintBufferGridLines(const GridLineSet lines, const COLORREF color, const size_t cchLine, const COORD coordTarget) noexcept
try
{
if (!_api.bufferLineWasHyperlinked && lines.test(GridLines::Underline) && WI_IsFlagClear(_api.flags, CellFlags::Underline))
{
_api.bufferLineWasHyperlinked = true;

WI_UpdateFlagsInMask(_api.flags, CellFlags::Underline | CellFlags::UnderlineDotted | CellFlags::UnderlineDouble, CellFlags::Underline);

const BufferLineMetadata metadata{ _api.currentColor, _api.flags };
const size_t x = _api.lastPaintBufferLineCoord.x;
std::fill_n(_api.bufferLineMetadata.data() + x, _api.bufferLineMetadata.size() - x, metadata);
}
return S_OK;
}
CATCH_RETURN()

[[nodiscard]] HRESULT AtlasEngine::PaintSelection(SMALL_RECT rect) noexcept
try
Expand Down Expand Up @@ -569,16 +582,24 @@ try

if (!isSettingDefaultBrushes)
{
const auto hyperlinkId = textAttributes.GetHyperlinkId();

auto flags = CellFlags::None;
WI_SetFlagIf(flags, CellFlags::BorderLeft, textAttributes.IsLeftVerticalDisplayed());
WI_SetFlagIf(flags, CellFlags::BorderTop, textAttributes.IsTopHorizontalDisplayed());
WI_SetFlagIf(flags, CellFlags::BorderRight, textAttributes.IsRightVerticalDisplayed());
WI_SetFlagIf(flags, CellFlags::BorderBottom, textAttributes.IsBottomHorizontalDisplayed());
WI_SetFlagIf(flags, CellFlags::Underline, textAttributes.IsUnderlined());
WI_SetFlagIf(flags, CellFlags::UnderlineDotted, textAttributes.IsHyperlink());
WI_SetFlagIf(flags, CellFlags::UnderlineDotted, hyperlinkId != 0);
WI_SetFlagIf(flags, CellFlags::UnderlineDouble, textAttributes.IsDoublyUnderlined());
WI_SetFlagIf(flags, CellFlags::Strikethrough, textAttributes.IsCrossedOut());

if (_api.hyperlinkHoveredId && _api.hyperlinkHoveredId == hyperlinkId)
{
WI_SetFlag(flags, CellFlags::Underline);
WI_ClearAllFlags(flags, CellFlags::UnderlineDotted | CellFlags::UnderlineDouble);
}

const u32x2 newColors{ gsl::narrow_cast<u32>(fg | 0xff000000), gsl::narrow_cast<u32>(bg | _api.backgroundOpaqueMixin) };
const AtlasKeyAttributes attributes{ 0, textAttributes.IsBold(), textAttributes.IsItalic(), 0 };

Expand Down Expand Up @@ -1445,7 +1466,7 @@ void AtlasEngine::_emplaceGlyph(IDWriteFontFace* fontFace, float scale, size_t b

const auto valueData = value.data();
const auto coords = &valueData->coords[0];
const auto data = _getCell(x1, _api.currentRow);
const auto data = _getCell(x1, _api.lastPaintBufferLineCoord.y);

for (u32 i = 0; i < cellCount; ++i)
{
Expand Down
5 changes: 4 additions & 1 deletion src/renderer/atlas/AtlasEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -734,10 +734,13 @@ namespace Microsoft::Console::Render
u32 backgroundOpaqueMixin = 0xff000000; // changes are flagged as ApiInvalidations::Device
u32x2 currentColor;
AtlasKeyAttributes attributes{};
u16 currentRow = 0;
u16x2 lastPaintBufferLineCoord;
CellFlags flags = CellFlags::None;
// SetSelectionBackground()
u32 selectionColor = 0x7fffffff;
// UpdateHyperlinkHoveredId()
u16 hyperlinkHoveredId = 0;
bool bufferLineWasHyperlinked = false;

// dirtyRect is a computed value based on invalidatedRows.
til::rect dirtyRect;
Expand Down