also remove dependency from png and xgif libs
This commit is contained in:
parent
4dda484b50
commit
504eb2fa69
4 changed files with 26 additions and 26 deletions
|
|
@ -434,7 +434,7 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, la
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
out, err := xgif.AnimatePNGs(ms, pngs, int(animateInterval))
|
out, err := AnimatePNGs(ms, pngs, int(animateInterval))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
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)
|
bundleErr = multierr.Combine(bundleErr, bundleErr2)
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err = png.ConvertSVG(ms, page, svg)
|
out, err = ConvertSVG(ms, page, svg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return svg, err
|
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)
|
svg = appendix.Append(diagram, ruler, svg)
|
||||||
|
|
||||||
pngImg, err := png.ConvertSVG(ms, page, svg)
|
pngImg, err := ConvertSVG(ms, page, svg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return svg, err
|
return svg, err
|
||||||
}
|
}
|
||||||
|
|
@ -945,7 +945,7 @@ func renderPPTX(ctx context.Context, ms *xmain.State, presentation *pptx.Present
|
||||||
|
|
||||||
svg = appendix.Append(diagram, ruler, svg)
|
svg = appendix.Append(diagram, ruler, svg)
|
||||||
|
|
||||||
pngImg, err := png.ConvertSVG(ms, page, svg)
|
pngImg, err := ConvertSVG(ms, page, svg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -1191,7 +1191,7 @@ func renderPNGsForGIF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plug
|
||||||
|
|
||||||
svg = appendix.Append(diagram, ruler, svg)
|
svg = appendix.Append(diagram, ruler, svg)
|
||||||
|
|
||||||
pngImg, err := png.ConvertSVG(ms, page, svg)
|
pngImg, err := ConvertSVG(ms, page, svg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -1222,3 +1222,21 @@ func renderPNGsForGIF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plug
|
||||||
|
|
||||||
return svg, pngs, nil
|
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
||||||
|
|
@ -14,9 +13,7 @@ import (
|
||||||
pngstruct "github.com/dsoprea/go-png-image-structure/v2"
|
pngstruct "github.com/dsoprea/go-png-image-structure/v2"
|
||||||
"github.com/playwright-community/playwright-go"
|
"github.com/playwright-community/playwright-go"
|
||||||
|
|
||||||
"oss.terrastruct.com/d2/lib/background"
|
|
||||||
"oss.terrastruct.com/d2/lib/version"
|
"oss.terrastruct.com/d2/lib/version"
|
||||||
"oss.terrastruct.com/util-go/xmain"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConvertSVG scales the image by 2x
|
// ConvertSVG scales the image by 2x
|
||||||
|
|
@ -88,12 +85,7 @@ const pngPrefix = "data:image/png;base64,"
|
||||||
|
|
||||||
// ConvertSVG converts the given SVG into a PNG.
|
// 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)
|
// 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) {
|
func ConvertSVG(page playwright.Page, svg []byte) ([]byte, error) {
|
||||||
cancel := background.Repeat(func() {
|
|
||||||
ms.Log.Info.Printf("converting to PNG...")
|
|
||||||
}, time.Second*5)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
encodedSVG := base64.StdEncoding.EncodeToString(svg)
|
encodedSVG := base64.StdEncoding.EncodeToString(svg)
|
||||||
pngInterface, err := page.Evaluate(genPNGScript, map[string]interface{}{
|
pngInterface, err := page.Evaluate(genPNGScript, map[string]interface{}{
|
||||||
"imgString": "data:image/svg+xml;charset=utf-8;base64," + encodedSVG,
|
"imgString": "data:image/svg+xml;charset=utf-8;base64," + encodedSVG,
|
||||||
|
|
|
||||||
|
|
@ -15,27 +15,17 @@ import (
|
||||||
"image/color"
|
"image/color"
|
||||||
"image/gif"
|
"image/gif"
|
||||||
"image/png"
|
"image/png"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/ericpauley/go-quantize/quantize"
|
"github.com/ericpauley/go-quantize/quantize"
|
||||||
|
|
||||||
"oss.terrastruct.com/d2/lib/background"
|
|
||||||
"oss.terrastruct.com/util-go/go2"
|
"oss.terrastruct.com/util-go/go2"
|
||||||
"oss.terrastruct.com/util-go/xmain"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const INFINITE_LOOP = 0
|
const INFINITE_LOOP = 0
|
||||||
|
|
||||||
var BG_COLOR = color.White
|
var BG_COLOR = color.White
|
||||||
|
|
||||||
func AnimatePNGs(ms *xmain.State, pngs [][]byte, animIntervalMs int) ([]byte, error) {
|
func AnimatePNGs(pngs [][]byte, animIntervalMs int) ([]byte, error) {
|
||||||
if ms != nil {
|
|
||||||
cancel := background.Repeat(func() {
|
|
||||||
ms.Log.Info.Printf("generating GIF...")
|
|
||||||
}, time.Second*5)
|
|
||||||
defer cancel()
|
|
||||||
}
|
|
||||||
|
|
||||||
var width, height int
|
var width, height int
|
||||||
pngImgs := make([]image.Image, len(pngs))
|
pngImgs := make([]image.Image, len(pngs))
|
||||||
for i, pngBytes := range pngs {
|
for i, pngBytes := range pngs {
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ var test_output []byte
|
||||||
func TestPngToGif(t *testing.T) {
|
func TestPngToGif(t *testing.T) {
|
||||||
boards := [][]byte{test_input1, test_input2}
|
boards := [][]byte{test_input1, test_input2}
|
||||||
interval := 1_000
|
interval := 1_000
|
||||||
gifBytes, err := AnimatePNGs(nil, boards, interval)
|
gifBytes, err := AnimatePNGs(boards, interval)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// use this to update the test output
|
// use this to update the test output
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue