Skip to content

Commit 25648d4

Browse files
committed
Add drawing centric path building api.
-migrate GlyphRenderer to use new helper class directly
1 parent 5d46d68 commit 25648d4

4 files changed

Lines changed: 121 additions & 40 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Numerics;
5+
6+
namespace SixLabors.ImageSharp.Drawing
7+
{
8+
/// <summary>
9+
/// Allow you to derivatively draw shapes and paths.
10+
/// </summary>
11+
public class PathDrawer
12+
{
13+
private readonly PathBuilder builder;
14+
private Vector2 currentPoint = default;
15+
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="PathDrawer"/> class.
18+
/// </summary>
19+
/// <param name="pathBuilder">The path build to draw to.</param>
20+
public PathDrawer(PathBuilder pathBuilder) => this.builder = pathBuilder;
21+
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="PathDrawer"/> class.
24+
/// </summary>
25+
public PathDrawer()
26+
: this(new PathBuilder())
27+
{
28+
}
29+
30+
/// <summary>
31+
/// Clears all drawn paths, Leaving any applied transforms.
32+
/// </summary>
33+
public void Clear() => this.builder.Clear();
34+
35+
/// <summary>
36+
/// Begins the figure.
37+
/// </summary>
38+
public void StartFigure() => this.builder.StartFigure();
39+
40+
/// <summary>
41+
/// Draws a cubic bezier from the current point to the <paramref name="point"/>
42+
/// </summary>
43+
/// <param name="secondControlPoint">The second control point.</param>
44+
/// <param name="thirdControlPoint">The third control point.</param>
45+
/// <param name="point">The point.</param>
46+
public void CubicBezierTo(Vector2 secondControlPoint, Vector2 thirdControlPoint, Vector2 point)
47+
{
48+
this.builder.AddBezier(this.currentPoint, secondControlPoint, thirdControlPoint, point);
49+
this.currentPoint = point;
50+
}
51+
52+
/// <summary>
53+
/// Ends the glyph.
54+
/// </summary>
55+
/// <returns>The built path</returns>
56+
public IPath Build() => this.builder.Build();
57+
58+
/// <summary>
59+
/// Ends the figure.
60+
/// </summary>
61+
public void CloseFigure() => this.builder.CloseFigure();
62+
63+
/// <summary>
64+
/// Draws a line from the current point to the <paramref name="point"/>.
65+
/// </summary>
66+
/// <param name="point">The point.</param>
67+
public void LineTo(Vector2 point)
68+
{
69+
this.builder.AddLine(this.currentPoint, point);
70+
this.currentPoint = point;
71+
}
72+
73+
/// <summary>
74+
/// Moves to current point to the supplied vector.
75+
/// </summary>
76+
/// <param name="point">The point.</param>
77+
public void MoveTo(Vector2 point)
78+
{
79+
this.builder.StartFigure();
80+
this.currentPoint = point;
81+
}
82+
83+
/// <summary>
84+
/// Draws a quadratics bezier from the current point to the <paramref name="point"/>
85+
/// </summary>
86+
/// <param name="secondControlPoint">The second control point.</param>
87+
/// <param name="point">The point.</param>
88+
public void QuadraticBezierTo(Vector2 secondControlPoint, Vector2 point)
89+
{
90+
this.builder.AddBezier(this.currentPoint, secondControlPoint, point);
91+
this.currentPoint = point;
92+
}
93+
94+
/// <summary>
95+
/// Sets the translation to be applied to all items to follow being applied to the <see cref="PathDrawer"/>.
96+
/// </summary>
97+
/// <param name="translation">The translation.</param>
98+
public void SetTransform(Matrix3x2 translation) => this.builder.SetTransform(translation);
99+
100+
/// <summary>
101+
/// Sets the origin all subsequent point should be relative to.
102+
/// </summary>
103+
/// <param name="origin">The origin.</param>
104+
public void SetOrigin(Vector2 origin) => this.builder.SetOrigin(origin);
105+
}
106+
}

src/ImageSharp.Drawing/Shapes/Text/BaseGlyphBuilder.cs

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@ internal class BaseGlyphBuilder : IGlyphRenderer
1717
/// The builder. TODO: Should this be a property?
1818
/// </summary>
1919
// ReSharper disable once InconsistentNaming
20-
protected readonly PathBuilder builder;
20+
protected readonly PathDrawer drawer;
2121
#pragma warning restore SA1401 // Fields should be private
22-
private readonly List<IPath> paths = new List<IPath>();
23-
private Vector2 currentPoint = default;
22+
private readonly List<IPath> paths = new();
2423

2524
/// <summary>
2625
/// Initializes a new instance of the <see cref="BaseGlyphBuilder"/> class.
2726
/// </summary>
28-
public BaseGlyphBuilder()
29-
{
30-
// glyphs are renderd realative to bottom left so invert the Y axis to allow it to render on top left origin surface
31-
this.builder = new PathBuilder();
32-
}
27+
public BaseGlyphBuilder() => this.drawer = new PathDrawer();
3328

