// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Rtf.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// Class defining all the Rtf constants and methods.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using Catel.Windows.Controls;
namespace Catel.Windows.Documents
{
/// <summary>
/// Class defining all the Rtf constants and methods.
/// </summary>
public static class Rtf
{
#region Constants
/// <summary>
/// Rtf marker for font style.
/// </summary>
public const string RtfFontStyleMarker = "{\\fonttbl";
/// <summary>
/// Rtf marker for font size.
/// </summary>
public const string RtfFontSizeMarker = "\\fs";
private const string CarriageReturn = "\r";
private const string LineFeed = "\n";
private const string CarriageReturnLineFeed = "\r\n";
private const string CustomCarriageReturn = "[cr]";
private const string CustomLineFeed = "[lf]";
private const string CustomCarriageReturnLineFeed = "[crlf]";
#endregion
#region Methods
/// <summary>
/// Converts plain text to Rtf with the newline bug fix.
/// </summary>
/// <param name="text">Text to convert.</param>
/// <param name="fontSize">Size of the font.</param>
/// <param name="fontFamily">The font family.</param>
/// <returns>Rtf version of the text.</returns>
public static string ConvertPlainTextToRtf(this string text, double fontSize, FontFamily fontFamily)
{
const string RtfCarriageReturn = "\\line ";
// Replace all known feeds to a custom constant
text = text.Replace(CarriageReturnLineFeed, CustomCarriageReturnLineFeed).Replace(CarriageReturn, CustomCarriageReturn).Replace(LineFeed, CustomLineFeed);
// Now convert all working feeds back
text = text.Replace(CustomCarriageReturnLineFeed, CarriageReturnLineFeed).Replace(CustomLineFeed, CarriageReturnLineFeed);
// Convert using a control
RichTextBox rtfControl = CreateRichTextBoxWithText(text, fontSize, fontFamily);
// Read the text again (now it should be rtf)
text = rtfControl.GetText();
// Now replace all known bugs
text = text.Replace(CustomCarriageReturn, RtfCarriageReturn);
return text;
}
/// <summary>
/// Converts the RTF to plain text.
/// </summary>
/// <param name="text">The text.</param>
/// <returns></returns>
public static string ConvertRtfToPlainText(this string text)
{
// Fix the newline bug by replacing \par by \par\par and \line by \par
text = text.Replace("\\par", "\\par\\par").Replace("\\line", "\\par").Replace("\\par\\pard", "\\pard");
// Convert using a control
RichTextBox rtfControl = CreateRichTextBoxWithText(text);
// Get the text range
var textRange = new TextRange(rtfControl.Document.ContentStart, rtfControl.Document.ContentEnd);
// Get the plain text from the text range
text = textRange.Text;
// Remove last linefeeds
while (text.EndsWith(Environment.NewLine))
{
text = text.Substring(0, text.Length - Environment.NewLine.Length);
}
// Replace all known feeds to a custom constant
text = text.Replace(CarriageReturnLineFeed + CarriageReturnLineFeed, CustomCarriageReturnLineFeed).Replace(CarriageReturnLineFeed, CustomCarriageReturn).Replace(LineFeed, CustomLineFeed);
// Now convert all working feeds back
text = text.Replace(CustomCarriageReturnLineFeed, CarriageReturnLineFeed).Replace(CustomCarriageReturn, CarriageReturn).Replace(CustomLineFeed, CarriageReturnLineFeed);
// Return text
return text;
}
#endregion
#region Private helper methods
private static RichTextBox CreateRichTextBoxWithText(string text)
{
return CreateRichTextBoxWithText(text, null, null);
}
private static RichTextBox CreateRichTextBoxWithText(string text, double? fontSize, FontFamily fontFamily)
{
var rtfControl = new RichTextBox();
rtfControl.Width = 300;
rtfControl.Height = 300;
var textRange = new TextRange(rtfControl.Document.ContentStart, rtfControl.Document.ContentEnd);
textRange.SetText(text);
if (fontSize.HasValue)
{
rtfControl.FontSize = fontSize.Value;
}
if (fontFamily != null)
{
rtfControl.FontFamily = fontFamily;
}
textRange.ClearTextMargins();
return rtfControl;
}
#endregion
}
}