LineCanvas.cs 29 KB

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