move ThemableElement and others to d2themes, smaller fixes

This commit is contained in:
Vojtěch Fošnár 2023-02-19 13:00:01 +01:00
parent 6a89beaeb1
commit 331cf95284
No known key found for this signature in database
GPG key ID: 657727E71C40859A
9 changed files with 131 additions and 136 deletions

1
ci/sub

@ -1 +0,0 @@
Subproject commit 512bad5a958c5e33ba9b3e89dfac1bfd6002f98c

View file

@ -18,16 +18,14 @@ import (
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/d2/d2layouts/d2sequence"
"oss.terrastruct.com/d2/d2target"
"oss.terrastruct.com/d2/d2themes/d2themescatalog"
"oss.terrastruct.com/d2/lib/geo"
"oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/textmeasure"
)
type testCase struct {
name string
dsl string
themeID int64
name string
dsl string
assertions func(t *testing.T, d *d2target.Diagram)
}
@ -132,8 +130,7 @@ func testConnection(t *testing.T) {
{
// This is a regression test where a connection with stroke-dash of 0 on terrastruct flagship theme would have a diff color
// than a connection without stroke dash
themeID: d2themescatalog.FlagshipTerrastruct.ID,
name: "theme_stroke-dash",
name: "theme_stroke-dash",
dsl: `x -> y: { style.stroke-dash: 0 }
x -> y
`,
@ -168,16 +165,14 @@ func testLabel(t *testing.T) {
func testTheme(t *testing.T) {
tcs := []testCase{
{
name: "shape_without_bold",
themeID: d2themescatalog.FlagshipTerrastruct.ID,
name: "shape_without_bold",
dsl: `x: {
style.bold: false
}
`,
},
{
name: "shape_with_italic",
themeID: d2themescatalog.FlagshipTerrastruct.ID,
name: "shape_with_italic",
dsl: `x: {
style.italic: true
}
@ -187,7 +182,6 @@ func testTheme(t *testing.T) {
name: "connection_without_italic",
dsl: `x -> y: asdf { style.italic: false }
`,
themeID: d2themescatalog.FlagshipTerrastruct.ID,
},
{
name: "connection_with_italic",
@ -195,7 +189,6 @@ func testTheme(t *testing.T) {
style.italic: true
}
`,
themeID: d2themescatalog.FlagshipTerrastruct.ID,
},
{
name: "connection_with_bold",
@ -203,7 +196,6 @@ func testTheme(t *testing.T) {
style.bold: true
}
`,
themeID: d2themescatalog.FlagshipTerrastruct.ID,
},
}

View file

@ -11,11 +11,11 @@ import (
"github.com/dop251/goja"
"oss.terrastruct.com/d2/d2target"
"oss.terrastruct.com/d2/d2themes"
"oss.terrastruct.com/d2/lib/color"
"oss.terrastruct.com/d2/lib/geo"
"oss.terrastruct.com/d2/lib/label"
"oss.terrastruct.com/d2/lib/svg"
svgstyle "oss.terrastruct.com/d2/lib/svg/style"
"oss.terrastruct.com/util-go/go2"
)
@ -83,9 +83,9 @@ func Rect(r *Runner, shape d2target.Shape) (string, error) {
return "", err
}
output := ""
pathEl := svgstyle.NewThemableElement("path")
pathEl.Transform = fmt.Sprintf("translate(%d %d)", shape.Pos.X, shape.Pos.Y)
pathEl.Fill, pathEl.Stroke = svgstyle.ShapeTheme(shape)
pathEl := d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
pathEl.Class = "shape"
pathEl.Style = shape.CSSStyle()
for _, p := range paths {
@ -93,11 +93,11 @@ func Rect(r *Runner, shape d2target.Shape) (string, error) {
output += pathEl.Render()
}
sketchOEl := svgstyle.NewThemableElement("rect")
sketchOEl.Transform = fmt.Sprintf("translate(%d %d)", shape.Pos.X, shape.Pos.Y)
sketchOEl := d2themes.NewThemableElement("rect")
sketchOEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
sketchOEl.Width = float64(shape.Width)
sketchOEl.Height = float64(shape.Height)
renderedSO, err := svgstyle.NewThemableSketchOverlay(sketchOEl, pathEl.Fill).Render()
renderedSO, err := d2themes.NewThemableSketchOverlay(sketchOEl, pathEl.Fill).Render()
if err != nil {
return "", err
}
@ -130,9 +130,9 @@ func DoubleRect(r *Runner, shape d2target.Shape) (string, error) {
output := ""
pathEl := svgstyle.NewThemableElement("path")
pathEl.Transform = fmt.Sprintf("translate(%d %d)", shape.Pos.X, shape.Pos.Y)
pathEl.Fill, pathEl.Stroke = svgstyle.ShapeTheme(shape)
pathEl := d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
pathEl.Class = "shape"
pathEl.Style = shape.CSSStyle()
for _, p := range pathsBigRect {
@ -140,9 +140,9 @@ func DoubleRect(r *Runner, shape d2target.Shape) (string, error) {
output += pathEl.Render()
}
pathEl = svgstyle.NewThemableElement("path")
pathEl.Transform = fmt.Sprintf("translate(%d %d)", shape.Pos.X+d2target.INNER_BORDER_OFFSET, shape.Pos.Y+d2target.INNER_BORDER_OFFSET)
pathEl.Fill, pathEl.Stroke = svgstyle.ShapeTheme(shape)
pathEl = d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X+d2target.INNER_BORDER_OFFSET), float64(shape.Pos.Y+d2target.INNER_BORDER_OFFSET))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
pathEl.Class = "shape"
pathEl.Style = shape.CSSStyle()
for _, p := range pathsSmallRect {
@ -169,9 +169,9 @@ func Oval(r *Runner, shape d2target.Shape) (string, error) {
return "", err
}
output := ""
pathEl := svgstyle.NewThemableElement("path")
pathEl.Transform = fmt.Sprintf("translate(%d %d)", shape.Pos.X, shape.Pos.Y)
pathEl.Fill, pathEl.Stroke = svgstyle.ShapeTheme(shape)
pathEl := d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
pathEl.Class = "shape"
pathEl.Style = shape.CSSStyle()
for _, p := range paths {
@ -179,11 +179,11 @@ func Oval(r *Runner, shape d2target.Shape) (string, error) {
output += pathEl.Render()
}
soElement := svgstyle.NewThemableElement("ellipse")
soElement.Transform = fmt.Sprintf("translate(%d %d)", shape.Pos.X+shape.Width/2, shape.Pos.Y+shape.Height/2)
soElement := d2themes.NewThemableElement("ellipse")
soElement.SetTranslate(float64(shape.Pos.X+shape.Width)/2, float64(shape.Pos.Y+shape.Height)/2)
soElement.Rx = float64(shape.Width / 2)
soElement.Ry = float64(shape.Height / 2)
renderedSO, err := svgstyle.NewThemableSketchOverlay(
renderedSO, err := d2themes.NewThemableSketchOverlay(
soElement,
pathEl.Fill,
).Render()
@ -219,9 +219,9 @@ func DoubleOval(r *Runner, shape d2target.Shape) (string, error) {
output := ""
pathEl := svgstyle.NewThemableElement("path")
pathEl.Transform = fmt.Sprintf("translate(%d %d)", shape.Pos.X, shape.Pos.Y)
pathEl.Fill, pathEl.Stroke = svgstyle.ShapeTheme(shape)
pathEl := d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
pathEl.Class = "shape"
pathEl.Style = shape.CSSStyle()
for _, p := range pathsBigCircle {
@ -229,9 +229,9 @@ func DoubleOval(r *Runner, shape d2target.Shape) (string, error) {
output += pathEl.Render()
}
pathEl = svgstyle.NewThemableElement("path")
pathEl.Transform = fmt.Sprintf("translate(%d %d)", shape.Pos.X, shape.Pos.Y)
pathEl.Fill, pathEl.Stroke = svgstyle.ShapeTheme(shape)
pathEl = d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
pathEl.Class = "shape"
pathEl.Style = shape.CSSStyle()
for _, p := range pathsSmallCircle {
@ -260,8 +260,8 @@ func Paths(r *Runner, shape d2target.Shape, paths []string) (string, error) {
if err != nil {
return "", err
}
pathEl := svgstyle.NewThemableElement("path")
pathEl.Fill, pathEl.Stroke = svgstyle.ShapeTheme(shape)
pathEl := d2themes.NewThemableElement("path")
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
pathEl.Class = "shape"
pathEl.Style = shape.CSSStyle()
for _, p := range sketchPaths {
@ -269,10 +269,10 @@ func Paths(r *Runner, shape d2target.Shape, paths []string) (string, error) {
output += pathEl.Render()
}
soElement := svgstyle.NewThemableElement("path")
soElement := d2themes.NewThemableElement("path")
for _, p := range sketchPaths {
soElement.D = p
renderedSO, err := svgstyle.NewThemableSketchOverlay(
renderedSO, err := d2themes.NewThemableSketchOverlay(
soElement,
pathEl.Fill,
).Render()
@ -298,9 +298,9 @@ func Connection(r *Runner, connection d2target.Connection, path, attrs string) (
animatedClass = " animated-connection"
}
pathEl := svgstyle.NewThemableElement("path")
pathEl := d2themes.NewThemableElement("path")
pathEl.Fill = color.None
pathEl.Stroke = svgstyle.ConnectionTheme(connection)
pathEl.Stroke = d2themes.ConnectionTheme(connection)
pathEl.Class = fmt.Sprintf("connection%s", animatedClass)
pathEl.Style = connection.CSSStyle()
pathEl.Attributes = attrs
@ -324,9 +324,9 @@ func Table(r *Runner, shape d2target.Shape) (string, error) {
if err != nil {
return "", err
}
pathEl := svgstyle.NewThemableElement("path")
pathEl.Transform = fmt.Sprintf("translate(%d %d)", shape.Pos.X, shape.Pos.Y)
pathEl.Fill, pathEl.Stroke = svgstyle.ShapeTheme(shape)
pathEl := d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
pathEl.Class = "shape"
pathEl.Style = shape.CSSStyle()
for _, p := range paths {
@ -350,8 +350,8 @@ func Table(r *Runner, shape d2target.Shape) (string, error) {
if err != nil {
return "", err
}
pathEl = svgstyle.NewThemableElement("path")
pathEl.Transform = fmt.Sprintf("translate(%d %d)", shape.Pos.X, shape.Pos.Y)
pathEl = d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill = shape.Fill
pathEl.Class = "class_header"
for _, p := range paths {
@ -367,7 +367,7 @@ func Table(r *Runner, shape d2target.Shape) (string, error) {
float64(shape.LabelHeight),
)
textEl := svgstyle.NewThemableElement("text")
textEl := d2themes.NewThemableElement("text")
textEl.X = tl.X
textEl.Y = tl.Y + float64(shape.LabelHeight)*3/4
textEl.Fill = shape.Stroke
@ -400,7 +400,7 @@ func Table(r *Runner, shape d2target.Shape) (string, error) {
float64(shape.FontSize),
)
textEl := svgstyle.NewThemableElement("text")
textEl := d2themes.NewThemableElement("text")
textEl.X = nameTL.X
textEl.Y = nameTL.Y + float64(shape.FontSize)*3/4
textEl.Fill = shape.PrimaryAccentColor
@ -430,7 +430,7 @@ func Table(r *Runner, shape d2target.Shape) (string, error) {
if err != nil {
return "", err
}
pathEl := svgstyle.NewThemableElement("path")
pathEl := d2themes.NewThemableElement("path")
pathEl.Fill = shape.Fill
for _, p := range paths {
pathEl.D = p
@ -438,11 +438,11 @@ func Table(r *Runner, shape d2target.Shape) (string, error) {
}
}
sketchOEl := svgstyle.NewThemableElement("rect")
sketchOEl.Transform = fmt.Sprintf("translate(%d %d)", shape.Pos.X, shape.Pos.Y)
sketchOEl := d2themes.NewThemableElement("rect")
sketchOEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
sketchOEl.Width = float64(shape.Width)
sketchOEl.Height = float64(shape.Height)
renderedSO, err := svgstyle.NewThemableSketchOverlay(sketchOEl, pathEl.Fill).Render()
renderedSO, err := d2themes.NewThemableSketchOverlay(sketchOEl, pathEl.Fill).Render()
if err != nil {
return "", err
}
@ -463,9 +463,9 @@ func Class(r *Runner, shape d2target.Shape) (string, error) {
if err != nil {
return "", err
}
pathEl := svgstyle.NewThemableElement("path")
pathEl.Transform = fmt.Sprintf("translate(%d %d)", shape.Pos.X, shape.Pos.Y)
pathEl.Fill, pathEl.Stroke = svgstyle.ShapeTheme(shape)
pathEl := d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
pathEl.Class = "shape"
pathEl.Style = shape.CSSStyle()
for _, p := range paths {
@ -490,8 +490,8 @@ func Class(r *Runner, shape d2target.Shape) (string, error) {
if err != nil {
return "", err
}
pathEl = svgstyle.NewThemableElement("path")
pathEl.Transform = fmt.Sprintf("translate(%d %d)", shape.Pos.X, shape.Pos.Y)
pathEl = d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill = shape.Fill
pathEl.Class = "class_header"
for _, p := range paths {
@ -499,11 +499,11 @@ func Class(r *Runner, shape d2target.Shape) (string, error) {
output += pathEl.Render()
}
sketchOEl := svgstyle.NewThemableElement("rect")
sketchOEl.Transform = fmt.Sprintf("translate(%d %d)", shape.Pos.X, shape.Pos.Y)
sketchOEl := d2themes.NewThemableElement("rect")
sketchOEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
sketchOEl.Width = float64(shape.Width)
sketchOEl.Height = headerBox.Height
renderedSO, err := svgstyle.NewThemableSketchOverlay(sketchOEl, pathEl.Fill).Render()
renderedSO, err := d2themes.NewThemableSketchOverlay(sketchOEl, pathEl.Fill).Render()
if err != nil {
return "", err
}
@ -517,7 +517,7 @@ func Class(r *Runner, shape d2target.Shape) (string, error) {
float64(shape.LabelHeight),
)
textEl := svgstyle.NewThemableElement("text")
textEl := d2themes.NewThemableElement("text")
textEl.X = tl.X + float64(shape.LabelWidth)/2
textEl.Y = tl.Y + float64(shape.LabelHeight)*3/4
textEl.Fill = shape.Stroke
@ -544,7 +544,7 @@ func Class(r *Runner, shape d2target.Shape) (string, error) {
if err != nil {
return "", err
}
pathEl = svgstyle.NewThemableElement("path")
pathEl = d2themes.NewThemableElement("path")
pathEl.Fill = shape.Fill
pathEl.Class = "class_header"
for _, p := range paths {
@ -575,7 +575,7 @@ func classRow(shape d2target.Shape, box *geo.Box, prefix, nameText, typeText str
fontSize,
)
textEl := svgstyle.NewThemableElement("text")
textEl := d2themes.NewThemableElement("text")
textEl.X = prefixTL.X
textEl.Y = prefixTL.Y + fontSize*3/4
textEl.Fill = shape.PrimaryAccentColor
@ -786,7 +786,7 @@ func Arrowheads(r *Runner, bgColor string, connection d2target.Connection, srcAd
roughPaths = append(roughPaths, extraPaths...)
}
pathEl := svgstyle.NewThemableElement("path")
pathEl := d2themes.NewThemableElement("path")
pathEl.Class = "connection"
pathEl.Attributes = transform
for _, rp := range roughPaths {
@ -825,7 +825,7 @@ func Arrowheads(r *Runner, bgColor string, connection d2target.Connection, srcAd
roughPaths = append(roughPaths, extraPaths...)
}
pathEl := svgstyle.NewThemableElement("path")
pathEl := d2themes.NewThemableElement("path")
pathEl.Class = "connection"
pathEl.Attributes = transform
for _, rp := range roughPaths {

View file

@ -5,14 +5,14 @@ import (
"io"
"oss.terrastruct.com/d2/d2target"
"oss.terrastruct.com/d2/d2themes"
"oss.terrastruct.com/d2/lib/geo"
"oss.terrastruct.com/d2/lib/label"
"oss.terrastruct.com/d2/lib/svg"
svgstyle "oss.terrastruct.com/d2/lib/svg/style"
)
func classHeader(shape d2target.Shape, box *geo.Box, text string, textWidth, textHeight, fontSize float64) string {
rectEl := svgstyle.NewThemableElement("rect")
rectEl := d2themes.NewThemableElement("rect")
rectEl.X, rectEl.Y = box.TopLeft.X, box.TopLeft.Y
rectEl.Width, rectEl.Height = box.Width, box.Height
rectEl.Fill = shape.Fill
@ -27,7 +27,7 @@ func classHeader(shape d2target.Shape, box *geo.Box, text string, textWidth, tex
textHeight,
)
textEl := svgstyle.NewThemableElement("text")
textEl := d2themes.NewThemableElement("text")
textEl.X = tl.X + textWidth/2
textEl.Y = tl.Y + textHeight*3/4
textEl.Fill = shape.Stroke
@ -57,7 +57,7 @@ func classRow(shape d2target.Shape, box *geo.Box, prefix, nameText, typeText str
fontSize,
)
textEl := svgstyle.NewThemableElement("text")
textEl := d2themes.NewThemableElement("text")
textEl.X = prefixTL.X
textEl.Y = prefixTL.Y + fontSize*3/4
textEl.Fill = shape.PrimaryAccentColor
@ -82,12 +82,12 @@ func classRow(shape d2target.Shape, box *geo.Box, prefix, nameText, typeText str
}
func drawClass(writer io.Writer, targetShape d2target.Shape) {
el := svgstyle.NewThemableElement("rect")
el := d2themes.NewThemableElement("rect")
el.X = float64(targetShape.Pos.X)
el.Y = float64(targetShape.Pos.Y)
el.Width = float64(targetShape.Width)
el.Height = float64(targetShape.Height)
el.Fill, el.Stroke = svgstyle.ShapeTheme(targetShape)
el.Fill, el.Stroke = d2themes.ShapeTheme(targetShape)
el.Style = targetShape.CSSStyle()
fmt.Fprint(writer, el.Render())
@ -112,7 +112,7 @@ func drawClass(writer io.Writer, targetShape d2target.Shape) {
rowBox.TopLeft.Y += rowHeight
}
lineEl := svgstyle.NewThemableElement("line")
lineEl := d2themes.NewThemableElement("line")
lineEl.X1, lineEl.Y1 = rowBox.TopLeft.X, rowBox.TopLeft.Y
lineEl.X2, lineEl.Y2 = rowBox.TopLeft.X+rowBox.Width, rowBox.TopLeft.Y
lineEl.Stroke = targetShape.Fill

View file

@ -27,13 +27,13 @@ import (
"oss.terrastruct.com/d2/d2renderers/d2latex"
"oss.terrastruct.com/d2/d2renderers/d2sketch"
"oss.terrastruct.com/d2/d2target"
"oss.terrastruct.com/d2/d2themes"
"oss.terrastruct.com/d2/d2themes/d2themescatalog"
"oss.terrastruct.com/d2/lib/color"
"oss.terrastruct.com/d2/lib/geo"
"oss.terrastruct.com/d2/lib/label"
"oss.terrastruct.com/d2/lib/shape"
"oss.terrastruct.com/d2/lib/svg"
svgstyle "oss.terrastruct.com/d2/lib/svg/style"
"oss.terrastruct.com/d2/lib/textmeasure"
)
@ -84,7 +84,7 @@ func arrowheadMarkerID(isTarget bool, connection d2target.Connection) string {
}
return fmt.Sprintf("mk-%s", hash(fmt.Sprintf("%s,%t,%d,%s",
arrowhead, isTarget, connection.StrokeWidth, svgstyle.ConnectionTheme(connection),
arrowhead, isTarget, connection.StrokeWidth, d2themes.ConnectionTheme(connection),
)))
}
@ -130,8 +130,8 @@ func arrowheadMarker(isTarget bool, id string, bgColor string, connection d2targ
var path string
switch arrowhead {
case d2target.ArrowArrowhead:
polygonEl := svgstyle.NewThemableElement("polygon")
polygonEl.Fill = svgstyle.ConnectionTheme(connection)
polygonEl := d2themes.NewThemableElement("polygon")
polygonEl.Fill = d2themes.ConnectionTheme(connection)
polygonEl.Class = "connection"
polygonEl.Attributes = fmt.Sprintf(`stroke-width="%d"`, connection.StrokeWidth)
@ -152,8 +152,8 @@ func arrowheadMarker(isTarget bool, id string, bgColor string, connection d2targ
}
path = polygonEl.Render()
case d2target.TriangleArrowhead:
polygonEl := svgstyle.NewThemableElement("polygon")
polygonEl.Fill = svgstyle.ConnectionTheme(connection)
polygonEl := d2themes.NewThemableElement("polygon")
polygonEl.Fill = d2themes.ConnectionTheme(connection)
polygonEl.Class = "connection"
polygonEl.Attributes = fmt.Sprintf(`stroke-width="%d"`, connection.StrokeWidth)
@ -172,10 +172,10 @@ func arrowheadMarker(isTarget bool, id string, bgColor string, connection d2targ
}
path = polygonEl.Render()
case d2target.LineArrowhead:
polylineEl := svgstyle.NewThemableElement("polyline")
polylineEl := d2themes.NewThemableElement("polyline")
polylineEl.Fill = color.None
polylineEl.Class = "connection"
polylineEl.Stroke = svgstyle.ConnectionTheme(connection)
polylineEl.Stroke = d2themes.ConnectionTheme(connection)
polylineEl.Attributes = fmt.Sprintf(`stroke-width="%d"`, connection.StrokeWidth)
if isTarget {
@ -193,9 +193,9 @@ func arrowheadMarker(isTarget bool, id string, bgColor string, connection d2targ
}
path = polylineEl.Render()
case d2target.FilledDiamondArrowhead:
polygonEl := svgstyle.NewThemableElement("polygon")
polygonEl := d2themes.NewThemableElement("polygon")
polygonEl.Class = "connection"
polygonEl.Fill = svgstyle.ConnectionTheme(connection)
polygonEl.Fill = d2themes.ConnectionTheme(connection)
polygonEl.Attributes = fmt.Sprintf(`stroke-width="%d"`, connection.StrokeWidth)
if isTarget {
@ -215,10 +215,10 @@ func arrowheadMarker(isTarget bool, id string, bgColor string, connection d2targ
}
path = polygonEl.Render()
case d2target.DiamondArrowhead:
polygonEl := svgstyle.NewThemableElement("polygon")
polygonEl := d2themes.NewThemableElement("polygon")
polygonEl.Class = "connection"
polygonEl.Fill = bgColor
polygonEl.Stroke = svgstyle.ConnectionTheme(connection)
polygonEl.Stroke = d2themes.ConnectionTheme(connection)
polygonEl.Attributes = fmt.Sprintf(`stroke-width="%d"`, connection.StrokeWidth)
if isTarget {
@ -240,7 +240,7 @@ func arrowheadMarker(isTarget bool, id string, bgColor string, connection d2targ
case d2target.FilledCircleArrowhead:
radius := width / 2
circleEl := svgstyle.NewThemableElement("circle")
circleEl := d2themes.NewThemableElement("circle")
circleEl.Cy = radius
circleEl.R = radius - strokeWidth/2 // @alixander says there maybe should be a plus sign instead
circleEl.Fill = connection.Stroke
@ -257,7 +257,7 @@ func arrowheadMarker(isTarget bool, id string, bgColor string, connection d2targ
case d2target.CircleArrowhead:
radius := width / 2
circleEl := svgstyle.NewThemableElement("circle")
circleEl := d2themes.NewThemableElement("circle")
circleEl.Cy = radius
circleEl.R = radius - strokeWidth
circleEl.Fill = bgColor
@ -274,29 +274,29 @@ func arrowheadMarker(isTarget bool, id string, bgColor string, connection d2targ
case d2target.CfOne, d2target.CfMany, d2target.CfOneRequired, d2target.CfManyRequired:
offset := 3.0 + float64(connection.StrokeWidth)*1.8
var modifierEl *svgstyle.ThemableElement
var modifierEl *d2themes.ThemableElement
if arrowhead == d2target.CfOneRequired || arrowhead == d2target.CfManyRequired {
modifierEl = svgstyle.NewThemableElement("path")
modifierEl = d2themes.NewThemableElement("path")
modifierEl.D = fmt.Sprintf("M%f,%f %f,%f",
offset, 0.,
offset, height,
)
modifierEl.Fill = bgColor
modifierEl.Stroke = svgstyle.ConnectionTheme(connection)
modifierEl.Stroke = d2themes.ConnectionTheme(connection)
modifierEl.Class = "connection"
modifierEl.Attributes = fmt.Sprintf(`stroke-width="%d"`, connection.StrokeWidth)
} else {
modifierEl = svgstyle.NewThemableElement("circle")
modifierEl = d2themes.NewThemableElement("circle")
modifierEl.Cx = offset/2.0 + 2.0
modifierEl.Cy = height / 2.0
modifierEl.R = offset / 2.0
modifierEl.Fill = bgColor
modifierEl.Stroke = svgstyle.ConnectionTheme(connection)
modifierEl.Stroke = d2themes.ConnectionTheme(connection)
modifierEl.Class = "connection"
modifierEl.Attributes = fmt.Sprintf(`stroke-width="%d"`, connection.StrokeWidth)
}
childPathEl := svgstyle.NewThemableElement("path")
childPathEl := d2themes.NewThemableElement("path")
if arrowhead == d2target.CfMany || arrowhead == d2target.CfManyRequired {
childPathEl.D = fmt.Sprintf("M%f,%f %f,%f M%f,%f %f,%f M%f,%f %f,%f",
width-3.0, height/2.0,
@ -315,12 +315,12 @@ func arrowheadMarker(isTarget bool, id string, bgColor string, connection d2targ
)
}
gEl := svgstyle.NewThemableElement("g")
gEl := d2themes.NewThemableElement("g")
if !isTarget {
gEl.Transform = fmt.Sprintf("scale(-1) translate(-%f, -%f)", width, height)
}
gEl.Fill = bgColor
gEl.Stroke = svgstyle.ConnectionTheme(connection)
gEl.Stroke = d2themes.ConnectionTheme(connection)
gEl.Class = "connection"
gEl.Attributes = fmt.Sprintf(`stroke-width="%d"`, connection.StrokeWidth)
gEl.Content = fmt.Sprintf("%s%s",
@ -542,10 +542,10 @@ func drawConnection(writer io.Writer, bgColor string, fgColor string, labelMaskI
animatedClass = " animated-connection"
}
pathEl := svgstyle.NewThemableElement("path")
pathEl := d2themes.NewThemableElement("path")
pathEl.D = path
pathEl.Fill = color.None
pathEl.Stroke = svgstyle.ConnectionTheme(connection)
pathEl.Stroke = d2themes.ConnectionTheme(connection)
pathEl.Class = fmt.Sprintf("connection%s", animatedClass)
pathEl.Style = connection.CSSStyle()
pathEl.Attributes = fmt.Sprintf("%s%s%s", markerStart, markerEnd, mask)
@ -565,14 +565,14 @@ func drawConnection(writer io.Writer, bgColor string, fgColor string, labelMaskI
}
if connection.Fill != color.Empty {
rectEl := svgstyle.NewThemableElement("rect")
rectEl := d2themes.NewThemableElement("rect")
rectEl.X, rectEl.Y = labelTL.X, labelTL.Y
rectEl.Width, rectEl.Height = float64(connection.LabelWidth), float64(connection.LabelHeight)
rectEl.Fill = connection.Fill
fmt.Fprint(writer, rectEl.Render())
}
textEl := svgstyle.NewThemableElement("text")
textEl := d2themes.NewThemableElement("text")
textEl.X = labelTL.X + float64(connection.LabelWidth)/2
textEl.Y = labelTL.Y + float64(connection.FontSize)
textEl.Fill = fontColor
@ -608,7 +608,7 @@ func drawConnection(writer io.Writer, bgColor string, fgColor string, labelMaskI
func renderArrowheadLabel(fgColor string, connection d2target.Connection, text string, position, width, height float64) string {
labelTL := label.UnlockedTop.GetPointOnRoute(connection.Route, float64(connection.StrokeWidth), position, width, height)
textEl := svgstyle.NewThemableElement("text")
textEl := d2themes.NewThemableElement("text")
textEl.X = labelTL.X + width/2
textEl.Y = labelTL.Y + float64(connection.FontSize)
textEl.Fill = fgColor
@ -619,7 +619,7 @@ func renderArrowheadLabel(fgColor string, connection d2target.Connection, text s
}
func renderOval(tl *geo.Point, width, height float64, fill, stroke, style string) string {
el := svgstyle.NewThemableElement("ellipse")
el := d2themes.NewThemableElement("ellipse")
el.Rx = width / 2
el.Ry = height / 2
el.Cx = tl.X + el.Rx
@ -679,10 +679,10 @@ func render3dRect(targetShape d2target.Shape) string {
borderSegments = append(borderSegments,
lineTo(d2target.Point{X: targetShape.Width + d2target.THREE_DEE_OFFSET, Y: -d2target.THREE_DEE_OFFSET}),
)
border := svgstyle.NewThemableElement("path")
border := d2themes.NewThemableElement("path")
border.D = strings.Join(borderSegments, " ")
border.Fill = color.None
_, borderStroke := svgstyle.ShapeTheme(targetShape)
_, borderStroke := d2themes.ShapeTheme(targetShape)
border.Stroke = borderStroke
borderStyle := targetShape.CSSStyle()
border.Style = borderStyle
@ -702,13 +702,13 @@ func render3dRect(targetShape d2target.Shape) string {
}, "\n")
// render the main rectangle without stroke and the border mask
mainShape := svgstyle.NewThemableElement("rect")
mainShape := d2themes.NewThemableElement("rect")
mainShape.X = float64(targetShape.Pos.X)
mainShape.Y = float64(targetShape.Pos.Y)
mainShape.Width = float64(targetShape.Width)
mainShape.Height = float64(targetShape.Height)
mainShape.Mask = fmt.Sprintf("url(#%s)", maskID)
mainShapeFill, _ := svgstyle.ShapeTheme(targetShape)
mainShapeFill, _ := d2themes.ShapeTheme(targetShape)
mainShape.Fill = mainShapeFill
mainShape.Stroke = color.None
mainShape.Style = targetShape.CSSStyle()
@ -733,7 +733,7 @@ func render3dRect(targetShape d2target.Shape) string {
if err != nil {
darkerColor = targetShape.Fill
}
sideShape := svgstyle.NewThemableElement("polygon")
sideShape := d2themes.NewThemableElement("polygon")
sideShape.Fill = darkerColor
sideShape.Points = strings.Join(sidePoints, " ")
sideShape.Mask = fmt.Sprintf("url(#%s)", maskID)
@ -758,7 +758,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
tl := geo.NewPoint(float64(targetShape.Pos.X), float64(targetShape.Pos.Y))
width := float64(targetShape.Width)
height := float64(targetShape.Height)
fill, stroke := svgstyle.ShapeTheme(targetShape)
fill, stroke := d2themes.ShapeTheme(targetShape)
style := targetShape.CSSStyle()
shapeType := d2target.DSL_SHAPE_TO_SHAPE_TYPE[targetShape.Type]
@ -847,7 +847,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
}
case d2target.ShapeImage:
el := svgstyle.NewThemableElement("image")
el := d2themes.NewThemableElement("image")
el.X = float64(targetShape.Pos.X)
el.Y = float64(targetShape.Pos.Y)
el.Width = float64(targetShape.Width)
@ -870,7 +870,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
} else {
if !targetShape.DoubleBorder {
if targetShape.Multiple {
el := svgstyle.NewThemableElement("rect")
el := d2themes.NewThemableElement("rect")
el.X = float64(targetShape.Pos.X + 10)
el.Y = float64(targetShape.Pos.Y - 10)
el.Width = float64(targetShape.Width)
@ -888,7 +888,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
}
fmt.Fprint(writer, out)
} else {
el := svgstyle.NewThemableElement("rect")
el := d2themes.NewThemableElement("rect")
el.X = float64(targetShape.Pos.X)
el.Y = float64(targetShape.Pos.Y)
el.Width = float64(targetShape.Width)
@ -901,7 +901,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
}
} else {
if targetShape.Multiple {
el := svgstyle.NewThemableElement("rect")
el := d2themes.NewThemableElement("rect")
el.X = float64(targetShape.Pos.X + 10)
el.Y = float64(targetShape.Pos.Y - 10)
el.Width = float64(targetShape.Width)
@ -912,7 +912,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
el.Attributes = rx
fmt.Fprint(writer, el.Render())
el = svgstyle.NewThemableElement("rect")
el = d2themes.NewThemableElement("rect")
el.X = float64(targetShape.Pos.X + 10 + d2target.INNER_BORDER_OFFSET)
el.Y = float64(targetShape.Pos.Y - 10 + d2target.INNER_BORDER_OFFSET)
el.Width = float64(targetShape.Width - 2*d2target.INNER_BORDER_OFFSET)
@ -930,7 +930,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
}
fmt.Fprint(writer, out)
} else {
el := svgstyle.NewThemableElement("rect")
el := d2themes.NewThemableElement("rect")
el.X = float64(targetShape.Pos.X)
el.Y = float64(targetShape.Pos.Y)
el.Width = float64(targetShape.Width)
@ -941,7 +941,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
el.Attributes = rx
fmt.Fprint(writer, el.Render())
el = svgstyle.NewThemableElement("rect")
el = d2themes.NewThemableElement("rect")
el.X = float64(targetShape.Pos.X + d2target.INNER_BORDER_OFFSET)
el.Y = float64(targetShape.Pos.Y + d2target.INNER_BORDER_OFFSET)
el.Width = float64(targetShape.Width - 2*d2target.INNER_BORDER_OFFSET)
@ -958,7 +958,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
default:
if targetShape.Multiple {
multiplePathData := shape.NewShape(shapeType, geo.NewBox(multipleTL, width, height)).GetSVGPathData()
el := svgstyle.NewThemableElement("path")
el := d2themes.NewThemableElement("path")
el.Fill = fill
el.Stroke = stroke
el.Style = style
@ -975,7 +975,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
}
fmt.Fprint(writer, out)
} else {
el := svgstyle.NewThemableElement("path")
el := d2themes.NewThemableElement("path")
el.Fill = fill
el.Stroke = stroke
el.Style = style
@ -1058,7 +1058,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
svgStyles := styleToSVG(style)
fmt.Fprintf(writer, `<g transform="translate(%f %f)">`, box.TopLeft.X, box.TopLeft.Y)
rectEl := svgstyle.NewThemableElement("rect")
rectEl := d2themes.NewThemableElement("rect")
rectEl.Width = float64(targetShape.Width)
rectEl.Height = float64(targetShape.Height)
rectEl.Stroke = targetShape.Stroke
@ -1087,8 +1087,8 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
if err != nil {
return labelMask, err
}
gEl := svgstyle.NewThemableElement("g")
gEl.Transform = fmt.Sprintf("translate(%f %f)", box.TopLeft.X, box.TopLeft.Y)
gEl := d2themes.NewThemableElement("g")
gEl.SetTranslate(float64(box.TopLeft.X), float64(box.TopLeft.Y))
gEl.Color = targetShape.Stroke
gEl.Content = render
fmt.Fprint(writer, gEl.Render())
@ -1103,7 +1103,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
// we need the self closing form in this svg/xhtml context
render = strings.ReplaceAll(render, "<hr>", "<hr />")
mdEl := svgstyle.NewThemableElement("div")
mdEl := d2themes.NewThemableElement("div")
mdEl.Xmlns = "http://www.w3.org/1999/xhtml"
mdEl.Class = "md"
mdEl.Content = render
@ -1115,7 +1115,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
fontColor = targetShape.Color
}
if targetShape.LabelFill != "" {
rectEl := svgstyle.NewThemableElement("rect")
rectEl := d2themes.NewThemableElement("rect")
rectEl.X = labelTL.X
rectEl.Y = labelTL.Y
rectEl.Width = float64(targetShape.LabelWidth)
@ -1123,7 +1123,7 @@ func drawShape(writer io.Writer, targetShape d2target.Shape, sketchRunner *d2ske
rectEl.Fill = targetShape.LabelFill
fmt.Fprint(writer, rectEl.Render())
}
textEl := svgstyle.NewThemableElement("text")
textEl := d2themes.NewThemableElement("text")
textEl.X = labelTL.X + float64(targetShape.LabelWidth)/2
// text is vertically positioned at its baseline which is at labelTL+FontSize
textEl.Y = labelTL.Y + float64(targetShape.FontSize)
@ -1560,7 +1560,7 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
// TODO minify
// TODO background stuff. e.g. dotted, grid, colors
backgroundEl := svgstyle.NewThemableElement("rect")
backgroundEl := d2themes.NewThemableElement("rect")
backgroundEl.X = float64(left)
backgroundEl.Y = float64(top)
backgroundEl.Width = float64(w)

View file

@ -5,15 +5,15 @@ import (
"io"
"oss.terrastruct.com/d2/d2target"
"oss.terrastruct.com/d2/d2themes"
"oss.terrastruct.com/d2/lib/geo"
"oss.terrastruct.com/d2/lib/label"
"oss.terrastruct.com/d2/lib/svg"
svgstyle "oss.terrastruct.com/d2/lib/svg/style"
"oss.terrastruct.com/util-go/go2"
)
func tableHeader(shape d2target.Shape, box *geo.Box, text string, textWidth, textHeight, fontSize float64) string {
rectEl := svgstyle.NewThemableElement("rect")
rectEl := d2themes.NewThemableElement("rect")
rectEl.X, rectEl.Y = box.TopLeft.X, box.TopLeft.Y
rectEl.Width, rectEl.Height = box.Width, box.Height
rectEl.Fill = shape.Fill
@ -28,7 +28,7 @@ func tableHeader(shape d2target.Shape, box *geo.Box, text string, textWidth, tex
textHeight,
)
textEl := svgstyle.NewThemableElement("text")
textEl := d2themes.NewThemableElement("text")
textEl.X = tl.X
textEl.Y = tl.Y + textHeight*3/4
textEl.Fill = shape.Stroke
@ -58,7 +58,7 @@ func tableRow(shape d2target.Shape, box *geo.Box, nameText, typeText, constraint
fontSize,
)
textEl := svgstyle.NewThemableElement("text")
textEl := d2themes.NewThemableElement("text")
textEl.X = nameTL.X
textEl.Y = nameTL.Y + fontSize*3/4
textEl.Fill = shape.PrimaryAccentColor
@ -83,12 +83,12 @@ func tableRow(shape d2target.Shape, box *geo.Box, nameText, typeText, constraint
}
func drawTable(writer io.Writer, targetShape d2target.Shape) {
rectEl := svgstyle.NewThemableElement("rect")
rectEl := d2themes.NewThemableElement("rect")
rectEl.X = float64(targetShape.Pos.X)
rectEl.Y = float64(targetShape.Pos.Y)
rectEl.Width = float64(targetShape.Width)
rectEl.Height = float64(targetShape.Height)
rectEl.Fill, rectEl.Stroke = svgstyle.ShapeTheme(targetShape)
rectEl.Fill, rectEl.Stroke = d2themes.ShapeTheme(targetShape)
rectEl.Class = "shape"
rectEl.Style = targetShape.CSSStyle()
fmt.Fprint(writer, rectEl.Render())
@ -119,7 +119,7 @@ func drawTable(writer io.Writer, targetShape d2target.Shape) {
)
rowBox.TopLeft.Y += rowHeight
lineEl := svgstyle.NewThemableElement("line")
lineEl := d2themes.NewThemableElement("line")
lineEl.X1, lineEl.Y1 = rowBox.TopLeft.X, rowBox.TopLeft.Y
lineEl.X2, lineEl.Y2 = rowBox.TopLeft.X+rowBox.Width, rowBox.TopLeft.Y
lineEl.Stroke = targetShape.Fill

View file

@ -1,4 +1,4 @@
package style
package d2themes
import (
"oss.terrastruct.com/d2/d2target"

View file

@ -1,4 +1,4 @@
package style
package d2themes
import (
"fmt"
@ -80,6 +80,10 @@ func NewThemableElement(tag string) *ThemableElement {
}
}
func (el *ThemableElement) SetTranslate(x, y float64) {
el.Transform = fmt.Sprintf("translate(%f %f)", x, y)
}
func (el *ThemableElement) Render() string {
out := "<" + el.tag

View file

@ -1,4 +1,4 @@
package style
package d2themes
import (
"fmt"