diff options
| author | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2025-03-04 13:52:08 +0100 |
|---|---|---|
| committer | Jean-Philippe Bruyère <jp_bruyere@hotmail.com> | 2025-03-04 13:52:08 +0100 |
| commit | 54687bd5dcf322096a8c8cf8dbc1374cb5ce49f9 (patch) | |
| tree | 17a2ae896970b47d6b429bc1ac3bbe5d1ba7b94b | |
| parent | 8970e6cecac4f7fb9eead2ae26d0a996c46d1590 (diff) | |
move textBuffer to drawing2d and several other text classes and measure
| -rw-r--r-- | Crow/src/ExtensionsMethods.cs | 33 | ||||
| -rw-r--r-- | Drawing2D/src/CharLocation.cs (renamed from Crow/src/Text/CharLocation.cs) | 0 | ||||
| -rw-r--r-- | Drawing2D/src/ExtensionsMethods.cs (renamed from Crow/src/Text/Extensions.cs) | 38 | ||||
| -rw-r--r-- | Drawing2D/src/LineSpan.cs (renamed from Crow/src/Text/LineSpan.cs) | 0 | ||||
| -rw-r--r-- | Drawing2D/src/Measure.cs (renamed from Crow/src/2d/Measure.cs) | 0 | ||||
| -rw-r--r-- | Drawing2D/src/TextBuffer.cs | 99 | ||||
| -rw-r--r-- | Drawing2D/src/TextChange.cs (renamed from Crow/src/Text/TextChange.cs) | 0 | ||||
| -rw-r--r-- | Drawing2D/src/TextLine.cs (renamed from Crow/src/Text/TextLine.cs) | 0 | ||||
| -rw-r--r-- | Drawing2D/src/TextLineCollection.cs (renamed from Crow/src/Text/TextLineCollection.cs) | 0 | ||||
| -rw-r--r-- | Drawing2D/src/TextSpan.cs (renamed from Crow/src/Text/TextSpan.cs) | 0 |
10 files changed, 134 insertions, 36 deletions
diff --git a/Crow/src/ExtensionsMethods.cs b/Crow/src/ExtensionsMethods.cs index ed226b8a..302caec9 100644 --- a/Crow/src/ExtensionsMethods.cs +++ b/Crow/src/ExtensionsMethods.cs @@ -179,32 +179,14 @@ namespace Crow } return Alignment.Center; } - public static void Raise(this EventHandler handler, object sender, EventArgs e) - { - handler?.Invoke (sender, e); - } - public static void Raise<T>(this EventHandler<T> handler, object sender, T e) - { - handler?.Invoke (sender, e); - } - public static byte[] GetBytes(this string str) - { - byte[] bytes = new byte[str.Length * sizeof(char)]; - System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); - return bytes; - } - public static bool IsWhiteSpaceOrNewLine (this char c) - { - return c == '\t' || c.IsAnyLineBreakCharacter() || char.IsWhiteSpace (c); - } - public static object GetDefaultValue(this object obj) + /*public static object GetDefaultValue(this object obj) { Type t = obj.GetType (); if (t.IsValueType) return Activator.CreateInstance (t); return null; - } + }*/ public static FileSystemInfo [] GetFileSystemInfosOrdered (this DirectoryInfo di) { try { @@ -213,17 +195,6 @@ namespace Crow return null; } } - - internal static bool IsAnyLineBreakCharacter (this char c) - => c == '\n' || c == '\r' || c == '\u0085' || c == '\u2028' || c == '\u2029'; - - public static bool TryGetResource (this Assembly a, string resId, out Stream stream) { - stream = null; - if (a == null) - return false; - stream = a.GetManifestResourceStream (resId); - return stream != null; - } } } diff --git a/Crow/src/Text/CharLocation.cs b/Drawing2D/src/CharLocation.cs index 345f8fa3..345f8fa3 100644 --- a/Crow/src/Text/CharLocation.cs +++ b/Drawing2D/src/CharLocation.cs diff --git a/Crow/src/Text/Extensions.cs b/Drawing2D/src/ExtensionsMethods.cs index 77360ddd..dba42420 100644 --- a/Crow/src/Text/Extensions.cs +++ b/Drawing2D/src/ExtensionsMethods.cs @@ -1,12 +1,40 @@ -// Copyright (c) 2013-2021 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> +// Copyright (c) 2013-2022 Jean-Philippe Bruyère <jp_bruyere@hotmail.com> // // This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + +using Crow.Text; using System; +using System.IO; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; + +using Drawing2D; -namespace Crow.Text +namespace Crow { - public static class Extensions - { + public static class ExtensionsMethods + { + public static void Raise(this EventHandler handler, object sender, EventArgs e) + { + handler?.Invoke (sender, e); + } + public static void Raise<T>(this EventHandler<T> handler, object sender, T e) + { + handler?.Invoke (sender, e); + } + public static byte[] GetBytes(this string str) + { + byte[] bytes = new byte[str.Length * sizeof(char)]; + System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); + return bytes; + } + public static bool IsWhiteSpaceOrNewLine (this char c) + { + return c == '\t' || c.IsAnyLineBreakCharacter() || char.IsWhiteSpace (c); + } + internal static bool IsAnyLineBreakCharacter (this char c) + => c == '\n' || c == '\r' || c == '\u0085' || c == '\u2028' || c == '\u2029'; public static ReadOnlySpan<char> GetLine (this ReadOnlySpan<char> str, TextLine ls) { if (ls.Start >= str.Length) return "".AsSpan (); @@ -47,6 +75,6 @@ namespace Crow.Text return "\r\n".AsSpan (); } } - } } + diff --git a/Crow/src/Text/LineSpan.cs b/Drawing2D/src/LineSpan.cs index 1dc90de6..1dc90de6 100644 --- a/Crow/src/Text/LineSpan.cs +++ b/Drawing2D/src/LineSpan.cs diff --git a/Crow/src/2d/Measure.cs b/Drawing2D/src/Measure.cs index 4b66a975..4b66a975 100644 --- a/Crow/src/2d/Measure.cs +++ b/Drawing2D/src/Measure.cs diff --git a/Drawing2D/src/TextBuffer.cs b/Drawing2D/src/TextBuffer.cs new file mode 100644 index 00000000..ff85e2ff --- /dev/null +++ b/Drawing2D/src/TextBuffer.cs @@ -0,0 +1,99 @@ +// Copyright (c) 2021-2025 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.Linq; +using System.Collections.Generic; +using Crow.Text; +using Crow; + +namespace CrowEditBase +{ + public class TextBuffer { + static int bufferExpension = 100; + int length; + Memory<char> buffer; + ReadOnlyMemory<char> origBuffer; + LineCollection lines; + public bool mixedLineBreak = false; + public string lineBreak = null; + + internal LineCollection GetLineListCopy() => new LineCollection(lines.ToArray()); + public Span<char> Span => buffer.Span.Slice(0, length); + public ReadOnlySpan<char> ReadOnlySpan => buffer.Span.Slice(0, length); + public bool IsEmpty => length == 0; + public bool IsDirty => !origBuffer.Span.Equals(Span, StringComparison.Ordinal); + public int LinesCount => lines.Count; + public int Length => length; + public ReadOnlyMemory<char> ReadOnlyCopy { + get { + return ReadOnlySpan.ToArray(); + } + } + public void ResetDirtyState () { + origBuffer = Span.ToArray(); + } + public TextBuffer(ReadOnlySpan<char> origText) { + length = origText.Length; + buffer = new char[length + bufferExpension]; + origText.CopyTo(buffer.Span); + lines = new LineCollection (10); + if (IsEmpty) + lines.Add (new TextLine (0, 0, 0)); + else + lines.Update (Span); + ResetDirtyState (); + } + public void Update (TextChange change) { + ReadOnlySpan<char> orig = buffer.Span; + char[] newBuff = null; + Span<char> tmp; + if (buffer.Length < length + change.CharDiff) { + newBuff = new char[length + change.CharDiff + bufferExpension]; + tmp = newBuff; + orig.Slice(0, change.Start).CopyTo(tmp); + if (change.CharDiff == 0) + orig.Slice(change.End, length - change.End).CopyTo(tmp.Slice(change.End)); + } else + tmp = buffer.Span; + + if (change.CharDiff != 0) + orig.Slice(change.End, length - change.End).CopyTo(tmp.Slice(change.End2)); + if (!string.IsNullOrEmpty (change.ChangedText)) + change.ChangedText.AsSpan ().CopyTo (tmp.Slice (change.Start)); + + if (newBuff != null) + buffer = newBuff; + length += change.CharDiff; + + lines.Update (change); + } + public string GetLineBreak () { + if (string.IsNullOrEmpty (lineBreak)) { + mixedLineBreak = false; + + if (lines.Count == 0 || lines[0].LineBreakLength == 0) + lineBreak = Environment.NewLine; + else { + lineBreak = ReadOnlySpan.GetLineBreak (lines[0]).ToString (); + for (int i = 1; i < lines.Count; i++) { + if (!ReadOnlySpan.GetLineBreak (lines[i]).SequenceEqual (lineBreak)) { + mixedLineBreak = true; + break; + } + } + } + } + return lineBreak; + } + public CharLocation GetLocation (int absolutePosition) => lines.GetLocation (absolutePosition); + public TextLine GetLine (int index) => lines[index]; + public void SetLine (int index, TextLine line) => lines[index] = line; + public ReadOnlySpan<char> GetText (TextLine line) => GetText(line.Span); + public ReadOnlySpan<char> GetText (TextSpan textSpan) => buffer.Span.Slice(textSpan.Start, textSpan.Length); + public int GetAbsolutePosition (CharLocation loc) => lines.GetAbsolutePosition (loc); + public CharLocation EndLocation => new CharLocation (lines.Count - 1, lines[lines.Count - 1].Length); + public override string ToString() => ReadOnlySpan.ToString(); + } +}
\ No newline at end of file diff --git a/Crow/src/Text/TextChange.cs b/Drawing2D/src/TextChange.cs index 6533a9b4..6533a9b4 100644 --- a/Crow/src/Text/TextChange.cs +++ b/Drawing2D/src/TextChange.cs diff --git a/Crow/src/Text/TextLine.cs b/Drawing2D/src/TextLine.cs index d757ed77..d757ed77 100644 --- a/Crow/src/Text/TextLine.cs +++ b/Drawing2D/src/TextLine.cs diff --git a/Crow/src/Text/TextLineCollection.cs b/Drawing2D/src/TextLineCollection.cs index 5031496b..5031496b 100644 --- a/Crow/src/Text/TextLineCollection.cs +++ b/Drawing2D/src/TextLineCollection.cs diff --git a/Crow/src/Text/TextSpan.cs b/Drawing2D/src/TextSpan.cs index 83fbc68d..83fbc68d 100644 --- a/Crow/src/Text/TextSpan.cs +++ b/Drawing2D/src/TextSpan.cs |