TextExtensions.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using QuestPDF.Drawing;
  5. using QuestPDF.Elements;
  6. using QuestPDF.Elements.Text;
  7. using QuestPDF.Elements.Text.Items;
  8. using QuestPDF.Infrastructure;
  9. using static System.String;
  10. namespace QuestPDF.Fluent
  11. {
  12. public class TextSpanDescriptor
  13. {
  14. internal TextStyle TextStyle = TextStyle.Default;
  15. internal Action<TextStyle> AssignTextStyle { get; }
  16. internal TextSpanDescriptor(Action<TextStyle> assignTextStyle)
  17. {
  18. AssignTextStyle = assignTextStyle;
  19. }
  20. internal void MutateTextStyle(Func<TextStyle, TextStyle> handler)
  21. {
  22. TextStyle = handler(TextStyle);
  23. AssignTextStyle(TextStyle);
  24. }
  25. }
  26. public delegate string PageNumberFormatter(int? pageNumber);
  27. public class TextPageNumberDescriptor : TextSpanDescriptor
  28. {
  29. internal Action<PageNumberFormatter> AssignFormatFunction { get; }
  30. internal TextPageNumberDescriptor(Action<TextStyle> assignTextStyle, Action<PageNumberFormatter> assignFormatFunction) : base(assignTextStyle)
  31. {
  32. AssignFormatFunction = assignFormatFunction;
  33. }
  34. public TextPageNumberDescriptor Format(PageNumberFormatter formatter)
  35. {
  36. AssignFormatFunction(formatter);
  37. return this;
  38. }
  39. }
  40. public class TextDescriptor
  41. {
  42. private ICollection<TextBlock> TextBlocks { get; } = new List<TextBlock>();
  43. private TextStyle DefaultStyle { get; set; } = TextStyle.Default;
  44. internal HorizontalAlignment Alignment { get; set; } = HorizontalAlignment.Left;
  45. private float Spacing { get; set; } = 0f;
  46. public void DefaultTextStyle(TextStyle style)
  47. {
  48. DefaultStyle = style;
  49. }
  50. public void DefaultTextStyle(Func<TextStyle, TextStyle> style)
  51. {
  52. DefaultStyle = style(TextStyle.Default);
  53. }
  54. public void AlignLeft()
  55. {
  56. Alignment = HorizontalAlignment.Left;
  57. }
  58. public void AlignCenter()
  59. {
  60. Alignment = HorizontalAlignment.Center;
  61. }
  62. public void AlignRight()
  63. {
  64. Alignment = HorizontalAlignment.Right;
  65. }
  66. public void ParagraphSpacing(float value, Unit unit = Unit.Point)
  67. {
  68. Spacing = value.ToPoints(unit);
  69. }
  70. private void AddItemToLastTextBlock(ITextBlockItem item)
  71. {
  72. if (!TextBlocks.Any())
  73. TextBlocks.Add(new TextBlock());
  74. TextBlocks.Last().Items.Add(item);
  75. }
  76. [Obsolete("This element has been renamed since version 2022.3. Please use the overload that returns a TextSpanDescriptor object which allows to specify text style.")]
  77. public void Span(string? text, TextStyle style)
  78. {
  79. Span(text).Style(style);
  80. }
  81. public TextSpanDescriptor Span(string? text)
  82. {
  83. if (text == null)
  84. return new TextSpanDescriptor(_ => { });
  85. var items = text
  86. .Replace("\r", string.Empty)
  87. .Split(new[] { '\n' }, StringSplitOptions.None)
  88. .Select(x => new TextBlockSpan
  89. {
  90. Text = x
  91. })
  92. .ToList();
  93. AddItemToLastTextBlock(items.First());
  94. items
  95. .Skip(1)
  96. .Select(x => new TextBlock
  97. {
  98. Items = new List<ITextBlockItem> { x }
  99. })
  100. .ToList()
  101. .ForEach(TextBlocks.Add);
  102. return new TextSpanDescriptor(x => items.ForEach(y => y.Style = x));
  103. }
  104. public TextSpanDescriptor Line(string? text)
  105. {
  106. text ??= string.Empty;
  107. return Span(text + Environment.NewLine);
  108. }
  109. public TextSpanDescriptor EmptyLine()
  110. {
  111. return Span(Environment.NewLine);
  112. }
  113. private TextPageNumberDescriptor PageNumber(Func<IPageContext, int?> pageNumber)
  114. {
  115. var textBlockItem = new TextBlockPageNumber();
  116. AddItemToLastTextBlock(textBlockItem);
  117. return new TextPageNumberDescriptor(x => textBlockItem.Style = x, x => textBlockItem.Source = context => x(pageNumber(context)));
  118. }
  119. public TextPageNumberDescriptor CurrentPageNumber()
  120. {
  121. return PageNumber(x => x.CurrentPage);
  122. }
  123. public TextPageNumberDescriptor TotalPages()
  124. {
  125. return PageNumber(x => x.GetLocation(PageContext.DocumentLocation)?.Length);
  126. }
  127. [Obsolete("This element has been renamed since version 2022.3. Please use the BeginPageNumberOfSection method.")]
  128. public void PageNumberOfLocation(string locationName, TextStyle? style = null)
  129. {
  130. BeginPageNumberOfSection(locationName).Style(style);
  131. }
  132. public TextPageNumberDescriptor BeginPageNumberOfSection(string locationName)
  133. {
  134. return PageNumber(x => x.GetLocation(locationName)?.PageStart);
  135. }
  136. public TextPageNumberDescriptor EndPageNumberOfSection(string locationName)
  137. {
  138. return PageNumber(x => x.GetLocation(locationName)?.PageEnd);
  139. }
  140. public TextPageNumberDescriptor PageNumberWithinSection(string locationName)
  141. {
  142. return PageNumber(x => x.CurrentPage + 1 - x.GetLocation(locationName)?.PageStart);
  143. }
  144. public TextPageNumberDescriptor TotalPagesWithinSection(string locationName)
  145. {
  146. return PageNumber(x => x.GetLocation(locationName)?.Length);
  147. }
  148. public TextSpanDescriptor SectionLink(string? text, string sectionName)
  149. {
  150. if (IsNullOrEmpty(sectionName))
  151. throw new ArgumentException("Section name cannot be null or empty", nameof(sectionName));
  152. if (IsNullOrEmpty(text))
  153. return new TextSpanDescriptor(_ => { });
  154. var textBlockItem = new TextBlockSectionLink
  155. {
  156. Text = text,
  157. SectionName = sectionName
  158. };
  159. AddItemToLastTextBlock(textBlockItem);
  160. return new TextSpanDescriptor(x => textBlockItem.Style = x);
  161. }
  162. [Obsolete("This element has been renamed since version 2022.3. Please use the SectionLink method.")]
  163. public void InternalLocation(string? text, string locationName, TextStyle? style = null)
  164. {
  165. SectionLink(text, locationName).Style(style);
  166. }
  167. public TextSpanDescriptor Hyperlink(string? text, string url)
  168. {
  169. if (IsNullOrEmpty(url))
  170. throw new ArgumentException("Url cannot be null or empty", nameof(url));
  171. if (IsNullOrEmpty(text))
  172. return new TextSpanDescriptor(_ => { });
  173. var textBlockItem = new TextBlockHyperlink
  174. {
  175. Text = text,
  176. Url = url
  177. };
  178. AddItemToLastTextBlock(textBlockItem);
  179. return new TextSpanDescriptor(x => textBlockItem.Style = x);
  180. }
  181. [Obsolete("This element has been renamed since version 2022.3. Please use the Hyperlink method.")]
  182. public void ExternalLocation(string? text, string url, TextStyle? style = null)
  183. {
  184. Hyperlink(text, url).Style(style);
  185. }
  186. public IContainer Element()
  187. {
  188. var container = new Container();
  189. AddItemToLastTextBlock(new TextBlockElement
  190. {
  191. Element = container
  192. });
  193. return container.AlignBottom().MinimalBox();
  194. }
  195. internal void Compose(IContainer container)
  196. {
  197. TextBlocks.ToList().ForEach(x => x.Alignment = Alignment);
  198. container = container.DefaultTextStyle(DefaultStyle);
  199. if (TextBlocks.Count == 1)
  200. {
  201. container.Element(TextBlocks.First());
  202. return;
  203. }
  204. container.Column(column =>
  205. {
  206. column.Spacing(Spacing);
  207. foreach (var textBlock in TextBlocks)
  208. column.Item().Element(textBlock);
  209. });
  210. }
  211. }
  212. public static class TextExtensions
  213. {
  214. public static void Text(this IContainer element, Action<TextDescriptor> content)
  215. {
  216. var descriptor = new TextDescriptor();
  217. if (element is Alignment alignment)
  218. descriptor.Alignment = alignment.Horizontal;
  219. content?.Invoke(descriptor);
  220. descriptor.Compose(element);
  221. }
  222. [Obsolete("This element has been renamed since version 2022.3. Please use the overload that returns a TextSpanDescriptor object which allows to specify text style.")]
  223. public static void Text(this IContainer element, object? text, TextStyle style)
  224. {
  225. element.Text(text).Style(style);
  226. }
  227. public static TextSpanDescriptor Text(this IContainer element, object? text)
  228. {
  229. var descriptor = (TextSpanDescriptor) null;
  230. element.Text(x => descriptor = x.Span(text?.ToString()));
  231. return descriptor;
  232. }
  233. }
  234. }