unipdf/internal/testutils/testutils.go
2020-11-11 18:48:37 +00:00

12 lines
8.7 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";_bb "encoding/hex";_f "errors";_ac "fmt";_gb "github.com/unidoc/unipdf/v3/common";_ce "github.com/unidoc/unipdf/v3/core";_cg "image";_bc "image/png";_gc "io";_bg "os";_c "os/exec";_e "path/filepath";_b "strings";_ag "testing";);func HashFile (file string )(string ,error ){_ga ,_gcf :=_bg .Open (file );if _gcf !=nil {return "",_gcf ;};defer _ga .Close ();_ee :=_g .New ();if _ ,_gcf =_gc .Copy (_ee ,_ga );_gcf !=nil {return "",_gcf ;};return _bb .EncodeToString (_ee .Sum (nil )),nil ;};func CopyFile (src ,dst string )error {_af ,_aa :=_bg .Open (src );if _aa !=nil {return _aa ;};defer _af .Close ();_ba ,_aa :=_bg .Create (dst );if _aa !=nil {return _aa ;};defer _ba .Close ();_ ,_aa =_gc .Copy (_ba ,_af );return _aa ;};func _egd (_dfe _ce .PdfObject ,_feg map[int64 ]_ce .PdfObject )error {switch _ab :=_dfe .(type ){case *_ce .PdfIndirectObject :_bdb :=_ab ;_egd (_bdb .PdfObject ,_feg );case *_ce .PdfObjectDictionary :_dd :=_ab ;for _ ,_edd :=range _dd .Keys (){_eddd :=_dd .Get (_edd );if _bee ,_dc :=_eddd .(*_ce .PdfObjectReference );_dc {_dbd ,_fd :=_feg [_bee .ObjectNumber ];if !_fd {return _f .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");};_dd .Set (_edd ,_dbd );}else {_egd (_eddd ,_feg );};};case *_ce .PdfObjectArray :_gg :=_ab ;for _ebd ,_fee :=range _gg .Elements (){if _gda ,_ef :=_fee .(*_ce .PdfObjectReference );_ef {_aecc ,_cdg :=_feg [_gda .ObjectNumber ];if !_cdg {return _f .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 (_ebd ,_aecc );}else {_egd (_fee ,_feg );};};};return nil ;};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_bca :=_c .LookPath ("\u0067\u0073");_bca !=nil {return ErrRenderNotSupported ;};return _c .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_ac .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();};func RunRenderTest (t *_ag .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_fa :=_b .TrimSuffix (_e .Base (pdfPath ),_e .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_gf *_ag .T ){_ffae :=_e .Join (outputDir ,_fa );_gaf :=_ffae +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _fe :=RenderPDFToPNGs (pdfPath ,0,_gaf );_fe !=nil {_gf .Skip (_fe );};for _cgb :=1;true ;_cgb ++{_df :=_ac .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_ffae ,_cgb );_ffg :=_e .Join (baselineRenderPath ,_ac .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_fa ,_cgb ));if _ ,_fbc :=_bg .Stat (_df );_fbc !=nil {break ;};_gf .Logf ("\u0025\u0073",_ffg );if _ ,_bd :=_bg .Stat (_ffg );_bg .IsNotExist (_bd ){if saveBaseline {_gf .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_df ,_ffg );CopyFile (_df ,_ffg );continue ;};break ;};_gf .Run (_ac .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_cgb ),func (_cd *_ag .T ){_cd .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_df ,_ffg );_gce ,_ae :=ComparePNGFiles (_df ,_ffg );if _bg .IsNotExist (_ae ){_cd .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_gce {_cd .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");};});};});};var (ErrRenderNotSupported =_f .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 CompareDictionariesDeep (d1 ,d2 *_ce .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_gb .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 ()));_gb .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 ;};_bgc :=_ce .TraceToDirectObject (d1 .Get (_aab ));_fab :=_ce .TraceToDirectObject (d2 .Get (_aab ));if _bgc ==nil {_gb .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _fab ==nil {_gb .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};switch _edc :=_bgc .(type ){case *_ce .PdfObjectDictionary :_beea ,_bcb :=_fab .(*_ce .PdfObjectDictionary );if !_bcb {_gb .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_bgc ,_fab );return false ;};if !CompareDictionariesDeep (_edc ,_beea ){return false ;};continue ;case *_ce .PdfObjectArray :_efb ,_da :=_fab .(*_ce .PdfObjectArray );if !_da {_gb .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");return false ;};if _edc .Len ()!=_efb .Len (){_gb .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",_edc .Len (),_efb .Len ());return false ;};for _bdf :=0;_bdf < _edc .Len ();_bdf ++{_dfd :=_ce .TraceToDirectObject (_edc .Get (_bdf ));_cde :=_ce .TraceToDirectObject (_efb .Get (_bdf ));if _gae ,_gafd :=_dfd .(*_ce .PdfObjectDictionary );_gafd {_beg ,_ffaec :=_cde .(*_ce .PdfObjectDictionary );if !_ffaec {return false ;};if !CompareDictionariesDeep (_gae ,_beg ){return false ;};}else {if _dfd .WriteString ()!=_cde .WriteString (){_gb .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_dfd .WriteString (),_cde .WriteString ());return false ;};};};continue ;};if _bgc .String ()!=_fab .String (){_gb .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 ,_bgc .String (),_fab .String ());_gb .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_bgc ,_fab );_gb .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_bgc ,_fab );return false ;};};return true ;};func ReadPNG (file string )(_cg .Image ,error ){_be ,_bf :=_bg .Open (file );if _bf !=nil {return nil ,_bf ;};defer _be .Close ();return _bc .Decode (_be );};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_bbc ,_fb :=HashFile (file1 );if _fb !=nil {return false ,_fb ;};_agc ,_fb :=HashFile (file2 );if _fb !=nil {return false ,_fb ;};if _bbc ==_agc {return true ,nil ;};_ebg ,_fb :=ReadPNG (file1 );if _fb !=nil {return false ,_fb ;};_aaf ,_fb :=ReadPNG (file2 );if _fb !=nil {return false ,_fb ;};if _ebg .Bounds ()!=_aaf .Bounds (){return false ,nil ;};return CompareImages (_ebg ,_aaf );};func CompareImages (img1 ,img2 _cg .Image )(bool ,error ){_d :=img1 .Bounds ();_fc :=0;for _db :=0;_db < _d .Size ().X ;_db ++{for _gd :=0;_gd < _d .Size ().Y ;_gd ++{_eb ,_de ,_acd ,_ :=img1 .At (_db ,_gd ).RGBA ();_gab ,_ec ,_gbb ,_ :=img2 .At (_db ,_gd ).RGBA ();if _eb !=_gab ||_de !=_ec ||_acd !=_gbb {_fc ++;};};};_ffa :=float64 (_fc )/float64 (_d .Dx ()*_d .Dy ());if _ffa > 0.0001{_ac .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_ffa ,_fc );return false ,nil ;};return true ,nil ;};func ParseIndirectObjects (rawpdf string )(map[int64 ]_ce .PdfObject ,error ){_aee :=_ce .NewParserFromString (rawpdf );_aec :=map[int64 ]_ce .PdfObject {};for {_ebc ,_agce :=_aee .ParseIndirectObject ();if _agce !=nil {if _agce ==_gc .EOF {break ;};return nil ,_agce ;};switch _gea :=_ebc .(type ){case *_ce .PdfIndirectObject :_aec [_gea .ObjectNumber ]=_ebc ;case *_ce .PdfObjectStream :_aec [_gea .ObjectNumber ]=_ebc ;};};for _ ,_eg :=range _aec {_egd (_eg ,_aec );};return _aec ,nil ;};