2025-03-02 17:45:42 +00:00
|
|
|
package shape
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"math"
|
|
|
|
|
|
|
|
|
|
"oss.terrastruct.com/d2/lib/geo"
|
|
|
|
|
"oss.terrastruct.com/d2/lib/svg"
|
|
|
|
|
"oss.terrastruct.com/util-go/go2"
|
|
|
|
|
)
|
|
|
|
|
|
2025-03-14 01:41:31 +00:00
|
|
|
// Constants to match frontend implementation
|
|
|
|
|
const (
|
|
|
|
|
C4_PERSON_AR_LIMIT = 1.5
|
|
|
|
|
HEAD_RADIUS_FACTOR = 0.22
|
|
|
|
|
BODY_TOP_FACTOR = 0.8
|
|
|
|
|
CORNER_RADIUS_FACTOR = 0.175
|
|
|
|
|
)
|
|
|
|
|
|
2025-03-02 17:45:42 +00:00
|
|
|
type shapeC4Person struct {
|
|
|
|
|
*baseShape
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewC4Person(box *geo.Box) Shape {
|
|
|
|
|
shape := shapeC4Person{
|
|
|
|
|
baseShape: &baseShape{
|
|
|
|
|
Type: C4_PERSON_TYPE,
|
|
|
|
|
Box: box,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
shape.FullShape = go2.Pointer(Shape(shape))
|
|
|
|
|
return shape
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeC4Person) GetInnerBox() *geo.Box {
|
|
|
|
|
width := s.Box.Width
|
2025-03-14 01:24:54 +00:00
|
|
|
height := s.Box.Height
|
2025-03-02 17:45:42 +00:00
|
|
|
|
2025-03-14 02:11:35 +00:00
|
|
|
headRadius := width * HEAD_RADIUS_FACTOR
|
2025-03-14 01:41:31 +00:00
|
|
|
headCenterY := headRadius
|
2025-03-14 02:11:35 +00:00
|
|
|
bodyTop := headCenterY + headRadius*BODY_TOP_FACTOR
|
2025-03-02 17:45:42 +00:00
|
|
|
|
2025-03-14 01:41:31 +00:00
|
|
|
// Horizontal padding = 5% of width
|
2025-03-14 02:11:35 +00:00
|
|
|
horizontalPadding := width * 0.05
|
2025-03-14 01:41:31 +00:00
|
|
|
// Vertical padding = 3% of height
|
2025-03-14 02:11:35 +00:00
|
|
|
verticalPadding := height * 0.03
|
2025-03-13 18:20:34 +00:00
|
|
|
|
2025-03-02 17:45:42 +00:00
|
|
|
tl := s.Box.TopLeft.Copy()
|
|
|
|
|
tl.X += horizontalPadding
|
2025-03-14 01:41:31 +00:00
|
|
|
tl.Y += bodyTop + verticalPadding
|
2025-03-02 17:45:42 +00:00
|
|
|
|
2025-03-14 02:11:35 +00:00
|
|
|
innerWidth := width - (horizontalPadding * 2)
|
|
|
|
|
innerHeight := height - bodyTop - (verticalPadding * 2)
|
2025-03-02 17:45:42 +00:00
|
|
|
|
|
|
|
|
return geo.NewBox(tl, innerWidth, innerHeight)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func bodyPath(box *geo.Box) *svg.SvgPathContext {
|
|
|
|
|
width := box.Width
|
|
|
|
|
height := box.Height
|
|
|
|
|
|
|
|
|
|
pc := svg.NewSVGPathContext(box.TopLeft, 1, 1)
|
|
|
|
|
|
2025-03-14 02:11:35 +00:00
|
|
|
headRadius := width * HEAD_RADIUS_FACTOR
|
2025-03-14 01:41:31 +00:00
|
|
|
headCenterY := headRadius
|
2025-03-14 02:11:35 +00:00
|
|
|
bodyTop := headCenterY + headRadius*BODY_TOP_FACTOR
|
2025-03-02 17:45:42 +00:00
|
|
|
bodyWidth := width
|
2025-03-14 02:11:35 +00:00
|
|
|
bodyHeight := height - bodyTop
|
2025-03-02 17:45:42 +00:00
|
|
|
bodyLeft := 0
|
2025-03-14 01:41:31 +00:00
|
|
|
|
2025-03-14 02:11:35 +00:00
|
|
|
// Use the same corner radius calculation as frontend
|
|
|
|
|
cornerRadius := math.Min(width*CORNER_RADIUS_FACTOR, bodyHeight*0.25)
|
2025-03-02 17:45:42 +00:00
|
|
|
|
|
|
|
|
pc.StartAt(pc.Absolute(float64(bodyLeft), bodyTop+cornerRadius))
|
|
|
|
|
|
2025-03-14 02:14:40 +00:00
|
|
|
pc.C(true, 0, -4*(math.Sqrt(2)-1)/3*cornerRadius, 4*(math.Sqrt(2)-1)/3*cornerRadius, -cornerRadius, cornerRadius, -cornerRadius)
|
2025-03-02 17:45:42 +00:00
|
|
|
pc.H(true, bodyWidth-2*cornerRadius)
|
2025-03-14 02:14:40 +00:00
|
|
|
pc.C(true, 4*(math.Sqrt(2)-1)/3*cornerRadius, 0, cornerRadius, 4*(math.Sqrt(2)-1)/3*cornerRadius, cornerRadius, cornerRadius)
|
2025-03-02 17:45:42 +00:00
|
|
|
pc.V(true, bodyHeight-2*cornerRadius)
|
2025-03-14 02:14:40 +00:00
|
|
|
pc.C(true, 0, 4*(math.Sqrt(2)-1)/3*cornerRadius, -4*(math.Sqrt(2)-1)/3*cornerRadius, cornerRadius, -cornerRadius, cornerRadius)
|
2025-03-02 17:45:42 +00:00
|
|
|
pc.H(true, -(bodyWidth - 2*cornerRadius))
|
2025-03-14 02:14:40 +00:00
|
|
|
pc.C(true, -4*(math.Sqrt(2)-1)/3*cornerRadius, 0, -cornerRadius, -4*(math.Sqrt(2)-1)/3*cornerRadius, -cornerRadius, -cornerRadius)
|
2025-03-02 17:45:42 +00:00
|
|
|
pc.Z()
|
|
|
|
|
|
|
|
|
|
return pc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func headPath(box *geo.Box) *svg.SvgPathContext {
|
|
|
|
|
width := box.Width
|
2025-03-14 01:24:54 +00:00
|
|
|
|
2025-03-02 17:45:42 +00:00
|
|
|
pc := svg.NewSVGPathContext(box.TopLeft, 1, 1)
|
|
|
|
|
|
2025-03-14 02:11:35 +00:00
|
|
|
headRadius := width * HEAD_RADIUS_FACTOR
|
|
|
|
|
headCenterX := width / 2
|
2025-03-14 01:41:31 +00:00
|
|
|
headCenterY := headRadius
|
2025-03-02 17:45:42 +00:00
|
|
|
|
|
|
|
|
pc.StartAt(pc.Absolute(headCenterX, headCenterY-headRadius))
|
|
|
|
|
|
|
|
|
|
pc.C(false,
|
2025-03-14 02:14:40 +00:00
|
|
|
headCenterX+headRadius*4*(math.Sqrt(2)-1)/3, headCenterY-headRadius,
|
|
|
|
|
headCenterX+headRadius, headCenterY-headRadius*4*(math.Sqrt(2)-1)/3,
|
2025-03-02 17:45:42 +00:00
|
|
|
headCenterX+headRadius, headCenterY)
|
|
|
|
|
|
|
|
|
|
pc.C(false,
|
2025-03-14 02:14:40 +00:00
|
|
|
headCenterX+headRadius, headCenterY+headRadius*4*(math.Sqrt(2)-1)/3,
|
|
|
|
|
headCenterX+headRadius*4*(math.Sqrt(2)-1)/3, headCenterY+headRadius,
|
2025-03-02 17:45:42 +00:00
|
|
|
headCenterX, headCenterY+headRadius)
|
|
|
|
|
|
|
|
|
|
pc.C(false,
|
2025-03-14 02:14:40 +00:00
|
|
|
headCenterX-headRadius*4*(math.Sqrt(2)-1)/3, headCenterY+headRadius,
|
|
|
|
|
headCenterX-headRadius, headCenterY+headRadius*4*(math.Sqrt(2)-1)/3,
|
2025-03-02 17:45:42 +00:00
|
|
|
headCenterX-headRadius, headCenterY)
|
|
|
|
|
|
|
|
|
|
pc.C(false,
|
2025-03-14 02:14:40 +00:00
|
|
|
headCenterX-headRadius, headCenterY-headRadius*4*(math.Sqrt(2)-1)/3,
|
|
|
|
|
headCenterX-headRadius*4*(math.Sqrt(2)-1)/3, headCenterY-headRadius,
|
2025-03-02 17:45:42 +00:00
|
|
|
headCenterX, headCenterY-headRadius)
|
|
|
|
|
|
|
|
|
|
return pc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeC4Person) Perimeter() []geo.Intersectable {
|
|
|
|
|
width := s.Box.Width
|
|
|
|
|
|
|
|
|
|
bodyPerimeter := bodyPath(s.Box).Path
|
|
|
|
|
|
2025-03-14 02:11:35 +00:00
|
|
|
headRadius := width * HEAD_RADIUS_FACTOR
|
|
|
|
|
headCenterX := s.Box.TopLeft.X + width/2
|
2025-03-14 01:41:31 +00:00
|
|
|
headCenterY := s.Box.TopLeft.Y + headRadius
|
2025-03-02 17:45:42 +00:00
|
|
|
headCenter := geo.NewPoint(headCenterX, headCenterY)
|
|
|
|
|
|
|
|
|
|
headEllipse := geo.NewEllipse(headCenter, headRadius, headRadius)
|
|
|
|
|
|
|
|
|
|
return append(bodyPerimeter, headEllipse)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeC4Person) GetSVGPathData() []string {
|
|
|
|
|
return []string{
|
|
|
|
|
bodyPath(s.Box).PathData(),
|
|
|
|
|
headPath(s.Box).PathData(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeC4Person) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
|
2025-03-14 01:24:54 +00:00
|
|
|
contentWidth := width + paddingX
|
|
|
|
|
contentHeight := height + paddingY
|
2025-03-13 18:20:34 +00:00
|
|
|
|
2025-03-14 01:24:54 +00:00
|
|
|
// Account for 10% total horizontal padding (5% on each side)
|
2025-03-14 02:11:35 +00:00
|
|
|
totalWidth := contentWidth / 0.9
|
|
|
|
|
headRadius := totalWidth * HEAD_RADIUS_FACTOR
|
2025-03-14 01:41:31 +00:00
|
|
|
|
|
|
|
|
// Use positioning matching frontend
|
|
|
|
|
headCenterY := headRadius
|
2025-03-14 02:11:35 +00:00
|
|
|
bodyTop := headCenterY + headRadius*BODY_TOP_FACTOR
|
2025-03-13 21:41:03 +00:00
|
|
|
|
2025-03-14 01:41:31 +00:00
|
|
|
// Include vertical padding
|
2025-03-14 02:11:35 +00:00
|
|
|
verticalPadding := totalWidth * 0.06 // 3% top + 3% bottom
|
|
|
|
|
totalHeight := contentHeight + bodyTop + verticalPadding
|
2025-03-13 17:55:30 +00:00
|
|
|
|
2025-03-14 01:41:31 +00:00
|
|
|
// Calculate minimum height
|
2025-03-14 02:11:35 +00:00
|
|
|
minHeight := totalWidth * 0.95
|
2025-03-14 01:24:54 +00:00
|
|
|
if totalHeight < minHeight {
|
|
|
|
|
totalHeight = minHeight
|
|
|
|
|
}
|
2025-03-13 21:41:03 +00:00
|
|
|
|
2025-03-13 18:20:34 +00:00
|
|
|
totalWidth, totalHeight = LimitAR(totalWidth, totalHeight, C4_PERSON_AR_LIMIT)
|
2025-03-02 17:45:42 +00:00
|
|
|
return math.Ceil(totalWidth), math.Ceil(totalHeight)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeC4Person) GetDefaultPadding() (paddingX, paddingY float64) {
|
2025-03-13 20:40:33 +00:00
|
|
|
return 10, defaultPadding
|
2025-03-02 17:45:42 +00:00
|
|
|
}
|