diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 1bf1c2093..fcaa580c4 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -1,5 +1,8 @@ #### Features ๐Ÿš€ +- Diagram padding can now can be configured in the CLI (default 100px). + [https://github.com/terrastruct/d2/pull/431](https://github.com/terrastruct/d2/pull/431) + #### Improvements ๐Ÿงน #### Bugfixes โ›‘๏ธ diff --git a/ci/release/template/man/d2.1 b/ci/release/template/man/d2.1 index 275eca9da..cdfe48392 100644 --- a/ci/release/template/man/d2.1 +++ b/ci/release/template/man/d2.1 @@ -58,6 +58,9 @@ Port listening address when used with Set the diagram theme to the passed integer. For a list of available options, see .Lk https://oss.terrastruct.com/d2 .Ns . +.It Fl -pad Ar 100 +Pixels padded around the rendered diagram +.Ns . .It Fl l , -layout Ar dagre Set the diagram layout engine to the passed string. For a list of available options, run .Ar layout diff --git a/d2renderers/d2svg/d2svg.go b/d2renderers/d2svg/d2svg.go index 11f083c46..022fcf66d 100644 --- a/d2renderers/d2svg/d2svg.go +++ b/d2renderers/d2svg/d2svg.go @@ -34,7 +34,7 @@ import ( ) const ( - padding = 100 + DEFAULT_PADDING = 100 MIN_ARROWHEAD_STROKE_WIDTH = 2 threeDeeOffset = 15 ) @@ -47,10 +47,10 @@ var styleCSS string //go:embed github-markdown.css var mdCSS string -func setViewbox(writer io.Writer, diagram *d2target.Diagram) (width int, height int) { +func setViewbox(writer io.Writer, diagram *d2target.Diagram, pad int) (width int, height int) { tl, br := diagram.BoundingBox() - w := br.X - tl.X + padding*2 - h := br.Y - tl.Y + padding*2 + w := br.X - tl.X + pad*2 + h := br.Y - tl.Y + pad*2 // TODO minify // TODO background stuff. e.g. dotted, grid, colors @@ -58,7 +58,7 @@ func setViewbox(writer io.Writer, diagram *d2target.Diagram) (width int, height `, w, h, tl.X-padding, tl.Y-padding, w, h) +width="%d" height="%d" viewBox="%d %d %d %d">`, w, h, tl.X-pad, tl.Y-pad, w, h) return w, h } @@ -949,9 +949,9 @@ func embedFonts(buf *bytes.Buffer) { } // TODO minify output at end -func Render(diagram *d2target.Diagram) ([]byte, error) { +func Render(diagram *d2target.Diagram, pad int) ([]byte, error) { buf := &bytes.Buffer{} - w, h := setViewbox(buf, diagram) + w, h := setViewbox(buf, diagram, pad) buf.WriteString(fmt.Sprintf(`