78 lines
1.7 KiB
Go

package svgtemplate
import "math"
// Data holds dynamic values for the SVG mask template.
type Data struct {
Width, Height float64
CornerRadius float64
Bleed float64
Margin float64
Padding float64
ViewBoxMin float64
ViewBoxWidth float64
ViewBoxHeight float64
MaskWidth float64
MaskHeight float64
OuterWidth float64
OuterHeight float64
OuterOffsetX float64
OuterOffsetY float64
InnerWidth float64
InnerHeight float64
InnerOffsetX float64
InnerOffsetY float64
InnerRadius float64
MarkLength float64
}
const defaultMarkLength = 6.0 // mm
// Build computes template data from product dimensions.
func Build(width, height, bleed, margin, padding, cornerRadius float64) Data {
offset := bleed + margin + defaultMarkLength
viewBoxW := width + (offset * 2)
viewBoxH := height + (offset * 2)
innerW := width - (padding * 2)
innerH := height - (padding * 2)
if innerW < 0 {
innerW = 0
}
if innerH < 0 {
innerH = 0
}
innerR := cornerRadius - padding
if innerR < 0 {
innerR = 0
}
maxInnerR := math.Min(innerW, innerH) / 2
if innerR > maxInnerR && maxInnerR > 0 {
innerR = maxInnerR
}
return Data{
Width: width,
Height: height,
CornerRadius: cornerRadius,
Bleed: bleed,
Margin: margin,
Padding: padding,
ViewBoxMin: -offset,
ViewBoxWidth: viewBoxW,
ViewBoxHeight: viewBoxH,
MarkLength: defaultMarkLength,
MaskWidth: width,
MaskHeight: height,
OuterWidth: width + (bleed * 2),
OuterHeight: height + (bleed * 2),
OuterOffsetX: -bleed,
OuterOffsetY: -bleed,
InnerWidth: innerW,
InnerHeight: innerH,
InnerOffsetX: padding,
InnerOffsetY: padding,
InnerRadius: innerR,
}
}