From 609b3e5eb3354e3accbd11f5b57c79857d16fd00 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Thu, 2 Mar 2023 14:52:30 -0800 Subject: [PATCH] move person labels to outside bottom --- d2graph/d2graph.go | 20 +++++++++++++++++++- d2layouts/d2dagrelayout/layout.go | 4 ++-- d2layouts/d2elklayout/layout.go | 4 ++-- d2layouts/d2near/layout.go | 3 +-- d2layouts/d2sequence/sequence_diagram.go | 3 +-- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index 265f81c1b..22a6d935b 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -447,6 +447,18 @@ func (obj *Object) IsContainer() bool { return len(obj.Children) > 0 } +func (obj *Object) HasOutsideBottomLabel() bool { + if obj == nil || obj.Attributes == nil { + return false + } + switch obj.Attributes.Shape.Value { + case d2target.ShapeImage, d2target.ShapePerson: + return true + default: + return false + } +} + func (obj *Object) AbsID() string { if obj.Parent != nil && obj.Parent.ID != "" { return obj.Parent.AbsID() + "." + obj.ID @@ -1326,7 +1338,13 @@ func (g *Graph) SetDimensions(mtexts []*d2target.MText, ruler *textmeasure.Ruler } } - fitWidth, fitHeight := s.GetDimensionsToFit(contentBox.Width, contentBox.Height, paddingX, paddingY) + var fitWidth, fitHeight float64 + if shapeType == shape.PERSON_TYPE { + fitWidth = contentBox.Width + paddingX + fitHeight = contentBox.Height + paddingY + } else { + fitWidth, fitHeight = s.GetDimensionsToFit(contentBox.Width, contentBox.Height, paddingX, paddingY) + } obj.Width = math.Max(float64(desiredWidth), fitWidth) obj.Height = math.Max(float64(desiredHeight), fitHeight) if s.AspectRatio1() { diff --git a/d2layouts/d2dagrelayout/layout.go b/d2layouts/d2dagrelayout/layout.go index 2db597de6..1c4793928 100644 --- a/d2layouts/d2dagrelayout/layout.go +++ b/d2layouts/d2dagrelayout/layout.go @@ -149,7 +149,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err height := obj.Height if obj.LabelWidth != nil && obj.LabelHeight != nil { - if obj.Attributes.Shape.Value == d2target.ShapeImage || obj.Attributes.Icon != nil { + if obj.HasOutsideBottomLabel() || obj.Attributes.Icon != nil { height += float64(*obj.LabelHeight) + label.PADDING } if len(obj.ChildrenArray) > 0 { @@ -217,7 +217,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err if obj.LabelWidth != nil && obj.LabelHeight != nil { if len(obj.ChildrenArray) > 0 { obj.LabelPosition = go2.Pointer(string(label.OutsideTopCenter)) - } else if obj.Attributes.Shape.Value == d2target.ShapeImage { + } else if obj.HasOutsideBottomLabel() { obj.LabelPosition = go2.Pointer(string(label.OutsideBottomCenter)) // remove the extra height we added to the node when passing to dagre obj.Height -= float64(*obj.LabelHeight) + label.PADDING diff --git a/d2layouts/d2elklayout/layout.go b/d2layouts/d2elklayout/layout.go index a413923c3..9c55caf9c 100644 --- a/d2layouts/d2elklayout/layout.go +++ b/d2layouts/d2elklayout/layout.go @@ -179,7 +179,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err height := obj.Height width := obj.Width if obj.LabelWidth != nil && obj.LabelHeight != nil { - if obj.Attributes.Shape.Value == d2target.ShapeImage || obj.Attributes.Icon != nil { + if obj.HasOutsideBottomLabel() || obj.Attributes.Icon != nil { height += float64(*obj.LabelHeight) + label.PADDING } width = go2.Max(width, float64(*obj.LabelWidth)) @@ -332,7 +332,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err if obj.LabelWidth != nil && obj.LabelHeight != nil { if len(obj.ChildrenArray) > 0 { obj.LabelPosition = go2.Pointer(string(label.InsideTopCenter)) - } else if obj.Attributes.Shape.Value == d2target.ShapeImage { + } else if obj.HasOutsideBottomLabel() { obj.LabelPosition = go2.Pointer(string(label.OutsideBottomCenter)) obj.Height -= float64(*obj.LabelHeight) + label.PADDING } else if obj.Attributes.Icon != nil { diff --git a/d2layouts/d2near/layout.go b/d2layouts/d2near/layout.go index 8a57e9a3f..082a0e296 100644 --- a/d2layouts/d2near/layout.go +++ b/d2layouts/d2near/layout.go @@ -8,7 +8,6 @@ import ( "strings" "oss.terrastruct.com/d2/d2graph" - "oss.terrastruct.com/d2/d2target" "oss.terrastruct.com/d2/lib/geo" "oss.terrastruct.com/d2/lib/label" "oss.terrastruct.com/util-go/go2" @@ -43,7 +42,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, constantNears []*d2graph.Obje // These shapes skipped core layout, which means they also skipped label placements for _, obj := range constantNears { - if obj.Attributes.Shape.Value == d2target.ShapeImage { + if obj.HasOutsideBottomLabel() { obj.LabelPosition = go2.Pointer(string(label.OutsideBottomCenter)) } else if obj.Attributes.Icon != nil { obj.LabelPosition = go2.Pointer(string(label.InsideTopCenter)) diff --git a/d2layouts/d2sequence/sequence_diagram.go b/d2layouts/d2sequence/sequence_diagram.go index 98129222c..a6e13b9e7 100644 --- a/d2layouts/d2sequence/sequence_diagram.go +++ b/d2layouts/d2sequence/sequence_diagram.go @@ -335,9 +335,8 @@ func (sd *sequenceDiagram) adjustGroupLabel(group *d2graph.Object) { func (sd *sequenceDiagram) placeActors() { centerX := sd.actors[0].Width / 2. for rank, actor := range sd.actors { - shape := actor.Attributes.Shape.Value var yOffset float64 - if shape == d2target.ShapeImage || shape == d2target.ShapePerson { + if actor.HasOutsideBottomLabel() { actor.LabelPosition = go2.Pointer(string(label.OutsideBottomCenter)) yOffset = sd.maxActorHeight - actor.Height if actor.LabelHeight != nil {