void ConfigureEditor(ScintillaControl sci, string code) { editor.SetMarginWidthN(1, 0); editor.ConfigurationLanguage = sci.ConfigurationLanguage; editor.Text = " "; columnWidth = editor.PointXFromPosition(1) - editor.PointXFromPosition(0); rowHeight = editor.TextHeight(0); editor.TabWidth = sci.TabWidth; editor.Text = code; editor.SetProperty("lexer.cpp.track.preprocessor", "0"); Language language = GetLanguage(editor.ConfigurationLanguage); if (language == null) { return; } UseStyle defaultStyle = null; foreach (var useStyle in language.usestyles) { if (useStyle.name == "default") { defaultStyle = useStyle; break; } } if (defaultStyle == null) { return; } codeTip.BackColor = DataConverter.BGRToColor(defaultStyle.BackgroundColor); }
/// <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()); }
/// <summary> /// Converts a string to RTF based on scintilla configuration /// </summary> public static string GetConversion(Language lang, ScintillaControl sci, int start, int end) { int lengthDoc = sci.TextLength; if (end < 0 || end > lengthDoc) { end = lengthDoc; } var useStyles = new UseStyle[MAX_STYLEDEF]; foreach (var useStyle in lang.usestyles) { if (useStyle.key >= MAX_STYLEDEF) { // highest style is STYLE_LASTPREDEFINED, which is 39 // something is wrong if the code reaches here... throw an exception? continue; } useStyles[useStyle.key] = useStyle; } var fontTable = new StringBuilder(RTF_FONTTABLEOPEN); var colorTable = new StringBuilder(RTF_COLORTABLEOPEN); var body = new StringBuilder(); var fontDef = new Dictionary <string, int>(); var colorDef = new Dictionary <int, int>(); int fontCount = 0; int colorCount = 0; int lastStyleByte = -1; string lastFontName = null; int lastFontSize = -1; int lastForeColor = -1; int lastBackColor = -1; bool lastBold = false; bool lastItalic = false; // Add the 'default' style back colour without the colour definition // so the RTF reader uses its own default colour - the 'auto' colour colorDef.Add(GetBackColor(useStyles[0]), colorCount++); colorTable.Append(RTF_COLORDEFDELIMITER); string chars = sci.GetTextRange(start, end); int length = end - start; for (int i = 0; i < length; i++) { int styleByte = sci.StyleAt(start + i); if (styleByte >= MAX_STYLEDEF) { // highest style is STYLE_LASTPREDEFINED, which is 39 // something is wrong if the code reaches here... throw an exception? styleByte = 0; } if (styleByte != lastStyleByte) { var style = useStyles[styleByte]; if (style == null) { // there shouldn't be any style that's not defined but present in the editor // something is wrong if the code reaches here... throw an exception? styleByte = 0; style = useStyles[styleByte]; } string fontName = style.FontName; if (lastFontName != fontName) { int fontIndex; if (!fontDef.TryGetValue(fontName, out fontIndex)) { fontIndex = fontCount++; fontDef.Add(fontName, fontIndex); fontTable.Append(RTF_FONTDEFOPEN + RTF_SETFONTFACE + fontIndex + " " + fontName + RTF_FONTDEFCLOSE); } body.Append(RTF_SETFONTFACE + fontIndex + " "); lastFontName = fontName; } int fontSize = style.FontSize << 1; // font size is stored in half-points in RTF if (lastFontSize != fontSize) { body.Append(RTF_SETFONTSIZE + fontSize + " "); lastFontSize = fontSize; } int foreColor = GetForeColor(style); if (lastForeColor != foreColor) { int colorIndex; if (!colorDef.TryGetValue(foreColor, out colorIndex)) { var color = Color.FromArgb(foreColor); colorIndex = colorCount++; colorDef.Add(foreColor, colorIndex); colorTable.Append(RTF_SETCOLORRED + color.R + RTF_SETCOLORGREEN + color.G + RTF_SETCOLORBLUE + color.B + RTF_COLORDEFDELIMITER); } body.Append(RTF_SETCOLORFORE + colorIndex + " "); lastForeColor = foreColor; } int backColor = GetBackColor(style); if (lastBackColor != backColor) { int colorIndex; if (!colorDef.TryGetValue(backColor, out colorIndex)) { var color = Color.FromArgb(backColor); colorIndex = colorCount++; colorDef.Add(backColor, colorIndex); colorTable.Append(RTF_SETCOLORRED + color.R + RTF_SETCOLORGREEN + color.G + RTF_SETCOLORBLUE + color.B + RTF_COLORDEFDELIMITER); } body.Append(RTF_SETCOLORBACK + colorIndex + " "); body.Append(RTF_SETHIGHLIGHT + colorIndex + " "); // set highlight colour as well lastBackColor = backColor; } bool bold = style.IsBold; if (lastBold != bold) { body.Append(bold ? RTF_BOLD_ON : RTF_BOLD_OFF); lastBold = bold; } bool italic = style.IsItalics; if (lastItalic != italic) { body.Append(italic ? RTF_ITALIC_ON : RTF_ITALIC_OFF); lastItalic = italic; } lastStyleByte = styleByte; } char c = chars[i]; switch (c) { case '\0': break; // ignore NULL characters as they will cause the clipboard to truncate the string case '{': body.Append(@"\{"); break; case '}': body.Append(@"\}"); break; case '\\': body.Append(@"\\"); break; case '\t': body.Append(RTF_TAB); break; case '\r': body.Append(RTF_EOL); // alternative: RTF_LINEBREAK break; case '\n': if (i == 0 || chars[i - 1] != '\r') { body.Append(RTF_EOL); // alternative: RTF_LINEBREAK } break; default: if (' ' <= c || c <= sbyte.MaxValue) { body.Append(c); } else if (c <= byte.MaxValue) { body.Append(RTF_HEX + ((byte)c).ToString("x2")); } else if (c <= ushort.MaxValue) { body.Append(RTF_UNICODE + ((short)c) + RTF_UNICODESUBSTITUTE); } break; } } fontTable.Append(RTF_FONTTABLECLOSE); colorTable.Append(RTF_COLORTABLECLOSE); return(RTF_HEADEROPEN + fontTable + colorTable + body + RTF_HEADERCLOSE); }