TextStyleTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using System.Collections.Generic;
  2. using NUnit.Framework;
  3. using QuestPDF.Fluent;
  4. using QuestPDF.Helpers;
  5. using QuestPDF.Infrastructure;
  6. using QuestPDF.Skia.Text;
  7. namespace QuestPDF.UnitTests
  8. {
  9. [TestFixture]
  10. public class TextStyleTests
  11. {
  12. #region Text Decoration
  13. [Test]
  14. public void TextDecoration_Default()
  15. {
  16. var textStyle = TextStyle.LibraryDefault;
  17. Assert.That(textStyle.HasStrikethrough, Is.False);
  18. Assert.That(textStyle.HasUnderline, Is.False);
  19. Assert.That(textStyle.HasOverline, Is.False);
  20. Assert.That(textStyle.DecorationColor, Is.EqualTo(Colors.Black));
  21. Assert.That(textStyle.DecorationThickness, Is.EqualTo(1f));
  22. Assert.That(textStyle.DecorationStyle, Is.EqualTo(TextStyleConfiguration.TextDecorationStyle.Solid));
  23. }
  24. [Test]
  25. public void SetsCorrectTextDecoration_Strikethrough()
  26. {
  27. var textStyle = TextStyle.Default.Strikethrough();
  28. Assert.That(textStyle.HasStrikethrough, Is.True);
  29. }
  30. [Test]
  31. public void SetsCorrectTextDecoration_StrikethroughDisabled()
  32. {
  33. var textStyle = TextStyle.Default.Strikethrough().Strikethrough(false);
  34. Assert.That(textStyle.HasStrikethrough, Is.False);
  35. }
  36. [Test]
  37. public void SetsCorrectTextDecoration_Underline()
  38. {
  39. var textStyle = TextStyle.Default.Underline();
  40. Assert.That(textStyle.HasUnderline, Is.True);
  41. }
  42. [Test]
  43. public void SetsCorrectTextDecoration_UnderlineDisabled()
  44. {
  45. var textStyle = TextStyle.Default.Underline().Underline(false);
  46. Assert.That(textStyle.HasUnderline, Is.False);
  47. }
  48. [Test]
  49. public void SetsCorrectTextDecoration_Overline()
  50. {
  51. var textStyle = TextStyle.Default.Overline();
  52. Assert.That(textStyle.HasOverline, Is.True);
  53. }
  54. [Test]
  55. public void SetsCorrectTextDecoration_OverlineDisabled()
  56. {
  57. var textStyle = TextStyle.Default.Overline().Overline(false);
  58. Assert.That(textStyle.HasOverline, Is.False);
  59. }
  60. [Test]
  61. public void SetsCorrectTextDecoration_DecorationColor()
  62. {
  63. var textStyle = TextStyle.Default.DecorationColor(Colors.Red.Medium);
  64. Assert.That(textStyle.DecorationColor, Is.EqualTo(Colors.Red.Medium));
  65. }
  66. [Test]
  67. public void SetsCorrectTextDecoration_DecorationThickness()
  68. {
  69. var textStyle = TextStyle.Default.DecorationThickness(1.5f);
  70. Assert.That(textStyle.DecorationThickness, Is.EqualTo(1.5f));
  71. }
  72. [Test]
  73. public void SetsCorrectTextDecoration_DecorationSolid()
  74. {
  75. var textStyle = TextStyle.Default.DecorationSolid();
  76. Assert.That(textStyle.DecorationStyle, Is.EqualTo(TextStyleConfiguration.TextDecorationStyle.Solid));
  77. }
  78. [Test]
  79. public void SetsCorrectTextDecoration_DecorationDouble()
  80. {
  81. var textStyle = TextStyle.Default.DecorationDouble();
  82. Assert.That(textStyle.DecorationStyle, Is.EqualTo(TextStyleConfiguration.TextDecorationStyle.Double));
  83. }
  84. [Test]
  85. public void SetsCorrectTextDecoration_DecorationWavy()
  86. {
  87. var textStyle = TextStyle.Default.DecorationWavy();
  88. Assert.That(textStyle.DecorationStyle, Is.EqualTo(TextStyleConfiguration.TextDecorationStyle.Wavy));
  89. }
  90. [Test]
  91. public void SetsCorrectTextDecoration_DecorationDotted()
  92. {
  93. var textStyle = TextStyle.Default.DecorationDotted();
  94. Assert.That(textStyle.DecorationStyle, Is.EqualTo(TextStyleConfiguration.TextDecorationStyle.Dotted));
  95. }
  96. [Test]
  97. public void SetsCorrectTextDecoration_DecorationDashed()
  98. {
  99. var textStyle = TextStyle.Default.DecorationDashed();
  100. Assert.That(textStyle.DecorationStyle, Is.EqualTo(TextStyleConfiguration.TextDecorationStyle.Dashed));
  101. }
  102. #endregion
  103. #region Font Weight
  104. [Test]
  105. public void FontWeight_Default()
  106. {
  107. var textStyle = TextStyle.LibraryDefault;
  108. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Normal));
  109. }
  110. [Test]
  111. public void SetsCorrectSetsCorrectFontWeight_Thin()
  112. {
  113. var textStyle = TextStyle.Default.Thin();
  114. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Thin));
  115. }
  116. [Test]
  117. public void SetsCorrectFontWeight_ExtraLight()
  118. {
  119. var textStyle = TextStyle.Default.ExtraLight();
  120. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.ExtraLight));
  121. }
  122. [Test]
  123. public void SetsCorrectFontWeight_Light()
  124. {
  125. var textStyle = TextStyle.Default.Light();
  126. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Light));
  127. }
  128. [Test]
  129. public void SetsCorrectFontWeight_Normal()
  130. {
  131. var textStyle = TextStyle.Default.Bold().NormalWeight(); // first change from default, then normal
  132. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Normal));
  133. }
  134. [Test]
  135. public void SetsCorrectFontWeight_Medium()
  136. {
  137. var textStyle = TextStyle.Default.Medium();
  138. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Medium));
  139. }
  140. [Test]
  141. public void SetsCorrectFontWeight_SemiBold()
  142. {
  143. var textStyle = TextStyle.Default.SemiBold();
  144. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.SemiBold));
  145. }
  146. [Test]
  147. public void SetsCorrectFontWeight_Bold()
  148. {
  149. var textStyle = TextStyle.Default.Bold();
  150. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Bold));
  151. }
  152. [Test]
  153. public void SetsCorrectFontWeight_ExtraBold()
  154. {
  155. var textStyle = TextStyle.Default.ExtraBold();
  156. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.ExtraBold));
  157. }
  158. [Test]
  159. public void SetsCorrectFontWeight_Black()
  160. {
  161. var textStyle = TextStyle.Default.Black();
  162. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Black));
  163. }
  164. [Test]
  165. public void SetsCorrectFontWeight_ExtraBlack()
  166. {
  167. var textStyle = TextStyle.Default.ExtraBlack();
  168. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.ExtraBlack));
  169. }
  170. #endregion
  171. #region Text Position
  172. [Test]
  173. public void TextPosition_Default()
  174. {
  175. var textStyle = TextStyle.LibraryDefault;
  176. Assert.That(textStyle.FontPosition, Is.EqualTo(FontPosition.Normal));
  177. }
  178. [Test]
  179. public void SetsCorrectTextPosition_Subscript()
  180. {
  181. var textStyle = TextStyle.Default.Subscript();
  182. Assert.That(textStyle.FontPosition, Is.EqualTo(FontPosition.Subscript));
  183. }
  184. [Test]
  185. public void SetsCorrectTextPosition_Normal()
  186. {
  187. var textStyle = TextStyle.Default.Subscript().NormalPosition(); // first change from default, then normal
  188. Assert.That(textStyle.FontPosition, Is.EqualTo(FontPosition.Normal));
  189. }
  190. [Test]
  191. public void SetsCorrectTextPosition_Superscript()
  192. {
  193. var textStyle = TextStyle.Default.Superscript();
  194. Assert.That(textStyle.FontPosition, Is.EqualTo(FontPosition.Superscript));
  195. }
  196. #endregion
  197. #region Text Direction
  198. [Test]
  199. public void TextDirection_Default()
  200. {
  201. var textStyle = TextStyle.LibraryDefault;
  202. Assert.That(textStyle.Direction, Is.EqualTo(TextDirection.Auto));
  203. }
  204. [Test]
  205. public void SetsCorrectTextDirection_LeftToRight()
  206. {
  207. var textStyle = TextStyle.Default.DirectionFromLeftToRight();
  208. Assert.That(textStyle.Direction, Is.EqualTo(TextDirection.LeftToRight));
  209. }
  210. [Test]
  211. public void SetsCorrectTextDirection_RightToLeft()
  212. {
  213. var textStyle = TextStyle.Default.DirectionFromRightToLeft();
  214. Assert.That(textStyle.Direction, Is.EqualTo(TextDirection.RightToLeft));
  215. }
  216. [Test]
  217. public void SetsCorrectTextDirection_Auto()
  218. {
  219. var textStyle = TextStyle.Default.DirectionFromRightToLeft().DirectionAuto(); // first change from default, then auto
  220. Assert.That(textStyle.Direction, Is.EqualTo(TextDirection.Auto));
  221. }
  222. #endregion
  223. #region Font Features
  224. [Test]
  225. public void FontFeatures_Default()
  226. {
  227. var textStyle = TextStyle.LibraryDefault;
  228. Assert.That(textStyle.FontFeatures, Is.Empty);
  229. }
  230. [Test]
  231. public void EnableFontFeature_SingleFeature()
  232. {
  233. var textStyle = TextStyle.Default
  234. .EnableFontFeature(FontFeatures.StandardLigatures);
  235. Assert.That(textStyle.FontFeatures, Has.Length.EqualTo(1));
  236. Assert.That(textStyle.FontFeatures[0], Is.EqualTo((FontFeatures.StandardLigatures, true)));
  237. }
  238. [Test]
  239. public void DisableFontFeature_SingleFeature()
  240. {
  241. var textStyle = TextStyle.Default
  242. .DisableFontFeature(FontFeatures.Kerning);
  243. Assert.That(textStyle.FontFeatures, Has.Length.EqualTo(1));
  244. Assert.That(textStyle.FontFeatures[0], Is.EqualTo((FontFeatures.Kerning, false)));
  245. }
  246. // NOTE: font features applied further down the chain override those applied earlier, and will appear first in the list
  247. [Test]
  248. public void FontFeatures_MixedEnableDisable()
  249. {
  250. var textStyle = TextStyle.Default
  251. .EnableFontFeature(FontFeatures.StandardLigatures)
  252. .DisableFontFeature(FontFeatures.Kerning)
  253. .EnableFontFeature(FontFeatures.DiscretionaryLigatures);
  254. Assert.That(textStyle.FontFeatures, Has.Length.EqualTo(3));
  255. Assert.That(textStyle.FontFeatures[0], Is.EqualTo((FontFeatures.DiscretionaryLigatures, true)));
  256. Assert.That(textStyle.FontFeatures[1], Is.EqualTo((FontFeatures.Kerning, false)));
  257. Assert.That(textStyle.FontFeatures[2], Is.EqualTo((FontFeatures.StandardLigatures, true)));
  258. }
  259. [Test]
  260. public void FontFeatures_OverrideSameFeature()
  261. {
  262. var textStyle = TextStyle.Default
  263. .EnableFontFeature(FontFeatures.StandardLigatures)
  264. .DisableFontFeature(FontFeatures.Kerning)
  265. .DisableFontFeature(FontFeatures.Kerning)
  266. .DisableFontFeature(FontFeatures.StandardLigatures);
  267. Assert.That(textStyle.FontFeatures, Has.Length.EqualTo(2));
  268. Assert.That(textStyle.FontFeatures[0], Is.EqualTo((FontFeatures.StandardLigatures, false)));
  269. Assert.That(textStyle.FontFeatures[1], Is.EqualTo((FontFeatures.Kerning, false)));
  270. }
  271. #endregion
  272. [Test]
  273. public void ApplyInheritedAndGlobalStyle()
  274. {
  275. // arrange
  276. var defaultTextStyle = TextStyle
  277. .Default
  278. .FontSize(20)
  279. .FontFamily("Arial", "Microsoft YaHei")
  280. .BackgroundColor(Colors.Green.Lighten2)
  281. .EnableFontFeature(FontFeatures.StandardLigatures);
  282. var spanTextStyle = TextStyle
  283. .Default
  284. .FontFamily("Times New Roman", "Arial", "Calibri")
  285. .Bold()
  286. .Strikethrough()
  287. .BackgroundColor(Colors.Red.Lighten2)
  288. .DisableFontFeature(FontFeatures.StandardLigatures)
  289. .EnableFontFeature(FontFeatures.Kerning);
  290. // act
  291. var targetStyle = spanTextStyle.ApplyInheritedStyle(defaultTextStyle).ApplyGlobalStyle();
  292. // assert
  293. var expectedStyle = TextStyle.LibraryDefault with
  294. {
  295. Id = targetStyle.Id, // expect to break when adding new TextStyle properties, so use the real one
  296. Size = 20,
  297. FontFamilies = new[] { "Times New Roman", "Arial", "Calibri", "Microsoft YaHei", "Lato" },
  298. FontWeight = FontWeight.Bold,
  299. BackgroundColor = Colors.Red.Lighten2,
  300. FontFeatures = new[]
  301. {
  302. (FontFeatures.Kerning, true),
  303. (FontFeatures.StandardLigatures, false)
  304. },
  305. HasStrikethrough = true
  306. };
  307. Assert.That(targetStyle, Is.Not.Null);
  308. Assert.That(targetStyle.Id, Is.GreaterThan(1));
  309. Assert.That(targetStyle.ToString(), Is.EqualTo(expectedStyle.ToString()));
  310. }
  311. }
  312. }