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

More Span<T> and ReadOnlySpan<T> #2669

Open
wants to merge 16 commits into
base: dev/reduce-arrays
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 18 additions & 18 deletions source/SkiaSharp.HarfBuzz/SkiaSharp.HarfBuzz/CanvasExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,60 @@ namespace SkiaSharp.HarfBuzz
{
public static class CanvasExtensions
{
[Obsolete("Use DrawShapedText(string text, SKPoint p, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
[Obsolete("Use DrawShapedText(ReadOnlySpan<char> text, SKPoint p, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
public static void DrawShapedText(this SKCanvas canvas, string text, SKPoint p, SKPaint paint) =>
canvas.DrawShapedText(text, p.X, p.Y, paint.TextAlign, paint.GetFont(), paint);
canvas.DrawShapedText(text.AsSpan(), p.X, p.Y, paint.TextAlign, paint.GetFont(), paint);

public static void DrawShapedText(this SKCanvas canvas, string text, SKPoint p, SKFont font, SKPaint paint) =>
public static void DrawShapedText(this SKCanvas canvas, ReadOnlySpan<char> text, SKPoint p, SKFont font, SKPaint paint) =>
#pragma warning disable CS0618 // Type or member is obsolete (TODO: replace paint.TextAlign with SKTextAlign.Left)
canvas.DrawShapedText(text, p.X, p.Y, paint.TextAlign, font, paint);
#pragma warning restore CS0618 // Type or member is obsolete

public static void DrawShapedText(this SKCanvas canvas, string text, SKPoint p, SKTextAlign textAlign, SKFont font, SKPaint paint) =>
public static void DrawShapedText(this SKCanvas canvas, ReadOnlySpan<char> text, SKPoint p, SKTextAlign textAlign, SKFont font, SKPaint paint) =>
canvas.DrawShapedText(text, p.X, p.Y, textAlign, font, paint);

[Obsolete("Use DrawShapedText(string text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
[Obsolete("Use DrawShapedText(ReadOnlySpan<char> text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
public static void DrawShapedText(this SKCanvas canvas, string text, float x, float y, SKPaint paint) =>
canvas.DrawShapedText(text, x, y, paint.TextAlign, paint.GetFont(), paint);
canvas.DrawShapedText(text.AsSpan(), x, y, paint.TextAlign, paint.GetFont(), paint);

public static void DrawShapedText(this SKCanvas canvas, string text, float x, float y, SKFont font, SKPaint paint) =>
public static void DrawShapedText(this SKCanvas canvas, ReadOnlySpan<char> text, float x, float y, SKFont font, SKPaint paint) =>
#pragma warning disable CS0618 // Type or member is obsolete (TODO: replace paint.TextAlign with SKTextAlign.Left)
canvas.DrawShapedText(text, x, y, paint.TextAlign, font, paint);
#pragma warning restore CS0618 // Type or member is obsolete

public static void DrawShapedText(this SKCanvas canvas, string text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint)
public static void DrawShapedText(this SKCanvas canvas, ReadOnlySpan<char> text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint)
{
if (string.IsNullOrEmpty(text))
if (text.IsEmpty)
return;

using var shaper = new SKShaper(font.Typeface);
canvas.DrawShapedText(shaper, text, x, y, textAlign, font, paint);
}

[Obsolete("Use DrawShapedText(SKShaper shaper, string text, SKPoint p, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
[Obsolete("Use DrawShapedText(SKShaper shaper, ReadOnlySpan<char> text, SKPoint p, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
public static void DrawShapedText(this SKCanvas canvas, SKShaper shaper, string text, SKPoint p, SKPaint paint) =>
canvas.DrawShapedText(shaper, text, p.X, p.Y, paint.TextAlign, paint.GetFont(), paint);
canvas.DrawShapedText(shaper, text.AsSpan(), p.X, p.Y, paint.TextAlign, paint.GetFont(), paint);

public static void DrawShapedText(this SKCanvas canvas, SKShaper shaper, string text, SKPoint p, SKFont font, SKPaint paint) =>
public static void DrawShapedText(this SKCanvas canvas, SKShaper shaper, ReadOnlySpan<char> text, SKPoint p, SKFont font, SKPaint paint) =>
#pragma warning disable CS0618 // Type or member is obsolete (TODO: replace paint.TextAlign with SKTextAlign.Left)
canvas.DrawShapedText(shaper, text, p.X, p.Y, paint.TextAlign, font, paint);
#pragma warning restore CS0618 // Type or member is obsolete

public static void DrawShapedText(this SKCanvas canvas, SKShaper shaper, string text, SKPoint p, SKTextAlign textAlign, SKFont font, SKPaint paint) =>
public static void DrawShapedText(this SKCanvas canvas, SKShaper shaper, ReadOnlySpan<char> text, SKPoint p, SKTextAlign textAlign, SKFont font, SKPaint paint) =>
canvas.DrawShapedText(shaper, text, p.X, p.Y, textAlign, font, paint);

[Obsolete("Use DrawShapedText(SKShaper shaper, string text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
[Obsolete("Use DrawShapedText(SKShaper shaper, ReadOnlySpan<char> text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
public static void DrawShapedText(this SKCanvas canvas, SKShaper shaper, string text, float x, float y, SKPaint paint) =>
canvas.DrawShapedText(shaper, text, x, y, paint.TextAlign, paint.GetFont(), paint);
canvas.DrawShapedText(shaper, text.AsSpan(), x, y, paint.TextAlign, paint.GetFont(), paint);

public static void DrawShapedText(this SKCanvas canvas, SKShaper shaper, string text, float x, float y, SKFont font, SKPaint paint) =>
public static void DrawShapedText(this SKCanvas canvas, SKShaper shaper, ReadOnlySpan<char> text, float x, float y, SKFont font, SKPaint paint) =>
#pragma warning disable CS0618 // Type or member is obsolete (TODO: replace paint.TextAlign with SKTextAlign.Left)
canvas.DrawShapedText(shaper, text, x, y, paint.TextAlign, font, paint);
#pragma warning restore CS0618 // Type or member is obsolete

public static void DrawShapedText(this SKCanvas canvas, SKShaper shaper, string text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint)
public static void DrawShapedText(this SKCanvas canvas, SKShaper shaper, ReadOnlySpan<char> text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint)
{
if (string.IsNullOrEmpty(text))
if (text.IsEmpty)
return;

if (canvas == null)
Expand Down
18 changes: 18 additions & 0 deletions source/SkiaSharp.HarfBuzz/SkiaSharp.HarfBuzz/SKShaper.cs
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason public Result Shape(string text, float xOffset, float yOffset, SKFont font) uses buffer.AddUtf8 instead of buffer.AddUtf16?

Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,24 @@ public Result Shape(string text, float xOffset, float yOffset, SKFont font)
return Shape(buffer, xOffset, yOffset, font);
}

public Result Shape(ReadOnlySpan<char> text, SKFont font) =>
Shape(text, 0, 0, font);

public Result Shape(ReadOnlySpan<char> text, float xOffset, float yOffset, SKFont font)
{
if (text.IsEmpty)
{
return new Result();
}

using var buffer = new Buffer();
buffer.AddUtf16(text);

buffer.GuessSegmentProperties();

return Shape(buffer, xOffset, yOffset, font);
}

public class Result
{
public Result()
Expand Down