Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up SKTextBlobBuilder and SKRunBuffer APIs #2775

Merged
merged 4 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 85 additions & 45 deletions binding/SkiaSharp/SKRunBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#nullable disable

using System;
using System.ComponentModel;

namespace SkiaSharp
{
// Base

public unsafe class SKRunBuffer
{
internal readonly SKRunBufferInternal internalBuffer;
Expand All @@ -17,103 +18,142 @@ internal SKRunBuffer (SKRunBufferInternal buffer, int size)

public int Size { get; }

public Span<ushort> GetGlyphSpan () =>
new Span<ushort> (internalBuffer.glyphs, internalBuffer.glyphs == null ? 0 : Size);
public Span<ushort> Glyphs => new (internalBuffer.glyphs, Size);

public void SetGlyphs (ReadOnlySpan<ushort> glyphs) => glyphs.CopyTo (Glyphs);

public void SetGlyphs (ReadOnlySpan<ushort> glyphs) =>
glyphs.CopyTo (GetGlyphSpan ());
[Obsolete ("Use Glyphs instead.")]
public Span<ushort> GetGlyphSpan () => Glyphs;
}

public sealed unsafe class SKHorizontalRunBuffer : SKRunBuffer
{
internal SKHorizontalRunBuffer (SKRunBufferInternal buffer, int count)
: base (buffer, count)
internal SKHorizontalRunBuffer (SKRunBufferInternal buffer, int size)
: base (buffer, size)
{
}

public Span<float> GetPositionSpan () =>
new Span<float> (internalBuffer.pos, internalBuffer.pos == null ? 0 : Size);
public Span<float> Positions => new (internalBuffer.pos, Size);

public void SetPositions (ReadOnlySpan<float> positions) =>
positions.CopyTo (GetPositionSpan ());
public void SetPositions (ReadOnlySpan<float> positions) => positions.CopyTo (Positions);

[Obsolete ("Use Positions instead.")]
public Span<float> GetPositionSpan () => Positions;
}

public sealed unsafe class SKPositionedRunBuffer : SKRunBuffer
{
internal SKPositionedRunBuffer (SKRunBufferInternal buffer, int count)
: base (buffer, count)
internal SKPositionedRunBuffer (SKRunBufferInternal buffer, int size)
: base (buffer, size)
{
}

public Span<SKPoint> GetPositionSpan () =>
new Span<SKPoint> (internalBuffer.pos, internalBuffer.pos == null ? 0 : Size);
public Span<SKPoint> Positions => new (internalBuffer.pos, Size);

public void SetPositions (ReadOnlySpan<SKPoint> positions) => positions.CopyTo (Positions);

public void SetPositions (ReadOnlySpan<SKPoint> positions) =>
positions.CopyTo (GetPositionSpan ());
[Obsolete ("Use Positions instead.")]
public Span<SKPoint> GetPositionSpan () => Positions;
}

public sealed unsafe class SKRotationScaleRunBuffer : SKRunBuffer
{
internal SKRotationScaleRunBuffer (SKRunBufferInternal buffer, int count)
: base (buffer, count)
internal SKRotationScaleRunBuffer (SKRunBufferInternal buffer, int size)
: base (buffer, size)
{
}

public Span<SKRotationScaleMatrix> GetRotationScaleSpan () =>
new Span<SKRotationScaleMatrix> (internalBuffer.pos, Size);
public Span<SKRotationScaleMatrix> Positions => new (internalBuffer.pos, Size);

public void SetRotationScale (ReadOnlySpan<SKRotationScaleMatrix> positions) =>
positions.CopyTo (GetRotationScaleSpan ());
public void SetPositions (ReadOnlySpan<SKRotationScaleMatrix> positions) => positions.CopyTo (Positions);

[Obsolete ("Use Positions instead.")]
public Span<SKRotationScaleMatrix> GetRotationScaleSpan () => Positions;

[Obsolete ("Use SetPositions instead.")]
public void SetRotationScale (ReadOnlySpan<SKRotationScaleMatrix> positions) => SetPositions (positions);
}

// Text

public unsafe class SKTextRunBuffer : SKRunBuffer
{
internal SKTextRunBuffer (SKRunBufferInternal buffer, int count, int textSize)
: base (buffer, count)
internal SKTextRunBuffer (SKRunBufferInternal buffer, int size, int textSize)
: base (buffer, size)
{
TextSize = textSize;
}

public int TextSize { get; }

public Span<byte> GetTextSpan () =>
new Span<byte> (internalBuffer.utf8text, internalBuffer.utf8text == null ? 0 : TextSize);
public Span<byte> Text => new (internalBuffer.utf8text, TextSize);

public Span<uint> GetClusterSpan () =>
new Span<uint> (internalBuffer.clusters, internalBuffer.clusters == null ? 0 : Size);
public Span<uint> Clusters => new (internalBuffer.clusters, Size);

public void SetText (ReadOnlySpan<byte> text) =>
text.CopyTo (GetTextSpan ());
public void SetText (ReadOnlySpan<byte> text) => text.CopyTo (Text);

public void SetClusters (ReadOnlySpan<uint> clusters) =>
clusters.CopyTo (GetClusterSpan ());
public void SetClusters (ReadOnlySpan<uint> clusters) => clusters.CopyTo (Clusters);
}

public sealed unsafe class SKHorizontalTextRunBuffer : SKTextRunBuffer
{
internal SKHorizontalTextRunBuffer (SKRunBufferInternal buffer, int count, int textSize)
: base (buffer, count, textSize)
internal SKHorizontalTextRunBuffer (SKRunBufferInternal buffer, int size, int textSize)
: base (buffer, size, textSize)
{
}

public Span<float> GetPositionSpan () =>
new Span<float> (internalBuffer.pos, internalBuffer.pos == null ? 0 : Size);
public Span<float> Positions => new (internalBuffer.pos, Size);

public void SetPositions (ReadOnlySpan<float> positions) =>
positions.CopyTo (GetPositionSpan ());
public void SetPositions (ReadOnlySpan<float> positions) => positions.CopyTo (Positions);
}

public sealed unsafe class SKPositionedTextRunBuffer : SKTextRunBuffer
{
internal SKPositionedTextRunBuffer (SKRunBufferInternal buffer, int count, int textSize)
: base (buffer, count, textSize)
internal SKPositionedTextRunBuffer (SKRunBufferInternal buffer, int size, int textSize)
: base (buffer, size, textSize)
{
}

public Span<SKPoint> GetPositionSpan () =>
new Span<SKPoint> (internalBuffer.pos, internalBuffer.pos == null ? 0 : Size);
public Span<SKPoint> Positions => new (internalBuffer.pos, Size);

public void SetPositions (ReadOnlySpan<SKPoint> positions) => positions.CopyTo (Positions);
}

public sealed unsafe class SKRotationScaleTextRunBuffer : SKTextRunBuffer
{
internal SKRotationScaleTextRunBuffer (SKRunBufferInternal buffer, int size, int textSize)
: base (buffer, size, textSize)
{
}

public Span<SKRotationScaleMatrix> Positions => new (internalBuffer.pos, Size);

public void SetPositions (ReadOnlySpan<SKRotationScaleMatrix> positions) => positions.CopyTo (Positions);
}

// Raw / Struct

public unsafe readonly struct SKRawRunBuffer<T>
{
internal readonly SKRunBufferInternal buffer;
private readonly int size;
private readonly int posSize;
private readonly int textSize;

internal SKRawRunBuffer (SKRunBufferInternal buffer, int size, int posSize, int textSize)
{
this.buffer = buffer;
this.size = size;
this.posSize = posSize;
this.textSize = textSize;
}

public Span<ushort> Glyphs => new (buffer.glyphs, size);

public Span<T> Positions => new (buffer.pos, posSize);

public Span<byte> Text => new (buffer.utf8text, textSize);

public void SetPositions (ReadOnlySpan<SKPoint> positions) =>
positions.CopyTo (GetPositionSpan ());
public Span<uint> Clusters => new (buffer.clusters, size);
}
}
Loading
Loading