TextStyleExtensions.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System;
  2. using QuestPDF.Helpers;
  3. using QuestPDF.Infrastructure;
  4. namespace QuestPDF.Fluent
  5. {
  6. public static class TextStyleExtensions
  7. {
  8. [Obsolete("This element has been renamed since version 2022.3. Please use the FontColor method.")]
  9. public static TextStyle Color(this TextStyle style, string value)
  10. {
  11. return style.FontColor(value);
  12. }
  13. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.fontColor"]/*' />
  14. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="colorParam"]/*' />
  15. public static TextStyle FontColor(this TextStyle style, Color color)
  16. {
  17. return style.Mutate(TextStyleProperty.Color, color);
  18. }
  19. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.backgroundColor"]/*' />
  20. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="colorParam"]/*' />
  21. public static TextStyle BackgroundColor(this TextStyle style, Color color)
  22. {
  23. return style.Mutate(TextStyleProperty.BackgroundColor, color);
  24. }
  25. [Obsolete("This element has been renamed since version 2022.3. Please use the FontFamily method.")]
  26. public static TextStyle FontType(this TextStyle style, string value)
  27. {
  28. return style.FontFamily(value);
  29. }
  30. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.fontFamily"]/*' />
  31. public static TextStyle FontFamily(this TextStyle style, string value)
  32. {
  33. return style.Mutate(TextStyleProperty.FontFamily, value);
  34. }
  35. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.fontFallback"]/*' />
  36. public static TextStyle FontFamilyFallback(this TextStyle style, string value)
  37. {
  38. return style.Mutate(TextStyleProperty.FontFamilyFallback, value);
  39. }
  40. [Obsolete("This element has been renamed since version 2022.3. Please use the FontSize method.")]
  41. public static TextStyle Size(this TextStyle style, float value)
  42. {
  43. return style.FontSize(value);
  44. }
  45. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.fontSize"]/*' />
  46. public static TextStyle FontSize(this TextStyle style, float value)
  47. {
  48. if (value <= 0)
  49. throw new ArgumentException("Font size must be greater than 0.");
  50. return style.Mutate(TextStyleProperty.Size, value);
  51. }
  52. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.lineHeight"]/*' />
  53. public static TextStyle LineHeight(this TextStyle style, float factor = 1)
  54. {
  55. if (factor <= 0)
  56. throw new ArgumentException("Line height must be greater than 0.");
  57. return style.Mutate(TextStyleProperty.LineHeight, factor);
  58. }
  59. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.letterSpacing"]/*' />
  60. public static TextStyle LetterSpacing(this TextStyle style, float factor = 0)
  61. {
  62. return style.Mutate(TextStyleProperty.LetterSpacing, factor);
  63. }
  64. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.italic"]/*' />
  65. public static TextStyle Italic(this TextStyle style, bool value = true)
  66. {
  67. return style.Mutate(TextStyleProperty.IsItalic, value);
  68. }
  69. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.strikethrough"]/*' />
  70. public static TextStyle Strikethrough(this TextStyle style, bool value = true)
  71. {
  72. return style.Mutate(TextStyleProperty.HasStrikethrough, value);
  73. }
  74. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.underline"]/*' />
  75. public static TextStyle Underline(this TextStyle style, bool value = true)
  76. {
  77. return style.Mutate(TextStyleProperty.HasUnderline, value);
  78. }
  79. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.wrapAnywhere"]/*' />
  80. [Obsolete("This setting is not supported since the 2024.3 version. This flag should be handled automatically by the layout engine.")]
  81. public static TextStyle WrapAnywhere(this TextStyle style, bool value = true)
  82. {
  83. return style;
  84. }
  85. #region Weight
  86. public static TextStyle Weight(this TextStyle style, FontWeight weight)
  87. {
  88. return style.Mutate(TextStyleProperty.FontWeight, weight);
  89. }
  90. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.thin"]/*' />
  91. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.remarks"]/*' />
  92. public static TextStyle Thin(this TextStyle style)
  93. {
  94. return style.Weight(FontWeight.Thin);
  95. }
  96. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.extraLight"]/*' />
  97. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.remarks"]/*' />
  98. public static TextStyle ExtraLight(this TextStyle style)
  99. {
  100. return style.Weight(FontWeight.ExtraLight);
  101. }
  102. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.light"]/*' />
  103. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.remarks"]/*' />
  104. public static TextStyle Light(this TextStyle style)
  105. {
  106. return style.Weight(FontWeight.Light);
  107. }
  108. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.normal"]/*' />
  109. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.remarks"]/*' />
  110. public static TextStyle NormalWeight(this TextStyle style)
  111. {
  112. return style.Weight(FontWeight.Normal);
  113. }
  114. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.medium"]/*' />
  115. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.remarks"]/*' />
  116. public static TextStyle Medium(this TextStyle style)
  117. {
  118. return style.Weight(FontWeight.Medium);
  119. }
  120. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.semiBold"]/*' />
  121. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.remarks"]/*' />
  122. public static TextStyle SemiBold(this TextStyle style)
  123. {
  124. return style.Weight(FontWeight.SemiBold);
  125. }
  126. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.bold"]/*' />
  127. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.remarks"]/*' />
  128. public static TextStyle Bold(this TextStyle style)
  129. {
  130. return style.Weight(FontWeight.Bold);
  131. }
  132. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.extraBold"]/*' />
  133. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.remarks"]/*' />
  134. public static TextStyle ExtraBold(this TextStyle style)
  135. {
  136. return style.Weight(FontWeight.ExtraBold);
  137. }
  138. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.black"]/*' />
  139. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.remarks"]/*' />
  140. public static TextStyle Black(this TextStyle style)
  141. {
  142. return style.Weight(FontWeight.Black);
  143. }
  144. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.extraBlack"]/*' />
  145. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.weight.remarks"]/*' />
  146. public static TextStyle ExtraBlack(this TextStyle style)
  147. {
  148. return style.Weight(FontWeight.ExtraBlack);
  149. }
  150. #endregion
  151. #region Position
  152. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.position.normal"]/*' />
  153. public static TextStyle NormalPosition(this TextStyle style)
  154. {
  155. return style.Position(FontPosition.Normal);
  156. }
  157. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.position.subscript"]/*' />
  158. public static TextStyle Subscript(this TextStyle style)
  159. {
  160. return style.Position(FontPosition.Subscript);
  161. }
  162. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.position.superscript"]/*' />
  163. public static TextStyle Superscript(this TextStyle style)
  164. {
  165. return style.Position(FontPosition.Superscript);
  166. }
  167. private static TextStyle Position(this TextStyle style, FontPosition fontPosition)
  168. {
  169. return style.Mutate(TextStyleProperty.FontPosition, fontPosition);
  170. }
  171. #endregion
  172. #region Fallback
  173. [Obsolete("This setting is not supported since the 2024.3 version. Please use the FontFamilyFallback method or rely on the new automated fallback mechanism.")]
  174. public static TextStyle Fallback(this TextStyle style, TextStyle? value = null)
  175. {
  176. return style;
  177. }
  178. [Obsolete("This setting is not supported since the 2024.3 version. Please use the FontFamilyFallback method or rely on the new automated fallback mechanism.")]
  179. public static TextStyle Fallback(this TextStyle style, Func<TextStyle, TextStyle> handler)
  180. {
  181. return style.Fallback(handler(TextStyle.Default));
  182. }
  183. #endregion
  184. #region Direction
  185. private static TextStyle TextDirection(this TextStyle style, TextDirection textDirection)
  186. {
  187. return style.Mutate(TextStyleProperty.Direction, textDirection);
  188. }
  189. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.direction.auto"]/*' />
  190. public static TextStyle DirectionAuto(this TextStyle style)
  191. {
  192. return style.TextDirection(Infrastructure.TextDirection.Auto);
  193. }
  194. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.direction.ltr"]/*' />
  195. public static TextStyle DirectionFromLeftToRight(this TextStyle style)
  196. {
  197. return style.TextDirection(Infrastructure.TextDirection.LeftToRight);
  198. }
  199. /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.direction.rtl"]/*' />
  200. public static TextStyle DirectionFromRightToLeft(this TextStyle style)
  201. {
  202. return style.TextDirection(Infrastructure.TextDirection.RightToLeft);
  203. }
  204. #endregion
  205. }
  206. }