From 510c10227eefacf11a9133eedda48761cea4f870 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Fri, 30 Dec 2022 20:49:49 -0800 Subject: [PATCH] fix bounding boxes --- ci/release/changelogs/next.md | 4 ++-- d2renderers/d2svg/appendix/appendix.go | 3 ++- d2target/d2target.go | 14 ++++++++++---- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index f7e94cca05..f6b3aaea68 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -8,8 +8,8 @@ This release also gives more power to configure layouts. `width` and `height` ar #### Features 🚀 -- Tooltips can be set on shapes. See [https://d2lang.com/tour/tooltips](https://d2lang.com/tour/interactive). [#548](https://github.com/terrastruct/d2/pull/548) -- Links can be set on shapes. See [https://d2lang.com/tour/tooltips](https://d2lang.com/tour/interactive). [#548](https://github.com/terrastruct/d2/pull/548) +- Tooltips can be set on shapes. See [https://d2lang.com/tour/interactive](https://d2lang.com/tour/interactive). [#548](https://github.com/terrastruct/d2/pull/548) +- Links can be set on shapes. See [https://d2lang.com/tour/interactive](https://d2lang.com/tour/interactive). [#548](https://github.com/terrastruct/d2/pull/548) - The `width` and `height` attributes are no longer restricted to images and can be applied to non-container shapes. [#498](https://github.com/terrastruct/d2/pull/498) - Layout engine options are exposed and configurable. See individual layout pages on [https://d2lang.com/tour/layouts](https://d2lang.com/tour/layouts) for list of configurations. [#563](https://github.com/terrastruct/d2/pull/563) diff --git a/d2renderers/d2svg/appendix/appendix.go b/d2renderers/d2svg/appendix/appendix.go index f25dc1c028..47e7a7ba12 100644 --- a/d2renderers/d2svg/appendix/appendix.go +++ b/d2renderers/d2svg/appendix/appendix.go @@ -153,6 +153,7 @@ func generateAppendix(diagram *d2target.Diagram, ruler *textmeasure.Ruler, svg s } } } + totalHeight += SPACER return fmt.Sprintf(`%s `, tl.X, br.Y, (br.X - tl.X), strings.Join(lines, "\n")), maxWidth, totalHeight @@ -182,5 +183,5 @@ func generateLine(i, y int, text string, ruler *textmeasure.Ruler) (string, int, line += fmt.Sprintf(`%s`, ICON_RADIUS*3, y, FONT_SIZE, d2svg.RenderText(text, ICON_RADIUS*3, float64(dims.Height))) - return line, dims.Width + ICON_RADIUS*3, dims.Height + return line, dims.Width + ICON_RADIUS*3, go2.IntMax(dims.Height, ICON_RADIUS*2) } diff --git a/d2target/d2target.go b/d2target/d2target.go index 90974c7f12..ea54ee52ff 100644 --- a/d2target/d2target.go +++ b/d2target/d2target.go @@ -55,10 +55,16 @@ func (diagram Diagram) BoundingBox() (topLeft, bottomRight Point) { y2 := int(math.MinInt32) for _, targetShape := range diagram.Shapes { - x1 = go2.Min(x1, targetShape.Pos.X) - y1 = go2.Min(y1, targetShape.Pos.Y) - x2 = go2.Max(x2, targetShape.Pos.X+targetShape.Width) - y2 = go2.Max(y2, targetShape.Pos.Y+targetShape.Height) + x1 = go2.Min(x1, targetShape.Pos.X-targetShape.StrokeWidth) + y1 = go2.Min(y1, targetShape.Pos.Y-targetShape.StrokeWidth) + x2 = go2.Max(x2, targetShape.Pos.X+targetShape.Width+targetShape.StrokeWidth) + y2 = go2.Max(y2, targetShape.Pos.Y+targetShape.Height+targetShape.StrokeWidth) + + if targetShape.Tooltip != "" || targetShape.Link != "" { + // 16 is the icon radius + y1 = go2.Min(y1, targetShape.Pos.Y-targetShape.StrokeWidth-16) + x2 = go2.Max(x2, targetShape.Pos.X+targetShape.StrokeWidth+targetShape.Width+16) + } if targetShape.Label != "" { labelPosition := label.Position(targetShape.LabelPosition)