Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the background color of specific characters in a RTF document?

Tags:

ruby

rtf

I'm trying to output RTF (Rich Text Format) from a Ruby program - and I'd prefer to just emit RTF directly without using the RTF gem as I'm doing pretty simple stuff.

I would like to highlight specific characters in a DNA sequence alignment and from the docs it seems that I can either use \highlightN ... \highlight0 or \cbN ... \cb1

The problem is that I cannot get \cb to work in either Word:Mac 2008 or Mac TextEdit (\cf works fine so I know it's not a color table issue)

\highlight does work but seemingly only with two of the possible colors (black and red) and \highlight does not use the custom color table.

By creating simple docs in Word with character shading and saving as RTF I can see blocks of ridiculously verbose RTF code that presumably does what I want, but it is so impenetrable that I'm not seeing the wood for the trees.

Part of the problem may well be that Mac Word is just not implementing RTF properly. I don't have a Windows version of Word handy.

Anyone know the right way to shade blocks of text?

Thanks --Rob

like image 683
Rob Jones Avatar asked Dec 28 '22 08:12

Rob Jones


2 Answers

There is a note in the RTF Pocket Guide that says MS Word does not implement the \cb command. It says MS Word uses \chshdng0\chcbpatN (where "N" is the color number that you would use with \cb). The book recommends using something like the following for compatibility with programs that implement \cbN and/or \chshdng0\chcbpatN: {\chshdng0\chcbpat5\cb5 text}.

Note: The copy of the book I have was published in 2003, so it might be a bit out-of-date.

like image 114
joast Avatar answered May 08 '23 07:05

joast


The sequence of RTF commands that seems to be most universally supported by RTF-capable applications is:

\chshdng10000\chcbpatN\chcfpatN\cbN

These commands:

  1. set the shading to 100 percent
  2. set the pattern foreground and background colors to the color from the color table (we're not actually specifying a shading pattern)
  3. set the character background to the color from the color table

Word was the most difficult application to properly render background colors in:

Despite what the latest (1.9.1) RTF spec says, Word 2013 does not resolve \highlightN colors from the \colortbl. Instead, \highlightN maps to a predefined list of colors. It looks like those colors come from the 1.5 version of the RTF spec.

Regarding \cb, the 1.9.1 spec contains this helpful pointer at the end of the section on Color Table:

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

This is almost a useful suggestion, except that if you read the documentation for \chshdngN:

Character shading. The N argument is a value representing the shading of the text in hundredths of a percent.

So, 0 turns out to not be a very useful value; 100 / 0.01 gives us the 10000 we used in the sequence above.

like image 26
Jacob Carpenter Avatar answered May 08 '23 07:05

Jacob Carpenter