summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fpdf.go11
-rw-r--r--fpdf_test.go31
2 files changed, 38 insertions, 4 deletions
diff --git a/fpdf.go b/fpdf.go
index f4d7bdc..08f0fb3 100644
--- a/fpdf.go
+++ b/fpdf.go
@@ -313,10 +313,13 @@ func (f *Fpdf) SetFontLocation(fontDirStr string) {
f.fontpath = fontDirStr
}
-// SetFontLoader sets a loader used to load font files (.json and .z) from
-// arbitrary locations. If a font loader has been specified, Fpdf first tries
-// to load files using the font loader. If the loading files Fpdf tries to
-// load the font from the configured fonts directory (see SetFontLocation).
+// SetFontLoader sets a loader used to read font files (.json and .z) from an
+// arbitrary source. If a font loader has been specified, it is used to load
+// the named font resources when AddFont() is called. If this operation fails,
+// an attempt is made to load the resources from the configured font directory
+// (see SetFontLocation()).
+//
+// See tutorial 29 for an example of this method.
func (f *Fpdf) SetFontLoader(loader FontLoader) {
f.fontLoader = loader
}
diff --git a/fpdf_test.go b/fpdf_test.go
index bb69569..3439f91 100644
--- a/fpdf_test.go
+++ b/fpdf_test.go
@@ -18,8 +18,10 @@ package gofpdf_test
import (
"bufio"
+ "bytes"
"fmt"
"github.com/jung-kurt/gofpdf"
+ "io"
"io/ioutil"
"math"
"net/http"
@@ -101,6 +103,19 @@ func textFile(fileStr string) string {
return filepath.Join(cnTextDir, fileStr)
}
+type fontResourceType struct {
+}
+
+func (f fontResourceType) Open(name string) (rdr io.Reader, err error) {
+ var buf []byte
+ buf, err = ioutil.ReadFile(fontFile(name))
+ if err == nil {
+ rdr = bytes.NewReader(buf)
+ fmt.Printf("Generalized font loader reading %s\n", name)
+ }
+ return
+}
+
// Convert 'ABCDEFG' to, for example, 'A,BCD,EFG'
func strDelimit(str string, sepstr string, sepcount int) string {
pos := len(str) - sepcount
@@ -1427,3 +1442,19 @@ func ExampleFpdf_tutorial28() {
// Successfully generated pdf/tutorial28.pdf
}
+
+// Non-standard font using generalized font loader
+func ExampleFpdf_tutorial29() {
+ var fr fontResourceType
+ pdf := gofpdf.New("P", "mm", "A4", cnFontDir)
+ pdf.SetFontLoader(fr)
+ pdf.AddFont("Calligrapher", "", "calligra.json")
+ pdf.AddPage()
+ pdf.SetFont("Calligrapher", "", 35)
+ pdf.Cell(0, 10, "Enjoy new fonts with FPDF!")
+ pdf.OutputAndClose(docWriter(pdf, 29))
+ // Output:
+ // Generalized font loader reading calligra.json
+ // Generalized font loader reading calligra.z
+ // Successfully generated pdf/tutorial29.pdf
+}