Skip to content

Commit

Permalink
Merge pull request #174 from SixLabors/js/horizontal-alignment
Browse files Browse the repository at this point in the history
Fix Horizontal Alignment
  • Loading branch information
JimBobSquarePants authored May 13, 2021
2 parents 5b7a13c + 07a352e commit 1aab62a
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/SixLabors.Fonts/TextLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,19 @@ public IReadOnlyList<GlyphLayout> GenerateLayout(ReadOnlySpan<char> text, Render
float originX = 0;
if (options.WrappingWidth > 0)
{
// trim trailing white spaces from the text
// Trim trailing white spaces from the text
text = text.TrimEnd(null);
maxWidth = options.WrappingWidth / options.DpiX;

switch (options.HorizontalAlignment)
{
case HorizontalAlignment.Right:
originX = maxWidth;
break;
case HorizontalAlignment.Center:
originX = maxWidth * .5F;
break;
}
}

int codePointCount = CodePoint.GetCodePointCount(text);
Expand Down Expand Up @@ -134,7 +144,7 @@ public IReadOnlyList<GlyphLayout> GenerateLayout(ReadOnlySpan<char> text, Render
top = lineMaxAscender;
break;
case VerticalAlignment.Center:
top = (lineMaxAscender / 2F) - (lineMaxDescender / 2F);
top = (lineMaxAscender * .5F) - (lineMaxDescender * .5F);
break;
case VerticalAlignment.Bottom:
top = -lineMaxDescender;
Expand Down Expand Up @@ -338,7 +348,7 @@ public IReadOnlyList<GlyphLayout> GenerateLayout(ReadOnlySpan<char> text, Render
switch (options.VerticalAlignment)
{
case VerticalAlignment.Center:
offsetY += new Vector2(0, -(totalHeight / 2F));
offsetY += new Vector2(0, -(totalHeight * .5F));
break;
case VerticalAlignment.Bottom:
offsetY += new Vector2(0, -totalHeight);
Expand Down Expand Up @@ -378,7 +388,7 @@ public IReadOnlyList<GlyphLayout> GenerateLayout(ReadOnlySpan<char> text, Render
offsetX = new Vector2(originX - width, 0) + offsetY;
break;
case HorizontalAlignment.Center:
offsetX = new Vector2(originX - (width / 2F), 0) + offsetY;
offsetX = new Vector2(originX - (width * .5F), 0) + offsetY;
break;
}
}
Expand Down

0 comments on commit 1aab62a

Please sign in to comment.