/// <summary> /// Converts a string to RTF based on scintilla configuration /// </summary> public static String GetConversion(Language lang, ScintillaControl sci, int start, int end) { UseStyle[] useStyles = lang.usestyles; Dictionary<uint, ColorData> StyleColors = new Dictionary<uint, ColorData>(MAX_COLORDEF); Dictionary<string, FontData> StyleFonts = new Dictionary<string, FontData>(MAX_FONTDEF); String text = sci.Text.Clone().ToString(); StringBuilder rtfHeader = new StringBuilder(RTF_HEADEROPEN); StringBuilder rtfFont = new StringBuilder(RTF_FONTDEFOPEN); StringBuilder rtfColor = new StringBuilder(RTF_COLORDEFOPEN); StringBuilder rtf = new StringBuilder(); char[] chars = text.ToCharArray(); int lengthDoc = text.Length; int lastStyleByte = -1; string lastFontName = ""; int lastFontSize = -1; bool lastBold = false; bool lastItalic = false; uint lastBack = 0; uint lastFore = 0; if (end < 0 || end > lengthDoc) { end = lengthDoc; } int totalColors = 1; int totalFonts = 0; //---------------------------------------------------- // Grab all styles used based on the Style Byte. // Then store the basic properties in a Dictionary. //---------------------------------------------------- for (int istyle = start; istyle < end; istyle++) { // Store Byte int styleByte = sci.StyleAt(istyle); // Check Difference if (styleByte != lastStyleByte) { // Store Style UseStyle sty = useStyles[styleByte]; // Grab Properties string fontName = sty.FontName; int fontSize = sty.FontSize * 2; bool bold = sty.IsBold; bool italic = sty.IsItalics; uint back = (uint)sty.BackgroundColor; uint fore = (uint)(sty.fore != null && sty.fore.Length > 0 ? int.Parse(sty.fore.Substring(2, sty.fore.Length - 2), System.Globalization.NumberStyles.HexNumber) : 0); if (lastFontName != fontName || lastFontSize != fontSize || lastBold != bold || lastItalic != italic || lastBack != back || lastFore != fore) { // Check Colors ColorData backColorTest; ColorData foreColorTest; if (!StyleColors.TryGetValue(back, out backColorTest)) { Color newColor = Color.FromArgb((int)back); backColorTest = new ColorData(totalColors++, newColor); StyleColors.Add(back, backColorTest); rtfColor.AppendFormat(RTF_SET_COLOR, newColor.R, newColor.G, newColor.B); Console.WriteLine(Color.FromArgb((int)back)); } if (!StyleColors.TryGetValue(fore, out foreColorTest)) { Color newColor = Color.FromArgb((int)fore); foreColorTest = new ColorData(totalColors++, newColor); StyleColors.Add(fore, foreColorTest); rtfColor.AppendFormat(RTF_SET_COLOR, newColor.R, newColor.G, newColor.B); Console.WriteLine(Color.FromArgb((int)fore)); } // Check Fonts FontData fontTest; if (!StyleFonts.TryGetValue(fontName, out fontTest)) { fontTest = new FontData(totalFonts, fontName); StyleFonts.Add(fontName, fontTest); rtfFont.Append(@"{" + RTF_SETFONTFACE + totalFonts + " " + fontName + ";}"); totalFonts++; Console.WriteLine(fontName); } rtf.Append((lastStyleByte == -1 ? "{\\pard\\plain" : "}{\\pard\\plain")); // Write out RTF rtf.AppendFormat(RTF_SET_FORMAT, fontTest.FontIndex, fontSize, backColorTest.ColorIndex, foreColorTest.ColorIndex, (bold ? "" : "0"), (italic ? "" : "0")); } lastFontName = fontName; lastFontSize = fontSize; lastBold = bold; lastItalic = italic; lastBack = back; lastFore = fore; } lastStyleByte = styleByte; char ch = chars[istyle]; String curr = ""; if (ch == '{') curr = "\\{"; else if (ch == '}') curr = "\\}"; else if (ch == '\\') curr = "\\\\"; else if (ch == '\t') { if (sci.IsUseTabs) curr = RTF_TAB; else curr = "".PadRight(sci.Indent, ' '); } else if (ch == '\n') { if (istyle == 0 || chars[istyle - 1] != '\r') curr = "\\line\n"; } else if (ch == '\r') curr = "\\line\n"; else if (!(Char.IsLetterOrDigit(ch) || Char.IsWhiteSpace(ch))) curr = "\\'" + ((int)ch).ToString("x2"); else curr = ch.ToString(); rtf.Append(@curr); } // Close Headers rtfColor.Append('}'); rtfFont.Append('}'); rtf.Append('}'); rtfHeader.AppendFormat("\n{0}\n{1}\n{2}\n{3}", rtfFont.ToString(), rtfColor.ToString(), rtf.ToString(), "}"); return rtfHeader.ToString(); }
/// <summary> /// Converts a string to RTF based on scintilla configuration /// </summary> public static String GetConversion(Language lang, ScintillaControl sci, int start, int end) { UseStyle[] useStyles = lang.usestyles; Dictionary <uint, ColorData> StyleColors = new Dictionary <uint, ColorData>(MAX_COLORDEF); Dictionary <string, FontData> StyleFonts = new Dictionary <string, FontData>(MAX_FONTDEF); String text = sci.Text.Clone().ToString(); StringBuilder rtfHeader = new StringBuilder(RTF_HEADEROPEN); StringBuilder rtfFont = new StringBuilder(RTF_FONTDEFOPEN); StringBuilder rtfColor = new StringBuilder(RTF_COLORDEFOPEN); StringBuilder rtf = new StringBuilder(); char[] chars = text.ToCharArray(); int lengthDoc = text.Length; int lastStyleByte = -1; string lastFontName = ""; int lastFontSize = -1; bool lastBold = false; bool lastItalic = false; uint lastBack = 0; uint lastFore = 0; if (end < 0 || end > lengthDoc) { end = lengthDoc; } int totalColors = 1; int totalFonts = 0; //---------------------------------------------------- // Grab all styles used based on the Style Byte. // Then store the basic properties in a Dictionary. //---------------------------------------------------- for (int istyle = start; istyle < end; istyle++) { // Store Byte int styleByte = sci.StyleAt(istyle); // Check Difference if (styleByte != lastStyleByte) { // Store Style UseStyle sty = useStyles[styleByte]; // Grab Properties string fontName = sty.FontName; int fontSize = sty.FontSize * 2; bool bold = sty.IsBold; bool italic = sty.IsItalics; uint back = (uint)sty.BackgroundColor; uint fore = (uint)(!string.IsNullOrEmpty(sty.fore) ? int.Parse(sty.fore.Substring(2, sty.fore.Length - 2), NumberStyles.HexNumber) : 0); if (lastFontName != fontName || lastFontSize != fontSize || lastBold != bold || lastItalic != italic || lastBack != back || lastFore != fore) { // Check Colors ColorData backColorTest; ColorData foreColorTest; if (!StyleColors.TryGetValue(back, out backColorTest)) { Color newColor = Color.FromArgb((int)back); backColorTest = new ColorData(totalColors++, newColor); StyleColors.Add(back, backColorTest); rtfColor.AppendFormat(RTF_SET_COLOR, newColor.R, newColor.G, newColor.B); Console.WriteLine(Color.FromArgb((int)back)); } if (!StyleColors.TryGetValue(fore, out foreColorTest)) { Color newColor = Color.FromArgb((int)fore); foreColorTest = new ColorData(totalColors++, newColor); StyleColors.Add(fore, foreColorTest); rtfColor.AppendFormat(RTF_SET_COLOR, newColor.R, newColor.G, newColor.B); Console.WriteLine(Color.FromArgb((int)fore)); } // Check Fonts FontData fontTest; if (!StyleFonts.TryGetValue(fontName, out fontTest)) { fontTest = new FontData(totalFonts, fontName); StyleFonts.Add(fontName, fontTest); rtfFont.Append(@"{" + RTF_SETFONTFACE + totalFonts + " " + fontName + ";}"); totalFonts++; Console.WriteLine(fontName); } rtf.Append((lastStyleByte == -1 ? "{\\pard\\plain" : "}{\\pard\\plain")); // Write out RTF rtf.AppendFormat(RTF_SET_FORMAT, fontTest.FontIndex, fontSize, backColorTest.ColorIndex, foreColorTest.ColorIndex, (bold ? "" : "0"), (italic ? "" : "0")); } lastFontName = fontName; lastFontSize = fontSize; lastBold = bold; lastItalic = italic; lastBack = back; lastFore = fore; } lastStyleByte = styleByte; char ch = chars[istyle]; String curr = ""; if (ch == '{') { curr = "\\{"; } else if (ch == '}') { curr = "\\}"; } else if (ch == '\\') { curr = "\\\\"; } else if (ch == '\t') { if (sci.IsUseTabs) { curr = RTF_TAB; } else { curr = "".PadRight(sci.Indent, ' '); } } else if (ch == '\n') { if (istyle == 0 || chars[istyle - 1] != '\r') { curr = "\\line\n"; } } else if (ch == '\r') { curr = "\\line\n"; } else if (!(Char.IsLetterOrDigit(ch) || Char.IsWhiteSpace(ch))) { curr = "\\'" + ((int)ch).ToString("x2"); } else { curr = ch.ToString(); } rtf.Append(@curr); } // Close Headers rtfColor.Append('}'); rtfFont.Append('}'); rtf.Append('}'); rtfHeader.AppendFormat("\n{0}\n{1}\n{2}\n{3}", rtfFont.ToString(), rtfColor.ToString(), rtf.ToString(), "}"); return(rtfHeader.ToString()); }