2021-02-11 10:35:13 +00:00
|
|
|
package testutils ;import (_d "crypto/md5";_eb "encoding/hex";_gc "errors";_aa "fmt";_bf "github.com/unidoc/unipdf/v3/common";_ag "github.com/unidoc/unipdf/v3/core";_f "image";_dc "image/png";_dd "io";_a "os";_g "os/exec";_b "path/filepath";_dg "strings";_ed "testing";);func ReadPNG (file string )(_f .Image ,error ){_bd ,_ca :=_a .Open (file );if _ca !=nil {return nil ,_ca ;};defer _bd .Close ();return _dc .Decode (_bd );};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_ccg :=_g .LookPath ("\u0067\u0073");_ccg !=nil {return ErrRenderNotSupported ;};return _g .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_aa .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();};func RunRenderTest (t *_ed .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_gcg :=_dg .TrimSuffix (_b .Base (pdfPath ),_b .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_ef *_ed .T ){_gceg :=_b .Join (outputDir ,_gcg );_ad :=_gceg +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _bg :=RenderPDFToPNGs (pdfPath ,0,_ad );_bg !=nil {_ef .Skip (_bg );};for _fgd :=1;true ;_fgd ++{_ac :=_aa .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_gceg ,_fgd );_dee :=_b .Join (baselineRenderPath ,_aa .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_gcg ,_fgd ));if _ ,_ec :=_a .Stat (_ac );_ec !=nil {break ;};_ef .Logf ("\u0025\u0073",_dee );if saveBaseline {_ef .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_ac ,_dee );_bdf :=CopyFile (_ac ,_dee );if _bdf !=nil {_ef .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_dee ,_bdf );};continue ;};_ef .Run (_aa .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_fgd ),func (_cb *_ed .T ){_cb .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_ac ,_dee );_ee ,_ecg :=ComparePNGFiles (_ac ,_dee );if _a .IsNotExist (_ecg ){_cb .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_ee {_cb .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");};});};});};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 HashFile (file string )(string ,error ){_bb ,_eg :=_a .Open (file );if _eg !=nil {return "",_eg ;};defer _bb .Close ();_caa :=_d .New ();if _ ,_eg =_dd .Copy (_caa ,_bb );_eg !=nil {return "",_eg ;};return _eb .EncodeToString (_caa .Sum (nil )),nil ;};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_fd ,_bbc :=HashFile (file1 );if _bbc !=nil {return false ,_bbc ;};_de ,_bbc :=HashFile (file2 );if _bbc !=nil {return false ,_bbc ;};if _fd ==_de {return true ,nil ;};_cae ,_bbc :=ReadPNG (file1 );if _bbc !=nil {return false ,_bbc ;};_edf ,_bbc :=ReadPNG (file2 );if _bbc !=nil {return false ,_bbc ;};if _cae .Bounds ()!=_edf .Bounds (){return false ,nil ;};return CompareImages (_cae ,_edf );};func CopyFile (src ,dst string )error {_fb ,_fg :=_a .Open (src );if _fg !=nil {return _fg ;};defer _fb .Close ();_c ,_fg :=_a .Create (dst );if _fg !=nil {return _fg ;};defer _c .Close ();_ ,_fg =_dd .Copy (_c ,_fb );return _fg ;};func ParseIndirectObjects (rawpdf string )(map[int64 ]_ag .PdfObject ,error ){_fa :=_ag .NewParserFromString (rawpdf );_df :=map[int64 ]_ag .PdfObject {};for {_dbg ,_egb :=_fa .ParseIndirectObject ();if _egb !=nil {if _egb ==_dd .EOF {break ;};return nil ,_egb ;};switch _eca :=_dbg .(type ){case *_ag .PdfIndirectObject :_df [_eca .ObjectNumber ]=_dbg ;case *_ag .PdfObjectStream :_df [_
|