有效地存储和显示富文本

时间:2021-06-17 14:32:44

I need to store significant amounts of rich text in a SQL database, retrieve it and display it.

我需要在SQL数据库中存储大量的富文本,检索它并显示它。

One font throughout is OK but I need different font sizes/bold/colors.

一个字体是好的但我需要不同的字体大小/粗体/颜色。

For now I am using a RichTextBox (WPF) to display it, and XamlWriter.Save/XamlReader.Parse to serialize it to strings to store in the DB. It works well but the RichTextBox is so HUGELY SLOW at displaying the text that it's basically unusable.

现在我使用RichTextBox(WPF)来显示它,并使用XamlWriter.Save/XamlReader.Parse将其序列化为存储在DB中的字符串。它运行良好,但RichTextBox在显示文本时非常慢,它基本上无法使用。

Is there a quick way to do this with acceptable performance?

是否有一种快速的方法来实现可接受的性能?

I'm considering doing it with GlyphRun objects, drawing each character as a bitmap and computing all the alignment requirements to fit the destination image etc... But reinventing the wheel on simple colored/sizable text seems really strange in 2011.

我正在考虑使用GlyphRun对象,将每个角色绘制为位图并计算所有对齐要求以适应目标图像等...但是在简单的彩色/可调整大小的文本上重新发明*在2011年看起来很奇怪。

EDIT: Thanks for the answers, didn't see them until now, sorry.

编辑:谢谢你的答案,直到现在才看到他们,对不起。

Text is entered by the user from RichTextBoxes as well, basically I just save the resulting string XamlWriter.Save(richTextBox.Document) in the database. Other fields (double/int etc) are also entered by the user from TextBoxes.

用户也从RichTextBoxes输入文本,基本上我只是将结果字符串XamlWriter.Save(richTextBox.Document)保存在数据库中。用户也从TextBoxes输入其他字段(double / int等)。

As the user queries the database, pages of read-only rich text with colors and formatting is generated from scratch using the fields in the database, including the saved rich text fields above: these are converted from FlowDocuments to Spans and some replacement is done on them (InlineUIContainers which host a class derived from UIElement which references a database entry, inlined in the text, like "see [thisbook]" where [thisbook] references some database entry's ID). MSDN says all that is far too much text for a TextBlock.

当用户查询数据库时,使用数据库中的字段从头开始生成带有颜色和格式的只读富文本页面,包括上面保存的富文本字段:这些字段从FlowDocuments转换为Spans,并且一些替换在它们(InlineUIContainers,它托管一个派生自UIElement的类,它引用一个数据库条目,在文本中内联,如“see [thisbook]”,其中[thisbook]引用了一些数据库条目的ID)。 MSDN说明TextBlock的文本太多了。

That text rendering is the really slow part but there is no way around it, I need that formatting and it's just how the WPF RichTextBoxes are: even when entering a little simple text in the RichTextBoxes, there is a delay between typing and the character appearing on the screen...

那个文本渲染是非常缓慢的部分,但没有办法解决它,我需要格式化,这就是WPF RichTextBox的方式:即使在RichTextBox中输入一个简单的小文本时,打字和字符出现之间也会有延迟屏幕上...

For now I still use RichTextBoxes but I keep lots of rendered layouts in memory (the Paragraph/Section/Span objects) and I am careful to rerender only the least amount of formatted text possible when changes/queries are made or different views of the database data are requested by the user.

现在我仍然使用RichTextBoxes,但是我在内存中保留了大量渲染的布局(Paragraph / Section / Span对象),并且在进行更改/查询或数据库的不同视图时,我谨慎地仅重新呈现最少量的格式化文本数据由用户请求。

It's still not fast but it's OK, changing the whole structure (AvalonEdit or FormattedText or GlyphRun) doesn't seem worth it right now, too much work, the whole serialization API with XamlWriter.Save and XamlReader.Parse simplifies much (for FormattedText and GlyphRun, I'd have to come up with a file format myself to save the formatted text to the database).

它仍然不快但是没关系,改变整个结构(AvalonEdit或FormattedText或GlyphRun)现在似乎不值得,太多的工作,整个序列化API与XamlWriter.Save和XamlReader.Parse简化了很多(对于FormattedText和GlyphRun,我必须自己想出一个文件格式来保存格式化的文本到数据库中)。

There is also the possibility of using the OpenXML SDK to create Microsoft Word .docx documents but google says rendering performance isn't great either, and I don't know if embedding an UIElement in the text within an InlineUIContainer and serializing that to be saved in the database would be possible (same problem with AvalonEdit).

还有可能使用OpenXML SDK创建Microsoft Word .docx文档,但谷歌表示渲染性能也不是很好,我不知道是否在InlineUIContainer中的文本中嵌入UIElement并序列化要保存的内容在数据库中是可能的(与AvalonEdit相同的问题)。

1 个解决方案

#1


1  

Consider throwing away RichTextBox because it is so HUGELY SLOW (spot on). Instead of writing your own text editor check AvalonEdit. Performance wise it beats RichTextBox like a baby.

考虑扔掉RichTextBox,因为它非常慢(点亮)。而不是编写自己的文本编辑器检查AvalonEdit。性能方面,它像婴儿一样击败RichTextBox。

Or if you need read-only text you could try a TextBlock - it supports simple formatting:

或者,如果您需要只读文本,可以尝试使用TextBlock - 它支持简单格式化:

<TextBlock>
   <Run FontWeight="Bold">Hello</Run>
   <Run Foreground="Green">World</Run>
   <Run FontSize="24">!</Run>
</TextBlock>

#1


1  

Consider throwing away RichTextBox because it is so HUGELY SLOW (spot on). Instead of writing your own text editor check AvalonEdit. Performance wise it beats RichTextBox like a baby.

考虑扔掉RichTextBox,因为它非常慢(点亮)。而不是编写自己的文本编辑器检查AvalonEdit。性能方面,它像婴儿一样击败RichTextBox。

Or if you need read-only text you could try a TextBlock - it supports simple formatting:

或者,如果您需要只读文本,可以尝试使用TextBlock - 它支持简单格式化:

<TextBlock>
   <Run FontWeight="Bold">Hello</Run>
   <Run Foreground="Green">World</Run>
   <Run FontSize="24">!</Run>
</TextBlock>