From 02db9842ce6bf9386f9d0a877018577eefedd83a Mon Sep 17 00:00:00 2001 From: Adam Jack Date: Fri, 9 Jun 2023 20:40:39 -0600 Subject: [PATCH 1/2] Update splittext.go More careful insect the font for "character width" for each character (rune) and do not crash if the font does not have a width for the character. --- splittext.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/splittext.go b/splittext.go index 525f93b0..37b53df3 100644 --- a/splittext.go +++ b/splittext.go @@ -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) @@ -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 } From c24606be901c49c2f4ea889b751f93f85ec7f138 Mon Sep 17 00:00:00 2001 From: Adam Jack Date: Tue, 13 Jun 2023 20:56:43 -0600 Subject: [PATCH 2/2] Update go.mod Renaming --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 57f4693d..9cf19e03 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/phpdave11/gofpdf +module github.com/adamjack/gofpdf go 1.12