1package gofpdf
2
3import (
4	"math"
5	//	"strings"
6	"unicode"
7)
8
9// SplitText splits UTF-8 encoded text into several lines using the current
10// font. Each line has its length limited to a maximum width given by w. This
11// function can be used to determine the total height of wrapped text for
12// vertical placement purposes.
13func (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) // Return slice of UTF-8 runes
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