|
@@ -1,17 +1,24 @@
|
|
|
using System;
|
|
|
+using System.Buffers;
|
|
|
using System.Collections.Generic;
|
|
|
using Avalonia.Utilities;
|
|
|
|
|
|
namespace Avalonia.Media.TextFormatting
|
|
|
{
|
|
|
- public sealed class ShapedBuffer : IList<GlyphInfo>
|
|
|
+ public sealed class ShapedBuffer : IList<GlyphInfo>, IDisposable
|
|
|
{
|
|
|
private static readonly IComparer<GlyphInfo> s_clusterComparer = new CompareClusters();
|
|
|
+ private bool _bufferRented;
|
|
|
|
|
|
public ShapedBuffer(CharacterBufferRange characterBufferRange, int bufferLength, IGlyphTypeface glyphTypeface, double fontRenderingEmSize, sbyte bidiLevel) :
|
|
|
- this(characterBufferRange, new GlyphInfo[bufferLength], glyphTypeface, fontRenderingEmSize, bidiLevel)
|
|
|
+ this(characterBufferRange,
|
|
|
+ new ArraySlice<GlyphInfo>(ArrayPool<GlyphInfo>.Shared.Rent(bufferLength), 0, bufferLength),
|
|
|
+ glyphTypeface,
|
|
|
+ fontRenderingEmSize,
|
|
|
+ bidiLevel)
|
|
|
{
|
|
|
-
|
|
|
+ _bufferRented = true;
|
|
|
+ Length = bufferLength;
|
|
|
}
|
|
|
|
|
|
internal ShapedBuffer(CharacterBufferRange characterBufferRange, ArraySlice<GlyphInfo> glyphInfos, IGlyphTypeface glyphTypeface, double fontRenderingEmSize, sbyte bidiLevel)
|
|
@@ -21,11 +28,12 @@ namespace Avalonia.Media.TextFormatting
|
|
|
GlyphTypeface = glyphTypeface;
|
|
|
FontRenderingEmSize = fontRenderingEmSize;
|
|
|
BidiLevel = bidiLevel;
|
|
|
+ Length = GlyphInfos.Length;
|
|
|
}
|
|
|
|
|
|
internal ArraySlice<GlyphInfo> GlyphInfos { get; }
|
|
|
|
|
|
- public int Length => GlyphInfos.Length;
|
|
|
+ public int Length { get; }
|
|
|
|
|
|
public IGlyphTypeface GlyphTypeface { get; }
|
|
|
|
|
@@ -260,6 +268,23 @@ namespace Avalonia.Media.TextFormatting
|
|
|
|
|
|
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
|
|
|
}
|
|
|
+
|
|
|
+ public void Dispose()
|
|
|
+ {
|
|
|
+ GC.SuppressFinalize(this);
|
|
|
+ if (_bufferRented)
|
|
|
+ {
|
|
|
+ GlyphInfos.ReturnRent();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ~ShapedBuffer()
|
|
|
+ {
|
|
|
+ if (_bufferRented)
|
|
|
+ {
|
|
|
+ GlyphInfos.ReturnRent();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public readonly record struct GlyphInfo
|