TextStyleTests.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System.Collections.Generic;
  2. using NUnit.Framework;
  3. using QuestPDF.Fluent;
  4. using QuestPDF.Helpers;
  5. using QuestPDF.Infrastructure;
  6. namespace QuestPDF.UnitTests
  7. {
  8. [TestFixture]
  9. public class TextStyleTests
  10. {
  11. #region Font Weight
  12. [Test]
  13. public void FontWeight_Default()
  14. {
  15. var textStyle = TextStyle.LibraryDefault;
  16. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Normal));
  17. }
  18. [Test]
  19. public void SetsCorrectSetsCorrectFontWeight_Thin()
  20. {
  21. var textStyle = TextStyle.Default.Thin();
  22. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Thin));
  23. }
  24. [Test]
  25. public void SetsCorrectFontWeight_ExtraLight()
  26. {
  27. var textStyle = TextStyle.Default.ExtraLight();
  28. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.ExtraLight));
  29. }
  30. [Test]
  31. public void SetsCorrectFontWeight_Light()
  32. {
  33. var textStyle = TextStyle.Default.Light();
  34. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Light));
  35. }
  36. [Test]
  37. public void SetsCorrectFontWeight_Normal()
  38. {
  39. var textStyle = TextStyle.Default.Bold().NormalWeight(); // first change from default, then normal
  40. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Normal));
  41. }
  42. [Test]
  43. public void SetsCorrectFontWeight_Medium()
  44. {
  45. var textStyle = TextStyle.Default.Medium();
  46. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Medium));
  47. }
  48. [Test]
  49. public void SetsCorrectFontWeight_SemiBold()
  50. {
  51. var textStyle = TextStyle.Default.SemiBold();
  52. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.SemiBold));
  53. }
  54. [Test]
  55. public void SetsCorrectFontWeight_Bold()
  56. {
  57. var textStyle = TextStyle.Default.Bold();
  58. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Bold));
  59. }
  60. [Test]
  61. public void SetsCorrectFontWeight_ExtraBold()
  62. {
  63. var textStyle = TextStyle.Default.ExtraBold();
  64. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.ExtraBold));
  65. }
  66. [Test]
  67. public void SetsCorrectFontWeight_Black()
  68. {
  69. var textStyle = TextStyle.Default.Black();
  70. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.Black));
  71. }
  72. [Test]
  73. public void SetsCorrectFontWeight_ExtraBlack()
  74. {
  75. var textStyle = TextStyle.Default.ExtraBlack();
  76. Assert.That(textStyle.FontWeight, Is.EqualTo(FontWeight.ExtraBlack));
  77. }
  78. #endregion
  79. #region Text Position
  80. [Test]
  81. public void TextPosition_Default()
  82. {
  83. var textStyle = TextStyle.LibraryDefault;
  84. Assert.That(textStyle.FontPosition, Is.EqualTo(FontPosition.Normal));
  85. }
  86. [Test]
  87. public void SetsCorrectTextPosition_Subscript()
  88. {
  89. var textStyle = TextStyle.Default.Subscript();
  90. Assert.That(textStyle.FontPosition, Is.EqualTo(FontPosition.Subscript));
  91. }
  92. [Test]
  93. public void SetsCorrectTextPosition_Normal()
  94. {
  95. var textStyle = TextStyle.Default.Subscript().NormalPosition(); // first change from default, then normal
  96. Assert.That(textStyle.FontPosition, Is.EqualTo(FontPosition.Normal));
  97. }
  98. [Test]
  99. public void SetsCorrectTextPosition_Superscript()
  100. {
  101. var textStyle = TextStyle.Default.Superscript();
  102. Assert.That(textStyle.FontPosition, Is.EqualTo(FontPosition.Superscript));
  103. }
  104. #endregion
  105. #region Font Features
  106. [Test]
  107. public void FontFeatures_Default()
  108. {
  109. var textStyle = TextStyle.LibraryDefault;
  110. Assert.That(textStyle.FontFeatures, Is.Empty);
  111. }
  112. [Test]
  113. public void EnableFontFeature_SingleFeature()
  114. {
  115. var textStyle = TextStyle.Default
  116. .EnableFontFeature(FontFeatures.StandardLigatures);
  117. Assert.That(textStyle.FontFeatures, Has.Length.EqualTo(1));
  118. Assert.That(textStyle.FontFeatures[0], Is.EqualTo((FontFeatures.StandardLigatures, true)));
  119. }
  120. [Test]
  121. public void DisableFontFeature_SingleFeature()
  122. {
  123. var textStyle = TextStyle.Default
  124. .DisableFontFeature(FontFeatures.Kerning);
  125. Assert.That(textStyle.FontFeatures, Has.Length.EqualTo(1));
  126. Assert.That(textStyle.FontFeatures[0], Is.EqualTo((FontFeatures.Kerning, false)));
  127. }
  128. // NOTE: font features applied further down the chain override those applied earlier, and will appear first in the list
  129. [Test]
  130. public void FontFeatures_MixedEnableDisable()
  131. {
  132. var textStyle = TextStyle.Default
  133. .EnableFontFeature(FontFeatures.StandardLigatures)
  134. .DisableFontFeature(FontFeatures.Kerning)
  135. .EnableFontFeature(FontFeatures.DiscretionaryLigatures);
  136. Assert.That(textStyle.FontFeatures, Has.Length.EqualTo(3));
  137. Assert.That(textStyle.FontFeatures[0], Is.EqualTo((FontFeatures.DiscretionaryLigatures, true)));
  138. Assert.That(textStyle.FontFeatures[1], Is.EqualTo((FontFeatures.Kerning, false)));
  139. Assert.That(textStyle.FontFeatures[2], Is.EqualTo((FontFeatures.StandardLigatures, true)));
  140. }
  141. [Test]
  142. public void FontFeatures_OverrideSameFeature()
  143. {
  144. var textStyle = TextStyle.Default
  145. .EnableFontFeature(FontFeatures.StandardLigatures)
  146. .DisableFontFeature(FontFeatures.Kerning)
  147. .DisableFontFeature(FontFeatures.Kerning)
  148. .DisableFontFeature(FontFeatures.StandardLigatures);
  149. Assert.That(textStyle.FontFeatures, Has.Length.EqualTo(2));
  150. Assert.That(textStyle.FontFeatures[0], Is.EqualTo((FontFeatures.StandardLigatures, false)));
  151. Assert.That(textStyle.FontFeatures[1], Is.EqualTo((FontFeatures.Kerning, false)));
  152. }
  153. #endregion
  154. [Test]
  155. public void ApplyInheritedAndGlobalStyle()
  156. {
  157. // arrange
  158. var defaultTextStyle = TextStyle
  159. .Default
  160. .FontSize(20)
  161. .FontFamily("Arial", "Microsoft YaHei")
  162. .BackgroundColor(Colors.Green.Lighten2)
  163. .EnableFontFeature(FontFeatures.StandardLigatures);
  164. var spanTextStyle = TextStyle
  165. .Default
  166. .FontFamily("Times New Roman", "Arial", "Calibri")
  167. .Bold()
  168. .Strikethrough()
  169. .BackgroundColor(Colors.Red.Lighten2)
  170. .DisableFontFeature(FontFeatures.StandardLigatures)
  171. .EnableFontFeature(FontFeatures.Kerning);
  172. // act
  173. var targetStyle = spanTextStyle.ApplyInheritedStyle(defaultTextStyle).ApplyGlobalStyle();
  174. // assert
  175. var expectedStyle = TextStyle.LibraryDefault with
  176. {
  177. Id = targetStyle.Id, // expect to break when adding new TextStyle properties, so use the real one
  178. Size = 20,
  179. FontFamilies = new[] { "Times New Roman", "Arial", "Calibri", "Microsoft YaHei", "Lato" },
  180. FontWeight = FontWeight.Bold,
  181. BackgroundColor = Colors.Red.Lighten2,
  182. FontFeatures = new[]
  183. {
  184. (FontFeatures.Kerning, true),
  185. (FontFeatures.StandardLigatures, false)
  186. },
  187. HasStrikethrough = true
  188. };
  189. Assert.That(targetStyle, Is.Not.Null);
  190. Assert.That(targetStyle.Id, Is.GreaterThan(1));
  191. Assert.That(targetStyle.ToString(), Is.EqualTo(expectedStyle.ToString()));
  192. }
  193. }
  194. }