From 4ee90f77eda2c95a18e8d502d04cd6d23c797e09 Mon Sep 17 00:00:00 2001 From: kpym Date: Tue, 27 Oct 2020 19:43:39 +0100 Subject: [PATCH] optimisation des SVG --- go.mod | 1 + marianne.go | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 680df0f..e7a6d46 100644 --- a/go.mod +++ b/go.mod @@ -7,5 +7,6 @@ require ( github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/spf13/pflag v1.0.6-0.20201009195203-85dd5c8bc61c github.com/tdewolff/canvas v0.0.0-20201021153214-d9228b138ea8 + github.com/tdewolff/minify/v2 v2.9.5 golang.org/x/text v0.3.3 // indirect ) diff --git a/marianne.go b/marianne.go index 6442aed..9b567ff 100644 --- a/marianne.go +++ b/marianne.go @@ -24,6 +24,8 @@ import ( "github.com/tdewolff/canvas/pdf" "github.com/tdewolff/canvas/rasterizer" "github.com/tdewolff/canvas/svg" + "github.com/tdewolff/minify/v2" + minsvg "github.com/tdewolff/minify/v2/svg" ) // quelques variables globales @@ -354,8 +356,17 @@ func writeImages(c *canvas.Canvas, zp, formats string) { buf = reWidth.ReplaceAll(buf, []byte{}) reHeight := regexp.MustCompile(`(?m)(height\s*=\s*\"[^"]*\"\s*)`) buf = reHeight.ReplaceAll(buf, []byte{}) + // suppression de la partie fractale + reNumbers := regexp.MustCompile(`(?m)([ MZLHVCSQTA+-]\d*)\.\d+`) + buf = reNumbers.ReplaceAll(buf, []byte("$1")) + // compression du SVG (réécriture en coordonnées relatives) + memoryFile.Reset() + mediatype := "image/svg+xml" + m := minify.New() + m.AddFunc(mediatype, minsvg.Minify) + check(m.Minify(mediatype, memoryFile, bytes.NewReader(buf))) // et on enregistre finalement - err = ioutil.WriteFile(name, buf, 0644) + err = ioutil.WriteFile(name, memoryFile.Bytes(), 0644) check(err) log("SVG fait.\n") }