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 handling nested transform #236

Merged
merged 2 commits into from
Jul 18, 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: 4 additions & 4 deletions src/Avalonia.Svg/AvaloniaPicture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ private static void RecordCommand(CanvasCommand canvasCommand, List<DrawCommand>
}
case SetMatrixCanvasCommand setMatrixCanvasCommand:
{
var matrix = setMatrixCanvasCommand.Matrix.ToMatrix();
commands.Add(new SetTransformDrawCommand(matrix));
var matrix = setMatrixCanvasCommand.DeltaMatrix.ToMatrix();
commands.Add(new PushTransformDrawCommand(matrix));
break;
}
case SaveLayerCanvasCommand saveLayerCanvasCommand:
Expand Down Expand Up @@ -267,9 +267,9 @@ private void Draw(AM.DrawingContext context, DrawCommand command, Stack<Stack<ID
}
break;
}
case SetTransformDrawCommand setTransformDrawCommand:
case PushTransformDrawCommand pushTransformDrawCommand:
{
var transformPreTransform = context.PushTransform(setTransformDrawCommand.Matrix);
var transformPreTransform = context.PushTransform(pushTransformDrawCommand.Matrix);
var currentPushedStates = pushedStates.Peek();
currentPushedStates.Push(transformPreTransform);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Avalonia.Svg.Commands;

public sealed class SetTransformDrawCommand : DrawCommand
public sealed class PushTransformDrawCommand : DrawCommand
{
public A.Matrix Matrix { get; }

public SetTransformDrawCommand(A.Matrix matrix)
public PushTransformDrawCommand(A.Matrix matrix)
{
Matrix = matrix;
}
Expand Down
8 changes: 4 additions & 4 deletions src/ShimSkiaSharp/SKCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public record SaveCanvasCommand(int Count) : CanvasCommand;

public record SaveLayerCanvasCommand(int Count, SKPaint? Paint = null) : CanvasCommand;

public record SetMatrixCanvasCommand(SKMatrix Matrix) : CanvasCommand;
public record SetMatrixCanvasCommand(SKMatrix DeltaMatrix, SKMatrix TotalMatrix) : CanvasCommand;

public class SKCanvas
{
Expand Down Expand Up @@ -77,10 +77,10 @@ public void DrawTextOnPath(string text, SKPath path, float hOffset, float vOffse
Commands?.Add(new DrawTextOnPathCanvasCommand(text, path, hOffset, vOffset, paint));
}

public void SetMatrix(SKMatrix matrix)
public void SetMatrix(SKMatrix deltaMatrix)
{
TotalMatrix = matrix;
Commands?.Add(new SetMatrixCanvasCommand(matrix));
TotalMatrix = TotalMatrix.PreConcat(deltaMatrix);
Commands?.Add(new SetMatrixCanvasCommand(deltaMatrix, TotalMatrix));
}

public int Save()
Expand Down
2 changes: 1 addition & 1 deletion src/Svg.CodeGen.Skia/SkiaCSharpModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1780,7 +1780,7 @@ public static void ToSKPicture(this SKPicture? picture, SkiaCSharpCodeGenCounter
}
case SetMatrixCanvasCommand setMatrixCanvasCommand:
{
sb.AppendLine($"{indent}{counter.CanvasVarName}{counterCanvas}.SetMatrix({setMatrixCanvasCommand.Matrix.ToSKMatrix()});");
sb.AppendLine($"{indent}{counter.CanvasVarName}{counterCanvas}.SetMatrix({setMatrixCanvasCommand.TotalMatrix.ToSKMatrix()});");
break;
}
case SaveLayerCanvasCommand saveLayerCanvasCommand:
Expand Down
4 changes: 1 addition & 3 deletions src/Svg.Model/Drawables/DrawableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ public virtual void Draw(SKCanvas canvas, DrawAttributes ignoreAttributes, Drawa

if (!Transform.IsIdentity && enableTransform)
{
var skMatrixTotal = canvas.TotalMatrix;
skMatrixTotal = skMatrixTotal.PreConcat(Transform);
canvas.SetMatrix(skMatrixTotal);
canvas.SetMatrix(Transform);
}

if (Clip is { })
Expand Down
4 changes: 1 addition & 3 deletions src/Svg.Model/Drawables/Elements/ImageDrawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@ public override void OnDraw(SKCanvas canvas, DrawAttributes ignoreAttributes, Dr
{
canvas.Save();

var skMatrixTotal = canvas.TotalMatrix;
skMatrixTotal = skMatrixTotal.PreConcat(FragmentTransform);
canvas.SetMatrix(skMatrixTotal);
canvas.SetMatrix(FragmentTransform);

FragmentDrawable.Draw(canvas, ignoreAttributes, until, true);

Expand Down
4 changes: 1 addition & 3 deletions src/Svg.Model/Drawables/Elements/TextDrawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ internal void BeginDraw(SvgTextBase svgTextBase, SKCanvas skCanvas, SKRect skBou

if (!skMatrix.IsIdentity && enableTransform)
{
var skMatrixTotal = skCanvas.TotalMatrix;
skMatrixTotal = skMatrixTotal.PreConcat(skMatrix);
skCanvas.SetMatrix(skMatrixTotal);
skCanvas.SetMatrix(skMatrix);
}

if (enableClip)
Expand Down
2 changes: 1 addition & 1 deletion src/Svg.Skia/SkiaModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ public void Draw(CanvasCommand canvasCommand, SkiaSharp.SKCanvas skCanvas)
}
case SetMatrixCanvasCommand setMatrixCanvasCommand:
{
var matrix = ToSKMatrix(setMatrixCanvasCommand.Matrix);
var matrix = ToSKMatrix(setMatrixCanvasCommand.TotalMatrix);
skCanvas.SetMatrix(matrix);
break;
}
Expand Down
Loading