gonum / plot

A repository for plotting and visualizing data
BSD 3-Clause "New" or "Revised" License
2.72k stars 202 forks source link

Upgraded the plot module dependencies and now get 500KB svg instead of 40KB svg files (same content) #775

Closed chmike closed 5 months ago

chmike commented 5 months ago

What are you trying to do?

I'm upgrading a program that created svg images. I had small svg files(40KB) and I now get huge svg files (500KB).

What did you do?

I upgraded all the dependencies of the plot module and had to change from svg.Writerto renderers.SVG().

c := canvas.New(8*inchToMillimeter, 3*inchToMillimeter)
gonumCanvas := renderers.NewGonumPlot(c)
p.Draw(gonumCanvas)
if err := c.WriteFile(fileName, renderers.SVG()); err != nil {
    return errors.Wrap(err, "save SVG")
}

What did you expect to happen?

I expected to get small svg files as before.

What actually happened?

I get huge files.

I suspect that the fonts has been embedded in the file. I don't need this. Apparently renderes.SVG() accepts options as arguments. Unfortunately, the type is interface{}. I thus don't know what kind of options I can give as argument. I don't know where to find them. I couldn't find any code example for inspiration. I don't have mch time to explore.

Change the code into this without success. Files are still 500KB which is 10 times bigger than the prior version.

opts := &svg.Options{Compression: 0, EmbedFonts: false, SubsetFonts: false}
if err := c.WriteFile(filepath.Join(path, "comptes"+fileExt), renderers.SVG(opts)); err != nil {
    return errors.Wrap(err, "save SVG")
}

What version of Go and Gonum/plot are you using?

go version go1.22.1 darwin/arm64

From the go.mod file: gonum.org/v1/plot v0.14.0

Does this issue reproduce with the current master?

Didn't try.

kortschak commented 5 months ago

Please provide a complete minimal reproducer of the problem. Note that renderers.NewGonumPlot is not a function in plot, and may be doing work that includes embedding the fonts (see EmbedFonts). Without the code, we cannot know.

chmike commented 5 months ago

Thank you very much for answering.

Looking inside the svg, I can see that the fonts are embedded.

I tried the example code given in the gnum plot package. I don't see where I can specify that I don't want the embedded fonts.

if err := p.Save(8*vg.Inch, 3*vg.Inch, filepath.Join(path, "comptes"+fileExt)); err != nil {
    return errors.Wrap(err, "save SVG")
}

I tried the earlier method, where I can put an svg.Option structure argument. But the option to not embed fonts is apparently ignored.

I need to send many generated images in a mail. It wasn't a problem with 40KB images. It is impossible with 500KB images. I never had a problem with the font. So I don't need to embed the font.

chmike commented 5 months ago

Problem fixed.

I save plot files in two places. I changed only one place without seeing any effect on the file saved by the other place. My error.

I then removed any call and dependency to the tdewolff modules. I don't remember where I got that from and why.

I switched back to the example code provided in the gonum plot example given in the previous comment. I made sure to fix the code in the two places.

svg files are now 17KB.

Thank you very much for being so responsive.

chmike commented 5 months ago

I know the reason I switched to the tdewolff svg writer. It is because I wanted that the text in the SVG image could be selected and copied. It doesn't work anyway with some OS (macOS) but I left it as is. I found another way around this limitation.

While investigating the bug, I noticed that the tdewolff package had many indirect dependencies which in my opinion make the module brittle. I'm glad I got back to the gonum plot way to generate svg files. The files are remarkably small.