Skip to content
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
5 changes: 5 additions & 0 deletions samples/svgc/ImageSharpAssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ public float MeasureText(string? text, ShimSkiaSharp.SKPaint paint, ref ShimSkia
bounds = new ShimSkiaSharp.SKRect(0, -size * 0.8f, width, size * 0.2f);
return width;
}

public ShimSkiaSharp.SKPath? GetTextPath(string? text, ShimSkiaSharp.SKPaint paint, float x, float y)
{
return null;
}
}
6 changes: 6 additions & 0 deletions src/Svg.Controls.Avalonia/AvaloniaSvgAssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,10 @@ public float MeasureText(string? text, SKPaint paint, ref SKRect bounds)
bounds = new SKRect(0, -ascent, width, descent);
return width;
}

/// <inheritdoc />
public SKPath? GetTextPath(string? text, SKPaint paint, float x, float y)
{
return null;
}
}
3 changes: 3 additions & 0 deletions src/Svg.Model/Drawables/Elements/TextDrawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public sealed class TextDrawable : DrawableBase

public SKRect OwnerBounds { get; set; }

public SKPath? Path { get; private set; }

private TextDrawable(ISvgAssetLoader assetLoader, HashSet<Uri>? references)
: base(assetLoader, references)
{
Expand Down Expand Up @@ -87,6 +89,7 @@ private void Initialize()
var width = AssetLoader.MeasureText(text, paint, ref bounds);

GeometryBounds = new SKRect(x, y + metricsAscent, x + width, y + metricsDescent);
Path = AssetLoader.GetTextPath(text, paint, x, y);
Transform = TransformsService.ToMatrix(Text.Transforms);
}

Expand Down
1 change: 1 addition & 0 deletions src/Svg.Model/ISvgAssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public interface ISvgAssetLoader
List<TypefaceSpan> FindTypefaces(string? text, SKPaint paintPreferredTypeface);
SKFontMetrics GetFontMetrics(SKPaint paint);
float MeasureText(string? text, SKPaint paint, ref SKRect bounds);
SKPath? GetTextPath(string? text, SKPaint paint, float x, float y);
}
45 changes: 45 additions & 0 deletions src/Svg.Skia/SkiaModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@
};
}

public SkiaSharp.SKFilterQuality ToSKFilterQuality(SKFilterQuality filterQuality)

Check warning on line 918 in src/Svg.Skia/SkiaModel.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

'SKFilterQuality' is obsolete: 'Use SKSamplingOptions instead.'

Check warning on line 918 in src/Svg.Skia/SkiaModel.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

'SKFilterQuality' is obsolete: 'Use SKSamplingOptions instead.'

Check warning on line 918 in src/Svg.Skia/SkiaModel.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

'SKFilterQuality' is obsolete: 'Use SKSamplingOptions instead.'
{
return filterQuality switch
{
Expand Down Expand Up @@ -994,6 +994,15 @@
};
}

public SKPathFillType FromSKPathFillType(SkiaSharp.SKPathFillType pathFillType)
{
return pathFillType switch
{
SkiaSharp.SKPathFillType.EvenOdd => SKPathFillType.EvenOdd,
_ => SKPathFillType.Winding
};
}

public SkiaSharp.SKPathArcSize ToSKPathArcSize(SKPathArcSize pathArcSize)
{
return pathArcSize switch
Expand Down Expand Up @@ -1130,6 +1139,42 @@
return skPath;
}

public SKPath FromSKPath(SkiaSharp.SKPath skPath)
{
var path = new SKPath
{
FillType = FromSKPathFillType(skPath.FillType)
};

using var iter = skPath.CreateRawIterator();
var pts = new SkiaSharp.SKPoint[4];
while (true)
{
var verb = iter.Next(pts);
switch (verb)
{
case SkiaSharp.SKPathVerb.Move:
path.Commands?.Add(new MoveToPathCommand(pts[0].X, pts[0].Y));
break;
case SkiaSharp.SKPathVerb.Line:
path.Commands?.Add(new LineToPathCommand(pts[1].X, pts[1].Y));
break;
case SkiaSharp.SKPathVerb.Quad:
case SkiaSharp.SKPathVerb.Conic:
path.Commands?.Add(new QuadToPathCommand(pts[1].X, pts[1].Y, pts[2].X, pts[2].Y));
break;
case SkiaSharp.SKPathVerb.Cubic:
path.Commands?.Add(new CubicToPathCommand(pts[1].X, pts[1].Y, pts[2].X, pts[2].Y, pts[3].X, pts[3].Y));
break;
case SkiaSharp.SKPathVerb.Close:
path.Commands?.Add(new ClosePathCommand());
break;
case SkiaSharp.SKPathVerb.Done:
return path;
}
}
}

