Skip to content

Commit 1f5508b

Browse files
committed
refactored Shape & connection renderers
1 parent cb489a4 commit 1f5508b

File tree

8 files changed

+209
-232
lines changed

8 files changed

+209
-232
lines changed

d2exporter/export.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,8 @@ func applyStyles(shape *d2target.Shape, obj *d2graph.Object) {
194194
if obj.Style.DoubleBorder != nil {
195195
shape.DoubleBorder, _ = strconv.ParseBool(obj.Style.DoubleBorder.Value)
196196
}
197-
if obj.IconStyle != (d2graph.Style{}) {
198-
shape.IconStyle = &d2target.ShapeIconStyle{}
199-
}
200197
if obj.IconStyle.BorderRadius != nil {
201-
shape.IconStyle.BorderRadius, _ = strconv.Atoi(obj.IconStyle.BorderRadius.Value)
198+
shape.IconBorderRadius, _ = strconv.Atoi(obj.IconStyle.BorderRadius.Value)
202199
}
203200
}
204201

d2renderers/d2svg/d2svg.go

+48-38
Original file line numberDiff line numberDiff line change
@@ -853,25 +853,18 @@ func drawConnection(writer io.Writer, diagramHash string, connection d2target.Co
853853
if connection.Icon != nil {
854854
iconPos := connection.GetIconPosition()
855855
if iconPos != nil {
856+
connectionIconClipPath := ""
856857
if connection.IconBorderRadius != 0 {
857-
fmt.Fprintf(writer, `<image href="%s" x="%f" y="%f" width="%d" height="%d" clip-path="inset(0 round %fpx)" />`,
858-
html.EscapeString(connection.Icon.String()),
859-
iconPos.X,
860-
iconPos.Y,
861-
d2target.DEFAULT_ICON_SIZE,
862-
d2target.DEFAULT_ICON_SIZE,
863-
connection.IconBorderRadius,
864-
)
865-
} else {
866-
867-
fmt.Fprintf(writer, `<image href="%s" x="%f" y="%f" width="%d" height="%d" />`,
868-
html.EscapeString(connection.Icon.String()),
869-
iconPos.X,
870-
iconPos.Y,
871-
d2target.DEFAULT_ICON_SIZE,
872-
d2target.DEFAULT_ICON_SIZE,
873-
)
858+
connectionIconClipPath = fmt.Sprintf(` clip-path="inset(0 round %fpx)"`, connection.IconBorderRadius)
874859
}
860+
fmt.Fprintf(writer, `<image href="%s" x="%f" y="%f" width="%d" height="%d"%s />`,
861+
html.EscapeString(connection.Icon.String()),
862+
iconPos.X,
863+
iconPos.Y,
864+
d2target.DEFAULT_ICON_SIZE,
865+
d2target.DEFAULT_ICON_SIZE,
866+
connectionIconClipPath,
867+
)
875868
}
876869
}
877870

@@ -1447,9 +1440,10 @@ func drawShape(writer, appendixWriter io.Writer, diagramHash string, targetShape
14471440
el.Fill = fill
14481441
el.Stroke = stroke
14491442
el.Style = style
1450-
if targetShape.IconStyle != nil && targetShape.IconStyle.BorderRadius != 0 {
1451-
fmt.Fprint(writer, clipPathForIconBorderRadius(diagramHash, targetShape))
1452-
el.ClipPath = fmt.Sprintf("%v-%v-icon", diagramHash, targetShape.ID)
1443+
if targetShape.IconBorderRadius != 0 {
1444+
clipPathId := fmt.Sprintf("%v-%v-icon", diagramHash, svg.SVGID(targetShape.ID))
1445+
fmt.Fprint(writer, applyIconBorderRadius(clipPathId, targetShape))
1446+
el.ClipPath = clipPathId
14531447
}
14541448
fmt.Fprint(writer, el.Render())
14551449

@@ -1643,24 +1637,18 @@ func drawShape(writer, appendixWriter io.Writer, diagramHash string, targetShape
16431637

16441638
tl := iconPosition.GetPointOnBox(box, label.PADDING, float64(iconSize), float64(iconSize))
16451639

1646-
if targetShape.IconStyle != nil && targetShape.IconStyle.BorderRadius != 0 {
1647-
fmt.Fprintf(writer, `<image href="%s" x="%f" y="%f" width="%d" height="%d" clip-path="inset(0 round %dpx)" />`,
1648-
html.EscapeString(targetShape.Icon.String()),
1649-
tl.X,
1650-
tl.Y,
1651-
iconSize,
1652-
iconSize,
1653-
targetShape.IconStyle.BorderRadius,
1654-
)
1655-
} else {
1656-
fmt.Fprintf(writer, `<image href="%s" x="%f" y="%f" width="%d" height="%d" />`,
1657-
html.EscapeString(targetShape.Icon.String()),
1658-
tl.X,
1659-
tl.Y,
1660-
iconSize,
1661-
iconSize,
1662-
)
1663-
}
1640+
shapeIconClipPath := ""
1641+
if targetShape.IconBorderRadius != 0 {
1642+
shapeIconClipPath = fmt.Sprintf(` clip-path="inset(0 round %dpx)"`, targetShape.IconBorderRadius)
1643+
}
1644+
fmt.Fprintf(writer, `<image href="%s" x="%f" y="%f" width="%d" height="%d"%s />`,
1645+
html.EscapeString(targetShape.Icon.String()),
1646+
tl.X,
1647+
tl.Y,
1648+
iconSize,
1649+
iconSize,
1650+
shapeIconClipPath,
1651+
)
16641652
}
16651653

16661654
if targetShape.Label != "" && targetShape.Opacity != 0 {
@@ -1889,6 +1877,28 @@ func drawShape(writer, appendixWriter io.Writer, diagramHash string, targetShape
18891877
return labelMask, nil
18901878
}
18911879

1880+
func applyIconBorderRadius(clipPathId string, shape d2target.Shape) string {
1881+
box := geo.NewBox(
1882+
geo.NewPoint(float64(shape.Pos.X), float64(shape.Pos.Y)),
1883+
float64(shape.Width),
1884+
float64(shape.Height),
1885+
)
1886+
topX, topY := box.TopLeft.X+box.Width, box.TopLeft.Y
1887+
1888+
out := fmt.Sprintf(`<clipPath id="%s">`, clipPathId)
1889+
out += fmt.Sprintf(`<path d="M %f %f L %f %f S %f %f %f %f `, box.TopLeft.X, box.TopLeft.Y+float64(shape.IconBorderRadius), box.TopLeft.X, box.TopLeft.Y+float64(shape.IconBorderRadius), box.TopLeft.X, box.TopLeft.Y, box.TopLeft.X+float64(shape.IconBorderRadius), box.TopLeft.Y)
1890+
out += fmt.Sprintf(`L %f %f L %f %f `, box.TopLeft.X+box.Width-float64(shape.IconBorderRadius), box.TopLeft.Y, topX-float64(shape.IconBorderRadius), topY)
1891+
1892+
out += fmt.Sprintf(`S %f %f %f %f `, topX, topY, topX, topY+float64(shape.IconBorderRadius))
1893+
out += fmt.Sprintf(`L %f %f `, topX, topY+box.Height-float64(shape.IconBorderRadius))
1894+
out += fmt.Sprintf(`S %f % f %f %f `, topX, topY+box.Height, topX-float64(shape.IconBorderRadius), topY+box.Height)
1895+
out += fmt.Sprintf(`L %f %f `, box.TopLeft.X+float64(shape.IconBorderRadius), box.TopLeft.Y+box.Height)
1896+
out += fmt.Sprintf(`S %f %f %f %f`, box.TopLeft.X, box.TopLeft.Y+box.Height, box.TopLeft.X, box.TopLeft.Y+box.Height-float64(shape.IconBorderRadius))
1897+
out += fmt.Sprintf(`L %f %f`, box.TopLeft.X, box.TopLeft.Y+float64(shape.IconBorderRadius))
1898+
out += fmt.Sprintf(`Z %f %f" `, box.TopLeft.X, box.TopLeft.Y)
1899+
return out + `fill="none" /> </clipPath>`
1900+
}
1901+
18921902
func addAppendixItems(writer io.Writer, diagramHash string, targetShape d2target.Shape, s shape.Shape) {
18931903
var p1, p2 *geo.Point
18941904
if targetShape.Tooltip != "" || targetShape.Link != "" {

d2renderers/d2svg/table.go

-22
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,6 @@ func clipPathForBorderRadius(diagramHash string, shape d2target.Shape) string {
4141
return out + `fill="none" /> </clipPath>`
4242
}
4343

44-
func clipPathForIconBorderRadius(diagramHash string, shape d2target.Shape) string {
45-
box := geo.NewBox(
46-
geo.NewPoint(float64(shape.Pos.X), float64(shape.Pos.Y)),
47-
float64(shape.Width),
48-
float64(shape.Height),
49-
)
50-
topX, topY := box.TopLeft.X+box.Width, box.TopLeft.Y
51-
52-
out := fmt.Sprintf(`<clipPath id="%v-%v-icon">`, diagramHash, shape.ID)
53-
out += fmt.Sprintf(`<path d="M %f %f L %f %f S %f %f %f %f `, box.TopLeft.X, box.TopLeft.Y+float64(shape.IconStyle.BorderRadius), box.TopLeft.X, box.TopLeft.Y+float64(shape.IconStyle.BorderRadius), box.TopLeft.X, box.TopLeft.Y, box.TopLeft.X+float64(shape.IconStyle.BorderRadius), box.TopLeft.Y)
54-
out += fmt.Sprintf(`L %f %f L %f %f `, box.TopLeft.X+box.Width-float64(shape.IconStyle.BorderRadius), box.TopLeft.Y, topX-float64(shape.IconStyle.BorderRadius), topY)
55-
56-
out += fmt.Sprintf(`S %f %f %f %f `, topX, topY, topX, topY+float64(shape.IconStyle.BorderRadius))
57-
out += fmt.Sprintf(`L %f %f `, topX, topY+box.Height-float64(shape.IconStyle.BorderRadius))
58-
out += fmt.Sprintf(`S %f % f %f %f `, topX, topY+box.Height, topX-float64(shape.IconStyle.BorderRadius), topY+box.Height)
59-
out += fmt.Sprintf(`L %f %f `, box.TopLeft.X+float64(shape.IconStyle.BorderRadius), box.TopLeft.Y+box.Height)
60-
out += fmt.Sprintf(`S %f %f %f %f`, box.TopLeft.X, box.TopLeft.Y+box.Height, box.TopLeft.X, box.TopLeft.Y+box.Height-float64(shape.IconStyle.BorderRadius))
61-
out += fmt.Sprintf(`L %f %f`, box.TopLeft.X, box.TopLeft.Y+float64(shape.IconStyle.BorderRadius))
62-
out += fmt.Sprintf(`Z %f %f" `, box.TopLeft.X, box.TopLeft.Y)
63-
return out + `fill="none" /> </clipPath>`
64-
}
65-
6644
func tableHeader(diagramHash string, shape d2target.Shape, box *geo.Box, text string, textWidth, textHeight, fontSize float64, inlineTheme *d2themes.Theme) string {
6745
rectEl := d2themes.NewThemableElement("rect", inlineTheme)
6846
rectEl.X, rectEl.Y = box.TopLeft.X, box.TopLeft.Y

d2target/d2target.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,12 @@ type Shape struct {
503503
Multiple bool `json:"multiple"`
504504
DoubleBorder bool `json:"double-border"`
505505

506-
Tooltip string `json:"tooltip"`
507-
Link string `json:"link"`
508-
PrettyLink string `json:"prettyLink,omitempty"`
509-
Icon *url.URL `json:"icon"`
510-
IconStyle *ShapeIconStyle `json:"iconStyle,omitempty"`
511-
IconPosition string `json:"iconPosition"`
506+
Tooltip string `json:"tooltip"`
507+
Link string `json:"link"`
508+
PrettyLink string `json:"prettyLink,omitempty"`
509+
Icon *url.URL `json:"icon"`
510+
IconBorderRadius int `json:"iconBorderRadius,omitempty"`
511+
IconPosition string `json:"iconPosition"`
512512

513513
// Whether the shape should allow shapes behind it to bleed through
514514
// Currently just used for sequence diagram groups
@@ -532,10 +532,6 @@ type Shape struct {
532532
NeutralAccentColor string `json:"neutralAccentColor,omitempty"`
533533
}
534534

535-
type ShapeIconStyle struct {
536-
BorderRadius int `json:"borderRadius"`
537-
}
538-
539535
func (s Shape) GetFontColor() string {
540536
if s.Type == ShapeClass || s.Type == ShapeSQLTable {
541537
if !color.IsThemeColor(s.Color) {

e2etests/testdata/txtar/icon-style/dagre/board.exp.json

+1-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)