package d2dagrelayout import ( "context" _ "embed" "encoding/json" "fmt" "math" "regexp" "sort" "strings" "cdr.dev/slog" "github.com/dop251/goja" "oss.terrastruct.com/util-go/xdefer" "oss.terrastruct.com/util-go/go2" "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/d2/lib/log" ) //go:embed setup.js var setupJS string //go:embed dagre.js var dagreJS string const ( MIN_RANK_SEP = 60 EDGE_LABEL_GAP = 20 ) type ConfigurableOpts struct { NodeSep int `json:"nodesep"` EdgeSep int `json:"edgesep"` } var DefaultOpts = ConfigurableOpts{ NodeSep: 60, EdgeSep: 20, } type DagreNode struct { ID string `json:"id"` X float64 `json:"x"` Y float64 `json:"y"` Width float64 `json:"width"` Height float64 `json:"height"` } type DagreEdge struct { Points []*geo.Point `json:"points"` } type dagreOpts struct { // for a top to bottom graph: ranksep is y spacing, nodesep is x spacing, edgesep is x spacing ranksep int // graph direction: tb (top to bottom)| bt | lr | rl rankdir string ConfigurableOpts } func DefaultLayout(ctx context.Context, g *d2graph.Graph) (err error) { return Layout(ctx, g, nil) } func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err error) { if opts == nil { opts = &DefaultOpts } defer xdefer.Errorf(&err, "failed to dagre layout") debugJS := false vm := goja.New() if _, err := vm.RunString(dagreJS); err != nil { return err } if _, err := vm.RunString(setupJS); err != nil { return err } rootAttrs := dagreOpts{ ConfigurableOpts: ConfigurableOpts{ EdgeSep: opts.EdgeSep, NodeSep: opts.NodeSep, }, } isHorizontal := false switch g.Root.Direction.Value { case "down": rootAttrs.rankdir = "TB" case "right": rootAttrs.rankdir = "LR" isHorizontal = true case "left": rootAttrs.rankdir = "RL" isHorizontal = true case "up": rootAttrs.rankdir = "BT" default: rootAttrs.rankdir = "TB" } // set label and icon positions for dagre for _, obj := range g.Objects { positionLabelsIcons(obj) } maxContainerLabelHeight := 0 for _, obj := range g.Objects { // TODO count root level container label sizes for ranksep if len(obj.ChildrenArray) == 0 || obj.Parent == g.Root { continue } if obj.HasLabel() { maxContainerLabelHeight = go2.Max(maxContainerLabelHeight, obj.LabelDimensions.Height+label.PADDING) } if obj.Icon != nil && obj.Shape.Value != d2target.ShapeImage { s := obj.ToShape() 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)*2) } } maxLabelWidth := 0 maxLabelHeight := 0 for _, edge := range g.Edges { width := edge.LabelDimensions.Width height := edge.LabelDimensions.Height maxLabelWidth = go2.Max(maxLabelWidth, width) maxLabelHeight = go2.Max(maxLabelHeight, height) } if !isHorizontal { rootAttrs.ranksep = go2.Max(go2.Max(100, maxLabelHeight+40), maxContainerLabelHeight) } else { rootAttrs.ranksep = go2.Max(100, maxLabelWidth+40) // use existing config rootAttrs.NodeSep = rootAttrs.EdgeSep // configure vertical padding rootAttrs.EdgeSep = go2.Max(maxLabelHeight+40, maxContainerLabelHeight) // Note: non-containers have both of these as padding (rootAttrs.NodeSep + rootAttrs.EdgeSep) } configJS := setGraphAttrs(rootAttrs) if _, err := vm.RunString(configJS); err != nil { return err } loadScript := "" idToObj := make(map[string]*d2graph.Object) idToWidth := make(map[string]float64) idToHeight := make(map[string]float64) for _, obj := range g.Objects { id := obj.AbsID() idToObj[id] = obj // width, height := adjustDimensions(obj) width, height := obj.Width, obj.Height idToWidth[id] = width idToHeight[id] = height loadScript += generateAddNodeLine(id, int(width), int(height)) if obj.Parent != g.Root { loadScript += generateAddParentLine(id, obj.Parent.AbsID()) } } // for _, obj := range g.Objects { // if !obj.IsContainer() { // continue // } // id := obj.AbsID() // // add phantom children to adjust container dimensions // // phantomID := id + "___phantom" // // widthDelta := int(math.Ceil(idToWidth[id] - obj.Width)) // height := int(math.Ceil(idToHeight[id])) // // when a container has nodes with no connections, the layout will be in a row // // adding a node will add NodeSep width in addition to the node's width // // to add a specific amount of space we need to subtract this from the desired width // // if we add the phantom node at rank 0 it should be at the far right and top // // xSpace := rootAttrs.NodeSep // ySpace := rootAttrs.ranksep // if false { // maxChildHeight := math.Inf(-1) // for _, c := range obj.ChildrenArray { // if c.Height > maxChildHeight { // maxChildHeight = c.Height // } // if c.Shape.Value == d2target.ShapeImage || c.IconPosition == nil { // continue // } // h := c.Height // switch label.Position(*c.IconPosition) { // case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight, // label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight: // h += d2target.MAX_ICON_SIZE + 2*label.PADDING // } // if h > maxChildHeight { // maxChildHeight = h // } // } // // adjust for children with outside positioned icons // var hasTop, hasBottom bool // for _, child := range obj.ChildrenArray { // if child.Shape.Value == d2target.ShapeImage || child.IconPosition == nil { // continue // } // switch label.Position(*child.IconPosition) { // case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight: // hasTop = true // case label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight: // hasBottom = true // } // if hasTop && hasBottom { // break // } // } // if hasTop || hasBottom { // // TODO ranksep is already accounting for maxLabelHeight // // maxChildHeight += d2target.MAX_ICON_SIZE + 2*label.PADDING // } // height = go2.Max(height, ySpace+int(maxChildHeight)) // } // } for _, edge := range g.Edges { src, dst := getEdgeEndpoints(g, edge) width := edge.LabelDimensions.Width height := edge.LabelDimensions.Height numEdges := 0 for _, e := range g.Edges { otherSrc, otherDst := getEdgeEndpoints(g, e) if (otherSrc == src && otherDst == dst) || (otherSrc == dst && otherDst == src) { numEdges++ } } // We want to leave some gap between multiple edges if numEdges > 1 { switch g.Root.Direction.Value { case "down", "up", "": width += EDGE_LABEL_GAP case "left", "right": height += EDGE_LABEL_GAP } } loadScript += generateAddEdgeLine(src.AbsID(), dst.AbsID(), edge.AbsID(), width, height) } if debugJS { log.Debug(ctx, "script", slog.F("all", setupJS+configJS+loadScript)) } if _, err := vm.RunString(loadScript); err != nil { return err } if _, err := vm.RunString(`dagre.layout(g)`); err != nil { if debugJS { log.Warn(ctx, "layout error", slog.F("err", err)) } return err } for i := range g.Objects { val, err := vm.RunString(fmt.Sprintf("JSON.stringify(g.node(g.nodes()[%d]))", i)) if err != nil { return err } var dn DagreNode if err := json.Unmarshal([]byte(val.String()), &dn); err != nil { return err } if debugJS { log.Debug(ctx, "graph", slog.F("json", dn)) } obj := idToObj[dn.ID] // dagre gives center of node obj.TopLeft = geo.NewPoint(math.Round(dn.X-dn.Width/2), math.Round(dn.Y-dn.Height/2)) obj.Width = math.Ceil(dn.Width) obj.Height = math.Ceil(dn.Height) } for i, edge := range g.Edges { val, err := vm.RunString(fmt.Sprintf("JSON.stringify(g.edge(g.edges()[%d]))", i)) if err != nil { return err } var de DagreEdge if err := json.Unmarshal([]byte(val.String()), &de); err != nil { return err } if debugJS { log.Debug(ctx, "graph", slog.F("json", de)) } points := make([]*geo.Point, len(de.Points)) for i := range de.Points { if edge.SrcArrow && !edge.DstArrow { points[len(de.Points)-i-1] = de.Points[i].Copy() } else { points[i] = de.Points[i].Copy() } } startIndex, endIndex := 0, len(points)-1 start, end := points[startIndex], points[endIndex] // chop where edge crosses the source/target boxes since container edges were routed to a descendant if edge.Src != edge.Dst { for i := 1; i < len(points); i++ { segment := *geo.NewSegment(points[i-1], points[i]) if intersections := edge.Src.Box.Intersections(segment); len(intersections) > 0 { start = intersections[0] startIndex = i - 1 } if intersections := edge.Dst.Box.Intersections(segment); len(intersections) > 0 { end = intersections[0] endIndex = i break } } } points = points[startIndex : endIndex+1] points[0] = start points[len(points)-1] = end edge.Route = points } ranks, objectRanks := getRanks(g, isHorizontal) if ranks != nil && objectRanks != nil { fmt.Printf("got ranks: %v\n", ranks) } tops, centers, bottoms := getPositions(ranks, isHorizontal) if tops != nil { fmt.Printf("got tops: %v\ncenters: %v\nbottoms: %v\n", tops, centers, bottoms) fmt.Printf("spacing: ") for i := 1; i < len(tops); i++ { fmt.Printf("%v, ", tops[i]-bottoms[i-1]) } fmt.Printf("\n") } fmt.Printf("ranksep %v, nodesep %v\n", rootAttrs.ranksep, rootAttrs.NodeSep) // TODO // 1. Compute all current spacings // 2. Compute desired spacings // 3. Apply changes (shifting anything below) // // Two kinds of spacing, 1. rank spacing, 2. rank alignment spacing // all objects at a rank are center aligned, if one is much taller, then the rest will have more spacing to align with the taller node // if there is extra spacing due to rank alignment, we may not need to increase rank spacing // for now, just applying spacing increase for whole rank // shifting bottom rank down first, then moving up to next rank for i := len(ranks) - 1; i >= 0; i-- { objects := ranks[i] topSpacing := 0. for _, obj := range objects { _, adjustedHeight := adjustDimensions(obj) // TODO width topSpacing = math.Max(topSpacing, adjustedHeight-obj.Height) } fmt.Printf("rank %d topSpacing %v\n", i, topSpacing) // shiftDown(g, tops[i], topSpacing) // TODO: Testing shiftDown(g, tops[i], float64(100)) } for _, obj := range []*d2graph.Object{} { if !obj.HasLabel() || len(obj.ChildrenArray) == 0 { continue } // usually you don't want to take away here more than what was added, which is the label height // however, if the label height is more than the ranksep/2, we'll have no padding around children anymore // so cap the amount taken off at ranksep/2 subtract := float64(go2.Min(rootAttrs.ranksep/2, obj.LabelDimensions.Height+label.PADDING)) obj.Height -= subtract // If the edge is connected to two descendants that are about to be downshifted, their whole route gets downshifted movedEdges := make(map[*d2graph.Edge]struct{}) for _, e := range g.Edges { isSrcDesc := e.Src.IsDescendantOf(obj) isDstDesc := e.Dst.IsDescendantOf(obj) if isSrcDesc && isDstDesc { stepSize := subtract if e.Src != obj || e.Dst != obj { stepSize /= 2. } movedEdges[e] = struct{}{} for _, p := range e.Route { p.Y += stepSize } } } q := []*d2graph.Object{obj} // Downshift descendants and edges that have one endpoint connected to a descendant for len(q) > 0 { curr := q[0] q = q[1:] stepSize := subtract // The object itself needs to move down the height it was just subtracted // all descendants move half, to maintain vertical padding if curr != obj { stepSize /= 2. } curr.TopLeft.Y += stepSize almostEqual := func(a, b float64) bool { return b-1 <= a && a <= b+1 } shouldMove := func(p *geo.Point) bool { if curr != obj { return true } if isHorizontal { // Only move horizontal edges if they are connected to the top side of the shrinking container return almostEqual(p.Y, obj.TopLeft.Y-stepSize) } else { // Edge should only move if it's not connected to the bottom side of the shrinking container return !almostEqual(p.Y, obj.TopLeft.Y+obj.Height) } } for _, e := range g.Edges { if _, ok := movedEdges[e]; ok { continue } moveWholeEdge := false if e.Src == curr { // Don't move src points on side of container if almostEqual(e.Route[0].X, obj.TopLeft.X) || almostEqual(e.Route[0].X, obj.TopLeft.X+obj.Width) { // Unless the dst is also on a container if !e.Dst.HasLabel() || len(e.Dst.ChildrenArray) <= 0 { continue } } if shouldMove(e.Route[0]) { if isHorizontal && e.Src.Parent != g.Root && e.Dst.Parent != g.Root { moveWholeEdge = true } else { e.ShiftStart(stepSize, false) } } } if !moveWholeEdge && e.Dst == curr { if shouldMove(e.Route[len(e.Route)-1]) { if isHorizontal && e.Dst.Parent != g.Root && e.Src.Parent != g.Root { moveWholeEdge = true } else { e.ShiftEnd(stepSize, false) } } } if moveWholeEdge { for _, p := range e.Route { p.Y += stepSize / 2. } movedEdges[e] = struct{}{} } } q = append(q, curr.ChildrenArray...) } } // for _, obj := range g.Objects { for _, obj := range []*d2graph.Object{} { cleanupAdjustment(obj) } for _, edge := range g.Edges { points := edge.Route startIndex, endIndex := 0, len(points)-1 start, end := points[startIndex], points[endIndex] // arrowheads can appear broken if segments are very short from dagre routing a point just outside the shape // to fix this, we try extending the previous segment into the shape instead of having a very short segment if !start.Equals(points[0]) && startIndex+2 < len(points) { newStartingSegment := *geo.NewSegment(start, points[startIndex+1]) if newStartingSegment.Length() < d2graph.MIN_SEGMENT_LEN { // we don't want a very short segment right next to the source because it will mess up the arrowhead // instead we want to extend the next segment into the shape border if possible nextStart := points[startIndex+1] nextEnd := points[startIndex+2] // Note: in other direction to extend towards source nextSegment := *geo.NewSegment(nextStart, nextEnd) v := nextSegment.ToVector() extendedStart := nextEnd.ToVector().Add(v.AddLength(d2graph.MIN_SEGMENT_LEN)).ToPoint() extended := *geo.NewSegment(nextEnd, extendedStart) if intersections := edge.Src.Box.Intersections(extended); len(intersections) > 0 { startIndex++ points[startIndex] = intersections[0] start = points[startIndex] } } } if !end.Equals(points[len(points)-1]) && endIndex-2 >= 0 { newEndingSegment := *geo.NewSegment(end, points[endIndex-1]) if newEndingSegment.Length() < d2graph.MIN_SEGMENT_LEN { // extend the prev segment into the shape border if possible prevStart := points[endIndex-2] prevEnd := points[endIndex-1] prevSegment := *geo.NewSegment(prevStart, prevEnd) v := prevSegment.ToVector() extendedEnd := prevStart.ToVector().Add(v.AddLength(d2graph.MIN_SEGMENT_LEN)).ToPoint() extended := *geo.NewSegment(prevStart, extendedEnd) if intersections := edge.Dst.Box.Intersections(extended); len(intersections) > 0 { endIndex-- points[endIndex] = intersections[0] end = points[endIndex] } } } var originalSrcTL, originalDstTL *geo.Point // if the edge passes through 3d/multiple, use the offset box for tracing to border if srcDx, srcDy := edge.Src.GetModifierElementAdjustments(); srcDx != 0 || srcDy != 0 { if start.X > edge.Src.TopLeft.X+srcDx && start.Y < edge.Src.TopLeft.Y+edge.Src.Height-srcDy { originalSrcTL = edge.Src.TopLeft.Copy() edge.Src.TopLeft.X += srcDx edge.Src.TopLeft.Y -= srcDy } } if dstDx, dstDy := edge.Dst.GetModifierElementAdjustments(); dstDx != 0 || dstDy != 0 { if end.X > edge.Dst.TopLeft.X+dstDx && end.Y < edge.Dst.TopLeft.Y+edge.Dst.Height-dstDy { originalDstTL = edge.Dst.TopLeft.Copy() edge.Dst.TopLeft.X += dstDx edge.Dst.TopLeft.Y -= dstDy } } startIndex, endIndex = edge.TraceToShape(points, startIndex, endIndex) points = points[startIndex : endIndex+1] // build a curved path from the dagre route vectors := make([]geo.Vector, 0, len(points)-1) for i := 1; i < len(points); i++ { vectors = append(vectors, points[i-1].VectorTo(points[i])) } path := make([]*geo.Point, 0) path = append(path, points[0]) if len(vectors) > 1 { path = append(path, points[0].AddVector(vectors[0].Multiply(.8))) for i := 1; i < len(vectors)-2; i++ { p := points[i] v := vectors[i] path = append(path, p.AddVector(v.Multiply(.2))) path = append(path, p.AddVector(v.Multiply(.5))) path = append(path, p.AddVector(v.Multiply(.8))) } path = append(path, points[len(points)-2].AddVector(vectors[len(vectors)-1].Multiply(.2))) edge.IsCurve = true } path = append(path, points[len(points)-1]) edge.Route = path // compile needs to assign edge label positions if edge.Label.Value != "" { edge.LabelPosition = go2.Pointer(string(label.InsideMiddleCenter)) } // undo 3d/multiple offset if originalSrcTL != nil { edge.Src.TopLeft.X = originalSrcTL.X edge.Src.TopLeft.Y = originalSrcTL.Y } if originalDstTL != nil { edge.Dst.TopLeft.X = originalDstTL.X edge.Dst.TopLeft.Y = originalDstTL.Y } } return nil } func getEdgeEndpoints(g *d2graph.Graph, edge *d2graph.Edge) (*d2graph.Object, *d2graph.Object) { // dagre doesn't work with edges to containers so we connect container edges to their first child instead (going all the way down) // we will chop the edge where it intersects the container border so it only shows the edge from the container src := edge.Src for len(src.Children) > 0 && src.Class == nil && src.SQLTable == nil { // We want to get the bottom node of sources, setting its rank higher than all children src = getLongestEdgeChainTail(g, src) } dst := edge.Dst for len(dst.Children) > 0 && dst.Class == nil && dst.SQLTable == nil { dst = getLongestEdgeChainHead(g, dst) } if edge.SrcArrow && !edge.DstArrow { // for `b <- a`, edge.Edge is `a -> b` and we expect this routing result src, dst = dst, src } return src, dst } func setGraphAttrs(attrs dagreOpts) string { return fmt.Sprintf(`g.setGraph({ ranksep: %d, edgesep: %d, nodesep: %d, rankdir: "%s", }); `, attrs.ranksep, attrs.ConfigurableOpts.EdgeSep, attrs.ConfigurableOpts.NodeSep, attrs.rankdir, ) } func escapeID(id string) string { // fixes \\ id = strings.ReplaceAll(id, "\\", `\\`) // replaces \n with \\n whenever \n is not preceded by \ (does not replace \\n) re := regexp.MustCompile(`[^\\]\n`) id = re.ReplaceAllString(id, `\\n`) // avoid an unescaped \r becoming a \n in the layout result id = strings.ReplaceAll(id, "\r", `\r`) return id } func generateAddNodeLine(id string, width, height int) string { id = escapeID(id) return fmt.Sprintf("g.setNode(`%s`, { id: `%s`, width: %d, height: %d });\n", id, id, width, height) } func generateAddParentLine(childID, parentID string) string { return fmt.Sprintf("g.setParent(`%s`, `%s`);\n", escapeID(childID), escapeID(parentID)) } func generateAddEdgeLine(fromID, toID, edgeID string, width, height int) string { return fmt.Sprintf("g.setEdge({v:`%s`, w:`%s`, name:`%s`}, { width:%d, height:%d, labelpos: `c` });\n", escapeID(fromID), escapeID(toID), escapeID(edgeID), width, height) } // getLongestEdgeChainHead finds the longest chain in a container and gets its head // If there are multiple chains of the same length, get the head closest to the center func getLongestEdgeChainHead(g *d2graph.Graph, container *d2graph.Object) *d2graph.Object { rank := make(map[*d2graph.Object]int) chainLength := make(map[*d2graph.Object]int) for _, obj := range container.ChildrenArray { isHead := true for _, e := range g.Edges { if inContainer(e.Src, container) != nil && inContainer(e.Dst, obj) != nil { isHead = false break } } if !isHead { continue } rank[obj] = 1 chainLength[obj] = 1 // BFS queue := []*d2graph.Object{obj} visited := make(map[*d2graph.Object]struct{}) for len(queue) > 0 { curr := queue[0] queue = queue[1:] if _, ok := visited[curr]; ok { continue } visited[curr] = struct{}{} for _, e := range g.Edges { child := inContainer(e.Dst, container) if child == curr { continue } if child != nil && inContainer(e.Src, curr) != nil { if rank[curr]+1 > rank[child] { rank[child] = rank[curr] + 1 chainLength[obj] = go2.Max(chainLength[obj], rank[child]) } queue = append(queue, child) } } } } max := int(math.MinInt32) for _, obj := range container.ChildrenArray { if chainLength[obj] > max { max = chainLength[obj] } } var heads []*d2graph.Object for i, obj := range container.ChildrenArray { if rank[obj] == 1 && chainLength[obj] == max { heads = append(heads, container.ChildrenArray[i]) } } if len(heads) > 0 { return heads[int(math.Floor(float64(len(heads))/2.0))] } return container.ChildrenArray[0] } // getLongestEdgeChainTail gets the node at the end of the longest edge chain, because that will be the end of the container // and is what external connections should connect with. // If there are multiple of same length, get the one closest to the middle func getLongestEdgeChainTail(g *d2graph.Graph, container *d2graph.Object) *d2graph.Object { rank := make(map[*d2graph.Object]int) for _, obj := range container.ChildrenArray { isHead := true for _, e := range g.Edges { if inContainer(e.Src, container) != nil && inContainer(e.Dst, obj) != nil { isHead = false break } } if !isHead { continue } rank[obj] = 1 // BFS queue := []*d2graph.Object{obj} visited := make(map[*d2graph.Object]struct{}) for len(queue) > 0 { curr := queue[0] queue = queue[1:] if _, ok := visited[curr]; ok { continue } visited[curr] = struct{}{} for _, e := range g.Edges { child := inContainer(e.Dst, container) if child == curr { continue } if child != nil && inContainer(e.Src, curr) != nil { rank[child] = go2.Max(rank[child], rank[curr]+1) queue = append(queue, child) } } } } max := int(math.MinInt32) for _, obj := range container.ChildrenArray { if rank[obj] > max { max = rank[obj] } } var tails []*d2graph.Object for i, obj := range container.ChildrenArray { if rank[obj] == max { tails = append(tails, container.ChildrenArray[i]) } } return tails[int(math.Floor(float64(len(tails))/2.0))] } func inContainer(obj, container *d2graph.Object) *d2graph.Object { if obj == nil { return nil } if obj == container { return obj } if obj.Parent == container { return obj } return inContainer(obj.Parent, container) } func adjustDimensions(obj *d2graph.Object) (width, height float64) { width = obj.Width height = obj.Height // reserve spacing for labels if obj.HasLabel() { var position label.Position if obj.LabelPosition != nil { position = label.Position(*obj.LabelPosition) } else if len(obj.ChildrenArray) == 0 && obj.HasOutsideBottomLabel() { position = label.OutsideBottomCenter } if position.IsShapePosition() { adjustedWidth := false if obj.IsContainer() { switch position { case label.InsideMiddleLeft, label.InsideMiddleRight: width += float64(obj.LabelDimensions.Width) + label.PADDING adjustedWidth = true } } if !adjustedWidth { switch position { case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom, label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom: width += float64(obj.LabelDimensions.Width) + label.PADDING default: // TODO labelWidth+2*label.PADDING width = go2.Max(width, float64(obj.LabelDimensions.Width)) } } } } hasIconAboveBelow := false if obj.Icon != nil && obj.Shape.Value != d2target.ShapeImage { var position label.Position if obj.IconPosition != nil { position = label.Position(*obj.IconPosition) } if position.IsShapePosition() { adjustedWidth := false if obj.IsContainer() { switch position { case label.InsideMiddleLeft, label.InsideMiddleRight: width += d2target.MAX_ICON_SIZE + label.PADDING adjustedWidth = true } } if !adjustedWidth { switch position { case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom, label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom, label.InsideMiddleLeft, label.InsideMiddleRight: width += d2target.MAX_ICON_SIZE + label.PADDING default: width = go2.Max(width, d2target.MAX_ICON_SIZE+2*label.PADDING) } } switch position { case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight, label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight: hasIconAboveBelow = true } } } if true { // special handling if obj.HasOutsideBottomLabel() || hasIconAboveBelow { height += float64(obj.LabelDimensions.Height) + label.PADDING } } // reserve extra space for 3d/multiple by providing dagre the larger dimensions dx, dy := obj.GetModifierElementAdjustments() width += dx height += dy return } func cleanupAdjustment(obj *d2graph.Object) { // adjust size and position to account for space reserved for labels if obj.HasLabel() { position := label.Position(*obj.LabelPosition) if position.IsShapePosition() { labelWidth := float64(obj.LabelDimensions.Width) + label.PADDING switch position { case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom, label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom: obj.Width -= labelWidth } switch position { case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom: obj.TopLeft.X += labelWidth obj.ShiftDescendants(labelWidth, 0) case label.InsideMiddleLeft: if obj.IsContainer() || obj.Icon != nil { obj.ShiftDescendants(labelWidth, 0) } } } } hasIconAboveBelow := false if obj.Icon != nil && obj.Shape.Value != d2target.ShapeImage { position := label.Position(*obj.IconPosition) if position.IsShapePosition() { iconWidth := float64(d2target.MAX_ICON_SIZE + label.PADDING) switch position { case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom, label.OutsideRightTop, label.OutsideRightMiddle, label.OutsideRightBottom: obj.Width -= iconWidth } switch position { case label.OutsideLeftTop, label.OutsideLeftMiddle, label.OutsideLeftBottom: obj.TopLeft.X += iconWidth obj.ShiftDescendants(iconWidth, 0) case label.InsideMiddleLeft: if obj.IsContainer() || obj.Icon != nil { obj.ShiftDescendants(iconWidth, 0) } } switch position { case label.OutsideTopLeft, label.OutsideTopCenter, label.OutsideTopRight, label.OutsideBottomLeft, label.OutsideBottomCenter, label.OutsideBottomRight: hasIconAboveBelow = true } } } // special handling to start/end connections below label if true { if obj.HasOutsideBottomLabel() || hasIconAboveBelow { dy := float64(obj.LabelDimensions.Height) + label.PADDING obj.Height -= dy if obj.IsContainer() { obj.ShiftDescendants(0, -dy/2) } } } // remove the extra width/height we added for 3d/multiple after all objects/connections are placed // and shift the shapes down accordingly dx, dy := obj.GetModifierElementAdjustments() if dx != 0 || dy != 0 { obj.TopLeft.Y += dy obj.ShiftDescendants(0, dy) if !obj.IsContainer() { obj.Width -= dx obj.Height -= dy } } } func positionLabelsIcons(obj *d2graph.Object) { if obj.Icon != nil && obj.IconPosition == nil { if len(obj.ChildrenArray) > 0 { obj.IconPosition = go2.Pointer(string(label.OutsideTopLeft)) if obj.LabelPosition == nil { obj.LabelPosition = go2.Pointer(string(label.OutsideTopRight)) return } } else { obj.IconPosition = go2.Pointer(string(label.InsideMiddleCenter)) } } if obj.HasLabel() && obj.LabelPosition == nil { if len(obj.ChildrenArray) > 0 { obj.LabelPosition = go2.Pointer(string(label.OutsideTopCenter)) } else if obj.HasOutsideBottomLabel() { obj.LabelPosition = go2.Pointer(string(label.OutsideBottomCenter)) } else if obj.Icon != nil { obj.LabelPosition = go2.Pointer(string(label.InsideTopCenter)) } else { obj.LabelPosition = go2.Pointer(string(label.InsideMiddleCenter)) } } } func getRanks(g *d2graph.Graph, isHorizontal bool) (ranks [][]*d2graph.Object, objectRanks map[*d2graph.Object]int) { alignedObjects := make(map[float64][]*d2graph.Object) for _, obj := range g.Objects { if !obj.IsContainer() { if !isHorizontal { y := obj.TopLeft.Y + obj.Height/2 alignedObjects[y] = append(alignedObjects[y], obj) } else { x := obj.TopLeft.X + obj.Width/2 alignedObjects[x] = append(alignedObjects[x], obj) } } } levels := make([]float64, 0, len(alignedObjects)) for l := range alignedObjects { levels = append(levels, l) } sort.Slice(levels, func(i, j int) bool { return levels[i] < levels[j] }) ranks = make([][]*d2graph.Object, 0, len(levels)) objectRanks = make(map[*d2graph.Object]int) for i, l := range levels { for _, obj := range alignedObjects[l] { objectRanks[obj] = i } ranks = append(ranks, alignedObjects[l]) } for _, obj := range g.Objects { if rank, has := objectRanks[obj]; has { fmt.Printf("%v rank: %d\n", obj.AbsID(), rank) } else { fmt.Printf("%v rank: none\n", obj.AbsID()) } } startingParentRanks := make(map[*d2graph.Object]int) endingParentRanks := make(map[*d2graph.Object]int) for _, obj := range g.Objects { if obj.IsContainer() { continue } r := objectRanks[obj] // update all ancestor's min/max ranks for parent := obj.Parent; parent != nil && parent != g.Root; parent = parent.Parent { if start, has := startingParentRanks[parent]; !has || r < start { startingParentRanks[parent] = r } if end, has := endingParentRanks[parent]; !has || r > end { endingParentRanks[parent] = r } } } for parent, start := range startingParentRanks { fmt.Printf("parent %v start %v end %v\n", parent.AbsID(), start, endingParentRanks[parent]) } return ranks, objectRanks } func getPositions(ranks [][]*d2graph.Object, isHorizontal bool) (tops, centers, bottoms []float64) { for _, objects := range ranks { min := math.Inf(1) max := math.Inf(-1) for _, obj := range objects { if isHorizontal { min = math.Min(min, obj.TopLeft.X) max = math.Max(max, obj.TopLeft.X+obj.Width) } else { min = math.Min(min, obj.TopLeft.Y) max = math.Max(max, obj.TopLeft.Y+obj.Height) } } tops = append(tops, min) if isHorizontal { centers = append(centers, objects[0].TopLeft.X+objects[0].Width/2.) } else { centers = append(centers, objects[0].TopLeft.Y+objects[0].Height/2.) } bottoms = append(bottoms, max) } return } // shift everything down by distance if it is at or below startY func shiftDown(g *d2graph.Graph, startY, distance float64) { for _, obj := range g.Objects { if obj.TopLeft.Y < startY { continue } obj.TopLeft.Y += distance } for _, edge := range g.Edges { for _, p := range edge.Route { // Note: == so incoming edge shifts down with object at startY if p.Y <= startY { continue } p.Y += distance } } }