LineCanvas.cs 29 KB

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