LineCanvas.cs 28 KB

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