This commit is contained in:
Gavin Nishizawa 2022-11-23 20:40:24 -08:00
parent 61b8303c98
commit 1642310b4d
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD

View file

@ -450,43 +450,38 @@ func drawConnection(writer io.Writer, connection d2target.Connection, markers ma
length := geo.Route(connection.Route).Length() length := geo.Route(connection.Route).Length()
if connection.SrcLabel != "" { if connection.SrcLabel != "" {
// TODO use arrowhead label dimensions // TODO use arrowhead label dimensions https://github.com/terrastruct/d2/issues/183
size := float64(connection.FontSize) size := float64(connection.FontSize)
position := 0. position := 0.
if length > 0 { if length > 0 {
position = size / length position = size / length
} }
srcLabelTL := label.UnlockedTop.GetPointOnRoute(connection.Route, float64(connection.StrokeWidth), position, size, size) fmt.Fprint(writer, renderArrowheadLabel(connection, connection.SrcLabel, 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, `<text class="text-italic" x="%f" y="%f" style="%s">%s</text>`,
x, y,
textStyle,
renderText(connection.SrcLabel, x, size),
)
} }
if connection.DstLabel != "" { if connection.DstLabel != "" {
// TODO use arrowhead label dimensions // TODO use arrowhead label dimensions https://github.com/terrastruct/d2/issues/183
size := float64(connection.FontSize) size := float64(connection.FontSize)
position := 1. position := 1.
if length > 0 { if length > 0 {
position -= size / length position -= size / length
} }
dstLabelTL := label.UnlockedTop.GetPointOnRoute(connection.Route, float64(connection.StrokeWidth), position, size, size) fmt.Fprint(writer, renderArrowheadLabel(connection, connection.DstLabel, 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, `<text class="text-italic" x="%f" y="%f" style="%s">%s</text>`,
x, y,
textStyle,
renderText(connection.DstLabel, x, size),
)
} }
} }
func renderArrowheadLabel(connection d2target.Connection, text string, position, width, height float64) string {
labelTL := label.UnlockedTop.GetPointOnRoute(connection.Route, float64(connection.StrokeWidth), position, width, height)
textStyle := fmt.Sprintf("text-anchor:%s;font-size:%vpx;fill:%s", "middle", connection.FontSize, "black")
x := labelTL.X + width/2
y := labelTL.Y + float64(connection.FontSize)
return fmt.Sprintf(`<text class="text-italic" x="%f" y="%f" style="%s">%s</text>`,
x, y,
textStyle,
renderText(text, x, height),
)
}
func renderOval(tl *geo.Point, width, height float64, style string) string { func renderOval(tl *geo.Point, width, height float64, style string) string {
rx := width / 2 rx := width / 2
ry := height / 2 ry := height / 2