-
Notifications
You must be signed in to change notification settings - Fork 1
/
text.go
257 lines (240 loc) · 5.78 KB
/
text.go
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package tableimage
import (
"regexp"
"strconv"
"unicode"
"github.com/golang/freetype/truetype"
"github.com/mattn/go-runewidth"
"golang.org/x/image/font"
)
var (
reText = regexp.MustCompile(`<text(\s+?(?P<attrs>.+?))?(\s+)?>(?P<txt>.+?)</text>`)
reAttr = regexp.MustCompile(`(?P<attr>\w+)=["|'](?P<value>#?\w+)["|']`)
)
// Text string with width
type Text struct {
Value string
Width int
Color string
BgColor string
Padding int
Pos [2]int
}
// TextFromText create Text from Text
func TextFromText(value string, txt Text, fontFace font.Face) Text {
txt.Value = value
txt.Width = int(stringWidth(value, fontFace)) + txt.Padding*2
return txt
}
// SameStyle check if two Text style is same
func (t Text) SameStyle(t2 Text) bool {
return t.Color == t2.Color && t.BgColor == t2.BgColor && t.Padding == t2.Padding
}
// Word text array
type Word []Text
// Width calculate word length
func (w Word) Width() int {
var l int
for _, t := range w {
l += t.Width
}
return l
}
func wrap(s string, w int, fontFace font.Face, ignoreInlineStyle bool) ([]Word, int) {
segments := extractTexts(s, ignoreInlineStyle)
words := separateWords(segments, fontFace)
if w > 0 {
words = wrapWords(words, w, fontFace)
}
var maxWidth int
for _, w := range words {
if w.Width() > maxWidth {
maxWidth = w.Width()
}
}
return words, maxWidth
}
// wrapWords wrap words to lines in w length
func wrapWords(words []Word, w int, fontFace font.Face) []Word {
var (
retWords []Word
word Word
)
for _, segs := range words {
for _, txt := range segs {
ww := int(stringWidth(txt.Value, fontFace))
if txt.Value == "\n" || word.Width()+ww > w {
retWords = append(retWords, word)
word = Word{}
if txt.Value != "\n" {
word = append(word, txt)
}
continue
}
if len(word) > 0 && word[len(word)-1].SameStyle(txt) {
lastWord := word[len(word)-1]
word[len(word)-1] = TextFromText(lastWord.Value+txt.Value, word[len(word)-1], fontFace)
} else {
word = append(word, txt)
}
}
}
if len(word) > 0 {
retWords = append(retWords, word)
}
return retWords
}
// separateWords seperate a string into words and not break word
func separateWords(segments []Text, fontFace font.Face) []Word {
var (
words []Word
wordTexts Word
)
for _, seg := range segments {
var segWord []rune
for _, r := range []rune(seg.Value) {
l := len(segWord)
cw := runewidth.RuneWidth(r)
segWordStr := string(segWord)
if cw == 0 {
if l > 0 {
wordTexts = append(wordTexts, TextFromText(segWordStr, seg, fontFace))
words = append(words, wordTexts)
}
wordTexts = Word{}
segWord = []rune{}
continue
} else if unicode.IsSpace(r) || unicode.IsPunct(r) || cw == 2 { // \n \t \s or double width add new word
if l > 0 {
wordTexts = append(wordTexts, TextFromText(segWordStr, seg, fontFace))
}
wordTexts = append(wordTexts, TextFromText(string(r), seg, fontFace))
words = append(words, wordTexts)
wordTexts = Word{}
segWord = []rune{}
continue
}
segWord = append(segWord, r)
}
if len(segWord) > 0 {
segWordStr := string(segWord)
wordTexts = append(wordTexts, TextFromText(segWordStr, seg, fontFace))
}
}
if len(wordTexts) > 0 {
words = append(words, wordTexts)
}
return words
}
func extractTexts(s string, ignoreInlineStyle bool) []Text {
txtL := len(s)
var segments []Text
matches := reText.FindAllStringSubmatchIndex(s, -1)
groupNames := reText.SubexpNames()
totalMatches := len(matches)
if totalMatches == 0 || ignoreInlineStyle {
return []Text{{
Value: s,
}}
}
for idx, locs := range matches {
if idx == 0 && locs[0] > 0 {
segments = append(segments, Text{
Pos: [2]int{0, locs[0]},
Value: s[0:locs[0]],
})
} else if len(segments) > 0 && locs[0] > segments[idx-1].Pos[1] {
lastTxt := segments[idx-1]
segments = append(segments, Text{
Pos: [2]int{lastTxt.Pos[1], locs[0]},
Value: s[lastTxt.Pos[1]:locs[0]],
})
}
text := extractText(s, groupNames, locs)
segments = append(segments, text)
if idx == totalMatches-1 && locs[1] < txtL {
segments = append(segments, Text{
Pos: [2]int{locs[1], txtL},
Value: s[locs[1]:txtL],
})
}
}
return segments
}
func extractText(s string, groupNames []string, locs []int) Text {
text := Text{
Pos: [2]int{locs[0], locs[1]},
}
for j, name := range groupNames {
if j == 0 || name == "" {
continue
}
content := s[locs[j*2]:locs[j*2+1]]
if name == "attrs" {
attrs := extractAttrs(content)
for k, v := range attrs {
switch k {
case "color":
text.Color = v
case "bgcolor":
text.BgColor = v
case "padding":
text.Padding, _ = strconv.Atoi(v)
}
}
} else if name == "txt" {
text.Value = content
}
}
return text
}
// extractAttrs extract text tag attributes
func extractAttrs(s string) map[string]string {
matches := reAttr.FindAllStringSubmatch(s, -1)
groupNames := reAttr.SubexpNames()
ret := make(map[string]string, len(matches))
for _, m := range matches {
var (
k string
v string
)
for j, name := range groupNames {
if j == 0 || name == "" {
continue
}
switch name {
case "attr":
k = m[j]
case "value":
v = m[j]
}
}
if k != "" && v != "" {
ret[k] = v
}
}
return ret
}
// MeasureString returns the rendered width and height of the specified text
// given the current font face.
func stringWidth(s string, fontFace font.Face) float64 {
if fontFace == nil {
return 0
}
d := &font.Drawer{
Face: fontFace,
}
a := d.MeasureString(s)
return float64(a >> 6)
}
func stringHeight(fontSize float64, lineHeight float64) int {
return int(fontSize * lineHeight)
}
func newFontFace(ft *truetype.Font, fontSize float64) font.Face {
if ft == nil {
return nil
}
return truetype.NewFace(ft, &truetype.Options{
Size: fontSize,
})
}