pygments / pygments

Pygments is a generic syntax highlighter written in Python
http://pygments.org/
BSD 2-Clause "Simplified" License
1.83k stars 676 forks source link

RTF formatter does not set background color from style #1208

Open Anteru opened 5 years ago

Anteru commented 5 years ago

(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.

hmedina commented 4 years ago

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

Anteru commented 4 years ago

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?

hmedina commented 4 years ago

(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.

shamitb commented 2 years ago

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

jonascj commented 8 months ago

@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.