Fixed handling global text style when setting block-wide style

This commit is contained in:
Marcin Ziąbek
2021-10-07 22:56:51 +02:00
parent e379502a64
commit c2ba2addfc
3 changed files with 29 additions and 21 deletions
+1 -1
View File
@@ -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);
+13 -10
View File
@@ -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();
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);
}
+15 -10
View File
@@ -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()
{