LineCanvas.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>Facilitates box drawing and line intersection detection and rendering. Does not support diagonal lines.</summary>
  4. public class LineCanvas : IDisposable
  5. {
  6. /// <summary>Creates a new instance.</summary>
  7. public LineCanvas ()
  8. {
  9. // TODO: Refactor ConfigurationManager to not use an event handler for this.
  10. // Instead, have it call a method on any class appropriately attributed
  11. // to update the cached values. See Issue #2871
  12. Applied += ConfigurationManager_Applied;
  13. }
  14. private readonly List<StraightLine> _lines = [];
  15. /// <summary>Creates a new instance with the given <paramref name="lines"/>.</summary>
  16. /// <param name="lines">Initial lines for the canvas.</param>
  17. public LineCanvas (IEnumerable<StraightLine> lines) : this () { _lines = lines.ToList (); }
  18. /// <summary>
  19. /// Optional <see cref="FillPair"/> which when present overrides the <see cref="StraightLine.Attribute"/>
  20. /// (colors) of lines in the canvas. This can be used e.g. to apply a global <see cref="GradientFill"/>
  21. /// across all lines.
  22. /// </summary>
  23. public FillPair? Fill { get; set; }
  24. private Rectangle _cachedBounds;
  25. /// <summary>
  26. /// Gets the rectangle that describes the bounds of the canvas. Location is the coordinates of the line that is
  27. /// the furthest left/top and Size is defined by the line that extends the furthest right/bottom.
  28. /// </summary>
  29. public Rectangle Bounds
  30. {
  31. get
  32. {
  33. if (_cachedBounds.IsEmpty)
  34. {
  35. if (_lines.Count == 0)
  36. {
  37. return _cachedBounds;
  38. }
  39. Rectangle bounds = _lines [0].Bounds;
  40. for (var i = 1; i < _lines.Count; i++)
  41. {
  42. bounds = Rectangle.Union (bounds, _lines [i].Bounds);
  43. }
  44. if (bounds is { Width: 0 } or { Height: 0 })
  45. {
  46. bounds = bounds with
  47. {
  48. Width = Math.Clamp (bounds.Width, 1, short.MaxValue),
  49. Height = Math.Clamp (bounds.Height, 1, short.MaxValue)
  50. };
  51. }
  52. _cachedBounds = bounds;
  53. }
  54. return _cachedBounds;
  55. }
  56. }
  57. /// <summary>Gets the lines in the canvas.</summary>
  58. public IReadOnlyCollection<StraightLine> Lines => _lines.AsReadOnly ();
  59. /// <summary>
  60. /// <para>Adds a new <paramref name="length"/> long line to the canvas starting at <paramref name="start"/>.</para>
  61. /// <para>
  62. /// Use positive <paramref name="length"/> for the line to extend Right and negative for Left when
  63. /// <see cref="Orientation"/> is <see cref="Orientation.Horizontal"/>.
  64. /// </para>
  65. /// <para>
  66. /// Use positive <paramref name="length"/> for the line to extend Down and negative for Up when
  67. /// <see cref="Orientation"/> is <see cref="Orientation.Vertical"/>.
  68. /// </para>
  69. /// </summary>
  70. /// <param name="start">Starting point.</param>
  71. /// <param name="length">
  72. /// The length of line. 0 for an intersection (cross or T). Positive for Down/Right. Negative for
  73. /// Up/Left.
  74. /// </param>
  75. /// <param name="orientation">The direction of the line.</param>
  76. /// <param name="style">The style of line to use</param>
  77. /// <param name="attribute"></param>
  78. public void AddLine (
  79. Point start,
  80. int length,
  81. Orientation orientation,
  82. LineStyle style,
  83. Attribute? attribute = null
  84. )
  85. {
  86. _cachedBounds = Rectangle.Empty;
  87. _lines.Add (new (start, length, orientation, style, attribute));
  88. }
  89. /// <summary>Adds a new line to the canvas</summary>
  90. /// <param name="line"></param>
  91. public void AddLine (StraightLine line)
  92. {
  93. _cachedBounds = Rectangle.Empty;
  94. _lines.Add (line);
  95. }
  96. private Region? _exclusionRegion;
  97. /// <summary>
  98. /// Causes the provided region to be excluded from <see cref="GetCellMap"/> and <see cref="GetMap()"/>.
  99. /// </summary>
  100. /// <remarks>
  101. /// <para>
  102. /// Each call to this method will add to the exclusion region. To clear the exclusion region, call
  103. /// <see cref="ClearCache"/>.
  104. /// </para>
  105. /// </remarks>
  106. public void Exclude (Region region)
  107. {
  108. _exclusionRegion ??= new ();
  109. _exclusionRegion.Union (region);
  110. }
  111. /// <summary>
  112. /// Clears the exclusion region. After calling this method, <see cref="GetCellMap"/> and <see cref="GetMap()"/> will
  113. /// return all points in the canvas.
  114. /// </summary>
  115. public void ClearExclusions () { _exclusionRegion = null; }
  116. /// <summary>Clears all lines from the LineCanvas.</summary>
  117. public void Clear ()
  118. {
  119. _cachedBounds = Rectangle.Empty;
  120. _lines.Clear ();
  121. ClearExclusions ();
  122. }
  123. /// <summary>
  124. /// Clears any cached states from the canvas. Call this method if you make changes to lines that have already been
  125. /// added.
  126. /// </summary>
  127. public void ClearCache () { _cachedBounds = Rectangle.Empty; }
  128. /// <summary>
  129. /// Evaluates the lines that have been added to the canvas and returns a map containing the glyphs and their
  130. /// locations. The glyphs are the characters that should be rendered so that all lines connect up with the appropriate
  131. /// intersection symbols.
  132. /// </summary>
  133. /// <remarks>
  134. /// <para>
  135. /// Only the points within the <see cref="Bounds"/> of the canvas that are not in the exclusion region will be
  136. /// returned. To exclude points from the map, use <see cref="Exclude"/>.
  137. /// </para>
  138. /// </remarks>
  139. /// <returns>A map of all the points within the canvas.</returns>
  140. public Dictionary<Point, Cell?> GetCellMap ()
  141. {
  142. Dictionary<Point, Cell?> map = new ();
  143. // walk through each pixel of the bitmap
  144. for (int y = Bounds.Y; y < Bounds.Y + Bounds.Height; y++)
  145. {
  146. for (int x = Bounds.X; x < Bounds.X + Bounds.Width; x++)
  147. {
  148. IntersectionDefinition? [] intersects = _lines
  149. .Select (l => l.Intersects (x, y))
  150. .Where (i => i is { })
  151. .ToArray ();
  152. Cell? cell = GetCellForIntersects (Application.Driver, intersects);
  153. if (cell is { } && _exclusionRegion?.Contains (x, y) is null or false)
  154. {
  155. map.Add (new (x, y), cell);
  156. }
  157. }
  158. }
  159. return map;
  160. }
  161. // TODO: Unless there's an obvious use case for this API we should delete it in favor of the
  162. // simpler version that doesn't take an area.
  163. /// <summary>
  164. /// Evaluates the lines that have been added to the canvas and returns a map containing the glyphs and their
  165. /// locations. The glyphs are the characters that should be rendered so that all lines connect up with the appropriate
  166. /// intersection symbols.
  167. /// </summary>
  168. /// <remarks>
  169. /// <para>
  170. /// Only the points within the <paramref name="inArea"/> of the canvas that are not in the exclusion region will be
  171. /// returned. To exclude points from the map, use <see cref="Exclude"/>.
  172. /// </para>
  173. /// </remarks>
  174. /// <param name="inArea">A rectangle to constrain the search by.</param>
  175. /// <returns>A map of the points within the canvas that intersect with <paramref name="inArea"/>.</returns>
  176. public Dictionary<Point, Rune> GetMap (Rectangle inArea)
  177. {
  178. Dictionary<Point, Rune> map = new ();
  179. // walk through each pixel of the bitmap
  180. for (int y = inArea.Y; y < inArea.Y + inArea.Height; y++)
  181. {
  182. for (int x = inArea.X; x < inArea.X + inArea.Width; x++)
  183. {
  184. IntersectionDefinition? [] intersects = _lines
  185. .Select (l => l.Intersects (x, y))
  186. .Where (i => i is { })
  187. .ToArray ();
  188. Rune? rune = GetRuneForIntersects (Application.Driver, intersects);
  189. if (rune is { } && _exclusionRegion?.Contains (x, y) is null or false)
  190. {
  191. map.Add (new (x, y), rune.Value);
  192. }
  193. }
  194. }
  195. return map;
  196. }
  197. /// <summary>
  198. /// Evaluates the lines that have been added to the canvas and returns a map containing the glyphs and their
  199. /// locations. The glyphs are the characters that should be rendered so that all lines connect up with the appropriate
  200. /// intersection symbols.
  201. /// </summary>
  202. /// <remarks>
  203. /// <para>
  204. /// Only the points within the <see cref="Bounds"/> of the canvas that are not in the exclusion region will be
  205. /// returned. To exclude points from the map, use <see cref="Exclude"/>.
  206. /// </para>
  207. /// </remarks>
  208. /// <returns>A map of all the points within the canvas.</returns>
  209. public Dictionary<Point, Rune> GetMap () { return GetMap (Bounds); }
  210. /// <summary>Merges one line canvas into this one.</summary>
  211. /// <param name="lineCanvas"></param>
  212. public void Merge (LineCanvas lineCanvas)
  213. {
  214. foreach (StraightLine line in lineCanvas._lines)
  215. {
  216. AddLine (line);
  217. }
  218. if (lineCanvas._exclusionRegion is { })
  219. {
  220. _exclusionRegion ??= new ();
  221. _exclusionRegion.Union (lineCanvas._exclusionRegion);
  222. }
  223. }
  224. /// <summary>Removes the last line added to the canvas</summary>
  225. /// <returns></returns>
  226. public StraightLine RemoveLastLine ()
  227. {
  228. StraightLine? l = _lines.LastOrDefault ();
  229. if (l is { })
  230. {
  231. _lines.Remove (l);
  232. }
  233. return l!;
  234. }
  235. /// <summary>
  236. /// Returns the contents of the line canvas rendered to a string. The string will include all columns and rows,
  237. /// even if <see cref="Bounds"/> has negative coordinates. For example, if the canvas contains a single line that
  238. /// starts at (-1,-1) with a length of 2, the rendered string will have a length of 2.
  239. /// </summary>
  240. /// <returns>The canvas rendered to a string.</returns>
  241. public override string ToString ()
  242. {
  243. if (Bounds.IsEmpty)
  244. {
  245. return string.Empty;
  246. }
  247. // Generate the rune map for the entire canvas
  248. Dictionary<Point, Rune> runeMap = GetMap ();
  249. // Create the rune canvas
  250. Rune [,] canvas = new Rune [Bounds.Height, Bounds.Width];
  251. // Copy the rune map to the canvas, adjusting for any negative coordinates
  252. foreach (KeyValuePair<Point, Rune> kvp in runeMap)
  253. {
  254. int x = kvp.Key.X - Bounds.X;
  255. int y = kvp.Key.Y - Bounds.Y;
  256. canvas [y, x] = kvp.Value;
  257. }
  258. // Convert the canvas to a string
  259. var sb = new StringBuilder ();
  260. for (var y = 0; y < canvas.GetLength (0); y++)
  261. {
  262. for (var x = 0; x < canvas.GetLength (1); x++)
  263. {
  264. Rune r = canvas [y, x];
  265. sb.Append (r.Value == 0 ? ' ' : r.ToString ());
  266. }
  267. if (y < canvas.GetLength (0) - 1)
  268. {
  269. sb.AppendLine ();
  270. }
  271. }
  272. return sb.ToString ();
  273. }
  274. private static bool All (IntersectionDefinition? [] intersects, Orientation orientation)
  275. {
  276. return intersects.All (i => i!.Line.Orientation == orientation);
  277. }
  278. private void ConfigurationManager_Applied (object? sender, ConfigurationManagerEventArgs e)
  279. {
  280. foreach (KeyValuePair<IntersectionRuneType, IntersectionRuneResolver> irr in _runeResolvers)
  281. {
  282. irr.Value.SetGlyphs ();
  283. }
  284. }
  285. /// <summary>
  286. /// Returns true if all requested <paramref name="types"/> appear in <paramref name="intersects"/> and there are
  287. /// no additional <see cref="IntersectionRuneType"/>
  288. /// </summary>
  289. /// <param name="intersects"></param>
  290. /// <param name="types"></param>
  291. /// <returns></returns>
  292. private static bool Exactly (HashSet<IntersectionType> intersects, params IntersectionType [] types) { return intersects.SetEquals (types); }
  293. private Attribute? GetAttributeForIntersects (IntersectionDefinition? [] intersects)
  294. {
  295. return Fill?.GetAttribute (intersects [0]!.Point) ?? intersects [0]!.Line.Attribute;
  296. }
  297. private readonly Dictionary<IntersectionRuneType, IntersectionRuneResolver> _runeResolvers = new ()
  298. {
  299. {
  300. IntersectionRuneType.ULCorner,
  301. new ULIntersectionRuneResolver ()
  302. },
  303. {
  304. IntersectionRuneType.URCorner,
  305. new URIntersectionRuneResolver ()
  306. },
  307. {
  308. IntersectionRuneType.LLCorner,
  309. new LLIntersectionRuneResolver ()
  310. },
  311. {
  312. IntersectionRuneType.LRCorner,
  313. new LRIntersectionRuneResolver ()
  314. },
  315. {
  316. IntersectionRuneType.TopTee,
  317. new TopTeeIntersectionRuneResolver ()
  318. },
  319. {
  320. IntersectionRuneType.LeftTee,
  321. new LeftTeeIntersectionRuneResolver ()
  322. },
  323. {
  324. IntersectionRuneType.RightTee,
  325. new RightTeeIntersectionRuneResolver ()
  326. },
  327. {
  328. IntersectionRuneType.BottomTee,
  329. new BottomTeeIntersectionRuneResolver ()
  330. },
  331. {
  332. IntersectionRuneType.Cross,
  333. new CrossIntersectionRuneResolver ()
  334. }
  335. // TODO: Add other resolvers
  336. };
  337. private Cell? GetCellForIntersects (IConsoleDriver? driver, IntersectionDefinition? [] intersects)
  338. {
  339. if (!intersects.Any ())
  340. {
  341. return null;
  342. }
  343. var cell = new Cell ();
  344. Rune? rune = GetRuneForIntersects (driver, intersects);
  345. if (rune.HasValue)
  346. {
  347. cell.Rune = rune.Value;
  348. }
  349. cell.Attribute = GetAttributeForIntersects (intersects);
  350. return cell;
  351. }
  352. private Rune? GetRuneForIntersects (IConsoleDriver? driver, IntersectionDefinition? [] intersects)
  353. {
  354. if (!intersects.Any ())
  355. {
  356. return null;
  357. }
  358. IntersectionRuneType runeType = GetRuneTypeForIntersects (intersects);
  359. if (_runeResolvers.TryGetValue (runeType, out IntersectionRuneResolver? resolver))
  360. {
  361. return resolver.GetRuneForIntersects (driver, intersects);
  362. }
  363. // TODO: Remove these once we have all of the below ported to IntersectionRuneResolvers
  364. bool useDouble = intersects.Any (i => i?.Line.Style == LineStyle.Double);
  365. bool useDashed = intersects.Any (
  366. i => i?.Line.Style == LineStyle.Dashed
  367. || i?.Line.Style == LineStyle.RoundedDashed
  368. );
  369. bool useDotted = intersects.Any (
  370. i => i?.Line.Style == LineStyle.Dotted
  371. || i?.Line.Style == LineStyle.RoundedDotted
  372. );
  373. // horiz and vert lines same as Single for Rounded
  374. bool useThick = intersects.Any (i => i?.Line.Style == LineStyle.Heavy);
  375. bool useThickDashed = intersects.Any (i => i?.Line.Style == LineStyle.HeavyDashed);
  376. bool useThickDotted = intersects.Any (i => i?.Line.Style == LineStyle.HeavyDotted);
  377. // TODO: Support ruler
  378. //var useRuler = intersects.Any (i => i.Line.Style == LineStyle.Ruler && i.Line.Length != 0);
  379. // TODO: maybe make these resolvers too for simplicity?
  380. switch (runeType)
  381. {
  382. case IntersectionRuneType.None:
  383. return null;
  384. case IntersectionRuneType.Dot:
  385. return Glyphs.Dot;
  386. case IntersectionRuneType.HLine:
  387. if (useDouble)
  388. {
  389. return Glyphs.HLineDbl;
  390. }
  391. if (useDashed)
  392. {
  393. return Glyphs.HLineDa2;
  394. }
  395. if (useDotted)
  396. {
  397. return Glyphs.HLineDa3;
  398. }
  399. return useThick ? Glyphs.HLineHv :
  400. useThickDashed ? Glyphs.HLineHvDa2 :
  401. useThickDotted ? Glyphs.HLineHvDa3 : Glyphs.HLine;
  402. case IntersectionRuneType.VLine:
  403. if (useDouble)
  404. {
  405. return Glyphs.VLineDbl;
  406. }
  407. if (useDashed)
  408. {
  409. return Glyphs.VLineDa3;
  410. }
  411. if (useDotted)
  412. {
  413. return Glyphs.VLineDa4;
  414. }
  415. return useThick ? Glyphs.VLineHv :
  416. useThickDashed ? Glyphs.VLineHvDa3 :
  417. useThickDotted ? Glyphs.VLineHvDa4 : Glyphs.VLine;
  418. default:
  419. throw new (
  420. "Could not find resolver or switch case for "
  421. + nameof (runeType)
  422. + ":"
  423. + runeType
  424. );
  425. }
  426. }
  427. private IntersectionRuneType GetRuneTypeForIntersects (IntersectionDefinition? [] intersects)
  428. {
  429. HashSet<IntersectionType> set = new (intersects.Select (i => i!.Type));
  430. #region Cross Conditions
  431. if (Has (
  432. set,
  433. IntersectionType.PassOverHorizontal,
  434. IntersectionType.PassOverVertical
  435. ))
  436. {
  437. return IntersectionRuneType.Cross;
  438. }
  439. if (Has (
  440. set,
  441. IntersectionType.PassOverVertical,
  442. IntersectionType.StartLeft,
  443. IntersectionType.StartRight
  444. ))
  445. {
  446. return IntersectionRuneType.Cross;
  447. }
  448. if (Has (
  449. set,
  450. IntersectionType.PassOverHorizontal,
  451. IntersectionType.StartUp,
  452. IntersectionType.StartDown
  453. ))
  454. {
  455. return IntersectionRuneType.Cross;
  456. }
  457. if (Has (
  458. set,
  459. IntersectionType.StartLeft,
  460. IntersectionType.StartRight,
  461. IntersectionType.StartUp,
  462. IntersectionType.StartDown
  463. ))
  464. {
  465. return IntersectionRuneType.Cross;
  466. }
  467. #endregion
  468. #region Corner Conditions
  469. if (Exactly (
  470. set,
  471. IntersectionType.StartRight,
  472. IntersectionType.StartDown
  473. ))
  474. {
  475. return IntersectionRuneType.ULCorner;
  476. }
  477. if (Exactly (
  478. set,
  479. IntersectionType.StartLeft,
  480. IntersectionType.StartDown
  481. ))
  482. {
  483. return IntersectionRuneType.URCorner;
  484. }
  485. if (Exactly (
  486. set,
  487. IntersectionType.StartUp,
  488. IntersectionType.StartLeft
  489. ))
  490. {
  491. return IntersectionRuneType.LRCorner;
  492. }
  493. if (Exactly (
  494. set,
  495. IntersectionType.StartUp,
  496. IntersectionType.StartRight
  497. ))
  498. {
  499. return IntersectionRuneType.LLCorner;
  500. }
  501. #endregion Corner Conditions
  502. #region T Conditions
  503. if (Has (
  504. set,
  505. IntersectionType.PassOverHorizontal,
  506. IntersectionType.StartDown
  507. ))
  508. {
  509. return IntersectionRuneType.TopTee;
  510. }
  511. if (Has (
  512. set,
  513. IntersectionType.StartRight,
  514. IntersectionType.StartLeft,
  515. IntersectionType.StartDown
  516. ))
  517. {
  518. return IntersectionRuneType.TopTee;
  519. }
  520. if (Has (
  521. set,
  522. IntersectionType.PassOverHorizontal,
  523. IntersectionType.StartUp
  524. ))
  525. {
  526. return IntersectionRuneType.BottomTee;
  527. }
  528. if (Has (
  529. set,
  530. IntersectionType.StartRight,
  531. IntersectionType.StartLeft,
  532. IntersectionType.StartUp
  533. ))
  534. {
  535. return IntersectionRuneType.BottomTee;
  536. }
  537. if (Has (
  538. set,
  539. IntersectionType.PassOverVertical,
  540. IntersectionType.StartRight
  541. ))
  542. {
  543. return IntersectionRuneType.LeftTee;
  544. }
  545. if (Has (
  546. set,
  547. IntersectionType.StartRight,
  548. IntersectionType.StartDown,
  549. IntersectionType.StartUp
  550. ))
  551. {
  552. return IntersectionRuneType.LeftTee;
  553. }
  554. if (Has (
  555. set,
  556. IntersectionType.PassOverVertical,
  557. IntersectionType.StartLeft
  558. ))
  559. {
  560. return IntersectionRuneType.RightTee;
  561. }
  562. if (Has (
  563. set,
  564. IntersectionType.StartLeft,
  565. IntersectionType.StartDown,
  566. IntersectionType.StartUp
  567. ))
  568. {
  569. return IntersectionRuneType.RightTee;
  570. }
  571. #endregion
  572. if (All (intersects, Orientation.Horizontal))
  573. {
  574. return IntersectionRuneType.HLine;
  575. }
  576. if (All (intersects, Orientation.Vertical))
  577. {
  578. return IntersectionRuneType.VLine;
  579. }
  580. return IntersectionRuneType.Dot;
  581. }
  582. /// <summary>
  583. /// Returns true if the <paramref name="intersects"/> collection has all the <paramref name="types"/> specified
  584. /// (i.e. AND).
  585. /// </summary>
  586. /// <param name="intersects"></param>
  587. /// <param name="types"></param>
  588. /// <returns></returns>
  589. private bool Has (HashSet<IntersectionType> intersects, params IntersectionType [] types) { return types.All (t => intersects.Contains (t)); }
  590. private class BottomTeeIntersectionRuneResolver : IntersectionRuneResolver
  591. {
  592. public override void SetGlyphs ()
  593. {
  594. _round = Glyphs.BottomTee;
  595. _doubleH = Glyphs.BottomTeeDblH;
  596. _doubleV = Glyphs.BottomTeeDblV;
  597. _doubleBoth = Glyphs.BottomTeeDbl;
  598. _thickH = Glyphs.BottomTeeHvH;
  599. _thickV = Glyphs.BottomTeeHvV;
  600. _thickBoth = Glyphs.BottomTeeHvDblH;
  601. _normal = Glyphs.BottomTee;
  602. }
  603. }
  604. private class CrossIntersectionRuneResolver : IntersectionRuneResolver
  605. {
  606. public override void SetGlyphs ()
  607. {
  608. _round = Glyphs.Cross;
  609. _doubleH = Glyphs.CrossDblH;
  610. _doubleV = Glyphs.CrossDblV;
  611. _doubleBoth = Glyphs.CrossDbl;
  612. _thickH = Glyphs.CrossHvH;
  613. _thickV = Glyphs.CrossHvV;
  614. _thickBoth = Glyphs.CrossHv;
  615. _normal = Glyphs.Cross;
  616. }
  617. }
  618. private abstract class IntersectionRuneResolver
  619. {
  620. internal Rune _doubleBoth;
  621. internal Rune _doubleH;
  622. internal Rune _doubleV;
  623. internal Rune _normal;
  624. internal Rune _round;
  625. internal Rune _thickBoth;
  626. internal Rune _thickH;
  627. internal Rune _thickV;
  628. protected IntersectionRuneResolver () { SetGlyphs (); }
  629. public Rune? GetRuneForIntersects (IConsoleDriver? driver, IntersectionDefinition? [] intersects)
  630. {
  631. bool useRounded = intersects.Any (
  632. i => i?.Line.Length != 0
  633. && (
  634. i?.Line.Style == LineStyle.Rounded
  635. || i?.Line.Style
  636. == LineStyle.RoundedDashed
  637. || i?.Line.Style
  638. == LineStyle.RoundedDotted)
  639. );
  640. // Note that there aren't any glyphs for intersections of double lines with heavy lines
  641. bool doubleHorizontal = intersects.Any (
  642. l => l?.Line.Orientation == Orientation.Horizontal
  643. && l.Line.Style == LineStyle.Double
  644. );
  645. bool doubleVertical = intersects.Any (
  646. l => l?.Line.Orientation == Orientation.Vertical
  647. && l.Line.Style == LineStyle.Double
  648. );
  649. bool thickHorizontal = intersects.Any (
  650. l => l?.Line.Orientation == Orientation.Horizontal
  651. && (
  652. l.Line.Style == LineStyle.Heavy
  653. || l.Line.Style == LineStyle.HeavyDashed
  654. || l.Line.Style == LineStyle.HeavyDotted)
  655. );
  656. bool thickVertical = intersects.Any (
  657. l => l?.Line.Orientation == Orientation.Vertical
  658. && (
  659. l.Line.Style == LineStyle.Heavy
  660. || l.Line.Style == LineStyle.HeavyDashed
  661. || l.Line.Style == LineStyle.HeavyDotted)
  662. );
  663. if (doubleHorizontal)
  664. {
  665. return doubleVertical ? _doubleBoth : _doubleH;
  666. }
  667. if (doubleVertical)
  668. {
  669. return _doubleV;
  670. }
  671. if (thickHorizontal)
  672. {
  673. return thickVertical ? _thickBoth : _thickH;
  674. }
  675. if (thickVertical)
  676. {
  677. return _thickV;
  678. }
  679. return useRounded ? _round : _normal;
  680. }
  681. /// <summary>
  682. /// Sets the glyphs used. Call this method after construction and any time ConfigurationManager has updated the
  683. /// settings.
  684. /// </summary>
  685. public abstract void SetGlyphs ();
  686. }
  687. private class LeftTeeIntersectionRuneResolver : IntersectionRuneResolver
  688. {
  689. public override void SetGlyphs ()
  690. {
  691. _round = Glyphs.LeftTee;
  692. _doubleH = Glyphs.LeftTeeDblH;
  693. _doubleV = Glyphs.LeftTeeDblV;
  694. _doubleBoth = Glyphs.LeftTeeDbl;
  695. _thickH = Glyphs.LeftTeeHvH;
  696. _thickV = Glyphs.LeftTeeHvV;
  697. _thickBoth = Glyphs.LeftTeeHvDblH;
  698. _normal = Glyphs.LeftTee;
  699. }
  700. }
  701. private class LLIntersectionRuneResolver : IntersectionRuneResolver
  702. {
  703. public override void SetGlyphs ()
  704. {
  705. _round = Glyphs.LLCornerR;
  706. _doubleH = Glyphs.LLCornerSingleDbl;
  707. _doubleV = Glyphs.LLCornerDblSingle;
  708. _doubleBoth = Glyphs.LLCornerDbl;
  709. _thickH = Glyphs.LLCornerLtHv;
  710. _thickV = Glyphs.LLCornerHvLt;
  711. _thickBoth = Glyphs.LLCornerHv;
  712. _normal = Glyphs.LLCorner;
  713. }
  714. }
  715. private class LRIntersectionRuneResolver : IntersectionRuneResolver
  716. {
  717. public override void SetGlyphs ()
  718. {
  719. _round = Glyphs.LRCornerR;
  720. _doubleH = Glyphs.LRCornerSingleDbl;
  721. _doubleV = Glyphs.LRCornerDblSingle;
  722. _doubleBoth = Glyphs.LRCornerDbl;
  723. _thickH = Glyphs.LRCornerLtHv;
  724. _thickV = Glyphs.LRCornerHvLt;
  725. _thickBoth = Glyphs.LRCornerHv;
  726. _normal = Glyphs.LRCorner;
  727. }
  728. }
  729. private class RightTeeIntersectionRuneResolver : IntersectionRuneResolver
  730. {
  731. public override void SetGlyphs ()
  732. {
  733. _round = Glyphs.RightTee;
  734. _doubleH = Glyphs.RightTeeDblH;
  735. _doubleV = Glyphs.RightTeeDblV;
  736. _doubleBoth = Glyphs.RightTeeDbl;
  737. _thickH = Glyphs.RightTeeHvH;
  738. _thickV = Glyphs.RightTeeHvV;
  739. _thickBoth = Glyphs.RightTeeHvDblH;
  740. _normal = Glyphs.RightTee;
  741. }
  742. }
  743. private class TopTeeIntersectionRuneResolver : IntersectionRuneResolver
  744. {
  745. public override void SetGlyphs ()
  746. {
  747. _round = Glyphs.TopTee;
  748. _doubleH = Glyphs.TopTeeDblH;
  749. _doubleV = Glyphs.TopTeeDblV;
  750. _doubleBoth = Glyphs.TopTeeDbl;
  751. _thickH = Glyphs.TopTeeHvH;
  752. _thickV = Glyphs.TopTeeHvV;
  753. _thickBoth = Glyphs.TopTeeHvDblH;
  754. _normal = Glyphs.TopTee;
  755. }
  756. }
  757. private class ULIntersectionRuneResolver : IntersectionRuneResolver
  758. {
  759. public override void SetGlyphs ()
  760. {
  761. _round = Glyphs.ULCornerR;
  762. _doubleH = Glyphs.ULCornerSingleDbl;
  763. _doubleV = Glyphs.ULCornerDblSingle;
  764. _doubleBoth = Glyphs.ULCornerDbl;
  765. _thickH = Glyphs.ULCornerLtHv;
  766. _thickV = Glyphs.ULCornerHvLt;
  767. _thickBoth = Glyphs.ULCornerHv;
  768. _normal = Glyphs.ULCorner;
  769. }
  770. }
  771. private class URIntersectionRuneResolver : IntersectionRuneResolver
  772. {
  773. public override void SetGlyphs ()
  774. {
  775. _round = Glyphs.URCornerR;
  776. _doubleH = Glyphs.URCornerSingleDbl;
  777. _doubleV = Glyphs.URCornerDblSingle;
  778. _doubleBoth = Glyphs.URCornerDbl;
  779. _thickH = Glyphs.URCornerHvLt;
  780. _thickV = Glyphs.URCornerLtHv;
  781. _thickBoth = Glyphs.URCornerHv;
  782. _normal = Glyphs.URCorner;
  783. }
  784. }
  785. /// <inheritdoc/>
  786. public void Dispose ()
  787. {
  788. Applied -= ConfigurationManager_Applied;
  789. GC.SuppressFinalize (this);
  790. }
  791. }