This commit is contained in:
Gavin Nishizawa 2023-06-08 14:06:43 -07:00
parent 4dfd5d960d
commit 8d2636a147
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD

View file

@ -494,33 +494,28 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
// if an edge runs into an outside label, stop the edge at the label instead // if an edge runs into an outside label, stop the edge at the label instead
overlapsOutsideLabel := false overlapsOutsideLabel := false
switch srcShape.GetType() { if edge.Src.HasLabel() {
case shape.TEXT_TYPE, shape.TABLE_TYPE, shape.CLASS_TYPE, shape.CODE_TYPE: // assumes LabelPosition, LabelWidth, LabelHeight are all set if there is a label
// labels aren't actually labels labelPosition := label.Position(*edge.Src.LabelPosition)
default: if labelPosition.IsOutside() {
if edge.Src.Label.Value != "" { labelWidth := float64(edge.Src.LabelDimensions.Width)
// assumes LabelPosition, LabelWidth, LabelHeight are all set if there is a label labelHeight := float64(edge.Src.LabelDimensions.Height)
labelPosition := label.Position(*edge.Src.LabelPosition) labelTL := labelPosition.GetPointOnBox(edge.Src.Box, label.PADDING, labelWidth, labelHeight)
if labelPosition.IsOutside() {
labelWidth := float64(edge.Src.LabelDimensions.Width)
labelHeight := float64(edge.Src.LabelDimensions.Height)
labelTL := labelPosition.GetPointOnBox(edge.Src.Box, label.PADDING, labelWidth, labelHeight)
startingSegment := geo.Segment{Start: points[startIndex+1], End: points[startIndex]} startingSegment := geo.Segment{Start: points[startIndex+1], End: points[startIndex]}
labelBox := geo.NewBox(labelTL, labelWidth, labelHeight) labelBox := geo.NewBox(labelTL, labelWidth, labelHeight)
// add left/right padding to box // add left/right padding to box
labelBox.TopLeft.X -= label.PADDING labelBox.TopLeft.X -= label.PADDING
labelBox.Width += 2 * label.PADDING labelBox.Width += 2 * label.PADDING
if intersections := labelBox.Intersections(startingSegment); len(intersections) > 0 { if intersections := labelBox.Intersections(startingSegment); len(intersections) > 0 {
overlapsOutsideLabel = true overlapsOutsideLabel = true
// move starting segment to label intersection point // move starting segment to label intersection point
points[startIndex] = intersections[0] points[startIndex] = intersections[0]
startingSegment.End = intersections[0] startingSegment.End = intersections[0]
// if the segment becomes too short, just merge it with the next segment // if the segment becomes too short, just merge it with the next segment
if startIndex < len(points) && startingSegment.Length() < MIN_SEGMENT_LEN { if startIndex < len(points) && startingSegment.Length() < MIN_SEGMENT_LEN {
points[startIndex+1] = points[startIndex] points[startIndex+1] = points[startIndex]
startIndex++ startIndex++
}
} }
} }
} }
@ -531,33 +526,28 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
} }
overlapsOutsideLabel = false overlapsOutsideLabel = false
switch dstShape.GetType() { if edge.Dst.HasLabel() {
case shape.TEXT_TYPE, shape.TABLE_TYPE, shape.CLASS_TYPE, shape.CODE_TYPE: // assumes LabelPosition, LabelWidth, LabelHeight are all set if there is a label
// labels aren't actually labels labelPosition := label.Position(*edge.Dst.LabelPosition)
default: if labelPosition.IsOutside() {
if edge.Dst.Label.Value != "" { labelWidth := float64(edge.Dst.LabelDimensions.Width)
// assumes LabelPosition, LabelWidth, LabelHeight are all set if there is a label labelHeight := float64(edge.Dst.LabelDimensions.Height)
labelPosition := label.Position(*edge.Dst.LabelPosition) labelTL := labelPosition.GetPointOnBox(edge.Dst.Box, label.PADDING, labelWidth, labelHeight)
if labelPosition.IsOutside() {
labelWidth := float64(edge.Dst.LabelDimensions.Width)
labelHeight := float64(edge.Dst.LabelDimensions.Height)
labelTL := labelPosition.GetPointOnBox(edge.Dst.Box, label.PADDING, labelWidth, labelHeight)
endingSegment := geo.Segment{Start: points[endIndex-1], End: points[endIndex]} endingSegment := geo.Segment{Start: points[endIndex-1], End: points[endIndex]}
labelBox := geo.NewBox(labelTL, labelWidth, labelHeight) labelBox := geo.NewBox(labelTL, labelWidth, labelHeight)
// add left/right padding to box // add left/right padding to box
labelBox.TopLeft.X -= label.PADDING labelBox.TopLeft.X -= label.PADDING
labelBox.Width += 2 * label.PADDING labelBox.Width += 2 * label.PADDING
if intersections := labelBox.Intersections(endingSegment); len(intersections) > 0 { if intersections := labelBox.Intersections(endingSegment); len(intersections) > 0 {
overlapsOutsideLabel = true overlapsOutsideLabel = true
// move ending segment to label intersection point // move ending segment to label intersection point
points[endIndex] = intersections[0] points[endIndex] = intersections[0]
endingSegment.End = intersections[0] endingSegment.End = intersections[0]
// if the segment becomes too short, just merge it with the previous segment // if the segment becomes too short, just merge it with the previous segment
if endIndex-1 > 0 && endingSegment.Length() < MIN_SEGMENT_LEN { if endIndex-1 > 0 && endingSegment.Length() < MIN_SEGMENT_LEN {
points[endIndex-1] = points[endIndex] points[endIndex-1] = points[endIndex]
endIndex-- endIndex--
}
} }
} }
} }