Skip to content

Commit e09fe9b

Browse files
Merge pull request #116 from SixLabors/js/fix-114
Add TextOptions.LineSpacing.
2 parents ab51be9 + 0ddc366 commit e09fe9b

16 files changed

Lines changed: 96 additions & 24 deletions

.github/workflows/build-and-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ jobs:
7676
CI: True
7777
XUNIT_PATH: .\tests\ImageSharp.Drawing.Tests # Required for xunit
7878

79-
- name: Store Output Images after failed tests
79+
- name: Export Failed Output
8080
uses: actions/upload-artifact@v2
8181
if: failure()
8282
with:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ ClientBin/
169169
*.publishsettings
170170
node_modules/
171171
bower_components/
172+
.DS_STORE
172173

173174
# RIA/Silverlight projects
174175
Generated_Code/

ImageSharp.Drawing.sln

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_root", "_root", "{C317F1B1
1414
ci-test.ps1 = ci-test.ps1
1515
Directory.Build.props = Directory.Build.props
1616
Directory.Build.targets = Directory.Build.targets
17-
GitVersion.yml = GitVersion.yml
1817
LICENSE = LICENSE
1918
README.md = README.md
2019
EndProjectSection

src/ImageSharp.Drawing/ImageSharp.Drawing.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</ItemGroup>
2121

2222
<ItemGroup>
23-
<PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta0013"/>
23+
<PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta13.5" />
2424
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.2" />
2525
</ItemGroup>
2626

src/ImageSharp.Drawing/Processing/Processors/Text/DrawTextProcessor{TPixel}.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5-
using System.Buffers;
65
using System.Collections.Generic;
76
using System.Numerics;
87
using SixLabors.Fonts;
98
using SixLabors.ImageSharp.Drawing.Processing.Processors.Drawing;
109
using SixLabors.ImageSharp.Drawing.Shapes.Rasterization;
11-
using SixLabors.ImageSharp.Drawing.Utilities;
1210
using SixLabors.ImageSharp.Memory;
1311
using SixLabors.ImageSharp.PixelFormats;
1412
using SixLabors.ImageSharp.Processing.Processors;
@@ -56,6 +54,7 @@ protected override void BeforeImageApply()
5654
WrappingWidth = this.Options.TextOptions.WrapTextWidth,
5755
HorizontalAlignment = this.Options.TextOptions.HorizontalAlignment,
5856
VerticalAlignment = this.Options.TextOptions.VerticalAlignment,
57+
LineSpacing = this.Options.TextOptions.LineSpacing,
5958
FallbackFontFamilies = this.Options.TextOptions.FallbackFonts,
6059
ColorFontSupport = this.definition.Options.TextOptions.RenderColorFonts ? ColorFontSupport.MicrosoftColrFormat : ColorFontSupport.None,
6160
};

src/ImageSharp.Drawing/Processing/TextOptions.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
using System.Collections.Generic;
55
using SixLabors.Fonts;
6-
using SixLabors.ImageSharp.Drawing.Shapes;
7-
using SixLabors.ImageSharp.PixelFormats;
86

