From c2ba2addfc27086ebdcc3668b4f2e55db98ae504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Zi=C4=85bek?= Date: Thu, 7 Oct 2021 22:56:51 +0200 Subject: [PATCH] Fixed handling global text style when setting block-wide style --- QuestPDF/Drawing/DocumentGenerator.cs | 2 +- QuestPDF/Fluent/TextExtensions.cs | 23 +++++++++++++---------- QuestPDF/Infrastructure/TextStyle.cs | 25 +++++++++++++++---------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/QuestPDF/Drawing/DocumentGenerator.cs b/QuestPDF/Drawing/DocumentGenerator.cs index 8b53173..9b0d83b 100644 --- a/QuestPDF/Drawing/DocumentGenerator.cs +++ b/QuestPDF/Drawing/DocumentGenerator.cs @@ -98,7 +98,7 @@ namespace QuestPDF.Drawing } } - private static void ApplyDefaultTextStyle(Container content, TextStyle documentDefaultTextStyle) + private static void ApplyDefaultTextStyle(Element content, TextStyle documentDefaultTextStyle) { documentDefaultTextStyle.ApplyGlobalStyle(TextStyle.LibraryDefault); diff --git a/QuestPDF/Fluent/TextExtensions.cs b/QuestPDF/Fluent/TextExtensions.cs index dd8eb3c..71036ab 100644 --- a/QuestPDF/Fluent/TextExtensions.cs +++ b/QuestPDF/Fluent/TextExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using QuestPDF.Drawing; using QuestPDF.Elements; using QuestPDF.Elements.Text; using QuestPDF.Elements.Text.Items; @@ -13,7 +14,7 @@ namespace QuestPDF.Fluent { private ICollection TextBlocks { get; } = new List(); private TextStyle DefaultStyle { get; set; } = TextStyle.Default; - private HorizontalAlignment Alignment { get; set; } = HorizontalAlignment.Left; + internal HorizontalAlignment Alignment { get; set; } = HorizontalAlignment.Left; private float Spacing { get; set; } = 0f; public void DefaultTextStyle(TextStyle style) @@ -51,7 +52,7 @@ namespace QuestPDF.Fluent public void Span(string text, TextStyle? style = null) { - style ??= DefaultStyle; + style ??= TextStyle.Default; var items = text .Replace("\r", string.Empty) @@ -92,7 +93,7 @@ namespace QuestPDF.Fluent private void PageNumber(string slotName, TextStyle? style = null) { - style ??= DefaultStyle; + style ??= TextStyle.Default; AddItemToLastTextBlock(new TextBlockPageNumber() { @@ -118,7 +119,7 @@ namespace QuestPDF.Fluent public void InternalLocation(string text, string locationName, TextStyle? style = null) { - style ??= DefaultStyle; + style ??= TextStyle.Default; AddItemToLastTextBlock(new TextBlockInternalLink { @@ -130,7 +131,7 @@ namespace QuestPDF.Fluent public void ExternalLocation(string text, string url, TextStyle? style = null) { - style ??= DefaultStyle; + style ??= TextStyle.Default; AddItemToLastTextBlock(new TextBlockExternalLink { @@ -156,6 +157,9 @@ namespace QuestPDF.Fluent { TextBlocks.ToList().ForEach(x => x.Alignment = Alignment); + foreach (var textBlockSpan in TextBlocks.SelectMany(x => x.Children).Where(x => x is TextBlockSpan).Cast()) + textBlockSpan.Style.ApplyParentStyle(DefaultStyle); + container.Stack(stack => { stack.Spacing(Spacing); @@ -170,12 +174,11 @@ namespace QuestPDF.Fluent { public static void Text(this IContainer element, Action content) { - var textBlock = new TextBlock(); - - if (element is Alignment alignment) - textBlock.Alignment = alignment.Horizontal; - var descriptor = new TextDescriptor(); + + if (element is Alignment alignment) + descriptor.Alignment = alignment.Horizontal; + content?.Invoke(descriptor); descriptor.Compose(element); } diff --git a/QuestPDF/Infrastructure/TextStyle.cs b/QuestPDF/Infrastructure/TextStyle.cs index 512e1af..ee8aa5d 100644 --- a/QuestPDF/Infrastructure/TextStyle.cs +++ b/QuestPDF/Infrastructure/TextStyle.cs @@ -34,25 +34,30 @@ namespace QuestPDF.Infrastructure private static TextStyle DefaultTextStyleCache = new TextStyle(); public static TextStyle Default => DefaultTextStyleCache; - internal void ApplyGlobalStyle(TextStyle global) + internal void ApplyGlobalStyle(TextStyle globalStyle) { if (HasGlobalStyleApplied) return; HasGlobalStyleApplied = true; - Color ??= global.Color; - BackgroundColor ??= global.BackgroundColor; - FontType ??= global.FontType; - Size ??= global.Size; - LineHeight ??= global.LineHeight; - FontWeight ??= global.FontWeight; - IsItalic ??= global.IsItalic; - HasStrikethrough ??= global.HasStrikethrough; - HasUnderline ??= global.HasUnderline; + ApplyParentStyle(globalStyle); Key ??= $"{Color}|{BackgroundColor}|{FontType}|{Size}|{LineHeight}|{FontWeight}|{IsItalic}|{HasStrikethrough}|{HasUnderline}"; } + + internal void ApplyParentStyle(TextStyle parentStyle) + { + Color ??= parentStyle.Color; + BackgroundColor ??= parentStyle.BackgroundColor; + FontType ??= parentStyle.FontType; + Size ??= parentStyle.Size; + LineHeight ??= parentStyle.LineHeight; + FontWeight ??= parentStyle.FontWeight; + IsItalic ??= parentStyle.IsItalic; + HasStrikethrough ??= parentStyle.HasStrikethrough; + HasUnderline ??= parentStyle.HasUnderline; + } internal TextStyle Clone() {