//[Test] public void TestCharPosition21() { //Testing VisualToCharOffset if line is empty TextBuffer textBuffer = new TextBuffer("hello\n\n\t\tcruel\n\tworld"); coordinates = new CharPosition(textBuffer); int offset = coordinates.CharToVisualOffset(4, 4); int charoffset = coordinates.VisualToCharOffset(4, offset); Assert.AreEqual(0, charoffset); }
//[Test] public void TestCharPosition22() { TextBuffer textBuffer = new TextBuffer("hello\n\n\t\tcruel\n\tworld"); coordinates = new CharPosition(textBuffer); int offset = coordinates.CharToVisualOffset(2, -1); Assert.AreEqual(0, offset); int charoffset = coordinates.VisualToCharOffset(2, offset); Assert.AreEqual(0, charoffset); }
//[Test] public void TestCharPosition20() { //Testing VisualToCharOffset for tab character TextBuffer textBuffer = new TextBuffer("hello\n\n\t\tcruel\n\tworld"); coordinates = new CharPosition(textBuffer); int offset = coordinates.CharToVisualOffset(2, 2); Assert.AreEqual(8, offset); int charoffset = coordinates.VisualToCharOffset(2, offset); Assert.AreEqual(2, charoffset); }
protected override DrawingVisual RenderCore(RenderParameters renderParams) { if (drawingVisual == null) { drawingVisual = new DrawingVisual(); } if (null == currentScript) { return(drawingVisual); // No active script yet! } System.Windows.Point linePosition = new System.Windows.Point( Configurations.CanvasMarginLeft, 0); ITextEditorCore textCore = textEditorCanvas.TextEditorCore; ITextBuffer textBuffer = currentScript.GetTextBuffer(); numberOfLines = textBuffer.GetLineCount(); int lastVisibleLine = renderParams.firstVisibleLine + renderParams.maxVisibleLines; if (lastVisibleLine >= numberOfLines) { lastVisibleLine = numberOfLines - 1; } // Retrieve the DrawingContext from the DrawingVisual. DrawingContext context = drawingVisual.RenderOpen(); int maxColumns = textEditorCanvas.MaxVisibelColumns + 1; double hiddenWidth = textEditorCanvas.FirstVisibleColumn * Configurations.FormatFontWidth; CharPosition converter = this.currentScript.CreateCharPosition(); Typeface font = new Typeface(Configurations.FontFace); for (int lineIndex = renderParams.firstVisibleLine; lineIndex <= lastVisibleLine; lineIndex++) { // A constant starting point on left edge. linePosition.X = Configurations.CanvasMarginLeft; int startColumn = converter.VisualToCharOffset(lineIndex, textEditorCanvas.FirstVisibleColumn); string lineContent = textBuffer.GetLineContent(lineIndex); int lineLength = (lineContent != null ? lineContent.Length : 0); if (lineLength <= startColumn) { linePosition.Y += Configurations.FontDisplayHeight; continue; } if (startColumn > 0) { string hiddenText = lineContent.Substring(0, startColumn); hiddenText = hiddenText.Replace("\t", Configurations.TabSpaces); // Accounting for the part that is hidden beyond the left of canvas. double hiddenTextWidth = hiddenText.Length * Configurations.FormatFontWidth; linePosition.X = linePosition.X + hiddenTextWidth - hiddenWidth; } int endColumn = startColumn + maxColumns; endColumn = ((endColumn >= lineLength) ? lineLength - 1 : endColumn); for (int charIndex = startColumn; charIndex <= endColumn; charIndex++) { CodeFragment fragment = null; int fragmentWidth = textCore.GetFragment( charIndex, lineIndex, out fragment); // We may be showing partial fragment now. if (null != fragment) { fragmentWidth -= (charIndex - fragment.ColStart); } int offsetToNextChar = fragmentWidth - 1; if ((charIndex + fragmentWidth) > (lineLength - 1)) { fragmentWidth = (lineLength - 1) - charIndex; offsetToNextChar = fragmentWidth - 1; } // Initialize the text store. string textContent = string.Empty; textContent = lineContent.Substring(charIndex, ((fragmentWidth == 0) ? 1 : fragmentWidth)); // Replace all TAB characters with actual spaces. textContent = textContent.Replace("\t", Configurations.TabSpaces); CodeFragment.Type fragmentType = CodeFragment.Type.None; if (null != fragment) { fragmentType = fragment.CodeType; } Color textColor = CodeFragment.GetFragmentColor(fragmentType); if ((textBuffer.ParsePending == true) && (fragmentType == CodeFragment.Type.None)) { textColor = Colors.Black; } FormattedText formattedText = new FormattedText( textContent, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, font, Configurations.FontHeight, new SolidColorBrush(textColor)); // Draw the formatted text into the drawing context. context.DrawText(formattedText, linePosition); if (lineContent[charIndex] == '\n') { break; } else { linePosition.X += formattedText.WidthIncludingTrailingWhitespace; } if (fragmentWidth > 0) { charIndex += offsetToNextChar; } } // Update the line position coordinate for the displayed line. linePosition.Y += Configurations.FontDisplayHeight; } // Persist the drawn text content. context.Close(); return(drawingVisual); }
public void TestCharPosition22() { TextBuffer textBuffer = new TextBuffer("hello\n\n\t\tcruel\n\tworld"); coordinates = new CharPosition(textBuffer); int offset = coordinates.CharToVisualOffset(2, -1); Assert.AreEqual(0, offset); int charoffset = coordinates.VisualToCharOffset(2, offset); Assert.AreEqual(0, charoffset); }
public void TestCharPosition21() { //Testing VisualToCharOffset if line is empty TextBuffer textBuffer = new TextBuffer("hello\n\n\t\tcruel\n\tworld"); coordinates = new CharPosition(textBuffer); int offset = coordinates.CharToVisualOffset(4, 4); int charoffset = coordinates.VisualToCharOffset(4, offset); Assert.AreEqual(0, charoffset); }
public void TestCharPosition20() { //Testing VisualToCharOffset for tab character TextBuffer textBuffer = new TextBuffer("hello\n\n\t\tcruel\n\tworld"); coordinates = new CharPosition(textBuffer); int offset = coordinates.CharToVisualOffset(2, 2); Assert.AreEqual(8, offset); int charoffset = coordinates.VisualToCharOffset(2, offset); Assert.AreEqual(2, charoffset); }