unipdf/internal/testutils/testutils.go
2022-06-06 22:48:24 +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 (_g "crypto/md5";_db "encoding/hex";_bc "errors";_ff "fmt";_a "github.com/unidoc/unipdf/v3/common";_ae "github.com/unidoc/unipdf/v3/core";_be "image";_e "image/png";_fe "io";_c "os";_gf "os/exec";_bd "path/filepath";_b "strings";
_d "testing";);func ReadPNG (file string )(_be .Image ,error ){_ca ,_bf :=_c .Open (file );if _bf !=nil {return nil ,_bf ;};defer _ca .Close ();return _e .Decode (_ca );};var (ErrRenderNotSupported =_bc .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 RunRenderTest (t *_d .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_gd :=_b .TrimSuffix (_bd .Base (pdfPath ),_bd .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_fffd *_d .T ){_gbe :=_bd .Join (outputDir ,_gd );
_dgd :=_gbe +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _bcdg :=RenderPDFToPNGs (pdfPath ,0,_dgd );_bcdg !=nil {_fffd .Skip (_bcdg );};for _ef :=1;true ;_ef ++{_egd :=_ff .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_gbe ,_ef );_gdc :=_bd .Join (baselineRenderPath ,_ff .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_gd ,_ef ));
if _ ,_bce :=_c .Stat (_egd );_bce !=nil {break ;};_fffd .Logf ("\u0025\u0073",_gdc );if saveBaseline {_fffd .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_egd ,_gdc );_gc :=CopyFile (_egd ,_gdc );if _gc !=nil {_fffd .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_gdc ,_gc );
};continue ;};_fffd .Run (_ff .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_ef ),func (_bg *_d .T ){_bg .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_egd ,_gdc );_bfd ,_dbc :=ComparePNGFiles (_egd ,_gdc );
if _c .IsNotExist (_dbc ){_bg .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_bfd {_bg .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");
};});};});};func CompareImages (img1 ,img2 _be .Image )(bool ,error ){_gb :=img1 .Bounds ();_eg :=0;for _ba :=0;_ba < _gb .Size ().X ;_ba ++{for _cfg :=0;_cfg < _gb .Size ().Y ;_cfg ++{_fc ,_ee ,_fa ,_ :=img1 .At (_ba ,_cfg ).RGBA ();_ed ,_dg ,_bcd ,_ :=img2 .At (_ba ,_cfg ).RGBA ();
if _fc !=_ed ||_ee !=_dg ||_fa !=_bcd {_eg ++;};};};_cg :=float64 (_eg )/float64 (_gb .Dx ()*_gb .Dy ());if _cg > 0.0001{_ff .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_cg ,_eg );
return false ,nil ;};return true ,nil ;};func HashFile (file string )(string ,error ){_cf ,_ag :=_c .Open (file );if _ag !=nil {return "",_ag ;};defer _cf .Close ();_fd :=_g .New ();if _ ,_ag =_fe .Copy (_fd ,_cf );_ag !=nil {return "",_ag ;};return _db .EncodeToString (_fd .Sum (nil )),nil ;
};func ParseIndirectObjects (rawpdf string )(map[int64 ]_ae .PdfObject ,error ){_fac :=_ae .NewParserFromString (rawpdf );_edab :=map[int64 ]_ae .PdfObject {};for {_aa ,_cc :=_fac .ParseIndirectObject ();if _cc !=nil {if _cc ==_fe .EOF {break ;};return nil ,_cc ;
};switch _eea :=_aa .(type ){case *_ae .PdfIndirectObject :_edab [_eea .ObjectNumber ]=_aa ;case *_ae .PdfObjectStream :_edab [_eea .ObjectNumber ]=_aa ;};};for _ ,_fffg :=range _edab {_gff (_fffg ,_edab );};return _edab ,nil ;};func CompareDictionariesDeep (d1 ,d2 *_ae .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_a .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 ()));
_a .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_gbd :=range d1 .Keys (){if _gbd =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;};
_cgf :=_ae .TraceToDirectObject (d1 .Get (_gbd ));_bed :=_ae .TraceToDirectObject (d2 .Get (_gbd ));if _cgf ==nil {_a .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _bed ==nil {_a .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");
return false ;};switch _gae :=_cgf .(type ){case *_ae .PdfObjectDictionary :_ab ,_cda :=_bed .(*_ae .PdfObjectDictionary );if !_cda {_a .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_cgf ,_bed );
return false ;};if !CompareDictionariesDeep (_gae ,_ab ){return false ;};continue ;case *_ae .PdfObjectArray :_edc ,_bb :=_bed .(*_ae .PdfObjectArray );if !_bb {_a .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");
return false ;};if _gae .Len ()!=_edc .Len (){_a .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",_gae .Len (),_edc .Len ());
return false ;};for _fdf :=0;_fdf < _gae .Len ();_fdf ++{_dcd :=_ae .TraceToDirectObject (_gae .Get (_fdf ));_eee :=_ae .TraceToDirectObject (_edc .Get (_fdf ));if _bfg ,_ccg :=_dcd .(*_ae .PdfObjectDictionary );_ccg {_aag ,_cab :=_eee .(*_ae .PdfObjectDictionary );
if !_cab {return false ;};if !CompareDictionariesDeep (_bfg ,_aag ){return false ;};}else {if _dcd .WriteString ()!=_eee .WriteString (){_a .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_dcd .WriteString (),_eee .WriteString ());
return false ;};};};continue ;};if _cgf .String ()!=_bed .String (){_a .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",_gbd ,_cgf .String (),_bed .String ());
_a .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_cgf ,_bed );_a .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_cgf ,_bed );return false ;
};};return true ;};func CopyFile (src ,dst string )error {_cb ,_bdd :=_c .Open (src );if _bdd !=nil {return _bdd ;};defer _cb .Close ();_ec ,_bdd :=_c .Create (dst );if _bdd !=nil {return _bdd ;};defer _ec .Close ();_ ,_bdd =_fe .Copy (_ec ,_cb );return _bdd ;
};func _gff (_dbe _ae .PdfObject ,_ffd map[int64 ]_ae .PdfObject )error {switch _ga :=_dbe .(type ){case *_ae .PdfIndirectObject :_ad :=_ga ;_gff (_ad .PdfObject ,_ffd );case *_ae .PdfObjectDictionary :_fdc :=_ga ;for _ ,_fgd :=range _fdc .Keys (){_df :=_fdc .Get (_fgd );
if _fad ,_acc :=_df .(*_ae .PdfObjectReference );_acc {_faf ,_gaf :=_ffd [_fad .ObjectNumber ];if !_gaf {return _bc .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
};_fdc .Set (_fgd ,_faf );}else {_gff (_df ,_ffd );};};case *_ae .PdfObjectArray :_gg :=_ga ;for _fag ,_gdce :=range _gg .Elements (){if _gafc ,_ce :=_gdce .(*_ae .PdfObjectReference );_ce {_aea ,_bfc :=_ffd [_gafc .ObjectNumber ];if !_bfc {return _bc .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
};_gg .Set (_fag ,_aea );}else {_gff (_gdce ,_ffd );};};};return nil ;};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_cae :=_gf .LookPath ("\u0067\u0073");_cae !=nil {return ErrRenderNotSupported ;
};return _gf .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_ff .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_dc ,_cd :=HashFile (file1 );
if _cd !=nil {return false ,_cd ;};_fff ,_cd :=HashFile (file2 );if _cd !=nil {return false ,_cd ;};if _dc ==_fff {return true ,nil ;};_eda ,_cd :=ReadPNG (file1 );if _cd !=nil {return false ,_cd ;};_ffc ,_cd :=ReadPNG (file2 );if _cd !=nil {return false ,_cd ;
};if _eda .Bounds ()!=_ffc .Bounds (){return false ,nil ;};return CompareImages (_eda ,_ffc );};