check shape type

This commit is contained in:
Gavin Nishizawa 2023-06-08 14:00:32 -07:00
parent f9a55e6acd
commit fe52f1a659
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD

View file

@ -494,28 +494,33 @@ 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
if edge.Src.Label.Value != "" && !srcShape.Is(shape.TEXT_TYPE) { switch srcShape.GetType() {
// assumes LabelPosition, LabelWidth, LabelHeight are all set if there is a label case shape.TEXT_TYPE, shape.TABLE_TYPE, shape.CLASS_TYPE, shape.CODE_TYPE:
labelPosition := label.Position(*edge.Src.LabelPosition) // labels aren't actually labels
if labelPosition.IsOutside() { default:
labelWidth := float64(edge.Src.LabelDimensions.Width) if edge.Src.Label.Value != "" {
labelHeight := float64(edge.Src.LabelDimensions.Height) // assumes LabelPosition, LabelWidth, LabelHeight are all set if there is a label
labelTL := labelPosition.GetPointOnBox(edge.Src.Box, label.PADDING, labelWidth, labelHeight) labelPosition := label.Position(*edge.Src.LabelPosition)
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++
}
} }
} }
} }
@ -526,28 +531,33 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
} }
overlapsOutsideLabel = false overlapsOutsideLabel = false
if edge.Dst.Label.Value != "" && !dstShape.Is(shape.TEXT_TYPE) { switch dstShape.GetType() {
// assumes LabelPosition, LabelWidth, LabelHeight are all set if there is a label case shape.TEXT_TYPE, shape.TABLE_TYPE, shape.CLASS_TYPE, shape.CODE_TYPE:
labelPosition := label.Position(*edge.Dst.LabelPosition) // labels aren't actually labels
if labelPosition.IsOutside() { default:
labelWidth := float64(edge.Dst.LabelDimensions.Width) if edge.Dst.Label.Value != "" {
labelHeight := float64(edge.Dst.LabelDimensions.Height) // assumes LabelPosition, LabelWidth, LabelHeight are all set if there is a label
labelTL := labelPosition.GetPointOnBox(edge.Dst.Box, label.PADDING, labelWidth, labelHeight) labelPosition := label.Position(*edge.Dst.LabelPosition)
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--
}
} }
} }
} }