Browse Source

Code refactoring

MarcinZiabek 2 years ago
parent
commit
f79bd1db62

+ 1 - 1
Source/QuestPDF.UnitTests/TextStyleTests.cs

@@ -31,7 +31,7 @@ namespace QuestPDF.UnitTests
                 .BackgroundColor(Colors.Red.Lighten2);
                 .BackgroundColor(Colors.Red.Lighten2);
             
             
             // act
             // act
-            var targetStyle = spanTextStyle.ApplyGlobalStyle(defaultTextStyle, true).ApplyGlobalStyle(TextStyle.LibraryDefault, false);
+            var targetStyle = spanTextStyle.ApplyInheritedStyle(defaultTextStyle).ApplyGlobalStyle();
             
             
             // assert
             // assert
             var expectedStyle = TextStyle.LibraryDefault with
             var expectedStyle = TextStyle.LibraryDefault with

+ 3 - 4
Source/QuestPDF/Drawing/DocumentGenerator.cs

@@ -203,8 +203,7 @@ namespace QuestPDF.Drawing
                 {
                 {
                     if (textBlockItem is TextBlockSpan textSpan)
                     if (textBlockItem is TextBlockSpan textSpan)
                     {
                     {
-                        textSpan.Style = textSpan.Style.ApplyGlobalStyle(documentDefaultTextStyle, true);
-                        textSpan.Style = textSpan.Style.ApplyGlobalStyle(TextStyle.LibraryDefault, false);
+                        textSpan.Style = textSpan.Style.ApplyInheritedStyle(documentDefaultTextStyle).ApplyGlobalStyle();
                     }
                     }
                     else if (textBlockItem is TextBlockElement textElement)
                     else if (textBlockItem is TextBlockElement textElement)
                     {
                     {
@@ -216,10 +215,10 @@ namespace QuestPDF.Drawing
             }
             }
 
 
             if (content is DynamicHost dynamicHost)
             if (content is DynamicHost dynamicHost)
-                dynamicHost.TextStyle = dynamicHost.TextStyle.ApplyGlobalStyle(documentDefaultTextStyle, true);
+                dynamicHost.TextStyle = dynamicHost.TextStyle.ApplyInheritedStyle(documentDefaultTextStyle);
             
             
             if (content is DefaultTextStyle defaultTextStyleElement)
             if (content is DefaultTextStyle defaultTextStyleElement)
-               documentDefaultTextStyle = defaultTextStyleElement.TextStyle.ApplyGlobalStyle(documentDefaultTextStyle, true);
+               documentDefaultTextStyle = defaultTextStyleElement.TextStyle.ApplyInheritedStyle(documentDefaultTextStyle);
 
 
             foreach (var child in content.GetChildren())
             foreach (var child in content.GetChildren())
                 ApplyDefaultTextStyle(child, documentDefaultTextStyle);
                 ApplyDefaultTextStyle(child, documentDefaultTextStyle);

+ 16 - 18
Source/QuestPDF/Infrastructure/TextStyleManager.cs

@@ -25,7 +25,8 @@ namespace QuestPDF.Infrastructure
     internal static class TextStyleManager
     internal static class TextStyleManager
     {
     {
         private static readonly ConcurrentDictionary<(TextStyle origin, TextStyleProperty property, object value), TextStyle> TextStyleMutateCache = new();
         private static readonly ConcurrentDictionary<(TextStyle origin, TextStyleProperty property, object value), TextStyle> TextStyleMutateCache = new();
-        private static readonly ConcurrentDictionary<(TextStyle origin, TextStyle parent), TextStyle> TextStyleApplyGlobalCache = new();
+        private static readonly ConcurrentDictionary<(TextStyle origin, TextStyle parent), TextStyle> TextStyleApplyInheritedCache = new();
+        private static readonly ConcurrentDictionary<TextStyle, TextStyle> TextStyleApplyGlobalCache = new();
         private static readonly ConcurrentDictionary<(TextStyle origin, TextStyle parent), TextStyle> TextStyleOverrideCache = new();
         private static readonly ConcurrentDictionary<(TextStyle origin, TextStyle parent), TextStyle> TextStyleOverrideCache = new();
 
 
         public static TextStyle Mutate(this TextStyle origin, TextStyleProperty property, object value)
         public static TextStyle Mutate(this TextStyle origin, TextStyleProperty property, object value)
@@ -224,18 +225,23 @@ namespace QuestPDF.Infrastructure
             throw new ArgumentOutOfRangeException(nameof(property), property, "Expected to mutate the TextStyle object. Provided property type is not supported.");
             throw new ArgumentOutOfRangeException(nameof(property), property, "Expected to mutate the TextStyle object. Provided property type is not supported.");
         }
         }
         
         
-        internal static TextStyle ApplyGlobalStyle(this TextStyle style, TextStyle parent, bool overrideStyle)
+        internal static TextStyle ApplyInheritedStyle(this TextStyle style, TextStyle parent)
         {
         {
             var cacheKey = (style, parent);
             var cacheKey = (style, parent);
-            return TextStyleApplyGlobalCache.GetOrAdd(cacheKey, key => ApplyStyle(key.origin, key.parent, overrideStyle: false).ApplyFontFallback(overrideStyle));
+            return TextStyleApplyInheritedCache.GetOrAdd(cacheKey, key => key.origin.ApplyStyleProperties(key.parent, overrideStyle: false, overrideFontFamily: true).UpdateFontFallback(true));
         }
         }
         
         
-        private static TextStyle ApplyFontFallback(this TextStyle style, bool overrideStyle)
+        internal static TextStyle ApplyGlobalStyle(this TextStyle style)
+        {
+            return TextStyleApplyGlobalCache.GetOrAdd(style, key => key.ApplyStyleProperties(TextStyle.LibraryDefault, overrideStyle: false).UpdateFontFallback(false));
+        }
+        
+        private static TextStyle UpdateFontFallback(this TextStyle style, bool overrideStyle)
         {
         {
             var targetFallbackStyle = style
             var targetFallbackStyle = style
                 ?.Fallback
                 ?.Fallback
-                ?.ApplyStyle(style, overrideStyle: overrideStyle, overrideFontFamily: false, applyFallback: false)
-                ?.ApplyFontFallback(overrideStyle);
+                ?.ApplyStyleProperties(style, overrideStyle: overrideStyle,  applyFallback: false)
+                ?.UpdateFontFallback(overrideStyle);
             
             
             return MutateStyle(style, TextStyleProperty.Fallback, targetFallbackStyle);
             return MutateStyle(style, TextStyleProperty.Fallback, targetFallbackStyle);
         }
         }
@@ -243,24 +249,16 @@ namespace QuestPDF.Infrastructure
         internal static TextStyle OverrideStyle(this TextStyle style, TextStyle parent)
         internal static TextStyle OverrideStyle(this TextStyle style, TextStyle parent)
         {
         {
             var cacheKey = (style, parent);
             var cacheKey = (style, parent);
-            
-            return TextStyleOverrideCache.GetOrAdd(cacheKey, key =>
-            {
-                var result = ApplyStyle(key.origin, key.parent);
-                return MutateStyle(result, TextStyleProperty.Fallback, key.parent.Fallback);
-            });
+            return TextStyleOverrideCache.GetOrAdd(cacheKey, key => ApplyStyleProperties(key.origin, key.parent, overrideStyle: true, overrideFontFamily: true, applyFallback: true));
         }
         }
         
         
-        private static TextStyle ApplyStyle(this TextStyle style, TextStyle parent, bool overrideStyle = true, bool overrideFontFamily = true, bool applyFallback = true)
+        private static TextStyle ApplyStyleProperties(this TextStyle style, TextStyle parent, bool overrideStyle = true, bool overrideFontFamily = false, bool applyFallback = true)
         {
         {
             var result = style;
             var result = style;
-
+            
             result = MutateStyle(result, TextStyleProperty.Color, parent.Color, overrideStyle);
             result = MutateStyle(result, TextStyleProperty.Color, parent.Color, overrideStyle);
             result = MutateStyle(result, TextStyleProperty.BackgroundColor, parent.BackgroundColor, overrideStyle);
             result = MutateStyle(result, TextStyleProperty.BackgroundColor, parent.BackgroundColor, overrideStyle);
-            
-            if (style.FontFamily == null)
-                result = MutateStyle(result, TextStyleProperty.FontFamily, parent.FontFamily, overrideFontFamily);
-            
+            result = MutateStyle(result, TextStyleProperty.FontFamily, parent.FontFamily, overrideFontFamily);
             result = MutateStyle(result, TextStyleProperty.Size, parent.Size, overrideStyle);
             result = MutateStyle(result, TextStyleProperty.Size, parent.Size, overrideStyle);
             result = MutateStyle(result, TextStyleProperty.LineHeight, parent.LineHeight, overrideStyle);
             result = MutateStyle(result, TextStyleProperty.LineHeight, parent.LineHeight, overrideStyle);
             result = MutateStyle(result, TextStyleProperty.LetterSpacing, parent.LetterSpacing, overrideStyle);
             result = MutateStyle(result, TextStyleProperty.LetterSpacing, parent.LetterSpacing, overrideStyle);