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