2022-11-03 13:54:49 +00:00
|
|
|
package shape
|
|
|
|
|
|
|
|
|
|
import (
|
2023-01-21 04:04:59 +00:00
|
|
|
"math"
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
"oss.terrastruct.com/d2/lib/geo"
|
|
|
|
|
"oss.terrastruct.com/d2/lib/svg"
|
2023-02-10 19:03:07 +00:00
|
|
|
"oss.terrastruct.com/util-go/go2"
|
2022-11-03 13:54:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type shapePage struct {
|
|
|
|
|
*baseShape
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 04:04:59 +00:00
|
|
|
const (
|
|
|
|
|
// TODO: cleanup
|
|
|
|
|
pageCornerWidth = 20.8164
|
|
|
|
|
pageCornerHeight = 20.348
|
|
|
|
|
)
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
func NewPage(box *geo.Box) Shape {
|
2023-02-10 19:03:07 +00:00
|
|
|
shape := shapePage{
|
2022-11-03 13:54:49 +00:00
|
|
|
baseShape: &baseShape{
|
|
|
|
|
Type: PAGE_TYPE,
|
|
|
|
|
Box: box,
|
|
|
|
|
},
|
|
|
|
|
}
|
2023-02-10 19:03:07 +00:00
|
|
|
shape.FullShape = go2.Pointer(Shape(shape))
|
|
|
|
|
return shape
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-21 04:04:59 +00:00
|
|
|
func (s shapePage) GetInnerBox() *geo.Box {
|
|
|
|
|
// Note: for simplicity this assumes shape padding is greater than pageCornerSize
|
|
|
|
|
width := s.Box.Width
|
|
|
|
|
// consider right hand side occupied by corner for short pages
|
|
|
|
|
if s.Box.Height < 3*pageCornerHeight {
|
|
|
|
|
width -= pageCornerWidth
|
|
|
|
|
}
|
|
|
|
|
return geo.NewBox(s.Box.TopLeft.Copy(), width, s.Box.Height)
|
|
|
|
|
}
|
2022-12-06 02:52:08 +00:00
|
|
|
|
2022-12-06 03:36:15 +00:00
|
|
|
func pageOuterPath(box *geo.Box) *svg.SvgPathContext {
|
2023-01-21 04:04:59 +00:00
|
|
|
// TODO: cleanup
|
2022-12-06 02:52:08 +00:00
|
|
|
pc := svg.NewSVGPathContext(box.TopLeft, 1., 1.)
|
2022-12-06 03:36:15 +00:00
|
|
|
pc.StartAt(pc.Absolute(0.5, 0))
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.H(false, box.Width-20.8164)
|
|
|
|
|
pc.C(false, box.Width-19.6456, 0.0, box.Width-18.521, 0.456297, box.Width-17.6811, 1.27202)
|
|
|
|
|
pc.L(false, box.Width-1.3647, 17.12)
|
|
|
|
|
pc.C(false, box.Width-0.4923, 17.9674, box.Width, 19.1318, box.Width, 20.348)
|
|
|
|
|
pc.V(false, box.Height-0.5)
|
|
|
|
|
pc.C(false, box.Width, box.Height-0.2239, box.Width-0.2239, box.Height, box.Width-0.5, box.Height)
|
2022-12-06 02:52:08 +00:00
|
|
|
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.H(false, 0.499999)
|
|
|
|
|
pc.C(false, 0.223857, box.Height, 0, box.Height-0.2239, 0, box.Height-0.5)
|
2022-12-06 03:36:15 +00:00
|
|
|
pc.V(false, 0.499999)
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.C(false, 0, 0.223857, 0.223857, 0, 0.5, 0)
|
2022-11-03 13:54:49 +00:00
|
|
|
pc.Z()
|
|
|
|
|
return pc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func pageInnerPath(box *geo.Box) *svg.SvgPathContext {
|
2022-12-06 03:36:15 +00:00
|
|
|
pc := svg.NewSVGPathContext(box.TopLeft, 1., 1.)
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.StartAt(pc.Absolute(box.Width-1.08197, box.Height))
|
2022-12-06 03:05:35 +00:00
|
|
|
pc.H(false, 1.08196)
|
2022-12-06 03:16:45 +00:00
|
|
|
pc.C(true, -0.64918, 0, -1.08196, -0.43287, -1.08196, -1.08219)
|
|
|
|
|
pc.V(false, 1.08219)
|
|
|
|
|
pc.C(true, 0, -0.64931, 0.43278, -1.08219, 1.08196, -1.08219)
|
2022-12-06 03:36:15 +00:00
|
|
|
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.H(true, box.Width-22.72132)
|
2022-12-06 03:16:45 +00:00
|
|
|
pc.C(true, 0.64918, 0, 1.08196, 0.43287, 1.08196, 1.08219)
|
|
|
|
|
pc.V(true, 17.09863)
|
|
|
|
|
pc.C(true, 0, 1.29863, 0.86557, 2.38082, 2.38032, 2.38082)
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.H(false, box.Width-1.08197)
|
2022-12-06 03:16:45 +00:00
|
|
|
pc.C(true, .64918, 0, 1.08196, 0.43287, 1.08196, 1.08196)
|
2023-01-21 04:04:59 +00:00
|
|
|
pc.V(false, box.Height-1.0822)
|
|
|
|
|
pc.C(false, box.Width-1.0, box.Height-0.43288, box.Width-0.43279, box.Height, box.Width-1.08197, box.Height)
|
2022-11-03 13:54:49 +00:00
|
|
|
pc.Z()
|
|
|
|
|
return pc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapePage) Perimeter() []geo.Intersectable {
|
|
|
|
|
return pageOuterPath(s.Box).Path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s shapePage) GetSVGPathData() []string {
|
|
|
|
|
return []string{
|
|
|
|
|
pageOuterPath(s.Box).PathData(),
|
|
|
|
|
pageInnerPath(s.Box).PathData(),
|
2023-01-21 04:04:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-23 18:32:12 +00:00
|
|
|
func (s shapePage) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
|
|
|
|
|
totalWidth := width + paddingX
|
|
|
|
|
totalHeight := height + paddingY
|
2023-01-21 04:04:59 +00:00
|
|
|
// add space for corner with short pages
|
|
|
|
|
if totalHeight < 3*pageCornerHeight {
|
|
|
|
|
totalWidth += pageCornerWidth
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
2023-01-21 04:04:59 +00:00
|
|
|
totalWidth = math.Max(totalWidth, 2*pageCornerWidth)
|
|
|
|
|
totalHeight = math.Max(totalHeight, pageCornerHeight)
|
2023-01-25 01:32:42 +00:00
|
|
|
return math.Ceil(totalWidth), math.Ceil(totalHeight)
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
2023-01-24 03:10:31 +00:00
|
|
|
|
|
|
|
|
func (s shapePage) GetDefaultPadding() (paddingX, paddingY float64) {
|
|
|
|
|
return defaultPadding, pageCornerHeight + defaultPadding
|
|
|
|
|
}
|