Skip to content

Commit 3b4ac4f

Browse files
committed
Fix bug for text starting with ScriptInherited, fixes #336
1 parent e419d36 commit 3b4ac4f

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

text/text.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ type ScriptItem struct {
1313
Text string
1414
}
1515

16+
func (item *ScriptItem) String() string {
17+
return fmt.Sprintf("{%v %v %v}", item.Script, item.Level, item.Text)
18+
}
19+
1620
// ScriptItemizer divides the string in parts for each different script. Also separates on different embedding levels and unicode.ReplacementChar (replaced by object).
1721
func ScriptItemizer(runes []rune, embeddingLevels []int) []ScriptItem {
1822
if len(runes) == 0 {
@@ -27,8 +31,10 @@ func ScriptItemizer(runes []rune, embeddingLevels []int) []ScriptItem {
2731
if script == ScriptInherited {
2832
if r == '\u200C' || r == '\u200D' {
2933
script = ScriptCommon
34+
} else if level < len(scripts) {
35+
script = scripts[level] // take level from preceding base character
3036
} else {
31-
script = scripts[level] // take leven from preceding base character
37+
script = ScriptUnknown
3238
}
3339
}
3440
prevScript := scripts[len(scripts)-1]

text/text_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package text
2+
3+
import (
4+
"testing"
5+
6+
"github.com/tdewolff/test"
7+
)
8+
9+
func TestScriptItemizer(t *testing.T) {
10+
var tests = []struct {
11+
str string
12+
items []ScriptItem
13+
}{
14+
{"abc", []ScriptItem{{Latin, 0, "abc"}}},
15+
{"\u064bياعادلا", []ScriptItem{{Arabic, 1, "\u064bياعادلا"}}},
16+
}
17+
18+
for _, tt := range tests {
19+
t.Run(tt.str, func(t *testing.T) {
20+
runes := []rune(tt.str)
21+
embeddingLevels := EmbeddingLevels(runes)
22+
items := ScriptItemizer(runes, embeddingLevels)
23+
test.T(t, items, tt.items)
24+
})
25+
}
26+
}

0 commit comments

Comments
 (0)