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 /Drawing2D/src/ExtensionsMethods.cs | |
| parent | 8970e6cecac4f7fb9eead2ae26d0a996c46d1590 (diff) | |
move textBuffer to drawing2d and several other text classes and measure
Diffstat (limited to 'Drawing2D/src/ExtensionsMethods.cs')
| -rw-r--r-- | Drawing2D/src/ExtensionsMethods.cs | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/Drawing2D/src/ExtensionsMethods.cs b/Drawing2D/src/ExtensionsMethods.cs new file mode 100644 index 00000000..dba42420 --- /dev/null +++ b/Drawing2D/src/ExtensionsMethods.cs @@ -0,0 +1,80 @@ +// 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 +{ + 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 (); + return str.Slice (ls.Start, ls.Length); + } + public static ReadOnlySpan<char> GetLine (this ReadOnlySpan<char> str, TextLine ls, int offset) { + int start = ls.Start + offset; + if (start >= str.Length) + return "".AsSpan (); + return str.Slice (start, ls.Length); + } + public static ReadOnlySpan<char> GetLineIncludingLineBreak (this string str, TextLine ls) { + if (ls.Start >= str.Length) + return "".AsSpan (); + return str.AsSpan ().Slice (ls.Start, ls.LengthIncludingLineBreak); + } + public static ReadOnlySpan<char> GetLineBreak (this ReadOnlySpan<char> str, TextLine ls) { + if (ls.LineBreakLength == 0) + return "".AsSpan (); + return str.Slice (ls.End, ls.LineBreakLength); + } + public static ReadOnlySpan<char> GetLineIncludingLineBreak (this string str, TextLine ls, int offset) { + int start = ls.Start + offset; + if (start >= str.Length) + return "".AsSpan (); + return str.AsSpan ().Slice (start, ls.LengthIncludingLineBreak); + } + + public static ReadOnlySpan<char> ToCharSpan (this LineBreakKind lineBreak) { + switch (lineBreak) { + case LineBreakKind.Unix: + return "\n".AsSpan (); + case LineBreakKind.Windows: + return "\r\n".AsSpan (); + case LineBreakKind.Other: + return "\r".AsSpan (); + default: + return "\r\n".AsSpan (); + } + } + } +} + |