Skip to content

Commit 31c8f6c

Browse files
committed
Unicode lines are part of the text step.
1 parent cc2eeb7 commit 31c8f6c

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

‎src/canvas.go‎

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ var reservedRunes = map[rune]bool{
2525
'\\': true,
2626
')': true,
2727
'(': true,
28-
'╱': true,
29-
'╲': true,
30-
'╳': true,
3128
' ': true,
3229
}
3330

@@ -157,6 +154,8 @@ type Line struct {
157154
//dashed bool
158155
needsNudgingUp bool
159156
needsNudgingDown bool
157+
lonely bool
158+
orientation Orientation
160159

161160
state lineState
162161
}
@@ -265,13 +264,13 @@ func (c *Canvas) Lines() []Line {
265264

266265
lines = append(lines, c.linesFromIterator(
267266
diagUp,
268-
[]rune{'/', '╱', '╳'},
267+
[]rune{'/'},
269268
append([]rune{'o', '*', '<', '>', '^', 'v', '|'}, jointRunes...),
270269
)...)
271270

272271
lines = append(lines, c.linesFromIterator(
273272
diagDown,
274-
[]rune{'\\', '╲', '╳'},
273+
[]rune{'\\'},
275274
append([]rune{'o', '*', '<', '>', '^', 'v', '|'}, jointRunes...),
276275
)...)
277276

@@ -499,13 +498,42 @@ func (c *Canvas) isRoundedCorner(i Index) Orientation {
499498
return NONE
500499
}
501500

502-
// Text returns a slace of all text characters not belonging to part of the diagram.
501+
// Text returns a slice of all text characters not belonging to part of the diagram.
503502
// How these characters are identified is rather complicated.
504-
func (c *Canvas) Text() []Text {
505-
var text []Text
503+
func (c *Canvas) Text() []Drawable {
504+
var text []Drawable
505+
506+
newLine := func(i Index, o Orientation) Line {
507+
stop := i
508+
509+
switch o {
510+
case NE:
511+
stop = i.nEast()
512+
case SE:
513+
stop = i.sEast()
514+
}
515+
516+
return Line{
517+
start: i,
518+
stop: stop,
519+
lonely: true,
520+
orientation: o,
521+
}
522+
}
506523

507524
for i, r := range c.text {
508-
text = append(text, Text{start: i, contents: string(r)})
525+
switch r {
526+
// Weird unicode edge cases that markdeep handles. These get
527+
// substituted with lines.
528+
case '╱':
529+
text = append(text, newLine(i, NE))
530+
case '╲':
531+
text = append(text, newLine(i, SE))
532+
case '╳':
533+
text = append(text, newLine(i, NE), newLine(i, SE))
534+
default:
535+
text = append(text, Text{start: i, contents: string(r)})
536+
}
509537
}
510538

511539
return text
@@ -622,7 +650,6 @@ func (c *Canvas) hasLineAboveOrBelow(i Index) bool {
622650
case '|':
623651
return c.partOfVerticalLine(i) || c.partOfRoundedCorner(i)
624652
case '/', '\\':
625-
// TODO: unicode cases
626653
return c.partOfDiagonalLine(i)
627654
case '-':
628655
return c.partOfRoundedCorner(i)

‎src/svg.go‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func writeBytes(out *io.Writer, format string, args ...interface{}) {
6060
}
6161

6262
// Draw a straight line as an SVG path.
63-
func (l *Line) Draw(out io.Writer) {
63+
func (l Line) Draw(out io.Writer) {
6464

6565
start := l.start.asPixel()
6666
stop := l.stop.asPixel()
@@ -75,6 +75,25 @@ func (l *Line) Draw(out io.Writer) {
7575
// appropriate direction in order to meet up cleanly with the midline of
7676
// the cell next to it.
7777

78+
// A diagonal segment all by itself needs to be shifted slightly to line
79+
// up with _ baselines:
80+
// _
81+
// \_
82+
//
83+
if l.lonely {
84+
start.x -= 8
85+
stop.x -= 8
86+
87+
switch l.orientation {
88+
case NE:
89+
start.y += 8
90+
stop.y += 8
91+
case SE:
92+
start.y -= 8
93+
stop.y -= 8
94+
}
95+
}
96+
7897
if l.needsNudgingUp {
7998
start.y -= 8
8099
}
@@ -175,7 +194,7 @@ func (c *Circle) Draw(out io.Writer) {
175194
}
176195

177196
// Draw a single text character as an SVG text element.
178-
func (t *Text) Draw(out io.Writer) {
197+
func (t Text) Draw(out io.Writer) {
179198
p := t.start.asPixel()
180199
c := t.contents
181200

0 commit comments

Comments
 (0)