LineCanvas.cs 32 KB

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