Skip to content

fix(Geometries): Relative line drawing #14013

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

15 changes: 9 additions & 6 deletions src/Avalonia.Base/Media/PathMarkupParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private void AddLine(ref ReadOnlySpan<char> span, bool relative)
{
ThrowIfDisposed();

_currentPoint = relative
var next = relative
? ReadRelativePoint(ref span, _currentPoint)
: ReadPoint(ref span);

Expand All @@ -268,14 +268,15 @@ private void AddLine(ref ReadOnlySpan<char> span, bool relative)
CreateFigure();
}

_geometryContext.LineTo(_currentPoint);
_geometryContext.LineTo(next);
_currentPoint = next;
}

private void AddHorizontalLine(ref ReadOnlySpan<char> span, bool relative)
{
ThrowIfDisposed();

_currentPoint = relative
var next = relative
? new Point(_currentPoint.X + ReadDouble(ref span), _currentPoint.Y)
: _currentPoint.WithX(ReadDouble(ref span));

Expand All @@ -284,14 +285,15 @@ private void AddHorizontalLine(ref ReadOnlySpan<char> span, bool relative)
CreateFigure();
}

_geometryContext.LineTo(_currentPoint);
_geometryContext.LineTo(next);
_currentPoint = next;
}

private void AddVerticalLine(ref ReadOnlySpan<char> span, bool relative)
{
ThrowIfDisposed();

_currentPoint = relative
var next = relative
? new Point(_currentPoint.X, _currentPoint.Y + ReadDouble(ref span))
: _currentPoint.WithY(ReadDouble(ref span));

Expand All @@ -300,7 +302,8 @@ private void AddVerticalLine(ref ReadOnlySpan<char> span, bool relative)
CreateFigure();
}

_geometryContext.LineTo(_currentPoint);
_geometryContext.LineTo(next);
_currentPoint = next;
}

private void AddCubicBezierCurve(ref ReadOnlySpan<char> span, bool relative)
Expand Down
32 changes: 29 additions & 3 deletions tests/Avalonia.RenderTests/Media/GeometryDrawingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
#if AVALONIA_SKIA
namespace Avalonia.Skia.RenderTests
#else

using Avalonia.Direct2D1.RenderTests;

namespace Avalonia.Direct2D1.RenderTests.Media
#endif
{
Expand Down Expand Up @@ -47,6 +44,35 @@ public void DrawingGeometry_WithoutPen()
Assert.Equal(200, geometryDrawing.GetBounds().Width);
}

[Theory]
[InlineData("l", "F1 M50,50z l -5,-5")]
[InlineData("h", "F1 M50,50z h 10")]
[InlineData("v", "F1 M50,50z v 10")]
[InlineData("m", "M50,50z l -5,-5")]
public async void DrawingGeometry_RelativeLine_Commands(string name, string command)
{
var target = new Avalonia.Controls.Decorator
{
Width = 200,
Height = 200,
Child = new Avalonia.Controls.Image()
{
Width = 200,
Height = 200,
Source = new DrawingImage()
{
Drawing = new GeometryDrawing()
{
Geometry = StreamGeometry.Parse(command),
Pen = new Pen(Brushes.Black, 2, lineCap: PenLineCap.Round, lineJoin: PenLineJoin.Round)
}
},

}
};
var testName = $"Relative_Line_{name}";
await RenderToFile(target, testName);
CompareImages(testName);
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.