Skip to content

Commit

Permalink
Fix the what the IgnorePixelScaling property works (#1804)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow committed Sep 12, 2021
1 parent 5d1f324 commit 4a687a8
Show file tree
Hide file tree
Showing 83 changed files with 683 additions and 325 deletions.
38 changes: 38 additions & 0 deletions binding/Binding/SKCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public unsafe class SKCanvas : SKObject
{
private const int PatchCornerCount = 4;
private const int PatchCubicsCount = 12;
private const double RadiansCircle = 2.0 * Math.PI;
private const double DegreesCircle = 360.0;

internal SKCanvas (IntPtr handle, bool owns)
: base (handle, owns)
Expand Down Expand Up @@ -117,33 +119,51 @@ public void RestoreToCount (int count)

public void Translate (float dx, float dy)
{
if (dx == 0 && dy == 0)
return;

SkiaApi.sk_canvas_translate (Handle, dx, dy);
}

public void Translate (SKPoint point)
{
if (point.IsEmpty)
return;

SkiaApi.sk_canvas_translate (Handle, point.X, point.Y);
}

// Scale

public void Scale (float s)
{
if (s == 1)
return;

SkiaApi.sk_canvas_scale (Handle, s, s);
}

public void Scale (float sx, float sy)
{
if (sx == 1 && sy == 1)
return;

SkiaApi.sk_canvas_scale (Handle, sx, sy);
}

public void Scale (SKPoint size)
{
if (size.IsEmpty)
return;

SkiaApi.sk_canvas_scale (Handle, size.X, size.Y);
}

public void Scale (float sx, float sy, float px, float py)
{
if (sx == 1 && sy == 1)
return;

Translate (px, py);
Scale (sx, sy);
Translate (-px, -py);
Expand All @@ -153,23 +173,35 @@ public void Scale (float sx, float sy, float px, float py)

public void RotateDegrees (float degrees)
{
if (degrees % DegreesCircle == 0)
return;

SkiaApi.sk_canvas_rotate_degrees (Handle, degrees);
}

public void RotateRadians (float radians)
{
if (radians % RadiansCircle == 0)
return;

SkiaApi.sk_canvas_rotate_radians (Handle, radians);
}

public void RotateDegrees (float degrees, float px, float py)
{
if (degrees % DegreesCircle == 0)
return;

Translate (px, py);
RotateDegrees (degrees);
Translate (-px, -py);
}

public void RotateRadians (float radians, float px, float py)
{
if (radians % RadiansCircle == 0)
return;

Translate (px, py);
RotateRadians (radians);
Translate (-px, -py);
Expand All @@ -179,11 +211,17 @@ public void RotateRadians (float radians, float px, float py)

public void Skew (float sx, float sy)
{
if (sx == 0 && sy == 0)
return;

SkiaApi.sk_canvas_skew (Handle, sx, sy);
}

public void Skew (SKPoint skew)
{
if (skew.IsEmpty)
return;

SkiaApi.sk_canvas_skew (Handle, skew.X, skew.Y);
}

Expand Down
7 changes: 7 additions & 0 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ Task ("samples")
{ "tvos", isMac },
{ "uwp", isWin },
{ "winui", isWin },
{ "wapproj", isWin },
{ "msix", isWin },
{ "watchos", isMac },
{ "wpf", isWin },
};
Expand All @@ -433,6 +435,7 @@ Task ("samples")
{ "tvos", "iPhoneSimulator" },
{ "uwp", "x86" },
{ "winui", "x64" },
{ "wapproj", "x64" },
{ "watchos", "iPhoneSimulator" },
{ "xamarin.forms.mac", "iPhone" },
{ "xamarin.forms.windows", "x86" },
Expand All @@ -458,8 +461,12 @@ Task ("samples")
buildPlatform = platformMatrix [platform];
}
Information ($"Building {sln} ({platform})...");
RunNuGetRestorePackagesConfig (sln);
RunMSBuild (sln, platform: buildPlatform);
} else {
Information ($"Skipping {sln} ({platform})...");
}
}
Expand Down
9 changes: 1 addition & 8 deletions samples/Basic/Android/SkiaSharpSample/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
// the the canvas and properties
var canvas = e.Surface.Canvas;

// get the screen density for scaling
var scale = Resources.DisplayMetrics.Density;
var scaledSize = new SKSize(e.Info.Width / scale, e.Info.Height / scale);

// handle the device screen density
canvas.Scale(scale);

// make sure the canvas is blank
canvas.Clear(SKColors.White);

