From e01835cc551acad2adb077f213d8f987ca1fd190 Mon Sep 17 00:00:00 2001 From: MarcinZiabek Date: Sat, 22 Apr 2023 09:52:33 +0200 Subject: [PATCH] Minor code refactoring --- Source/QuestPDF/Drawing/DocumentGenerator.cs | 15 ++++++--------- Source/QuestPDF/Elements/Dynamic.cs | 2 +- .../QuestPDF/Infrastructure/TextStyleManager.cs | 6 +++--- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/Source/QuestPDF/Drawing/DocumentGenerator.cs b/Source/QuestPDF/Drawing/DocumentGenerator.cs index 855ab04..3e30ef0 100644 --- a/Source/QuestPDF/Drawing/DocumentGenerator.cs +++ b/Source/QuestPDF/Drawing/DocumentGenerator.cs @@ -64,7 +64,7 @@ namespace QuestPDF.Drawing var container = new DocumentContainer(); document.Compose(container); var content = container.Compose(); - ApplyDefaultTextStyle(content, TextStyle.Default); + ApplyInheritedAndGlobalTexStyle(content, TextStyle.Default); ApplyContentDirection(content, ContentDirection.LeftToRight); var debuggingState = Settings.EnableDebugging ? ApplyDebugging(content) : null; @@ -192,7 +192,7 @@ namespace QuestPDF.Drawing ApplyContentDirection(child, direction); } - internal static void ApplyDefaultTextStyle(this Element? content, TextStyle documentDefaultTextStyle) + internal static void ApplyInheritedAndGlobalTexStyle(this Element? content, TextStyle documentDefaultTextStyle) { if (content == null) return; @@ -202,13 +202,10 @@ namespace QuestPDF.Drawing foreach (var textBlockItem in textBlock.Items) { if (textBlockItem is TextBlockSpan textSpan) - { textSpan.Style = textSpan.Style.ApplyInheritedStyle(documentDefaultTextStyle).ApplyGlobalStyle(); - } - else if (textBlockItem is TextBlockElement textElement) - { - ApplyDefaultTextStyle(textElement.Element, documentDefaultTextStyle); - } + + if (textBlockItem is TextBlockElement textElement) + ApplyInheritedAndGlobalTexStyle(textElement.Element, documentDefaultTextStyle); } return; @@ -221,7 +218,7 @@ namespace QuestPDF.Drawing documentDefaultTextStyle = defaultTextStyleElement.TextStyle.ApplyInheritedStyle(documentDefaultTextStyle); foreach (var child in content.GetChildren()) - ApplyDefaultTextStyle(child, documentDefaultTextStyle); + ApplyInheritedAndGlobalTexStyle(child, documentDefaultTextStyle); } } } \ No newline at end of file diff --git a/Source/QuestPDF/Elements/Dynamic.cs b/Source/QuestPDF/Elements/Dynamic.cs index 450ec19..2e2c5f3 100644 --- a/Source/QuestPDF/Elements/Dynamic.cs +++ b/Source/QuestPDF/Elements/Dynamic.cs @@ -89,7 +89,7 @@ namespace QuestPDF.Elements var container = new DynamicElement(); content(container); - container.ApplyDefaultTextStyle(TextStyle); + container.ApplyInheritedAndGlobalTexStyle(TextStyle); container.ApplyContentDirection(ContentDirection); container.InjectDependencies(PageContext, Canvas); diff --git a/Source/QuestPDF/Infrastructure/TextStyleManager.cs b/Source/QuestPDF/Infrastructure/TextStyleManager.cs index 96abce0..6ab5d33 100644 --- a/Source/QuestPDF/Infrastructure/TextStyleManager.cs +++ b/Source/QuestPDF/Infrastructure/TextStyleManager.cs @@ -228,12 +228,12 @@ namespace QuestPDF.Infrastructure internal static TextStyle ApplyInheritedStyle(this TextStyle style, TextStyle parent) { var cacheKey = (style, parent); - return TextStyleApplyInheritedCache.GetOrAdd(cacheKey, key => key.origin.ApplyStyleProperties(key.parent, overrideStyle: false, overrideFontFamily: false, applyFallback: true).UpdateFontFallback(true)); + return TextStyleApplyInheritedCache.GetOrAdd(cacheKey, key => key.origin.ApplyStyleProperties(key.parent, overrideStyle: false, overrideFontFamily: false, applyFallback: true).UpdateFontFallback(overrideStyle: true)); } internal static TextStyle ApplyGlobalStyle(this TextStyle style) { - return TextStyleApplyGlobalCache.GetOrAdd(style, key => key.ApplyStyleProperties(TextStyle.LibraryDefault, overrideStyle: false, overrideFontFamily: false, applyFallback: true).UpdateFontFallback(false)); + return TextStyleApplyGlobalCache.GetOrAdd(style, key => key.ApplyStyleProperties(TextStyle.LibraryDefault, overrideStyle: false, overrideFontFamily: false, applyFallback: true).UpdateFontFallback(overrideStyle: false)); } private static TextStyle UpdateFontFallback(this TextStyle style, bool overrideStyle) @@ -258,7 +258,7 @@ namespace QuestPDF.Infrastructure result = MutateStyle(result, TextStyleProperty.Color, parent.Color, overrideStyle); result = MutateStyle(result, TextStyleProperty.BackgroundColor, parent.BackgroundColor, overrideStyle); - result = MutateStyle(result, TextStyleProperty.FontFamily, parent.FontFamily, overrideFontFamily); + result = MutateStyle(result, TextStyleProperty.FontFamily, parent.FontFamily, overrideStyle); result = MutateStyle(result, TextStyleProperty.Size, parent.Size, overrideStyle); result = MutateStyle(result, TextStyleProperty.LineHeight, parent.LineHeight, overrideStyle); result = MutateStyle(result, TextStyleProperty.LetterSpacing, parent.LetterSpacing, overrideStyle);