unipdf/internal/testutils/testutils.go
2021-03-23 23:12:52 +00:00

41 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";_aa "encoding/hex";_ac "errors";_fgb "fmt";_fe "github.com/unidoc/unipdf/v3/common";_b "github.com/unidoc/unipdf/v3/core";_a "image";_ca "image/png";_ec "io";_ce "os";_gc "os/exec";_c "path/filepath";_fg "strings";
_g "testing";);func ParseIndirectObjects (rawpdf string )(map[int64 ]_b .PdfObject ,error ){_gbb :=_b .NewParserFromString (rawpdf );_bed :=map[int64 ]_b .PdfObject {};for {_cca ,_aeab :=_gbb .ParseIndirectObject ();if _aeab !=nil {if _aeab ==_ec .EOF {break ;
};return nil ,_aeab ;};switch _dge :=_cca .(type ){case *_b .PdfIndirectObject :_bed [_dge .ObjectNumber ]=_cca ;case *_b .PdfObjectStream :_bed [_dge .ObjectNumber ]=_cca ;};};for _ ,_ge :=range _bed {_gbe (_ge ,_bed );};return _bed ,nil ;};func ReadPNG (file string )(_a .Image ,error ){_cb ,_ga :=_ce .Open (file );
if _ga !=nil {return nil ,_ga ;};defer _cb .Close ();return _ca .Decode (_cb );};func CopyFile (src ,dst string )error {_d ,_bc :=_ce .Open (src );if _bc !=nil {return _bc ;};defer _d .Close ();_cf ,_bc :=_ce .Create (dst );if _bc !=nil {return _bc ;};
defer _cf .Close ();_ ,_bc =_ec .Copy (_cf ,_d );return _bc ;};var (ErrRenderNotSupported =_ac .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 *_g .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_cac :=_fg .TrimSuffix (_c .Base (pdfPath ),_c .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_acg *_g .T ){_fef :=_c .Join (outputDir ,_cac );
_gb :=_fef +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _edda :=RenderPDFToPNGs (pdfPath ,0,_gb );_edda !=nil {_acg .Skip (_edda );};for _cff :=1;true ;_cff ++{_ecf :=_fgb .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_fef ,_cff );_fa :=_c .Join (baselineRenderPath ,_fgb .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_cac ,_cff ));
if _ ,_daa :=_ce .Stat (_ecf );_daa !=nil {break ;};_acg .Logf ("\u0025\u0073",_fa );if saveBaseline {_acg .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_ecf ,_fa );_gda :=CopyFile (_ecf ,_fa );if _gda !=nil {_acg .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_fa ,_gda );
};continue ;};_acg .Run (_fgb .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_cff ),func (_bef *_g .T ){_bef .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_ecf ,_fa );_cc ,_aea :=ComparePNGFiles (_ecf ,_fa );
if _ce .IsNotExist (_aea ){_bef .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_cc {_bef .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");
};});};});};func CompareDictionariesDeep (d1 ,d2 *_b .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_fe .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 ()));
_fe .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_efe :=range d1 .Keys (){if _efe =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;
};_feae :=_b .TraceToDirectObject (d1 .Get (_efe ));_deg :=_b .TraceToDirectObject (d2 .Get (_efe ));if _feae ==nil {_fe .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _deg ==nil {_fe .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");
return false ;};switch _dba :=_feae .(type ){case *_b .PdfObjectDictionary :_cfd ,_ad :=_deg .(*_b .PdfObjectDictionary );if !_ad {_fe .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_feae ,_deg );
return false ;};if !CompareDictionariesDeep (_dba ,_cfd ){return false ;};continue ;case *_b .PdfObjectArray :_ff ,_abf :=_deg .(*_b .PdfObjectArray );if !_abf {_fe .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");
return false ;};if _dba .Len ()!=_ff .Len (){_fe .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",_dba .Len (),_ff .Len ());
return false ;};for _feag :=0;_feag < _dba .Len ();_feag ++{_bf :=_b .TraceToDirectObject (_dba .Get (_feag ));_dgg :=_b .TraceToDirectObject (_ff .Get (_feag ));if _abe ,_dabc :=_bf .(*_b .PdfObjectDictionary );_dabc {_add ,_bad :=_dgg .(*_b .PdfObjectDictionary );
if !_bad {return false ;};if !CompareDictionariesDeep (_abe ,_add ){return false ;};}else {if _bf .WriteString ()!=_dgg .WriteString (){_fe .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_bf .WriteString (),_dgg .WriteString ());
return false ;};};};continue ;};if _feae .String ()!=_deg .String (){_fe .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",_efe ,_feae .String (),_deg .String ());
_fe .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_feae ,_deg );_fe .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_feae ,_deg );return false ;
};};return true ;};func _gbe (_aeb _b .PdfObject ,_dgc map[int64 ]_b .PdfObject )error {switch _afd :=_aeb .(type ){case *_b .PdfIndirectObject :_ab :=_afd ;_gbe (_ab .PdfObject ,_dgc );case *_b .PdfObjectDictionary :_dae :=_afd ;for _ ,_fd :=range _dae .Keys (){_fdg :=_dae .Get (_fd );
if _aba ,_gbd :=_fdg .(*_b .PdfObjectReference );_gbd {_fdf ,_gg :=_dgc [_aba .ObjectNumber ];if !_gg {return _ac .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
};_dae .Set (_fd ,_fdf );}else {_gbe (_fdg ,_dgc );};};case *_b .PdfObjectArray :_dee :=_afd ;for _dd ,_efa :=range _dee .Elements (){if _bce ,_eb :=_efa .(*_b .PdfObjectReference );_eb {_aeae ,_fea :=_dgc [_bce .ObjectNumber ];if !_fea {return _ac .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
};_dee .Set (_dd ,_aeae );}else {_gbe (_efa ,_dgc );};};};return nil ;};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_da :=_gc .LookPath ("\u0067\u0073");_da !=nil {return ErrRenderNotSupported ;};
return _gc .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_fgb .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_eca ,_db :=HashFile (file1 );
if _db !=nil {return false ,_db ;};_af ,_db :=HashFile (file2 );if _db !=nil {return false ,_db ;};if _eca ==_af {return true ,nil ;};_gd ,_db :=ReadPNG (file1 );if _db !=nil {return false ,_db ;};_cg ,_db :=ReadPNG (file2 );if _db !=nil {return false ,_db ;
};if _gd .Bounds ()!=_cg .Bounds (){return false ,nil ;};return CompareImages (_gd ,_cg );};func HashFile (file string )(string ,error ){_ef ,_caf :=_ce .Open (file );if _caf !=nil {return "",_caf ;};defer _ef .Close ();_ea :=_f .New ();if _ ,_caf =_ec .Copy (_ea ,_ef );
_caf !=nil {return "",_caf ;};return _aa .EncodeToString (_ea .Sum (nil )),nil ;};func CompareImages (img1 ,img2 _a .Image )(bool ,error ){_de :=img1 .Bounds ();_dg :=0;for _fed :=0;_fed < _de .Size ().X ;_fed ++{for _ba :=0;_ba < _de .Size ().Y ;_ba ++{_fc ,_ed ,_be ,_ :=img1 .At (_fed ,_ba ).RGBA ();
_edd ,_cd ,_bb ,_ :=img2 .At (_fed ,_ba ).RGBA ();if _fc !=_edd ||_ed !=_cd ||_be !=_bb {_dg ++;};};};_ae :=float64 (_dg )/float64 (_de .Dx ()*_de .Dy ());if _ae > 0.0001{_fgb .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_ae ,_dg );
return false ,nil ;};return true ,nil ;};