unipdf/internal/testutils/testutils.go
2022-02-05 21:34:53 +00:00

40 lines
8.9 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 (_g "crypto/md5";_e "encoding/hex";_be "errors";_bf "fmt";_gd "github.com/unidoc/unipdf/v3/common";_af "github.com/unidoc/unipdf/v3/core";_gf "image";_cc "image/png";_ga "io";_c "os";_dc "os/exec";_db "path/filepath";_a "strings";
_b "testing";);func CompareDictionariesDeep (d1 ,d2 *_af .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_gd .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 ()));
_gd .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_eda :=range d1 .Keys (){if _eda =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;
};_cceg :=_af .TraceToDirectObject (d1 .Get (_eda ));_cdee :=_af .TraceToDirectObject (d2 .Get (_eda ));if _cceg ==nil {_gd .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _cdee ==nil {_gd .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");
return false ;};switch _gaf :=_cceg .(type ){case *_af .PdfObjectDictionary :_gdfa ,_aaf :=_cdee .(*_af .PdfObjectDictionary );if !_aaf {_gd .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_cceg ,_cdee );
return false ;};if !CompareDictionariesDeep (_gaf ,_gdfa ){return false ;};continue ;case *_af .PdfObjectArray :_ebg ,_efe :=_cdee .(*_af .PdfObjectArray );if !_efe {_gd .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");
return false ;};if _gaf .Len ()!=_ebg .Len (){_gd .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",_gaf .Len (),_ebg .Len ());
return false ;};for _ca :=0;_ca < _gaf .Len ();_ca ++{_ggf :=_af .TraceToDirectObject (_gaf .Get (_ca ));_ab :=_af .TraceToDirectObject (_ebg .Get (_ca ));if _ebb ,_dg :=_ggf .(*_af .PdfObjectDictionary );_dg {_bae ,_adg :=_ab .(*_af .PdfObjectDictionary );
if !_adg {return false ;};if !CompareDictionariesDeep (_ebb ,_bae ){return false ;};}else {if _ggf .WriteString ()!=_ab .WriteString (){_gd .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_ggf .WriteString (),_ab .WriteString ());
return false ;};};};continue ;};if _cceg .String ()!=_cdee .String (){_gd .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",_eda ,_cceg .String (),_cdee .String ());
_gd .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_cceg ,_cdee );_gd .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_cceg ,_cdee );return false ;
};};return true ;};func RunRenderTest (t *_b .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_fg :=_a .TrimSuffix (_db .Base (pdfPath ),_db .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_ea *_b .T ){_aag :=_db .Join (outputDir ,_fg );
_cdf :=_aag +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _dad :=RenderPDFToPNGs (pdfPath ,0,_cdf );_dad !=nil {_ea .Skip (_dad );};for _bg :=1;true ;_bg ++{_eg :=_bf .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_aag ,_bg );_ec :=_db .Join (baselineRenderPath ,_bf .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_fg ,_bg ));
if _ ,_ccf :=_c .Stat (_eg );_ccf !=nil {break ;};_ea .Logf ("\u0025\u0073",_ec );if saveBaseline {_ea .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_eg ,_ec );_eab :=CopyFile (_eg ,_ec );if _eab !=nil {_ea .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_ec ,_eab );
};continue ;};_ea .Run (_bf .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_bg ),func (_de *_b .T ){_de .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_eg ,_ec );_ee ,_daf :=ComparePNGFiles (_eg ,_ec );
if _c .IsNotExist (_daf ){_de .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_ee {_de .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");
};});};});};func CompareImages (img1 ,img2 _gf .Image )(bool ,error ){_dbf :=img1 .Bounds ();_gdg :=0;for _fd :=0;_fd < _dbf .Size ().X ;_fd ++{for _fc :=0;_fc < _dbf .Size ().Y ;_fc ++{_bd ,_ed ,_fec ,_ :=img1 .At (_fd ,_fc ).RGBA ();_fb ,_cf ,_bfd ,_ :=img2 .At (_fd ,_fc ).RGBA ();
if _bd !=_fb ||_ed !=_cf ||_fec !=_bfd {_gdg ++;};};};_ge :=float64 (_gdg )/float64 (_dbf .Dx ()*_dbf .Dy ());if _ge > 0.0001{_bf .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_ge ,_gdg );
return false ,nil ;};return true ,nil ;};func ParseIndirectObjects (rawpdf string )(map[int64 ]_af .PdfObject ,error ){_dac :=_af .NewParserFromString (rawpdf );_cde :=map[int64 ]_af .PdfObject {};for {_cb ,_dcda :=_dac .ParseIndirectObject ();if _dcda !=nil {if _dcda ==_ga .EOF {break ;
};return nil ,_dcda ;};switch _ad :=_cb .(type ){case *_af .PdfIndirectObject :_cde [_ad .ObjectNumber ]=_cb ;case *_af .PdfObjectStream :_cde [_ad .ObjectNumber ]=_cb ;};};for _ ,_fdbg :=range _cde {_cg (_fdbg ,_cde );};return _cde ,nil ;};func _cg (_fab _af .PdfObject ,_fed map[int64 ]_af .PdfObject )error {switch _cda :=_fab .(type ){case *_af .PdfIndirectObject :_bab :=_cda ;
_cg (_bab .PdfObject ,_fed );case *_af .PdfObjectDictionary :_bfe :=_cda ;for _ ,_dcb :=range _bfe .Keys (){_cca :=_bfe .Get (_dcb );if _adf ,_gef :=_cca .(*_af .PdfObjectReference );_gef {_gfg ,_cdd :=_fed [_adf .ObjectNumber ];if !_cdd {return _be .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
};_bfe .Set (_dcb ,_gfg );}else {_cg (_cca ,_fed );};};case *_af .PdfObjectArray :_dadf :=_cda ;for _gfd ,_ced :=range _dadf .Elements (){if _gde ,_eb :=_ced .(*_af .PdfObjectReference );_eb {_gg ,_bfa :=_fed [_gde .ObjectNumber ];if !_bfa {return _be .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
};_dadf .Set (_gfd ,_gg );}else {_cg (_ced ,_fed );};};};return nil ;};func CopyFile (src ,dst string )error {_gac ,_cd :=_c .Open (src );if _cd !=nil {return _cd ;};defer _gac .Close ();_f ,_cd :=_c .Create (dst );if _cd !=nil {return _cd ;};defer _f .Close ();
_ ,_cd =_ga .Copy (_f ,_gac );return _cd ;};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_ccb ,_fcd :=HashFile (file1 );if _fcd !=nil {return false ,_fcd ;};_bdc ,_fcd :=HashFile (file2 );if _fcd !=nil {return false ,_fcd ;};if _ccb ==_bdc {return true ,nil ;
};_fdb ,_fcd :=ReadPNG (file1 );if _fcd !=nil {return false ,_fcd ;};_ag ,_fcd :=ReadPNG (file2 );if _fcd !=nil {return false ,_fcd ;};if _fdb .Bounds ()!=_ag .Bounds (){return false ,nil ;};return CompareImages (_fdb ,_ag );};func HashFile (file string )(string ,error ){_fa ,_cce :=_c .Open (file );
if _cce !=nil {return "",_cce ;};defer _fa .Close ();_gae :=_g .New ();if _ ,_cce =_ga .Copy (_gae ,_fa );_cce !=nil {return "",_cce ;};return _e .EncodeToString (_gae .Sum (nil )),nil ;};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;
};if _ ,_da :=_dc .LookPath ("\u0067\u0073");_da !=nil {return ErrRenderNotSupported ;};return _dc .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_bf .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();
};var (ErrRenderNotSupported =_be .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 ReadPNG (file string )(_gf .Image ,error ){_dcd ,_beb :=_c .Open (file );if _beb !=nil {return nil ,_beb ;};defer _dcd .Close ();return _cc .Decode (_dcd );};