Added the possibilty to not only make quadratic items

This commit is contained in:
2026-05-26 17:35:50 +02:00
parent be58c5941d
commit bca4fcc936
15 changed files with 371 additions and 134 deletions
+6 -7
View File
@@ -13,12 +13,11 @@ const renderDPI = 300
// BuildCompositeSVG assembles a plate-sized SVG by embedding each item as a PNG
// rendered by rsvg-convert (identical to the standalone item file).
func BuildCompositeSVG(plate model.Plate, spec model.ItemSpec, itemSVG []byte, preview model.LayoutPreview) ([]byte, error) {
itemW, itemH, err := svgViewportMM(itemSVG)
if err != nil {
return nil, err
}
if itemW <= 0 || itemH <= 0 {
itemW, itemH = spec.SizeMM, spec.SizeMM
canvasW := preview.CanvasWidthMM
canvasH := preview.CanvasHeightMM
if canvasW <= 0 || canvasH <= 0 {
canvasW = spec.CanvasWidthMM()
canvasH = spec.CanvasHeightMM()
}
itemPNG, err := svgToPNGViaRsvg(itemSVG, renderDPI)
@@ -41,7 +40,7 @@ func BuildCompositeSVG(plate model.Plate, spec model.ItemSpec, itemSVG []byte, p
for _, pos := range preview.Positions {
fmt.Fprintf(&b,
`<image x="%.4f" y="%.4f" width="%.4f" height="%.4f" href="data:image/png;base64,%s"/>`,
pos.XMM, pos.YMM, itemW, itemH, itemB64,
pos.XMM, pos.YMM, canvasW, canvasH, itemB64,
)
}
+12 -4
View File
@@ -2,6 +2,7 @@ package platepdf
import (
"bytes"
"fmt"
"os"
"strings"
"testing"
@@ -38,7 +39,7 @@ func TestBuildCompositeSVG(t *testing.T) {
WidthMM: 300, HeightMM: 400,
MarginTop: 10, MarginRight: 10, MarginBottom: 10, MarginLeft: 10,
}
spec := model.ItemSpec{SizeMM: 80, BleedMM: 2, MarginMM: 5, PaddingMM: 3}
spec := model.ItemSpec{WidthMM: 80, HeightMM: 80, BleedMM: 2, MarginMM: 5, PaddingMM: 3}
preview := layout.Pack(plate, spec, 2)
out, err := BuildCompositeSVG(plate, spec, []byte(sampleItemSVG), preview)
@@ -53,7 +54,10 @@ func TestBuildCompositeSVG(t *testing.T) {
t.Fatal("expected embedded item images")
}
if preview.Count > 0 && !strings.Contains(s, `x="10.0000"`) {
t.Fatal("expected items at plate margin (footprint origin)")
t.Fatal("expected items at plate margin (canvas origin)")
}
if preview.Count > 0 && !strings.Contains(s, fmt.Sprintf(`width="%.4f"`, preview.CanvasWidthMM)) {
t.Fatal("expected full canvas width on placed images")
}
}
@@ -73,11 +77,15 @@ func TestGeneratePDF(t *testing.T) {
WidthMM: 200, HeightMM: 200,
MarginLeft: 10, MarginTop: 10, MarginRight: 10, MarginBottom: 10,
}
spec := model.ItemSpec{SizeMM: 80, BleedMM: 2, MarginMM: 5, PaddingMM: 3}
spec := model.ItemSpec{WidthMM: 80, HeightMM: 80, BleedMM: 2, MarginMM: 5, PaddingMM: 3}
preview := layout.Pack(plate, spec, 2)
var buf bytes.Buffer
if err := svgtemplate.Write(&buf, svgtemplate.Build(spec.SizeMM, spec.BleedMM, spec.MarginMM, spec.PaddingMM)); err != nil {
if err := svgtemplate.Write(&buf, svgtemplate.Build(
spec.WidthMM, spec.HeightMM,
spec.BleedMM, spec.MarginMM, spec.PaddingMM,
spec.CornerRadiusMM,
)); err != nil {
t.Fatal(err)
}