Skip to content

Commit a37be50

Browse files
committedFeb 10, 2016
Fix diagonal line check.
1 parent ca82041 commit a37be50

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed
 

‎src/canvas.go

+36-17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func contains(in []rune, r rune) bool {
3737
return false
3838
}
3939

40+
func isJoint(r rune) bool {
41+
return contains(jointRunes, r)
42+
}
43+
4044
// Canvas represents a 2D ASCII rectangle.
4145
type Canvas struct {
4246
Width int
@@ -334,13 +338,13 @@ func (c *Canvas) Triangles() []Triangle {
334338
case '^':
335339
o = N
336340
r := c.runeAt(start.north())
337-
if r == '-' || contains(jointRunes, r) {
341+
if r == '-' || isJoint(r) {
338342
needsNudging = true
339343
}
340344
case 'v':
341345
o = S
342346
r := c.runeAt(start.south())
343-
if r == '-' || contains(jointRunes, r) {
347+
if r == '-' || isJoint(r) {
344348
needsNudging = true
345349
}
346350
case '<':
@@ -509,7 +513,7 @@ func (c *Canvas) isText(i Index) bool {
509513
}
510514

511515
// This index refers to a rune not in our reserved set.
512-
if c.isDefinitelyText(i) {
516+
if c.isNotReserved(i) {
513517
return true
514518
}
515519

@@ -534,7 +538,7 @@ func (c *Canvas) isTextLeft(i Index, limit uint8) bool {
534538
}
535539
left := i.west()
536540

537-
return c.isDefinitelyText(left) || c.isTextLeft(left, limit-1)
541+
return c.isNotReserved(left) || c.isTextLeft(left, limit-1)
538542
}
539543

540544
func (c *Canvas) isTextRight(i Index, limit uint8) bool {
@@ -543,13 +547,13 @@ func (c *Canvas) isTextRight(i Index, limit uint8) bool {
543547
}
544548
right := i.east()
545549

546-
return c.isDefinitelyText(right) || c.isTextRight(right, limit-1)
550+
return c.isNotReserved(right) || c.isTextRight(right, limit-1)
547551
}
548552

549553
// Returns true if the character at this index is not reserved for diagrams.
550554
// Characters like "o" need more context (e.g., are other text characters
551555
// 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 {
553557
r := c.runeAt(i)
554558

555559
if r == ' ' {
@@ -561,19 +565,20 @@ func (c *Canvas) isDefinitelyText(i Index) bool {
561565
return !isReserved
562566
}
563567

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.
564571
func (c *Canvas) hasLineAboveOrBelow(i Index) bool {
565572
r := c.runeAt(i)
566573

567-
nEast := i.nEast()
568-
sWest := i.sWest()
569-
570574
switch r {
571575
case '*', 'o', '+':
572576
return c.partOfDiagonalLine(i) || c.partOfVerticalLine(i)
573577
case '|':
574578
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)
577582
case '-':
578583
return c.partOfRoundedCorner(i)
579584
case '(', ')':
@@ -589,13 +594,13 @@ func (c *Canvas) partOfVerticalLine(i Index) bool {
589594
north := c.runeAt(i.north())
590595
south := c.runeAt(i.south())
591596

592-
jointAboveMe := this == '|' && contains(jointRunes, north)
597+
jointAboveMe := this == '|' && isJoint(north)
593598

594599
if north == '|' || jointAboveMe {
595600
return true
596601
}
597602

598-
jointBelowMe := this == '|' && contains(jointRunes, south)
603+
jointBelowMe := this == '|' && isJoint(south)
599604

600605
if south == '|' || jointBelowMe {
601606
return true
@@ -610,10 +615,24 @@ func (c *Canvas) partOfHorizontalLine(i Index) bool {
610615
}
611616

612617
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+
}
617636
}
618637

619638
// For "-" and "|" characters returns true if they could be part of a rounded

0 commit comments

Comments
 (0)
Please sign in to comment.