2
0

LineCanvas.cs 32 KB

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