unipdf/internal/testutils/testutils.go
2022-06-27 19:58:38 +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 (_e "crypto/md5";_bb "encoding/hex";_cb "errors";_fde "fmt";_d "github.com/unidoc/unipdf/v3/common";_a "github.com/unidoc/unipdf/v3/core";_ca "image";_fg "image/png";_b "io";_fb "os";_fa "os/exec";_fd "path/filepath";_f "strings";
_cc "testing";);func ReadPNG (file string )(_ca .Image ,error ){_ec ,_bd :=_fb .Open (file );if _bd !=nil {return nil ,_bd ;};defer _ec .Close ();return _fg .Decode (_ec );};func CopyFile (src ,dst string )error {_fbf ,_ac :=_fb .Open (src );if _ac !=nil {return _ac ;
};defer _fbf .Close ();_bc ,_ac :=_fb .Create (dst );if _ac !=nil {return _ac ;};defer _bc .Close ();_ ,_ac =_b .Copy (_bc ,_fbf );return _ac ;};func CompareDictionariesDeep (d1 ,d2 *_a .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_d .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 ()));
_d .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_eba :=range d1 .Keys (){if _eba =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;};
_aca :=_a .TraceToDirectObject (d1 .Get (_eba ));_ed :=_a .TraceToDirectObject (d2 .Get (_eba ));if _aca ==nil {_d .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _ed ==nil {_d .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");
return false ;};switch _ece :=_aca .(type ){case *_a .PdfObjectDictionary :_bcc ,_eaf :=_ed .(*_a .PdfObjectDictionary );if !_eaf {_d .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_aca ,_ed );
return false ;};if !CompareDictionariesDeep (_ece ,_bcc ){return false ;};continue ;case *_a .PdfObjectArray :_acbg ,_dca :=_ed .(*_a .PdfObjectArray );if !_dca {_d .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");
return false ;};if _ece .Len ()!=_acbg .Len (){_d .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",_ece .Len (),_acbg .Len ());
return false ;};for _bbdb :=0;_bbdb < _ece .Len ();_bbdb ++{_ce :=_a .TraceToDirectObject (_ece .Get (_bbdb ));_dgb :=_a .TraceToDirectObject (_acbg .Get (_bbdb ));if _db ,_ggf :=_ce .(*_a .PdfObjectDictionary );_ggf {_gcf ,_bfa :=_dgb .(*_a .PdfObjectDictionary );
if !_bfa {return false ;};if !CompareDictionariesDeep (_db ,_gcf ){return false ;};}else {if _ce .WriteString ()!=_dgb .WriteString (){_d .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_ce .WriteString (),_dgb .WriteString ());
return false ;};};};continue ;};if _aca .String ()!=_ed .String (){_d .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",_eba ,_aca .String (),_ed .String ());
_d .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_aca ,_ed );_d .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_aca ,_ed );return false ;
};};return true ;};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 HashFile (file string )(string ,error ){_fc ,_ag :=_fb .Open (file );if _ag !=nil {return "",_ag ;};defer _fc .Close ();_fdg :=_e .New ();if _ ,_ag =_b .Copy (_fdg ,_fc );_ag !=nil {return "",_ag ;};return _bb .EncodeToString (_fdg .Sum (nil )),nil ;
};func ParseIndirectObjects (rawpdf string )(map[int64 ]_a .PdfObject ,error ){_bfe :=_a .NewParserFromString (rawpdf );_gcc :=map[int64 ]_a .PdfObject {};for {_ffc ,_dc :=_bfe .ParseIndirectObject ();if _dc !=nil {if _dc ==_b .EOF {break ;};return nil ,_dc ;
};switch _dd :=_ffc .(type ){case *_a .PdfIndirectObject :_gcc [_dd .ObjectNumber ]=_ffc ;case *_a .PdfObjectStream :_gcc [_dd .ObjectNumber ]=_ffc ;};};for _ ,_be :=range _gcc {_fbe (_be ,_gcc );};return _gcc ,nil ;};func RunRenderTest (t *_cc .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_aaf :=_f .TrimSuffix (_fd .Base (pdfPath ),_fd .Ext (pdfPath ));
t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_ff *_cc .T ){_fdb :=_fd .Join (outputDir ,_aaf );_ab :=_fdb +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _fe :=RenderPDFToPNGs (pdfPath ,0,_ab );_fe !=nil {_ff .Skip (_fe );};for _bg :=1;true ;_bg ++{_ea :=_fde .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_fdb ,_bg );
_ge :=_fd .Join (baselineRenderPath ,_fde .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_aaf ,_bg ));if _ ,_gc :=_fb .Stat (_ea );_gc !=nil {break ;};_ff .Logf ("\u0025\u0073",_ge );if saveBaseline {_ff .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_ea ,_ge );
_dfd :=CopyFile (_ea ,_ge );if _dfd !=nil {_ff .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_ge ,_dfd );};continue ;};_ff .Run (_fde .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_bg ),func (_cf *_cc .T ){_cf .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_ea ,_ge );
_dfb ,_af :=ComparePNGFiles (_ea ,_ge );if _fb .IsNotExist (_af ){_cf .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_dfb {_cf .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");
};});};});};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_cge :=_fa .LookPath ("\u0067\u0073");_cge !=nil {return ErrRenderNotSupported ;};return _fa .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_fde .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();
};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_ba ,_aa :=HashFile (file1 );if _aa !=nil {return false ,_aa ;};_ggd ,_aa :=HashFile (file2 );if _aa !=nil {return false ,_aa ;};if _ba ==_ggd {return true ,nil ;};_ace ,_aa :=ReadPNG (file1 );
if _aa !=nil {return false ,_aa ;};_bff ,_aa :=ReadPNG (file2 );if _aa !=nil {return false ,_aa ;};if _ace .Bounds ()!=_bff .Bounds (){return false ,nil ;};return CompareImages (_ace ,_bff );};func CompareImages (img1 ,img2 _ca .Image )(bool ,error ){_dg :=img1 .Bounds ();
_bf :=0;for _g :=0;_g < _dg .Size ().X ;_g ++{for _fcb :=0;_fcb < _dg .Size ().Y ;_fcb ++{_acb ,_gd ,_ee ,_ :=img1 .At (_g ,_fcb ).RGBA ();_bdg ,_ecg ,_gb ,_ :=img2 .At (_g ,_fcb ).RGBA ();if _acb !=_bdg ||_gd !=_ecg ||_ee !=_gb {_bf ++;};};};_cg :=float64 (_bf )/float64 (_dg .Dx ()*_dg .Dy ());
if _cg > 0.0001{_fde .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_cg ,_bf );return false ,nil ;};return true ,nil ;};func _fbe (_agc _a .PdfObject ,_faa map[int64 ]_a .PdfObject )error {switch _eb :=_agc .(type ){case *_a .PdfIndirectObject :_ged :=_eb ;
_fbe (_ged .PdfObject ,_faa );case *_a .PdfObjectDictionary :_cfc :=_eb ;for _ ,_gdg :=range _cfc .Keys (){_bcf :=_cfc .Get (_gdg );if _cfg ,_acc :=_bcf .(*_a .PdfObjectReference );_acc {_gce ,_fed :=_faa [_cfg .ObjectNumber ];if !_fed {return _cb .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
};_cfc .Set (_gdg ,_gce );}else {_fbe (_bcf ,_faa );};};case *_a .PdfObjectArray :_bbd :=_eb ;for _bge ,_gbd :=range _bbd .Elements (){if _ebc ,_abf :=_gbd .(*_a .PdfObjectReference );_abf {_ga ,_da :=_faa [_ebc .ObjectNumber ];if !_da {return _cb .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
};_bbd .Set (_bge ,_ga );}else {_fbe (_gbd ,_faa );};};};return nil ;};