render with arrowhead dimensions

This commit is contained in:
Gavin Nishizawa 2023-04-14 11:51:48 -07:00
parent 5e5f9f476c
commit 71546caeeb
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
3 changed files with 30 additions and 14 deletions

View file

@ -213,7 +213,11 @@ func toConnection(edge *d2graph.Edge, theme *d2themes.Theme) d2target.Connection
} }
if edge.SrcArrowhead != nil { if edge.SrcArrowhead != nil {
if edge.SrcArrowhead.Label.Value != "" { 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 { if edge.DstArrow {
@ -230,7 +234,11 @@ func toConnection(edge *d2graph.Edge, theme *d2themes.Theme) d2target.Connection
} }
if edge.DstArrowhead != nil { if edge.DstArrowhead != nil {
if edge.DstArrowhead.Label.Value != "" { 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 { if theme != nil && theme.SpecialRules.NoCornerRadius {

View file

@ -621,23 +621,27 @@ func drawConnection(writer io.Writer, labelMaskID string, connection d2target.Co
} }
length := geo.Route(connection.Route).Length() 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 // 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. position := 0.
if length > 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 // 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. position := 1.
if length > 0 { 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, `</g>`) fmt.Fprintf(writer, `</g>`)
return return

View file

@ -286,8 +286,12 @@ func (diagram Diagram) GetCorpus() string {
} }
for _, c := range diagram.Connections { for _, c := range diagram.Connections {
corpus += c.Label corpus += c.Label
corpus += c.SrcLabel if c.SrcLabel != nil {
corpus += c.DstLabel corpus += c.SrcLabel.Label
}
if c.DstLabel != nil {
corpus += c.DstLabel.Label
}
} }
return corpus return corpus
@ -431,11 +435,11 @@ type Connection struct {
Src string `json:"src"` Src string `json:"src"`
SrcArrow Arrowhead `json:"srcArrow"` SrcArrow Arrowhead `json:"srcArrow"`
SrcLabel string `json:"srcLabel"` SrcLabel *Text `json:"srcLabel,omitempty"`
Dst string `json:"dst"` Dst string `json:"dst"`
DstArrow Arrowhead `json:"dstArrow"` DstArrow Arrowhead `json:"dstArrow"`
DstLabel string `json:"dstLabel"` DstLabel *Text `json:"dstLabel,omitempty"`
Opacity float64 `json:"opacity"` Opacity float64 `json:"opacity"`
StrokeDash float64 `json:"strokeDash"` StrokeDash float64 `json:"strokeDash"`