Skip to content

Commit

Permalink
all: migrate to go-pdf/fpdf
Browse files Browse the repository at this point in the history
Migrating to go-fpdf/[email protected] improves somewhat plotting performances:

```
  name           old time/op    new time/op    delta
  Canvas-8          221µs ± 2%     181µs ± 4%  -18.25%  (p=0.000 n=30+29)
  CanvasImage-8    72.0ms ± 4%    72.3ms ± 4%     ~     (p=0.406 n=27+29)

  name           old alloc/op   new alloc/op   delta
  Canvas-8          111kB ± 1%     109kB ± 1%   -1.79%  (p=0.000 n=27+23)
  CanvasImage-8    2.94MB ± 0%    2.69MB ± 0%   -8.64%  (p=0.000 n=29+29)

  name           old allocs/op  new allocs/op  delta
  Canvas-8          1.48k ± 0%     0.87k ± 0%  -41.09%  (p=0.000 n=30+30)
  CanvasImage-8       121 ± 0%        84 ± 0%  -30.58%  (p=0.000 n=26+27)
```

Fixes gonum#697.
  • Loading branch information
sbinet committed Jun 24, 2021
1 parent e208a50 commit bd0e370
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cmpimg/cmpimg.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"reflect"
"strings"

"github.com/phpdave11/gofpdf"
"github.com/go-pdf/fpdf"

_ "image/jpeg"
_ "image/png"
Expand Down Expand Up @@ -90,7 +90,7 @@ func EqualApprox(typ string, raw1, raw2 []byte, delta float64) (bool, error) {
}

func cmpPdf(pdf1, pdf2 []byte) bool {
return gofpdf.CompareBytes(pdf1, pdf1, false) == nil
return fpdf.CompareBytes(pdf1, pdf1, false) == nil
}

func cmpImg(v1, v2 image.Image, delta float64) bool {
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ require (
github.com/fogleman/gg v1.3.0
github.com/go-fonts/liberation v0.1.1
github.com/go-latex/latex v0.0.0-20210118124228-b3d85cf34e07
github.com/phpdave11/gofpdf v1.4.2
github.com/go-pdf/fpdf v0.4.0
golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3
golang.org/x/image v0.0.0-20210216034530-4410531fe030
golang.org/x/text v0.3.5 // indirect
golang.org/x/image v0.0.0-20210607152325-775e3b0c77b9
gonum.org/v1/gonum v0.8.2
)
14 changes: 9 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af h1:wVe6/Ea46ZMeNkQjjBW6xcqyQA/j5e0D6GytH95g0gQ=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
Expand All @@ -18,17 +19,20 @@ github.com/go-fonts/stix v0.1.0/go.mod h1:w/c1f0ldAUlJmLBvlbkvVXLAD+tAMqobIIQpmn
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-latex/latex v0.0.0-20210118124228-b3d85cf34e07 h1:OTlfMvwR1rLyf9goVmXfuS5AJn80+Vmj4rTf4n46SOs=
github.com/go-latex/latex v0.0.0-20210118124228-b3d85cf34e07/go.mod h1:CO1AlKB2CSIqUrmQPqA0gdRIlnLEY0gK5JGjh37zN5U=
github.com/go-pdf/fpdf v0.4.0 h1:1+bMcVeIfseu+fRoanK10qw+WlrL69W6ZLpgYXCWN0U=
github.com/go-pdf/fpdf v0.4.0/go.mod h1:Zv67d0wluNY8KnOYFHNR21BRjHJrMFE78sa1GPegzXQ=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/phpdave11/gofpdf v1.4.2 h1:KPKiIbfwbvC/wOncwhrpRdXVj2CZTCFlw4wnoyjtHfQ=
github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY=
github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
github.com/phpdave11/gofpdi v1.0.13/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand All @@ -46,8 +50,8 @@ golang.org/x/image v0.0.0-20200119044424-58c23975cae1/go.mod h1:FeLwcggjj3mMvU+o
golang.org/x/image v0.0.0-20200430140353-33d19683fad8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20201208152932-35266b937fa6/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20210216034530-4410531fe030 h1:lP9pYkih3DUSC641giIXa2XqfTIbbbRr0w2EOTA7wHA=
golang.org/x/image v0.0.0-20210216034530-4410531fe030/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20210607152325-775e3b0c77b9 h1:D0iM1dTCbD5Dg1CbuvLC/v/agLc79efSj/L35Q3Vqhs=
golang.org/x/image v0.0.0-20210607152325-775e3b0c77b9/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand All @@ -59,8 +63,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210304124612-50617c2ba197 h1:7+SpRyhoo46QjKkYInQXpcfxx3TYFEYkn131lwGE9/0=
golang.org/x/sys v0.0.0-20210304124612-50617c2ba197/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190206041539-40960b6deb8e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
2 changes: 1 addition & 1 deletion vg/vgpdf/vgpdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"path/filepath"
"sync"

pdf "github.com/phpdave11/gofpdf"
pdf "github.com/go-pdf/fpdf"
stdfnt "golang.org/x/image/font"

"gonum.org/v1/plot/font"
Expand Down
50 changes: 50 additions & 0 deletions vg/vgpdf/vgpdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"fmt"
"image/color"
"image/png"
"io/ioutil"
"log"
"os"
Expand All @@ -16,6 +17,7 @@ import (
"gonum.org/v1/plot"
"gonum.org/v1/plot/cmpimg"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
"gonum.org/v1/plot/vg/vgpdf"
)
Expand Down Expand Up @@ -207,3 +209,51 @@ func TestIssue540(t *testing.T) {
t.Fatalf("images differ")
}
}

func BenchmarkCanvas(b *testing.B) {
p := plot.New()

xys := plotter.XYs{
plotter.XY{X: 0, Y: 0},
plotter.XY{X: 1, Y: 1},
plotter.XY{X: 2, Y: 2},
}

p.Title.Text = "My title"
p.X.Tick.Label.Font.Size = 0 // hide X-axis labels
p.Y.Tick.Label.Font.Size = 0 // hide Y-axis labels

lines, points, err := plotter.NewLinePoints(xys)
if err != nil {
log.Fatal(err)
}
lines.Color = color.RGBA{B: 255, A: 255}

p.Add(lines, points)
p.Add(plotter.NewGrid())

c := vgpdf.New(5*vg.Centimeter, 5*vg.Centimeter)
d := draw.New(c)

b.ResetTimer()
for i := 0; i < b.N; i++ {
p.Draw(d)
}
}

func BenchmarkCanvasImage(b *testing.B) {
c := vgpdf.New(5*vg.Centimeter, 5*vg.Centimeter)
raw, err := ioutil.ReadFile("../../plotter/testdata/gopher.png")
if err != nil {
b.Fatalf("could not read test image: %+v", err)
}
img, err := png.Decode(bytes.NewReader(raw))
if err != nil {
b.Fatalf("could not decode test image: %+v", err)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
c.DrawImage(vg.Rectangle{Max: vg.Point{X: 2, Y: 2}}, img)
}
}

0 comments on commit bd0e370

Please sign in to comment.