Storing and displaying rich text efficiently
I need to store significant amounts of rich text in a SQL database, retrieve it and display it.
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.
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 desti开发者_JAVA百科nation image etc... But reinventing the wheel on simple colored/sizable text seems really strange in 2011.
EDIT: Thanks for the answers, didn't see them until now, sorry.
Text is entered by the user from RichTextBox
es 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 TextBox
es.
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 FlowDocument
s to Span
s and some replacement is done on them (InlineUIContainer
s 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
.
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 RichTextBox
es are: even when entering a little simple text in the RichTextBox
es, there is a delay between typing and the character appearing on the screen...
For now I still use RichTextBox
es 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.
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).
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).
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.
Or if you need read-only text you could try a TextBlock - it supports simple formatting:
<TextBlock>
<Run FontWeight="Bold">Hello</Run>
<Run Foreground="Green">World</Run>
<Run FontSize="24">!</Run>
</TextBlock>
精彩评论