diff options
author | Kurt <kurt.w.jung@gmail.com> | 2019-06-18 14:23:18 -0400 |
---|---|---|
committer | Kurt <kurt.w.jung@gmail.com> | 2019-06-18 14:23:18 -0400 |
commit | 9d03221773acee72ec2753772dc81d9df81ab63a (patch) | |
tree | 7cf43a418b667ddb05fcb17b3a0f1185fd23da4c | |
parent | 8060f8371088d63b5935a6eeb617328705387ace (diff) | |
parent | 31bdfeb6a974f0ac84d8847231d6b6f79e7d7ceb (diff) |
Merge branch 'nanjj-master'
-rw-r--r-- | fpdf_test.go | 48 | ||||
-rw-r--r-- | utf8fontfile.go | 13 |
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 +} |