New tests.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / RichTextBox.cs
index f9842135226e02bf22bcaed859cffdaf56a1e837..1db7f29a519e0819d7696faeb9cff1a41f001da9 100644 (file)
@@ -1950,12 +1950,12 @@ namespace System.Windows.Forms {
 
                static readonly char [] ReservedRTFChars = new char [] { '\\', '{', '}' };
 
-               [MonoInternalNote ("Emit unicode and other special characters properly")]
                private void EmitRTFText(StringBuilder rtf, string text) {
                        int start = rtf.Length;
                        int count = text.Length;
 
-                       rtf.Append(text);
+                       // First emit simple unicode chars as escaped
+                       EmitEscapedUnicode (rtf, text);
 
                        // This method emits user text *only*, so it's safe to escape any reserved rtf chars
                        // Escape '\' first, since it is used later to escape the other chars
@@ -1966,6 +1966,39 @@ namespace System.Windows.Forms {
                        }
                }
 
+               // The chars to be escaped use "\'" + its hexadecimal value.
+               private void EmitEscapedUnicode (StringBuilder sb, string text)
+               {
+                       int pos;
+                       int start = 0;
+
+                       while ((pos = IndexOfNonAscii (text, start)) > -1) {
+                               sb.Append (text, start, pos - start);
+
+                               int n = (int)text [pos];
+                               sb.Append ("\\'");
+                               sb.Append (n.ToString ("X"));
+
+                               start = pos + 1;
+                       }
+
+                       // Append remaining (maybe all) the text value.
+                       if (start < text.Length)
+                               sb.Append (text, start, text.Length - start);
+               }
+
+               // MS seems to be escaping values larger than 0x80
+               private int IndexOfNonAscii (string text, int startIndex)
+               {
+                       for (int i = startIndex; i < text.Length; i++) {
+                               int n = (int)text [i];
+                               if (n < 0 || n >= 0x80)
+                                       return i;
+                       }
+
+                       return -1;
+               }
+
                // start_pos and end_pos are 0-based
                private StringBuilder GenerateRTF(Line start_line, int start_pos, Line end_line, int end_pos) {
                        StringBuilder   sb;