summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fpdf_test.go48
-rw-r--r--utf8fontfile.go13
2 files changed, 61 insertions, 0 deletions
diff --git a/fpdf_test.go b/fpdf_test.go
index 6b43af0..7ee0831 100644
--- a/fpdf_test.go
+++ b/fpdf_test.go
@@ -2561,3 +2561,51 @@ func ExampleFpdf_AddUTF8Font() {
// Output:
// Successfully generated pdf/Fpdf_AddUTF8Font.pdf
}
+
+// ExampleUTF8CutFont demonstrates how generate a TrueType font subset.
+func ExampleUTF8CutFont() {
+ var pdfFileStr, fullFontFileStr, subFontFileStr string
+ var subFont, fullFont []byte
+ var err error
+
+ pdfFileStr = example.Filename("Fpdf_UTF8CutFont")
+ fullFontFileStr = example.FontFile("calligra.ttf")
+ fullFont, err = ioutil.ReadFile(fullFontFileStr)
+ if err == nil {
+ subFontFileStr = "calligra_abcde.ttf"
+ subFont = gofpdf.UTF8CutFont(fullFont, "abcde")
+ err = ioutil.WriteFile(subFontFileStr, subFont, 0600)
+ if err == nil {
+ y := 24.0
+ pdf := gofpdf.New("P", "mm", "A4", "")
+ fontHt := 17.0
+ lineHt := pdf.PointConvert(fontHt)
+ write := func(format string, args ...interface{}) {
+ pdf.SetXY(24.0, y)
+ pdf.Cell(200.0, lineHt, fmt.Sprintf(format, args...))
+ y += lineHt
+ }
+ writeSize := func(fileStr string) {
+ var info os.FileInfo
+ var err error
+ info, err = os.Stat(fileStr)
+ if err == nil {
+ write("%6d: size of %s", info.Size(), fileStr)
+ }
+ }
+ pdf.AddPage()
+ pdf.AddUTF8Font("calligra", "", subFontFileStr)
+ pdf.SetFont("calligra", "", fontHt)
+ write("cabbed")
+ write("vwxyz")
+ pdf.SetFont("courier", "", fontHt)
+ writeSize(fullFontFileStr)
+ writeSize(subFontFileStr)
+ err = pdf.OutputFileAndClose(pdfFileStr)
+ os.Remove(subFontFileStr)
+ }
+ }
+ example.Summary(err, pdfFileStr)
+ // Output:
+ // Successfully generated pdf/Fpdf_UTF8CutFont.pdf
+}
diff --git a/utf8fontfile.go b/utf8fontfile.go
index 794bae2..4d90f28 100644
--- a/utf8fontfile.go
+++ b/utf8fontfile.go
@@ -1138,3 +1138,16 @@ func keySortArrayRangeMap(s map[int][]int) []int {
sort.Ints(keys)
return keys
}
+
+// UTF8CutFont is a utility function that generates a TrueType font composed
+// only of the runes included in cutset. The rune glyphs are copied from This
+// function is demonstrated in ExampleUTF8CutFont().
+func UTF8CutFont(inBuf []byte, cutset string) (outBuf []byte) {
+ f := newUTF8Font(&fileReader{readerPosition: 0, array: inBuf})
+ runes := map[int]int{}
+ for i, r := range cutset {
+ runes[i] = int(r)
+ }
+ outBuf = f.GenerateСutFont(runes)
+ return
+}