2022-11-03 13:54:49 +00:00
|
|
|
package d2svg
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
2023-03-06 10:48:00 +00:00
|
|
|
"strings"
|
2022-11-03 13:54:49 +00:00
|
|
|
|
|
|
|
|
"oss.terrastruct.com/d2/d2target"
|
2023-02-19 12:00:01 +00:00
|
|
|
"oss.terrastruct.com/d2/d2themes"
|
2022-11-03 13:54:49 +00:00
|
|
|
"oss.terrastruct.com/d2/lib/geo"
|
|
|
|
|
"oss.terrastruct.com/d2/lib/label"
|
2022-12-22 19:06:57 +00:00
|
|
|
"oss.terrastruct.com/d2/lib/svg"
|
2022-11-03 13:54:49 +00:00
|
|
|
)
|
|
|
|
|
|
2022-12-24 23:56:22 +00:00
|
|
|
func classHeader(shape d2target.Shape, box *geo.Box, text string, textWidth, textHeight, fontSize float64) string {
|
2023-02-19 12:00:01 +00:00
|
|
|
rectEl := d2themes.NewThemableElement("rect")
|
2023-01-09 18:16:28 +00:00
|
|
|
rectEl.X, rectEl.Y = box.TopLeft.X, box.TopLeft.Y
|
|
|
|
|
rectEl.Width, rectEl.Height = box.Width, box.Height
|
|
|
|
|
rectEl.Fill = shape.Fill
|
2023-02-19 13:26:24 +00:00
|
|
|
rectEl.ClassName = "class_header"
|
2023-01-09 18:16:28 +00:00
|
|
|
str := rectEl.Render()
|
2023-03-06 10:48:00 +00:00
|
|
|
if shape.BorderRadius != 0 {
|
|
|
|
|
str = strings.Replace(str, "/>", fmt.Sprintf(`clip-path="url(#%v)" />`, shape.ID), 1)
|
|
|
|
|
}
|
2022-11-03 13:54:49 +00:00
|
|
|
|
|
|
|
|
if text != "" {
|
|
|
|
|
tl := label.InsideMiddleCenter.GetPointOnBox(
|
|
|
|
|
box,
|
|
|
|
|
0,
|
|
|
|
|
textWidth,
|
|
|
|
|
textHeight,
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-19 12:00:01 +00:00
|
|
|
textEl := d2themes.NewThemableElement("text")
|
2023-01-09 18:16:28 +00:00
|
|
|
textEl.X = tl.X + textWidth/2
|
|
|
|
|
textEl.Y = tl.Y + textHeight*3/4
|
2023-02-25 04:26:40 +00:00
|
|
|
textEl.Fill = shape.GetFontColor()
|
2023-02-19 13:26:24 +00:00
|
|
|
textEl.ClassName = "text-mono"
|
2023-01-09 18:16:28 +00:00
|
|
|
textEl.Style = fmt.Sprintf(`text-anchor:%s;font-size:%vpx;`,
|
|
|
|
|
"middle", 4+fontSize,
|
2022-11-03 13:54:49 +00:00
|
|
|
)
|
2023-01-09 18:16:28 +00:00
|
|
|
textEl.Content = svg.EscapeText(text)
|
|
|
|
|
str += textEl.Render()
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-24 23:56:22 +00:00
|
|
|
func classRow(shape d2target.Shape, box *geo.Box, prefix, nameText, typeText string, fontSize float64) string {
|
2022-11-03 13:54:49 +00:00
|
|
|
// Row is made up of prefix, name, and type
|
|
|
|
|
// e.g. | + firstName string |
|
|
|
|
|
prefixTL := label.InsideMiddleLeft.GetPointOnBox(
|
|
|
|
|
box,
|
2022-12-22 19:32:41 +00:00
|
|
|
d2target.PrefixPadding,
|
2022-11-03 13:54:49 +00:00
|
|
|
box.Width,
|
|
|
|
|
fontSize,
|
|
|
|
|
)
|
|
|
|
|
typeTR := label.InsideMiddleRight.GetPointOnBox(
|
|
|
|
|
box,
|
2022-12-22 19:32:41 +00:00
|
|
|
d2target.TypePadding,
|
2022-11-03 13:54:49 +00:00
|
|
|
0,
|
|
|
|
|
fontSize,
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-19 12:00:01 +00:00
|
|
|
textEl := d2themes.NewThemableElement("text")
|
2023-01-09 18:16:28 +00:00
|
|
|
textEl.X = prefixTL.X
|
|
|
|
|
textEl.Y = prefixTL.Y + fontSize*3/4
|
|
|
|
|
textEl.Fill = shape.PrimaryAccentColor
|
2023-02-19 13:26:24 +00:00
|
|
|
textEl.ClassName = "text-mono"
|
2023-01-09 18:16:28 +00:00
|
|
|
textEl.Style = fmt.Sprintf("text-anchor:%s;font-size:%vpx", "start", fontSize)
|
|
|
|
|
textEl.Content = prefix
|
|
|
|
|
out := textEl.Render()
|
|
|
|
|
|
|
|
|
|
textEl.X = prefixTL.X + d2target.PrefixWidth
|
|
|
|
|
textEl.Fill = shape.Fill
|
|
|
|
|
textEl.Content = svg.EscapeText(nameText)
|
|
|
|
|
out += textEl.Render()
|
|
|
|
|
|
|
|
|
|
textEl.X = typeTR.X
|
|
|
|
|
textEl.Y = typeTR.Y + fontSize*3/4
|
|
|
|
|
textEl.Fill = shape.SecondaryAccentColor
|
|
|
|
|
textEl.Style = fmt.Sprintf("text-anchor:%s;font-size:%vpx", "end", fontSize)
|
|
|
|
|
textEl.Content = svg.EscapeText(typeText)
|
|
|
|
|
out += textEl.Render()
|
|
|
|
|
|
|
|
|
|
return out
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func drawClass(writer io.Writer, targetShape d2target.Shape) {
|
2023-02-19 12:00:01 +00:00
|
|
|
el := d2themes.NewThemableElement("rect")
|
2023-01-09 18:16:28 +00:00
|
|
|
el.X = float64(targetShape.Pos.X)
|
|
|
|
|
el.Y = float64(targetShape.Pos.Y)
|
|
|
|
|
el.Width = float64(targetShape.Width)
|
|
|
|
|
el.Height = float64(targetShape.Height)
|
2023-02-19 12:00:01 +00:00
|
|
|
el.Fill, el.Stroke = d2themes.ShapeTheme(targetShape)
|
2023-01-15 20:36:43 +00:00
|
|
|
el.Style = targetShape.CSSStyle()
|
2023-03-09 03:03:00 +00:00
|
|
|
if targetShape.BorderRadius != 0 {
|
|
|
|
|
el.Rx = float64(targetShape.BorderRadius)
|
|
|
|
|
el.Ry = float64(targetShape.BorderRadius)
|
|
|
|
|
}
|
2023-01-09 18:16:28 +00:00
|
|
|
fmt.Fprint(writer, el.Render())
|
2022-11-03 13:54:49 +00:00
|
|
|
|
|
|
|
|
box := geo.NewBox(
|
|
|
|
|
geo.NewPoint(float64(targetShape.Pos.X), float64(targetShape.Pos.Y)),
|
|
|
|
|
float64(targetShape.Width),
|
|
|
|
|
float64(targetShape.Height),
|
|
|
|
|
)
|
|
|
|
|
rowHeight := box.Height / float64(2+len(targetShape.Class.Fields)+len(targetShape.Class.Methods))
|
|
|
|
|
headerBox := geo.NewBox(box.TopLeft, box.Width, 2*rowHeight)
|
|
|
|
|
|
|
|
|
|
fmt.Fprint(writer,
|
2022-12-24 23:56:22 +00:00
|
|
|
classHeader(targetShape, headerBox, targetShape.Label, float64(targetShape.LabelWidth), float64(targetShape.LabelHeight), float64(targetShape.FontSize)),
|
2022-11-03 13:54:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
|
|
|
|
|
rowBox.TopLeft.Y += headerBox.Height
|
2022-12-22 19:32:41 +00:00
|
|
|
for _, f := range targetShape.Fields {
|
2022-11-03 13:54:49 +00:00
|
|
|
fmt.Fprint(writer,
|
2022-12-24 23:56:22 +00:00
|
|
|
classRow(targetShape, rowBox, f.VisibilityToken(), f.Name, f.Type, float64(targetShape.FontSize)),
|
2022-11-03 13:54:49 +00:00
|
|
|
)
|
|
|
|
|
rowBox.TopLeft.Y += rowHeight
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 12:00:01 +00:00
|
|
|
lineEl := d2themes.NewThemableElement("line")
|
2023-03-08 08:46:32 +00:00
|
|
|
|
|
|
|
|
if targetShape.BorderRadius != 0 && len(targetShape.Methods) == 0 {
|
|
|
|
|
lineEl.X1, lineEl.Y1 = rowBox.TopLeft.X+float64(targetShape.BorderRadius), rowBox.TopLeft.Y
|
|
|
|
|
lineEl.X2, lineEl.Y2 = rowBox.TopLeft.X+rowBox.Width-float64(targetShape.BorderRadius), rowBox.TopLeft.Y
|
|
|
|
|
} else {
|
|
|
|
|
lineEl.X1, lineEl.Y1 = rowBox.TopLeft.X, rowBox.TopLeft.Y
|
|
|
|
|
lineEl.X2, lineEl.Y2 = rowBox.TopLeft.X+rowBox.Width, rowBox.TopLeft.Y
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-09 18:16:28 +00:00
|
|
|
lineEl.Stroke = targetShape.Fill
|
|
|
|
|
lineEl.Style = "stroke-width:1"
|
|
|
|
|
fmt.Fprint(writer, lineEl.Render())
|
2022-11-03 13:54:49 +00:00
|
|
|
|
2022-12-22 19:32:41 +00:00
|
|
|
for _, m := range targetShape.Methods {
|
2022-11-03 13:54:49 +00:00
|
|
|
fmt.Fprint(writer,
|
2022-12-24 23:56:22 +00:00
|
|
|
classRow(targetShape, rowBox, m.VisibilityToken(), m.Name, m.Return, float64(targetShape.FontSize)),
|
2022-11-03 13:54:49 +00:00
|
|
|
)
|
|
|
|
|
rowBox.TopLeft.Y += rowHeight
|
|
|
|
|
}
|
|
|
|
|
}
|