Browse Source

[Text] Clone CodePoint ReadAt for Span

pull/9928/head
Sergey Mikolaitis 3 years ago
parent
commit
ba9bf5d364
  1. 68
      src/Avalonia.Base/Media/TextFormatting/Unicode/Codepoint.cs
  2. 2
      src/Avalonia.Base/Media/TextFormatting/Unicode/CodepointEnumerator.cs
  3. 2
      src/Avalonia.Base/Media/TextFormatting/Unicode/GraphemeEnumerator.cs
  4. 2
      tests/Avalonia.UnitTests/MockTextShaperImpl.cs

68
src/Avalonia.Base/Media/TextFormatting/Unicode/Codepoint.cs

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Avalonia.Media.TextFormatting.Unicode
@ -222,6 +223,71 @@ namespace Avalonia.Media.TextFormatting.Unicode
return new Codepoint(code);
}
/// <summary>
/// Reads the <see cref="Codepoint"/> at specified position.
/// </summary>
/// <param name="text">The buffer to read from.</param>
/// <param name="index">The index to read at.</param>
/// <param name="count">The count of character that were read.</param>
/// <returns></returns>
public static Codepoint ReadAt(ReadOnlySpan<char> text, int index, out int count)
{
count = 1;
if (index >= text.Length)
{
return ReplacementCodepoint;
}
var code = text[index];
ushort hi, low;
//# High surrogate
if (0xD800 <= code && code <= 0xDBFF)
{
hi = code;
if (index + 1 == text.Length)
{
return ReplacementCodepoint;
}
low = text[index + 1];
if (0xDC00 <= low && low <= 0xDFFF)
{
count = 2;
return new Codepoint((uint)((hi - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000));
}
return ReplacementCodepoint;
}
//# Low surrogate
if (0xDC00 <= code && code <= 0xDFFF)
{
if (index == 0)
{
return ReplacementCodepoint;
}
hi = text[index - 1];
low = code;
if (0xD800 <= hi && hi <= 0xDBFF)
{
count = 2;
return new Codepoint((uint)((hi - 0xD800) * 0x400 + (low - 0xDC00) + 0x10000));
}
return ReplacementCodepoint;
}
return new Codepoint(code);
}
/// <summary>
/// Returns <see langword="true"/> if <paramref name="cp"/> is between

2
src/Avalonia.Base/Media/TextFormatting/Unicode/CodepointEnumerator.cs

@ -30,7 +30,7 @@ namespace Avalonia.Media.TextFormatting.Unicode
return false;
}
Current = Codepoint.ReadAt(_text, 0, out var count);
Current = Codepoint.ReadAt(_text.Span, 0, out var count);
_text = _text.Skip(count);

2
src/Avalonia.Base/Media/TextFormatting/Unicode/GraphemeEnumerator.cs

@ -245,7 +245,7 @@ namespace Avalonia.Media.TextFormatting.Unicode
if (CurrentCodeUnitOffset < _buffer.Length)
{
CurrentCodepoint = Codepoint.ReadAt(_buffer, CurrentCodeUnitOffset,
CurrentCodepoint = Codepoint.ReadAt(_buffer.Span, CurrentCodeUnitOffset,
out _codeUnitLengthOfCurrentScalar);
}
else

2
tests/Avalonia.UnitTests/MockTextShaperImpl.cs

@ -18,7 +18,7 @@ namespace Avalonia.UnitTests
{
var glyphCluster = i + text.OffsetToFirstChar;
var codepoint = Codepoint.ReadAt(characterBufferRange, i, out var count);
var codepoint = Codepoint.ReadAt(characterBufferRange.Span, i, out var count);
var glyphIndex = typeface.GetGlyph(codepoint);

Loading…
Cancel
Save