unipdf/internal/testutils/testutils.go
2024-12-20 06:39:10 +00:00

39 lines
8.8 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 (_ag "crypto/md5";_abd "encoding/hex";_ff "errors";_d "fmt";_bcf "github.com/unidoc/unipdf/v3/common";_agd "github.com/unidoc/unipdf/v3/core";_ea "image";_ab "image/png";_be "io";_e "os";_bc "os/exec";_f "path/filepath";_b "strings";
_aga "testing";);func CompareDictionariesDeep (d1 ,d2 *_agd .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_bcf .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 ()));
_bcf .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_bfa :=range d1 .Keys (){if _bfa =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;
};_acf :=_agd .TraceToDirectObject (d1 .Get (_bfa ));_dgd :=_agd .TraceToDirectObject (d2 .Get (_bfa ));if _acf ==nil {_bcf .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _dgd ==nil {_bcf .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");
return false ;};switch _eff :=_acf .(type ){case *_agd .PdfObjectDictionary :_cc ,_dag :=_dgd .(*_agd .PdfObjectDictionary );if !_dag {_bcf .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_acf ,_dgd );
return false ;};if !CompareDictionariesDeep (_eff ,_cc ){return false ;};continue ;case *_agd .PdfObjectArray :_ed ,_fgb :=_dgd .(*_agd .PdfObjectArray );if !_fgb {_bcf .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");
return false ;};if _eff .Len ()!=_ed .Len (){_bcf .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",_eff .Len (),_ed .Len ());
return false ;};for _ba :=0;_ba < _eff .Len ();_ba ++{_bag :=_agd .TraceToDirectObject (_eff .Get (_ba ));_edf :=_agd .TraceToDirectObject (_ed .Get (_ba ));if _fd ,_dcg :=_bag .(*_agd .PdfObjectDictionary );_dcg {_bdb ,_gdc :=_edf .(*_agd .PdfObjectDictionary );
if !_gdc {return false ;};if !CompareDictionariesDeep (_fd ,_bdb ){return false ;};}else {if _bag .WriteString ()!=_edf .WriteString (){_bcf .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_bag .WriteString (),_edf .WriteString ());
return false ;};};};continue ;};if _acf .String ()!=_dgd .String (){_bcf .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",_bfa ,_acf .String (),_dgd .String ());
_bcf .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_acf ,_dgd );_bcf .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_acf ,_dgd );return false ;
};};return true ;};func CopyFile (src ,dst string )error {_bcd ,_da :=_e .Open (src );if _da !=nil {return _da ;};defer _bcd .Close ();_g ,_da :=_e .Create (dst );if _da !=nil {return _da ;};defer _g .Close ();_ ,_da =_be .Copy (_g ,_bcd );return _da ;
};func ReadPNG (file string )(_ea .Image ,error ){_eg ,_ffe :=_e .Open (file );if _ffe !=nil {return nil ,_ffe ;};defer _eg .Close ();return _ab .Decode (_eg );};func RunRenderTest (t *_aga .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_aa :=_b .TrimSuffix (_f .Base (pdfPath ),_f .Ext (pdfPath ));
t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_acb *_aga .T ){_de :=_f .Join (outputDir ,_aa );_cd :=_de +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _ega :=RenderPDFToPNGs (pdfPath ,0,_cd );_ega !=nil {_acb .Skip (_ega );};for _fc :=1;true ;_fc ++{_ge :=_d .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_de ,_fc );
_egf :=_f .Join (baselineRenderPath ,_d .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_aa ,_fc ));if _ ,_cb :=_e .Stat (_ge );_cb !=nil {break ;};_acb .Logf ("\u0025\u0073",_egf );if saveBaseline {_acb .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_ge ,_egf );
_dc :=CopyFile (_ge ,_egf );if _dc !=nil {_acb .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_egf ,_dc );};continue ;};_acb .Run (_d .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_fc ),func (_dg *_aga .T ){_dg .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_ge ,_egf );
_bdg ,_acd :=ComparePNGFiles (_ge ,_egf );if _e .IsNotExist (_acd ){_dg .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_bdg {_dg .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");
};});};});};func CompareImages (img1 ,img2 _ea .Image )(bool ,error ){_gb :=img1 .Bounds ();_bb :=0;for _beb :=0;_beb < _gb .Size ().X ;_beb ++{for _ga :=0;_ga < _gb .Size ().Y ;_ga ++{_beg ,_gd ,_egg ,_ :=img1 .At (_beb ,_ga ).RGBA ();_gad ,_gg ,_ad ,_ :=img2 .At (_beb ,_ga ).RGBA ();
if _beg !=_gad ||_gd !=_gg ||_egg !=_ad {_bb ++;};};};_ebde :=float64 (_bb )/float64 (_gb .Dx ()*_gb .Dy ());if _ebde > 0.0001{_d .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_ebde ,_bb );
return false ,nil ;};return true ,nil ;};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_bed :=_bc .LookPath ("\u0067\u0073");_bed !=nil {return ErrRenderNotSupported ;};return _bc .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_d .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();
};func _feb (_gee _agd .PdfObject ,_bf map[int64 ]_agd .PdfObject )error {switch _bcb :=_gee .(type ){case *_agd .PdfIndirectObject :_af :=_bcb ;_feb (_af .PdfObject ,_bf );case *_agd .PdfObjectDictionary :_fbd :=_bcb ;for _ ,_fac :=range _fbd .Keys (){_cdc :=_fbd .Get (_fac );
if _deb ,_ef :=_cdc .(*_agd .PdfObjectReference );_ef {_adf ,_fga :=_bf [_deb .ObjectNumber ];if !_fga {return _ff .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
};_fbd .Set (_fac ,_adf );}else {_feb (_cdc ,_bf );};};case *_agd .PdfObjectArray :_ebb :=_bcb ;for _feg ,_gef :=range _ebb .Elements (){if _ebg ,_efg :=_gef .(*_agd .PdfObjectReference );_efg {_ada ,_faca :=_bf [_ebg .ObjectNumber ];if !_faca {return _ff .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
};_ebb .Set (_feg ,_ada );}else {_feb (_gef ,_bf );};};};return nil ;};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_df ,_c :=HashFile (file1 );if _c !=nil {return false ,_c ;};_ade ,_c :=HashFile (file2 );if _c !=nil {return false ,_c ;};
if _df ==_ade {return true ,nil ;};_fa ,_c :=ReadPNG (file1 );if _c !=nil {return false ,_c ;};_cf ,_c :=ReadPNG (file2 );if _c !=nil {return false ,_c ;};if _fa .Bounds ()!=_cf .Bounds (){return false ,nil ;};return CompareImages (_fa ,_cf );};var (ErrRenderNotSupported =_ff .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 ){_bd ,_ebd :=_e .Open (file );if _ebd !=nil {return "",_ebd ;};defer _bd .Close ();_agc :=_ag .New ();if _ ,_ebd =_be .Copy (_agc ,_bd );_ebd !=nil {return "",_ebd ;};return _abd .EncodeToString (_agc .Sum (nil )),nil ;
};func ParseIndirectObjects (rawpdf string )(map[int64 ]_agd .PdfObject ,error ){_fg :=_agd .NewParserFromString (rawpdf );_ec :=map[int64 ]_agd .PdfObject {};for {_fcf ,_dgf :=_fg .ParseIndirectObject ();if _dgf !=nil {if _dgf ==_be .EOF {break ;};return nil ,_dgf ;
};switch _dd :=_fcf .(type ){case *_agd .PdfIndirectObject :_ec [_dd .ObjectNumber ]=_fcf ;case *_agd .PdfObjectStream :_ec [_dd .ObjectNumber ]=_fcf ;};};for _ ,_faf :=range _ec {_feb (_faf ,_ec );};return _ec ,nil ;};