simplify func

This commit is contained in:
Gavin Nishizawa 2023-05-04 20:58:39 -07:00
parent 5a3fa6b5af
commit 45d798dc53
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
2 changed files with 18 additions and 8 deletions

View file

@ -1052,17 +1052,28 @@ func (obj *Object) GetDefaultSize(mtexts []*d2target.MText, ruler *textmeasure.R
return &dims, nil
}
func (obj *Object) SizeToContent(contentBox *geo.Box, paddingX, paddingY, desiredWidth, desiredHeight float64, labelDims d2target.TextDimensions) {
// resizes the object to fit content of the given width and height in its inner box with the given padding.
// this accounts for the shape of the object, and if there is a desired width or height set for the object
func (obj *Object) SizeToContent(contentWidth, contentHeight, paddingX, paddingY float64) {
var desiredWidth int
var desiredHeight int
if obj.WidthAttr != nil {
desiredWidth, _ = strconv.Atoi(obj.WidthAttr.Value)
}
if obj.HeightAttr != nil {
desiredHeight, _ = strconv.Atoi(obj.HeightAttr.Value)
}
dslShape := strings.ToLower(obj.Shape.Value)
shapeType := d2target.DSL_SHAPE_TO_SHAPE_TYPE[dslShape]
s := shape.NewShape(shapeType, contentBox)
s := shape.NewShape(shapeType, geo.NewBox(geo.NewPoint(0, 0), contentWidth, contentHeight))
var fitWidth, fitHeight float64
if shapeType == shape.PERSON_TYPE {
fitWidth = contentBox.Width + paddingX
fitHeight = contentBox.Height + paddingY
fitWidth = contentWidth + paddingX
fitHeight = contentHeight + paddingY
} else {
fitWidth, fitHeight = s.GetDimensionsToFit(contentBox.Width, contentBox.Height, paddingX, paddingY)
fitWidth, fitHeight = s.GetDimensionsToFit(contentWidth, contentHeight, paddingX, paddingY)
}
obj.Width = math.Max(float64(desiredWidth), fitWidth)
obj.Height = math.Max(float64(desiredHeight), fitHeight)
@ -1495,7 +1506,7 @@ func (g *Graph) SetDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler
}
}
obj.SizeToContent(contentBox, paddingX, paddingY, float64(desiredWidth), float64(desiredHeight), *labelDims)
obj.SizeToContent(contentBox.Width, contentBox.Height, paddingX, paddingY)
}
for _, edge := range g.Edges {
usedFont := fontFamily

View file

@ -75,8 +75,7 @@ func withoutGridDiagrams(ctx context.Context, g *d2graph.Graph) (gridDiagrams ma
if obj.Box != nil {
// size shape according to grid
contentBox := geo.NewBox(geo.NewPoint(0, 0), float64(gd.width), float64(gd.height))
obj.SizeToContent(contentBox, 2*CONTAINER_PADDING, 2*CONTAINER_PADDING, 0, 0, obj.LabelDimensions)
obj.SizeToContent(float64(gd.width), float64(gd.height), 2*CONTAINER_PADDING, 2*CONTAINER_PADDING)
// compute where the grid should be placed inside shape
dslShape := strings.ToLower(obj.Shape.Value)