3429
/// <summary>
3530
/// Gets the paths that have been rendered by this.
@@ -43,14 +38,12 @@ void IGlyphRenderer.EndText()
4338

4439
/// <inheritdoc/>
4540
void IGlyphRenderer.BeginText(FontRectangle bounds)
46-
{
47-
this.BeginText(bounds);
48-
}
41+
=> this.BeginText(bounds);
4942

5043
/// <inheritdoc/>
5144
bool IGlyphRenderer.BeginGlyph(FontRectangle bounds, GlyphRendererParameters paramaters)
5245
{
53-
this.builder.Clear();
46+
this.drawer.Clear();
5447
this.BeginGlyph(bounds);
5548
return true;
5649
}
@@ -59,9 +52,7 @@ bool IGlyphRenderer.BeginGlyph(FontRectangle bounds, GlyphRendererParameters par
5952
/// Begins the figure.
6053
/// </summary>
6154
void IGlyphRenderer.BeginFigure()
62-
{
63-
this.builder.StartFigure();
64-
}
55+
=> this.drawer.StartFigure();
6556

6657
/// <summary>
6758
/// Draws a cubic bezier from the current point to the <paramref name="point"/>
@@ -70,57 +61,41 @@ void IGlyphRenderer.BeginFigure()
7061
/// <param name="thirdControlPoint">The third control point.</param>
7162
/// <param name="point">The point.</param>
7263
void IGlyphRenderer.CubicBezierTo(Vector2 secondControlPoint, Vector2 thirdControlPoint, Vector2 point)
73-
{
74-
this.builder.AddBezier(this.currentPoint, secondControlPoint, thirdControlPoint, point);
75-
this.currentPoint = point;
76-
}
64+
=> this.drawer.CubicBezierTo(secondControlPoint, thirdControlPoint, point);
7765

7866
/// <summary>
7967
/// Ends the glyph.
8068
/// </summary>
8169
void IGlyphRenderer.EndGlyph()
82-
{
83-
this.paths.Add(this.builder.Build());
84-
}
70+
=> this.paths.Add(this.drawer.Build());
8571

8672
/// <summary>
8773
/// Ends the figure.
8874
/// </summary>
8975
void IGlyphRenderer.EndFigure()
90-
{
91-
this.builder.CloseFigure();
92-
}
76+
=> this.drawer.CloseFigure();
9377

9478
/// <summary>
9579
/// Draws a line from the current point to the <paramref name="point"/>.
9680
/// </summary>
9781
/// <param name="point">The point.</param>
9882
void IGlyphRenderer.LineTo(Vector2 point)
99-
{
100-
this.builder.AddLine(this.currentPoint, point);
101-
this.currentPoint = point;
102-
}
83+
=> this.drawer.LineTo(point);
10384

10485
/// <summary>
10586
/// Moves to current point to the supplied vector.
10687
/// </summary>
10788
/// <param name="point">The point.</param>
10889
void IGlyphRenderer.MoveTo(Vector2 point)
109-
{
110-
this.builder.StartFigure();
111-
this.currentPoint = point;
112-
}
90+
=> this.drawer.MoveTo(point);
11391

11492
/// <summary>
11593
/// Draws a quadratics bezier from the current point to the <paramref name="point"/>
11694
/// </summary>
11795
/// <param name="secondControlPoint">The second control point.</param>
11896
/// <param name="point">The point.</param>
11997
void IGlyphRenderer.QuadraticBezierTo(Vector2 secondControlPoint, Vector2 point)
120-
{
121-
this.builder.AddBezier(this.currentPoint, secondControlPoint, point);
122-
this.currentPoint = point;
123-
}
98+
=> this.drawer.QuadraticBezierTo(secondControlPoint, point);
12499

125100
/// <summary>Called before any glyphs have been rendered.</summary>
126101
/// <param name="rect">The bounds the text will be rendered at and at whats size.</param>

src/ImageSharp.Drawing/Shapes/Text/GlyphBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System.Numerics;
@@ -25,7 +25,7 @@ public GlyphBuilder()
2525
public GlyphBuilder(Vector2 origin)
2626
: base()
2727
{
28-
this.builder.SetOrigin(origin);
28+
this.drawer.SetOrigin(origin);
2929
}
3030
}
3131
}

src/ImageSharp.Drawing/Shapes/Text/PathGlyphBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected override void BeginGlyph(FontRectangle rect)
4545
// Due to how matrix combining works you have to combine this in the reverse order of operation
4646
// this one rotates the glype then moves it.
4747
Matrix3x2 matrix = Matrix3x2.CreateTranslation(targetPoint - rect.Location) * Matrix3x2.CreateRotation(point.Angle - Pi, point.Point);
48-
this.builder.SetTransform(matrix);
48+
this.drawer.SetTransform(matrix);
4949
}
5050
}
5151
}

0 commit comments

Comments
 (0)