builder_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package pdfrender
  2. import (
  3. "bytes"
  4. "fmt"
  5. "image"
  6. "image/color"
  7. "testing"
  8. )
  9. /*
  10. Test helpers: a tiny PDF writer for hand made fixtures, and pixel checks.
  11. */
  12. type pdfBuilder struct {
  13. objs [][]byte
  14. //Extra trailer entries
  15. trailer string
  16. }
  17. // add stores an object body and returns its number
  18. func (b *pdfBuilder) add(body string) int {
  19. b.objs = append(b.objs, []byte(body))
  20. return len(b.objs)
  21. }
  22. // reserve allocates an object number filled in later with set
  23. func (b *pdfBuilder) reserve() int {
  24. b.objs = append(b.objs, nil)
  25. return len(b.objs)
  26. }
  27. func (b *pdfBuilder) set(num int, body string) {
  28. b.objs[num-1] = []byte(body)
  29. }
  30. // stream builds a stream object body
  31. func streamBody(dict string, data []byte) string {
  32. return fmt.Sprintf("<< %s /Length %d >>\nstream\n%s\nendstream", dict, len(data), data)
  33. }
  34. func (b *pdfBuilder) addStream(dict string, data []byte) int {
  35. return b.add(streamBody(dict, data))
  36. }
  37. // bytes writes the file with a classic xref table
  38. func (b *pdfBuilder) bytes(root int) []byte {
  39. var buf bytes.Buffer
  40. buf.WriteString("%PDF-1.7\n%\xe2\xe3\xcf\xd3\n")
  41. offsets := make([]int, len(b.objs))
  42. for i, o := range b.objs {
  43. offsets[i] = buf.Len()
  44. fmt.Fprintf(&buf, "%d 0 obj\n%s\nendobj\n", i+1, o)
  45. }
  46. xref := buf.Len()
  47. fmt.Fprintf(&buf, "xref\n0 %d\n0000000000 65535 f \n", len(b.objs)+1)
  48. for _, off := range offsets {
  49. fmt.Fprintf(&buf, "%010d 00000 n \n", off)
  50. }
  51. fmt.Fprintf(&buf, "trailer\n<< /Size %d /Root %d 0 R %s >>\nstartxref\n%d\n%%%%EOF\n", len(b.objs)+1, root, b.trailer, xref)
  52. return buf.Bytes()
  53. }
  54. // onePage builds a single page document of w x h points
  55. func onePage(w, h int, resources string, content string, extra func(b *pdfBuilder) string) []byte {
  56. b := &pdfBuilder{}
  57. pagesNum := b.reserve()
  58. contentNum := b.addStream("", []byte(content))
  59. //Objects 1-3 are fixed, so fixtures can refer to their own from 4 on
  60. pageNum := b.reserve()
  61. pageExtra := ""
  62. if extra != nil {
  63. pageExtra = extra(b)
  64. }
  65. b.set(pageNum, fmt.Sprintf("<< /Type /Page /Parent %d 0 R /MediaBox [0 0 %d %d] /Resources << %s >> /Contents %d 0 R %s >>", pagesNum, w, h, resources, contentNum, pageExtra))
  66. b.set(pagesNum, fmt.Sprintf("<< /Type /Pages /Kids [%d 0 R] /Count 1 >>", pageNum))
  67. root := b.add(fmt.Sprintf("<< /Type /Catalog /Pages %d 0 R >>", pagesNum))
  68. return b.bytes(root)
  69. }
  70. // renderBytes renders page 0 at the given scale
  71. func renderBytes(t *testing.T, data []byte, scale float64) *image.RGBA {
  72. t.Helper()
  73. doc, err := Open(bytes.NewReader(data), int64(len(data)))
  74. if err != nil {
  75. t.Fatalf("Open: %v", err)
  76. }
  77. img, err := doc.Render(0, Options{Scale: scale})
  78. if err != nil {
  79. t.Fatalf("Render: %v", err)
  80. }
  81. return img
  82. }
  83. // at reads a pixel in PDF coordinates (origin bottom left) of a page of
  84. // height h rendered at scale 1
  85. func pixelAt(img *image.RGBA, x, y, h int) color.RGBA {
  86. return img.RGBAAt(x, h-1-y)
  87. }
  88. func near(a, b uint8, tol int) bool {
  89. d := int(a) - int(b)
  90. return d >= -tol && d <= tol
  91. }
  92. func assertColor(t *testing.T, img *image.RGBA, x, y, h int, want color.RGBA, tol int, what string) {
  93. t.Helper()
  94. got := pixelAt(img, x, y, h)
  95. if !near(got.R, want.R, tol) || !near(got.G, want.G, tol) || !near(got.B, want.B, tol) {
  96. t.Errorf("%s: pixel (%d,%d) = %v, want %v", what, x, y, got, want)
  97. }
  98. }
  99. // darkCount counts pixels darker than mid grey in a device rectangle
  100. func darkCount(img *image.RGBA, r image.Rectangle) int {
  101. n := 0
  102. r = r.Intersect(img.Bounds())
  103. for y := r.Min.Y; y < r.Max.Y; y++ {
  104. for x := r.Min.X; x < r.Max.X; x++ {
  105. c := img.RGBAAt(x, y)
  106. if int(c.R)+int(c.G)+int(c.B) < 3*128 {
  107. n++
  108. }
  109. }
  110. }
  111. return n
  112. }
  113. var (
  114. white = color.RGBA{255, 255, 255, 255}
  115. black = color.RGBA{0, 0, 0, 255}
  116. red = color.RGBA{255, 0, 0, 255}
  117. green = color.RGBA{0, 255, 0, 255}
  118. blue = color.RGBA{0, 0, 255, 255}
  119. )