@@ -37,6 +37,10 @@ func contains(in []rune, r rune) bool {
37
37
return false
38
38
}
39
39
40
+ func isJoint (r rune ) bool {
41
+ return contains (jointRunes , r )
42
+ }
43
+
40
44
// Canvas represents a 2D ASCII rectangle.
41
45
type Canvas struct {
42
46
Width int
@@ -334,13 +338,13 @@ func (c *Canvas) Triangles() []Triangle {
334
338
case '^' :
335
339
o = N
336
340
r := c .runeAt (start .north ())
337
- if r == '-' || contains ( jointRunes , r ) {
341
+ if r == '-' || isJoint ( r ) {
338
342
needsNudging = true
339
343
}
340
344
case 'v' :
341
345
o = S
342
346
r := c .runeAt (start .south ())
343
- if r == '-' || contains ( jointRunes , r ) {
347
+ if r == '-' || isJoint ( r ) {
344
348
needsNudging = true
345
349
}
346
350
case '<' :
@@ -509,7 +513,7 @@ func (c *Canvas) isText(i Index) bool {
509
513
}
510
514
511
515
// This index refers to a rune not in our reserved set.
512
- if c .isDefinitelyText (i ) {
516
+ if c .isNotReserved (i ) {
513
517
return true
514
518
}
515
519
@@ -534,7 +538,7 @@ func (c *Canvas) isTextLeft(i Index, limit uint8) bool {
534
538
}
535
539
left := i .west ()
536
540
537
- return c .isDefinitelyText (left ) || c .isTextLeft (left , limit - 1 )
541
+ return c .isNotReserved (left ) || c .isTextLeft (left , limit - 1 )
538
542
}
539
543
540
544
func (c * Canvas ) isTextRight (i Index , limit uint8 ) bool {
@@ -543,13 +547,13 @@ func (c *Canvas) isTextRight(i Index, limit uint8) bool {
543
547
}
544
548
right := i .east ()
545
549
546
- return c .isDefinitelyText (right ) || c .isTextRight (right , limit - 1 )
550
+ return c .isNotReserved (right ) || c .isTextRight (right , limit - 1 )
547
551
}
548
552
549
553
// Returns true if the character at this index is not reserved for diagrams.
550
554
// Characters like "o" need more context (e.g., are other text characters
551
555
// nearby) to determine whether they're part of a diagram.
552
- func (c * Canvas ) isDefinitelyText (i Index ) bool {
556
+ func (c * Canvas ) isNotReserved (i Index ) bool {
553
557
r := c .runeAt (i )
554
558
555
559
if r == ' ' {
@@ -561,19 +565,20 @@ func (c *Canvas) isDefinitelyText(i Index) bool {
561
565
return ! isReserved
562
566
}
563
567
568
+ // Returns true if it looks like this character belongs to anything besides a
569
+ // horizontal line. This is the context we use to determine if a reserved
570
+ // character is text or not.
564
571
func (c * Canvas ) hasLineAboveOrBelow (i Index ) bool {
565
572
r := c .runeAt (i )
566
573
567
- nEast := i .nEast ()
568
- sWest := i .sWest ()
569
-
570
574
switch r {
571
575
case '*' , 'o' , '+' :
572
576
return c .partOfDiagonalLine (i ) || c .partOfVerticalLine (i )
573
577
case '|' :
574
578
return c .partOfVerticalLine (i ) || c .partOfRoundedCorner (i )
575
- case '/' :
576
- return c .partOfDiagonalLine (i ) || contains (jointRunes , c .runeAt (nEast )) || contains (jointRunes , c .runeAt (sWest ))
579
+ case '/' , '\\' :
580
+ // TODO: unicode cases
581
+ return c .partOfDiagonalLine (i )
577
582
case '-' :
578
583
return c .partOfRoundedCorner (i )
579
584
case '(' , ')' :
@@ -589,13 +594,13 @@ func (c *Canvas) partOfVerticalLine(i Index) bool {
589
594
north := c .runeAt (i .north ())
590
595
south := c .runeAt (i .south ())
591
596
592
- jointAboveMe := this == '|' && contains ( jointRunes , north )
597
+ jointAboveMe := this == '|' && isJoint ( north )
593
598
594
599
if north == '|' || jointAboveMe {
595
600
return true
596
601
}
597
602
598
- jointBelowMe := this == '|' && contains ( jointRunes , south )
603
+ jointBelowMe := this == '|' && isJoint ( south )
599
604
600
605
if south == '|' || jointBelowMe {
601
606
return true
@@ -610,10 +615,24 @@ func (c *Canvas) partOfHorizontalLine(i Index) bool {
610
615
}
611
616
612
617
func (c * Canvas ) partOfDiagonalLine (i Index ) bool {
613
- return (c .runeAt (i .nWest ()) == '\\' ||
614
- c .runeAt (i .sEast ()) == '\\' ||
615
- c .runeAt (i .nEast ()) == '/' ||
616
- c .runeAt (i .sWest ()) == '/' )
618
+ r := c .runeAt (i )
619
+
620
+ nWest := c .runeAt (i .nWest ())
621
+ sEast := c .runeAt (i .sEast ())
622
+ nEast := c .runeAt (i .nEast ())
623
+ sWest := c .runeAt (i .sWest ())
624
+
625
+ switch r {
626
+ // Diagonal segments can be connected to joint or other segments.
627
+ case '/' :
628
+ return nEast == r || sWest == r || isJoint (nEast ) || isJoint (sWest )
629
+ case '\\' :
630
+ return nWest == r || sEast == r || isJoint (nWest ) || isJoint (sEast )
631
+
632
+ // For everything else just check if we have segments next to us.
633
+ default :
634
+ return nWest == '\\' || nEast == '/' || sWest == '/' || sEast == '\\'
635
+ }
617
636
}
618
637
619
638
// For "-" and "|" characters returns true if they could be part of a rounded
0 commit comments