- SVG: strokes → SVG path elements with dashed/arrow support, coordinates scaled from 300 DPI to 72 DPI - JPG: rasterization at 300 DPI using Go image package, Bresenham line drawing with round pen circles - PDF: minimal PDF generation with raw operators, no external library - 6 tests: SVG output, dashed style, arrow heads, JPG magic bytes, PDF header, page size calculations Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
133 lines
2.6 KiB
Go
133 lines
2.6 KiB
Go
package render
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"math"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func makePointData(points ...float32) []byte {
|
|
data := make([]byte, len(points)*4)
|
|
for i, p := range points {
|
|
binary.LittleEndian.PutUint32(data[i*4:], math.Float32bits(p))
|
|
}
|
|
return data
|
|
}
|
|
|
|
func TestRenderSVG(t *testing.T) {
|
|
strokes := []Stroke{
|
|
{
|
|
PenSize: 4.49,
|
|
Color: -16777216, // 0xFF000000 (black)
|
|
Style: "plain",
|
|
PointData: makePointData(100, 200, 300, 400),
|
|
},
|
|
}
|
|
|
|
svg := RenderSVG("REGULAR", strokes)
|
|
|
|
if !strings.Contains(svg, "<svg") {
|
|
t.Fatal("expected SVG element")
|
|
}
|
|
if !strings.Contains(svg, "viewBox") {
|
|
t.Fatal("expected viewBox")
|
|
}
|
|
if !strings.Contains(svg, "<path") {
|
|
t.Fatal("expected path element")
|
|
}
|
|
if !strings.Contains(svg, `stroke="black"`) {
|
|
t.Fatal("expected black stroke")
|
|
}
|
|
}
|
|
|
|
func TestRenderSVGDashed(t *testing.T) {
|
|
strokes := []Stroke{
|
|
{
|
|
PenSize: 4.49,
|
|
Color: -16777216,
|
|
Style: "dashed",
|
|
PointData: makePointData(100, 200, 300, 400),
|
|
},
|
|
}
|
|
|
|
svg := RenderSVG("REGULAR", strokes)
|
|
if !strings.Contains(svg, "stroke-dasharray") {
|
|
t.Fatal("expected stroke-dasharray for dashed line")
|
|
}
|
|
}
|
|
|
|
func TestRenderSVGArrow(t *testing.T) {
|
|
strokes := []Stroke{
|
|
{
|
|
PenSize: 4.49,
|
|
Color: -16777216,
|
|
Style: "arrow",
|
|
PointData: makePointData(100, 200, 500, 200),
|
|
},
|
|
}
|
|
|
|
svg := RenderSVG("REGULAR", strokes)
|
|
if !strings.Contains(svg, "<line") {
|
|
t.Fatal("expected arrow head lines")
|
|
}
|
|
}
|
|
|
|
func TestRenderJPG(t *testing.T) {
|
|
strokes := []Stroke{
|
|
{
|
|
PenSize: 4.49,
|
|
Color: -16777216,
|
|
Style: "plain",
|
|
PointData: makePointData(100, 200, 300, 400),
|
|
},
|
|
}
|
|
|
|
data, err := RenderJPG("REGULAR", strokes, 90)
|
|
if err != nil {
|
|
t.Fatalf("render: %v", err)
|
|
}
|
|
if len(data) == 0 {
|
|
t.Fatal("expected non-empty JPG")
|
|
}
|
|
// Check JPEG magic bytes
|
|
if data[0] != 0xFF || data[1] != 0xD8 {
|
|
t.Fatal("expected JPEG magic bytes")
|
|
}
|
|
}
|
|
|
|
func TestRenderPDF(t *testing.T) {
|
|
pages := []Page{
|
|
{
|
|
PageNumber: 1,
|
|
Strokes: []Stroke{
|
|
{
|
|
PenSize: 4.49,
|
|
Color: -16777216,
|
|
Style: "plain",
|
|
PointData: makePointData(100, 200, 300, 400),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
data := RenderPDF("REGULAR", pages)
|
|
if len(data) == 0 {
|
|
t.Fatal("expected non-empty PDF")
|
|
}
|
|
if !strings.HasPrefix(string(data), "%PDF") {
|
|
t.Fatal("expected PDF header")
|
|
}
|
|
}
|
|
|
|
func TestPageSizePt(t *testing.T) {
|
|
w, h := PageSizePt("REGULAR")
|
|
if int(w) != 612 || int(h) != 792 {
|
|
t.Fatalf("REGULAR: got %v x %v, want 612 x 792", w, h)
|
|
}
|
|
w, h = PageSizePt("LARGE")
|
|
if int(w) != 792 || int(h) != 1224 {
|
|
t.Fatalf("LARGE: got %v x %v, want 792 x 1224", w, h)
|
|
}
|
|
}
|