Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure BreakWord wrapping includes at least one glyph #439

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions src/SixLabors.Fonts/TextLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,11 @@ VerticalOrientationType.Rotate or
// If the line is too long, insert a forced line break.
if (textLine.ScaledLineAdvance > wrappingLength)
{
remaining.InsertAt(0, textLine.SplitAt(wrappingLength));
TextLine overflow = textLine.SplitAt(wrappingLength);
if (overflow != textLine)
{
remaining.InsertAt(0, overflow);
}
}
}

Expand All @@ -1249,6 +1253,21 @@ VerticalOrientationType.Rotate or
// Add the final line.
if (textLine.Count > 0)
{
if (shouldWrap && (breakWord || breakAll))
{
while (textLine.ScaledLineAdvance > wrappingLength)
{
TextLine overflow = textLine.SplitAt(wrappingLength);
if (overflow == textLine)
{
break;
}

textLines.Add(textLine.Finalize(options));
textLine = overflow;
}
}

textLines.Add(textLine.Finalize(options));
}

Expand Down Expand Up @@ -1342,29 +1361,40 @@ public TextLine SplitAt(int index)
return this;
}

TextLine result = new();
result.data.AddRange(this.data.GetRange(index, this.data.Count - index));
int count = this.data.Count - index;
TextLine result = new(count);
result.data.AddRange(this.data.GetRange(index, count));
RecalculateLineMetrics(result);

this.data.RemoveRange(index, this.data.Count - index);
this.data.RemoveRange(index, count);
RecalculateLineMetrics(this);
return result;
}

public TextLine SplitAt(float length)
{
TextLine result = new();
float advance = 0;
for (int i = 0; i < this.data.Count; i++)
float advance = this.data[0].ScaledAdvance;

// Ensure at least one glyph is in the line.
// trailing whitespace should be ignored as it is trimmed
// on finalization.
for (int i = 1; i < this.data.Count; i++)
{
GlyphLayoutData glyph = this.data[i];
advance += glyph.ScaledAdvance;
if (CodePoint.IsWhiteSpace(glyph.CodePoint))
{
continue;
}

if (advance >= length)
{
result.data.AddRange(this.data.GetRange(i, this.data.Count - i));
int count = this.data.Count - i;
TextLine result = new(count);
result.data.AddRange(this.data.GetRange(i, count));
RecalculateLineMetrics(result);

this.data.RemoveRange(i, this.data.Count - i);
this.data.RemoveRange(i, count);
RecalculateLineMetrics(this);
return result;
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions tests/SixLabors.Fonts.Tests/TextLayoutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@
}
}

public static TheoryData<char, FontRectangle> OpenSans_Data

Check warning on line 852 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, ubuntu-latest, net6.0, 6.0.x, -x64, false)

Non-constant fields should not be visible

Check warning on line 852 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

Non-constant fields should not be visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2211)

Check warning on line 852 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

Non-constant fields should not be visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2211)
= new()
{
{ '!', new(0F, 0F, 1.1621094F, 7.2753906F) },
Expand Down Expand Up @@ -1206,6 +1206,23 @@
}
}

[Fact]
public void BreakWordEnsuresSingleCharacterPerLine()
{
Font font = CreateRenderingFont(20);
TextOptions options = new(font)
{
WordBreaking = WordBreaking.BreakWord,
WrappingLength = 1
};

const string text = "Hello World!";
int lineCount = TextMeasurer.CountLines(text, options);
Assert.Equal(text.Length - 1, lineCount);

TextLayoutTestUtilities.TestLayout(text, options);
}

private class CaptureGlyphBoundBuilder : IGlyphRenderer
{
public static List<FontRectangle> GenerateGlyphsBoxes(string text, TextOptions options)
Expand All @@ -1215,15 +1232,15 @@
renderer.RenderText(text, options);
return glyphBuilder.GlyphBounds;
}
public readonly List<FontRectangle> GlyphBounds = new();

Check warning on line 1235 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, ubuntu-latest, net6.0, 6.0.x, -x64, false)

Elements should be separated by blank line

Check warning on line 1235 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

Check warning on line 1235 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

public CaptureGlyphBoundBuilder() { }

Check warning on line 1236 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, ubuntu-latest, net6.0, 6.0.x, -x64, false)

Elements should be separated by blank line

Check warning on line 1236 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

Check warning on line 1236 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

bool IGlyphRenderer.BeginGlyph(in FontRectangle bounds, in GlyphRendererParameters parameters)

Check warning on line 1237 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, ubuntu-latest, net6.0, 6.0.x, -x64, false)

Elements should be separated by blank line

Check warning on line 1237 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

Check warning on line 1237 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

{
this.GlyphBounds.Add(bounds);
return true;
}
public void BeginFigure() { }

Check warning on line 1242 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

Check warning on line 1242 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

public void MoveTo(Vector2 point) { }

Check warning on line 1243 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

public void QuadraticBezierTo(Vector2 secondControlPoint, Vector2 point) { }
public void CubicBezierTo(Vector2 secondControlPoint, Vector2 thirdControlPoint, Vector2 point) { }
public void LineTo(Vector2 point) { }
Expand All @@ -1239,7 +1256,7 @@
private static readonly Font OpenSansWoff = new FontCollection().Add(TestFonts.OpenSansFile).CreateFont(10);

#if OS_WINDOWS
public static TheoryData<char, FontRectangle> SegoeUi_Data

Check warning on line 1259 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

Non-constant fields should not be visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2211)

Check warning on line 1259 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)

Non-constant fields should not be visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2211)
= new()
{
{ '!', new(0F, 0F, 1.0839844F, 7.0898438F) },
Expand Down Expand Up @@ -1337,7 +1354,7 @@
{ '}', new(0F, 0F, 2.2558594F, 8.59375F) },
{ '~', new(0F, 0F, 4.8095703F, 1.5136719F) },

};

Check warning on line 1357 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

View workflow job for this annotation

GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)


[Theory]
[MemberData(nameof(SegoeUi_Data))]
Expand Down
Loading