Skip to content

Commit

Permalink
optimisation des SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
kpym committed Oct 27, 2020
1 parent 72e7bd2 commit 4ee90f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
13 changes: 12 additions & 1 deletion marianne.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit 4ee90f7

Please sign in to comment.