From aa1f123f71dfbc5d5f1f46e85a78ad42ac3b407d Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 13 Feb 2023 11:23:54 -0800 Subject: [PATCH] more accurate --- d2layouts/d2dagrelayout/layout.go | 14 ++-- d2layouts/d2elklayout/layout.go | 9 +- e2etests/stable_test.go | 3 + .../dagre/board.exp.json | 18 ++-- .../dagre/sketch.exp.svg | 6 +- .../icon-containers/dagre/board.exp.json | 82 +++++++++++++++---- .../icon-containers/dagre/sketch.exp.svg | 6 +- .../stable/icon-containers/elk/board.exp.json | 78 +++++++++++++++--- .../stable/icon-containers/elk/sketch.exp.svg | 6 +- .../container_icon_label/dagre/board.exp.json | 32 ++++---- .../container_icon_label/dagre/sketch.exp.svg | 6 +- .../container_icon_label/elk/board.exp.json | 20 ++--- .../container_icon_label/elk/sketch.exp.svg | 6 +- 13 files changed, 200 insertions(+), 86 deletions(-) diff --git a/d2layouts/d2dagrelayout/layout.go b/d2layouts/d2dagrelayout/layout.go index d4f26c187..d1c91f2ed 100644 --- a/d2layouts/d2dagrelayout/layout.go +++ b/d2layouts/d2dagrelayout/layout.go @@ -109,17 +109,20 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err maxContainerLabelHeight := 0 for _, obj := range g.Objects { - if len(obj.ChildrenArray) == 0 { + if len(obj.ChildrenArray) == 0 || obj.Parent == g.Root { continue } if obj.LabelHeight != nil { maxContainerLabelHeight = go2.Max(maxContainerLabelHeight, *obj.LabelHeight+label.PADDING) } - if obj.Attributes.Icon != nil { - iconSize := d2target.GetIconSize(obj.Box, string(label.InsideTopLeft)) + if obj.Attributes.Icon != nil && obj.Attributes.Shape.Value != d2target.ShapeImage { + contentBox := geo.NewBox(geo.NewPoint(0, 0), float64(obj.Width), float64(obj.Height)) + shapeType := d2target.DSL_SHAPE_TO_SHAPE_TYPE[obj.Attributes.Shape.Value] + s := shape.NewShape(shapeType, contentBox) + iconSize := d2target.GetIconSize(s.GetInnerBox(), string(label.InsideTopLeft)) // Since dagre container labels are pushed up, we don't want a child container to collide - maxContainerLabelHeight = go2.Max(maxContainerLabelHeight, (iconSize+label.PADDING*2)*3) + maxContainerLabelHeight = go2.Max(maxContainerLabelHeight, (iconSize+label.PADDING*2)*2) } } @@ -226,7 +229,8 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err } if obj.Attributes.Icon != nil { if len(obj.ChildrenArray) > 0 { - obj.IconPosition = go2.Pointer(string(label.InsideTopLeft)) + obj.IconPosition = go2.Pointer(string(label.OutsideTopLeft)) + obj.LabelPosition = go2.Pointer(string(label.OutsideTopRight)) } else { obj.IconPosition = go2.Pointer(string(label.InsideMiddleCenter)) } diff --git a/d2layouts/d2elklayout/layout.go b/d2layouts/d2elklayout/layout.go index bd69f539e..7e1703b05 100644 --- a/d2layouts/d2elklayout/layout.go +++ b/d2layouts/d2elklayout/layout.go @@ -186,7 +186,6 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err } if len(obj.ChildrenArray) > 0 { - n.LayoutOptions = &elkOpts{ ForceNodeModelOrder: true, Thoroughness: 8, @@ -207,8 +206,11 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err if obj.LabelHeight != nil { paddingTop = go2.Max(paddingTop, *obj.LabelHeight+label.PADDING) } - if obj.Attributes.Icon != nil { - iconSize := d2target.GetIconSize(obj.Box, string(label.InsideTopLeft)) + if obj.Attributes.Icon != nil && obj.Attributes.Shape.Value != d2target.ShapeImage { + contentBox := geo.NewBox(geo.NewPoint(0, 0), float64(n.Width), float64(n.Height)) + shapeType := d2target.DSL_SHAPE_TO_SHAPE_TYPE[obj.Attributes.Shape.Value] + s := shape.NewShape(shapeType, contentBox) + iconSize := d2target.GetIconSize(s.GetInnerBox(), string(label.InsideTopLeft)) paddingTop = go2.Max(paddingTop, iconSize+label.PADDING*2) } n.LayoutOptions.Padding = fmt.Sprintf("[top=%d,left=50,bottom=50,right=50]", @@ -328,6 +330,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err if obj.Attributes.Icon != nil { if len(obj.ChildrenArray) > 0 { obj.IconPosition = go2.Pointer(string(label.InsideTopLeft)) + obj.LabelPosition = go2.Pointer(string(label.InsideTopRight)) } else { obj.IconPosition = go2.Pointer(string(label.InsideMiddleCenter)) } diff --git a/e2etests/stable_test.go b/e2etests/stable_test.go index 1c82b78d8..7f9666e9e 100644 --- a/e2etests/stable_test.go +++ b/e2etests/stable_test.go @@ -920,6 +920,9 @@ a -> b font-color: purple fill: "#e1d5e7" } + ec2: EC2 Instance { + icon: https://icons.terrastruct.com/aws%2FCompute%2F_Instance%2FAmazon-EC2_C4-Instance_light-bg.svg + } } } } diff --git a/e2etests/testdata/stable/font_sizes_containers_large/dagre/board.exp.json b/e2etests/testdata/stable/font_sizes_containers_large/dagre/board.exp.json index 74443f63c..22a8aacd9 100644 --- a/e2etests/testdata/stable/font_sizes_containers_large/dagre/board.exp.json +++ b/e2etests/testdata/stable/font_sizes_containers_large/dagre/board.exp.json @@ -7,10 +7,10 @@ "type": "rectangle", "pos": { "x": 0, - "y": 65 + "y": 50 }, "width": 264, - "height": 511, + "height": 406, "opacity": 1, "strokeDash": 0, "strokeWidth": 2, @@ -48,10 +48,10 @@ "type": "rectangle", "pos": { "x": 20, - "y": 162 + "y": 125 }, "width": 224, - "height": 381, + "height": 306, "opacity": 1, "strokeDash": 0, "strokeWidth": 2, @@ -89,10 +89,10 @@ "type": "rectangle", "pos": { "x": 40, - "y": 241 + "y": 196 }, "width": 184, - "height": 270, + "height": 210, "opacity": 1, "strokeDash": 0, "strokeWidth": 2, @@ -130,10 +130,10 @@ "type": "rectangle", "pos": { "x": 60, - "y": 309 + "y": 249 }, "width": 144, - "height": 160, + "height": 130, "opacity": 1, "strokeDash": 0, "strokeWidth": 2, @@ -171,7 +171,7 @@ "type": "rectangle", "pos": { "x": 100, - "y": 361 + "y": 286 }, "width": 64, "height": 56, diff --git a/e2etests/testdata/stable/font_sizes_containers_large/dagre/sketch.exp.svg b/e2etests/testdata/stable/font_sizes_containers_large/dagre/sketch.exp.svg index 61db3ed3b..167c42957 100644 --- a/e2etests/testdata/stable/font_sizes_containers_large/dagre/sketch.exp.svg +++ b/e2etests/testdata/stable/font_sizes_containers_large/dagre/sketch.exp.svg @@ -3,7 +3,7 @@ id="d2-svg" style="background: white;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" -width="652" height="843" viewBox="-194 -165 652 843">