pygments icon indicating copy to clipboard operation
pygments copied to clipboard

RTF formatter does not set background color from style

Open Anteru opened this issue 6 years ago • 5 comments

(Original issue 1504 created by dansimau on 2019-04-09T08:32:41.068432+00:00)

When running pygmentize with RTF output and a theme with a background color, I expect the RTF output to have the background color matching the theme.

However, the RTF output does not have any background color set, e.g.:

$ cat /tmp/test
test() {
    echo "This is a test"
}
$ cat /tmp/test | pygmentize -f rtf -O style=solarizeddark -l shell |grep "This is a test"
    }{\cf8 echo}{\cf1  }{\cf10 "This is a test"}{\cf1 \par
$ 

What I am expecting is the RTF output to have the background color set using the \cbN control word.

Anteru avatar Aug 31 '19 17:08 Anteru

This issue is also present for the png formatter.

Using the png formatter: pygmentize -f png -O style=murphy -o foo.png foo.py foo

Using the html formatter: pygmentize -f html -O style=murphy,full -o foo.html foo.py Capture

hmedina avatar Jan 25 '20 03:01 hmedina

Looking a bit into this, this is by design. The background color used for tokens is the background color for the token, and solarized-dark does not specify any background color on a token (except for Error), and indeed, for error, a background color will get generated. Now, we can change the RTF output to have the background color set for everything, but that's a breaking change. @birkenfeld Any thoughts?

Anteru avatar Jan 26 '20 18:01 Anteru

(apologies for this being off-topic for the RTF formatter) Looking into the image formatter, I realized it had no functionality to support background colors. PR https://github.com/pygments/pygments/pull/1374 aims to fix that issue.

hmedina avatar Jan 29 '20 03:01 hmedina

Is this resolved now, can background color be added to word docs or rtfs?

shamitb avatar Oct 04 '22 12:10 shamitb

@shamitb It is already part of the RTF-formatter using the \cbN control word: {\cb25 '}{\cb25 foo}{\cb25 '}, but according to the RTF 1.9 specification Windows versions of Word have never supported the \cbN control word (and from my tests neither does WordPad Win10, LibreOffice or OpenOffice):

Note: Windows versions of Word have never supported \cbN, but it can be emulated by the control word sequence \chshdng0\chcbpatN.

All of WordPad, LibreOffice and OpenOffice does appear to support \chshdng0\chcbpatN, so we could use those control words instead (or \highlightN).

Screenshot from 2024-02-28 08-12-52

.rtf renamed to .txt for uploading issue1208.py.txt

You can test an implementation here:

git clone https://github.com/jonascj/pygments
cd pygments
git checkout issue1208-bgcolor
python -m venv venv
# activate venv (`source venv/bin/activate` or `venv\Scripts\activate.bat`)
pip install -e .
python -m  pygments -O style=murphy -v -o issue1208.py.rtf issue1208.py

The question is which combination of control words would render as expected in the most RTF-viewers - \chshdng0\chcbpatN, \highlightN or \cbN (the last one appears to be ignored by all major readers I've tested). The first two works in the same readers.

jonascj avatar Feb 28 '24 07:02 jonascj