diff options
author | DarkFreedman <misterdark@mail.ru> | 2019-10-04 13:25:04 +0300 |
---|---|---|
committer | DarkFreedman <misterdark@mail.ru> | 2019-10-04 13:25:04 +0300 |
commit | 6b0f0dee0dd42aebd05ef612bc8069361d06e487 (patch) | |
tree | 6ab44fb49bf3359e3803181dcd3ab9e1df6a65e4 /splittext.go | |
parent | 2e803db13067e702208818c79d70bd0ad1b13370 (diff) | |
parent | 291a9758a2b6dad177c43ee435a68dcd46959496 (diff) |
Merge remote-tracking branch 'upstream/master'
Update to latest version
Diffstat (limited to 'splittext.go')
-rw-r--r-- | splittext.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/splittext.go b/splittext.go new file mode 100644 index 0000000..525f93b --- /dev/null +++ b/splittext.go @@ -0,0 +1,53 @@ +package gofpdf + +import ( + "math" + // "strings" + "unicode" +) + +// SplitText splits UTF-8 encoded text into several lines using the current +// font. Each line has its length limited to a maximum width given by w. This +// function can be used to determine the total height of wrapped text for +// vertical placement purposes. +func (f *Fpdf) SplitText(txt string, w float64) (lines []string) { + cw := f.currentFont.Cw + wmax := int(math.Ceil((w - 2*f.cMargin) * 1000 / f.fontSize)) + s := []rune(txt) // Return slice of UTF-8 runes + nb := len(s) + for nb > 0 && s[nb-1] == '\n' { + nb-- + } + s = s[0:nb] + sep := -1 + i := 0 + j := 0 + l := 0 + for i < nb { + c := s[i] + l += cw[c] + if unicode.IsSpace(c) || isChinese(c) { + sep = i + } + if c == '\n' || l > wmax { + if sep == -1 { + if i == j { + i++ + } + sep = i + } else { + i = sep + 1 + } + lines = append(lines, string(s[j:sep])) + sep = -1 + j = i + l = 0 + } else { + i++ + } + } + if i != j { + lines = append(lines, string(s[j:i])) + } + return lines +} |