makefont.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/jung-kurt/gofpdf"
  6. "os"
  7. )
  8. func errPrintf(fmtStr string, args ...interface{}) {
  9. fmt.Fprintf(os.Stderr, fmtStr, args...)
  10. }
  11. func showHelp() {
  12. errPrintf("Usage: %s [options] font_file [font_file...]\n", os.Args[0])
  13. flag.PrintDefaults()
  14. fmt.Fprintln(os.Stderr, "\n"+
  15. "font_file is the name of the TrueType file (extension .ttf), OpenType file\n"+
  16. "(extension .otf) or binary Type1 file (extension .pfb) from which to\n"+
  17. "generate a definition file. If an OpenType file is specified, it must be one\n"+
  18. "that is based on TrueType outlines, not PostScript outlines; this cannot be\n"+
  19. "determined from the file extension alone. If a Type1 file is specified, a\n"+
  20. "metric file with the same pathname except with the extension .afm must be\n"+
  21. "present.")
  22. errPrintf("\nExample: %s --embed --enc=../font/cp1252.map --dst=../font calligra.ttf /opt/font/symbol.pfb\n", os.Args[0])
  23. }
  24. func tutorialSummary(f *gofpdf.Fpdf, fileStr string) {
  25. if f.Ok() {
  26. fl, err := os.Create(fileStr)
  27. defer fl.Close()
  28. if err == nil {
  29. f.Output(fl)
  30. } else {
  31. f.SetError(err)
  32. }
  33. }
  34. if f.Ok() {
  35. fmt.Printf("Successfully generated %s\n", fileStr)
  36. } else {
  37. errPrintf("%s\n", f.Error())
  38. }
  39. }
  40. func main() {
  41. var dstDirStr, encodingFileStr string
  42. var err error
  43. var help, embed bool
  44. flag.StringVar(&dstDirStr, "dst", ".", "directory for output files (*.z, *.json)")
  45. flag.StringVar(&encodingFileStr, "enc", "cp1252.map", "code page file")
  46. flag.BoolVar(&embed, "embed", false, "embed font into PDF")
  47. flag.BoolVar(&help, "help", false, "command line usage")
  48. flag.Parse()
  49. if help {
  50. showHelp()
  51. } else {
  52. args := flag.Args()
  53. if len(args) > 0 {
  54. for _, fileStr := range args {
  55. err = gofpdf.MakeFont(fileStr, encodingFileStr, dstDirStr, os.Stderr, embed)
  56. if err != nil {
  57. errPrintf("%s\n", err)
  58. }
  59. // errPrintf("Font file [%s], Encoding file [%s], Embed [%v]\n", fileStr, encodingFileStr, embed)
  60. }
  61. } else {
  62. errPrintf("At least one Type1 or TrueType font must be specified\n")
  63. showHelp()
  64. }
  65. }
  66. }