save
This commit is contained in:
parent
f119d582e0
commit
f5a4e4f059
1 changed files with 63 additions and 59 deletions
|
|
@ -5,12 +5,13 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
|
"cdr.dev/slog"
|
||||||
"oss.terrastruct.com/d2/d2graph"
|
"oss.terrastruct.com/d2/d2graph"
|
||||||
"oss.terrastruct.com/d2/d2target"
|
"oss.terrastruct.com/d2/d2target"
|
||||||
"oss.terrastruct.com/d2/lib/geo"
|
"oss.terrastruct.com/d2/lib/geo"
|
||||||
"oss.terrastruct.com/d2/lib/label"
|
"oss.terrastruct.com/d2/lib/label"
|
||||||
|
"oss.terrastruct.com/d2/lib/log"
|
||||||
"oss.terrastruct.com/util-go/go2"
|
"oss.terrastruct.com/util-go/go2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -69,7 +70,6 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
|
||||||
labelHeight = float64(obj.LabelDimensions.Height) + 2*label.PADDING
|
labelHeight = float64(obj.LabelDimensions.Height) + 2*label.PADDING
|
||||||
}
|
}
|
||||||
|
|
||||||
var dx, dy float64
|
|
||||||
if labelWidth > 0 {
|
if labelWidth > 0 {
|
||||||
switch labelPosition {
|
switch labelPosition {
|
||||||
case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight,
|
case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight,
|
||||||
|
|
@ -112,67 +112,71 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
overflowTop := padding.Top - float64(verticalPadding)
|
padding.Top = math.Max(padding.Top, float64(verticalPadding))
|
||||||
if overflowTop > 0 {
|
padding.Bottom = math.Max(padding.Bottom, float64(verticalPadding))
|
||||||
contentHeight += overflowTop
|
padding.Left = math.Max(padding.Left, float64(horizontalPadding))
|
||||||
dy += overflowTop
|
padding.Right = math.Max(padding.Right, float64(horizontalPadding))
|
||||||
}
|
|
||||||
overflowBottom := padding.Bottom - float64(verticalPadding)
|
|
||||||
if overflowBottom > 0 {
|
|
||||||
contentHeight += overflowBottom
|
|
||||||
}
|
|
||||||
overflowLeft := padding.Left - float64(horizontalPadding)
|
|
||||||
if overflowLeft > 0 {
|
|
||||||
contentWidth += overflowLeft
|
|
||||||
dx += overflowLeft
|
|
||||||
}
|
|
||||||
overflowRight := padding.Right - float64(horizontalPadding)
|
|
||||||
if overflowRight > 0 {
|
|
||||||
contentWidth += overflowRight
|
|
||||||
}
|
|
||||||
|
|
||||||
// manually handle desiredWidth/Height so we can center the grid
|
// TODO: rethink how this works with shapes and padding
|
||||||
var desiredWidth, desiredHeight int
|
// // manually handle desiredWidth/Height so we can center the grid
|
||||||
var originalWidthAttr, originalHeightAttr *d2graph.Scalar
|
// var desiredWidth, desiredHeight int
|
||||||
if obj.WidthAttr != nil {
|
// var originalWidthAttr, originalHeightAttr *d2graph.Scalar
|
||||||
desiredWidth, _ = strconv.Atoi(obj.WidthAttr.Value)
|
// if obj.WidthAttr != nil {
|
||||||
// SizeToContent without desired width
|
// desiredWidth, _ = strconv.Atoi(obj.WidthAttr.Value)
|
||||||
originalWidthAttr = obj.WidthAttr
|
// // SizeToContent without desired width
|
||||||
obj.WidthAttr = nil
|
// originalWidthAttr = obj.WidthAttr
|
||||||
}
|
// obj.WidthAttr = nil
|
||||||
if obj.HeightAttr != nil {
|
// }
|
||||||
desiredHeight, _ = strconv.Atoi(obj.HeightAttr.Value)
|
// if obj.HeightAttr != nil {
|
||||||
originalHeightAttr = obj.HeightAttr
|
// desiredHeight, _ = strconv.Atoi(obj.HeightAttr.Value)
|
||||||
obj.HeightAttr = nil
|
// originalHeightAttr = obj.HeightAttr
|
||||||
}
|
// obj.HeightAttr = nil
|
||||||
// size shape according to grid
|
// }
|
||||||
obj.SizeToContent(contentWidth, contentHeight, float64(2*horizontalPadding), float64(2*verticalPadding))
|
|
||||||
if originalWidthAttr != nil {
|
|
||||||
obj.WidthAttr = originalWidthAttr
|
|
||||||
}
|
|
||||||
if originalHeightAttr != nil {
|
|
||||||
obj.HeightAttr = originalHeightAttr
|
|
||||||
}
|
|
||||||
|
|
||||||
if desiredWidth > 0 {
|
totalWidth := padding.Left + contentWidth + padding.Right
|
||||||
ddx := float64(desiredWidth) - obj.Width
|
totalHeight := padding.Top + contentHeight + padding.Bottom
|
||||||
if ddx > 0 {
|
obj.SizeToContent(totalWidth, totalHeight, 0, 0)
|
||||||
dx += ddx / 2
|
|
||||||
obj.Width = float64(desiredWidth)
|
// if originalWidthAttr != nil {
|
||||||
}
|
// obj.WidthAttr = originalWidthAttr
|
||||||
}
|
// }
|
||||||
if desiredHeight > 0 {
|
// if originalHeightAttr != nil {
|
||||||
ddy := float64(desiredHeight) - obj.Height
|
// obj.HeightAttr = originalHeightAttr
|
||||||
if ddy > 0 {
|
// }
|
||||||
dy += ddy / 2
|
|
||||||
obj.Height = float64(desiredHeight)
|
// var offsetX, offsetY float64
|
||||||
}
|
// if desiredWidth > 0 {
|
||||||
}
|
// ddx := float64(desiredWidth) - obj.Width
|
||||||
|
// if ddx > 0 {
|
||||||
|
// offsetX = ddx / 2
|
||||||
|
// obj.Width = float64(desiredWidth)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if desiredHeight > 0 {
|
||||||
|
// ddy := float64(desiredHeight) - obj.Height
|
||||||
|
// if ddy > 0 {
|
||||||
|
// offsetY = ddy / 2
|
||||||
|
// obj.Height = float64(desiredHeight)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
// compute where the grid should be placed inside shape
|
// compute where the grid should be placed inside shape
|
||||||
innerBox := obj.ToShape().GetInnerBox()
|
s := obj.ToShape()
|
||||||
dx = innerBox.TopLeft.X + dx
|
innerTL := s.GetInsidePlacement(totalWidth, totalHeight, 0, 0)
|
||||||
dy = innerBox.TopLeft.Y + dy
|
|
||||||
|
log.Warn(ctx, obj.Shape.Value,
|
||||||
|
slog.F("box", obj.Box.ToString()),
|
||||||
|
slog.F("innerTL", innerTL.ToString()),
|
||||||
|
slog.F("contentWidth", contentWidth),
|
||||||
|
slog.F("contentHeight", contentHeight),
|
||||||
|
slog.F("labelWidth", labelWidth),
|
||||||
|
slog.F("labelHeight", labelHeight),
|
||||||
|
slog.F("padding", padding),
|
||||||
|
)
|
||||||
|
|
||||||
|
// move from horizontalPadding,verticalPadding to innerTL.X+padding.Left, innerTL.Y+padding.Top
|
||||||
|
dx := -float64(horizontalPadding) + innerTL.X + padding.Left
|
||||||
|
dy := -float64(verticalPadding) + innerTL.Y + padding.Top
|
||||||
if dx != 0 || dy != 0 {
|
if dx != 0 || dy != 0 {
|
||||||
gd.shift(dx, dy)
|
gd.shift(dx, dy)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue