...

Source file src/oss.terrastruct.com/d2/lib/shape/shape_document.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 shapeDocument struct {
    12  	*baseShape
    13  }
    14  
    15  const (
    16  	// the shape is taller than where the bottom of the path ends
    17  	docPathHeight      = 18.925
    18  	docPathInnerBottom = 14
    19  	docPathBottom      = 16.3
    20  )
    21  
    22  func NewDocument(box *geo.Box) Shape {
    23  	shape := shapeDocument{
    24  		baseShape: &baseShape{
    25  			Type: DOCUMENT_TYPE,
    26  			Box:  box,
    27  		},
    28  	}
    29  	shape.FullShape = go2.Pointer(Shape(shape))
    30  	return shape
    31  }
    32  
    33  func (s shapeDocument) GetInnerBox() *geo.Box {
    34  	height := s.Box.Height * docPathInnerBottom / docPathHeight
    35  	return geo.NewBox(s.Box.TopLeft.Copy(), s.Box.Width, height)
    36  }
    37  
    38  func documentPath(box *geo.Box) *svg.SvgPathContext {
    39  	pc := svg.NewSVGPathContext(box.TopLeft, box.Width, box.Height)
    40  	pc.StartAt(pc.Absolute(0, docPathBottom/docPathHeight))
    41  	pc.L(false, 0, 0)
    42  	pc.L(false, 1, 0)
    43  	pc.L(false, 1, docPathBottom/docPathHeight)
    44  	pc.C(false, 5/6.0, 12.8/docPathHeight, 2/3.0, 12.8/docPathHeight, 1/2.0, docPathBottom/docPathHeight)
    45  	pc.C(false, 1/3.0, 19.8/docPathHeight, 1/6.0, 19.8/docPathHeight, 0, docPathBottom/docPathHeight)
    46  	pc.Z()
    47  	return pc
    48  }
    49  
    50  func (s shapeDocument) Perimeter() []geo.Intersectable {
    51  	return documentPath(s.Box).Path
    52  }
    53  
    54  func (s shapeDocument) GetSVGPathData() []string {
    55  	return []string{
    56  		documentPath(s.Box).PathData(),
    57  	}
    58  }
    59  
    60  func (s shapeDocument) GetDimensionsToFit(width, height, paddingX, paddingY float64) (float64, float64) {
    61  	baseHeight := (height + paddingY) * docPathHeight / docPathInnerBottom
    62  	return math.Ceil(width + paddingX), math.Ceil(baseHeight)
    63  }
    64  
    65  func (s shapeDocument) GetDefaultPadding() (paddingX, paddingY float64) {
    66  	return defaultPadding, defaultPadding * docPathInnerBottom / docPathHeight
    67  }
    68  

View as plain text