/// <summary> /// Set color and font to default control settings. /// </summary> /// <param name="sb">the string builder building the RTF</param> /// <param name="colors">colors hashtable</param> /// <param name="fonts">fonts hashtable</param> private void SetDefaultSettings(StringBuilder sb, Hashtable colors, FontNameIndex fonts) { SetFont(sb, Font, fonts); SetColor(sb, ForeColor, colors); SetFontSize(sb, (int)Font.Size); EndTags(sb); }
/// <summary> /// Adds a font to the RTF's font table and to the fonts hashtable. /// </summary> /// <param name="sb">The RTF's string builder</param> /// <param name="font">the Font to add</param> /// <param name="counter">a counter, containing the amount of fonts in the table</param> /// <param name="fonts">an hashtable. the key is the font's name. the value is it's index in the table</param> //private void AddFontToTable(StringBuilder sb, Font font, ref int counter, FontIndex fonts) private void AddFontToTable(StringBuilder sbHeader, Font font, FontNameIndex fonts) { //fix // sb.Append(@"{\f").Append(counter).Append(@"\fnil\fcharset0").Append(font.Name).Append(";}"); //sb.Append(@"\f").Append(counter).Append(@"\fnil\fcharset0").Append(font.Name).Append(";}"); int iFountCount = fonts.Count; sbHeader.Append("\r\n "); sbHeader.Append(@"{\f").Append(iFountCount).Append(@"\fnil"); sbHeader.Append(@"\fcharset0 ").Append(font.Name).Append(";}"); fonts.Add(font.Name, iFountCount); //fonts.Add(font, counter++); }
/// <summary> /// Sets the font to the specified font. /// </summary> private void SetFont(StringBuilder sb, Font pHighlightingFont, FontNameIndex fonts) { if (pHighlightingFont == null) { return; } int iFontIndex = fonts.Count; //tweak, only add fonts that are used. if (!fonts.TryGetValue(pHighlightingFont.Name, out iFontIndex)) { iFontIndex = fonts.Count; AddFontToTable(m_FontHeader, pHighlightingFont, fonts); } sb.Append(@"\f").Append(iFontIndex).Append("{"); sb.Append(this.m_FontStyles.GetFontStyle(pHighlightingFont)); }
/// <summary> /// Set Color and font to a highlight descriptor settings. /// </summary> /// <param name="sb">the string builder building the RTF</param> /// <param name="hd">the HighlightDescriptor with the font and color settings to apply.</param> /// <param name="colors">colors hashtable</param> /// <param name="fonts">fonts hashtable</param> private void SetDescriptorSettings(StringBuilder sb, HighlightDescriptor hd, Hashtable colors, FontNameIndex fonts) { if (hd.Color != Color.Transparent) { SetColor(sb, hd.Color, colors); } Font aFont = hd.Font; if (aFont != null) { SetFont(sb, aFont, fonts); SetFontSize(sb, (int)aFont.Size); sb.Append(m_FontStyles.GetFontStyle(aFont)); } EndTags(sb); }
public void Highlight() { if (Text.Length > maxHighlightTextLength) { return; } mParsing = true; m_FontHeader.Length = 0; m_FontStyles.Clear(); //Store cursor and scrollbars location Win32.LockWindowUpdate(Handle); Win32.POINT scrollPos = GetScrollPos(); int cursorLoc = SelectionStart; StringBuilder sbBody = new StringBuilder(); try // nenecham to kvuli blbemu zvyraznovani spadnut, neee? { //Font table creation FontNameIndex fonts = new FontNameIndex(); //Adding RTF header - FIX m_FontHeader.Append(@"{\rtf1\fbidis\ansi\ansicpg1255\deff0\deflang1037"); Hashtable colors = AddColorTable(m_FontHeader); m_FontHeader.Append("\n{\\fonttbl"); //sb.Append(@"{\rtf1\fbidis\ansi\ansicpg1255\deff0\deflang1037{\fonttbl{"); //we do not need the counter, the Count-property of Font-index will work. //int fontCounter = 0; //AddFontToTable(sb, Font, ref fontCounter, fonts); //Add default font. AddFontToTable(m_FontHeader, Font, fonts); //this adds the defaultfont to the style definitions m_FontStyles.GetFontStyle(Font); //Tweak: Only load fonts that are used... //foreach (HighlightDescriptor hd in mHighlightDescriptors) //{ // if (hd.Font != null) // { // if (!fonts.ContainsKey(hd.Font)) // { // AddFontToTable(sb, hd.Font, ref fontCounter, fonts); // } // } //} //Do not close the header, we'll do that after all the fonts are added. // sbHeader.Append("}\n"); //Parsing text sbBody.Append(@"\viewkind4\uc1\pard\ltrpar").Append('\n'); //this is why we added the default font allready SetDefaultSettings(sbBody, colors, fonts); m_SeperatorCharArray = mSeperators.GetAsCharArray(); //Replacing "\" to "\\" for RTF... string sCurrentText = Text; string[] lines = sCurrentText.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Split('\n'); //will be used to determine the text to be formatted StringBuilder sbSubText = new StringBuilder(); #region RtfBodyGeneration #region for (int lineCounter = 0; lineCounter < lines.Length; lineCounter++) for (int lineCounter = 0; lineCounter < lines.Length; lineCounter++) { if (lineCounter != 0) { AddNewLine(sbBody); } string line = lines[lineCounter]; string[] tokens = mCaseSensitive ? line.Split(m_SeperatorCharArray) : line.ToUpper().Split(m_SeperatorCharArray); if (tokens.Length == 0) { AddUnicode(sbBody, line); AddNewLine(sbBody); continue; } int tokenCounter = 0; #region for (int i = 0; i < line.Length; ) for (int i = 0; i < line.Length;) { char curChar = line[i]; if (mSeperators.Contains(curChar)) { sbBody.Append(curChar); i++; } else { if (tokenCounter >= tokens.Length) { break; } string curToken = tokens[tokenCounter++]; bool bAddToken = true; #region foreach (HighlightDescriptor hd in mHighlightDescriptors) foreach (HighlightDescriptor hd in mHighlightDescriptors) { string compareStr = mCaseSensitive ? hd.Token : hd.Token.ToUpper(); bool match = false; //Check if the highlight descriptor matches the current toker according to the DescriptoRecognision property. #region switch (hd.DescriptorType) switch (hd.DescriptorRecognition) { case DescriptorRecognition.RegEx: continue; case DescriptorRecognition.WholeWord: if (curToken == compareStr) { match = true; } break; case DescriptorRecognition.StartsWith: if (curToken.StartsWith(compareStr)) { match = true; } break; case DescriptorRecognition.Contains: if (curToken.IndexOf(compareStr) != -1) { match = true; } break; } if (!match) { //If this token doesn't match chech the next one. continue; } #endregion switch (hd.DescriptorType) //printing this token will be handled by the inner code, don't apply default settings... bAddToken = false; //Set colors to current descriptor settings. //Open a "block", this we will close after adding the text to the body sbBody.Append('{'); SetDescriptorSettings(sbBody, hd, colors, fonts); //Improvement for readability instead of formatting the text in the //switch, just determine the text to be formatted, and format it after the swich. //the result is just one call to SetDefaultSettings, which is encsulated in FormatText //for better font support (another improvement). string sSubText = ""; //Print text affected by this descriptor. #region switch (hd.DescriptorType) switch (hd.DescriptorType) { case DescriptorType.Word: //Will be added to the rtf after the switch sSubText = line.Substring(i, curToken.Length); i += curToken.Length; break; case DescriptorType.ToEOW: int dummy; sSubText = GetWordBounsOnCharIndex(line, i, out dummy, out i); break; case DescriptorType.ToEOL: //Will be added to the rtf after the switch sSubText = line.Remove(0, i); i = line.Length; break; case DescriptorType.ToCloseToken: { //we have multiple itterations, so clear it first: sbSubText.Length = 0; //determine endtoken, add all encountered text to subtext int closeStart = i + hd.Token.Length; while ((line.IndexOf(hd.CloseToken, closeStart) == -1) && (lineCounter < lines.Length)) { //Will be added to the rtf after the switch sbSubText.Append(line.Remove(0, i)); lineCounter++; if (lineCounter < lines.Length) { AddNewLine(sbSubText); line = lines[lineCounter]; i = closeStart = 0; } else { i = closeStart = line.Length; } } int closeIndex = line.IndexOf(hd.CloseToken, closeStart); if (closeIndex != -1) { //Will be added to the rtf after the switch sbSubText.Append(line.Substring(i, closeIndex + hd.CloseToken.Length - i)); line = line.Remove(0, closeIndex + hd.CloseToken.Length); tokenCounter = 0; tokens = mCaseSensitive ? line.Split(m_SeperatorCharArray) : line.ToUpper().Split(m_SeperatorCharArray); i = 0; } sSubText = sbSubText.ToString(); } break; } #endregion switch (hd.DescriptorType) //now that sSubText has a value (what do we want to format), format it. //Add the text to the RTF AddUnicode(sbBody, sSubText); //Close the "block" since the text we wanted to format, is now in the body. sbBody.Append('}'); //this ends all the styles that were applied in the SetDescriptorSettings, //returning all formatting to the default SetDefaultSettings(sbBody, colors, fonts); break; } #endregion foreach (HighlightDescriptor hd in mHighlightDescriptors) if (bAddToken) { //Print text with default settings... AddUnicode(sbBody, (line.Substring(i, curToken.Length))); i += curToken.Length; } } } #endregion for (int i = 0; i < line.Length; ) } #endregion for (int lineCounter = 0; lineCounter < lines.Length; lineCounter++) #endregion //we might have added fonts to the header, which means the header is not closed yet. close it: m_FontHeader.Append("\n}\n"); ApplyRTF(m_FontHeader, sbBody); PerformRegEx(sCurrentText); } #if DEBUG catch (Exception exc) { System.Diagnostics.Debug.Fail(exc.Message, exc.StackTrace); } #else catch { } #endif //Restore cursor and scrollbars location. SelectionStart = cursorLoc; SelectionLength = 0; SetScrollPos(scrollPos); Win32.LockWindowUpdate((IntPtr)0); Invalidate(); mParsing = false; }