also remove dependency from png and xgif libs

This commit is contained in:
Gavin Nishizawa 2023-10-19 16:54:10 -07:00
parent 4dda484b50
commit 504eb2fa69
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
4 changed files with 26 additions and 26 deletions

View file

@ -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)
}

View file

@ -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,

View file

@ -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 {

View file

@ -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