This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
package wpdf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func makeTestPNG(t *testing.T, w, h int) []byte {
|
||||
t.Helper()
|
||||
img := image.NewRGBA(image.Rect(0, 0, w, h))
|
||||
for y := range h {
|
||||
for x := range w {
|
||||
img.Set(x, y, color.RGBA{R: uint8(x), G: uint8(y), B: 128, A: 255})
|
||||
}
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
if err := png.Encode(&buf, img); err != nil {
|
||||
t.Fatalf("png encode: %v", err)
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func TestRegisterImageDetectsPNG(t *testing.T) {
|
||||
b := NewPDFBuilder(Portrait, SizeA4, false)
|
||||
b.AddPage()
|
||||
|
||||
bin := makeTestPNG(t, 16, 16)
|
||||
ref := b.RegisterImage(bin)
|
||||
|
||||
if ref == nil {
|
||||
t.Fatal("RegisterImage returned nil")
|
||||
}
|
||||
if !strings.HasPrefix(ref.Name, "fpdf_img_") {
|
||||
t.Errorf("default name not prefixed: %q", ref.Name)
|
||||
}
|
||||
if ref.Mime != "image/png" {
|
||||
t.Errorf("mime = %q, want image/png", ref.Mime)
|
||||
}
|
||||
if ref.Info == nil {
|
||||
t.Error("info is nil")
|
||||
}
|
||||
if !bytes.Equal(ref.Bin, bin) {
|
||||
t.Error("Bin not stored correctly")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterImageWithCustomName(t *testing.T) {
|
||||
b := NewPDFBuilder(Portrait, SizeA4, false)
|
||||
b.AddPage()
|
||||
|
||||
bin := makeTestPNG(t, 8, 8)
|
||||
ref := b.RegisterImage(bin, NewPDFImageRegisterOpt().Name("custom_image"))
|
||||
|
||||
if ref.Name != "custom_image" {
|
||||
t.Errorf("custom name not used: %q", ref.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterImageWithExplicitType(t *testing.T) {
|
||||
b := NewPDFBuilder(Portrait, SizeA4, false)
|
||||
b.AddPage()
|
||||
|
||||
bin := makeTestPNG(t, 8, 8)
|
||||
ref := b.RegisterImage(bin, NewPDFImageRegisterOpt().ImageType("PNG"))
|
||||
|
||||
if ref.Info == nil {
|
||||
t.Error("info is nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterImageLargeBuffer(t *testing.T) {
|
||||
b := NewPDFBuilder(Portrait, SizeA4, false)
|
||||
b.AddPage()
|
||||
|
||||
// Larger than 512 bytes to exercise that branch in detection.
|
||||
bin := makeTestPNG(t, 256, 256)
|
||||
if len(bin) <= 512 {
|
||||
t.Fatalf("test png unexpectedly small: %d bytes", len(bin))
|
||||
}
|
||||
ref := b.RegisterImage(bin)
|
||||
|
||||
if ref.Mime != "image/png" {
|
||||
t.Errorf("mime = %q, want image/png", ref.Mime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageDrawing(t *testing.T) {
|
||||
b := NewPDFBuilder(Portrait, SizeA4, false)
|
||||
b.AddPage()
|
||||
|
||||
bin := makeTestPNG(t, 16, 16)
|
||||
ref := b.RegisterImage(bin)
|
||||
|
||||
b.Image(ref, NewPDFImageOpt().X(10).Y(10).Width(20).Height(20))
|
||||
|
||||
out, err := b.Build()
|
||||
if err != nil {
|
||||
t.Fatalf("Build error: %v", err)
|
||||
}
|
||||
if !bytes.HasPrefix(out, []byte("%PDF-")) {
|
||||
t.Error("output not a PDF")
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageDrawingDefaults(t *testing.T) {
|
||||
b := NewPDFBuilder(Portrait, SizeA4, false)
|
||||
b.AddPage()
|
||||
|
||||
bin := makeTestPNG(t, 16, 16)
|
||||
ref := b.RegisterImage(bin)
|
||||
|
||||
b.Image(ref) // no opts: use info defaults
|
||||
|
||||
out, err := b.Build()
|
||||
if err != nil {
|
||||
t.Fatalf("Build error: %v", err)
|
||||
}
|
||||
if !bytes.HasPrefix(out, []byte("%PDF-")) {
|
||||
t.Error("output not a PDF")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user