ttfparser_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2013-2015 Kurt Jung (Gmail: kurt.w.jung)
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. package gofpdf_test
  17. import (
  18. "bytes"
  19. "fmt"
  20. "github.com/jung-kurt/gofpdf"
  21. "github.com/jung-kurt/gofpdf/internal/example"
  22. )
  23. func ExampleTtfParse() {
  24. ttf, err := gofpdf.TtfParse(example.FontDir() + "/calligra.ttf")
  25. if err == nil {
  26. fmt.Printf("Postscript name: %s\n", ttf.PostScriptName)
  27. fmt.Printf("unitsPerEm: %8d\n", ttf.UnitsPerEm)
  28. fmt.Printf("Xmin: %8d\n", ttf.Xmin)
  29. fmt.Printf("Ymin: %8d\n", ttf.Ymin)
  30. fmt.Printf("Xmax: %8d\n", ttf.Xmax)
  31. fmt.Printf("Ymax: %8d\n", ttf.Ymax)
  32. } else {
  33. fmt.Printf("%s\n", err)
  34. }
  35. // Output:
  36. // Postscript name: CalligrapherRegular
  37. // unitsPerEm: 1000
  38. // Xmin: -173
  39. // Ymin: -234
  40. // Xmax: 1328
  41. // Ymax: 899
  42. }
  43. func hexStr(s string) string {
  44. var b bytes.Buffer
  45. b.WriteString("\"")
  46. for _, ch := range []byte(s) {
  47. b.WriteString(fmt.Sprintf("\\x%02x", ch))
  48. }
  49. b.WriteString("\":")
  50. return b.String()
  51. }
  52. func ExampleFpdf_GetStringWidth() {
  53. pdf := gofpdf.New("", "", "", example.FontDir())
  54. pdf.SetFont("Helvetica", "", 12)
  55. pdf.AddPage()
  56. for _, s := range []string{"Hello", "世界", "\xe7a va?"} {
  57. fmt.Printf("%-32s width %5.2f, bytes %2d, runes %2d\n",
  58. hexStr(s), pdf.GetStringWidth(s), len(s), len([]rune(s)))
  59. if pdf.Err() {
  60. fmt.Println(pdf.Error())
  61. }
  62. }
  63. pdf.Close()
  64. // Output:
  65. // "\x48\x65\x6c\x6c\x6f": width 9.64, bytes 5, runes 5
  66. // "\xe4\xb8\x96\xe7\x95\x8c": width 13.95, bytes 6, runes 2
  67. // "\xe7\x61\x20\x76\x61\x3f": width 12.47, bytes 6, runes 6
  68. }