diff --git a/d2exporter/export.go b/d2exporter/export.go index 8f8a398f1..fe9be6c46 100644 --- a/d2exporter/export.go +++ b/d2exporter/export.go @@ -213,7 +213,11 @@ func toConnection(edge *d2graph.Edge, theme *d2themes.Theme) d2target.Connection } if edge.SrcArrowhead != nil { if edge.SrcArrowhead.Label.Value != "" { - connection.SrcLabel = edge.SrcArrowhead.Label.Value + connection.SrcLabel = &d2target.Text{ + Label: edge.SrcArrowhead.Label.Value, + LabelWidth: edge.SrcArrowhead.LabelDimensions.Width, + LabelHeight: edge.SrcArrowhead.LabelDimensions.Height, + } } } if edge.DstArrow { @@ -230,7 +234,11 @@ func toConnection(edge *d2graph.Edge, theme *d2themes.Theme) d2target.Connection } if edge.DstArrowhead != nil { if edge.DstArrowhead.Label.Value != "" { - connection.DstLabel = edge.DstArrowhead.Label.Value + connection.DstLabel = &d2target.Text{ + Label: edge.DstArrowhead.Label.Value, + LabelWidth: edge.DstArrowhead.LabelDimensions.Width, + LabelHeight: edge.DstArrowhead.LabelDimensions.Height, + } } } if theme != nil && theme.SpecialRules.NoCornerRadius { diff --git a/d2renderers/d2svg/d2svg.go b/d2renderers/d2svg/d2svg.go index f38d5b006..3e38c8cf3 100644 --- a/d2renderers/d2svg/d2svg.go +++ b/d2renderers/d2svg/d2svg.go @@ -621,23 +621,27 @@ func drawConnection(writer io.Writer, labelMaskID string, connection d2target.Co } length := geo.Route(connection.Route).Length() - if connection.SrcLabel != "" { + if connection.SrcLabel != nil && connection.SrcLabel.Label != "" { // TODO use arrowhead label dimensions https://github.com/terrastruct/d2/issues/183 - size := float64(connection.FontSize) + // size := float64(connection.FontSize) + width := float64(connection.SrcLabel.LabelWidth) + height := float64(connection.DstLabel.LabelHeight) position := 0. if length > 0 { - position = size / length + position = math.Max(width, height) / length } - fmt.Fprint(writer, renderArrowheadLabel(connection, connection.SrcLabel, position, size, size)) + fmt.Fprint(writer, renderArrowheadLabel(connection, connection.SrcLabel.Label, position, width, height)) } - if connection.DstLabel != "" { + if connection.DstLabel != nil && connection.DstLabel.Label != "" { // TODO use arrowhead label dimensions https://github.com/terrastruct/d2/issues/183 - size := float64(connection.FontSize) + // size := float64(connection.FontSize) + width := float64(connection.DstLabel.LabelWidth) + height := float64(connection.DstLabel.LabelHeight) position := 1. if length > 0 { - position -= size / length + position -= math.Max(width, height) / length } - fmt.Fprint(writer, renderArrowheadLabel(connection, connection.DstLabel, position, size, size)) + fmt.Fprint(writer, renderArrowheadLabel(connection, connection.DstLabel.Label, position, width, height)) } fmt.Fprintf(writer, ``) return diff --git a/d2target/d2target.go b/d2target/d2target.go index fd42e3407..bd5c49411 100644 --- a/d2target/d2target.go +++ b/d2target/d2target.go @@ -286,8 +286,12 @@ func (diagram Diagram) GetCorpus() string { } for _, c := range diagram.Connections { corpus += c.Label - corpus += c.SrcLabel - corpus += c.DstLabel + if c.SrcLabel != nil { + corpus += c.SrcLabel.Label + } + if c.DstLabel != nil { + corpus += c.DstLabel.Label + } } return corpus @@ -431,11 +435,11 @@ type Connection struct { Src string `json:"src"` SrcArrow Arrowhead `json:"srcArrow"` - SrcLabel string `json:"srcLabel"` + SrcLabel *Text `json:"srcLabel,omitempty"` Dst string `json:"dst"` DstArrow Arrowhead `json:"dstArrow"` - DstLabel string `json:"dstLabel"` + DstLabel *Text `json:"dstLabel,omitempty"` Opacity float64 `json:"opacity"` StrokeDash float64 `json:"strokeDash"`