Added support for zero-width joiners. Resolves #161

This commit is contained in:
Oliver 2018-09-25 17:31:49 +02:00
parent fed6446838
commit 29458dad3e
2 changed files with 21 additions and 8 deletions

View File

@ -883,8 +883,12 @@ func (t *TextView) Draw(screen tcell.Screen) {
comb = make([]rune, len(runeSequence)-1)
copy(comb, runeSequence[1:])
}
for offset := 0; offset < runeSeqWidth; offset++ {
for offset := runeSeqWidth - 1; offset >= 0; offset-- {
if offset == 0 {
screen.SetContent(x+posX+offset, y+line-t.lineOffset, runeSequence[0], comb, style)
} else {
screen.SetContent(x+posX+offset, y+line-t.lineOffset, ' ', nil, style)
}
}
// Advance.
@ -926,14 +930,18 @@ func (t *TextView) Draw(screen tcell.Screen) {
// Determine the width of this rune.
chWidth := runewidth.RuneWidth(ch)
if chWidth == 0 {
// If this is not a modifier, we treat it as a space character.
if len(runeSequence) == 0 {
// If this is not a modifier, we treat it as a space character.
ch = ' '
chWidth = 1
} else {
runeSequence = append(runeSequence, ch)
continue
}
} else if runeSequence[len(runeSequence)-1] == '\u200d' {
// Keep collecting if the previous character was a zero-width joiner.
runeSequence = append(runeSequence, ch)
continue
}
// Skip to the right.

13
util.go
View File

@ -321,7 +321,7 @@ func printWithStyle(screen tcell.Screen, text string, x, y, maxWidth, align int,
colorPos, escapePos int
foregroundColor, backgroundColor, attributes string
)
runeSequence := make([]rune, 0, 10)
runeSequence := make([]rune, 0, 20)
runeSeqWidth := 0
flush := func() {
if len(runeSequence) == 0 {
@ -339,9 +339,13 @@ func printWithStyle(screen tcell.Screen, text string, x, y, maxWidth, align int,
comb = make([]rune, len(runeSequence)-1)
copy(comb, runeSequence[1:])
}
for offset := 0; offset < runeSeqWidth; offset++ {
for offset := runeSeqWidth - 1; offset >= 0; offset-- {
// To avoid undesired effects, we place the same character in all cells.
if offset == 0 {
screen.SetContent(finalX+offset, y, runeSequence[0], comb, finalStyle)
} else {
screen.SetContent(finalX+offset, y, ' ', nil, finalStyle)
}
}
// Advance and reset.
@ -384,8 +388,9 @@ func printWithStyle(screen tcell.Screen, text string, x, y, maxWidth, align int,
ch = ' '
chWidth = 1
}
} else {
// We have a character. Flush all previous runes.
} else if len(runeSequence) > 0 && runeSequence[len(runeSequence)-1] != '\u200d' {
// We have a character that doesn't follow a zero-width joiner. Flush all
// previous runes.
flush()
}
runeSequence = append(runeSequence, ch)