public SkiaSharp.SKPath? ToSKPath(ClipPath? clipPath)
{
if (clipPath?.Clips is null)
Expand Down
13 changes: 13 additions & 0 deletions src/Svg.Skia/SkiaSvgAssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@
{
var currentTypefaceText = text.Substring(currentTypefaceStartIndex, i - currentTypefaceStartIndex);

ret.Add(new(currentTypefaceText, runningPaint.MeasureText(currentTypefaceText),

Check warning on line 75 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

'SKPaint.MeasureText(string)' is obsolete: 'Use SKFont.MeasureText() instead.'

Check warning on line 75 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

'SKPaint.MeasureText(string)' is obsolete: 'Use SKFont.MeasureText() instead.'

Check warning on line 75 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

'SKPaint.MeasureText(string)' is obsolete: 'Use SKFont.MeasureText() instead.'
runningPaint.Typeface is null

Check warning on line 76 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'

Check warning on line 76 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'

Check warning on line 76 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'
? null
: ShimSkiaSharp.SKTypeface.FromFamilyName(
runningPaint.Typeface.FamilyName,

Check warning on line 79 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'

Check warning on line 79 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'

Check warning on line 79 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'
// SkiaSharp provides int properties here. Let's just assume our
// ShimSkiaSharp defines the same values as SkiaSharp and convert directly
(ShimSkiaSharp.SKFontStyleWeight)runningPaint.Typeface.FontWeight,

Check warning on line 82 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'

Check warning on line 82 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'

Check warning on line 82 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'
(ShimSkiaSharp.SKFontStyleWidth)runningPaint.Typeface.FontWidth,

Check warning on line 83 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'

Check warning on line 83 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'

Check warning on line 83 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'
(ShimSkiaSharp.SKFontStyleSlant)runningPaint.Typeface.FontSlant)

Check warning on line 84 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'

Check warning on line 84 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'

Check warning on line 84 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build ubuntu-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'
));
}

Expand All @@ -91,10 +91,10 @@

if (i == 0)
{
runningPaint.Typeface = typeface;

Check warning on line 94 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build windows-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'

Check warning on line 94 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'
}
else if (runningPaint.Typeface is null

Check warning on line 96 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'
&& typeface is { } || runningPaint.Typeface is { }

Check warning on line 97 in src/Svg.Skia/SkiaSvgAssetLoader.cs

View workflow job for this annotation

GitHub Actions / Build macos-latest

'SKPaint.Typeface' is obsolete: 'Use SKFont.Typeface instead.'
&& typeface is null || runningPaint.Typeface is { } l
&& typeface is { } r
&& (l.FamilyName, l.FontWeight, l.FontWidth, l.FontSlant) != (r.FamilyName, r.FontWeight, r.FontWidth, r.FontSlant))
Expand Down Expand Up @@ -151,4 +151,17 @@
bounds = new ShimSkiaSharp.SKRect(skBounds.Left, skBounds.Top, skBounds.Right, skBounds.Bottom);
return width;
}

/// <inheritdoc />
public ShimSkiaSharp.SKPath? GetTextPath(string? text, ShimSkiaSharp.SKPaint paint, float x, float y)
{
using var skPaint = _skiaModel.ToSKPaint(paint);
if (skPaint is null || text is null)
{
return null;
}

using var skPath = skPaint.GetTextPath(text, x, y);
return _skiaModel.FromSKPath(skPath);
}
}
5 changes: 5 additions & 0 deletions src/Svg.SourceGenerator.Skia/SkiaGeneratorSvgAssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ public float MeasureText(string? text, ShimSkiaSharp.SKPaint paint, ref ShimSkia
bounds = new ShimSkiaSharp.SKRect(0, -size * 0.8f, width, size * 0.2f);
return width;
}

public ShimSkiaSharp.SKPath? GetTextPath(string? text, ShimSkiaSharp.SKPaint paint, float x, float y)
{
return null;
}
}
Loading