committed by
GitHub
10 changed files with 841 additions and 379 deletions
@ -0,0 +1,66 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using Avalonia.Media; |
|||
|
|||
namespace Avalonia.Platform |
|||
{ |
|||
/// <summary>
|
|||
/// Describes a geometry using drawing commands.
|
|||
/// </summary>
|
|||
public interface IGeometryContext : IDisposable |
|||
{ |
|||
/// <summary>
|
|||
/// Draws an arc to the specified point.
|
|||
/// </summary>
|
|||
/// <param name="point">The destination point.</param>
|
|||
/// <param name="size">The radii of an oval whose perimeter is used to draw the angle.</param>
|
|||
/// <param name="rotationAngle">The rotation angle of the oval that specifies the curve.</param>
|
|||
/// <param name="isLargeArc">true to draw the arc greater than 180 degrees; otherwise, false.</param>
|
|||
/// <param name="sweepDirection">
|
|||
/// A value that indicates whether the arc is drawn in the Clockwise or Counterclockwise direction.
|
|||
/// </param>
|
|||
void ArcTo(Point point, Size size, double rotationAngle, bool isLargeArc, SweepDirection sweepDirection); |
|||
|
|||
/// <summary>
|
|||
/// Begins a new figure.
|
|||
/// </summary>
|
|||
/// <param name="startPoint">The starting point for the figure.</param>
|
|||
/// <param name="isFilled">Whether the figure is filled.</param>
|
|||
void BeginFigure(Point startPoint, bool isFilled = true); |
|||
|
|||
/// <summary>
|
|||
/// Draws a Bezier curve to the specified point.
|
|||
/// </summary>
|
|||
/// <param name="point1">The first control point used to specify the shape of the curve.</param>
|
|||
/// <param name="point2">The second control point used to specify the shape of the curve.</param>
|
|||
/// <param name="point3">The destination point for the end of the curve.</param>
|
|||
void CubicBezierTo(Point point1, Point point2, Point point3); |
|||
|
|||
/// <summary>
|
|||
/// Draws a quadratic Bezier curve to the specified point
|
|||
/// </summary>
|
|||
/// <param name="control">Control point</param>
|
|||
/// <param name="endPoint">DestinationPoint</param>
|
|||
void QuadraticBezierTo(Point control, Point endPoint); |
|||
|
|||
/// <summary>
|
|||
/// Draws a line to the specified point.
|
|||
/// </summary>
|
|||
/// <param name="point">The destination point.</param>
|
|||
void LineTo(Point point); |
|||
|
|||
/// <summary>
|
|||
/// Ends the figure started by <see cref="BeginFigure(Point, bool)"/>.
|
|||
/// </summary>
|
|||
/// <param name="isClosed">Whether the figure is closed.</param>
|
|||
void EndFigure(bool isClosed); |
|||
|
|||
/// <summary>
|
|||
/// Sets the fill rule.
|
|||
/// </summary>
|
|||
/// <param name="fillRule">The fill rule.</param>
|
|||
void SetFillRule(FillRule fillRule); |
|||
} |
|||
} |
|||
@ -1,62 +1,12 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using Avalonia.Media; |
|||
|
|||
namespace Avalonia.Platform |
|||
{ |
|||
/// <summary>
|
|||
/// Describes a geometry using drawing commands.
|
|||
/// </summary>
|
|||
public interface IStreamGeometryContextImpl : IDisposable |
|||
{ |
|||
/// <summary>
|
|||
/// Draws an arc to the specified point.
|
|||
/// </summary>
|
|||
/// <param name="point">The destination point.</param>
|
|||
/// <param name="size">The radii of an oval whose perimeter is used to draw the angle.</param>
|
|||
/// <param name="rotationAngle">The rotation angle of the oval that specifies the curve.</param>
|
|||
/// <param name="isLargeArc">true to draw the arc greater than 180 degrees; otherwise, false.</param>
|
|||
/// <param name="sweepDirection">
|
|||
/// A value that indicates whether the arc is drawn in the Clockwise or Counterclockwise direction.
|
|||
/// </param>
|
|||
void ArcTo(Point point, Size size, double rotationAngle, bool isLargeArc, SweepDirection sweepDirection); |
|||
|
|||
/// <summary>
|
|||
/// Begins a new figure.
|
|||
/// </summary>
|
|||
/// <param name="startPoint">The starting point for the figure.</param>
|
|||
/// <param name="isFilled">Whether the figure is filled.</param>
|
|||
void BeginFigure(Point startPoint, bool isFilled); |
|||
|
|||
/// <summary>
|
|||
/// Draws a Bezier curve to the specified point.
|
|||
/// </summary>
|
|||
/// <param name="point1">The first control point used to specify the shape of the curve.</param>
|
|||
/// <param name="point2">The second control point used to specify the shape of the curve.</param>
|
|||
/// <param name="point3">The destination point for the end of the curve.</param>
|
|||
void CubicBezierTo(Point point1, Point point2, Point point3); |
|||
|
|||
/// <summary>
|
|||
/// Draws a quadratic Bezier curve to the specified point
|
|||
/// </summary>
|
|||
/// <param name="control">Control point</param>
|
|||
/// <param name="endPoint">DestinationPoint</param>
|
|||
void QuadraticBezierTo(Point control, Point endPoint); |
|||
|
|||
/// <summary>
|
|||
/// Draws a line to the specified point.
|
|||
/// </summary>
|
|||
/// <param name="point">The destination point.</param>
|
|||
void LineTo(Point point); |
|||
|
|||
/// <summary>
|
|||
/// Ends the figure started by <see cref="BeginFigure(Point, bool)"/>.
|
|||
/// </summary>
|
|||
/// <param name="isClosed">Whether the figure is closed.</param>
|
|||
void EndFigure(bool isClosed); |
|||
|
|||
void SetFillRule(FillRule fillRule); |
|||
public interface IStreamGeometryContextImpl : IGeometryContext |
|||
{ |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,85 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using Avalonia.Media; |
|||
using Avalonia.Platform; |
|||
using System; |
|||
|
|||
namespace Avalonia.Visuals.Platform |
|||
{ |
|||
public class PathGeometryContext : IGeometryContext |
|||
{ |
|||
private PathFigure _currentFigure; |
|||
private PathGeometry _pathGeometry; |
|||
|
|||
public PathGeometryContext(PathGeometry pathGeometry) |
|||
{ |
|||
_pathGeometry = pathGeometry ?? throw new ArgumentNullException(nameof(pathGeometry)); |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
_pathGeometry = null; |
|||
} |
|||
|
|||
public void ArcTo(Point point, Size size, double rotationAngle, bool isLargeArc, SweepDirection sweepDirection) |
|||
{ |
|||
var arcSegment = new ArcSegment |
|||
{ |
|||
Size = size, |
|||
RotationAngle = rotationAngle, |
|||
IsLargeArc = isLargeArc, |
|||
SweepDirection = sweepDirection, |
|||
Point = point |
|||
}; |
|||
|
|||
_currentFigure.Segments.Add(arcSegment); |
|||
} |
|||
|
|||
public void BeginFigure(Point startPoint, bool isFilled) |
|||
{ |
|||
_currentFigure = new PathFigure { StartPoint = startPoint, IsClosed = false, IsFilled = isFilled }; |
|||
|
|||
_pathGeometry.Figures.Add(_currentFigure); |
|||
} |
|||
|
|||
public void CubicBezierTo(Point point1, Point point2, Point point3) |
|||
{ |
|||
var bezierSegment = new BezierSegment { Point1 = point1, Point2 = point2, Point3 = point3 }; |
|||
|
|||
_currentFigure.Segments.Add(bezierSegment); |
|||
} |
|||
|
|||
public void QuadraticBezierTo(Point control, Point endPoint) |
|||
{ |
|||
var quadraticBezierSegment = new QuadraticBezierSegment { Point1 = control, Point2 = endPoint }; |
|||
|
|||
_currentFigure.Segments.Add(quadraticBezierSegment); |
|||
} |
|||
|
|||
public void LineTo(Point point) |
|||
{ |
|||
var lineSegment = new LineSegment |
|||
{ |
|||
Point = point |
|||
}; |
|||
|
|||
_currentFigure.Segments.Add(lineSegment); |
|||
} |
|||
|
|||
public void EndFigure(bool isClosed) |
|||
{ |
|||
if (_currentFigure != null) |
|||
{ |
|||
_currentFigure.IsClosed = isClosed; |
|||
} |
|||
|
|||
_currentFigure = null; |
|||
} |
|||
|
|||
public void SetFillRule(FillRule fillRule) |
|||
{ |
|||
_pathGeometry.FillRule = fillRule; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using Avalonia.Media; |
|||
using Avalonia.UnitTests; |
|||
using BenchmarkDotNet.Attributes; |
|||
|
|||
namespace Avalonia.Benchmarks.Visuals.Media |
|||
{ |
|||
[MemoryDiagnoser] |
|||
public class PathMarkupParserTests : IDisposable |
|||
{ |
|||
private IDisposable _app; |
|||
|
|||
public PathMarkupParserTests() |
|||
{ |
|||
_app = UnitTestApplication.Start(TestServices.StyledWindow); |
|||
} |
|||
|
|||
public void Dispose() |
|||
{ |
|||
_app.Dispose(); |
|||
} |
|||
|
|||
[Benchmark] |
|||
public void Parse_Large_Path() |
|||
{ |
|||
const string PathData = "F1 M 16.6309 18.6563C 17.1309 8.15625 29.8809 14.1563 29.8809 14.1563C 30.8809 11.1563 34.1308 11.4063" + |
|||
" 34.1308 11.4063C 33.5 12 34.6309 13.1563 34.6309 13.1563C 32.1309 13.1562 31.1309 14.9062 31.1309 14.9" + |
|||
"062C 41.1309 23.9062 32.6309 27.9063 32.6309 27.9062C 24.6309 24.9063 21.1309 22.1562 16.6309 18.6563 Z" + |
|||
" M 16.6309 19.9063C 21.6309 24.1563 25.1309 26.1562 31.6309 28.6562C 31.6309 28.6562 26.3809 39.1562 18" + |
|||
".3809 36.1563C 18.3809 36.1563 18 38 16.3809 36.9063C 15 36 16.3809 34.9063 16.3809 34.9063C 16.3809 34" + |
|||
".9063 10.1309 30.9062 16.6309 19.9063 Z "; |
|||
|
|||
var streamGeometry = new StreamGeometry(); |
|||
|
|||
using (var context = streamGeometry.Open()) |
|||
using (var parser = new PathMarkupParser(context)) |
|||
{ |
|||
parser.Parse(PathData); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue