summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKurt <kurt.w.jung@gmail.com>2017-06-12 10:42:40 -0400
committerKurt <kurt.w.jung@gmail.com>2017-06-12 10:42:40 -0400
commit67dbdc6f213695af48d5fce5ffcfeb14d0b685bf (patch)
treebf986e58c908f57f371aad6d9121d2e457ce33a6
parented00acdb054d3e26b9a2ffdb09fdd94585bab65d (diff)
Trigger an error when attempting to render text if font has not been set
-rw-r--r--fpdf.go12
-rw-r--r--fpdf_test.go23
2 files changed, 34 insertions, 1 deletions
diff --git a/fpdf.go b/fpdf.go
index 78a7c65..c862364 100644
--- a/fpdf.go
+++ b/fpdf.go
@@ -1793,6 +1793,9 @@ func (f *Fpdf) SetAcceptPageBreakFunc(fnc func() bool) {
// current position moves to the right or to the next line. It is possible to
// put a link on the text.
//
+// An error will be returned if a call to SetFont() has not already taken
+// place before this method is called.
+//
// If automatic page breaking is enabled and the cell goes beyond the limit, a
// page break is done before outputting.
//
@@ -1823,11 +1826,18 @@ func (f *Fpdf) SetAcceptPageBreakFunc(fnc func() bool) {
//
// linkStr is a target URL or empty for no external link. A non--zero value for
// link takes precedence over linkStr.
-func (f *Fpdf) CellFormat(w, h float64, txtStr string, borderStr string, ln int, alignStr string, fill bool, link int, linkStr string) {
+func (f *Fpdf) CellFormat(w, h float64, txtStr string, borderStr string, ln int,
+ alignStr string, fill bool, link int, linkStr string) {
// dbg("CellFormat. h = %.2f, borderStr = %s", h, borderStr)
if f.err != nil {
return
}
+
+ if f.currentFont.Name == "" {
+ f.err = fmt.Errorf("font has not been set; unable to render text")
+ return
+ }
+
borderStr = strings.ToUpper(borderStr)
k := f.k
if f.y+h > f.pageBreakTrigger && !f.inHeader && !f.inFooter && f.acceptPageBreak() {
diff --git a/fpdf_test.go b/fpdf_test.go
index 5781ad6..7da45aa 100644
--- a/fpdf_test.go
+++ b/fpdf_test.go
@@ -28,6 +28,7 @@ import (
"path/filepath"
"strconv"
"strings"
+ "testing"
"github.com/jung-kurt/gofpdf"
"github.com/jung-kurt/gofpdf/internal/example"
@@ -55,6 +56,28 @@ func cleanup() {
})
}
+// TestIssue0116 addresses issue 116 in which library silently fails after
+// calling CellFormat when no font has been set.
+func TestIssue0116(t *testing.T) {
+ var pdf *gofpdf.Fpdf
+
+ pdf = gofpdf.New("P", "mm", "A4", "")
+ pdf.AddPage()
+ pdf.SetFont("Arial", "B", 16)
+ pdf.Cell(40, 10, "OK")
+ if pdf.Error() != nil {
+ t.Fatalf("not expecting error when rendering text")
+ }
+
+ pdf = gofpdf.New("P", "mm", "A4", "")
+ pdf.AddPage()
+ pdf.Cell(40, 10, "Not OK") // Font not set
+ if pdf.Error() == nil {
+ t.Fatalf("expecting error when rendering text without having set font")
+ }
+
+}
+
type fontResourceType struct {
}