Skip to content

Commit

Permalink
Use MSWord compatible RTF sequence for background text color (#16035)
Browse files Browse the repository at this point in the history
The `GenRTF(...)` was using `\highlight` control word for sending
background text color in the RTF format during a copy command. This
doesn't work correctly, since many applications (E.g. MSWord) don't
support full RGB with `\highlight`, and instead uses an approximation of
what is received. For example, `rgb(197, 15, 31)` becomes `rgb(255, 0,
255)`. Also, the standard way of using background colors is `\cbN`
control word, which isn't supported as per the [RTF Spec 1.9.1]
in Word.

But it briefly mentioned a workaround at Pg. 23, which seems to work on
all the RTF editors I tested.

The PR makes the changes to use `\chshdng0\chcbpatN` for the background
coloring.

Also did some refactoring to make the implementation concise.

## Validation Steps Performed

Verified that the background is correctly copied on below editors:
- MSWord
- WordPad
- LibreOffice
- Outlook

[RTF Spec 1.9.1]: https://msopenspecs.azureedge.net/files/Archive_References/[MSFT-RTF].pdf

(cherry picked from commit 310814b)
Service-Card-Id: 91349195
Service-Version: 1.19
  • Loading branch information
tusharsnx authored and DHowett committed Dec 15, 2023
1 parent bcc01d9 commit 306cb40
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .github/actions/spelling/expect/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@ changelist
chaof
charinfo
CHARSETINFO
chcbpat
chh
chk
chshdng
CHT
Cic
cielab
Expand Down
85 changes: 36 additions & 49 deletions src/buffer/out/textBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2277,6 +2277,7 @@ std::string TextBuffer::GenHTML(const TextAndColor& rows,
// Routine Description:
// - Generates an RTF document based on the passed in text and color data
// RTF 1.5 Spec: https://www.biblioscape.com/rtf15_spec.htm
// RTF 1.9.1 Spec: https://msopenspecs.azureedge.net/files/Archive_References/[MSFT-RTF].pdf
// Arguments:
// - rows - the text and color data we will format & encapsulate
// - backgroundColor - default background color for characters, also used in padding
Expand All @@ -2296,10 +2297,18 @@ std::string TextBuffer::GenRTF(const TextAndColor& rows, const int fontHeightPoi

// Standard RTF header.
// This is similar to the header generated by WordPad.
// \ansi - specifies that the ANSI char set is used in the current doc
// \ansicpg1252 - represents the ANSI code page which is used to perform the Unicode to ANSI conversion when writing RTF text
// \deff0 - specifies that the default font for the document is the one at index 0 in the font table
// \nouicompat - ?
// \ansi:
// Specifies that the ANSI char set is used in the current doc.
// \ansicpg1252:
// Represents the ANSI code page which is used to perform
// the Unicode to ANSI conversion when writing RTF text.
// \deff0:
// Specifies that the default font for the document is the one
// at index 0 in the font table.
// \nouicompat:
// Some features are blocked by default to maintain compatibility
// with older programs (Eg. Word 97-2003). `nouicompat` disables this
// behavior, and unblocks these features. See: Spec 1.9.1, Pg. 51.
rtfBuilder << "\\rtf1\\ansi\\ansicpg1252\\deff0\\nouicompat";

// font table
Expand All @@ -2308,17 +2317,25 @@ std::string TextBuffer::GenRTF(const TextAndColor& rows, const int fontHeightPoi
// map to keep track of colors:
// keys are colors represented by COLORREF
// values are indices of the corresponding colors in the color table
std::unordered_map<COLORREF, int> colorMap;
auto nextColorIndex = 1; // leave 0 for the default color and start from 1.
std::unordered_map<COLORREF, size_t> colorMap;

// RTF color table
std::ostringstream colorTableBuilder;
colorTableBuilder << "{\\colortbl ;";
colorTableBuilder << "\\red" << static_cast<int>(GetRValue(backgroundColor))
<< "\\green" << static_cast<int>(GetGValue(backgroundColor))
<< "\\blue" << static_cast<int>(GetBValue(backgroundColor))
<< ";";
colorMap[backgroundColor] = nextColorIndex++;

const auto getColorTableIndex = [&](const COLORREF color) -> size_t {
// Exclude the 0 index for the default color, and start with 1.

const auto [it, inserted] = colorMap.emplace(color, colorMap.size() + 1);
if (inserted)
{
colorTableBuilder << "\\red" << static_cast<int>(GetRValue(color))
<< "\\green" << static_cast<int>(GetGValue(color))
<< "\\blue" << static_cast<int>(GetBValue(color))
<< ";";
}
return it->second;
};

// content
std::ostringstream contentBuilder;
Expand All @@ -2328,7 +2345,12 @@ std::string TextBuffer::GenRTF(const TextAndColor& rows, const int fontHeightPoi
// \fs specifies font size in half-points i.e. \fs20 results in a font size
// of 10 pts. That's why, font size is multiplied by 2 here.
contentBuilder << "\\pard\\slmult1\\f0\\fs" << std::to_string(2 * fontHeightPoints)
<< "\\highlight1"
// Set the background color for the page. But, the
// standard way (\cbN) to do this isn't supported in Word.
// However, the following control words sequence works
// in Word (and other RTF editors also) for applying the
// text background color. See: Spec 1.9.1, Pg. 23.
<< "\\chshdng0\\chcbpat" << getColorTableIndex(backgroundColor)
<< " ";

std::optional<COLORREF> fgColor = std::nullopt;
Expand Down Expand Up @@ -2378,43 +2400,8 @@ std::string TextBuffer::GenRTF(const TextAndColor& rows, const int fontHeightPoi
if (colorChanged)
{
writeAccumulatedChars(false);

auto bkColorIndex = 0;
if (colorMap.find(bkColor.value()) != colorMap.end())
{
// color already exists in the map, just retrieve the index
bkColorIndex = colorMap[bkColor.value()];
}
else
{
// color not present in the map, so add it
colorTableBuilder << "\\red" << static_cast<int>(GetRValue(bkColor.value()))
<< "\\green" << static_cast<int>(GetGValue(bkColor.value()))
<< "\\blue" << static_cast<int>(GetBValue(bkColor.value()))
<< ";";
colorMap[bkColor.value()] = nextColorIndex;
bkColorIndex = nextColorIndex++;
}

auto fgColorIndex = 0;
if (colorMap.find(fgColor.value()) != colorMap.end())
{
// color already exists in the map, just retrieve the index
fgColorIndex = colorMap[fgColor.value()];
}
else
{
// color not present in the map, so add it
colorTableBuilder << "\\red" << static_cast<int>(GetRValue(fgColor.value()))
<< "\\green" << static_cast<int>(GetGValue(fgColor.value()))
<< "\\blue" << static_cast<int>(GetBValue(fgColor.value()))
<< ";";
colorMap[fgColor.value()] = nextColorIndex;
fgColorIndex = nextColorIndex++;
}

contentBuilder << "\\highlight" << bkColorIndex
<< "\\cf" << fgColorIndex
contentBuilder << "\\chshdng0\\chcbpat" << getColorTableIndex(bkColor.value())
<< "\\cf" << getColorTableIndex(fgColor.value())
<< " ";
}

Expand Down

0 comments on commit 306cb40

Please sign in to comment.