Expand All @@ -59,7 +52,7 @@ private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
TextAlign = SKTextAlign.Center,
TextSize = 24
};
var coord = new SKPoint(scaledSize.Width / 2, (scaledSize.Height + paint.TextSize) / 2);
var coord = new SKPoint(e.Info.Width / 2, (e.Info.Height + paint.TextSize) / 2);
canvas.DrawText("SkiaSharp", coord, paint);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SkiaSharp.Views.Android.SKCanvasView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:ignorePixelScaling="true"
android:id="@+id/skiaView" />
</FrameLayout>
9 changes: 1 addition & 8 deletions samples/Basic/Desktop/SkiaSharpSample/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ private void skiaView_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
// the the canvas and properties
var canvas = e.Surface.Canvas;

// get the screen density for scaling
var scale = 1f;
var scaledSize = new SKSize(e.Info.Width / scale, e.Info.Height / scale);

// handle the device screen density
canvas.Scale(scale);

// make sure the canvas is blank
canvas.Clear(SKColors.White);

Expand All @@ -36,7 +29,7 @@ private void skiaView_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
TextAlign = SKTextAlign.Center,
TextSize = 24
};
var coord = new SKPoint(scaledSize.Width / 2, (scaledSize.Height + paint.TextSize) / 2);
var coord = new SKPoint(e.Info.Width / 2, (e.Info.Height + paint.TextSize) / 2);
canvas.DrawText("SkiaSharp", coord, paint);
}
}
Expand Down
12 changes: 2 additions & 10 deletions samples/Basic/ElmSharp/SkiaSharpSample/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private void Initialize()
window.Show();

var skiaView = new SKCanvasView(window);
skiaView.IgnorePixelScaling = true;
skiaView.PaintSurface += OnPaintSurface;
skiaView.Show();

Expand All @@ -43,18 +44,9 @@ private void Initialize()

private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
var skiaView = sender as SKCanvasView;

// the the canvas and properties
var canvas = e.Surface.Canvas;

// get the screen density for scaling
var scale = (float)ScalingInfo.ScalingFactor;
var scaledSize = new SKSize(e.Info.Width / scale, e.Info.Height / scale);

// handle the device screen density
canvas.Scale(scale);

// make sure the canvas is blank
canvas.Clear(SKColors.White);

Expand All @@ -67,7 +59,7 @@ private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
TextAlign = SKTextAlign.Center,
TextSize = 24
};
var coord = new SKPoint(scaledSize.Width / 2, (scaledSize.Height + paint.TextSize) / 2);
var coord = new SKPoint(e.Info.Width / 2, (e.Info.Height + paint.TextSize) / 2);
canvas.DrawText("SkiaSharp", coord, paint);
}

Expand Down
9 changes: 1 addition & 8 deletions samples/Basic/Gtk/SkiaSharpSample/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
// the the canvas and properties
var canvas = e.Surface.Canvas;

// get the screen density for scaling
var scale = 1f;
var scaledSize = new SKSize(e.Info.Width / scale, e.Info.Height / scale);

// handle the device screen density
canvas.Scale(scale);

// make sure the canvas is blank
canvas.Clear(SKColors.White);

Expand All @@ -43,7 +36,7 @@ private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
TextAlign = SKTextAlign.Center,
TextSize = 24
};
var coord = new SKPoint(scaledSize.Width / 2, (scaledSize.Height + paint.TextSize) / 2);
var coord = new SKPoint(e.Info.Width / 2, (e.Info.Height + paint.TextSize) / 2);
canvas.DrawText("SkiaSharp", coord, paint);
}
}
9 changes: 1 addition & 8 deletions samples/Basic/Gtk3/SkiaSharpSample/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@ private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
// the the canvas and properties
var canvas = e.Surface.Canvas;

// get the screen density for scaling
var scale = 1f;
var scaledSize = new SKSize(e.Info.Width / scale, e.Info.Height / scale);

// handle the device screen density
canvas.Scale(scale);

// make sure the canvas is blank
canvas.Clear(SKColors.White);

Expand All @@ -56,7 +49,7 @@ private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
TextAlign = SKTextAlign.Center,
TextSize = 24
};
var coord = new SKPoint(scaledSize.Width / 2, (scaledSize.Height + paint.TextSize) / 2);
var coord = new SKPoint(e.Info.Width / 2, (e.Info.Height + paint.TextSize) / 2);
canvas.DrawText("SkiaSharp", coord, paint);
}
}
Expand Down
3 changes: 2 additions & 1 deletion samples/Basic/Maui/SkiaSharpSample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
BackgroundColor="White">

<skia:SKCanvasView x:Name="skiaView" PaintSurface="OnPaintSurface"
EnableTouchEvents="True" Touch="OnTouch" />
EnableTouchEvents="True" Touch="OnTouch"
IgnorePixelScaling="True" />

</ContentPage>
10 changes: 2 additions & 8 deletions samples/Basic/Maui/SkiaSharpSample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
// the the canvas and properties
var canvas = e.Surface.Canvas;

