Skip to content

Commit 5ebfdbb

Browse files
Simplify code and add TODO
1 parent 832e0f5 commit 5ebfdbb

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ internal sealed class PathGlyphBuilder : GlyphBuilder
1515
{
1616
private const float Pi = MathF.PI;
1717
private readonly IPathInternals path;
18-
private float xOffset;
19-
private float yOffset;
18+
private Vector2 textOffset;
2019

2120
/// <summary>
2221
/// Initializes a new instance of the <see cref="PathGlyphBuilder"/> class.
@@ -35,31 +34,29 @@ public PathGlyphBuilder(IPath path)
3534
}
3635

3736
/// <inheritdoc/>
38-
protected override void BeginText(FontRectangle bounds)
39-
{
40-
this.yOffset = bounds.Bottom;
41-
this.xOffset = bounds.Left;
42-
}
37+
protected override void BeginText(FontRectangle bounds) => this.textOffset = new(bounds.Left, bounds.Bottom);
4338

4439
/// <inheritdoc/>
4540
protected override void BeginGlyph(FontRectangle bounds) => this.TransformGlyph(bounds);
4641

4742
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4843
private void TransformGlyph(FontRectangle bounds)
4944
{
50-
// Find the intersection point. This should be offset to ensure we rotate at the bottom-center of the glyph.
51-
float halfWidth = (bounds.Right - bounds.Left) * .5F;
52-
Vector2 intersectPoint = new(bounds.Left + halfWidth, bounds.Top);
45+
// Find the intersection point.
46+
// This should be offset to ensure we rotate at the bottom-center of the glyph.
47+
float halfWidth = bounds.Width * .5F;
5348

5449
// Find the point of this intersection along the given path.
55-
SegmentInfo pathPoint = this.path.PointAlongPath(intersectPoint.X - this.xOffset);
50+
SegmentInfo pathPoint = this.path.PointAlongPath(bounds.Left + halfWidth);
5651

5752
// Now offset our target point since we're aligning the bottom-left location of our glyph against the path.
58-
Vector2 targetPoint = pathPoint.Point + new PointF(-halfWidth, intersectPoint.Y - this.yOffset);
53+
// TODO: This is good and accurate when we are vertically alligned to the path however the distance between
54+
// characters in multiline text scales with the angle and vertical offset.
55+
// It would be good to be able to fix this.
56+
Vector2 targetPoint = (Vector2)pathPoint.Point + new Vector2(-halfWidth, bounds.Top) - bounds.Location - this.textOffset;
5957

6058
// Due to how matrix combining works you have to combine this in the reverse order of operation.
61-
// First rotate the glyph then move it.
62-
Matrix3x2 matrix = Matrix3x2.CreateTranslation(targetPoint - bounds.Location) * Matrix3x2.CreateRotation(pathPoint.Angle - Pi, pathPoint.Point);
59+
Matrix3x2 matrix = Matrix3x2.CreateTranslation(targetPoint) * Matrix3x2.CreateRotation(pathPoint.Angle - Pi, pathPoint.Point);
6360
this.Builder.SetTransform(matrix);
6461
}
6562
}

0 commit comments

Comments
 (0)