From 31bdfeb6a974f0ac84d8847231d6b6f79e7d7ceb Mon Sep 17 00:00:00 2001 From: Kurt Date: Tue, 18 Jun 2019 14:22:40 -0400 Subject: Demonstrate UTF8CutFont with test example --- fpdf_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'fpdf_test.go') 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 +} -- cgit v1.2.1-24-ge1ad