Skip to content

Commit

Permalink
Ensure we never add empty text lines
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants committed Feb 15, 2024
1 parent 3bf24e4 commit 83d24b7
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/SixLabors.Fonts/TextLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ private static TextBox BreakLines(
else if (shouldWrap && lineAdvance + glyphAdvance >= wrappingLength)
{
// Forced wordbreak
if (breakAll)
if (breakAll && textLine.Count > 0)
{
textLines.Add(textLine.Finalize());
glyphCount += textLine.Count;
Expand All @@ -1086,7 +1086,7 @@ private static TextBox BreakLines(
lineAdvance = split.ScaledLineAdvance;
}
}
else
else if (textLine.Count > 0)
{
textLines.Add(textLine.Finalize());
glyphCount += textLine.Count;
Expand Down Expand Up @@ -1123,15 +1123,15 @@ private static TextBox BreakLines(
textLine = split;
lineAdvance = split.ScaledLineAdvance;
}
else if (breakWord)
else if (breakWord && textLine.Count > 0)
{
textLines.Add(textLine.Finalize());
glyphCount += textLine.Count;
textLine = new();
lineAdvance = 0;
}
}
else if (breakWord)
else if (breakWord && textLine.Count > 0)
{
textLines.Add(textLine.Finalize());
glyphCount += textLine.Count;
Expand Down
94 changes: 94 additions & 0 deletions tests/SixLabors.Fonts.Tests/Issues/Issues_383.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

#if OS_WINDOWS
using System.Numerics;

namespace SixLabors.Fonts.Tests.Issues;

public class Issues_383
{
[Fact]
public void CanBreakLinesWithShortWrappingLength()
{
FontFamily fontFamily = SystemFonts.Get("Yu Gothic");
Font font = fontFamily.CreateFont(20.0F);

TextOptions textOption = new(font)
{
WrappingLength = 10.0F,
WordBreaking = WordBreaking.BreakAll
};

// OK
TextRenderer.RenderTextTo(new DummyGlyphRenderer(), "i", textOption);

// OK
TextRenderer.RenderTextTo(new DummyGlyphRenderer(), "v", textOption);

// raise ArgumentOutOfRangeException
TextRenderer.RenderTextTo(new DummyGlyphRenderer(), "a", textOption);

textOption.WrappingLength = 9.0F;

// OK
TextRenderer.RenderTextTo(new DummyGlyphRenderer(), "i", textOption);

// raise ArgumentOutOfRangeException
TextRenderer.RenderTextTo(new DummyGlyphRenderer(), "v", textOption);

// OK
TextRenderer.RenderTextTo(new DummyGlyphRenderer(), "i\r\nv", textOption);

// raise ArgumentOutOfRangeException
TextRenderer.RenderTextTo(new DummyGlyphRenderer(), "v\r\ni", textOption);
}
}

internal class DummyGlyphRenderer : IGlyphRenderer
{
public void BeginFigure()
{
}

public bool BeginGlyph(in FontRectangle bounds, in GlyphRendererParameters parameters) => true;

public void BeginText(in FontRectangle bounds)
{
}

public void CubicBezierTo(Vector2 secondControlPoint, Vector2 thirdControlPoint, Vector2 point)
{
}

public TextDecorations EnabledDecorations() => TextDecorations.None;

public void EndFigure()
{
}

public void EndGlyph()
{
}

public void EndText()
{
}

public void LineTo(Vector2 point)
{
}

public void MoveTo(Vector2 point)
{
}

public void QuadraticBezierTo(Vector2 secondControlPoint, Vector2 point)
{
}

public void SetDecoration(TextDecorations textDecorations, Vector2 start, Vector2 end, float thickness)
{
}
}
#endif

0 comments on commit 83d24b7

Please sign in to comment.