-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathu_line.go
More file actions
204 lines (177 loc) · 5.4 KB
/
u_line.go
File metadata and controls
204 lines (177 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package utf8
import (
"io"
"github.com/blampe/goat/svg"
)
// Principle: Treat the two axes completely independently.
// Therefore, input "┼─┬" produces three line structs.
//
type line struct { // XX take local
Started bool // XX could be local variable of constructor loop?
// These point to 'segment' characters only in the case of "bare end" --
// more commonly they point to adjoining junction characters.
Start, Stop svg.XyIndex
// Always one of the compass points O_E, O_S.
Orientation svg.Orientation
}
func (c *Canvas) SetStart(l *line, i svg.XyIndex) {
if l.Started {
panic("Already started")
}
l.Start = i
l.Started = true
}
func (c *Canvas) SetStop(l *line, i svg.XyIndex) {
if ! l.Started {
panic("not started")
}
l.Stop = i
}
func reverse(o svg.Orientation) svg.Orientation {
switch o {
case svg.O_E:
return svg.O_W
case svg.O_S:
return svg.O_N
}
panic("unexpected svg.Orientation")
}
// Recall that spatial precision of output line{} is whole character cells.
//
// In the baseline case, an SVG line is to be drawn from the center of line.Start to center of line.End .
// Adjustments to abut well with neighbors happen later.
func (c *Canvas) getlines(
ci svg.CanvasIterator, // the order that the loop below traverses cells on the canvas.
minor_axis svg.Orientation, // either O_E or O_S
) (lines []line) {
reverse := reverse(minor_axis) // either O_W or O_N
// Write 'currentLine' onto the output.
// line may be of apparently zero-length i.e. contained within a single cell.
outputLine := func(l line) line {
lines = append(lines, l)
// start a new one.
// XX Writing the orientation into the line struct is
// redundant (now, with separated slices).
return line{Orientation: minor_axis}
}
currentline := line{Orientation: minor_axis}
for idx := range ci(c.Width+1, c.Height+1) {
r := c.RuneAt(idx)
if !currentline.Started {
if connects[reverse].Contains(r) {
// Half-cell-long segment
c.SetStart(¤tline, idx)
c.SetStop(¤tline, idx)
continue
}
if connects[minor_axis].Contains(r) {
c.SetStart(¤tline, idx)
c.SetStop(¤tline, idx)
}
continue
}
if connects[minor_axis].Contains(r) {
// Keep the line going and extend it by one cell.
c.SetStop(¤tline, idx)
} else {
if connects[reverse].Contains(r) {
// Terminate the line at a BOX T-intersection, or a triangle
c.SetStop(¤tline, idx)
} else {
// terminate at a dead end
// Stop is as set in previous iteration
}
currentline = outputLine(currentline)
}
}
return
}
// Draw a straight line as an SVG path.
//
// Cases:
// 1. Unterminated lines composed of '─' or '│' faithfully -- extend to
// limit of end cells, just as for interior '─' or '│'.
//
// 2. If a terminal circle or square, possibly open, lies at either end,
// extend so as to abut there.
//
// 3. Isolated joints e.g. '┬' or '┼': Decompose into horizontal and vertical,
// each independent of the other.
func (c *Canvas) DrawLine(l line, out io.Writer) {
startPix := c.startingPixel(l)
stopPix := c.stoppingPixel(l)
if startPix.X == stopPix.X && startPix.Y == stopPix.Y {
return
}
svg.WritePolyline(out, startPix, stopPix)
}
func (c *Canvas) startingPixel(l line) svg.Pixel {
// initial values of these are at centers of cells -- possibly adjusted later
startPix := l.Start.AsPixel()
startRune := c.RuneAt(l.Start)
switch l.Orientation {
case svg.O_E:
if startRune == '╭' || startRune == '╰' {
startPix.X += cornerRadius
} else if connects[reverse(l.Orientation)].Contains(startRune) {
westRune := c.RuneAt(l.Start.West())
startTriangleBase := leftArrowheadRunes.Contains(westRune)
startPix.X -= W/2
if startTriangleBase {
startPix.X -= W/4
}
}
case svg.O_S:
if startRune == '╭' || startRune == '╮' {
startPix.Y += cornerRadius
} else if connects[reverse(l.Orientation)].Contains(startRune) {
// If either end abuts a circle, extend drawing to the edge of the circle,
// rather extending as usual to center of the cell.
northRune := c.RuneAt(l.Start.North())
startTriangleBase := northRune == '▲'
startPix.Y -= H/2
if isDot(northRune) {
startPix.Y -= H/4
} else if startTriangleBase {
startPix.Y -= H/1
}
}
}
return startPix
}
func (c *Canvas) stoppingPixel(l line) svg.Pixel {
// initial values of these are at centers of cells -- possibly adjusted later
stopPix := l.Stop.AsPixel()
stopRune := c.RuneAt(l.Stop)
switch l.Orientation {
case svg.O_E:
if stopRune == '╮' || stopRune == '╯' {
stopPix.X -= cornerRadius
} else if connects[l.Orientation].Contains(stopRune) {
eastRune := c.RuneAt(l.Stop.East())
stopTriangleBase := rightArrowheadRunes.Contains(eastRune)
// extend to edge of cell
stopPix.X += W/2
if stopTriangleBase {
stopPix.X += W/4
}
}
case svg.O_S:
if stopRune == '╯' || stopRune == '╰' {
stopPix.Y -= cornerRadius
} else if connects[l.Orientation].Contains(stopRune) {
// If either end abuts a circle, extend drawing to the edge of the circle,
// rather extending as usual to center of the cell.
southRune := c.RuneAt(l.Stop.South())
stopTriangleBase := southRune == '▼'
// draw from center to "forward" edge of cell
stopPix.Y += H/2
if isDot(southRune) {
stopPix.Y += H/4
} else if stopTriangleBase {
stopPix.Y += H/1
}
}
}
return stopPix
}