LineCanvas.cs 28 KB

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