From 504eb2fa69be9fe872190dade6e2bc0d6a97081c Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Thu, 19 Oct 2023 16:54:10 -0700 Subject: [PATCH] also remove dependency from png and xgif libs --- d2cli/main.go | 28 +++++++++++++++++++++++----- lib/png/png.go | 10 +--------- lib/xgif/xgif.go | 12 +----------- lib/xgif/xgif_test.go | 2 +- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/d2cli/main.go b/d2cli/main.go index 7963f9693..68ea60b42 100644 --- a/d2cli/main.go +++ b/d2cli/main.go @@ -434,7 +434,7 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, la if err != nil { return nil, false, err } - out, err := xgif.AnimatePNGs(ms, pngs, int(animateInterval)) + out, err := AnimatePNGs(ms, pngs, int(animateInterval)) if err != nil { return nil, false, err } @@ -769,7 +769,7 @@ func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opts bundleErr = multierr.Combine(bundleErr, bundleErr2) } - out, err = png.ConvertSVG(ms, page, svg) + out, err = ConvertSVG(ms, page, svg) if err != nil { return svg, err } @@ -843,7 +843,7 @@ func renderPDF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opt } svg = appendix.Append(diagram, ruler, svg) - pngImg, err := png.ConvertSVG(ms, page, svg) + pngImg, err := ConvertSVG(ms, page, svg) if err != nil { return svg, err } @@ -945,7 +945,7 @@ func renderPPTX(ctx context.Context, ms *xmain.State, presentation *pptx.Present svg = appendix.Append(diagram, ruler, svg) - pngImg, err := png.ConvertSVG(ms, page, svg) + pngImg, err := ConvertSVG(ms, page, svg) if err != nil { return nil, err } @@ -1191,7 +1191,7 @@ func renderPNGsForGIF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plug svg = appendix.Append(diagram, ruler, svg) - pngImg, err := png.ConvertSVG(ms, page, svg) + pngImg, err := ConvertSVG(ms, page, svg) if err != nil { return nil, nil, err } @@ -1222,3 +1222,21 @@ func renderPNGsForGIF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plug return svg, pngs, nil } + +func ConvertSVG(ms *xmain.State, page playwright.Page, svg []byte) ([]byte, error) { + cancel := background.Repeat(func() { + ms.Log.Info.Printf("converting to PNG...") + }, time.Second*5) + defer cancel() + + return png.ConvertSVG(page, svg) +} + +func AnimatePNGs(ms *xmain.State, pngs [][]byte, animIntervalMs int) ([]byte, error) { + cancel := background.Repeat(func() { + ms.Log.Info.Printf("generating GIF...") + }, time.Second*5) + defer cancel() + + return xgif.AnimatePNGs(pngs, animIntervalMs) +} diff --git a/lib/png/png.go b/lib/png/png.go index c0cc23687..ac21e0e7b 100644 --- a/lib/png/png.go +++ b/lib/png/png.go @@ -5,7 +5,6 @@ import ( "encoding/base64" "fmt" "strings" - "time" _ "embed" @@ -14,9 +13,7 @@ import ( pngstruct "github.com/dsoprea/go-png-image-structure/v2" "github.com/playwright-community/playwright-go" - "oss.terrastruct.com/d2/lib/background" "oss.terrastruct.com/d2/lib/version" - "oss.terrastruct.com/util-go/xmain" ) // ConvertSVG scales the image by 2x @@ -88,12 +85,7 @@ const pngPrefix = "data:image/png;base64," // ConvertSVG converts the given SVG into a PNG. // Note that the resulting PNG has 2x the size (width and height) of the original SVG (see generate_png.js) -func ConvertSVG(ms *xmain.State, page playwright.Page, svg []byte) ([]byte, error) { - cancel := background.Repeat(func() { - ms.Log.Info.Printf("converting to PNG...") - }, time.Second*5) - defer cancel() - +func ConvertSVG(page playwright.Page, svg []byte) ([]byte, error) { encodedSVG := base64.StdEncoding.EncodeToString(svg) pngInterface, err := page.Evaluate(genPNGScript, map[string]interface{}{ "imgString": "data:image/svg+xml;charset=utf-8;base64," + encodedSVG, diff --git a/lib/xgif/xgif.go b/lib/xgif/xgif.go index e70f856c7..d39b9ca15 100644 --- a/lib/xgif/xgif.go +++ b/lib/xgif/xgif.go @@ -15,27 +15,17 @@ import ( "image/color" "image/gif" "image/png" - "time" "github.com/ericpauley/go-quantize/quantize" - "oss.terrastruct.com/d2/lib/background" "oss.terrastruct.com/util-go/go2" - "oss.terrastruct.com/util-go/xmain" ) const INFINITE_LOOP = 0 var BG_COLOR = color.White -func AnimatePNGs(ms *xmain.State, pngs [][]byte, animIntervalMs int) ([]byte, error) { - if ms != nil { - cancel := background.Repeat(func() { - ms.Log.Info.Printf("generating GIF...") - }, time.Second*5) - defer cancel() - } - +func AnimatePNGs(pngs [][]byte, animIntervalMs int) ([]byte, error) { var width, height int pngImgs := make([]image.Image, len(pngs)) for i, pngBytes := range pngs { diff --git a/lib/xgif/xgif_test.go b/lib/xgif/xgif_test.go index bc3cc1061..fcdc370e7 100644 --- a/lib/xgif/xgif_test.go +++ b/lib/xgif/xgif_test.go @@ -20,7 +20,7 @@ var test_output []byte func TestPngToGif(t *testing.T) { boards := [][]byte{test_input1, test_input2} interval := 1_000 - gifBytes, err := AnimatePNGs(nil, boards, interval) + gifBytes, err := AnimatePNGs(boards, interval) assert.NoError(t, err) // use this to update the test output