2020-11-23 22:15:56 +00:00
|
|
|
package testutils ;import (_f "crypto/md5";_ca "encoding/hex";_gbf "errors";_ag "fmt";_ce "github.com/unidoc/unipdf/v3/common";_dg "github.com/unidoc/unipdf/v3/core";_gb "image";_c "image/png";_ga "io";_ge "os";_b "os/exec";_a "path/filepath";_g "strings";_ff "testing";);func CompareImages (img1 ,img2 _gb .Image )(bool ,error ){_geb :=img1 .Bounds ();_bag :=0;for _bc :=0;_bc < _geb .Size ().X ;_bc ++{for _aff :=0;_aff < _geb .Size ().Y ;_aff ++{_ea ,_cd ,_ae ,_ :=img1 .At (_bc ,_aff ).RGBA ();_fg ,_afb ,_eb ,_ :=img2 .At (_bc ,_aff ).RGBA ();if _ea !=_fg ||_cd !=_afb ||_ae !=_eb {_bag ++;};};};_dc :=float64 (_bag )/float64 (_geb .Dx ()*_geb .Dy ());if _dc > 0.0001{_ag .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_dc ,_bag );return false ,nil ;};return true ,nil ;};func CopyFile (src ,dst string )error {_cee ,_af :=_ge .Open (src );if _af !=nil {return _af ;};defer _cee .Close ();_gd ,_af :=_ge .Create (dst );if _af !=nil {return _af ;};defer _gd .Close ();_ ,_af =_ga .Copy (_gd ,_cee );return _af ;};func ReadPNG (file string )(_gb .Image ,error ){_bgd ,_e :=_ge .Open (file );if _e !=nil {return nil ,_e ;};defer _bgd .Close ();return _c .Decode (_bgd );};func ParseIndirectObjects (rawpdf string )(map[int64 ]_dg .PdfObject ,error ){_dgc :=_dg .NewParserFromString (rawpdf );_gdb :=map[int64 ]_dg .PdfObject {};for {_db ,_gaf :=_dgc .ParseIndirectObject ();if _gaf !=nil {if _gaf ==_ga .EOF {break ;};return nil ,_gaf ;};switch _fff :=_db .(type ){case *_dg .PdfIndirectObject :_gdb [_fff .ObjectNumber ]=_db ;case *_dg .PdfObjectStream :_gdb [_fff .ObjectNumber ]=_db ;};};for _ ,_gdd :=range _gdb {_gg (_gdd ,_gdb );};return _gdb ,nil ;};func RunRenderTest (t *_ff .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_cg :=_g .TrimSuffix (_a .Base (pdfPath ),_a .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_bfa *_ff .T ){_fe :=_a .Join (outputDir ,_cg );_be :=_fe +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _aa :=RenderPDFToPNGs (pdfPath ,0,_be );_aa !=nil {_bfa .Skip (_aa );};for _fb :=1;true ;_fb ++{_dff :=_ag .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_fe ,_fb );_ac :=_a .Join (baselineRenderPath ,_ag .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_cg ,_fb ));if _ ,_bcg :=_ge .Stat (_dff );_bcg !=nil {break ;};_bfa .Logf ("\u0025\u0073",_ac );if _ ,_cge :=_ge .Stat (_ac );_ge .IsNotExist (_cge ){if saveBaseline {_bfa .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_dff ,_ac );CopyFile (_dff ,_ac );continue ;};break ;};_bfa .Run (_ag .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_fb ),func (_faf *_ff .T ){_faf .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_dff ,_ac );_ef ,_beg :=ComparePNGFiles (_dff ,_ac );if _ge .IsNotExist (_beg ){_faf .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_ef {_faf .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");};});};});};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_baa :=_b .LookPath ("\u0067\u0073");_baa !=nil {return ErrRenderNotSupported ;};return _b .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_ag .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();};func HashFile (file string )(string ,error ){_ba ,_ab :=_ge .Open (file );if _ab !=nil {return "",_ab ;};defer _ba .Close ();_agd :=_f .New ();if _ ,_ab =_ga .Copy (_agd ,_ba );_ab !=nil {return "",_ab ;};return _ca .EncodeToString (_agd .Sum (nil )),nil ;};var (ErrRenderNotSupported =_gbf .New ("\u0072\u0065\u006e\u0064\u0065r\u0069\u006e\u0067\u0020\u0050\u0044\u0046\u0020\u0066\u0069\u006c\u0065\u0073 \u0069\u0073\u0020\u006e\u006f\u0074\u0020\u0073\u007
|