TextFormatter.cs 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using NStack;
  6. namespace Terminal.Gui {
  7. /// <summary>
  8. /// Text alignment enumeration, controls how text is displayed.
  9. /// </summary>
  10. public enum TextAlignment {
  11. /// <summary>
  12. /// Aligns the text to the left of the frame.
  13. /// </summary>
  14. Left,
  15. /// <summary>
  16. /// Aligns the text to the right side of the frame.
  17. /// </summary>
  18. Right,
  19. /// <summary>
  20. /// Centers the text in the frame.
  21. /// </summary>
  22. Centered,
  23. /// <summary>
  24. /// Shows the text as justified text in the frame.
  25. /// </summary>
  26. Justified
  27. }
  28. /// <summary>
  29. /// Vertical text alignment enumeration, controls how text is displayed.
  30. /// </summary>
  31. public enum VerticalTextAlignment {
  32. /// <summary>
  33. /// Aligns the text to the top of the frame.
  34. /// </summary>
  35. Top,
  36. /// <summary>
  37. /// Aligns the text to the bottom of the frame.
  38. /// </summary>
  39. Bottom,
  40. /// <summary>
  41. /// Centers the text verticaly in the frame.
  42. /// </summary>
  43. Middle,
  44. /// <summary>
  45. /// Shows the text as justified text in the frame.
  46. /// </summary>
  47. Justified
  48. }
  49. /// TextDirection [H] = Horizontal [V] = Vertical
  50. /// =============
  51. /// LeftRight_TopBottom [H] Normal
  52. /// TopBottom_LeftRight [V] Normal
  53. ///
  54. /// RightLeft_TopBottom [H] Invert Text
  55. /// TopBottom_RightLeft [V] Invert Lines
  56. ///
  57. /// LeftRight_BottomTop [H] Invert Lines
  58. /// BottomTop_LeftRight [V] Invert Text
  59. ///
  60. /// RightLeft_BottomTop [H] Invert Text + Invert Lines
  61. /// BottomTop_RightLeft [V] Invert Text + Invert Lines
  62. ///
  63. /// <summary>
  64. /// Text direction enumeration, controls how text is displayed.
  65. /// </summary>
  66. public enum TextDirection {
  67. /// <summary>
  68. /// Normal horizontal direction.
  69. /// <code>HELLO<br/>WORLD</code>
  70. /// </summary>
  71. LeftRight_TopBottom,
  72. /// <summary>
  73. /// Normal vertical direction.
  74. /// <code>H W<br/>E O<br/>L R<br/>L L<br/>O D</code>
  75. /// </summary>
  76. TopBottom_LeftRight,
  77. /// <summary>
  78. /// This is a horizontal direction. <br/> RTL
  79. /// <code>OLLEH<br/>DLROW</code>
  80. /// </summary>
  81. RightLeft_TopBottom,
  82. /// <summary>
  83. /// This is a vertical direction.
  84. /// <code>W H<br/>O E<br/>R L<br/>L L<br/>D O</code>
  85. /// </summary>
  86. TopBottom_RightLeft,
  87. /// <summary>
  88. /// This is a horizontal direction.
  89. /// <code>WORLD<br/>HELLO</code>
  90. /// </summary>
  91. LeftRight_BottomTop,
  92. /// <summary>
  93. /// This is a vertical direction.
  94. /// <code>O D<br/>L L<br/>L R<br/>E O<br/>H W</code>
  95. /// </summary>
  96. BottomTop_LeftRight,
  97. /// <summary>
  98. /// This is a horizontal direction.
  99. /// <code>DLROW<br/>OLLEH</code>
  100. /// </summary>
  101. RightLeft_BottomTop,
  102. /// <summary>
  103. /// This is a vertical direction.
  104. /// <code>D O<br/>L L<br/>R L<br/>O E<br/>W H</code>
  105. /// </summary>
  106. BottomTop_RightLeft
  107. }
  108. /// <summary>
  109. /// Provides text formatting capabilities for console apps. Supports, hotkeys, horizontal alignment, multiple lines, and word-based line wrap.
  110. /// </summary>
  111. public class TextFormatter {
  112. List<ustring> lines = new List<ustring> ();
  113. ustring text;
  114. TextAlignment textAlignment;
  115. VerticalTextAlignment textVerticalAlignment;
  116. TextDirection textDirection;
  117. Attribute textColor = -1;
  118. bool needsFormat;
  119. Key hotKey;
  120. Size size;
  121. /// <summary>
  122. /// Event invoked when the <see cref="HotKey"/> is changed.
  123. /// </summary>
  124. public event Action<Key> HotKeyChanged;
  125. /// <summary>
  126. /// The text to be displayed. This text is never modified.
  127. /// </summary>
  128. public virtual ustring Text {
  129. get => text;
  130. set {
  131. text = value;
  132. if (text.RuneCount > 0 && (Size.Width == 0 || Size.Height == 0 || Size.Width != text.RuneCount)) {
  133. // Provide a default size (width = length of longest line, height = 1)
  134. // TODO: It might makes more sense for the default to be width = length of first line?
  135. Size = new Size (TextFormatter.MaxWidth (Text, int.MaxValue), 1);
  136. }
  137. NeedsFormat = true;
  138. }
  139. }
  140. /// <summary>
  141. /// Used by <see cref="Text"/> to resize the view's <see cref="View.Bounds"/> with the <see cref="Size"/>.
  142. /// Setting <see cref="AutoSize"/> to true only work if the <see cref="View.Width"/> and <see cref="View.Height"/> are null or
  143. /// <see cref="LayoutStyle.Absolute"/> values and doesn't work with <see cref="LayoutStyle.Computed"/> layout,
  144. /// to avoid breaking the <see cref="Pos"/> and <see cref="Dim"/> settings.
  145. /// </summary>
  146. public bool AutoSize { get; set; }
  147. // TODO: Add Vertical Text Alignment
  148. /// <summary>
  149. /// Controls the horizontal text-alignment property.
  150. /// </summary>
  151. /// <value>The text alignment.</value>
  152. public TextAlignment Alignment {
  153. get => textAlignment;
  154. set {
  155. textAlignment = value;
  156. NeedsFormat = true;
  157. }
  158. }
  159. /// <summary>
  160. /// Controls the vertical text-alignment property.
  161. /// </summary>
  162. /// <value>The text vertical alignment.</value>
  163. public VerticalTextAlignment VerticalAlignment {
  164. get => textVerticalAlignment;
  165. set {
  166. textVerticalAlignment = value;
  167. NeedsFormat = true;
  168. }
  169. }
  170. /// <summary>
  171. /// Controls the text-direction property.
  172. /// </summary>
  173. /// <value>The text vertical alignment.</value>
  174. public TextDirection Direction {
  175. get => textDirection;
  176. set {
  177. textDirection = value;
  178. NeedsFormat = true;
  179. }
  180. }
  181. /// <summary>
  182. /// Check if it is a horizontal direction
  183. /// </summary>
  184. public static bool IsHorizontalDirection (TextDirection textDirection)
  185. {
  186. switch (textDirection) {
  187. case TextDirection.LeftRight_TopBottom:
  188. case TextDirection.LeftRight_BottomTop:
  189. case TextDirection.RightLeft_TopBottom:
  190. case TextDirection.RightLeft_BottomTop:
  191. return true;
  192. default:
  193. return false;
  194. }
  195. }
  196. /// <summary>
  197. /// Check if it is a vertical direction
  198. /// </summary>
  199. public static bool IsVerticalDirection (TextDirection textDirection)
  200. {
  201. switch (textDirection) {
  202. case TextDirection.TopBottom_LeftRight:
  203. case TextDirection.TopBottom_RightLeft:
  204. case TextDirection.BottomTop_LeftRight:
  205. case TextDirection.BottomTop_RightLeft:
  206. return true;
  207. default:
  208. return false;
  209. }
  210. }
  211. /// <summary>
  212. /// Check if it is Left to Right direction
  213. /// </summary>
  214. public static bool IsLeftToRight (TextDirection textDirection)
  215. {
  216. switch (textDirection) {
  217. case TextDirection.LeftRight_TopBottom:
  218. case TextDirection.LeftRight_BottomTop:
  219. return true;
  220. default:
  221. return false;
  222. }
  223. }
  224. /// <summary>
  225. /// Check if it is Top to Bottom direction
  226. /// </summary>
  227. public static bool IsTopToBottom (TextDirection textDirection)
  228. {
  229. switch (textDirection) {
  230. case TextDirection.TopBottom_LeftRight:
  231. case TextDirection.TopBottom_RightLeft:
  232. return true;
  233. default:
  234. return false;
  235. }
  236. }
  237. /// <summary>
  238. /// Gets or sets the size of the area the text will be constrained to when formatted.
  239. /// </summary>
  240. public Size Size {
  241. get => size;
  242. set {
  243. size = value;
  244. NeedsFormat = true;
  245. }
  246. }
  247. /// <summary>
  248. /// The specifier character for the hotkey (e.g. '_'). Set to '\xffff' to disable hotkey support for this View instance. The default is '\xffff'.
  249. /// </summary>
  250. public Rune HotKeySpecifier { get; set; } = (Rune)0xFFFF;
  251. /// <summary>
  252. /// The position in the text of the hotkey. The hotkey will be rendered using the hot color.
  253. /// </summary>
  254. public int HotKeyPos { get => hotKeyPos; set => hotKeyPos = value; }
  255. /// <summary>
  256. /// Gets the hotkey. Will be an upper case letter or digit.
  257. /// </summary>
  258. public Key HotKey {
  259. get => hotKey;
  260. internal set {
  261. if (hotKey != value) {
  262. var oldKey = hotKey;
  263. hotKey = value;
  264. HotKeyChanged?.Invoke (oldKey);
  265. }
  266. }
  267. }
  268. /// <summary>
  269. /// Specifies the mask to apply to the hotkey to tag it as the hotkey. The default value of <c>0x100000</c> causes
  270. /// the underlying Rune to be identified as a "private use" Unicode character.
  271. /// </summary>HotKeyTagMask
  272. public uint HotKeyTagMask { get; set; } = 0x100000;
  273. /// <summary>
  274. /// Gets the cursor position from <see cref="HotKey"/>. If the <see cref="HotKey"/> is defined, the cursor will be positioned over it.
  275. /// </summary>
  276. public int CursorPosition { get; set; }
  277. /// <summary>
  278. /// Gets the formatted lines.
  279. /// </summary>
  280. /// <remarks>
  281. /// <para>
  282. /// Upon a 'get' of this property, if the text needs to be formatted (if <see cref="NeedsFormat"/> is <c>true</c>)
  283. /// <see cref="Format(ustring, int, bool, bool, bool, int, TextDirection)"/> will be called internally.
  284. /// </para>
  285. /// </remarks>
  286. public List<ustring> Lines {
  287. get {
  288. // With this check, we protect against subclasses with overrides of Text
  289. if (ustring.IsNullOrEmpty (Text)) {
  290. lines = new List<ustring> ();
  291. lines.Add (ustring.Empty);
  292. NeedsFormat = false;
  293. return lines;
  294. }
  295. if (NeedsFormat) {
  296. var shown_text = text;
  297. if (FindHotKey (text, HotKeySpecifier, true, out hotKeyPos, out Key newHotKey)) {
  298. HotKey = newHotKey;
  299. shown_text = RemoveHotKeySpecifier (Text, hotKeyPos, HotKeySpecifier);
  300. shown_text = ReplaceHotKeyWithTag (shown_text, hotKeyPos);
  301. }
  302. if (Size.IsEmpty) {
  303. throw new InvalidOperationException ("Size must be set before accessing Lines");
  304. }
  305. if (IsVerticalDirection (textDirection)) {
  306. lines = Format (shown_text, Size.Height, textVerticalAlignment == VerticalTextAlignment.Justified, Size.Width > 1,
  307. false, 0, textDirection);
  308. if (!AutoSize && lines.Count > Size.Width) {
  309. lines.RemoveRange (Size.Width, lines.Count - Size.Width);
  310. }
  311. } else {
  312. lines = Format (shown_text, Size.Width, textAlignment == TextAlignment.Justified, Size.Height > 1,
  313. false, 0, textDirection);
  314. if (!AutoSize && lines.Count > Size.Height) {
  315. lines.RemoveRange (Size.Height, lines.Count - Size.Height);
  316. }
  317. }
  318. NeedsFormat = false;
  319. }
  320. return lines;
  321. }
  322. }
  323. /// <summary>
  324. /// Gets or sets whether the <see cref="TextFormatter"/> needs to format the text when <see cref="Draw(Rect, Attribute, Attribute, Rect)"/> is called.
  325. /// If it is <c>false</c> when Draw is called, the Draw call will be faster.
  326. /// </summary>
  327. /// <remarks>
  328. /// <para>
  329. /// This is set to true when the properties of <see cref="TextFormatter"/> are set.
  330. /// </para>
  331. /// </remarks>
  332. public bool NeedsFormat { get => needsFormat; set => needsFormat = value; }
  333. static ustring StripCRLF (ustring str)
  334. {
  335. var runes = str.ToRuneList ();
  336. for (int i = 0; i < runes.Count; i++) {
  337. switch (runes [i]) {
  338. case '\n':
  339. runes.RemoveAt (i);
  340. break;
  341. case '\r':
  342. if ((i + 1) < runes.Count && runes [i + 1] == '\n') {
  343. runes.RemoveAt (i);
  344. runes.RemoveAt (i + 1);
  345. i++;
  346. } else {
  347. runes.RemoveAt (i);
  348. }
  349. break;
  350. }
  351. }
  352. return ustring.Make (runes);
  353. }
  354. static ustring ReplaceCRLFWithSpace (ustring str)
  355. {
  356. var runes = str.ToRuneList ();
  357. for (int i = 0; i < runes.Count; i++) {
  358. switch (runes [i]) {
  359. case '\n':
  360. runes [i] = (Rune)' ';
  361. break;
  362. case '\r':
  363. if ((i + 1) < runes.Count && runes [i + 1] == '\n') {
  364. runes [i] = (Rune)' ';
  365. runes.RemoveAt (i + 1);
  366. i++;
  367. } else {
  368. runes [i] = (Rune)' ';
  369. }
  370. break;
  371. }
  372. }
  373. return ustring.Make (runes);
  374. }
  375. /// <summary>
  376. /// Adds trailing whitespace or truncates <paramref name="text"/>
  377. /// so that it fits exactly <paramref name="width"/> console units.
  378. /// Note that some unicode characters take 2+ columns
  379. /// </summary>
  380. /// <param name="text"></param>
  381. /// <param name="width"></param>
  382. /// <returns></returns>
  383. public static string ClipOrPad (string text, int width)
  384. {
  385. if (string.IsNullOrEmpty (text))
  386. return text;
  387. // if value is not wide enough
  388. if (text.Sum (c => Rune.ColumnWidth (c)) < width) {
  389. // pad it out with spaces to the given alignment
  390. int toPad = width - (text.Sum (c => Rune.ColumnWidth (c)));
  391. return text + new string (' ', toPad);
  392. }
  393. // value is too wide
  394. return new string (text.TakeWhile (c => (width -= Rune.ColumnWidth (c)) >= 0).ToArray ());
  395. }
  396. /// <summary>
  397. /// Formats the provided text to fit within the width provided using word wrapping.
  398. /// </summary>
  399. /// <param name="text">The text to word wrap</param>
  400. /// <param name="width">The width to contain the text to</param>
  401. /// <param name="preserveTrailingSpaces">If <c>true</c>, the wrapped text will keep the trailing spaces.
  402. /// If <c>false</c>, the trailing spaces will be trimmed.</param>
  403. /// <param name="tabWidth">The tab width.</param>
  404. /// <param name="textDirection">The text direction.</param>
  405. /// <returns>Returns a list of word wrapped lines.</returns>
  406. /// <remarks>
  407. /// <para>
  408. /// This method does not do any justification.
  409. /// </para>
  410. /// <para>
  411. /// This method strips Newline ('\n' and '\r\n') sequences before processing.
  412. /// </para>
  413. /// </remarks>
  414. public static List<ustring> WordWrap (ustring text, int width, bool preserveTrailingSpaces = false, int tabWidth = 0,
  415. TextDirection textDirection = TextDirection.LeftRight_TopBottom)
  416. {
  417. if (width < 0) {
  418. throw new ArgumentOutOfRangeException ("Width cannot be negative.");
  419. }
  420. int start = 0, end;
  421. var lines = new List<ustring> ();
  422. if (ustring.IsNullOrEmpty (text)) {
  423. return lines;
  424. }
  425. var runes = StripCRLF (text).ToRuneList ();
  426. if (!preserveTrailingSpaces) {
  427. if (IsHorizontalDirection (textDirection)) {
  428. while ((end = start + GetMaxLengthForWidth (runes.GetRange (start, runes.Count - start), width)) < runes.Count) {
  429. while (runes [end] != ' ' && end > start)
  430. end--;
  431. if (end == start)
  432. end = start + GetMaxLengthForWidth (runes.GetRange (end, runes.Count - end), width);
  433. lines.Add (ustring.Make (runes.GetRange (start, end - start)));
  434. start = end;
  435. if (runes [end] == ' ') {
  436. start++;
  437. }
  438. }
  439. } else {
  440. while ((end = start + width) < runes.Count) {
  441. while (runes [end] != ' ' && end > start)
  442. end--;
  443. if (end == start)
  444. end = start + width;
  445. lines.Add (ustring.Make (runes.GetRange (start, end - start)));
  446. start = end;
  447. if (runes [end] == ' ') {
  448. start++;
  449. }
  450. }
  451. }
  452. } else {
  453. while ((end = start) < runes.Count) {
  454. end = GetNextWhiteSpace (start, width);
  455. lines.Add (ustring.Make (runes.GetRange (start, end - start)));
  456. start = end;
  457. }
  458. }
  459. int GetNextWhiteSpace (int from, int cWidth, int cLength = 0)
  460. {
  461. var to = from;
  462. var length = cLength;
  463. while (length < cWidth && to < runes.Count) {
  464. var rune = runes [to];
  465. if (IsHorizontalDirection (textDirection)) {
  466. length += Rune.ColumnWidth (rune);
  467. } else {
  468. length++;
  469. }
  470. if (rune == ' ') {
  471. if (length == cWidth) {
  472. return to + 1;
  473. } else if (length > cWidth) {
  474. return to;
  475. } else {
  476. return GetNextWhiteSpace (to + 1, cWidth, length);
  477. }
  478. } else if (rune == '\t') {
  479. length += tabWidth + 1;
  480. if (length == tabWidth && tabWidth > cWidth) {
  481. return to + 1;
  482. } else if (length > cWidth && tabWidth > cWidth) {
  483. return to;
  484. } else {
  485. return GetNextWhiteSpace (to + 1, cWidth, length);
  486. }
  487. }
  488. to++;
  489. }
  490. if (cLength > 0 && to < runes.Count && runes [to] != ' ') {
  491. return from;
  492. } else {
  493. return to;
  494. }
  495. }
  496. if (start < text.RuneCount) {
  497. lines.Add (ustring.Make (runes.GetRange (start, runes.Count - start)));
  498. }
  499. return lines;
  500. }
  501. /// <summary>
  502. /// Justifies text within a specified width.
  503. /// </summary>
  504. /// <param name="text">The text to justify.</param>
  505. /// <param name="width">If the text length is greater that <c>width</c> it will be clipped.</param>
  506. /// <param name="talign">Alignment.</param>
  507. /// <param name="textDirection">The text direction.</param>
  508. /// <returns>Justified and clipped text.</returns>
  509. public static ustring ClipAndJustify (ustring text, int width, TextAlignment talign, TextDirection textDirection = TextDirection.LeftRight_TopBottom)
  510. {
  511. return ClipAndJustify (text, width, talign == TextAlignment.Justified, textDirection);
  512. }
  513. /// <summary>
  514. /// Justifies text within a specified width.
  515. /// </summary>
  516. /// <param name="text">The text to justify.</param>
  517. /// <param name="width">If the text length is greater that <c>width</c> it will be clipped.</param>
  518. /// <param name="justify">Justify.</param>
  519. /// <param name="textDirection">The text direction.</param>
  520. /// <returns>Justified and clipped text.</returns>
  521. public static ustring ClipAndJustify (ustring text, int width, bool justify, TextDirection textDirection = TextDirection.LeftRight_TopBottom)
  522. {
  523. if (width < 0) {
  524. throw new ArgumentOutOfRangeException ("Width cannot be negative.");
  525. }
  526. if (ustring.IsNullOrEmpty (text)) {
  527. return text;
  528. }
  529. var runes = text.ToRuneList ();
  530. int slen = runes.Count;
  531. if (slen > width) {
  532. return ustring.Make (runes.GetRange (0, GetMaxLengthForWidth (text, width)));
  533. } else {
  534. if (justify) {
  535. return Justify (text, width, ' ', textDirection);
  536. } else if (GetTextWidth (text) > width && IsHorizontalDirection (textDirection)) {
  537. return ustring.Make (runes.GetRange (0, GetMaxLengthForWidth (text, width)));
  538. }
  539. return text;
  540. }
  541. }
  542. /// <summary>
  543. /// Justifies the text to fill the width provided. Space will be added between words (demarked by spaces and tabs) to
  544. /// make the text just fit <c>width</c>. Spaces will not be added to the ends.
  545. /// </summary>
  546. /// <param name="text"></param>
  547. /// <param name="width"></param>
  548. /// <param name="spaceChar">Character to replace whitespace and pad with. For debugging purposes.</param>
  549. /// <param name="textDirection">The text direction.</param>
  550. /// <returns>The justified text.</returns>
  551. public static ustring Justify (ustring text, int width, char spaceChar = ' ', TextDirection textDirection = TextDirection.LeftRight_TopBottom)
  552. {
  553. if (width < 0) {
  554. throw new ArgumentOutOfRangeException ("Width cannot be negative.");
  555. }
  556. if (ustring.IsNullOrEmpty (text)) {
  557. return text;
  558. }
  559. var words = text.Split (ustring.Make (' '));
  560. int textCount;
  561. if (IsHorizontalDirection (textDirection)) {
  562. textCount = words.Sum (arg => GetTextWidth (arg));
  563. } else {
  564. textCount = words.Sum (arg => arg.RuneCount);
  565. }
  566. var spaces = words.Length > 1 ? (width - textCount) / (words.Length - 1) : 0;
  567. var extras = words.Length > 1 ? (width - textCount) % words.Length : 0;
  568. var s = new System.Text.StringBuilder ();
  569. for (int w = 0; w < words.Length; w++) {
  570. var x = words [w];
  571. s.Append (x);
  572. if (w + 1 < words.Length)
  573. for (int i = 0; i < spaces; i++)
  574. s.Append (spaceChar);
  575. if (extras > 0) {
  576. extras--;
  577. }
  578. }
  579. return ustring.Make (s.ToString ());
  580. }
  581. static char [] whitespace = new char [] { ' ', '\t' };
  582. private int hotKeyPos;
  583. /// <summary>
  584. /// Reformats text into lines, applying text alignment and optionally wrapping text to new lines on word boundaries.
  585. /// </summary>
  586. /// <param name="text"></param>
  587. /// <param name="width">The width to bound the text to for word wrapping and clipping.</param>
  588. /// <param name="talign">Specifies how the text will be aligned horizontally.</param>
  589. /// <param name="wordWrap">If <c>true</c>, the text will be wrapped to new lines as need. If <c>false</c>, forces text to fit a single line. Line breaks are converted to spaces. The text will be clipped to <c>width</c></param>
  590. /// <param name="preserveTrailingSpaces">If <c>true</c> and 'wordWrap' also true, the wrapped text will keep the trailing spaces. If <c>false</c>, the trailing spaces will be trimmed.</param>
  591. /// <param name="tabWidth">The tab width.</param>
  592. /// <param name="textDirection">The text direction.</param>
  593. /// <returns>A list of word wrapped lines.</returns>
  594. /// <remarks>
  595. /// <para>
  596. /// An empty <c>text</c> string will result in one empty line.
  597. /// </para>
  598. /// <para>
  599. /// If <c>width</c> is 0, a single, empty line will be returned.
  600. /// </para>
  601. /// <para>
  602. /// If <c>width</c> is int.MaxValue, the text will be formatted to the maximum width possible.
  603. /// </para>
  604. /// </remarks>
  605. public static List<ustring> Format (ustring text, int width, TextAlignment talign, bool wordWrap, bool preserveTrailingSpaces = false, int tabWidth = 0, TextDirection textDirection = TextDirection.LeftRight_TopBottom)
  606. {
  607. return Format (text, width, talign == TextAlignment.Justified, wordWrap, preserveTrailingSpaces, tabWidth, textDirection);
  608. }
  609. /// <summary>
  610. /// Reformats text into lines, applying text alignment and optionally wrapping text to new lines on word boundaries.
  611. /// </summary>
  612. /// <param name="text"></param>
  613. /// <param name="width">The width to bound the text to for word wrapping and clipping.</param>
  614. /// <param name="justify">Specifies whether the text should be justified.</param>
  615. /// <param name="wordWrap">If <c>true</c>, the text will be wrapped to new lines as need. If <c>false</c>, forces text to fit a single line. Line breaks are converted to spaces. The text will be clipped to <c>width</c></param>
  616. /// <param name="preserveTrailingSpaces">If <c>true</c> and 'wordWrap' also true, the wrapped text will keep the trailing spaces. If <c>false</c>, the trailing spaces will be trimmed.</param>
  617. /// <param name="tabWidth">The tab width.</param>
  618. /// <param name="textDirection">The text direction.</param>
  619. /// <returns>A list of word wrapped lines.</returns>
  620. /// <remarks>
  621. /// <para>
  622. /// An empty <c>text</c> string will result in one empty line.
  623. /// </para>
  624. /// <para>
  625. /// If <c>width</c> is 0, a single, empty line will be returned.
  626. /// </para>
  627. /// <para>
  628. /// If <c>width</c> is int.MaxValue, the text will be formatted to the maximum width possible.
  629. /// </para>
  630. /// </remarks>
  631. public static List<ustring> Format (ustring text, int width, bool justify, bool wordWrap,
  632. bool preserveTrailingSpaces = false, int tabWidth = 0, TextDirection textDirection = TextDirection.LeftRight_TopBottom)
  633. {
  634. if (width < 0) {
  635. throw new ArgumentOutOfRangeException ("width cannot be negative");
  636. }
  637. if (preserveTrailingSpaces && !wordWrap) {
  638. throw new ArgumentException ("if 'preserveTrailingSpaces' is true, then 'wordWrap' must be true either.");
  639. }
  640. List<ustring> lineResult = new List<ustring> ();
  641. if (ustring.IsNullOrEmpty (text) || width == 0) {
  642. lineResult.Add (ustring.Empty);
  643. return lineResult;
  644. }
  645. if (wordWrap == false) {
  646. text = ReplaceCRLFWithSpace (text);
  647. lineResult.Add (ClipAndJustify (text, width, justify, textDirection));
  648. return lineResult;
  649. }
  650. var runes = text.ToRuneList ();
  651. int runeCount = runes.Count;
  652. int lp = 0;
  653. for (int i = 0; i < runeCount; i++) {
  654. Rune c = runes [i];
  655. if (c == '\n') {
  656. var wrappedLines = WordWrap (ustring.Make (runes.GetRange (lp, i - lp)), width, preserveTrailingSpaces, tabWidth, textDirection);
  657. foreach (var line in wrappedLines) {
  658. lineResult.Add (ClipAndJustify (line, width, justify, textDirection));
  659. }
  660. if (wrappedLines.Count == 0) {
  661. lineResult.Add (ustring.Empty);
  662. }
  663. lp = i + 1;
  664. }
  665. }
  666. foreach (var line in WordWrap (ustring.Make (runes.GetRange (lp, runeCount - lp)), width, preserveTrailingSpaces, tabWidth, textDirection)) {
  667. lineResult.Add (ClipAndJustify (line, width, justify, textDirection));
  668. }
  669. return lineResult;
  670. }
  671. /// <summary>
  672. /// Computes the number of lines needed to render the specified text given the width.
  673. /// </summary>
  674. /// <returns>Number of lines.</returns>
  675. /// <param name="text">Text, may contain newlines.</param>
  676. /// <param name="width">The minimum width for the text.</param>
  677. public static int MaxLines (ustring text, int width)
  678. {
  679. var result = TextFormatter.Format (text, width, false, true);
  680. return result.Count;
  681. }
  682. /// <summary>
  683. /// Computes the maximum width needed to render the text (single line or multiple lines) given a minimum width.
  684. /// </summary>
  685. /// <returns>Max width of lines.</returns>
  686. /// <param name="text">Text, may contain newlines.</param>
  687. /// <param name="width">The minimum width for the text.</param>
  688. public static int MaxWidth (ustring text, int width)
  689. {
  690. var result = TextFormatter.Format (text, width, false, true);
  691. var max = 0;
  692. result.ForEach (s => {
  693. var m = 0;
  694. s.ToRuneList ().ForEach (r => m += Math.Max (Rune.ColumnWidth (r), 1));
  695. if (m > max) {
  696. max = m;
  697. }
  698. });
  699. return max;
  700. }
  701. /// <summary>
  702. /// Gets the total width of the passed text.
  703. /// </summary>
  704. /// <param name="text"></param>
  705. /// <returns>The text width.</returns>
  706. public static int GetTextWidth (ustring text)
  707. {
  708. return text.ToRuneList ().Sum (r => Math.Max (Rune.ColumnWidth (r), 1));
  709. }
  710. /// <summary>
  711. /// Gets the maximum characters width from the list based on the <paramref name="startIndex"/>
  712. /// and the <paramref name="length"/>.
  713. /// </summary>
  714. /// <param name="lines">The lines.</param>
  715. /// <param name="startIndex">The start index.</param>
  716. /// <param name="length">The length.</param>
  717. /// <returns>The maximum characters width.</returns>
  718. public static int GetSumMaxCharWidth (List<ustring> lines, int startIndex = -1, int length = -1)
  719. {
  720. var max = 0;
  721. for (int i = (startIndex == -1 ? 0 : startIndex); i < (length == -1 ? lines.Count : startIndex + length); i++) {
  722. var runes = lines [i];
  723. if (runes.Length > 0)
  724. max += runes.Max (r => Math.Max (Rune.ColumnWidth (r), 1));
  725. }
  726. return max;
  727. }
  728. /// <summary>
  729. /// Gets the maximum characters width from the text based on the <paramref name="startIndex"/>
  730. /// and the <paramref name="length"/>.
  731. /// </summary>
  732. /// <param name="text">The text.</param>
  733. /// <param name="startIndex">The start index.</param>
  734. /// <param name="length">The length.</param>
  735. /// <returns>The maximum characters width.</returns>
  736. public static int GetSumMaxCharWidth (ustring text, int startIndex = -1, int length = -1)
  737. {
  738. var max = 0;
  739. var runes = text.ToRunes ();
  740. for (int i = (startIndex == -1 ? 0 : startIndex); i < (length == -1 ? runes.Length : startIndex + length); i++) {
  741. max += Math.Max (Rune.ColumnWidth (runes [i]), 1);
  742. }
  743. return max;
  744. }
  745. /// <summary>
  746. /// Gets the index position from the text based on the <paramref name="width"/>.
  747. /// </summary>
  748. /// <param name="text">The text.</param>
  749. /// <param name="width">The width.</param>
  750. /// <returns>The index of the text that fit the width.</returns>
  751. public static int GetMaxLengthForWidth (ustring text, int width)
  752. {
  753. var runes = text.ToRuneList ();
  754. var runesLength = 0;
  755. var runeIdx = 0;
  756. for (; runeIdx < runes.Count; runeIdx++) {
  757. var runeWidth = Math.Max (Rune.ColumnWidth (runes [runeIdx]), 1);
  758. if (runesLength + runeWidth > width) {
  759. break;
  760. }
  761. runesLength += runeWidth;
  762. }
  763. return runeIdx;
  764. }
  765. /// <summary>
  766. /// Gets the index position from the list based on the <paramref name="width"/>.
  767. /// </summary>
  768. /// <param name="runes">The runes.</param>
  769. /// <param name="width">The width.</param>
  770. /// <returns>The index of the list that fit the width.</returns>
  771. public static int GetMaxLengthForWidth (List<Rune> runes, int width)
  772. {
  773. var runesLength = 0;
  774. var runeIdx = 0;
  775. for (; runeIdx < runes.Count; runeIdx++) {
  776. var runeWidth = Math.Max (Rune.ColumnWidth (runes [runeIdx]), 1);
  777. if (runesLength + runeWidth > width) {
  778. break;
  779. }
  780. runesLength += runeWidth;
  781. }
  782. return runeIdx;
  783. }
  784. /// <summary>
  785. /// Calculates the rectangle required to hold text, assuming no word wrapping.
  786. /// </summary>
  787. /// <param name="x">The x location of the rectangle</param>
  788. /// <param name="y">The y location of the rectangle</param>
  789. /// <param name="text">The text to measure</param>
  790. /// <param name="direction">The text direction.</param>
  791. /// <returns></returns>
  792. public static Rect CalcRect (int x, int y, ustring text, TextDirection direction = TextDirection.LeftRight_TopBottom)
  793. {
  794. if (ustring.IsNullOrEmpty (text)) {
  795. return new Rect (new Point (x, y), Size.Empty);
  796. }
  797. int w, h;
  798. if (IsHorizontalDirection (direction)) {
  799. int mw = 0;
  800. int ml = 1;
  801. int cols = 0;
  802. foreach (var rune in text) {
  803. if (rune == '\n') {
  804. ml++;
  805. if (cols > mw) {
  806. mw = cols;
  807. }
  808. cols = 0;
  809. } else if (rune != '\r') {
  810. cols++;
  811. var rw = Rune.ColumnWidth (rune);
  812. if (rw > 0) {
  813. rw--;
  814. }
  815. cols += rw;
  816. }
  817. }
  818. if (cols > mw) {
  819. mw = cols;
  820. }
  821. w = mw;
  822. h = ml;
  823. } else {
  824. int vw = 0;
  825. int vh = 0;
  826. int rows = 0;
  827. foreach (var rune in text) {
  828. if (rune == '\n') {
  829. vw++;
  830. if (rows > vh) {
  831. vh = rows;
  832. }
  833. rows = 0;
  834. } else if (rune != '\r') {
  835. rows++;
  836. var rw = Rune.ColumnWidth (rune);
  837. if (rw < 0) {
  838. rw++;
  839. }
  840. if (rw > vw) {
  841. vw = rw;
  842. }
  843. }
  844. }
  845. if (rows > vh) {
  846. vh = rows;
  847. }
  848. w = vw;
  849. h = vh;
  850. }
  851. return new Rect (x, y, w, h);
  852. }
  853. /// <summary>
  854. /// Finds the hotkey and its location in text.
  855. /// </summary>
  856. /// <param name="text">The text to look in.</param>
  857. /// <param name="hotKeySpecifier">The hotkey specifier (e.g. '_') to look for.</param>
  858. /// <param name="firstUpperCase">If <c>true</c> the legacy behavior of identifying the first upper case character as the hotkey will be enabled.
  859. /// Regardless of the value of this parameter, <c>hotKeySpecifier</c> takes precedence.</param>
  860. /// <param name="hotPos">Outputs the Rune index into <c>text</c>.</param>
  861. /// <param name="hotKey">Outputs the hotKey.</param>
  862. /// <returns><c>true</c> if a hotkey was found; <c>false</c> otherwise.</returns>
  863. public static bool FindHotKey (ustring text, Rune hotKeySpecifier, bool firstUpperCase, out int hotPos, out Key hotKey)
  864. {
  865. if (ustring.IsNullOrEmpty (text) || hotKeySpecifier == (Rune)0xFFFF) {
  866. hotPos = -1;
  867. hotKey = Key.Unknown;
  868. return false;
  869. }
  870. Rune hot_key = (Rune)0;
  871. int hot_pos = -1;
  872. // Use first hot_key char passed into 'hotKey'.
  873. // TODO: Ignore hot_key of two are provided
  874. // TODO: Do not support non-alphanumeric chars that can't be typed
  875. int i = 0;
  876. foreach (Rune c in text) {
  877. if ((char)c != 0xFFFD) {
  878. if (c == hotKeySpecifier) {
  879. hot_pos = i;
  880. } else if (hot_pos > -1) {
  881. hot_key = c;
  882. break;
  883. }
  884. }
  885. i++;
  886. }
  887. // Legacy support - use first upper case char if the specifier was not found
  888. if (hot_pos == -1 && firstUpperCase) {
  889. i = 0;
  890. foreach (Rune c in text) {
  891. if ((char)c != 0xFFFD) {
  892. if (Rune.IsUpper (c)) {
  893. hot_key = c;
  894. hot_pos = i;
  895. break;
  896. }
  897. }
  898. i++;
  899. }
  900. }
  901. if (hot_key != (Rune)0 && hot_pos != -1) {
  902. hotPos = hot_pos;
  903. if (hot_key.IsValid && char.IsLetterOrDigit ((char)hot_key)) {
  904. hotKey = (Key)char.ToUpperInvariant ((char)hot_key);
  905. return true;
  906. }
  907. }
  908. hotPos = -1;
  909. hotKey = Key.Unknown;
  910. return false;
  911. }
  912. /// <summary>
  913. /// Replaces the Rune at the index specified by the <c>hotPos</c> parameter with a tag identifying
  914. /// it as the hotkey.
  915. /// </summary>
  916. /// <param name="text">The text to tag the hotkey in.</param>
  917. /// <param name="hotPos">The Rune index of the hotkey in <c>text</c>.</param>
  918. /// <returns>The text with the hotkey tagged.</returns>
  919. /// <remarks>
  920. /// The returned string will not render correctly without first un-doing the tag. To undo the tag, search for
  921. /// Runes with a bitmask of <c>otKeyTagMask</c> and remove that bitmask.
  922. /// </remarks>
  923. public ustring ReplaceHotKeyWithTag (ustring text, int hotPos)
  924. {
  925. // Set the high bit
  926. var runes = text.ToRuneList ();
  927. if (Rune.IsLetterOrNumber (runes [hotPos])) {
  928. runes [hotPos] = new Rune ((uint)runes [hotPos] | HotKeyTagMask);
  929. }
  930. return ustring.Make (runes);
  931. }
  932. /// <summary>
  933. /// Removes the hotkey specifier from text.
  934. /// </summary>
  935. /// <param name="text">The text to manipulate.</param>
  936. /// <param name="hotKeySpecifier">The hot-key specifier (e.g. '_') to look for.</param>
  937. /// <param name="hotPos">Returns the position of the hot-key in the text. -1 if not found.</param>
  938. /// <returns>The input text with the hotkey specifier ('_') removed.</returns>
  939. public static ustring RemoveHotKeySpecifier (ustring text, int hotPos, Rune hotKeySpecifier)
  940. {
  941. if (ustring.IsNullOrEmpty (text)) {
  942. return text;
  943. }
  944. // Scan
  945. ustring start = ustring.Empty;
  946. int i = 0;
  947. foreach (Rune c in text) {
  948. if (c == hotKeySpecifier && i == hotPos) {
  949. i++;
  950. continue;
  951. }
  952. start += ustring.Make (c);
  953. i++;
  954. }
  955. return start;
  956. }
  957. /// <summary>
  958. /// Draws the text held by <see cref="TextFormatter"/> to <see cref="Application.Driver"/> using the colors specified.
  959. /// </summary>
  960. /// <param name="bounds">Specifies the screen-relative location and maximum size for drawing the text.</param>
  961. /// <param name="normalColor">The color to use for all text except the hotkey</param>
  962. /// <param name="hotColor">The color to use to draw the hotkey</param>
  963. /// <param name="containerBounds">Specifies the screen-relative location and maximum container size.</param>
  964. public void Draw (Rect bounds, Attribute normalColor, Attribute hotColor, Rect containerBounds = default)
  965. {
  966. // With this check, we protect against subclasses with overrides of Text (like Button)
  967. if (ustring.IsNullOrEmpty (text)) {
  968. return;
  969. }
  970. Application.Driver?.SetAttribute (normalColor);
  971. // Use "Lines" to ensure a Format (don't use "lines"))
  972. var linesFormated = Lines;
  973. switch (textDirection) {
  974. case TextDirection.TopBottom_RightLeft:
  975. case TextDirection.LeftRight_BottomTop:
  976. case TextDirection.RightLeft_BottomTop:
  977. case TextDirection.BottomTop_RightLeft:
  978. linesFormated.Reverse ();
  979. break;
  980. }
  981. for (int line = 0; line < linesFormated.Count; line++) {
  982. var isVertical = IsVerticalDirection (textDirection);
  983. if ((isVertical && (line > bounds.Width)) || (!isVertical && (line > bounds.Height)))
  984. continue;
  985. var runes = lines [line].ToRunes ();
  986. switch (textDirection) {
  987. case TextDirection.RightLeft_BottomTop:
  988. case TextDirection.RightLeft_TopBottom:
  989. case TextDirection.BottomTop_LeftRight:
  990. case TextDirection.BottomTop_RightLeft:
  991. runes = runes.Reverse ().ToArray ();
  992. break;
  993. }
  994. // When text is justified, we lost left or right, so we use the direction to align.
  995. int x, y;
  996. // Horizontal Alignment
  997. if (textAlignment == TextAlignment.Right || (textAlignment == TextAlignment.Justified && !IsLeftToRight (textDirection))) {
  998. if (isVertical) {
  999. var runesWidth = GetSumMaxCharWidth (Lines, line);
  1000. x = bounds.Right - runesWidth;
  1001. CursorPosition = bounds.Width - runesWidth + hotKeyPos;
  1002. } else {
  1003. var runesWidth = GetTextWidth (ustring.Make (runes));
  1004. x = bounds.Right - runesWidth;
  1005. CursorPosition = bounds.Width - runesWidth + hotKeyPos;
  1006. }
  1007. } else if (textAlignment == TextAlignment.Left || textAlignment == TextAlignment.Justified) {
  1008. if (isVertical) {
  1009. var runesWidth = line > 0 ? GetSumMaxCharWidth (Lines, 0, line) : 0;
  1010. x = bounds.Left + runesWidth;
  1011. } else {
  1012. x = bounds.Left;
  1013. }
  1014. CursorPosition = hotKeyPos;
  1015. } else if (textAlignment == TextAlignment.Centered) {
  1016. if (isVertical) {
  1017. var runesWidth = GetSumMaxCharWidth (Lines, line);
  1018. x = bounds.Left + line + ((bounds.Width - runesWidth) / 2);
  1019. CursorPosition = (bounds.Width - runesWidth) / 2 + hotKeyPos;
  1020. } else {
  1021. var runesWidth = GetTextWidth (ustring.Make (runes));
  1022. x = bounds.Left + (bounds.Width - runesWidth) / 2;
  1023. CursorPosition = (bounds.Width - runesWidth) / 2 + hotKeyPos;
  1024. }
  1025. } else {
  1026. throw new ArgumentOutOfRangeException ();
  1027. }
  1028. // Vertical Alignment
  1029. if (textVerticalAlignment == VerticalTextAlignment.Bottom || (textVerticalAlignment == VerticalTextAlignment.Justified && !IsTopToBottom (textDirection))) {
  1030. if (isVertical) {
  1031. y = bounds.Bottom - runes.Length;
  1032. } else {
  1033. y = bounds.Bottom - Lines.Count + line;
  1034. }
  1035. } else if (textVerticalAlignment == VerticalTextAlignment.Top || textVerticalAlignment == VerticalTextAlignment.Justified) {
  1036. if (isVertical) {
  1037. y = bounds.Top;
  1038. } else {
  1039. y = bounds.Top + line;
  1040. }
  1041. } else if (textVerticalAlignment == VerticalTextAlignment.Middle) {
  1042. if (isVertical) {
  1043. var s = (bounds.Height - runes.Length) / 2;
  1044. y = bounds.Top + s;
  1045. } else {
  1046. var s = (bounds.Height - Lines.Count) / 2;
  1047. y = bounds.Top + line + s;
  1048. }
  1049. } else {
  1050. throw new ArgumentOutOfRangeException ();
  1051. }
  1052. var start = isVertical ? bounds.Top : bounds.Left;
  1053. var size = isVertical ? bounds.Height : bounds.Width;
  1054. var current = start;
  1055. var startX = start < 0
  1056. ? start
  1057. : isVertical ? start - y : start - x;
  1058. var savedClip = Application.Driver?.Clip;
  1059. if (Application.Driver != null && containerBounds != default) {
  1060. Application.Driver.Clip = containerBounds == default
  1061. ? bounds
  1062. : new Rect (Math.Max (containerBounds.X, bounds.X),
  1063. Math.Max (containerBounds.Y, bounds.Y),
  1064. Math.Min (containerBounds.Width, containerBounds.Right - bounds.Left),
  1065. Math.Min (containerBounds.Height, containerBounds.Bottom - bounds.Top));
  1066. }
  1067. for (var idx = startX; current < start + size; idx++) {
  1068. if (idx < 0) {
  1069. current++;
  1070. continue;
  1071. }
  1072. var rune = (Rune)' ';
  1073. if (isVertical) {
  1074. Application.Driver?.Move (x, current);
  1075. if (idx >= 0 && idx < runes.Length) {
  1076. rune = runes [idx];
  1077. }
  1078. } else {
  1079. Application.Driver?.Move (current, y);
  1080. if (idx >= 0 && idx < runes.Length) {
  1081. rune = runes [idx];
  1082. }
  1083. }
  1084. if ((rune & HotKeyTagMask) == HotKeyTagMask) {
  1085. if ((isVertical && textVerticalAlignment == VerticalTextAlignment.Justified) ||
  1086. (!isVertical && textAlignment == TextAlignment.Justified)) {
  1087. CursorPosition = idx - start;
  1088. }
  1089. Application.Driver?.SetAttribute (hotColor);
  1090. Application.Driver?.AddRune ((Rune)((uint)rune & ~HotKeyTagMask));
  1091. Application.Driver?.SetAttribute (normalColor);
  1092. } else {
  1093. Application.Driver?.AddRune (rune);
  1094. }
  1095. var runeWidth = Math.Max (Rune.ColumnWidth (rune), 1);
  1096. if (isVertical) {
  1097. current++;
  1098. } else {
  1099. current += runeWidth;
  1100. }
  1101. if (!isVertical && idx + 1 < runes.Length && current + Rune.ColumnWidth (runes [idx + 1]) > start + size) {
  1102. break;
  1103. }
  1104. }
  1105. if (Application.Driver != null)
  1106. Application.Driver.Clip = (Rect)savedClip;
  1107. }
  1108. }
  1109. }
  1110. }