diff options
author | Kurt <kurt.w.jung@gmail.com> | 2018-11-21 07:12:14 -0500 |
---|---|---|
committer | Kurt <kurt.w.jung@gmail.com> | 2018-11-21 07:12:14 -0500 |
commit | 3ec0a92eeadb35a4665f6f8c169674698723d6ce (patch) | |
tree | 74e29e13b2b758d8778bca11ace9a6bde73a8cf2 | |
parent | 17f0a23a8534e47957dba0091fba52b73af4061e (diff) | |
parent | 76dbd3f09366910f918201e7efb42b9da1aaffe4 (diff) |
Merge branch 'master' of https://github.com/jung-kurt/gofpdf
-rw-r--r-- | fpdf.go | 10 | ||||
-rw-r--r-- | fpdf_test.go | 38 |
2 files changed, 43 insertions, 5 deletions
@@ -2180,7 +2180,7 @@ func (f *Fpdf) MultiCell(w, h float64, txtStr, borderStr, alignStr string, fill if w == 0 { w = f.w - f.rMargin - f.x } - wmax := (w - 2*f.cMargin) * 1000 / f.fontSize + wmax := int(math.Ceil((w - 2*f.cMargin) * 1000 / f.fontSize)) s := strings.Replace(txtStr, "\r", "", -1) nb := len(s) // if nb > 0 && s[nb-1:nb] == "\n" { @@ -2214,8 +2214,8 @@ func (f *Fpdf) MultiCell(w, h float64, txtStr, borderStr, alignStr string, fill sep := -1 i := 0 j := 0 - l := 0.0 - ls := 0.0 + l := 0 + ls := 0 ns := 0 nl := 1 for i < nb { @@ -2244,7 +2244,7 @@ func (f *Fpdf) MultiCell(w, h float64, txtStr, borderStr, alignStr string, fill ls = l ns++ } - l += float64(cw[c]) + l += cw[c] if l > wmax { // Automatic line break if sep == -1 { @@ -2259,7 +2259,7 @@ func (f *Fpdf) MultiCell(w, h float64, txtStr, borderStr, alignStr string, fill } else { if alignStr == "J" { if ns > 1 { - f.ws = (wmax - ls) / 1000 * f.fontSize / float64(ns-1) + f.ws = float64((wmax-ls)/1000) * f.fontSize / float64(ns-1) } else { f.ws = 0 } diff --git a/fpdf_test.go b/fpdf_test.go index b3732e0..61dc6f9 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -100,6 +100,44 @@ func TestIssue0193(t *testing.T) { } +// TestIssue0209 addresses issue 209 +// make SplitLines and MultiCell split at the same place +func TestIssue0209SplitLinesEqualMultiCell(t *testing.T) { + var pdf *gofpdf.Fpdf + + pdf = gofpdf.New("P", "mm", "A4", "") + pdf.AddPage() + pdf.SetFont("Arial", "", 8) + // this sentence should not be splited + str := "Guochin Amandine" + lines := pdf.SplitLines([]byte(str), 26) + _, FontSize := pdf.GetFontSize() + y_start := pdf.GetY() + pdf.MultiCell(26, FontSize, str, "", "L", false) + y_end := pdf.GetY() + + if len(lines) != 1 { + t.Fatalf("expect SplitLines split in one line") + } + if int(y_end-y_start) != int(FontSize) { + t.Fatalf("expect MultiCell split in one line %.2f != %.2f", y_end-y_start, FontSize) + } + + // this sentence should be splited in two lines + str = "Guiochini Amandine" + lines = pdf.SplitLines([]byte(str), 26) + y_start = pdf.GetY() + pdf.MultiCell(26, FontSize, str, "", "L", false) + y_end = pdf.GetY() + + if len(lines) != 2 { + t.Fatalf("expect SplitLines split in two lines") + } + if int(y_end-y_start) != int(FontSize*2) { + t.Fatalf("expect MultiCell split in two lines %.2f != %.2f", y_end-y_start, FontSize*2) + } +} + // Test to make sure the footer is not call twice and SetFooterFuncLpi can work // without SetFooterFunc. func TestFooterFuncLpi(t *testing.T) { |