d2/d2renderers/d2svg/class.go

129 lines
3.7 KiB
Go
Raw Normal View History

package d2svg
import (
"fmt"
"io"
"oss.terrastruct.com/d2/d2target"
"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"
2023-01-30 11:06:54 +00:00
svgstyle "oss.terrastruct.com/d2/lib/svg/style"
)
2022-12-24 23:56:22 +00:00
func classHeader(shape d2target.Shape, box *geo.Box, text string, textWidth, textHeight, fontSize float64) string {
2023-01-30 11:06:54 +00:00
rectEl := svgstyle.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
rectEl.Class = "class_header"
str := rectEl.Render()
if text != "" {
tl := label.InsideMiddleCenter.GetPointOnBox(
box,
0,
textWidth,
textHeight,
)
2023-01-30 11:06:54 +00:00
textEl := svgstyle.NewThemableElement("text")
2023-01-09 18:16:28 +00:00
textEl.X = tl.X + textWidth/2
textEl.Y = tl.Y + textHeight*3/4
textEl.Fill = shape.Stroke
textEl.Class = "text-mono"
textEl.Style = fmt.Sprintf(`text-anchor:%s;font-size:%vpx;`,
"middle", 4+fontSize,
)
2023-01-09 18:16:28 +00:00
textEl.Content = svg.EscapeText(text)
str += textEl.Render()
}
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 {
// 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,
box.Width,
fontSize,
)
typeTR := label.InsideMiddleRight.GetPointOnBox(
box,
2022-12-22 19:32:41 +00:00
d2target.TypePadding,
0,
fontSize,
)
2023-01-30 11:06:54 +00:00
textEl := svgstyle.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
textEl.Class = "text-mono"
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
}
func drawClass(writer io.Writer, targetShape d2target.Shape) {
2023-01-30 11:06:54 +00:00
el := svgstyle.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-01-30 11:06:54 +00:00
el.Fill, el.Stroke = svgstyle.ShapeTheme(targetShape)
2023-01-15 20:36:43 +00:00
el.Style = targetShape.CSSStyle()
2023-01-09 18:16:28 +00:00
fmt.Fprint(writer, el.Render())
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)),
)
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 {
fmt.Fprint(writer,
2022-12-24 23:56:22 +00:00
classRow(targetShape, rowBox, f.VisibilityToken(), f.Name, f.Type, float64(targetShape.FontSize)),
)
rowBox.TopLeft.Y += rowHeight
}
2023-01-30 11:06:54 +00:00
lineEl := svgstyle.NewThemableElement("line")
2023-01-09 18:16:28 +00:00
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
lineEl.Style = "stroke-width:1"
fmt.Fprint(writer, lineEl.Render())
2022-12-22 19:32:41 +00:00
for _, m := range targetShape.Methods {
fmt.Fprint(writer,
2022-12-24 23:56:22 +00:00
classRow(targetShape, rowBox, m.VisibilityToken(), m.Name, m.Return, float64(targetShape.FontSize)),
)
rowBox.TopLeft.Y += rowHeight
}
}