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 (_d "crypto/md5";_eb "encoding/hex";_db "errors";_e "fmt";_be "image";_f "image/png";_cf "io";_bg "os";_ga "os/exec";_c "path/filepath";_b "strings";_gf "testing";);func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_de ,_ge :=HashFile (file1 );if _ge !=nil {return false ,_ge ;};_dfb ,_ge :=HashFile (file2 );if _ge !=nil {return false ,_ge ;};if _de ==_dfb {return true ,nil ;};_bec ,_ge :=ReadPNG (file1 );if _ge !=nil {return false ,_ge ;};_fa ,_ge :=ReadPNG (file2 );if _ge !=nil {return false ,_ge ;};if _bec .Bounds ()!=_fa .Bounds (){return false ,nil ;};return CompareImages (_bec ,_fa );};func CompareImages (img1 ,img2 _be .Image )(bool ,error ){_gbe :=img1 .Bounds ();_a :=0;for _ee :=0;_ee < _gbe .Size ().X ;_ee ++{for _bee :=0;_bee < _gbe .Size ().Y ;_bee ++{_ad ,_bb ,_ebc ,_ :=img1 .At (_ee ,_bee ).RGBA ();_aa ,_gbc ,_cfb ,_ :=img2 .At (_ee ,_bee ).RGBA ();if _ad !=_aa ||_bb !=_gbc ||_ebc !=_cfb {_a ++;};};};_fe :=float64 (_a )/float64 (_gbe .Dx ()*_gbe .Dy ());if _fe > 0.0001{_e .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_fe ,_a );return false ,nil ;};return true ,nil ;};func RunRenderTest (t *_gf .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_aag :=_b .TrimSuffix (_c .Base (pdfPath ),_c .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_dga *_gf .T ){_fc :=_c .Join (outputDir ,_aag );_gaa :=_fc +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _bc :=RenderPDFToPNGs (pdfPath ,0,_gaa );_bc !=nil {_dga .Skip (_bc );};for _gfd :=1;true ;_gfd ++{_gad :=_e .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_fc ,_gfd );_cc :=_c .Join (baselineRenderPath ,_e .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_aag ,_gfd ));if _ ,_dfa :=_bg .Stat (_gad );_dfa !=nil {break ;};_dga .Logf ("\u0025\u0073",_cc );if saveBaseline {_dga .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_gad ,_cc );_cb :=CopyFile (_gad ,_cc );if _cb !=nil {_dga .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_cc ,_cb );};continue ;};_dga .Run (_e .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_gfd ),func (_eeb *_gf .T ){_eeb .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_gad ,_cc );_da ,_ae :=ComparePNGFiles (_gad ,_cc );if _bg .IsNotExist (_ae ){_eeb .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_da {_eeb .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");};});};});};func CopyFile (src ,dst string )error {_gff ,_dg :=_bg .Open (src );if _dg !=nil {return _dg ;};defer _gff .Close ();_ff ,_dg :=_bg .Create (dst );if _dg !=nil {return _dg ;};defer _ff .Close ();_ ,_dg =_cf .Copy (_ff ,_gff );return _dg ;};var (ErrRenderNotSupported =_db .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 _ ,_ecc :=_ga .LookPath ("\u0067\u0073");_ecc !=nil {return ErrRenderNotSupported ;};return _ga .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_e .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();};func ReadPNG (file string )(_be .Image ,error ){_gg ,_bgg :=_bg .Open (file );if _bgg !=nil {return nil ,_bgg ;};defer _gg .Close ();return _f .Decode (_gg );};func HashFile (file string )(string ,error ){_ca ,_ed :=_bg .Open (file );if _ed !=nil {return "",_ed ;};defer _ca .Close ();_gb :=_d .New ();if _ ,_ed =_cf .Copy (_gb ,_ca );_ed !=nil {return "",_ed ;};return _eb .EncodeToString (_gb .Sum (nil )),nil ;}; |