From 1786f57e66be4f281cdf8dd28bebfcf1cd3569b6 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Wed, 15 Nov 2023 19:58:29 -0800 Subject: [PATCH] account for shapes that need a larger innerBox --- d2layouts/d2grid/layout.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/d2layouts/d2grid/layout.go b/d2layouts/d2grid/layout.go index 3add3eaf5..819f5d09b 100644 --- a/d2layouts/d2grid/layout.go +++ b/d2layouts/d2grid/layout.go @@ -163,6 +163,17 @@ func Layout(ctx context.Context, g *d2graph.Graph) error { // compute where the grid should be placed inside shape s := obj.ToShape() 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, 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 - dx := -float64(horizontalPadding) + innerTL.X + padding.Left - dy := -float64(verticalPadding) + innerTL.Y + padding.Top + dx := -float64(horizontalPadding) + innerTL.X + padding.Left + resizeDx + dy := -float64(verticalPadding) + innerTL.Y + padding.Top + resizeDy if dx != 0 || dy != 0 { gd.shift(dx, dy) }