aboutsummaryrefslogtreecommitdiffstatsci
path: root/Drawing2D/src/TextLine.cs
blob: d757ed77e9708ed96e150fe1a7282f39449527bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) 2013-2021  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
//
// 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
    }
	/// <summary>
	/// represent a single line span with end of line handling.
	/// </summary>
	[DebuggerDisplay ("{Start}, {Length}, {LengthInPixel}")]
	public struct TextLine : IComparable<TextLine>
	{
		/// <summary>
		/// Line start absolute character position.
		/// </summary>
		public int Start;
		/// <summary>
		/// Line's character count not including linebreak if any.
		/// </summary>
		public int Length;
		/// <summary>
		/// Total line's Character count including linebreak characters if any.
		/// </summary>
		public int LengthIncludingLineBreak;
		/// <summary>
		/// Cached line's length in pixel. If not yet computed by renderer, value is '-1'
		/// </summary>
		public int LengthInPixel;
		/// <summary>
		/// Absolute end character position just before linebreak if any.
		/// </summary>
		public int End => Start + Length;
		public TextSpan Span => new TextSpan (Start, End);
		public TextSpan SpanIncludingLineBreak => new TextSpan (Start, EndIncludingLineBreak);
		/// <summary>
		/// Absolute line's end position after linebreak if any.
		/// </summary>
		public int EndIncludingLineBreak => Start + LengthIncludingLineBreak;
		/// <summary>
		/// Character count of the linebreak, 0 if no linebreak.
		/// </summary>
		public int LineBreakLength => LengthIncludingLineBreak - Length;
		/// <summary>
		/// True line has a linebreak, false otherwise.
		/// </summary>
		public bool HasLineBreak => LineBreakLength > 0;
		/// <summary>
		/// Create a new TextLine span using the absolute start and end character positions.
		/// </summary>
		public TextLine (int start, int end, int endIncludingLineBreak) {
			Start = start;
			Length = end - start;
			LengthIncludingLineBreak = endIncludingLineBreak - start;
			LengthInPixel = -1;
		}
		/// <summary>
		/// Create an empty line span without linebreak starting at absolute charater position given in argument.
		/// </summary>
		/// <param name="start"></param>
		public TextLine (int start) {
			Start = start;
			Length = 0;
			LengthIncludingLineBreak = 0;
			LengthInPixel = -1;
		}
		/// <summary>
		/// Set a new line's length and reset computed length in pixel to '-1'.
		/// </summary>
		/// <param name="newLength"></param>
		public void SetLength (int newLength) {
			LengthInPixel = -1;
			Length = newLength;
        }
		/// <summary>
		/// Create a new TextLine span with a start offset, length in pixel is reseted.
		/// </summary>
		/// <param name="start"></param>
		/// <returns></returns>
		public TextLine WithStartOffset (int start) => new TextLine (Start + start, End, EndIncludingLineBreak);
		public int CompareTo (TextLine other) => Start - other.Start;
    }
}