...

Source file src/oss.terrastruct.com/d2/lib/shape/shape_queue.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 shapeQueue struct {
    12  	*baseShape
    13  }
    14  
    15  func NewQueue(box *geo.Box) Shape {
    16  	shape := shapeQueue{
    17  		baseShape: &baseShape{
    18  			Type: QUEUE_TYPE,
    19  			Box:  box,
    20  		},
    21  	}
    22  	shape.FullShape = go2.Pointer(Shape(shape))
    23  	return shape
    24  }
    25  
    26  func getArcWidth(box *geo.Box) float64 {
    27  	arcWidth := defaultArcDepth
    28  	// Note: box width should always be larger than 3*default
    29  	// this just handles after collaping into an oval
    30  	if box.Width < arcWidth*2 {
    31  		arcWidth = box.Width / 2.0
    32  	}
    33  	return arcWidth
    34  }
    35  
    36  func (s shapeQueue) GetInnerBox() *geo.Box {
    37  	width := s.Box.Width
    38  	tl := s.Box.TopLeft.Copy()
    39  	arcWidth := getArcWidth(s.Box)
    40  	width -= 3 * arcWidth
    41  	tl.X += arcWidth
    42  	return geo.NewBox(tl, width, s.Box.Height)
    43  }
    44  
    45  func queueOuterPath(box *geo.Box) *svg.SvgPathContext {
    46  	arcWidth := getArcWidth(box)
    47  	multiplier := 0.45
    48  	pc := svg.NewSVGPathContext(box.TopLeft, 1, 1)
    49  	pc.StartAt(pc.Absolute(arcWidth, 0))
    50  	pc.H(true, box.Width-2*arcWidth)
    51  	pc.C(false, box.Width, 0, box.Width, box.Height*multiplier, box.Width, box.Height/2.0)
    52  	pc.C(false, box.Width, box.Height-box.Height*multiplier, box.Width, box.Height, box.Width-arcWidth, box.Height)
    53  	pc.H(true, -1*(box.Width-2*arcWidth))
    54  	pc.C(false, 0, box.Height, 0, box.Height-box.Height*multiplier, 0, box.Height/2.0)
    55  	pc.C(false, 0, box.Height*multiplier, 0, 0, arcWidth, 0)
    56  	pc.Z()
    57  	return pc
    58  }
    59  
    60  func queueInnerPath(box *geo.Box) *svg.SvgPathContext {
    61  	arcWidth := getArcWidth(box)
    62  	multiplier := 0.45
    63  	pc := svg.NewSVGPathContext(box.TopLeft, 1, 1)
    64  	pc.StartAt(pc.Absolute(box.Width-arcWidth, 0))
    65  	pc.C(false, box.Width-2*arcWidth, 0, box.Width-2*arcWidth, box.Height*multiplier, box.Width-2*arcWidth, box.Height/2.0)
    66  	pc.C(false, box.Width-2*arcWidth, box.Height-box.Height*multiplier, box.Width-2*arcWidth, box.Height, box.Width-arcWidth, box.Height)
    67  	return pc
    68  }
    69  
    70  func (s shapeQueue) Perimeter() []geo.Intersectable {
    71  	return queueOuterPath(s.Box).Path
    72  }
    73  
    74  func (s shapeQueue) GetSVGPathData() []string {
    75  	return []string{
    76  		queueOuterPath(s.Box).PathData(),
    77  		queueInnerPath(s.Box).PathData(),
    78  	}
    79  }
    80  
    81  func (s shapeQueue) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
    82  	// 1 arc left, width+ padding, 2 arcs right
    83  	totalWidth := 3*defaultArcDepth + width + paddingX
    84  	return math.Ceil(totalWidth), math.Ceil(height + paddingY)
    85  }
    86  
    87  func (s shapeQueue) GetDefaultPadding() (paddingX, paddingY float64) {
    88  	return defaultPadding / 2, defaultPadding
    89  }
    90  

View as plain text