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

Add 2 new line styles for brushes Edge and EdgeWide #199

Merged
merged 26 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
53 changes: 51 additions & 2 deletions src/Consolonia.Core/Drawing/ConsoloniaRenderInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Avalonia.Media.Imaging;
using Avalonia.Media.TextFormatting;
using Avalonia.Platform;
using Consolonia.Core.InternalHelpers;
using Consolonia.Core.Text;
using SkiaSharp;

Expand All @@ -30,7 +31,7 @@ public IGeometryImpl CreateRectangleGeometry(Rect rect)

public IStreamGeometryImpl CreateStreamGeometry()
{
throw new NotImplementedException();
return new StreamGeometryImpl();
}

public IGeometryImpl CreateGeometryGroup(FillRule fillRule, IReadOnlyList<IGeometryImpl> children)
Expand All @@ -40,7 +41,55 @@ public IGeometryImpl CreateGeometryGroup(FillRule fillRule, IReadOnlyList<IGeome

public IGeometryImpl CreateCombinedGeometry(GeometryCombineMode combineMode, IGeometryImpl g1, IGeometryImpl g2)
{
throw new NotImplementedException();
// this is handcrafted to only combine single thickness line strokes for borders.
// This needs to be much more robust to handle general cases.

if (combineMode != GeometryCombineMode.Exclude)
throw new NotImplementedException("Only GeometryCombineMode.Exclude is supported");

if (g1 is not StreamGeometryImpl stream1 || g2 is not StreamGeometryImpl stream2)
throw new ArgumentException("Only StreamGeometryImpl is supported");
tomlm marked this conversation as resolved.
Show resolved Hide resolved

var newGeometry = CreateStreamGeometry();
using (var ctx = newGeometry.Open())
{
// Resharper disable UnusedVariable
var hasLeftStroke = stream2.Bounds.X.IsNearlyEqual(1);
var hasTopStroke = stream2.Bounds.Y.IsNearlyEqual(1);
var hasRightStroke = (stream1.Bounds.Width - stream2.Bounds.Width).IsNearlyEqual(stream2.Bounds.X + 1);
var hasBottomStroke = (stream1.Bounds.Height - stream2.Bounds.Height).IsNearlyEqual(stream2.Bounds.Y + 1);
var topLeft = stream1.Bounds.TopLeft;
var topRight = stream1.Bounds.TopRight;
var bottomLeft = stream1.Bounds.BottomLeft;
var bottomRight = stream1.Bounds.BottomRight;
var topStroke = stream1.Strokes[0];
var rightStroke = stream1.Strokes[1];
var bottomStroke = stream1.Strokes[2];
var leftStroke = stream1.Strokes[3];
tomlm marked this conversation as resolved.
Show resolved Hide resolved
// Resharper enable UnusedVariable

// add "null" strokes to establish boundries of box even when there is a single real stroke.
AddStroke(ctx, topLeft, topLeft);
AddStroke(ctx, bottomRight, bottomRight);

if (hasTopStroke)
AddStroke(ctx, topStroke.PStart, topStroke.PEnd + new Vector(-1, 0));
if (hasRightStroke)
AddStroke(ctx, rightStroke.PStart + new Vector(-1, 0), rightStroke.PEnd + new Vector(-1, -1));
if (hasBottomStroke)
AddStroke(ctx, bottomStroke.PStart + new Vector(0,-1), bottomStroke.PEnd + new Vector(-1, -1));
if (hasLeftStroke)
AddStroke(ctx, leftStroke.PStart, leftStroke.PEnd + new Vector(0, -1));
}

return newGeometry;
}

private static void AddStroke(IStreamGeometryContextImpl ctx, Point start, Point end)
{
ctx.BeginFigure(start, false);
ctx.LineTo(end);
ctx.EndFigure(true);
}

public IGeometryImpl BuildGlyphRunGeometry(GlyphRun glyphRun)
Expand Down
469 changes: 347 additions & 122 deletions src/Consolonia.Core/Drawing/DrawingContextImpl.cs

Large diffs are not rendered by default.

15 changes: 14 additions & 1 deletion src/Consolonia.Core/Drawing/Line.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,22 @@ public Line(Point pStart, bool vertical, int length, IGeometryImpl sourceGeometr
Transform = transform;
}

public Line(Point pStart, Point pEnd, IGeometryImpl sourceGeometry = default, Matrix transform = default)
{
pStart = new Point(pStart.X, pStart.Y);
PStart = pStart;
pEnd = new Point(pEnd.X, pEnd.Y);
if (!(pStart.X.IsNearlyEqual(pEnd.X) || pStart.Y.IsNearlyEqual(pEnd.Y)))
throw new ArgumentException("Consolonia can only draw horizontal or vertical lines");
Vertical = pStart.X.IsNearlyEqual(pEnd.X);
Length = Vertical ? (int)Math.Abs(pStart.Y - pEnd.Y) : (int)Math.Abs(pStart.X - pEnd.X);
SourceGeometry = sourceGeometry!;
Transform = transform;
}
tomlm marked this conversation as resolved.
Show resolved Hide resolved

public Point PStart { get; }

private Point PEnd => Vertical ? PStart.WithY(PStart.Y + Length) : PStart.WithX(PStart.X + Length);
public Point PEnd => Vertical ? PStart.WithY(PStart.Y + Length) : PStart.WithX(PStart.X + Length);
public bool Vertical { get; }
public int Length { get; }

Expand Down
2 changes: 1 addition & 1 deletion src/Consolonia.Core/Drawing/LineBrush.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Avalonia;
using Avalonia;
using Avalonia.Animation;
using Avalonia.Media;
using Consolonia.Core.InternalHelpers;
Expand Down
4 changes: 3 additions & 1 deletion src/Consolonia.Core/Drawing/LineStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public enum LineStyle
{
SingleLine = 0,
DoubleLine,
Bold
Bold,
Edge,
EdgeWide
tomlm marked this conversation as resolved.
Show resolved Hide resolved
}
}
11 changes: 11 additions & 0 deletions src/Consolonia.Core/Drawing/RectangleLinePosition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Consolonia.Core.Drawing
{
internal enum RectangleLinePosition
{
Left,
Top,
Right,
Bottom,
Unknown,

Check notice on line 9 in src/Consolonia.Core/Drawing/RectangleLinePosition.cs

View workflow job for this annotation

GitHub Actions / build

"[ArrangeTrailingCommaInMultilineLists] Remove trailing comma to conform to code style" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Drawing/RectangleLinePosition.cs(9,16)
}
}
202 changes: 202 additions & 0 deletions src/Consolonia.Core/Drawing/StreamGeometryImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Avalonia;
using Avalonia.Media;
using Avalonia.Platform;

