account for shapes that need a larger innerBox

This commit is contained in:
Gavin Nishizawa 2023-11-15 19:58:29 -08:00
parent f5a4e4f059
commit 1786f57e66
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD

View file

@ -163,6 +163,17 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
// compute where the grid should be placed inside shape // compute where the grid should be placed inside shape
s := obj.ToShape() s := obj.ToShape()
innerTL := s.GetInsidePlacement(totalWidth, totalHeight, 0, 0) innerTL := s.GetInsidePlacement(totalWidth, totalHeight, 0, 0)
// depending on the shape innerBox may be larger than totalWidth, totalHeight
// if this is the case, we want to center the cells within the larger innerBox
innerBox := s.GetInnerBox()
var resizeDx, resizeDy float64
if innerBox.Width > totalWidth {
resizeDx = (innerBox.Width - totalWidth) / 2
}
if innerBox.Height > totalHeight {
resizeDy = (innerBox.Height - totalHeight) / 2
}
log.Warn(ctx, obj.Shape.Value, log.Warn(ctx, obj.Shape.Value,
slog.F("box", obj.Box.ToString()), slog.F("box", obj.Box.ToString()),
@ -175,8 +186,8 @@ func Layout(ctx context.Context, g *d2graph.Graph) error {
) )
// move from horizontalPadding,verticalPadding to innerTL.X+padding.Left, innerTL.Y+padding.Top // move from horizontalPadding,verticalPadding to innerTL.X+padding.Left, innerTL.Y+padding.Top
dx := -float64(horizontalPadding) + innerTL.X + padding.Left dx := -float64(horizontalPadding) + innerTL.X + padding.Left + resizeDx
dy := -float64(verticalPadding) + innerTL.Y + padding.Top dy := -float64(verticalPadding) + innerTL.Y + padding.Top + resizeDy
if dx != 0 || dy != 0 { if dx != 0 || dy != 0 {
gd.shift(dx, dy) gd.shift(dx, dy)
} }