public string GetHtml(Range r) { this.tb = r.tb; Dictionary <StyleIndex, object> styles = new Dictionary <StyleIndex, object>(); StringBuilder sb = new StringBuilder(); StringBuilder tempSB = new StringBuilder(); StyleIndex currentStyleId = StyleIndex.None; r.Normalize(); int currentLine = r.Start.iLine; styles[currentStyleId] = null; // if (UseOriginalFont) { sb.AppendFormat("<font style=\"font-family: {0}, monospace; font-size: {1}pt; line-height: {2}px;\">", r.tb.Font.Name, r.tb.Font.SizeInPoints, r.tb.CharHeight); } // if (IncludeLineNumbers) { tempSB.AppendFormat("<span class=lineNumber>{0}</span> ", currentLine + 1); } // bool hasNonSpace = false; foreach (Place p in r) { Char c = r.tb[p.iLine][p.iChar]; if (c.style != currentStyleId) { Flush(sb, tempSB, currentStyleId); currentStyleId = c.style; styles[currentStyleId] = null; } if (p.iLine != currentLine) { for (int i = currentLine; i < p.iLine; i++) { tempSB.Append(UseBr ? "<br>" : "\r\n"); if (IncludeLineNumbers) { tempSB.AppendFormat("<span class=lineNumber>{0}</span> ", i + 2); } } currentLine = p.iLine; hasNonSpace = false; } switch (c.c) { case ' ': if ((hasNonSpace || !UseForwardNbsp) && !UseNbsp) { goto default; } tempSB.Append(" "); break; case '<': tempSB.Append("<"); break; case '>': tempSB.Append(">"); break; case '&': tempSB.Append("&"); break; default: hasNonSpace = true; tempSB.Append(c.c); break; } } Flush(sb, tempSB, currentStyleId); if (UseOriginalFont) { sb.Append("</font>"); } //build styles if (UseStyleTag) { tempSB.Length = 0; tempSB.Append("<style type=\"text/css\">"); foreach (var styleId in styles.Keys) { tempSB.AppendFormat(".fctb{0}{{ {1} }}\r\n", GetStyleName(styleId), GetCss(styleId)); } tempSB.Append("</style>"); sb.Insert(0, tempSB.ToString()); } if (IncludeLineNumbers) { sb.Insert(0, LineNumbersCSS); } return(sb.ToString()); }
private void Find(string pattern, FindNextDirection direction) { string originalPattern = pattern; Place start = new Place(0, 0); Place endOfDocument = new Place(tb.GetLineLength(tb.LinesCount - 1), tb.LinesCount - 1); try { // create Regex RegexOptions opt = cbMatchCase.Checked ? RegexOptions.None : RegexOptions.IgnoreCase; if (!cbRegex.Checked) { pattern = Regex.Escape(pattern); } if (cbWholeWord.Checked) { pattern = "\\b" + pattern + "\\b"; } // the current position Range range = tb.Selection.Clone(); range.Normalize(); // remember the start position start = new Place(range.Start.iChar, range.Start.iLine); if (direction == FindNextDirection.Next) { // search till the end of the document if (this.hasPreviousFindResult) { // increase range.Start with one position (if we don't do this will keep finding the same string) range.Start = NextPlace(start); } else { range.Start = start; } range.End = endOfDocument; // search until end of document } else // find previous { // search backwards till start of document range.Start = new Place(0, 0); range.End = start; } Place foundMatchPlace; bool foundMatch = TryFindNext(pattern, opt, direction, range, out foundMatchPlace); if (foundMatch) { this.hasPreviousFindResult = true; return; } // Searching forwarded and started at (0,0) => we have found nothing... if (direction == FindNextDirection.Next && start == new Place(0, 0)) { // Only show message when we don't have a previous find. if (!this.hasPreviousFindResult) { MessageBox.Show(String.Format("Pattern {0} not found.", originalPattern)); } this.hasPreviousFindResult = false; return; } // Searching backward and started at end of document => we have found nothing if (direction == FindNextDirection.Previous && start == endOfDocument) { // Only show message when we don't have a previous find. if (!this.hasPreviousFindResult) { MessageBox.Show(String.Format("Pattern {0} not found.", originalPattern)); } this.hasPreviousFindResult = false; return; } // we haven't searched the entire document // Change the search range depending on whether we are searching for the next or previous if (direction == FindNextDirection.Next) { // search from (0,0) to the line-end of start range.Start = new Place(0, 0); range.End = EndOfLine(start); } else // find previous { // search from document-end to line-start of start range.Start = StartOfLine(start); range.End = endOfDocument; // search until end of document } Place foundMatchPlace2; bool foundMatch2 = TryFindNext(pattern, opt, direction, range, out foundMatchPlace2); if (foundMatch2) { this.hasPreviousFindResult = true; return; } // Found nothing // Only show message when we don't have a previous find. if (!this.hasPreviousFindResult) { MessageBox.Show(String.Format("Pattern {0} not found.", originalPattern)); } this.hasPreviousFindResult = false; } catch (Exception e) { MessageBox.Show(this, e.Message, "Exception while searching"); } }
public string GetRtf(Range r) { this.tb = r.tb; var styles = new Dictionary <StyleIndex, object>(); var sb = new StringBuilder(); var tempSB = new StringBuilder(); var currentStyleId = StyleIndex.None; r.Normalize(); int currentLine = r.Start.iLine; styles[currentStyleId] = null; colorTable.Clear(); // var lineNumberColor = GetColorTableNumber(r.tb.LineNumberColor); if (IncludeLineNumbers) { tempSB.AppendFormat(@"{{\cf{1} {0}}}\tab", currentLine + 1, lineNumberColor); } // foreach (Place p in r) { Char c = r.tb[p.iLine][p.iChar]; if (c.style != currentStyleId) { Flush(sb, tempSB, currentStyleId); currentStyleId = c.style; styles[currentStyleId] = null; } if (p.iLine != currentLine) { for (int i = currentLine; i < p.iLine; i++) { tempSB.AppendLine(@"\line"); if (IncludeLineNumbers) { tempSB.AppendFormat(@"{{\cf{1} {0}}}\tab", i + 2, lineNumberColor); } } currentLine = p.iLine; } switch (c.c) { case '\\': tempSB.Append(@"\\"); break; case '{': tempSB.Append(@"\{"); break; case '}': tempSB.Append(@"\}"); break; default: var ch = c.c; var code = (int)ch; if (code < 128) { tempSB.Append(c.c); } else { tempSB.AppendFormat(@"{{\u{0}}}", code); } break; } } Flush(sb, tempSB, currentStyleId); //build color table var list = new SortedList <int, Color>(); foreach (var pair in colorTable) { list.Add(pair.Value, pair.Key); } tempSB.Length = 0; tempSB.AppendFormat(@"{{\colortbl;"); foreach (var pair in list) { tempSB.Append(GetColorAsString(pair.Value) + ";"); } tempSB.AppendLine("}"); // if (UseOriginalFont) { sb.Insert(0, string.Format(@"{{\fonttbl{{\f0\fmodern {0};}}}}{{\fs{1} ", tb.Font.Name, (int)(2 * tb.Font.SizeInPoints), tb.CharHeight)); sb.AppendLine(@"}"); } sb.Insert(0, tempSB.ToString()); sb.Insert(0, @"{\rtf1\ud\deff0"); sb.AppendLine(@"}"); return(sb.ToString()); }
public virtual void FindNext(string pattern) { try { RegexOptions opt = cbMatchCase.Checked ? RegexOptions.None : RegexOptions.IgnoreCase; if (!cbRegex.Checked) { pattern = Regex.Escape(pattern); } if (cbWholeWord.Checked) { pattern = "\\b" + pattern + "\\b"; } // Range range = tb.Selection.Clone(); range.Normalize(); // if (firstSearch) { startPlace = range.Start; firstSearch = false; } // range.Start = range.End; if (range.Start >= startPlace) { range.End = new Place(tb.GetLineLength(tb.LinesCount - 1), tb.LinesCount - 1); } else { range.End = startPlace; } // foreach (var r in range.GetRangesByLines(pattern, opt)) { if (r.Start == r.End) { MessageBox.Show("찾는 문자열이 빈 문자열입니다."); return; } tb.Selection = r; tb.DoSelectionVisible(); tb.Invalidate(); return; } // if (range.Start >= startPlace && startPlace > Place.Empty) { tb.Selection.Start = new Place(0, 0); FindNext(pattern); return; } MessageBox.Show("찾을 수 없습니다."); } catch (Exception ex) { MessageBox.Show(ex.Message); } }