...
1 package gofpdf
2
3 import (
4 "math"
5
6 "unicode"
7 )
8
9
10
11
12
13 func (f *Fpdf) SplitText(txt string, w float64) (lines []string) {
14 cw := f.currentFont.Cw
15 wmax := int(math.Ceil((w - 2*f.cMargin) * 1000 / f.fontSize))
16 s := []rune(txt)
17 nb := len(s)
18 for nb > 0 && s[nb-1] == '\n' {
19 nb--
20 }
21 s = s[0:nb]
22 sep := -1
23 i := 0
24 j := 0
25 l := 0
26 for i < nb {
27 c := s[i]
28 l += cw[c]
29 if unicode.IsSpace(c) || isChinese(c) {
30 sep = i
31 }
32 if c == '\n' || l > wmax {
33 if sep == -1 {
34 if i == j {
35 i++
36 }
37 sep = i
38 } else {
39 i = sep + 1
40 }
41 lines = append(lines, string(s[j:sep]))
42 sep = -1
43 j = i
44 l = 0
45 } else {
46 i++
47 }
48 }
49 if i != j {
50 lines = append(lines, string(s[j:i]))
51 }
52 return lines
53 }
54
View as plain text