如何更改 RTF 文档中特定字符的背景颜色?

发布于 2024-09-14 18:04:23 字数 514 浏览 6 评论 0原文

我正在尝试从 Ruby 程序输出 RTF(富文本格式) - 我更喜欢直接发出 RTF,而不使用 RTF gem,因为我正在做非常简单的事情。

我想突出显示 DNA 序列比对中的特定字符,从文档看来我可以使用 \highlightN ... \highlight0 或 \cbN ... \cb1

问题是我无法让 \cb 工作Word:Mac 2008 或 Mac TextEdit(\cf 工作正常,所以我知道这不是颜色表问题)

\highlight 确实可以工作,但似乎只能使用两种可能的颜色(黑色和红色),并且 \highlight 不使用自定义颜色桌子。

通过在 Word 中创建带有字符底纹的简单文档并另存为 RTF,我可以看到冗长得可笑的 RTF 代码块,它们大概可以实现我想要的功能,但它是如此难以理解,以至于我只见树木不见森林。

部分问题很可能是 Mac Word 没有正确实现 RTF。我手头没有 Windows 版本的 Word。

有人知道给文本块着色的正确方法吗?

谢谢 - 抢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝海 2024-09-21 18:04:23

RTF Pocket Guide 中有一条注释,指出 MS Word 不实现 \cb 命令。它说 MS Word 使用 \chshdng0\chcbpatN (其中“N”是与 \cb 一起使用的颜色编号)。本书建议使用类似以下内容来与实现 \cbN 和/或 \chshdng0\chcbpatN 的程序兼容:{\chshdng0\chcbpat5\cb5 text}。

注:我手头的这本书是 2003 年出版的,所以可能有点过时了。

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.

明媚如初 2024-09-21 18:04:23

支持 RTF 的应用程序似乎最普遍支持的 RTF 命令序列是:

\chshdng10000\chcbpatN\chcfpatN\cbN

这些命令:

  1. 将阴影设置为 100%
  2. 设置图案前景色和背景色 将字符背景设置为颜色表中的颜色(我们实际上并未指定阴影图案)
  3. 将字符背景设置为颜色表中的颜色

Word 是正确渲染背景颜色的最困难的应用程序:

尽管最新的(1.9.1 ) RTF 规范 表示,Word 2013 无法解析 \highlightN来自 \colortbl 的颜色。相反,\highlightN 映射到预定义的颜色列表。看起来这些颜色来自 1.5 版本的 RTF 规范

关于 \cb,1.9.1 规范在颜色表部分的末尾包含了这个有用的指针:

注意: Windows 版本的 Word 从未支持 \cbN,但可以通过控制字序列 来模拟\chshdng0\chcbpatN

几乎是一个有用的建议,除非您阅读 \chshdngN 的文档:

字符底纹。 N 参数是一个值,表示文本的阴影程度(以百分之一为单位)。

因此,0 并不是一个非常有用的值; 100 / 0.01 给出了我们在上面的序列中使用的 10000

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.

十二 2024-09-21 18:04:23

使用写字板创建 RTF 文档,而不是 Word。写字板创建更简单的文档,即接近人类可读的文档。

每次需要在 WinForms 应用程序中显示格式化文本时,我都会使用写字板,并且需要 RichTextBox 控件可以处理分配给其 Rtf 参数。

Use WordPad to create RTF documents, not Word. WordPad creates much simpler documents, i.e. approaching human-readable.

I use WordPad every time I need to display formatted text in a WinForms application, and need something that the RichTextBox control can handle being assigned to its Rtf parameter.

久而酒知 2024-09-21 18:04:23

@Jacob Carpenter 的回答发表评论:https://stackoverflow.com/a/42960126/4993453

在 Apache OpenOffice 4.1.15 中 \chshdng0\chcbpatN 原来是控制字和参数的组合,产生预期的背景颜色。将 0 更改为较大的数字会使颜色变深(10000 产生黑色):

来自 Apache OpenOffice 的屏幕截图,显示颜色 #E0E0FF 被 \chsdngN 着色,N=0,100,1000,5000,10000

Windows 10 22H2 上的写字板似乎忽略 \chshdngN无论参数 N 的值如何,都会产生相同的颜色。LibreOffice Writer 6.1.6 也是如此。

Comment on @Jacob Carpenter's answer: https://stackoverflow.com/a/42960126/4993453

In Apache OpenOffice 4.1.15 \chshdng0\chcbpatN turns out to be the combination of control words and parameters which produce the expected background color. Changing 0 to a larger number makes the color darker (10000 yielding black):

Screenshot from Apache OpenOffice showing color #E0E0FF being shaded with \chsdngN with N=0,100,1000,5000,10000

WordPad on Windows 10 22H2 seems to ignore \chshdngN producing the same color regardless the value of the parameter N. Same goes for LibreOffice Writer 6.1.6.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文