unipdf/internal/testutils/testutils.go
2023-08-03 17:30:04 +00:00

40 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 (_f "crypto/md5";_gb "encoding/hex";_ed "errors";_a "fmt";_ge "github.com/unidoc/unipdf/v3/common";_bg "github.com/unidoc/unipdf/v3/core";_b "image";_dae "image/png";_e "io";_da "os";_d "os/exec";_g "path/filepath";_fe "strings";
_fb "testing";);func CompareImages (img1 ,img2 _b .Image )(bool ,error ){_cd :=img1 .Bounds ();_fg :=0;for _ff :=0;_ff < _cd .Size ().X ;_ff ++{for _dbd :=0;_dbd < _cd .Size ().Y ;_dbd ++{_eg ,_fgf ,_ec ,_ :=img1 .At (_ff ,_dbd ).RGBA ();_fbb ,_ef ,_fbf ,_ :=img2 .At (_ff ,_dbd ).RGBA ();
if _eg !=_fbb ||_fgf !=_ef ||_ec !=_fbf {_fg ++;};};};_faa :=float64 (_fg )/float64 (_cd .Dx ()*_cd .Dy ());if _faa > 0.0001{_a .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_faa ,_fg );
return false ,nil ;};return true ,nil ;};func HashFile (file string )(string ,error ){_ba ,_db :=_da .Open (file );if _db !=nil {return "",_db ;};defer _ba .Close ();_ea :=_f .New ();if _ ,_db =_e .Copy (_ea ,_ba );_db !=nil {return "",_db ;};return _gb .EncodeToString (_ea .Sum (nil )),nil ;
};func RunRenderTest (t *_fb .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_ecg :=_fe .TrimSuffix (_g .Base (pdfPath ),_g .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_cfd *_fb .T ){_bf :=_g .Join (outputDir ,_ecg );
_beb :=_bf +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _de :=RenderPDFToPNGs (pdfPath ,0,_beb );_de !=nil {_cfd .Skip (_de );};for _ddg :=1;true ;_ddg ++{_dbe :=_a .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_bf ,_ddg );_baf :=_g .Join (baselineRenderPath ,_a .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_ecg ,_ddg ));
if _ ,_gd :=_da .Stat (_dbe );_gd !=nil {break ;};_cfd .Logf ("\u0025\u0073",_baf );if saveBaseline {_cfd .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_dbe ,_baf );_ege :=CopyFile (_dbe ,_baf );if _ege !=nil {_cfd .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_baf ,_ege );
};continue ;};_cfd .Run (_a .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_ddg ),func (_ebd *_fb .T ){_ebd .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_dbe ,_baf );_ca ,_bfg :=ComparePNGFiles (_dbe ,_baf );
if _da .IsNotExist (_bfg ){_ebd .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_ca {_ebd .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 _ ,_edc :=_d .LookPath ("\u0067\u0073");_edc !=nil {return ErrRenderNotSupported ;};return _d .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_a .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();
};func _aac (_abc _bg .PdfObject ,_cc map[int64 ]_bg .PdfObject )error {switch _ce :=_abc .(type ){case *_bg .PdfIndirectObject :_ggg :=_ce ;_aac (_ggg .PdfObject ,_cc );case *_bg .PdfObjectDictionary :_geg :=_ce ;for _ ,_acf :=range _geg .Keys (){_ded :=_geg .Get (_acf );
if _cdd ,_ccf :=_ded .(*_bg .PdfObjectReference );_ccf {_bab ,_aaf :=_cc [_cdd .ObjectNumber ];if !_aaf {return _ed .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
};_geg .Set (_acf ,_bab );}else {_aac (_ded ,_cc );};};case *_bg .PdfObjectArray :_gec :=_ce ;for _bag ,_bgb :=range _gec .Elements (){if _bgc ,_bc :=_bgb .(*_bg .PdfObjectReference );_bc {_abf ,_gfe :=_cc [_bgc .ObjectNumber ];if !_gfe {return _ed .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
};_gec .Set (_bag ,_abf );}else {_aac (_bgb ,_cc );};};};return nil ;};func CompareDictionariesDeep (d1 ,d2 *_bg .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_ge .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 ()));
_ge .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_aab :=range d1 .Keys (){if _aab =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;
};_ecd :=_bg .TraceToDirectObject (d1 .Get (_aab ));_dba :=_bg .TraceToDirectObject (d2 .Get (_aab ));if _ecd ==nil {_ge .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _dba ==nil {_ge .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");
return false ;};switch _abfa :=_ecd .(type ){case *_bg .PdfObjectDictionary :_ecdb ,_fbfg :=_dba .(*_bg .PdfObjectDictionary );if !_fbfg {_ge .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_ecd ,_dba );
return false ;};if !CompareDictionariesDeep (_abfa ,_ecdb ){return false ;};continue ;case *_bg .PdfObjectArray :_babc ,_cec :=_dba .(*_bg .PdfObjectArray );if !_cec {_ge .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");
return false ;};if _abfa .Len ()!=_babc .Len (){_ge .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",_abfa .Len (),_babc .Len ());
return false ;};for _eda :=0;_eda < _abfa .Len ();_eda ++{_bcg :=_bg .TraceToDirectObject (_abfa .Get (_eda ));_gca :=_bg .TraceToDirectObject (_babc .Get (_eda ));if _cdg ,_bgd :=_bcg .(*_bg .PdfObjectDictionary );_bgd {_geb ,_ebdg :=_gca .(*_bg .PdfObjectDictionary );
if !_ebdg {return false ;};if !CompareDictionariesDeep (_cdg ,_geb ){return false ;};}else {if _bcg .WriteString ()!=_gca .WriteString (){_ge .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_bcg .WriteString (),_gca .WriteString ());
return false ;};};};continue ;};if _ecd .String ()!=_dba .String (){_ge .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",_aab ,_ecd .String (),_dba .String ());
_ge .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_ecd ,_dba );_ge .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_ecd ,_dba );return false ;
};};return true ;};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_ac ,_cg :=HashFile (file1 );if _cg !=nil {return false ,_cg ;};_fd ,_cg :=HashFile (file2 );if _cg !=nil {return false ,_cg ;};if _ac ==_fd {return true ,nil ;};_eaf ,_cg :=ReadPNG (file1 );
if _cg !=nil {return false ,_cg ;};_gf ,_cg :=ReadPNG (file2 );if _cg !=nil {return false ,_cg ;};if _eaf .Bounds ()!=_gf .Bounds (){return false ,nil ;};return CompareImages (_eaf ,_gf );};func CopyFile (src ,dst string )error {_fc ,_eb :=_da .Open (src );
if _eb !=nil {return _eb ;};defer _fc .Close ();_dc ,_eb :=_da .Create (dst );if _eb !=nil {return _eb ;};defer _dc .Close ();_ ,_eb =_e .Copy (_dc ,_fc );return _eb ;};func ParseIndirectObjects (rawpdf string )(map[int64 ]_bg .PdfObject ,error ){_aa :=_bg .NewParserFromString (rawpdf );
_gc :=map[int64 ]_bg .PdfObject {};for {_aad ,_ga :=_aa .ParseIndirectObject ();if _ga !=nil {if _ga ==_e .EOF {break ;};return nil ,_ga ;};switch _bfb :=_aad .(type ){case *_bg .PdfIndirectObject :_gc [_bfb .ObjectNumber ]=_aad ;case *_bg .PdfObjectStream :_gc [_bfb .ObjectNumber ]=_aad ;
};};for _ ,_ab :=range _gc {_aac (_ab ,_gc );};return _gc ,nil ;};func ReadPNG (file string )(_b .Image ,error ){_ee ,_dd :=_da .Open (file );if _dd !=nil {return nil ,_dd ;};defer _ee .Close ();return _dae .Decode (_ee );};var (ErrRenderNotSupported =_ed .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");
);