// // 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";_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 [_eca .ObjectNumber ]=_dbg ;};};for _ ,_caeb :=range _df {_gcc (_caeb ,_df );};return _df ,nil ;};func CompareDictionariesDeep (d1 ,d2 *_ag .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_bf .Log .Debug ("\u0044\u0069\u0063\u0074\u0020\u0065\u006e\u0074\u0072\u0069\u0065\u0073\u0020\u006d\u0069s\u006da\u0074\u0063\u0068\u0020\u0028\u0025\u0064\u0020\u0021\u003d\u0020\u0025\u0064\u0029",len (d1 .Keys ()),len (d2 .Keys ()));_bf .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_dec :=range d1 .Keys (){if _dec =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;};_bbe :=_ag .TraceToDirectObject (d1 .Get (_dec ));_cbd :=_ag .TraceToDirectObject (d2 .Get (_dec ));if _bbe ==nil {_bf .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _cbd ==nil {_bf .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};switch _aeb :=_bbe .(type ){case *_ag .PdfObjectDictionary :_eee ,_ceg :=_cbd .(*_ag .PdfObjectDictionary );if !_ceg {_bf .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_bbe ,_cbd );return false ;};if !CompareDictionariesDeep (_aeb ,_eee ){return false ;};continue ;case *_ag .PdfObjectArray :_ddg ,_bac :=_cbd .(*_ag .PdfObjectArray );if !_bac {_bf .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");return false ;};if _aeb .Len ()!=_ddg .Len (){_bf .Log .Debug ("\u0061\u0072\u0072\u0061\u0079\u0020\u006c\u0065\u006e\u0067\u0074\u0068\u0020\u006d\u0069s\u006da\u0074\u0063\u0068\u0020\u0028\u0025\u0064\u0020\u0021\u003d\u0020\u0025\u0064\u0029",_aeb .Len (),_ddg .Len ());return false ;};for _gdbf :=0;_gdbf < _aeb .Len ();_gdbf ++{_dac :=_ag .TraceToDirectObject (_aeb .Get (_gdbf ));_aaf :=_ag .TraceToDirectObject (_ddg .Get (_gdbf ));if _gee ,_aaa :=_dac .(*_ag .PdfObjectDictionary );_aaa {_be ,_aca :=_aaf .(*_ag .PdfObjectDictionary );if !_aca {return false ;};if !CompareDictionariesDeep (_gee ,_be ){return false ;};}else {if _dac .WriteString ()!=_aaf .WriteString (){_bf .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_dac .WriteString (),_aaf .WriteString ());return false ;};};};continue ;};if _bbe .String ()!=_cbd .String (){_bf .Log .Debug ("\u006b\u0065y\u003d\u0025\u0073\u0020\u004d\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0021\u0020\u0027\u0025\u0073\u0027\u0020\u0021\u003d\u0020'%\u0073\u0027",_dec ,_bbe .String (),_cbd .String ());_bf .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_bbe ,_cbd );_bf .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_bbe ,_cbd );return false ;};};return true ;};func _gcc (_dce _ag .PdfObject ,_cba map[int64 ]_ag .PdfObject )error {switch _aba :=_dce .(type ){case *_ag .PdfIndirectObject :_gda :=_aba ;_gcc (_gda .PdfObject ,_cba );case *_ag .PdfObjectDictionary :_ede :=_aba ;for _ ,_ba :=range _ede .Keys (){_dbga :=_ede .Get (_ba );if _ce ,_fga :=_dbga .(*_ag .PdfObjectReference );_fga {_cf ,_fgb :=_cba [_ce .ObjectNumber ];if !_fgb {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");};_ede .Set (_ba ,_cf );}else {_gcc (_dbga ,_cba );};};case *_ag .PdfObjectArray :_adc :=_aba ;for _bc ,_bfd :=range _adc .Elements (){if _eda ,_gg :=_bfd .(*_ag .PdfObjectReference );_gg {_eag ,_eaf :=_cba [_eda .ObjectNumber ];if !_eaf {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");};_adc .Set (_bc ,_eag );}else {_gcc (_bfd ,_cba );};};};return nil ;};func CompareImages (img1 ,img2 _f .Image )(bool ,error ){_ae :=img1 .Bounds ();_ab :=0;for _gdb :=0;_gdb < _ae .Size ().X ;_gdb ++{for _db :=0;_db < _ae .Size ().Y ;_db ++{_ge ,_gce ,_agc ,_ :=img1 .At (_gdb ,_db ).RGBA ();_cc ,_ea ,_geb ,_ :=img2 .At (_gdb ,_db ).RGBA ();if _ge !=_cc ||_gce !=_ea ||_agc !=_geb {_ab ++;};};};_ga :=float64 (_ab )/float64 (_ae .Dx ()*_ae .Dy ());if _ga > 0.0001{_aa .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_ga ,_ab );return false ,nil ;};return true ,nil ;};