RTF 行尾转换的意外结果 [英] Unexpected result of RTF line ending conversion

查看:36
本文介绍了RTF 行尾转换的意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 txtLogRichTextBox 控件:

If txtLog is a RichTextBox control:

Dim text = "hi" & vbCrLf
Debug.WriteLine("t:" & text.Length)        ' --> 4, as expected

txtLog.Text = text
Debug.WriteLine("tL:" & txtLog.TextLength) ' --> 3. muh?! :(

查看了 RTF 规范,段落的结尾标记为 \par,既不是 CR 也不是 LF.这是有道理的,因为 RTF 是标记语言;就像在 HTML 中一样,行尾本身没有什么意义.

Having looked at the RTF spec, the end of a paragraph is notated as \par, which is neither CR nor LF. This makes sense since RTF is markup language; like in HTML, line endings have little meaning on their own.

所以大概,在写入 RichTextBox 时,我的行尾被编码到 \par 中.然后,在提取时,\par 被转换回实际的行结尾以供使用.

So presumably, on writing into the RichTextBox, my line ending is being encoded into \par. And then, on extraction, the \par is being translated back to a real line ending for use.

原来这行结尾是vbLf.

为什么,既然 Microsoft 几乎一致地使用 CRLF 作为行尾,RichTextBox 会将 \par 翻译成 vbLf 而不是 vbCrLf?

Why, since Microsoft near-consistently employ CRLF for line endings, would RichTextBox translate \par to vbLf instead of vbCrLf?

推荐答案

以这种方式实现 RichTextBox 的直接原因是因为 RTF 规范 表示回车(自身)或换行符自身等价于 \par.

The immediate reason RichTextBox is implemented this way is because the RTF specification denotes that a carriage return (by itself) or a linefeed by itself is equivalent to \par.

...回车(字符值 13)或换行(字符值 10)将被视为 \par 控件...

. . . A carriage return (character value 13) or linefeed (character value 10) will be treated as a \par control . . .

至于为什么微软会制定这样的规范,我不确定.但是,我推测这与 RTF 的第一个版本 是1980 年代为 Mac 版 Microsoft Office 开发.我猜他们开发了这个标准规则,以便它在 Mac 上运行良好,或者作为跨平台格式运行良好.如果是这种情况,那么 Microsoft 可能会非常犹豫在未来几年(90 年代、00 年代等)修改规范以匹配标准的 Windows 行尾(因为一般而言,Microsoft 有尝试向后支持的历史)像这样的事情尽可能兼容).

As to why Microsoft would make the specification like this, I don't know for sure. However I would speculate that it had to do with the fact that the first version of RTF was developed for the Mac version of Microsoft Office in the 1980s. I would guess that they developed this par rule so that it worked well on a Mac or worked well as a cross platform format in general. If this is the case, then Microsoft would probably be very hesitant to revise the spec in the coming years ('90s, '00s, etc.) to match standard Windows line endings (since in general Microsoft has a history of trying to support backwards compatibility as much as possible for things like this).

这篇关于RTF 行尾转换的意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