2020-10-05 19:28:24 +00:00
|
|
|
package testutils ;import (_g "crypto/md5";_gg "encoding/hex";_gc "errors";_gfd "fmt";_d "github.com/unidoc/unipdf/v3/common";_de "github.com/unidoc/unipdf/v3/core";_ge "image";_gf "image/png";_bg "io";_e "os";_b "os/exec";_cc "path/filepath";_a "strings";_f "testing";);func CompareImages (img1 ,img2 _ge .Image )(bool ,error ){_cg :=img1 .Bounds ();_bd :=0;for _dd :=0;_dd < _cg .Size ().X ;_dd ++{for _fd :=0;_fd < _cg .Size ().Y ;_fd ++{_aab ,_ggb ,_df ,_ :=img1 .At (_dd ,_fd ).RGBA ();_gcf ,_bdf ,_cb ,_ :=img2 .At (_dd ,_fd ).RGBA ();if _aab !=_gcf ||_ggb !=_bdf ||_df !=_cb {_bd ++;};};};_bf :=float64 (_bd )/float64 (_cg .Dx ()*_cg .Dy ());if _bf > 0.0001{_gfd .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_bf ,_bd );return false ,nil ;};return true ,nil ;};func HashFile (file string )(string ,error ){_af ,_acf :=_e .Open (file );if _acf !=nil {return "",_acf ;};defer _af .Close ();_ccf :=_g .New ();if _ ,_acf =_bg .Copy (_ccf ,_af );_acf !=nil {return "",_acf ;};return _gg .EncodeToString (_ccf .Sum (nil )),nil ;};func _gfc (_gfb _de .PdfObject ,_gd map[int64 ]_de .PdfObject )error {switch _ga :=_gfb .(type ){case *_de .PdfIndirectObject :_bda :=_ga ;_gfc (_bda .PdfObject ,_gd );case *_de .PdfObjectDictionary :_egc :=_ga ;for _ ,_fg :=range _egc .Keys (){_cga :=_egc .Get (_fg );if _cff ,_bbf :=_cga .(*_de .PdfObjectReference );_bbf {_bbd ,_gba :=_gd [_cff .ObjectNumber ];if !_gba {return _gc .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");};_egc .Set (_fg ,_bbd );}else {_gfc (_cga ,_gd );};};case *_de .PdfObjectArray :_dbf :=_ga ;for _add ,_bfb :=range _dbf .Elements (){if _fdc ,_bdg :=_bfb .(*_de .PdfObjectReference );_bdg {_bc ,_dea :=_gd [_fdc .ObjectNumber ];if !_dea {return _gc .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");};_dbf .Set (_add ,_bc );}else {_gfc (_bfb ,_gd );};};};return nil ;};var (ErrRenderNotSupported =_gc .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\u0075\u0070\u0070\u006f\u0072\u0074\u0065\u0064\u0020\u006f\u006e\u0020\u0074\u0068\u0069\u0073\u0020\u0073\u0079\u0073\u0074\u0065m"););func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_cf :=_b .LookPath ("\u0067\u0073");_cf !=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 ,_gfd .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_bde ,_fa :=HashFile (file1 );if _fa !=nil {return false ,_fa ;};_eg ,_fa :=HashFile (file2 );if _fa !=nil {return false ,_fa ;};if _bde ==_eg {return true ,nil ;};_ff ,_fa :=ReadPNG (file1 );if _fa !=nil {return false ,_fa ;};_fe ,_fa :=ReadPNG (file2 );if _fa !=nil {return false ,_fa ;};if _ff .Bounds ()!=_fe .Bounds (){return false ,nil ;};return CompareImages (_ff ,_fe );};func RunRenderTest (t *_f .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_ec :=_a .TrimSuffix (_cc .Base (pdfPath ),_cc .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_ca *_f .T ){_fdg :=_cc .Join (outputDir ,_ec );_cad :=_fdg +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _dg :=RenderPDFToPNGs (pdfPath ,0,_cad );_dg !=nil {_ca .Skip (_dg );};for _fad :=1;true ;_fad ++{_faf :=_gfd .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_fdg ,_fad );_def :=_cc .Join (baselineRenderPath ,_gfd .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_ec ,_fad ));if _ ,_dc :=_e .Stat (_faf );_dc !=nil {break ;};_ca .Logf ("\u0025\u0073",_def );if _ ,_gb :=_e .Stat (_def );_e .IsNotExist (_gb ){if saveBasel
|