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 all 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
54 changes: 52 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,56 @@ 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

IStreamGeometryImpl newGeometry = CreateStreamGeometry();
using (IStreamGeometryContextImpl ctx = newGeometry.Open())
{
// Resharper disable UnusedVariable
bool hasLeftStroke = stream2.Bounds.X.IsNearlyEqual(1);
bool hasTopStroke = stream2.Bounds.Y.IsNearlyEqual(1);
bool hasRightStroke = (stream1.Bounds.Width - stream2.Bounds.Width).IsNearlyEqual(stream2.Bounds.X + 1);
bool hasBottomStroke =
(stream1.Bounds.Height - stream2.Bounds.Height).IsNearlyEqual(stream2.Bounds.Y + 1);
Point topLeft = stream1.Bounds.TopLeft;
Point topRight = stream1.Bounds.TopRight;
Point bottomLeft = stream1.Bounds.BottomLeft;
Point bottomRight = stream1.Bounds.BottomRight;
Line topStroke = stream1.Strokes[0];
Line rightStroke = stream1.Strokes[1];
Line bottomStroke = stream1.Strokes[2];
Line leftStroke = stream1.Strokes[3];
// 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
291 changes: 258 additions & 33 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
}
}
197 changes: 197 additions & 0 deletions src/Consolonia.Core/Drawing/StreamGeometryImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
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 readonly List<Rectangle> _fills;
private readonly List<Line> _strokes;
private Rect _bounds;

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

public IReadOnlyList<Line> Strokes => _strokes;

public IReadOnlyList<Rectangle> Fills => _fills;
// private SKPath _path;

public Rect Bounds => _bounds;

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


public IStreamGeometryImpl Clone()
{
var clone = new StreamGeometryImpl();
foreach (Line line in _strokes) clone._strokes.Add(line);
foreach (Rectangle rect in _fills) clone._fills.Add(rect);
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)
{
Rect 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 LineTo(Point point, bool isStroked)
{
LineTo(point);
}

/// <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();
}

/// <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)
{
Rect 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)
{
}

public void Dispose()
{
}
}
}
}
Loading
Loading