Skip to content

Commit 12f54c7

Browse files
Copilotraffaeler
andauthored
Replace obsolete SkiaSharp APIs and bump to 3.119.2
Agent-Logs-Url: https://github.com/dotnet/iot/sessions/33137b22-78a0-413b-aa9e-7f13c14797f7 Co-authored-by: raffaeler <5183152+raffaeler@users.noreply.github.com>
1 parent c6dbed4 commit 12f54c7

3 files changed

Lines changed: 27 additions & 15 deletions

File tree

src/Iot.Device.Bindings.SkiaSharpAdapter/Iot.Device.Bindings.SkiaSharpAdapter.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
<ItemGroup>
2020
<ProjectReference Include="..\Iot.Device.Bindings\Iot.Device.Bindings.csproj" />
2121
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsLoggingAbstractionsPackageVersion)" />
22-
<PackageReference Include="SkiaSharp" Version="2.88.6" />
22+
<PackageReference Include="SkiaSharp" Version="3.119.2" />
2323
<!-- The NativeAssets packages for Windows and MacOS are included in the above by default -->
24-
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.6" />
24+
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="3.119.2" />
2525
<Content Include="$(RepoRoot)src/devices/README-nuget.md" Pack="true" Visible="false" PackagePath="\README.md" />
2626
</ItemGroup>
2727

src/devices/SkiaSharpAdapter/SkiaSharpAdapter.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="SkiaSharp" Version="2.88.8" />
18-
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.8" />
17+
<PackageReference Include="SkiaSharp" Version="3.119.2" />
18+
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="3.119.2" />
1919
</ItemGroup>
2020

2121
</Project>

src/devices/SkiaSharpAdapter/SkiaSharpGraphicsExtensions.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ public static BitmapImage Resize(this BitmapImage image, Size size)
2727
{
2828
if (image is SkiaSharpBitmap img)
2929
{
30-
var resized = img.WrappedBitmap.Resize(new SKSizeI(size.Width, size.Height), SKFilterQuality.Medium);
30+
// SKFilterQuality.Medium maps to a linear filter with a linear mipmap.
31+
// See https://api.skia.org/SkSamplingOptions_8h_source.html
32+
var sampling = new SKSamplingOptions(SKFilterMode.Linear, SKMipmapMode.Linear);
33+
var resized = img.WrappedBitmap.Resize(new SKSizeI(size.Width, size.Height), sampling);
3134
return new SkiaSharpBitmap(resized, image.PixelFormat);
3235
}
3336

@@ -77,11 +80,15 @@ public static void FillRectangle(this IGraphics graphics, Rectangle rectangle, C
7780
public static SizeF MeasureText(this IGraphics graphics, string text, string fontFamilyName, int size)
7881
{
7982
var canvas = GetCanvas(graphics);
80-
SKFont fnt = new SKFont(SKTypeface.FromFamilyName(fontFamilyName), size);
81-
var paint = new SKPaint(fnt);
82-
paint.TextAlign = SKTextAlign.Left;
83-
paint.TextEncoding = SKTextEncoding.Utf16;
84-
float width = paint.MeasureText(text);
83+
using SKTypeface typeface = SKTypeface.FromFamilyName(fontFamilyName);
84+
using SKFont fnt = new SKFont(typeface, size);
85+
// Match the previous behavior of SKPaint(fnt).MeasureText(text). The obsolete SKPaint
86+
// text path defaulted IsAntialias=false, so we mirror that by disabling font edging.
87+
// The string overload of MeasureText handles UTF-16 (the natural encoding of managed
88+
// strings), matching TextEncoding=Utf16; the previous TextAlign=Left did not affect
89+
// the returned width.
90+
fnt.Edging = SKFontEdging.Alias;
91+
float width = fnt.MeasureText(text);
8592
return new SizeF(width, size);
8693
}
8794

@@ -91,11 +98,14 @@ public static SizeF MeasureText(this IGraphics graphics, string text, string fon
9198
public static void DrawText(this IGraphics graphics, string text, string fontFamilyName, int size, Color color, Point position)
9299
{
93100
var canvas = GetCanvas(graphics);
94-
SKFont fnt = new SKFont(SKTypeface.FromFamilyName(fontFamilyName), size);
95-
var paint = new SKPaint(fnt);
101+
using SKTypeface typeface = SKTypeface.FromFamilyName(fontFamilyName);
102+
using SKFont fnt = new SKFont(typeface, size);
103+
// Match the previous behavior of canvas.DrawText(t, pt, new SKPaint(fnt) { ... }).
104+
// The obsolete SKPaint text path defaulted IsAntialias=false, which produced aliased
105+
// glyphs; mirror that by disabling font edging so output is pixel-identical.
106+
fnt.Edging = SKFontEdging.Alias;
107+
using var paint = new SKPaint();
96108
paint.Color = new SKColor((uint)color.ToArgb());
97-
paint.TextAlign = SKTextAlign.Left;
98-
paint.TextEncoding = SKTextEncoding.Utf16;
99109
int lineSpacing = size + 2;
100110
SKPoint currentPosition = new SKPoint(position.X, position.Y + size); // drawing begins to the right and above the given point.
101111
var texts = text.Split(new char[]
@@ -106,7 +116,9 @@ public static void DrawText(this IGraphics graphics, string text, string fontFam
106116
// The DrawText implementation of SkiaSharp does not work with line breaks, so do that manually.
107117
foreach (var t in texts)
108118
{
109-
canvas.DrawText(t, currentPosition, paint);
119+
// The string + SKFont overload uses UTF-16 encoding (natural for managed strings),
120+
// matching the previous TextEncoding=Utf16. SKTextAlign.Left preserves the original alignment.
121+
canvas.DrawText(t, currentPosition, SKTextAlign.Left, fnt, paint);
110122
currentPosition.Y += lineSpacing;
111123
}
112124
}

0 commit comments

Comments
 (0)