LineCanvas.cs 32 KB

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