From 5082bbbdf2cf1b4e2656fe2452c462b3832370dc Mon Sep 17 00:00:00 2001 From: MarcinZiabek Date: Sat, 10 Dec 2022 17:34:07 +0100 Subject: [PATCH] Changed letter-spacing to be relative to the font size --- QuestPDF.Examples/TextExamples.cs | 21 ++++++++++++------- QuestPDF/Drawing/TextShaper.cs | 7 +++++-- .../Fluent/TextSpanDescriptorExtensions.cs | 5 +++++ QuestPDF/Fluent/TextStyleExtensions.cs | 5 +++++ 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/QuestPDF.Examples/TextExamples.cs b/QuestPDF.Examples/TextExamples.cs index ecb5d95..c2099ee 100644 --- a/QuestPDF.Examples/TextExamples.cs +++ b/QuestPDF.Examples/TextExamples.cs @@ -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); diff --git a/QuestPDF/Drawing/TextShaper.cs b/QuestPDF/Drawing/TextShaper.cs index 40ea223..97a4392 100644 --- a/QuestPDF/Drawing/TextShaper.cs +++ b/QuestPDF/Drawing/TextShaper.cs @@ -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 diff --git a/QuestPDF/Fluent/TextSpanDescriptorExtensions.cs b/QuestPDF/Fluent/TextSpanDescriptorExtensions.cs index 30dcdaa..74222b5 100644 --- a/QuestPDF/Fluent/TextSpanDescriptorExtensions.cs +++ b/QuestPDF/Fluent/TextSpanDescriptorExtensions.cs @@ -56,6 +56,11 @@ namespace QuestPDF.Fluent return descriptor; } + /// + /// 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. + /// public static T LetterSpacing(this T descriptor, float value) where T : TextSpanDescriptor { descriptor.MutateTextStyle(x => x.LetterSpacing(value)); diff --git a/QuestPDF/Fluent/TextStyleExtensions.cs b/QuestPDF/Fluent/TextStyleExtensions.cs index 2fe2d6a..b6f3906 100644 --- a/QuestPDF/Fluent/TextStyleExtensions.cs +++ b/QuestPDF/Fluent/TextStyleExtensions.cs @@ -49,6 +49,11 @@ namespace QuestPDF.Fluent return style.Mutate(TextStyleProperty.LineHeight, value); } + /// + /// 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. + /// public static TextStyle LetterSpacing(this TextStyle style, float value) { return style.Mutate(TextStyleProperty.LetterSpacing, value);