public static string GetRtf(string text) { MyRichTextBox tb = new MyRichTextBox(); WriteWords(text, new Rectangle(), null, null, true, tb); return tb.Rtf; }
public static Size WriteWords(string text, Rectangle bounds, Graphics g, List<WordStyle> words = null, bool notShow = false, MyRichTextBox tb = null) { if (text.Length == 0) return new Size(); Point point = new Point(Margin.Width, Margin.Height); Font font = FontText; Color color = colorText; Color prevColor = colorText; Size wordSize = new Size(); int prevHeight = 0; int maxWidth = 0; string[] lines = text.Split('\n'); for (int iline = 0; iline < lines.Length; iline++) { MatchCollection mc = regexWords.Matches(lines[iline]); foreach (Match m in mc) { string word = m.Groups[1].Value; string word2 = m.Groups[2].Value; if (word == "<t>" ) { font = FontTitle ; color = colorText ; continue; } if (word == "<b>" ) { font = FontTextBold; continue; } if (word == "</b>") { font = FontText ; continue; } if (word == "<h>" ) { font = FontHelp ; color = colorHelp ; continue; } if (word == "<v>" ) { font = FontValue ; color = colorValue ; continue; } if (word == "<i>" ) { font = FontItalic ; continue; } if (word == "</i>") { font = FontText ; continue; } if (word == "<s>" ) { prevColor = color ; color = colorString; continue; } if (word == "</s>") { color = prevColor ; continue; } if (word == "<r>" ) { prevColor = color ; color = colorRed ; continue; } if (word == "</r>") { color = prevColor ; continue; } if (word == "<p>" ) { prevColor = color ; font = FontTextBold; color = colorParam; continue; } if (word == "</p>") { color = prevColor ; font = FontTitle ; continue; } if (word == "<id>") { point.Y += 3 ; continue; } bool notColored; if (tb!=null) { notColored = (color == colorString) || (color == colorValue); if (!notColored && isKeyWord(word)) tb.AppendText(word, colorKeyword, font); else if (!notColored && isClass (word)) tb.AppendText(word, colorClass , font); else tb.AppendText(word, color , font); tb.AppendText(word2, color, font); continue; } wordSize = TextRenderer.MeasureText(g, word+word2, font, MaxSize, tf); maxWidth = Math.Max(maxWidth, point.X+wordSize.Width); if (wordSize.Width > (bounds.Width - point.X - Margin.Width)) { point.X = Margin.Width; point.Y += prevHeight; word = word.TrimStart(); } if (word.Length == 0) continue; if (word == "<hr>") { float y = point.Y + prevHeight + 2; if (!notShow) g.DrawLine(Pens.Gray, Margin.Width, y, bounds.Width - Margin.Width, y); point.Y += 4; continue; } if (!notShow) { notColored = (color == colorString) || (color == colorValue); if (!notColored && isKeyWord(word)) DrawText(g, word, font, point, colorKeyword, words); else if (!notColored && isClass (word)) DrawText(g, word, font, point, colorClass , words); else DrawText(g, word, font, point, color , words); if (word2.Length > 0) { point.X += wordSize.Width - 4; DrawText(g, word2, font, point, color, words); point.X -= wordSize.Width - 4; } } point.X += wordSize.Width; prevHeight = wordSize.Height; } point.Y += wordSize.Height; point.X = Margin.Width; if (tb != null && tb.IsHandleCreated && !tb.IsDisposed) { tb.AppendText("\r\n"); if (iline == 0) tb.AppendText("\r\n"); } } return new Size(maxWidth+Margin.Width, point.Y+Margin.Height); }