TextExtensions.cs 9.1 KB

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