Home > other >  Is there a faster way to create a Point from a text offset with AvalonEdit?
Is there a faster way to create a Point from a text offset with AvalonEdit?

Time:07-22

I am currently drawing in a BackgroundRenderer based on the text, so I need to convert from an offset to a Point, but I have found that this process is slowing down my application (and it's not the actual drawing part but rather getting the Point from the offset).

This is what I am doing currently:

var textLocation = textView.Document.GetLocation(offset);
var textViewPos = new TextViewPosition(textLocation);
var point = textView.GetVisualPosition(textViewPos, VisualYPosition.LineBottom);
point -= textView.ScrollOffset;

How can I make this more efficient?

CodePudding user response:

Avoid calling your code with offsets outside the visible region. GetVisualPosition is slow if the line is not visible, because in that case there won't be any cached layout for the line; so AvalonEdit has to perform text layout in order to return the correct X coordinate.

You can use GetVisualTopByDocumentLine and GetDocumentLineByVisualTop to efficiently convert between Y coordinates and line numbers without triggering text layout.

  • Related