From 033a005eea9e869fa6a6f48b501a32e59c06a018 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Thu, 12 Jan 2023 18:24:58 -0800 Subject: [PATCH 01/14] add arrowheads sketch test --- d2renderers/d2sketch/sketch_test.go | 45 ++++++++++ .../testdata/arrowheads/sketch.exp.svg | 87 +++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 d2renderers/d2sketch/testdata/arrowheads/sketch.exp.svg diff --git a/d2renderers/d2sketch/sketch_test.go b/d2renderers/d2sketch/sketch_test.go index f35eb1be7..ede3851f9 100644 --- a/d2renderers/d2sketch/sketch_test.go +++ b/d2renderers/d2sketch/sketch_test.go @@ -280,6 +280,51 @@ shipments.order_id <-> orders.id`, +getJobs(): "Job[]" +setTimeout(seconds int) } +`, + }, + { + name: "arrowheads", + script: ` +a.0 <-> b.0: <-> +a.1 -- b.1: -- +a.2 <-> b.2: arrow { + source-arrowhead.shape: arrow + target-arrowhead.shape: arrow +} +a.3 <-> b.3: triangle { + source-arrowhead.shape: triangle + target-arrowhead.shape: triangle +} +a.4 <-> b.4: diamond { + source-arrowhead.shape: diamond + target-arrowhead.shape: diamond +} +a.5 <-> b.5: diamond filled { + source-arrowhead: { + shape: diamond + style.filled: true + } + target-arrowhead: { + shape: diamond + style.filled: true + } +} +a.6 <-> b.6: cf-many { + source-arrowhead.shape: cf-many + target-arrowhead.shape: cf-many +} +a.7 <-> b.7: cf-many-required { + source-arrowhead.shape: cf-many-required + target-arrowhead.shape: cf-many-required +} +a.8 <-> b.8: cf-one { + source-arrowhead.shape: cf-one + target-arrowhead.shape: cf-one +} +a.9 <-> b.9: cf-one-required { + source-arrowhead.shape: cf-one-required + target-arrowhead.shape: cf-one-required +} `, }, } diff --git a/d2renderers/d2sketch/testdata/arrowheads/sketch.exp.svg b/d2renderers/d2sketch/testdata/arrowheads/sketch.exp.svg new file mode 100644 index 000000000..c743e309d --- /dev/null +++ b/d2renderers/d2sketch/testdata/arrowheads/sketch.exp.svg @@ -0,0 +1,87 @@ + + + + + + +ab00112233445566778899 <->-- arrowtriangle diamond diamond filled cf-many cf-many-required cf-one cf-one-required + + + + + + + + + + + + \ No newline at end of file From af2a032a9e981ab5455c9954308fcd25912973f1 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Thu, 12 Jan 2023 12:01:49 -0800 Subject: [PATCH 02/14] render sketch arrowheads separately --- d2renderers/d2svg/d2svg.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/d2renderers/d2svg/d2svg.go b/d2renderers/d2svg/d2svg.go index b57b483e8..5ed641c44 100644 --- a/d2renderers/d2svg/d2svg.go +++ b/d2renderers/d2svg/d2svg.go @@ -10,6 +10,7 @@ import ( "hash/fnv" "html" "io" + "regexp" "sort" "strings" @@ -60,6 +61,8 @@ var sketchStyleCSS string //go:embed github-markdown.css var mdCSS string +var strokeWidthRE = regexp.MustCompile(`(.*stroke-width):(.+?);(.*)`) + type RenderOpts struct { Pad int Sketch bool @@ -449,24 +452,32 @@ func drawConnection(writer io.Writer, labelMaskID string, connection d2target.Co } path := pathData(connection, idToShape) - attrs := fmt.Sprintf(`%s%smask="url(#%s)"`, - markerStart, - markerEnd, - labelMaskID, - ) + mask := fmt.Sprintf(`mask="url(#%s)"`, labelMaskID) if sketchRunner != nil { - out, err := d2sketch.Connection(sketchRunner, connection, path, attrs) + out, err := d2sketch.Connection(sketchRunner, connection, path, mask) if err != nil { return "", err } - fmt.Fprintf(writer, out) + fmt.Fprint(writer, out) + + // render sketch arrowheads separately + // TODO add sketch specific arrowheads + if markerStart != "" || markerEnd != "" { + // set stroke width to 0 + style := strokeWidthRE.ReplaceAllString(connection.CSSStyle(), `$1:0;$3`) + fmt.Fprintf(writer, ``, + path, style, + markerStart, + markerEnd, + ) + } } else { animatedClass := "" if connection.Animated { animatedClass = " animated-connection" } - fmt.Fprintf(writer, ``, - path, animatedClass, connection.CSSStyle(), attrs) + fmt.Fprintf(writer, ``, + path, animatedClass, connection.CSSStyle(), markerStart, markerEnd, mask) } if connection.Label != "" { From 62d153f79887fdb2d3b43c926594ca24d4e758eb Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Thu, 12 Jan 2023 12:05:05 -0800 Subject: [PATCH 03/14] update tests --- d2renderers/d2sketch/testdata/all_shapes/sketch.exp.svg | 2 +- d2renderers/d2sketch/testdata/animated/sketch.exp.svg | 2 +- d2renderers/d2sketch/testdata/arrowheads/sketch.exp.svg | 2 +- d2renderers/d2sketch/testdata/basic/sketch.exp.svg | 2 +- d2renderers/d2sketch/testdata/child_to_child/sketch.exp.svg | 2 +- d2renderers/d2sketch/testdata/connection_label/sketch.exp.svg | 2 +- d2renderers/d2sketch/testdata/sql_tables/sketch.exp.svg | 2 +- d2renderers/d2sketch/testdata/twitter/sketch.exp.svg | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/d2renderers/d2sketch/testdata/all_shapes/sketch.exp.svg b/d2renderers/d2sketch/testdata/all_shapes/sketch.exp.svg index efb2c8aba..fd826c475 100644 --- a/d2renderers/d2sketch/testdata/all_shapes/sketch.exp.svg +++ b/d2renderers/d2sketch/testdata/all_shapes/sketch.exp.svg @@ -51,7 +51,7 @@ width="1597" height="835" viewBox="-102 -102 1597 835">