From ed87689c048e7d41ff28684d171f77ba9cb579f2 Mon Sep 17 00:00:00 2001 From: mcpiroman <38111589+mcpiroman@users.noreply.github.com> Date: Mon, 16 Sep 2019 19:32:04 +0200 Subject: [PATCH] Set Proper Background Color in HTML Copy (#2762) * Set actual background color in HTML copy * const Co-Authored-By: Mike Griese * format --- src/buffer/out/textBuffer.cpp | 8 +++----- src/buffer/out/textBuffer.hpp | 1 + src/cascadia/TerminalControl/TermControl.cpp | 6 +++++- src/interactivity/win32/Clipboard.cpp | 3 ++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/buffer/out/textBuffer.cpp b/src/buffer/out/textBuffer.cpp index a64406f074..eb18a815a7 100644 --- a/src/buffer/out/textBuffer.cpp +++ b/src/buffer/out/textBuffer.cpp @@ -1056,12 +1056,13 @@ const TextBuffer::TextAndColor TextBuffer::GetTextForClipboard(const bool lineSe // - Generates a CF_HTML compliant structure based on the passed in text and color data // Arguments: // - rows - the text and color data we will format & encapsulate +// - backgroundColor - default background color for characters, also used in padding // - fontHeightPoints - the unscaled font height // - fontFaceName - the name of the font used // - htmlTitle - value used in title tag of html header. Used to name the application // Return Value: // - string containing the generated HTML -std::string TextBuffer::GenHTML(const TextAndColor& rows, const int fontHeightPoints, const PCWCHAR fontFaceName, const std::string& htmlTitle) +std::string TextBuffer::GenHTML(const TextAndColor& rows, const int fontHeightPoints, const PCWCHAR fontFaceName, const COLORREF backgroundColor, const std::string& htmlTitle) { try { @@ -1086,11 +1087,8 @@ std::string TextBuffer::GenHTML(const TextAndColor& rows, const int fontHeightPo htmlBuilder << "display:inline-block;"; htmlBuilder << "white-space:pre;"; - // fixme: this is only walkaround for filling background after last char of row. - // It is based on first char of first row, not the actual char at correct position. htmlBuilder << "background-color:"; - const COLORREF globalBgColor = rows.BkAttr.at(0).at(0); - htmlBuilder << Utils::ColorToHexString(globalBgColor); + htmlBuilder << Utils::ColorToHexString(backgroundColor); htmlBuilder << ";"; htmlBuilder << "font-family:"; diff --git a/src/buffer/out/textBuffer.hpp b/src/buffer/out/textBuffer.hpp index beb477fbbd..6f461eeefe 100644 --- a/src/buffer/out/textBuffer.hpp +++ b/src/buffer/out/textBuffer.hpp @@ -148,6 +148,7 @@ public: static std::string GenHTML(const TextAndColor& rows, const int fontHeightPoints, const PCWCHAR fontFaceName, + const COLORREF backgroundColor, const std::string& htmlTitle); private: diff --git a/src/cascadia/TerminalControl/TermControl.cpp b/src/cascadia/TerminalControl/TermControl.cpp index 3aade82053..ec3c4cf287 100644 --- a/src/cascadia/TerminalControl/TermControl.cpp +++ b/src/cascadia/TerminalControl/TermControl.cpp @@ -1435,7 +1435,11 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation } // convert text to HTML format - const auto htmlData = TextBuffer::GenHTML(bufferData, _actualFont.GetUnscaledSize().Y, _actualFont.GetFaceName(), "Windows Terminal"); + const auto htmlData = TextBuffer::GenHTML(bufferData, + _actualFont.GetUnscaledSize().Y, + _actualFont.GetFaceName(), + _settings.DefaultBackground(), + "Windows Terminal"); if (!_terminal->IsCopyOnSelectActive()) { diff --git a/src/interactivity/win32/Clipboard.cpp b/src/interactivity/win32/Clipboard.cpp index 54a48b4048..408d0d1aaf 100644 --- a/src/interactivity/win32/Clipboard.cpp +++ b/src/interactivity/win32/Clipboard.cpp @@ -277,7 +277,8 @@ void Clipboard::CopyTextToSystemClipboard(const TextBuffer::TextAndColor& rows, { const auto& fontData = ServiceLocator::LocateGlobals().getConsoleInformation().GetActiveOutputBuffer().GetCurrentFont(); int const iFontHeightPoints = fontData.GetUnscaledSize().Y * 72 / ServiceLocator::LocateGlobals().dpi; - std::string HTMLToPlaceOnClip = TextBuffer::GenHTML(rows, iFontHeightPoints, fontData.GetFaceName(), "Windows Console Host"); + const COLORREF bgColor = ServiceLocator::LocateGlobals().getConsoleInformation().GetDefaultBackground(); + std::string HTMLToPlaceOnClip = TextBuffer::GenHTML(rows, iFontHeightPoints, fontData.GetFaceName(), bgColor, "Windows Console Host"); const size_t cbNeededHTML = HTMLToPlaceOnClip.size(); if (cbNeededHTML) {