Skip to content

Commit cb0dc06

Browse files
committed
Add more text layout tests
1 parent 4a081fc commit cb0dc06

File tree

1 file changed

+57
-25
lines changed

1 file changed

+57
-25
lines changed

src/NitroSharp.Tests/TextLayoutTests.cs

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,6 @@ consectetur adipiscing elit.
4949
AssertLines(layout, s.Split('\n'));
5050
}
5151

52-
[Fact]
53-
public void StartWithNewline()
54-
{
55-
TextLayout withoutLinebreak = Layout("meow", null, null);
56-
57-
TextRun[] runs =
58-
{
59-
Regular("\n"),
60-
Regular("meow")
61-
};
62-
63-
TextLayout layout = Layout(runs, null, null);
64-
AssertGlyphs(layout, layout.Lines[1].GlyphSpan, "meow");
65-
for (int i = 0; i < "meow".Length; i++)
66-
{
67-
Assert.True(layout.Glyphs[i].Position.Y > withoutLinebreak.Glyphs[i].Position.Y);
68-
}
69-
}
70-
7152
[Fact]
7253
public void HardBreak_InSeparateTextRun()
7354
{
@@ -115,7 +96,7 @@ public void MultipleRuns_NoBreaks()
11596
};
11697

11798
TextLayout layout = Layout(runs, null, null);
118-
AssertLine(layout, "A gaze falls from the sky.");
99+
AssertLine(layout, layout.Lines[0], "A gaze falls from the sky.");
119100
}
120101

121102
[Fact]
@@ -134,7 +115,53 @@ public void MultipleRuns_AppendEach_NoBreaks()
134115
layout.Append(_rasterizer, run);
135116
}
136117

137-
AssertLine(layout, "A gaze falls from the sky.");
118+
AssertLine(layout, layout.Lines[0], "A gaze falls from the sky.");
119+
}
120+
121+
[Theory]
122+
[InlineData("\r\n")]
123+
[InlineData("\n")]
124+
public void StartWithNewline(string newlineSequence)
125+
{
126+
TextLayout withoutLinebreak = Layout("meow", null, null);
127+
128+
TextRun[] runs =
129+
{
130+
Regular(newlineSequence),
131+
Regular("meow")
132+
};
133+
134+
TextLayout layout = Layout(runs, null, null);
135+
Assert.True(layout.Lines[0].IsEmpty);
136+
AssertLine(layout, layout.Lines[1], "meow");
137+
for (int i = 0; i < "meow".Length; i++)
138+
{
139+
Assert.True(layout.Glyphs[i].Position.Y > withoutLinebreak.Glyphs[i].Position.Y);
140+
}
141+
}
142+
143+
[Theory]
144+
[InlineData("\r\n")]
145+
[InlineData("\n")]
146+
public void MultipleLines_AppendEach(string newlineSequence)
147+
{
148+
TextLayout layout = new();
149+
layout.Append(_rasterizer, Regular("A gaze"));
150+
layout.Append(_rasterizer, Regular(newlineSequence));
151+
layout.Append(_rasterizer, Regular("falls from the sky."));
152+
153+
AssertLines(layout, new[] { "A gaze", "falls from the sky." });
154+
}
155+
156+
[Theory]
157+
[InlineData("\n")]
158+
[InlineData("\r\n")]
159+
public void AppendText_StartingWithNewline(string newlineSequence)
160+
{
161+
TextLayout layout = new();
162+
layout.Append(_rasterizer, Regular("A gaze"));
163+
layout.Append(_rasterizer, Regular($"{newlineSequence}falls from the sky."));
164+
AssertLines(layout, new[] { "A gaze", "falls from the sky." });
138165
}
139166

140167
[Fact]
@@ -145,7 +172,7 @@ public void AppendText_SameLine()
145172
TextRun second = Regular(" falls from the sky.");
146173
layout.Append(_rasterizer, second);
147174
RectangleF bbAfter = layout.BoundingBox;
148-
AssertLine(layout, "A gaze falls from the sky.");
175+
AssertLine(layout, layout.Lines[0], "A gaze falls from the sky.");
149176
Assert.True(bbAfter.Width > bbBefore.Width);
150177

151178
Assert.Equal(
@@ -183,7 +210,7 @@ private void AssertLines(TextLayout layout, string[] lines)
183210
{
184211
for (int i = 0; i < lines.Length; i++)
185212
{
186-
AssertGlyphs(layout, layout.Lines[i].GlyphSpan, lines[i]);
213+
AssertLine(layout, layout.Lines[i], lines[i]);
187214
if (i > 0)
188215
{
189216
PositionedGlyph curStart = layout.Glyphs[layout.Lines[i].GlyphSpan.Start.Value];
@@ -193,9 +220,14 @@ private void AssertLines(TextLayout layout, string[] lines)
193220
}
194221
}
195222

196-
private void AssertLine(TextLayout layout, string text)
223+
private void AssertLine(TextLayout layout, Line line, string text)
197224
{
198-
AssertGlyphs(layout, layout.Lines[0].GlyphSpan, text);
225+
AssertGlyphs(layout, line.GlyphSpan, text);
226+
PositionedGlyph[] glyphs = layout.Glyphs[line.GlyphSpan].ToArray();
227+
var positions = glyphs.Select(x => x.Position.X)
228+
.ToArray();
229+
var orderedByX = positions.OrderBy(x => x);
230+
Assert.Equal(orderedByX, positions);
199231
}
200232

201233
private void AssertGlyphs(TextLayout layout, Range span, string text)

0 commit comments

Comments
 (0)