diff --git a/d2renderers/d2svg/d2svg.go b/d2renderers/d2svg/d2svg.go index ec8e33d55..6ff8738a3 100644 --- a/d2renderers/d2svg/d2svg.go +++ b/d2renderers/d2svg/d2svg.go @@ -447,6 +447,44 @@ func drawConnection(writer io.Writer, connection d2target.Connection, markers ma renderText(connection.Label, x, float64(connection.LabelHeight)), ) } + + length := geo.Route(connection.Route).Length() + if connection.SrcLabel != "" { + // TODO use arrowhead label dimensions + size := float64(connection.FontSize) + position := 0. + if length > 0 { + position = size / length + } + srcLabelTL := label.UnlockedTop.GetPointOnRoute(connection.Route, float64(connection.StrokeWidth), position, size, size) + + textStyle := fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "middle", connection.FontSize, "black") + x := srcLabelTL.X + size/2 + y := srcLabelTL.Y + float64(connection.FontSize) + fmt.Fprintf(writer, `%s`, + x, y, + textStyle, + renderText(connection.SrcLabel, x, size), + ) + } + if connection.DstLabel != "" { + // TODO use arrowhead label dimensions + size := float64(connection.FontSize) + position := 1. + if length > 0 { + position -= size / length + } + dstLabelTL := label.UnlockedTop.GetPointOnRoute(connection.Route, float64(connection.StrokeWidth), position, size, size) + + textStyle := fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "middle", connection.FontSize, "black") + x := dstLabelTL.X + size/2 + y := dstLabelTL.Y + float64(connection.FontSize) + fmt.Fprintf(writer, `%s`, + x, y, + textStyle, + renderText(connection.DstLabel, x, size), + ) + } } func renderOval(tl *geo.Point, width, height float64, style string) string { diff --git a/e2etests/testdata/stable/arrowhead_adjustment/dagre/sketch.exp.svg b/e2etests/testdata/stable/arrowhead_adjustment/dagre/sketch.exp.svg index 83d10d7a0..0009a91a5 100644 --- a/e2etests/testdata/stable/arrowhead_adjustment/dagre/sketch.exp.svg +++ b/e2etests/testdata/stable/arrowhead_adjustment/dagre/sketch.exp.svg @@ -14,11 +14,18 @@ width="480" height="778" viewBox="-100 -100 480 778">cba cba * \ No newline at end of file diff --git a/e2etests/testdata/stable/arrowhead_adjustment/elk/sketch.exp.svg b/e2etests/testdata/stable/arrowhead_adjustment/elk/sketch.exp.svg index e0d6183d7..d537b4b4b 100644 --- a/e2etests/testdata/stable/arrowhead_adjustment/elk/sketch.exp.svg +++ b/e2etests/testdata/stable/arrowhead_adjustment/elk/sketch.exp.svg @@ -14,11 +14,18 @@ width="749" height="446" viewBox="-88 -88 749 446">cba cba * \ No newline at end of file diff --git a/e2etests/testdata/stable/arrowhead_labels/dagre/sketch.exp.svg b/e2etests/testdata/stable/arrowhead_labels/dagre/sketch.exp.svg index 916fade4d..65b651774 100644 --- a/e2etests/testdata/stable/arrowhead_labels/dagre/sketch.exp.svg +++ b/e2etests/testdata/stable/arrowhead_labels/dagre/sketch.exp.svg @@ -17,7 +17,7 @@ width="401" height="552" viewBox="-144 -100 401 552">ab -To err is human, to moo bovineab -To err is human, to moo bovine