Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/phpdave11/gofpdf
module github.com/adamjack/gofpdf

go 1.12

Expand Down
25 changes: 23 additions & 2 deletions splittext.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,33 @@ import (
"unicode"
)

const (
PlaceholderRune rune = '_'
)

// CharacterWidth: Character (rune) widths ...
func (f *Fpdf) CharacterWidth(c rune) int {

// Calculate width more safely ...
w := 0
ci := int(c)
if len(f.currentFont.Cw) > ci {
w = f.currentFont.Cw[ci]
} else if f.currentFont.utf8File != nil && len(f.currentFont.utf8File.CharWidths) > ci {
w = f.currentFont.utf8File.CharWidths[ci]
} else if c != PlaceholderRune {
// Fallback to the placeholder width ...
w = f.CharacterWidth(PlaceholderRune)
}

return w
}

// 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)
Expand All @@ -25,7 +46,7 @@ func (f *Fpdf) SplitText(txt string, w float64) (lines []string) {
l := 0
for i < nb {
c := s[i]
l += cw[c]
l += f.CharacterWidth(c)
if unicode.IsSpace(c) || isChinese(c) {
sep = i
}
Expand Down