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

Fix for the "DrawingContext.DrawImage() method not draws an SvgImage in some cases" issue #228

Merged
merged 5 commits into from
Aug 29, 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
8 changes: 8 additions & 0 deletions samples/AvaloniaSvgSkiaSample/DrawControl.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaSvgSkiaSample.DrawControl">
</UserControl>

25 changes: 25 additions & 0 deletions samples/AvaloniaSvgSkiaSample/DrawControl.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Svg.Skia;

namespace AvaloniaSvgSkiaSample;

public partial class DrawControl : UserControl
{
public SvgImage Image { get; set; }

public DrawControl()
{
InitializeComponent();
}

public override void Render(DrawingContext context)
{
var center = Bounds.Center;
var width = Image.Size.Width / 2;
var height = Image.Size.Width / 2;
context.DrawImage(Image, new Rect(center.X - 0.5 * width, center.Y - 0.5 * height, width, height));
}
}

12 changes: 12 additions & 0 deletions samples/AvaloniaSvgSkiaSample/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:svg="clr-namespace:Avalonia.Svg.Skia;assembly=Avalonia.Svg.Skia"
xmlns:local="clr-namespace:AvaloniaSvgSkiaSample"
mc:Ignorable="d" d:DesignWidth="900" d:DesignHeight="600"
x:Class="AvaloniaSvgSkiaSample.MainWindow"
Width="900" Height="600" WindowStartupLocation="CenterScreen"
Expand Down Expand Up @@ -146,6 +147,17 @@
</DockPanel>
</TabItem>

<TabItem Header="Context.Draw()">
<DockPanel x:Name="svgDrawDockPanel"
Background="Transparent"
Margin="16"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
DragDrop.AllowDrop="True">
<local:DrawControl Image="{SvgImage /Assets/__tiger.svg}" />
</DockPanel>
</TabItem>

</TabControl>

</Window>
2 changes: 1 addition & 1 deletion src/Avalonia.Controls.Skia/SKBitmapControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public override void Render(DrawingContext context)
}

using (context.PushClip(destRect))
using (context.PushTransform(translateMatrix * scaleMatrix))
using (context.PushTransform(scaleMatrix * translateMatrix))
{
context.Custom(new SKBitmapDrawOperation(new Rect(0, 0, bounds.Width, bounds.Height), bitmap));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Controls.Skia/SKBitmapImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void IImage.Draw(DrawingContext context, Rect sourceRect, Rect destRect)
var scaleMatrix = Matrix.CreateScale(destRect.Width / sourceRect.Width, destRect.Height / sourceRect.Height);
var translateMatrix = Matrix.CreateTranslation(-sourceRect.X + destRect.X - bounds.Top, -sourceRect.Y + destRect.Y - bounds.Left);
using (context.PushClip(destRect))
using (context.PushTransform(translateMatrix * scaleMatrix))
using (context.PushTransform(scaleMatrix * translateMatrix))
{
context.Custom(new SKBitmapDrawOperation(new Rect(0, 0, bounds.Width, bounds.Height), source));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Controls.Skia/SKPathControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public override void Render(DrawingContext context)
}

using (context.PushClip(destRect))
using (context.PushTransform(translateMatrix * scaleMatrix))
using (context.PushTransform(scaleMatrix * translateMatrix))
{
context.Custom(
new SKPathDrawOperation(
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Controls.Skia/SKPathImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void IImage.Draw(DrawingContext context, Rect sourceRect, Rect destRect)
var scaleMatrix = Matrix.CreateScale(destRect.Width / sourceRect.Width, destRect.Height / sourceRect.Height);
var translateMatrix = Matrix.CreateTranslation(-sourceRect.X + destRect.X - bounds.Top, -sourceRect.Y + destRect.Y - bounds.Left);
using (context.PushClip(destRect))
using (context.PushTransform(translateMatrix * scaleMatrix))
using (context.PushTransform(scaleMatrix * translateMatrix))
{
context.Custom(new SKPathDrawOperation(new Rect(0, 0, bounds.Width, bounds.Height), source, paint));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Controls.Skia/SKPictureControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public override void Render(DrawingContext context)
}

using (context.PushClip(destRect))
using (context.PushTransform(translateMatrix * scaleMatrix))
using (context.PushTransform(scaleMatrix * translateMatrix))
{
context.Custom(
new SKPictureDrawOperation(
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Controls.Skia/SKPictureImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void IImage.Draw(DrawingContext context, Rect sourceRect, Rect destRect)
var scaleMatrix = Matrix.CreateScale(destRect.Width / sourceRect.Width, destRect.Height / sourceRect.Height);
var translateMatrix = Matrix.CreateTranslation(-sourceRect.X + destRect.X - bounds.Top, -sourceRect.Y + destRect.Y - bounds.Left);
using (context.PushClip(destRect))
using (context.PushTransform(translateMatrix * scaleMatrix))
using (context.PushTransform(scaleMatrix * translateMatrix))
{
context.Custom(new SKPictureDrawOperation(new Rect(0, 0, bounds.Width, bounds.Height), source));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Svg.Skia/Svg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public override void Render(DrawingContext context)
-sourceRect.Y + destRect.Y - bounds.Left);

using (context.PushClip(destRect))
using (context.PushTransform(translateMatrix * scaleMatrix))
using (context.PushTransform(scaleMatrix * translateMatrix))
{
context.Custom(
new SvgSourceCustomDrawOperation(
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Svg.Skia/SvgImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void IImage.Draw(DrawingContext context, Rect sourceRect, Rect destRect)
-sourceRect.X + destRect.X - bounds.Top,
-sourceRect.Y + destRect.Y - bounds.Left);
using (context.PushClip(destRect))
using (context.PushTransform(translateMatrix * scaleMatrix))
using (context.PushTransform(scaleMatrix * translateMatrix))
{
context.Custom(
new SvgSourceCustomDrawOperation(
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Svg/Svg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public override void Render(DrawingContext context)
-sourceRect.Y + destRect.Y - bounds.Left);

using (context.PushClip(destRect))
using (context.PushTransform(translateMatrix * scaleMatrix))
using (context.PushTransform(scaleMatrix * translateMatrix))
{
if (_avaloniaPicture is { })
{
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Svg/SvgImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void IImage.Draw(
-sourceRect.X + destRect.X - bounds.Top,
-sourceRect.Y + destRect.Y - bounds.Left);
using (context.PushClip(destRect))
using (context.PushTransform(translateMatrix * scaleMatrix))
using (context.PushTransform(scaleMatrix * translateMatrix))
{
try
{
Expand Down
Loading