// Copyright (c) 2013-2021 Jean-Philippe Bruyère // // This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; namespace Crow.Text { public enum LineBreakKind { Undefined, Unix, Windows, Other } /// /// represent a single line span with end of line handling. /// [DebuggerDisplay ("{Start}, {Length}, {LengthInPixel}")] public struct TextLine : IComparable { /// /// Line start absolute character position. /// public int Start; /// /// Line's character count not including linebreak if any. /// public int Length; /// /// Total line's Character count including linebreak characters if any. /// public int LengthIncludingLineBreak; /// /// Cached line's length in pixel. If not yet computed by renderer, value is '-1' /// public int LengthInPixel; /// /// Absolute end character position just before linebreak if any. /// public int End => Start + Length; public TextSpan Span => new TextSpan (Start, End); public TextSpan SpanIncludingLineBreak => new TextSpan (Start, EndIncludingLineBreak); /// /// Absolute line's end position after linebreak if any. /// public int EndIncludingLineBreak => Start + LengthIncludingLineBreak; /// /// Character count of the linebreak, 0 if no linebreak. /// public int LineBreakLength => LengthIncludingLineBreak - Length; /// /// True line has a linebreak, false otherwise. /// public bool HasLineBreak => LineBreakLength > 0; /// /// Create a new TextLine span using the absolute start and end character positions. /// public TextLine (int start, int end, int endIncludingLineBreak) { Start = start; Length = end - start; LengthIncludingLineBreak = endIncludingLineBreak - start; LengthInPixel = -1; } /// /// Create an empty line span without linebreak starting at absolute charater position given in argument. /// /// public TextLine (int start) { Start = start; Length = 0; LengthIncludingLineBreak = 0; LengthInPixel = -1; } /// /// Set a new line's length and reset computed length in pixel to '-1'. /// /// public void SetLength (int newLength) { LengthInPixel = -1; Length = newLength; } /// /// Create a new TextLine span with a start offset, length in pixel is reseted. /// /// /// public TextLine WithStartOffset (int start) => new TextLine (Start + start, End, EndIncludingLineBreak); public int CompareTo (TextLine other) => Start - other.Start; } }