...

Source file src/oss.terrastruct.com/d2/lib/shape/shape_stored_data.go

Documentation: oss.terrastruct.com/d2/lib/shape

     1  package shape
     2  
     3  import (
     4  	"math"
     5  
     6  	"oss.terrastruct.com/d2/lib/geo"
     7  	"oss.terrastruct.com/d2/lib/svg"
     8  	"oss.terrastruct.com/util-go/go2"
     9  )
    10  
    11  type shapeStoredData struct {
    12  	*baseShape
    13  }
    14  
    15  const storedDataWedgeWidth = 15.
    16  
    17  func NewStoredData(box *geo.Box) Shape {
    18  	shape := shapeStoredData{
    19  		baseShape: &baseShape{
    20  			Type: STORED_DATA_TYPE,
    21  			Box:  box,
    22  		},
    23  	}
    24  	shape.FullShape = go2.Pointer(Shape(shape))
    25  	return shape
    26  }
    27  
    28  func (s shapeStoredData) GetInnerBox() *geo.Box {
    29  	width := s.Box.Width
    30  	tl := s.Box.TopLeft.Copy()
    31  	width -= 2 * storedDataWedgeWidth
    32  	tl.X += storedDataWedgeWidth
    33  	return geo.NewBox(tl, width, s.Box.Height)
    34  }
    35  
    36  func storedDataPath(box *geo.Box) *svg.SvgPathContext {
    37  	wedgeWidth := storedDataWedgeWidth
    38  	multiplier := 0.27
    39  	if box.Width < wedgeWidth*2 {
    40  		wedgeWidth = box.Width / 2.0
    41  	}
    42  	pc := svg.NewSVGPathContext(box.TopLeft, 1, 1)
    43  	pc.StartAt(pc.Absolute(wedgeWidth, 0))
    44  	pc.H(true, box.Width-wedgeWidth)
    45  	pc.C(false, box.Width-wedgeWidth*multiplier, 0, box.Width-wedgeWidth, box.Height*multiplier, box.Width-wedgeWidth, box.Height/2.0)
    46  	pc.C(false, box.Width-wedgeWidth, box.Height-box.Height*multiplier, box.Width-wedgeWidth*multiplier, box.Height, box.Width, box.Height)
    47  	pc.H(true, -(box.Width - wedgeWidth))
    48  	pc.C(false, wedgeWidth-wedgeWidth*multiplier, box.Height, 0, box.Height-box.Height*multiplier, 0, box.Height/2.0)
    49  	pc.C(false, 0, box.Height*multiplier, wedgeWidth-wedgeWidth*multiplier, 0, wedgeWidth, 0)
    50  	pc.Z()
    51  	return pc
    52  }
    53  
    54  func (s shapeStoredData) Perimeter() []geo.Intersectable {
    55  	return storedDataPath(s.Box).Path
    56  }
    57  
    58  func (s shapeStoredData) GetSVGPathData() []string {
    59  	return []string{
    60  		storedDataPath(s.Box).PathData(),
    61  	}
    62  }
    63  
    64  func (s shapeStoredData) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
    65  	totalWidth := width + paddingX + 2*storedDataWedgeWidth
    66  	return math.Ceil(totalWidth), math.Ceil(height + paddingY)
    67  }
    68  
    69  func (s shapeStoredData) GetDefaultPadding() (paddingX, paddingY float64) {
    70  	return defaultPadding - 10, defaultPadding
    71  }
    72  

View as plain text