Skip to content

Commit 96e500f

Browse files
committed
Add support for tracking (letter-spacing)
Introduces a Tracking property to TextOptions to allow uniform adjustment of spacing between characters, measured in em units.
1 parent fd8f253 commit 96e500f

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/SixLabors.Fonts/TextLayout.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,8 @@ VerticalOrientationType.Rotate or
10881088
glyphAdvance = glyph.AdvanceHeight;
10891089
}
10901090

1091+
glyphAdvance += options.Tracking * glyph.FontMetrics.UnitsPerEm;
1092+
10911093
decomposedAdvances[0] = glyphAdvance;
10921094

10931095
if (CodePoint.IsTabulation(codePoint))

src/SixLabors.Fonts/TextOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ public float LineSpacing
170170
/// </summary>
171171
public KerningMode KerningMode { get; set; }
172172

173+
/// <summary>
174+
/// Gets or sets the tracking (letter-spacing) value.
175+
/// Tracking adjusts the spacing between all characters uniformly and is measured in em.
176+
/// Positive values increase spacing, negative values decrease spacing, and zero applies no adjustment.
177+
/// </summary>
178+
public float Tracking { get; set; }
179+
173180
/// <summary>
174181
/// Gets or sets the positioning mode used for rendering decorations.
175182
/// </summary>

tests/SixLabors.Fonts.Tests/TextLayoutTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,25 @@ public void TrueTypeHinting_CanHintSmallOpenSans(char c, FontRectangle expected)
967967
Assert.Equal(expected, actual, Comparer);
968968
}
969969

970+
[Theory]
971+
[InlineData("aaaa", 0, 134.0)]
972+
[InlineData("awwa", 0, 162.1)]
973+
[InlineData("aaaa", 0.1, 153.3)]
974+
[InlineData("awwa", 0.1, 181.4)]
975+
[InlineData("aaaa", 1, 326.1)]
976+
[InlineData("awwa", 1, 354.1)]
977+
public void FontTracking_SpaceCharacters(string text, float tracking, float width)
978+
{
979+
Font font = new FontCollection().Add(TestFonts.OpenSansFile).CreateFont(64);
980+
TextOptions options = new(font)
981+
{
982+
Tracking = tracking,
983+
};
984+
985+
FontRectangle actual = TextMeasurer.MeasureSize(text, options);
986+
Assert.Equal(new FontRectangle(0, 0, width, 35.4f), actual, Comparer);
987+
}
988+
970989
[Fact]
971990
public void CanMeasureTextAdvance()
972991
{

0 commit comments

Comments
 (0)