namespace Consolonia.Core.Drawing
{
internal class StreamGeometryImpl : IStreamGeometryImpl
{
private List<Line> _strokes;
private List<Rectangle> _fills;
private Rect _bounds;
// private SKPath _path;

public Rect Bounds => _bounds;

public double ContourLength => _strokes.Sum(l => l.ContourLength);

public IReadOnlyList<Line> Strokes => _strokes;

public IReadOnlyList<Rectangle> Fills => _fills;

public StreamGeometryImpl()
{
_strokes = new List<Line>();
_fills = new List<Rectangle>();
}


public IStreamGeometryImpl Clone()
{
var clone = new StreamGeometryImpl();
foreach(var line in _strokes)
{
clone._strokes.Add(line);
}
foreach (var rect in _fills)
{
clone._fills.Add(rect);
}
tomlm marked this conversation as resolved.
Show resolved Hide resolved
clone._bounds = _bounds;
return clone;
tomlm marked this conversation as resolved.
Show resolved Hide resolved
}

public bool FillContains(Point point)
tomlm marked this conversation as resolved.
Show resolved Hide resolved
{
return _bounds.Contains(point);
}

public Rect GetRenderBounds(IPen pen)
{
var strokeBounds = _strokes.Aggregate(new Rect(), (rect, line) => rect.Union(line.GetRenderBounds(pen)));
return _fills.Aggregate(strokeBounds, (rect, r) => rect.Union(r.GetRenderBounds(pen)));
}

public IGeometryImpl GetWidenedGeometry(IPen pen)
{
// TODO
throw new NotImplementedException();
}

public IGeometryImpl Intersect(IGeometryImpl geometry)
{
return geometry.Intersect(this);
}

public IStreamGeometryContextImpl Open()
{
return new StreamGeometryContextImpl(this);
}

public bool StrokeContains(IPen pen, Point point)
{
return _strokes.Any(line => line.StrokeContains(pen, point));
}

public bool TryGetPointAndTangentAtDistance(double distance, out Point point, out Point tangent)
{
throw new NotImplementedException();
}

public bool TryGetPointAtDistance(double distance, out Point point)
{
throw new NotImplementedException();
}

public bool TryGetSegment(double startDistance, double stopDistance, bool startOnBeginFigure, [NotNullWhen(true)] out IGeometryImpl segmentGeometry)
{
throw new NotImplementedException();
}

public ITransformedGeometryImpl WithTransform(Matrix transform)
{
throw new NotImplementedException();
}

/// <summary>
/// A Conolonia implementation of a <see cref="IStreamGeometryContextImpl"/>.
/// </summary>
private class StreamGeometryContextImpl : IStreamGeometryContextImpl, IGeometryContext2
{
private readonly StreamGeometryImpl _geometryImpl;
private bool _isFilled;
private Point _lastPoint;

/// <summary>
/// Initializes a new instance of the StreamGeometryContextImpl class.
/// <param name="geometryImpl">Geometry to operate on.</param>
/// </summary>
public StreamGeometryContextImpl(StreamGeometryImpl geometryImpl)
{
_geometryImpl = geometryImpl;
}

/// <inheritdoc />
public void ArcTo(Point point, Size size, double rotationAngle, bool isLargeArc, SweepDirection sweepDirection)
{
// ignore arc instructions. It's attempt to draw rounded corners, we don't do that.
//_lastPoint = point;
}

/// <inheritdoc />
public void BeginFigure(Point startPoint, bool isFilled)
{
_isFilled = isFilled;
_lastPoint = startPoint;
}

/// <inheritdoc />
public void CubicBezierTo(Point point1, Point point2, Point point3)
{
throw new NotSupportedException();
}

/// <inheritdoc />
public void QuadraticBezierTo(Point point1, Point point2)
{
throw new NotSupportedException();
}

/// <inheritdoc />
public void LineTo(Point point)
{
// our strokes are oriented from UpperLeft corner to Right or Down
if (_lastPoint.X > point.X || _lastPoint.Y > point.Y)
_geometryImpl._strokes.Add(new Line(point, _lastPoint));
else
_geometryImpl._strokes.Add(new Line(_lastPoint, point));
_lastPoint = point;
}

/// <inheritdoc />
public void EndFigure(bool isClosed)
{
var bound = _geometryImpl._strokes.Aggregate(new Rect(), (rect, line) => rect.Union(line.Bounds));
_geometryImpl._bounds = bound;
if (_isFilled)
{
// _geometryImpl._fills.Add(new Rectangle(bound));
}
}
tomlm marked this conversation as resolved.
Show resolved Hide resolved

/// <inheritdoc />
public void SetFillRule(FillRule fillRule)
{
}

/// <inheritdoc />
public void LineTo(Point point, bool isStroked)
{
this.LineTo(point);

Check notice on line 174 in src/Consolonia.Core/Drawing/StreamGeometryImpl.cs

View workflow job for this annotation

GitHub Actions / build

"[ArrangeThisQualifier] Qualifier 'this.' is redundant" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Drawing/StreamGeometryImpl.cs(174,17)
}

/// <inheritdoc />
public void ArcTo(Point point, Size size, double rotationAngle, bool isLargeArc, SweepDirection sweepDirection, bool isStroked)
{
_lastPoint = point;
}

/// <inheritdoc />
public void CubicBezierTo(Point point1, Point point2, Point point3, bool isStroked)
{
throw new NotSupportedException();

}

/// <inheritdoc />
public void QuadraticBezierTo(Point point1, Point point2, bool isStroked)
{
throw new NotSupportedException();
}

public void Dispose()
{
}
}

}
}
Loading
Loading