mirror of
https://github.com/unidoc/unioffice.git
synced 2025-04-29 13:49:10 +08:00
12 lines
4.5 KiB
Go
12 lines
4.5 KiB
Go
//
|
|
// Copyright 2020 FoxyUtils ehf. All rights reserved.
|
|
//
|
|
// This is a commercial product and requires a license to operate.
|
|
// A trial license can be obtained at https://unidoc.io
|
|
//
|
|
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
|
|
//
|
|
// Use of this source code is governed by the UniDoc End User License Agreement
|
|
// terms that can be accessed at https://unidoc.io/eula/
|
|
|
|
package testutils ;import (_a "crypto/md5";_ba "encoding/hex";_cb "errors";_bg "fmt";_e "image";_fc "image/png";_fe "io";_f "os";_ab "os/exec";_b "path/filepath";_dd "strings";_d "testing";);func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_gg ,_afa :=HashFile (file1 );if _afa !=nil {return false ,_afa ;};_cae ,_afa :=HashFile (file2 );if _afa !=nil {return false ,_afa ;};if _gg ==_cae {return true ,nil ;};_eeg ,_afa :=ReadPNG (file1 );if _afa !=nil {return false ,_afa ;};_ed ,_afa :=ReadPNG (file2 );if _afa !=nil {return false ,_afa ;};if _eeg .Bounds ()!=_ed .Bounds (){return false ,nil ;};return CompareImages (_eeg ,_ed );};func ReadPNG (file string )(_e .Image ,error ){_g ,_ca :=_f .Open (file );if _ca !=nil {return nil ,_ca ;};defer _g .Close ();return _fc .Decode (_g );};func HashFile (file string )(string ,error ){_cg ,_bf :=_f .Open (file );if _bf !=nil {return "",_bf ;};defer _cg .Close ();_ff :=_a .New ();if _ ,_bf =_fe .Copy (_ff ,_cg );_bf !=nil {return "",_bf ;};return _ba .EncodeToString (_ff .Sum (nil )),nil ;};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_gb :=_ab .LookPath ("\u0067\u0073");_gb !=nil {return ErrRenderNotSupported ;};return _ab .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_bg .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();};var (ErrRenderNotSupported =_cb .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 RunRenderTest (t *_d .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_caa :=_dd .TrimSuffix (_b .Base (pdfPath ),_b .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_bff *_d .T ){_eba :=_b .Join (outputDir ,_caa );_feb :=_eba +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _bfg :=RenderPDFToPNGs (pdfPath ,0,_feb );_bfg !=nil {_bff .Skip (_bfg );};for _be :=1;true ;_be ++{_fbc :=_bg .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_eba ,_be );_ad :=_b .Join (baselineRenderPath ,_bg .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_caa ,_be ));if _ ,_bfcg :=_f .Stat (_fbc );_bfcg !=nil {break ;};_bff .Logf ("\u0025\u0073",_ad );if saveBaseline {_bff .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_fbc ,_ad );_gaf :=CopyFile (_fbc ,_ad );if _gaf !=nil {_bff .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_ad ,_gaf );};continue ;};_bff .Run (_bg .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_be ),func (_gac *_d .T ){_gac .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_fbc ,_ad );_gcc ,_fg :=ComparePNGFiles (_fbc ,_ad );if _f .IsNotExist (_fg ){_gac .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_gcc {_gac .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");};});};});};func CopyFile (src ,dst string )error {_af ,_bgg :=_f .Open (src );if _bgg !=nil {return _bgg ;};defer _af .Close ();_fa ,_bgg :=_f .Create (dst );if _bgg !=nil {return _bgg ;};defer _fa .Close ();_ ,_bgg =_fe .Copy (_fa ,_af );return _bgg ;};func CompareImages (img1 ,img2 _e .Image )(bool ,error ){_gc :=img1 .Bounds ();_ac :=0;for _dda :=0;_dda < _gc .Size ().X ;_dda ++{for _fd :=0;_fd < _gc .Size ().Y ;_fd ++{_fce ,_ee ,_fb ,_ :=img1 .At (_dda ,_fd ).RGBA ();_ce ,_bfc ,_aca ,_ :=img2 .At (_dda ,_fd ).RGBA ();if _fce !=_ce ||_ee !=_bfc ||_fb !=_aca {_ac ++;};};};_ddab :=float64 (_ac )/float64 (_gc .Dx ()*_gc .Dy ());if _ddab > 0.0001{_bg .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_ddab ,_ac );return false ,nil ;};return true ,nil ;}; |