2022-11-03 13:54:49 +00:00
|
|
|
package shape
|
|
|
|
|
|
|
|
|
|
import (
|
2023-01-25 01:32:42 +00:00
|
|
|
"math"
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
"oss.terrastruct.com/d2/lib/geo"
|
|
|
|
|
"oss.terrastruct.com/d2/lib/svg"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type shapeDocument struct {
|
|
|
|
|
*baseShape
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 04:04:59 +00:00
|
|
|
const (
|
|
|
|
|
// the shape is taller than where the bottom of the path ends
|
|
|
|
|
docPathHeight = 18.925
|
|
|
|
|
docPathInnerBottom = 14
|
|
|
|
|
docPathBottom = 16.3
|
|
|
|
|
)
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
func NewDocument(box *geo.Box) Shape {
|
|
|
|
|
return shapeDocument{
|
|
|
|
|
baseShape: &baseShape{
|
|
|
|
|
Type: DOCUMENT_TYPE,
|
|
|
|
|
Box: box,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 04:04:59 +00:00
|
|
|
func (s shapeDocument) GetInnerBox() *geo.Box {
|
|
|
|
|
height := s.Box.Height * docPathInnerBottom / docPathHeight
|
|
|
|
|
return geo.NewBox(s.Box.TopLeft.Copy(), s.Box.Width, height)
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
func documentPath(box *geo.Box) *svg.SvgPathContext {
|
|
|
|
|
pc := svg.NewSVGPathContext(box.TopLeft, box.Width, box.Height)
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.StartAt(pc.Absolute(0, docPathBottom/docPathHeight))
|
2022-11-03 13:54:49 +00:00
|
|
|
pc.L(false, 0, 0)
|
|
|
|
|
pc.L(false, 1, 0)
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.L(false, 1, docPathBottom/docPathHeight)
|
|
|
|
|
pc.C(false, 5/6.0, 12.8/docPathHeight, 2/3.0, 12.8/docPathHeight, 1/2.0, docPathBottom/docPathHeight)
|
|
|
|
|
pc.C(false, 1/3.0, 19.8/docPathHeight, 1/6.0, 19.8/docPathHeight, 0, docPathBottom/docPathHeight)
|
2022-11-03 13:54:49 +00:00
|
|
|
pc.Z()
|
|
|
|
|
return pc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeDocument) Perimeter() []geo.Intersectable {
|
|
|
|
|
return documentPath(s.Box).Path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeDocument) GetSVGPathData() []string {
|
|
|
|
|
return []string{
|
|
|
|
|
documentPath(s.Box).PathData(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-21 04:04:59 +00:00
|
|
|
|
2023-01-23 18:32:12 +00:00
|
|
|
func (s shapeDocument) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
|
2023-01-24 03:10:31 +00:00
|
|
|
baseHeight := (height + paddingY) * docPathHeight / docPathInnerBottom
|
2023-01-25 01:32:42 +00:00
|
|
|
return math.Ceil(width + paddingX), math.Ceil(baseHeight)
|
2023-01-24 03:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapeDocument) GetDefaultPadding() (paddingX, paddingY float64) {
|
|
|
|
|
return defaultPadding, defaultPadding * docPathInnerBottom / docPathHeight
|
2023-01-21 04:04:59 +00:00
|
|
|
}
|