Browse Source

Fixed handling global text style when setting block-wide style

Marcin Ziąbek 4 years ago
parent
commit
c2ba2addfc

+ 1 - 1
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);
             

+ 12 - 9
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<TextBlock> TextBlocks { get; } = new List<TextBlock>();
         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>())
+                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<TextDescriptor> content)
         {
-            var textBlock = new TextBlock();
-
+            var descriptor = new TextDescriptor();
+            
             if (element is Alignment alignment)
-                textBlock.Alignment = alignment.Horizontal;
+                descriptor.Alignment = alignment.Horizontal;
             
-            var descriptor = new TextDescriptor();
             content?.Invoke(descriptor);
             descriptor.Compose(element);
         }

+ 15 - 10
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()
         {