Style.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // RichTextKit
  2. // Copyright © 2019-2020 Topten Software. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. // not use this product except in compliance with the License. You may obtain
  6. // a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. using SkiaSharp;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Text;
  19. namespace Topten.RichTextKit
  20. {
  21. /// <summary>
  22. /// A basic implementation of IStyle interface provides styling
  23. /// information for a run of text.
  24. /// </summary>
  25. public class Style : IStyle
  26. {
  27. void CheckNotSealed()
  28. {
  29. if (_sealed)
  30. throw new InvalidOperationException("Style has been sealed and can't be modified");
  31. }
  32. /// <summary>
  33. /// Seals the style to prevent it from further modification
  34. /// </summary>
  35. public void Seal()
  36. {
  37. _sealed = true;
  38. }
  39. /// <summary>
  40. /// The font family for text this text run (defaults to "Arial").
  41. /// </summary>
  42. public string FontFamily
  43. {
  44. get => _fontFamily;
  45. set { CheckNotSealed(); _fontFamily = value; }
  46. }
  47. /// <summary>
  48. /// The font size for text in this run (defaults to 16).
  49. /// </summary>
  50. public float FontSize
  51. {
  52. get => _fontSize;
  53. set { CheckNotSealed(); _fontSize = value; }
  54. }
  55. /// <summary>
  56. /// The font weight for text in this run (defaults to 400).
  57. /// </summary>
  58. public int FontWeight
  59. {
  60. get => _fontWeight;
  61. set { CheckNotSealed(); _fontWeight = value; }
  62. }
  63. /// <summary>
  64. /// The font width for text in this run (defaults to WidthStyle.Normal).
  65. /// </summary>
  66. public SKFontStyleWidth FontWidth
  67. {
  68. get => _fontWidth;
  69. set { CheckNotSealed(); _fontWidth = value; }
  70. }
  71. /// <summary>
  72. /// True if the text in this run should be displayed in an italic
  73. /// font; otherwise False (defaults to false).
  74. /// </summary>
  75. public bool FontItalic
  76. {
  77. get => _fontItalic;
  78. set { CheckNotSealed(); _fontItalic = value; }
  79. }
  80. /// <summary>
  81. /// The underline style for text in this run (defaults to None).
  82. /// </summary>
  83. public UnderlineStyle Underline
  84. {
  85. get => _underlineStyle;
  86. set { CheckNotSealed(); _underlineStyle = value; }
  87. }
  88. /// <summary>
  89. /// The strike through style for the text in this run (defaults to None).
  90. /// </summary>
  91. public StrikeThroughStyle StrikeThrough
  92. {
  93. get => _strikeThrough;
  94. set { CheckNotSealed(); _strikeThrough = value; }
  95. }
  96. /// <summary>
  97. /// The line height for text in this run as a multiplier (defaults to 1.0).
  98. /// </summary>
  99. public float LineHeight
  100. {
  101. get => _lineHeight;
  102. set { CheckNotSealed(); _lineHeight = value; }
  103. }
  104. /// <summary>
  105. /// The text color for text in this run (defaults to black).
  106. /// </summary>
  107. public SKColor TextColor
  108. {
  109. get => _textColor;
  110. set { CheckNotSealed(); _textColor = value; }
  111. }
  112. /// <summary>
  113. /// The background color of this run (no background is painted by default).
  114. /// </summary>
  115. public SKColor BackgroundColor
  116. {
  117. get => _backgroundColor;
  118. set
  119. {
  120. CheckNotSealed();
  121. _backgroundColor = value;
  122. }
  123. }
  124. /// <summary>
  125. /// Color of the halo
  126. /// </summary>
  127. public SKColor HaloColor
  128. {
  129. get => _haloColor;
  130. set
  131. {
  132. CheckNotSealed();
  133. _haloColor = value;
  134. }
  135. }
  136. /// <summary>
  137. /// Width of halo
  138. /// </summary>
  139. public float HaloWidth
  140. {
  141. get => _haloWidth;
  142. set
  143. {
  144. CheckNotSealed();
  145. _haloWidth = value;
  146. }
  147. }
  148. /// <summary>
  149. /// Blur of halo
  150. /// </summary>
  151. public float HaloBlur
  152. {
  153. get => _haloBlur;
  154. set
  155. {
  156. CheckNotSealed();
  157. _haloBlur = value;
  158. }
  159. }
  160. /// <summary>
  161. /// The character spacing for text in this run (defaults to 0).
  162. /// </summary>
  163. public float LetterSpacing
  164. {
  165. get => _letterSpacing;
  166. set { CheckNotSealed(); _letterSpacing = value; }
  167. }
  168. /// <summary>
  169. /// The font variant (ie: super/sub-script) for text in this run.
  170. /// </summary>
  171. public FontVariant FontVariant
  172. {
  173. get => _fontVariant;
  174. set { CheckNotSealed(); _fontVariant = value; }
  175. }
  176. /// <summary>
  177. /// Text direction override for this span
  178. /// </summary>
  179. public TextDirection TextDirection
  180. {
  181. get => _textDirection;
  182. set { CheckNotSealed(); _textDirection = value; }
  183. }
  184. /// <inheritdoc />
  185. public char ReplacementCharacter
  186. {
  187. get => _replacementCharacter;
  188. set { CheckNotSealed(); _replacementCharacter = value; }
  189. }
  190. bool _sealed;
  191. string _fontFamily = "Arial";
  192. float _fontSize = 16;
  193. int _fontWeight = 400;
  194. SKFontStyleWidth _fontWidth = SKFontStyleWidth.Normal;
  195. bool _fontItalic;
  196. UnderlineStyle _underlineStyle;
  197. StrikeThroughStyle _strikeThrough;
  198. float _lineHeight = 1.0f;
  199. SKColor _textColor = new SKColor(0xFF000000);
  200. SKColor _backgroundColor = SKColor.Empty;
  201. SKColor _haloColor = SKColor.Empty;
  202. float _haloWidth = 0f;
  203. float _haloBlur = 0f;
  204. float _letterSpacing;
  205. FontVariant _fontVariant;
  206. TextDirection _textDirection = TextDirection.Auto;
  207. char _replacementCharacter = '\0';
  208. /// <summary>
  209. /// Modifies this style with one or more attribute changes and returns a new style
  210. /// </summary>
  211. /// <remarks>
  212. /// Note this method always creates a new style instance.To avoid creating excessive
  213. /// style instances, consider using the StyleManager which caches instances of styles
  214. /// with the same attributes
  215. /// </remarks>
  216. /// <param name="fontFamily">The new font family</param>
  217. /// <param name="fontSize">The new font size</param>
  218. /// <param name="fontWeight">The new font weight</param>
  219. /// <param name="fontWidth">The new font width</param>
  220. /// <param name="fontItalic">The new font italic</param>
  221. /// <param name="underline">The new underline style</param>
  222. /// <param name="strikeThrough">The new strike-through style</param>
  223. /// <param name="lineHeight">The new line height</param>
  224. /// <param name="textColor">The new text color</param>
  225. /// <param name="backgroundColor">The new background color</param>
  226. /// <param name="haloColor">Color of the halo background</param>
  227. /// <param name="haloBlur">Blur amount for the halo background</param>
  228. /// <param name="haloWidth">Width of the halo background</param>
  229. /// <param name="letterSpacing">The new letterSpacing</param>
  230. /// <param name="fontVariant">The new font variant</param>
  231. /// <param name="textDirection">The new text direction</param>
  232. /// <param name="replacementCharacter">The new replacement character</param>
  233. /// <returns>A new style with the passed attributes changed</returns>
  234. public Style Modify(
  235. string fontFamily = null,
  236. float? fontSize = null,
  237. int? fontWeight = null,
  238. SKFontStyleWidth? fontWidth = null,
  239. bool? fontItalic = null,
  240. UnderlineStyle? underline = null,
  241. StrikeThroughStyle? strikeThrough = null,
  242. float? lineHeight = null,
  243. SKColor? textColor = null,
  244. SKColor? backgroundColor = null,
  245. SKColor? haloColor = null,
  246. float? haloWidth = null,
  247. float? haloBlur = null,
  248. float? letterSpacing = null,
  249. FontVariant? fontVariant = null,
  250. TextDirection? textDirection = null,
  251. char? replacementCharacter = null
  252. )
  253. {
  254. // Resolve new style against current style
  255. return new Style()
  256. {
  257. FontFamily = fontFamily ?? this.FontFamily,
  258. FontSize = fontSize ?? this.FontSize,
  259. FontWeight = fontWeight ?? this.FontWeight,
  260. FontWidth = fontWidth ?? this.FontWidth,
  261. FontItalic = fontItalic ?? this.FontItalic,
  262. Underline = underline ?? this.Underline,
  263. StrikeThrough = strikeThrough ?? this.StrikeThrough,
  264. LineHeight = lineHeight ?? this.LineHeight,
  265. TextColor = textColor ?? this.TextColor,
  266. BackgroundColor = backgroundColor ?? this.BackgroundColor,
  267. HaloColor = haloColor ?? this.HaloColor,
  268. HaloWidth = haloWidth ?? this.HaloWidth,
  269. HaloBlur = haloBlur ?? this.HaloBlur,
  270. LetterSpacing = letterSpacing ?? this.LetterSpacing,
  271. FontVariant = fontVariant ?? this.FontVariant,
  272. TextDirection = textDirection ?? this.TextDirection,
  273. ReplacementCharacter = replacementCharacter ?? this.ReplacementCharacter,
  274. };
  275. }
  276. }
  277. }