// get the screen density for scaling
var scale = (float)(e.Info.Width / skiaView.Width);

// handle the device screen density
canvas.Scale(scale);

// make sure the canvas is blank
canvas.Clear(SKColors.White);

Expand All @@ -51,8 +45,8 @@ private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)

// adjust the location based on the pointer
var coord = (touchLocation is SKPoint loc)
? new SKPoint(loc.X / scale, loc.Y / scale)
: new SKPoint((float)skiaView.Width / 2, ((float)skiaView.Height + paint.TextSize) / 2);
? new SKPoint(loc.X, loc.Y)
: new SKPoint(e.Info.Width / 2, (e.Info.Height + paint.TextSize) / 2);

// draw some text
canvas.DrawText("SkiaSharp", coord, paint);
Expand Down
10 changes: 5 additions & 5 deletions samples/Basic/Maui/SkiaSharpSample/SkiaSharpSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
<TargetFrameworks>net6.0-ios;net6.0-maccatalyst;net6.0-android</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows')) and '$(MSBuildRuntimeType)' == 'Full'">$(TargetFrameworks);net6.0-windows10.0.19041</TargetFrameworks>
<OutputType>Exe</OutputType>
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<EnablePreviewMsixTooling>true</EnablePreviewMsixTooling>
<RootNamespace>SkiaSharpSample</RootNamespace>
<ApplicationTitle>SkiaSharpSample</ApplicationTitle>
<ApplicationId>com.companyname.SkiaSharpSample</ApplicationId>
<ApplicationVersion>1.0</ApplicationVersion>
<AndroidVersionCode>1</AndroidVersionCode>
<UseMaui>true</UseMaui>
<LangVersion>9.0</LangVersion>
<UseInterpreter Condition="'$(Configuration)' == 'Debug'">True</UseInterpreter>
</PropertyGroup>
Expand Down Expand Up @@ -39,10 +40,9 @@
</None>
</ItemGroup>

<PropertyGroup>
<EnablePreviewMsixTooling>true</EnablePreviewMsixTooling>
<AppxPackageRecipe>bin\$(Configuration)\net6.0-windows10.0.19041\win-x64\SkiaSharpSample.build.appxrecipe</AppxPackageRecipe>
<RuntimeIdentifier Condition="$(TargetFramework.Contains('-windows'))">win-x64</RuntimeIdentifier>
<PropertyGroup Condition="$(TargetFramework.Contains('-windows'))">
<OutputType>WinExe</OutputType>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>

<Import Project="..\..\..\..\output\SkiaSharp\nuget\build\$(TargetFramework)\SkiaSharp.Local.targets" Condition="Exists('..\..\..\..\output\SkiaSharp\nuget\build\$(TargetFramework)\SkiaSharp.Local.targets')" />
Expand Down
2 changes: 1 addition & 1 deletion samples/Basic/NetCore/WPF/SkiaSharpSample/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
mc:Ignorable="d"
Title="SkiaSharp" Height="350" Width="525" Icon="/icon.ico">
<Grid>
<skia:SKElement PaintSurface="OnPaintSurface" />
<skia:SKElement PaintSurface="OnPaintSurface" IgnorePixelScaling="True" />
</Grid>
</Window>
9 changes: 1 addition & 8 deletions samples/Basic/NetCore/WPF/SkiaSharpSample/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
// the the canvas and properties
var canvas = e.Surface.Canvas;

// get the screen density for scaling
var scale = (float)PresentationSource.FromVisual(this).CompositionTarget.TransformToDevice.M11;
var scaledSize = new SKSize(e.Info.Width / scale, e.Info.Height / scale);

// handle the device screen density
canvas.Scale(scale);

// make sure the canvas is blank
canvas.Clear(SKColors.White);

Expand All @@ -36,7 +29,7 @@ private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
TextAlign = SKTextAlign.Center,
TextSize = 24
};
var coord = new SKPoint(scaledSize.Width / 2, (scaledSize.Height + paint.TextSize) / 2);
var coord = new SKPoint(e.Info.Width / 2, (e.Info.Height + paint.TextSize) / 2);
canvas.DrawText("SkiaSharp", coord, paint);
}
}
Expand Down
2 changes: 1 addition & 1 deletion samples/Basic/Tizen/SkiaSharpSample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
xmlns:skia="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms"
x:Class="SkiaSharpSample.MainPage">

<skia:SKCanvasView x:Name="skiaView" PaintSurface="OnPaintSurface" />
<skia:SKCanvasView x:Name="skiaView" PaintSurface="OnPaintSurface" IgnorePixelScaling="True" />

</ContentPage>
Loading

0 comments on commit 4a687a8

Please sign in to comment.