97
namespace SixLabors.ImageSharp.Drawing.Processing
108
{
@@ -16,6 +14,7 @@ public class TextOptions : IDeepCloneable<TextOptions>
1614
private float tabWidth = 4F;
1715
private float dpiX = 72F;
1816
private float dpiY = 72F;
17+
private float lineSpacing = 1F;
1918

2019
/// <summary>
2120
/// Initializes a new instance of the <see cref="TextOptions"/> class.
@@ -29,6 +28,7 @@ private TextOptions(TextOptions source)
2928
this.ApplyKerning = source.ApplyKerning;
3029
this.DpiX = source.DpiX;
3130
this.DpiY = source.DpiY;
31+
this.LineSpacing = source.LineSpacing;
3232
this.HorizontalAlignment = source.HorizontalAlignment;
3333
this.TabWidth = source.TabWidth;
3434
this.WrapTextWidth = source.WrapTextWidth;
@@ -49,10 +49,7 @@ private TextOptions(TextOptions source)
4949
/// </summary>
5050
public float TabWidth
5151
{
52-
get
53-
{
54-
return this.tabWidth;
55-
}
52+
get => this.tabWidth;
5653

5754
set
5855
{
@@ -73,10 +70,7 @@ public float TabWidth
7370
/// </summary>
7471
public float DpiX
7572
{
76-
get
77-
{
78-
return this.dpiX;
79-
}
73+
get => this.dpiX;
8074

8175
set
8276
{
@@ -91,10 +85,7 @@ public float DpiX
9185
/// </summary>
9286
public float DpiY
9387
{
94-
get
95-
{
96-
return this.dpiY;
97-
}
88+
get => this.dpiY;
9889

9990
set
10091
{
@@ -103,6 +94,21 @@ public float DpiY
10394
}
10495
}
10596

97+
/// <summary>
98+
/// Gets or sets the line spacing. Applied as a multiple of the line height.
99+
/// Defaults to 1.
100+
/// </summary>
101+
public float LineSpacing
102+
{
103+
get => this.lineSpacing;
104+
105+
set
106+
{
107+
Guard.IsTrue(value != 0, nameof(this.LineSpacing), "Value must not be equal to 0.");
108+
this.lineSpacing = value;
109+
}
110+
}
111+
106112
/// <summary>
107113
/// Gets or sets a value indicating how to align the text relative to the rendering space.
108114
/// If <see cref="WrapTextWidth"/> is greater than zero it will align relative to the space

tests/ImageSharp.Drawing.Tests/Drawing/Text/DrawTextOnImageTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,61 @@ public void FontShapesAreRenderedCorrectly_LargeText<TPixel>(
204204
false);
205205
}
206206

207+
[Theory]
208+
[WithSolidFilledImages(400, 550, "White", PixelTypes.Rgba32, 1, 5, true)]
209+
[WithSolidFilledImages(400, 550, "White", PixelTypes.Rgba32, 1.5, 3, true)]
210+
[WithSolidFilledImages(400, 550, "White", PixelTypes.Rgba32, 2, 2, true)]
211+
[WithSolidFilledImages(400, 100, "White", PixelTypes.Rgba32, 1, 5, false)]
212+
[WithSolidFilledImages(400, 100, "White", PixelTypes.Rgba32, 1.5, 3, false)]
213+
[WithSolidFilledImages(400, 100, "White", PixelTypes.Rgba32, 2, 2, false)]
214+
public void FontShapesAreRenderedCorrectly_WithLineSpacing<TPixel>(
215+
TestImageProvider<TPixel> provider,
216+
float lineSpacing,
217+
int lineCount,
218+
bool wrap)
219+
where TPixel : unmanaged, IPixel<TPixel>
220+
{
221+
Font font = CreateFont("OpenSans-Regular.ttf", 16);
222+
223+
var sb = new StringBuilder();
224+
string str = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.";
225+
226+
for (int i = 0; i < lineCount; i++)
227+
{
228+
sb.AppendLine(str);
229+
}
230+
231+
var textOptions = new TextOptions
232+
{
233+
ApplyKerning = true,
234+
VerticalAlignment = VerticalAlignment.Top,
235+
HorizontalAlignment = HorizontalAlignment.Left,
236+
LineSpacing = lineSpacing
237+
};
238+
239+
if (wrap)
240+
{
241+
textOptions.WrapTextWidth = 300;
242+
}
243+
244+
var textGraphicsOptions = new TextGraphicsOptions
245+
{
246+
TextOptions = textOptions
247+
};
248+
249+
Color color = Color.Black;
250+
251+
// NET472 is 0.0002 different.
252+
var comparer = ImageComparer.TolerantPercentage(0.0003f);
253+
254+
provider.VerifyOperation(
255+
comparer,
256+
img => img.Mutate(c => c.DrawText(textGraphicsOptions, sb.ToString(), font, color, new PointF(10, 1))),
257+
$"linespacing_{lineSpacing}_linecount_{lineCount}_wrap_{wrap}",
258+
false,
259+
false);
260+
}
261+
207262
[Theory]
208263
[WithSolidFilledImages(200, 150, "White", PixelTypes.Rgba32, 50, 0, 0, "SixLaborsSampleAB.woff", AB)]
209264
[WithSolidFilledImages(900, 150, "White", PixelTypes.Rgba32, 50, 0, 0, "OpenSans-Regular.ttf", TestText)]

tests/ImageSharp.Drawing.Tests/Drawing/Text/TextOptionsTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public void DefaultTextOptionsWrapTextWidth()
7171
Assert.Equal(Expected, this.cloneTextOptions.WrapTextWidth);
7272
}
7373

74+
[Fact]
75+
public void DefaultTextOptionsLineSpacing()
76+
{
77+
const float Expected = 1F;
78+
Assert.Equal(Expected, this.newTextOptions.LineSpacing);
79+
Assert.Equal(Expected, this.cloneTextOptions.LineSpacing);
80+
}
81+
7482
[Fact]
7583
public void NonDefaultClone()
7684
{
@@ -81,6 +89,7 @@ public void NonDefaultClone()
8189
DpiY = 52F,
8290
HorizontalAlignment = HorizontalAlignment.Center,
8391
TabWidth = 3F,
92+
LineSpacing = -1F,
8493
VerticalAlignment = VerticalAlignment.Bottom,
8594
WrapTextWidth = 42F
8695
};
@@ -90,6 +99,7 @@ public void NonDefaultClone()
9099
Assert.Equal(expected.ApplyKerning, actual.ApplyKerning);
91100
Assert.Equal(expected.DpiX, actual.DpiX);
92101
Assert.Equal(expected.DpiY, actual.DpiY);
102+
Assert.Equal(expected.LineSpacing, actual.LineSpacing);
93103
Assert.Equal(expected.HorizontalAlignment, actual.HorizontalAlignment);
94104
Assert.Equal(expected.TabWidth, actual.TabWidth);
95105
Assert.Equal(expected.VerticalAlignment, actual.VerticalAlignment);
@@ -107,12 +117,14 @@ public void CloneIsDeep()
107117
actual.DpiY = 52F;
108118
actual.HorizontalAlignment = HorizontalAlignment.Center;
109119
actual.TabWidth = 3F;
120+
actual.LineSpacing = 2F;
110121
actual.VerticalAlignment = VerticalAlignment.Bottom;
111122
actual.WrapTextWidth = 42F;
112123

113124
Assert.NotEqual(expected.ApplyKerning, actual.ApplyKerning);
114125
Assert.NotEqual(expected.DpiX, actual.DpiX);
115126
Assert.NotEqual(expected.DpiY, actual.DpiY);
127+
Assert.NotEqual(expected.LineSpacing, actual.LineSpacing);
116128
Assert.NotEqual(expected.HorizontalAlignment, actual.HorizontalAlignment);
117129
Assert.NotEqual(expected.TabWidth, actual.TabWidth);
118130
Assert.NotEqual(expected.VerticalAlignment, actual.VerticalAlignment);

tests/ImageSharp.Drawing.Tests/TestUtilities/Attributes/GroupOutputAttribute.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
5+
46
namespace SixLabors.ImageSharp.Drawing.Tests
57
{
6-
using System;
7-
88
/// <summary>
99
/// The output produced by this test class should be grouped into the specified subfolder.
1010
/// </summary>
@@ -17,4 +17,4 @@ public GroupOutputAttribute(string subfolder)
1717

1818
public string Subfolder { get; }
1919
}
20-
}
20+
}

0 commit comments

Comments
 (0)