Skip to content

Commit

Permalink
fix unit tests for new line positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
tocsoft committed May 24, 2018
1 parent 31af82b commit 0e72a8a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
6 changes: 6 additions & 0 deletions SixLabors.Fonts.v3.ncrunchsolution
@@ -0,0 +1,6 @@
<SolutionConfiguration>
<Settings>
<AllowParallelTestExecution>True</AllowParallelTestExecution>
<SolutionConfigured>True</SolutionConfigured>
</Settings>
</SolutionConfiguration>
4 changes: 1 addition & 3 deletions src/SixLabors.Fonts/SixLabors.Fonts.csproj
Expand Up @@ -43,9 +43,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004" PrivateAssets="All" />
<PackageReference Include="System.Collections.Immutable" Version="1.4.0" />
<PackageReference Include="SixLabors.Core" Version="1.0.0-beta0005" />
</ItemGroup>
Expand Down
44 changes: 27 additions & 17 deletions tests/SixLabors.Fonts.Tests/TextLayoutTests.cs
Expand Up @@ -21,17 +21,17 @@ public void FakeFontGetGlyph()
[InlineData(
VerticalAlignment.Top,
HorizontalAlignment.Left,
10,
0,
10)]
[InlineData(
VerticalAlignment.Top,
HorizontalAlignment.Right,
10,
0,
-320)]
[InlineData(
VerticalAlignment.Top,
HorizontalAlignment.Center,
10,
0,
-155)]
[InlineData(
VerticalAlignment.Bottom,
Expand All @@ -51,17 +51,17 @@ public void FakeFontGetGlyph()
[InlineData(
VerticalAlignment.Center,
HorizontalAlignment.Left,
-20,
-25,
10)]
[InlineData(
VerticalAlignment.Center,
HorizontalAlignment.Right,
-20,
-25,
-320)]
[InlineData(
VerticalAlignment.Center,
HorizontalAlignment.Center,
-20,
-25,
-155)]
public void VerticalAlignmentTests(
VerticalAlignment vertical,
Expand Down Expand Up @@ -114,14 +114,15 @@ public void MeasureText(string text, float height, float width)
}

[Fact]
public void TryMeasureCharacterBounds() {
public void TryMeasureCharacterBounds()
{
string text = "a b\nc";
GlyphMetric[] expectedGlyphMetrics = new GlyphMetric[] {
new GlyphMetric('a', new RectangleF(10, 10, 10, 10), false),
new GlyphMetric(' ', new RectangleF(40, 10, 30, 10), false),
new GlyphMetric('b', new RectangleF(70, 10, 10, 10), false),
new GlyphMetric('\n', new RectangleF(100, 10, 0, 10), true),
new GlyphMetric('c', new RectangleF(10, 40, 10, 10), false),
new GlyphMetric('a', new RectangleF(10, 0, 10, 10), false),
new GlyphMetric(' ', new RectangleF(40, 0, 30, 10), false),
new GlyphMetric('b', new RectangleF(70, 0, 10, 10), false),
new GlyphMetric('\n', new RectangleF(100, 0, 0, 10), true),
new GlyphMetric('c', new RectangleF(10, 30, 10, 10), false),
};
Font font = CreateFont(text);

Expand All @@ -130,9 +131,17 @@ public void MeasureText(string text, float height, float width)
Assert.True(TextMeasurer.TryMeasureCharacterBounds(text, new RendererOptions(font, 72 * font.EmSize), out glyphMetrics));

Assert.Equal(text.Length, glyphMetrics.Count);
int i = 0;
foreach (GlyphMetric glyphMetric in glyphMetrics) {
Assert.Equal(expectedGlyphMetrics[i++], glyphMetric);
for (var i = 0; i < glyphMetrics.Count; i++)
{
var expected = expectedGlyphMetrics[i];
var actual = glyphMetrics[i];
Assert.Equal(expected.Character, actual.Character);
Assert.Equal(expected.IsControlCharacter, actual.IsControlCharacter);
// 4 dp as there is minor offset difference in the float values
Assert.Equal(expected.Bounds.X, actual.Bounds.X, 4);
Assert.Equal(expected.Bounds.Y, actual.Bounds.Y, 4);
Assert.Equal(expected.Bounds.Height, actual.Bounds.Height, 4);
Assert.Equal(expected.Bounds.Width, actual.Bounds.Width, 4);
}
}

Expand Down Expand Up @@ -173,7 +182,7 @@ public void MeasureTextWithKerning(string text, float height, float width, bool
}

[Theory]
[InlineData("a", 100, 100, 125, 828)]
[InlineData("a", 100, 100, 125, 452)]
public void LayoutWithLocation(string text, float x, float y, float expectedX, float expectedY)
{
FontCollection c = new FontCollection();
Expand All @@ -184,7 +193,8 @@ public void LayoutWithLocation(string text, float x, float y, float expectedX, f
var renderer = new TextRenderer(glyphRenderer);
renderer.RenderText(text, new RendererOptions(new Font(font, 1), 72 * font.EmSize, new PointF(x, y)));

Assert.Equal(new PointF(expectedX, expectedY), glyphRenderer.GlyphRects[0].Location);
Assert.Equal(expectedX, glyphRenderer.GlyphRects[0].Location.X, 2);
Assert.Equal(expectedY, glyphRenderer.GlyphRects[0].Location.Y, 2);
}

public static Font CreateFont(string text)
Expand Down

0 comments on commit 0e72a8a

Please sign in to comment.