Changed letter-spacing to be relative to the font size

This commit is contained in:
MarcinZiabek
2022-12-10 17:34:07 +01:00
parent d2ba21821f
commit 5082bbbdf2
4 changed files with 28 additions and 10 deletions
+13 -8
View File
@@ -137,8 +137,8 @@ namespace QuestPDF.Examples
.Padding(20)
.Column(column =>
{
var letterSpacing = new[] { -1f, 0f, 2f };
var paragraph = Placeholders.Sentence();
var letterSpacing = new[] { -0.1f, 0f, 0.2f };
var paragraph = Placeholders.Sentence().ToUpper();
foreach (var spacing in letterSpacing)
{
@@ -154,12 +154,12 @@ namespace QuestPDF.Examples
.LetterSpacing(spacing);
nestedColumn.Item()
.Text($"Letter spacing of {spacing} pt")
.Text($"Letter spacing of {spacing} em")
.FontSize(10)
.Italic()
.FontColor(Colors.Blue.Medium);
});
}
});
});
@@ -179,7 +179,7 @@ namespace QuestPDF.Examples
.Padding(50)
.Column(column =>
{
var letterSpacing = new[] { -1f, 0f, 2f };
var letterSpacing = new[] { -0.1f, 0f, 0.2f };
var paragraph = "ينا الألم. في بعض الأحيان ونظراً للالتزامات التي يفرضها علينا";
foreach (var spacing in letterSpacing)
{
@@ -196,7 +196,7 @@ namespace QuestPDF.Examples
.LetterSpacing(spacing);
nestedColumn.Item()
.Text($"Letter spacing of {spacing} pt")
.Text($"Letter spacing of {spacing} em")
.FontSize(10)
.Italic()
.FontColor(Colors.Blue.Medium);
@@ -221,13 +221,18 @@ namespace QuestPDF.Examples
.Padding(50)
.Column(column =>
{
var letterSpacing = new[] { 0f, 20f };
var letterSpacing = new[] { 0f, 0.5f };
var paragraph = "Ţ̴̡̧̤̮̺̤̗͎̱̹͙͎͖͂̿̓́̉̊̀̍͜h̵̞̘͇̾̎̏̅į̵̹̖͔͉̰̎̉̄̐̏͑͂̅̃̃͘͝s̷͓͉̭̭̯̬̥̻̰̩̦̑̀̀͌́̒̍̒̌̇͛̀͛́̎ ̷̡̡̟͕̳̺̝̼͇͔̬̟̖͍̈́̽͜͝͝i̶͔͚̟̊̐͛́͛̄̌ṡ̸̡̤̪͙͍̥͙̟̼̝̰̥͈̿̓̄̿̓͠ ̶̢̦̙͍̯̖̱̰̯͕͔͎̯̝̎͑t̸͖̲̱̼̎͐̎̉̾̎̾̌̅̔̏͘ȩ̶̝̫̙͓̙̣̔̀̌̔̋̂̑̈́̏̀̈͘̕͜͝s̸̫̝̮̻̼͐̅̄̎̎̑͝ț̷̨̢̨̻͈̮̞̆͗̓͊̃̌͂̑̉̕̕͜͝͝";
foreach (var spacing in letterSpacing)
{
column.Item()
.Text($"Letter spacing of {spacing} pt")
.Text($"Letter spacing of {spacing} em")
.FontSize(10)
.Italic()
.FontColor(Colors.Blue.Medium);
+5 -2
View File
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using HarfBuzzSharp;
using QuestPDF.Fluent;
using QuestPDF.Infrastructure;
using SkiaSharp;
using Buffer = HarfBuzzSharp.Buffer;
@@ -49,8 +50,10 @@ namespace QuestPDF.Drawing
// used for letter spacing calculation
var lastCluster = glyphInfos.LastOrDefault().Cluster;
var letterSpacing = (TextStyle.LetterSpacing ?? 0) * (TextStyle.Size ?? 16);
var glyphs = new ShapedGlyph[length];
for (var i = 0; i < length; i++)
{
// letter spacing should be applied between glyph clusters, not between individual glyphs,
@@ -58,7 +61,7 @@ namespace QuestPDF.Drawing
if (lastCluster != glyphInfos[i].Cluster)
{
lastCluster = glyphInfos[i].Cluster;
xOffset += TextStyle.LetterSpacing ?? 0;
xOffset += letterSpacing;
}
glyphs[i] = new ShapedGlyph
@@ -56,6 +56,11 @@ namespace QuestPDF.Fluent
return descriptor;
}
/// <summary>
/// Letter spacing controls space between characters. Value 0 corresponds to normal spacing defined by a font.
/// Positive values create additional space, whereas negative values reduce space between characters.
/// Added / reduced space is relative to the font size.
/// </summary>
public static T LetterSpacing<T>(this T descriptor, float value) where T : TextSpanDescriptor
{
descriptor.MutateTextStyle(x => x.LetterSpacing(value));
+5
View File
@@ -49,6 +49,11 @@ namespace QuestPDF.Fluent
return style.Mutate(TextStyleProperty.LineHeight, value);
}
/// <summary>
/// Letter spacing controls space between characters. Value 0 corresponds to normal spacing defined by a font.
/// Positive values create additional space, whereas negative values reduce space between characters.
/// Added / reduced space is relative to the font size.
/// </summary>
public static TextStyle LetterSpacing(this TextStyle style, float value)
{
return style.Mutate(TextStyleProperty.LetterSpacing, value);