Skip to content

Commit 85d2d97

Browse files
Respect horizontal and vertical alignment.
1 parent 8f0c9ff commit 85d2d97

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ internal sealed class PathGlyphBuilder : GlyphBuilder
1616
private const float Pi = MathF.PI;
1717
private readonly IPathInternals path;
1818
private Vector2 textOffset;
19+
private TextOptions textOptions;
1920

2021
/// <summary>
2122
/// Initializes a new instance of the <see cref="PathGlyphBuilder"/> class.
2223
/// </summary>
2324
/// <param name="path">The path to render the glyphs along.</param>
24-
public PathGlyphBuilder(IPath path)
25+
/// <param name="textOptions">The text rendering options.</param>
26+
public PathGlyphBuilder(IPath path, TextOptions textOptions)
2527
{
2628
if (path is IPathInternals internals)
2729
{
@@ -31,10 +33,30 @@ public PathGlyphBuilder(IPath path)
3133
{
3234
this.path = new ComplexPolygon(path);
3335
}
36+
37+
this.textOptions = textOptions;
3438
}
3539

3640
/// <inheritdoc/>
37-
protected override void BeginText(FontRectangle bounds) => this.textOffset = new(bounds.Left, bounds.Bottom);
41+
protected override void BeginText(FontRectangle bounds)
42+
{
43+
float yOffset = this.textOptions.VerticalAlignment switch
44+
{
45+
VerticalAlignment.Center => bounds.Bottom - (bounds.Height * .5F),
46+
VerticalAlignment.Bottom => bounds.Bottom,
47+
VerticalAlignment.Top => bounds.Top,
48+
_ => bounds.Top,
49+
};
50+
51+
float xOffset = this.textOptions.HorizontalAlignment switch
52+
{
53+
HorizontalAlignment.Center => bounds.Right - (bounds.Width * .5F),
54+
HorizontalAlignment.Right => bounds.Right,
55+
HorizontalAlignment.Left => bounds.Left,
56+
_ => bounds.Left,
57+
};
58+
this.textOffset = new(xOffset, yOffset);
59+
}
3860

3961
/// <inheritdoc/>
4062
protected override void BeginGlyph(FontRectangle bounds) => this.TransformGlyph(bounds);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static IPathCollection GenerateGlyphs(string text, TextOptions textOption
3636
/// <returns>The <see cref="IPathCollection"/></returns>
3737
public static IPathCollection GenerateGlyphs(string text, IPath path, TextOptions textOptions)
3838
{
39-
PathGlyphBuilder glyphBuilder = new(path);
39+
PathGlyphBuilder glyphBuilder = new(path, textOptions);
4040
TextRenderer renderer = new(glyphBuilder);
4141

4242
renderer.RenderText(text, textOptions);

0 commit comments

Comments
 (0)