d2/d2renderers/d2sketch/sketch.go

883 lines
24 KiB
Go
Raw Normal View History

2022-12-21 07:43:45 +00:00
package d2sketch
import (
"bytes"
2022-12-21 07:43:45 +00:00
"encoding/json"
"fmt"
2023-01-13 04:25:02 +00:00
"regexp"
2022-12-22 19:06:57 +00:00
"strings"
2022-12-21 07:43:45 +00:00
_ "embed"
"github.com/dop251/goja"
"oss.terrastruct.com/d2/d2target"
"oss.terrastruct.com/d2/d2themes"
2023-01-09 18:16:28 +00:00
"oss.terrastruct.com/d2/lib/color"
2022-12-22 19:06:57 +00:00
"oss.terrastruct.com/d2/lib/geo"
"oss.terrastruct.com/d2/lib/label"
2022-12-21 07:43:45 +00:00
"oss.terrastruct.com/d2/lib/svg"
2022-12-22 19:06:57 +00:00
"oss.terrastruct.com/util-go/go2"
2022-12-21 07:43:45 +00:00
)
//go:embed rough.js
var roughJS string
//go:embed setup.js
var setupJS string
//go:embed streaks.txt
var streaks string
2022-12-21 07:43:45 +00:00
type Runner goja.Runtime
var baseRoughProps = `fillWeight: 2.0,
hachureGap: 16,
fillStyle: "solid",
bowing: 2,
seed: 1,`
2023-01-13 04:25:02 +00:00
var floatRE = regexp.MustCompile(`(\d+)\.(\d+)`)
const (
BG_COLOR = color.N7
FG_COLOR = color.N1
)
2022-12-21 07:43:45 +00:00
func (r *Runner) run(js string) (goja.Value, error) {
vm := (*goja.Runtime)(r)
return vm.RunString(js)
}
func InitSketchVM() (*Runner, error) {
vm := goja.New()
if _, err := vm.RunString(roughJS); err != nil {
return nil, err
}
if _, err := vm.RunString(setupJS); err != nil {
return nil, err
}
r := Runner(*vm)
return &r, nil
}
// DefineFillPatterns adds reusable patterns that are overlayed on shapes with
2022-12-21 07:43:45 +00:00
// fill. This gives it a subtle streaky effect that subtly looks hand-drawn but
// not distractingly so.
func DefineFillPatterns(buf *bytes.Buffer) {
source := buf.String()
fmt.Fprint(buf, "<defs>")
defineFillPattern(buf, source, "bright", "rgba(0, 0, 0, 0.1)")
defineFillPattern(buf, source, "normal", "rgba(0, 0, 0, 0.16)")
defineFillPattern(buf, source, "dark", "rgba(0, 0, 0, 0.32)")
defineFillPattern(buf, source, "darker", "rgba(255, 255, 255, 0.24)")
fmt.Fprint(buf, "</defs>")
}
func defineFillPattern(buf *bytes.Buffer, source string, luminanceCategory, fill string) {
trigger := fmt.Sprintf(`url(#streaks-%s)`, luminanceCategory)
if strings.Contains(source, trigger) {
fmt.Fprintf(buf, streaks, luminanceCategory, fill)
}
2022-12-21 07:43:45 +00:00
}
func Rect(r *Runner, shape d2target.Shape) (string, error) {
js := fmt.Sprintf(`node = rc.rectangle(0, 0, %d, %d, {
2023-01-09 18:16:28 +00:00
fill: "#000",
stroke: "#000",
2022-12-21 07:43:45 +00:00
strokeWidth: %d,
%s
2023-01-09 18:16:28 +00:00
});`, shape.Width, shape.Height, shape.StrokeWidth, baseRoughProps)
2023-01-13 02:27:53 +00:00
paths, err := computeRoughPathData(r, js)
2022-12-21 07:43:45 +00:00
if err != nil {
return "", err
}
output := ""
pathEl := d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
2023-03-14 07:11:41 +00:00
pathEl.FillPattern = shape.FillPattern
pathEl.ClassName = "shape"
2023-01-15 20:36:43 +00:00
pathEl.Style = shape.CSSStyle()
2022-12-21 07:43:45 +00:00
for _, p := range paths {
2023-01-09 18:16:28 +00:00
pathEl.D = p
output += pathEl.Render()
2022-12-21 07:43:45 +00:00
}
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 := d2themes.NewThemableSketchOverlay(sketchOEl, pathEl.Fill).Render()
if err != nil {
return "", err
}
output += renderedSO
2022-12-21 07:43:45 +00:00
return output, nil
}
2022-12-31 07:57:22 +00:00
func DoubleRect(r *Runner, shape d2target.Shape) (string, error) {
jsBigRect := fmt.Sprintf(`node = rc.rectangle(0, 0, %d, %d, {
2023-01-27 21:30:44 +00:00
fill: "#000",
stroke: "#000",
2022-12-31 07:57:22 +00:00
strokeWidth: %d,
%s
2023-01-27 21:30:44 +00:00
});`, shape.Width, shape.Height, shape.StrokeWidth, baseRoughProps)
2023-01-24 09:29:38 +00:00
pathsBigRect, err := computeRoughPathData(r, jsBigRect)
2022-12-31 07:57:22 +00:00
if err != nil {
return "", err
}
jsSmallRect := fmt.Sprintf(`node = rc.rectangle(0, 0, %d, %d, {
2023-01-27 21:30:44 +00:00
fill: "#000",
stroke: "#000",
2022-12-31 07:57:22 +00:00
strokeWidth: %d,
%s
2023-01-27 21:30:44 +00:00
});`, shape.Width-d2target.INNER_BORDER_OFFSET*2, shape.Height-d2target.INNER_BORDER_OFFSET*2, shape.StrokeWidth, baseRoughProps)
2023-01-24 09:29:38 +00:00
pathsSmallRect, err := computeRoughPathData(r, jsSmallRect)
2022-12-31 07:57:22 +00:00
if err != nil {
return "", err
}
2023-01-27 21:30:44 +00:00
2022-12-31 07:57:22 +00:00
output := ""
2023-01-27 21:30:44 +00:00
pathEl := d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
2023-03-14 07:11:41 +00:00
pathEl.FillPattern = shape.FillPattern
pathEl.ClassName = "shape"
2023-01-27 21:30:44 +00:00
pathEl.Style = shape.CSSStyle()
2022-12-31 07:57:22 +00:00
for _, p := range pathsBigRect {
2023-01-27 21:30:44 +00:00
pathEl.D = p
output += pathEl.Render()
2022-12-31 07:57:22 +00:00
}
2023-01-27 21:30:44 +00:00
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)
2023-02-28 03:17:19 +00:00
// No need for inner to double paint
pathEl.Fill = "transparent"
pathEl.ClassName = "shape"
2023-01-27 21:30:44 +00:00
pathEl.Style = shape.CSSStyle()
2022-12-31 07:57:22 +00:00
for _, p := range pathsSmallRect {
2023-01-27 21:30:44 +00:00
pathEl.D = p
output += pathEl.Render()
2022-12-31 07:57:22 +00:00
}
2023-01-27 21:30:44 +00:00
2023-02-28 03:17:19 +00:00
sketchOEl := d2themes.NewThemableElement("rect")
sketchOEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
sketchOEl.Width = float64(shape.Width)
sketchOEl.Height = float64(shape.Height)
2023-02-28 03:26:19 +00:00
renderedSO, err := d2themes.NewThemableSketchOverlay(sketchOEl, shape.Fill).Render()
2023-02-28 03:17:19 +00:00
if err != nil {
return "", err
}
output += renderedSO
2022-12-31 07:57:22 +00:00
return output, nil
}
2022-12-21 07:43:45 +00:00
func Oval(r *Runner, shape d2target.Shape) (string, error) {
js := fmt.Sprintf(`node = rc.ellipse(%d, %d, %d, %d, {
2023-01-09 18:16:28 +00:00
fill: "#000",
stroke: "#000",
2022-12-21 07:43:45 +00:00
strokeWidth: %d,
%s
2023-01-09 18:16:28 +00:00
});`, shape.Width/2, shape.Height/2, shape.Width, shape.Height, shape.StrokeWidth, baseRoughProps)
2023-01-13 02:27:53 +00:00
paths, err := computeRoughPathData(r, js)
2022-12-21 07:43:45 +00:00
if err != nil {
return "", err
}
output := ""
pathEl := d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
2023-03-14 16:39:49 +00:00
pathEl.FillPattern = shape.FillPattern
pathEl.ClassName = "shape"
2023-01-15 20:36:43 +00:00
pathEl.Style = shape.CSSStyle()
2022-12-21 07:43:45 +00:00
for _, p := range paths {
2023-01-09 18:16:28 +00:00
pathEl.D = p
output += pathEl.Render()
2022-12-21 07:43:45 +00:00
}
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 := d2themes.NewThemableSketchOverlay(
soElement,
pathEl.Fill,
).Render()
if err != nil {
return "", err
}
output += renderedSO
2022-12-21 07:43:45 +00:00
return output, nil
}
2022-12-30 09:30:29 +00:00
func DoubleOval(r *Runner, shape d2target.Shape) (string, error) {
2022-12-31 06:17:34 +00:00
jsBigCircle := fmt.Sprintf(`node = rc.ellipse(%d, %d, %d, %d, {
2023-01-27 21:30:44 +00:00
fill: "#000",
stroke: "#000",
2022-12-30 09:30:29 +00:00
strokeWidth: %d,
%s
2023-01-27 21:30:44 +00:00
});`, shape.Width/2, shape.Height/2, shape.Width, shape.Height, shape.StrokeWidth, baseRoughProps)
2022-12-31 06:17:34 +00:00
jsSmallCircle := fmt.Sprintf(`node = rc.ellipse(%d, %d, %d, %d, {
2023-01-27 21:30:44 +00:00
fill: "#000",
stroke: "#000",
2022-12-30 09:30:29 +00:00
strokeWidth: %d,
%s
2023-01-27 21:30:44 +00:00
});`, shape.Width/2, shape.Height/2, shape.Width-d2target.INNER_BORDER_OFFSET*2, shape.Height-d2target.INNER_BORDER_OFFSET*2, shape.StrokeWidth, baseRoughProps)
2023-01-24 09:29:38 +00:00
pathsBigCircle, err := computeRoughPathData(r, jsBigCircle)
2022-12-30 09:30:29 +00:00
if err != nil {
return "", err
}
2023-01-24 09:29:38 +00:00
pathsSmallCircle, err := computeRoughPathData(r, jsSmallCircle)
2022-12-30 09:30:29 +00:00
if err != nil {
return "", err
}
2023-01-27 21:30:44 +00:00
2022-12-30 09:30:29 +00:00
output := ""
2023-01-27 21:30:44 +00:00
pathEl := d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
2023-03-14 16:39:49 +00:00
pathEl.FillPattern = shape.FillPattern
pathEl.ClassName = "shape"
2023-01-27 21:30:44 +00:00
pathEl.Style = shape.CSSStyle()
2022-12-31 07:57:22 +00:00
for _, p := range pathsBigCircle {
2023-01-27 21:30:44 +00:00
pathEl.D = p
output += pathEl.Render()
2022-12-30 09:30:29 +00:00
}
2023-01-27 21:30:44 +00:00
pathEl = d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
2023-02-28 03:26:19 +00:00
// No need for inner to double paint
pathEl.Fill = "transparent"
pathEl.ClassName = "shape"
2023-01-27 21:30:44 +00:00
pathEl.Style = shape.CSSStyle()
2022-12-31 07:57:22 +00:00
for _, p := range pathsSmallCircle {
2023-01-27 21:30:44 +00:00
pathEl.D = p
output += pathEl.Render()
2022-12-30 09:30:29 +00:00
}
2023-02-28 03:26:19 +00:00
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 := d2themes.NewThemableSketchOverlay(
soElement,
shape.Fill,
).Render()
if err != nil {
return "", err
}
output += renderedSO
2023-01-27 21:30:44 +00:00
2022-12-30 09:30:29 +00:00
return output, nil
}
2022-12-21 07:43:45 +00:00
// TODO need to personalize this per shape like we do in Terrastruct app
func Paths(r *Runner, shape d2target.Shape, paths []string) (string, error) {
output := ""
for _, path := range paths {
js := fmt.Sprintf(`node = rc.path("%s", {
2023-01-09 18:16:28 +00:00
fill: "#000",
stroke: "#000",
2022-12-21 07:43:45 +00:00
strokeWidth: %d,
%s
2023-01-09 18:16:28 +00:00
});`, path, shape.StrokeWidth, baseRoughProps)
2023-01-13 02:27:53 +00:00
sketchPaths, err := computeRoughPathData(r, js)
2022-12-21 07:43:45 +00:00
if err != nil {
return "", err
}
pathEl := d2themes.NewThemableElement("path")
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
2023-03-14 16:39:49 +00:00
pathEl.FillPattern = shape.FillPattern
pathEl.ClassName = "shape"
2023-01-15 20:36:43 +00:00
pathEl.Style = shape.CSSStyle()
2022-12-21 07:43:45 +00:00
for _, p := range sketchPaths {
2023-01-09 18:16:28 +00:00
pathEl.D = p
output += pathEl.Render()
2022-12-21 07:43:45 +00:00
}
soElement := d2themes.NewThemableElement("path")
2022-12-21 07:43:45 +00:00
for _, p := range sketchPaths {
soElement.D = p
renderedSO, err := d2themes.NewThemableSketchOverlay(
soElement,
pathEl.Fill,
).Render()
if err != nil {
return "", err
}
output += renderedSO
2022-12-21 07:43:45 +00:00
}
}
return output, nil
}
func Connection(r *Runner, connection d2target.Connection, path, attrs string) (string, error) {
2023-03-11 18:00:50 +00:00
roughness := 0.5
2022-12-21 07:43:45 +00:00
js := fmt.Sprintf(`node = rc.path("%s", {roughness: %f, seed: 1});`, path, roughness)
2023-01-13 02:27:53 +00:00
paths, err := computeRoughPathData(r, js)
2022-12-21 07:43:45 +00:00
if err != nil {
return "", err
}
output := ""
2023-01-16 11:15:59 +00:00
animatedClass := ""
if connection.Animated {
animatedClass = " animated-connection"
}
pathEl := d2themes.NewThemableElement("path")
2023-01-09 18:16:28 +00:00
pathEl.Fill = color.None
pathEl.Stroke = connection.Stroke
pathEl.ClassName = fmt.Sprintf("connection%s", animatedClass)
2023-01-15 20:36:43 +00:00
pathEl.Style = connection.CSSStyle()
2023-01-09 18:16:28 +00:00
pathEl.Attributes = attrs
2022-12-21 07:43:45 +00:00
for _, p := range paths {
2023-01-09 18:16:28 +00:00
pathEl.D = p
output += pathEl.Render()
2022-12-21 07:43:45 +00:00
}
return output, nil
}
2022-12-22 19:06:57 +00:00
// TODO cleanup
func Table(r *Runner, shape d2target.Shape) (string, error) {
output := ""
js := fmt.Sprintf(`node = rc.rectangle(0, 0, %d, %d, {
2023-01-09 18:16:28 +00:00
fill: "#000",
stroke: "#000",
2022-12-22 19:06:57 +00:00
strokeWidth: %d,
%s
2023-01-09 18:16:28 +00:00
});`, shape.Width, shape.Height, shape.StrokeWidth, baseRoughProps)
2023-01-13 02:27:53 +00:00
paths, err := computeRoughPathData(r, js)
2022-12-22 19:06:57 +00:00
if err != nil {
return "", err
}
pathEl := d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
2023-03-16 05:53:12 +00:00
pathEl.FillPattern = shape.FillPattern
pathEl.ClassName = "shape"
2023-01-15 20:36:43 +00:00
pathEl.Style = shape.CSSStyle()
2022-12-22 19:06:57 +00:00
for _, p := range paths {
2023-01-09 18:16:28 +00:00
pathEl.D = p
output += pathEl.Render()
2022-12-22 19:06:57 +00:00
}
box := geo.NewBox(
geo.NewPoint(float64(shape.Pos.X), float64(shape.Pos.Y)),
float64(shape.Width),
float64(shape.Height),
)
rowHeight := box.Height / float64(1+len(shape.SQLTable.Columns))
headerBox := geo.NewBox(box.TopLeft, box.Width, rowHeight)
js = fmt.Sprintf(`node = rc.rectangle(0, 0, %d, %f, {
2023-01-09 18:16:28 +00:00
fill: "#000",
2022-12-22 19:06:57 +00:00
%s
2023-01-09 18:16:28 +00:00
});`, shape.Width, rowHeight, baseRoughProps)
2023-01-13 02:27:53 +00:00
paths, err = computeRoughPathData(r, js)
2022-12-22 19:06:57 +00:00
if err != nil {
return "", err
}
pathEl = d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
2023-01-09 18:16:28 +00:00
pathEl.Fill = shape.Fill
2023-03-16 05:53:12 +00:00
pathEl.FillPattern = shape.FillPattern
pathEl.ClassName = "class_header"
2022-12-22 19:06:57 +00:00
for _, p := range paths {
2023-01-09 18:16:28 +00:00
pathEl.D = p
output += pathEl.Render()
2022-12-22 19:06:57 +00:00
}
if shape.Label != "" {
tl := label.InsideMiddleLeft.GetPointOnBox(
headerBox,
20,
float64(shape.LabelWidth),
float64(shape.LabelHeight),
)
textEl := d2themes.NewThemableElement("text")
2023-01-09 18:16:28 +00:00
textEl.X = tl.X
textEl.Y = tl.Y + float64(shape.LabelHeight)*3/4
2023-02-25 04:26:40 +00:00
textEl.Fill = shape.GetFontColor()
textEl.ClassName = "text"
2023-01-09 18:16:28 +00:00
textEl.Style = fmt.Sprintf("text-anchor:%s;font-size:%vpx",
"start", 4+shape.FontSize,
2022-12-22 19:06:57 +00:00
)
2023-01-09 18:16:28 +00:00
textEl.Content = svg.EscapeText(shape.Label)
output += textEl.Render()
2022-12-22 19:06:57 +00:00
}
var longestNameWidth int
for _, f := range shape.Columns {
longestNameWidth = go2.Max(longestNameWidth, f.Name.LabelWidth)
}
rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
rowBox.TopLeft.Y += headerBox.Height
for _, f := range shape.Columns {
nameTL := label.InsideMiddleLeft.GetPointOnBox(
rowBox,
d2target.NamePadding,
rowBox.Width,
float64(shape.FontSize),
)
constraintTR := label.InsideMiddleRight.GetPointOnBox(
rowBox,
d2target.TypePadding,
0,
float64(shape.FontSize),
)
textEl := d2themes.NewThemableElement("text")
2023-01-09 18:16:28 +00:00
textEl.X = nameTL.X
textEl.Y = nameTL.Y + float64(shape.FontSize)*3/4
textEl.Fill = shape.PrimaryAccentColor
textEl.ClassName = "text"
2023-01-09 18:16:28 +00:00
textEl.Style = fmt.Sprintf("text-anchor:%s;font-size:%vpx", "start", float64(shape.FontSize))
textEl.Content = svg.EscapeText(f.Name.Label)
output += textEl.Render()
textEl.X = nameTL.X + float64(longestNameWidth) + 2*d2target.NamePadding
textEl.Fill = shape.NeutralAccentColor
textEl.Content = svg.EscapeText(f.Type.Label)
output += textEl.Render()
textEl.X = constraintTR.X
textEl.Y = constraintTR.Y + float64(shape.FontSize)*3/4
textEl.Fill = shape.SecondaryAccentColor
textEl.Style = fmt.Sprintf("text-anchor:%s;font-size:%vpx;letter-spacing:2px", "end", float64(shape.FontSize))
textEl.Content = f.ConstraintAbbr()
output += textEl.Render()
2022-12-22 19:06:57 +00:00
rowBox.TopLeft.Y += rowHeight
js = fmt.Sprintf(`node = rc.line(%f, %f, %f, %f, {
%s
});`, rowBox.TopLeft.X, rowBox.TopLeft.Y, rowBox.TopLeft.X+rowBox.Width, rowBox.TopLeft.Y, baseRoughProps)
2023-01-13 02:27:53 +00:00
paths, err = computeRoughPathData(r, js)
2022-12-22 19:06:57 +00:00
if err != nil {
return "", err
}
pathEl := d2themes.NewThemableElement("path")
2023-01-09 18:16:28 +00:00
pathEl.Fill = shape.Fill
2023-03-16 05:53:12 +00:00
pathEl.FillPattern = shape.FillPattern
2022-12-22 19:06:57 +00:00
for _, p := range paths {
2023-01-09 18:16:28 +00:00
pathEl.D = p
output += pathEl.Render()
2022-12-22 19:06:57 +00:00
}
}
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 := d2themes.NewThemableSketchOverlay(sketchOEl, pathEl.Fill).Render()
if err != nil {
return "", err
}
output += renderedSO
2022-12-22 19:06:57 +00:00
return output, nil
}
2022-12-22 19:32:41 +00:00
func Class(r *Runner, shape d2target.Shape) (string, error) {
output := ""
js := fmt.Sprintf(`node = rc.rectangle(0, 0, %d, %d, {
2023-01-09 18:16:28 +00:00
fill: "#000",
stroke: "#000",
2022-12-22 19:32:41 +00:00
strokeWidth: %d,
%s
2023-01-09 18:16:28 +00:00
});`, shape.Width, shape.Height, shape.StrokeWidth, baseRoughProps)
2023-01-13 02:27:53 +00:00
paths, err := computeRoughPathData(r, js)
2022-12-22 19:32:41 +00:00
if err != nil {
return "", err
}
pathEl := d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
pathEl.Fill, pathEl.Stroke = d2themes.ShapeTheme(shape)
2023-03-16 05:53:12 +00:00
pathEl.FillPattern = shape.FillPattern
pathEl.ClassName = "shape"
2023-01-15 20:36:43 +00:00
pathEl.Style = shape.CSSStyle()
2022-12-22 19:32:41 +00:00
for _, p := range paths {
2023-01-09 18:16:28 +00:00
pathEl.D = p
output += pathEl.Render()
2022-12-22 19:32:41 +00:00
}
box := geo.NewBox(
geo.NewPoint(float64(shape.Pos.X), float64(shape.Pos.Y)),
float64(shape.Width),
float64(shape.Height),
)
rowHeight := box.Height / float64(2+len(shape.Class.Fields)+len(shape.Class.Methods))
headerBox := geo.NewBox(box.TopLeft, box.Width, 2*rowHeight)
js = fmt.Sprintf(`node = rc.rectangle(0, 0, %d, %f, {
2023-01-09 18:16:28 +00:00
fill: "#000",
2022-12-22 19:32:41 +00:00
%s
2023-01-09 18:16:28 +00:00
});`, shape.Width, headerBox.Height, baseRoughProps)
2023-01-13 02:27:53 +00:00
paths, err = computeRoughPathData(r, js)
2022-12-22 19:32:41 +00:00
if err != nil {
return "", err
}
pathEl = d2themes.NewThemableElement("path")
pathEl.SetTranslate(float64(shape.Pos.X), float64(shape.Pos.Y))
2023-01-09 18:16:28 +00:00
pathEl.Fill = shape.Fill
2023-03-16 05:53:12 +00:00
pathEl.FillPattern = shape.FillPattern
pathEl.ClassName = "class_header"
2022-12-22 19:32:41 +00:00
for _, p := range paths {
2023-01-09 18:16:28 +00:00
pathEl.D = p
output += pathEl.Render()
2022-12-22 19:32:41 +00:00
}
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 := d2themes.NewThemableSketchOverlay(sketchOEl, pathEl.Fill).Render()
if err != nil {
return "", err
}
output += renderedSO
2022-12-22 19:32:41 +00:00
if shape.Label != "" {
2022-12-25 00:10:58 +00:00
tl := label.InsideMiddleCenter.GetPointOnBox(
2022-12-22 19:32:41 +00:00
headerBox,
0,
float64(shape.LabelWidth),
float64(shape.LabelHeight),
)
textEl := d2themes.NewThemableElement("text")
2023-01-09 18:16:28 +00:00
textEl.X = tl.X + float64(shape.LabelWidth)/2
textEl.Y = tl.Y + float64(shape.LabelHeight)*3/4
2023-02-25 04:26:40 +00:00
textEl.Fill = shape.GetFontColor()
textEl.ClassName = "text-mono"
2023-01-09 18:16:28 +00:00
textEl.Style = fmt.Sprintf("text-anchor:%s;font-size:%vpx",
"middle",
4+shape.FontSize,
2022-12-22 19:32:41 +00:00
)
2023-01-09 18:16:28 +00:00
textEl.Content = svg.EscapeText(shape.Label)
output += textEl.Render()
2022-12-22 19:32:41 +00:00
}
rowBox := geo.NewBox(box.TopLeft.Copy(), box.Width, rowHeight)
rowBox.TopLeft.Y += headerBox.Height
for _, f := range shape.Fields {
2022-12-24 23:56:22 +00:00
output += classRow(shape, rowBox, f.VisibilityToken(), f.Name, f.Type, float64(shape.FontSize))
2022-12-22 19:32:41 +00:00
rowBox.TopLeft.Y += rowHeight
}
js = fmt.Sprintf(`node = rc.line(%f, %f, %f, %f, {
%s
});`, rowBox.TopLeft.X, rowBox.TopLeft.Y, rowBox.TopLeft.X+rowBox.Width, rowBox.TopLeft.Y, baseRoughProps)
2023-01-13 02:27:53 +00:00
paths, err = computeRoughPathData(r, js)
2022-12-22 19:32:41 +00:00
if err != nil {
return "", err
}
pathEl = d2themes.NewThemableElement("path")
2023-01-09 18:16:28 +00:00
pathEl.Fill = shape.Fill
2023-03-16 05:53:12 +00:00
pathEl.FillPattern = shape.FillPattern
pathEl.ClassName = "class_header"
2022-12-22 19:32:41 +00:00
for _, p := range paths {
2023-01-09 18:16:28 +00:00
pathEl.D = p
output += pathEl.Render()
2022-12-22 19:32:41 +00:00
}
for _, m := range shape.Methods {
2022-12-24 23:56:22 +00:00
output += classRow(shape, rowBox, m.VisibilityToken(), m.Name, m.Return, float64(shape.FontSize))
2022-12-22 19:32:41 +00:00
rowBox.TopLeft.Y += rowHeight
}
return output, nil
}
2022-12-24 23:56:22 +00:00
func classRow(shape d2target.Shape, box *geo.Box, prefix, nameText, typeText string, fontSize float64) string {
2022-12-22 19:32:41 +00:00
output := ""
prefixTL := label.InsideMiddleLeft.GetPointOnBox(
box,
d2target.PrefixPadding,
box.Width,
fontSize,
)
typeTR := label.InsideMiddleRight.GetPointOnBox(
box,
d2target.TypePadding,
0,
fontSize,
)
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
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
output += textEl.Render()
textEl.X = prefixTL.X + d2target.PrefixWidth
textEl.Fill = shape.Fill
textEl.Content = svg.EscapeText(nameText)
output += 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)
output += textEl.Render()
2022-12-22 19:32:41 +00:00
return output
}
2023-01-13 02:27:53 +00:00
func computeRoughPathData(r *Runner, js string) ([]string, error) {
2022-12-22 19:06:57 +00:00
if _, err := r.run(js); err != nil {
return nil, err
}
2023-01-13 02:27:53 +00:00
roughPaths, err := extractRoughPaths(r)
if err != nil {
return nil, err
}
return extractPathData(roughPaths)
}
func computeRoughPaths(r *Runner, js string) ([]roughPath, error) {
2022-12-22 19:06:57 +00:00
if _, err := r.run(js); err != nil {
return nil, err
}
2023-01-13 02:27:53 +00:00
return extractRoughPaths(r)
2022-12-22 19:06:57 +00:00
}
2022-12-21 07:43:45 +00:00
type attrs struct {
D string `json:"d"`
}
2023-01-13 02:27:53 +00:00
type style struct {
Stroke string `json:"stroke,omitempty"`
StrokeWidth string `json:"strokeWidth,omitempty"`
Fill string `json:"fill,omitempty"`
}
type roughPath struct {
2022-12-21 07:43:45 +00:00
Attrs attrs `json:"attrs"`
2023-01-13 02:27:53 +00:00
Style style `json:"style"`
2022-12-21 07:43:45 +00:00
}
2023-01-13 02:27:53 +00:00
func (rp roughPath) StyleCSS() string {
style := ""
if rp.Style.StrokeWidth != "" {
style += fmt.Sprintf("stroke-width:%s;", rp.Style.StrokeWidth)
}
return style
2022-12-21 07:43:45 +00:00
}
2023-01-13 02:27:53 +00:00
func extractRoughPaths(r *Runner) ([]roughPath, error) {
val, err := r.run("JSON.stringify(node.children, null, ' ')")
2022-12-21 07:43:45 +00:00
if err != nil {
return nil, err
}
2023-01-13 02:27:53 +00:00
var roughPaths []roughPath
err = json.Unmarshal([]byte(val.String()), &roughPaths)
2022-12-21 07:43:45 +00:00
if err != nil {
return nil, err
}
2023-01-13 04:25:02 +00:00
// we want to have a fixed precision to the decimals in the path data
for i := range roughPaths {
// truncate all floats in path to only use up to 6 decimal places
roughPaths[i].Attrs.D = floatRE.ReplaceAllStringFunc(roughPaths[i].Attrs.D, func(floatStr string) string {
i := strings.Index(floatStr, ".")
decimalLen := len(floatStr) - i - 1
end := i + go2.Min(decimalLen, 6)
return floatStr[:end+1]
})
2022-12-21 07:43:45 +00:00
}
2023-01-13 02:27:53 +00:00
return roughPaths, nil
}
func extractPathData(roughPaths []roughPath) ([]string, error) {
2022-12-21 07:43:45 +00:00
var paths []string
2023-01-13 02:27:53 +00:00
for _, rp := range roughPaths {
paths = append(paths, rp.Attrs.D)
2022-12-21 07:43:45 +00:00
}
return paths, nil
}
2023-01-13 02:27:53 +00:00
func ArrowheadJS(r *Runner, arrowhead d2target.Arrowhead, stroke string, strokeWidth int) (arrowJS, extraJS string) {
2023-01-13 02:27:53 +00:00
// Note: selected each seed that looks the good for consistent renders
switch arrowhead {
case d2target.ArrowArrowhead:
arrowJS = fmt.Sprintf(
`node = rc.linearPath(%s, { strokeWidth: %d, stroke: "%s", seed: 3 })`,
`[[-10, -4], [0, 0], [-10, 4]]`,
strokeWidth,
stroke,
)
case d2target.TriangleArrowhead:
arrowJS = fmt.Sprintf(
`node = rc.polygon(%s, { strokeWidth: %d, stroke: "%s", fill: "%s", fillStyle: "solid", seed: 2 })`,
`[[-10, -4], [0, 0], [-10, 4]]`,
strokeWidth,
stroke,
stroke,
)
case d2target.DiamondArrowhead:
arrowJS = fmt.Sprintf(
`node = rc.polygon(%s, { strokeWidth: %d, stroke: "%s", fill: "%s", fillStyle: "solid", seed: 1 })`,
2023-01-13 02:27:53 +00:00
`[[-20, 0], [-10, 5], [0, 0], [-10, -5], [-20, 0]]`,
strokeWidth,
stroke,
BG_COLOR,
2023-01-13 02:27:53 +00:00
)
case d2target.FilledDiamondArrowhead:
arrowJS = fmt.Sprintf(
`node = rc.polygon(%s, { strokeWidth: %d, stroke: "%s", fill: "%s", fillStyle: "zigzag", fillWeight: 4, seed: 1 })`,
`[[-20, 0], [-10, 5], [0, 0], [-10, -5], [-20, 0]]`,
strokeWidth,
stroke,
stroke,
)
case d2target.CfManyRequired:
arrowJS = fmt.Sprintf(
// TODO why does fillStyle: "zigzag" error with path
`node = rc.path(%s, { strokeWidth: %d, stroke: "%s", fill: "%s", fillStyle: "solid", fillWeight: 4, seed: 2 })`,
`"M-15,-10 -15,10 M0,10 -15,0 M0,-10 -15,0"`,
2023-01-13 02:27:53 +00:00
strokeWidth,
stroke,
stroke,
)
case d2target.CfMany:
arrowJS = fmt.Sprintf(
`node = rc.path(%s, { strokeWidth: %d, stroke: "%s", fill: "%s", fillStyle: "solid", fillWeight: 4, seed: 8 })`,
`"M0,10 -15,0 M0,-10 -15,0"`,
2023-01-13 02:27:53 +00:00
strokeWidth,
stroke,
stroke,
)
extraJS = fmt.Sprintf(
`node = rc.circle(-20, 0, 8, { strokeWidth: %d, stroke: "%s", fill: "%s", fillStyle: "solid", fillWeight: 1, seed: 4 })`,
2023-01-13 02:27:53 +00:00
strokeWidth,
stroke,
BG_COLOR,
2023-01-13 02:27:53 +00:00
)
case d2target.CfOneRequired:
arrowJS = fmt.Sprintf(
2023-01-14 03:20:05 +00:00
`node = rc.path(%s, { strokeWidth: %d, stroke: "%s", fill: "%s", fillStyle: "solid", fillWeight: 4, seed: 2 })`,
`"M-15,-10 -15,10 M-10,-10 -10,10"`,
2023-01-13 02:27:53 +00:00
strokeWidth,
stroke,
stroke,
)
case d2target.CfOne:
arrowJS = fmt.Sprintf(
2023-01-14 02:29:11 +00:00
`node = rc.path(%s, { strokeWidth: %d, stroke: "%s", fill: "%s", fillStyle: "solid", fillWeight: 4, seed: 3 })`,
`"M-10,-10 -10,10"`,
2023-01-13 02:27:53 +00:00
strokeWidth,
stroke,
stroke,
)
extraJS = fmt.Sprintf(
`node = rc.circle(-20, 0, 8, { strokeWidth: %d, stroke: "%s", fill: "%s", fillStyle: "solid", fillWeight: 1, seed: 5 })`,
2023-01-13 02:27:53 +00:00
strokeWidth,
stroke,
BG_COLOR,
2023-01-13 02:27:53 +00:00
)
}
return
}
func Arrowheads(r *Runner, connection d2target.Connection, srcAdj, dstAdj *geo.Point) (string, error) {
2023-01-13 02:27:53 +00:00
arrowPaths := []string{}
if connection.SrcArrow != d2target.NoArrowhead {
arrowJS, extraJS := ArrowheadJS(r, connection.SrcArrow, connection.Stroke, connection.StrokeWidth)
2023-01-13 02:27:53 +00:00
if arrowJS == "" {
return "", nil
}
startingSegment := geo.NewSegment(connection.Route[0], connection.Route[1])
startingVector := startingSegment.ToVector().Reverse()
angle := startingVector.Degrees()
transform := fmt.Sprintf(`transform="translate(%f %f) rotate(%v)"`,
startingSegment.Start.X+srcAdj.X, startingSegment.Start.Y+srcAdj.Y, angle,
2023-01-13 02:27:53 +00:00
)
roughPaths, err := computeRoughPaths(r, arrowJS)
if err != nil {
return "", err
}
if extraJS != "" {
extraPaths, err := computeRoughPaths(r, extraJS)
if err != nil {
return "", err
}
roughPaths = append(roughPaths, extraPaths...)
}
pathEl := d2themes.NewThemableElement("path")
pathEl.ClassName = "connection"
2023-01-16 11:15:59 +00:00
pathEl.Attributes = transform
2023-01-13 02:27:53 +00:00
for _, rp := range roughPaths {
2023-01-16 11:15:59 +00:00
pathEl.D = rp.Attrs.D
pathEl.Fill = rp.Style.Fill
pathEl.Stroke = rp.Style.Stroke
pathEl.Style = rp.StyleCSS()
arrowPaths = append(arrowPaths, pathEl.Render())
2023-01-13 02:27:53 +00:00
}
}
if connection.DstArrow != d2target.NoArrowhead {
arrowJS, extraJS := ArrowheadJS(r, connection.DstArrow, connection.Stroke, connection.StrokeWidth)
2023-01-13 02:27:53 +00:00
if arrowJS == "" {
return "", nil
}
length := len(connection.Route)
endingSegment := geo.NewSegment(connection.Route[length-2], connection.Route[length-1])
endingVector := endingSegment.ToVector()
angle := endingVector.Degrees()
transform := fmt.Sprintf(`transform="translate(%f %f) rotate(%v)"`,
endingSegment.End.X+dstAdj.X, endingSegment.End.Y+dstAdj.Y, angle,
2023-01-13 02:27:53 +00:00
)
roughPaths, err := computeRoughPaths(r, arrowJS)
if err != nil {
return "", err
}
if extraJS != "" {
extraPaths, err := computeRoughPaths(r, extraJS)
if err != nil {
return "", err
}
roughPaths = append(roughPaths, extraPaths...)
}
pathEl := d2themes.NewThemableElement("path")
pathEl.ClassName = "connection"
2023-01-16 11:15:59 +00:00
pathEl.Attributes = transform
2023-01-13 02:27:53 +00:00
for _, rp := range roughPaths {
2023-01-16 11:15:59 +00:00
pathEl.D = rp.Attrs.D
pathEl.Fill = rp.Style.Fill
pathEl.Stroke = rp.Style.Stroke
pathEl.Style = rp.StyleCSS()
arrowPaths = append(arrowPaths, pathEl.Render())
2023-01-13 02:27:53 +00:00
}
}
return strings.Join(arrowPaths, " "), nil
}