From e3ac2a41706d293ea7c251cc13ca0e7ec97d9b89 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Thu, 23 Feb 2023 11:32:23 -0800 Subject: [PATCH] move container edge back if it runs into the container's label --- d2layouts/d2dagrelayout/layout.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/d2layouts/d2dagrelayout/layout.go b/d2layouts/d2dagrelayout/layout.go index d46da1e7c..51bb18d62 100644 --- a/d2layouts/d2dagrelayout/layout.go +++ b/d2layouts/d2dagrelayout/layout.go @@ -425,7 +425,27 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err // trace the edge to the specific shape's border points[startIndex] = shape.TraceToShapeBorder(srcShape, start, points[startIndex+1]) - points[endIndex] = shape.TraceToShapeBorder(dstShape, end, points[endIndex-1]) + + // if an edge to a container runs into its label, stop the edge at the label instead + overlapsContainerLabel := false + if edge.Dst.IsContainer() && edge.Dst.Attributes.Label.Value != "" { + // assumes LabelPosition, LabelWidth, LabelHeight are all set if there is a label + labelWidth := float64(*edge.Dst.LabelWidth) + labelHeight := float64(*edge.Dst.LabelHeight) + labelTL := label.Position(*edge.Dst.LabelPosition). + GetPointOnBox(edge.Dst.Box, label.PADDING, labelWidth, labelHeight) + + endingSegment := geo.Segment{Start: points[endIndex-1], End: points[endIndex]} + labelBox := geo.NewBox(labelTL, labelWidth, labelHeight) + if intersections := labelBox.Intersections(endingSegment); len(intersections) > 0 { + overlapsContainerLabel = true + // move ending segment to label intersection point + points[endIndex] = intersections[0] + } + } + if !overlapsContainerLabel { + points[endIndex] = shape.TraceToShapeBorder(dstShape, end, points[endIndex-1]) + } points = points[startIndex : endIndex+1] // build a curved path from the dagre route