// rendering protected override void OnRender(DrawingContext drawingContext) { // draw background drawingContext.PushClip(new RectangleGeometry(new Rect(0, 0, this.ActualWidth, this.ActualHeight))); drawingContext.DrawRectangle(this.BackgroundBrush, new Pen(), new Rect(0, 0, this.ActualWidth, this.ActualHeight)); // draw text if (this.Text != String.Empty) { FormattedText ft = new FormattedText( this.Text, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily.Source), this.FontSize, ForegroundBrush); var left_margin = 4.0 + this.BorderThickness.Left + LineNumberMarginWidth; var top_margin = 2.0 + this.BorderThickness.Top; // Background highlight //if (HighlightText != null && HighlightText.Count > 0) //{ // foreach (string text in HighlightText) // { // int txtEnd = this.Text.Length; // int index = 0; // int lastIndex = this.Text.LastIndexOf(text, StringComparison.OrdinalIgnoreCase); // while (index <= lastIndex) // { // index = this.Text.IndexOf(text, index, StringComparison.OrdinalIgnoreCase); // Geometry geom = ft.BuildHighlightGeometry(new Point(left_margin, top_margin - this.VerticalOffset), index, text.Length); // if (geom != null) // { // drawingContext.DrawGeometry(HighlightBrush, null, geom); // } // index += 1; // } // } //} //if (SyntaxLexer != null) //{ // // set color of tokens by syntax rules // foreach (var t in SyntaxLexer.Tokens) // { // SyntaxRuleItem rule = GetSyntaxRule(t.TokenType); // if (rule != null) // { // ft.SetForegroundBrush(rule.Foreground, t.Start, t.Length); // } // } //} ft.Trimming = TextTrimming.None; // left from first char boundary double left_border = GetRectFromCharacterIndex(0).Left; if (!Double.IsInfinity(left_border)) { _left_text_border = left_border; } drawingContext.DrawText(ft, new Point(_left_text_border - this.HorizontalOffset, top_margin - this.VerticalOffset)); // draw lines if (this.GetLastVisibleLineIndex() != -1) { LastLineNumberFormat = GetLineNumbers(); } if (LastLineNumberFormat != null) { LastLineNumberFormat.SetForegroundBrush(LineNumberBrush); drawingContext.DrawText(LastLineNumberFormat, new Point(3, top_margin)); } } }
// rendering protected override void OnRender(DrawingContext drawingContext) { // draw background drawingContext.PushClip(new RectangleGeometry(new Rect(0, 0, ActualWidth, ActualHeight))); drawingContext.DrawRectangle(BackgroundBrush, new Pen(), new Rect(0, 0, ActualWidth, ActualHeight)); // draw text if (Text == string.Empty) { return; } var formattedText = new FormattedText( Text, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(FontFamily.Source), FontSize, ForegroundBrush); var leftMargin = 4.0 + BorderThickness.Left + LineNumberMarginWidth; var topMargin = 2.0 + BorderThickness.Top; // Background highlight if (HighlightText != null && HighlightText.Any()) { foreach (string text in HighlightText) { var index = 0; var lastIndex = Text.LastIndexOf(text, StringComparison.OrdinalIgnoreCase); while (index <= lastIndex) { index = Text.IndexOf(text, index, StringComparison.OrdinalIgnoreCase); Geometry geom = formattedText.BuildHighlightGeometry(new Point(leftMargin, topMargin - VerticalOffset), index, text.Length); if (geom != null) { drawingContext.DrawGeometry(HighlightBrush, null, geom); } index += 1; } } } HighlightSyntax(formattedText); // left from first char boundary var leftBorder = GetRectFromCharacterIndex(0).Left; if (!double.IsInfinity(leftBorder)) { _leftTextBorder = leftBorder; } drawingContext.DrawText(formattedText, new Point(_leftTextBorder - HorizontalOffset, topMargin - VerticalOffset)); // draw lines if (GetLastVisibleLineIndex() != -1) { LastLineNumberFormat = GetLineNumbers(); } if (LastLineNumberFormat != null) { LastLineNumberFormat.SetForegroundBrush(LineNumberBrush); drawingContext.DrawText(LastLineNumberFormat, new Point(3, topMargin)); } }