unipdf/internal/testutils/testutils.go
2021-01-26 01:31:56 +00:00

12 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 (_aa "crypto/md5";_ef "encoding/hex";_bfc "errors";_bf "fmt";_bd "github.com/unidoc/unipdf/v3/common";_g "github.com/unidoc/unipdf/v3/core";_d "image";_fa "image/png";_ag "io";_ae "os";_e "os/exec";_b "path/filepath";_f "strings";_c "testing";);func CompareDictionariesDeep (d1 ,d2 *_g .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_bd .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 ()));_bd .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_dgg :=range d1 .Keys (){if _dgg =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;};_fda :=_g .TraceToDirectObject (d1 .Get (_dgg ));_acb :=_g .TraceToDirectObject (d2 .Get (_dgg ));if _fda ==nil {_bd .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _acb ==nil {_bd .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};switch _daa :=_fda .(type ){case *_g .PdfObjectDictionary :_cba ,_edge :=_acb .(*_g .PdfObjectDictionary );if !_edge {_bd .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_fda ,_acb );return false ;};if !CompareDictionariesDeep (_daa ,_cba ){return false ;};continue ;case *_g .PdfObjectArray :_dbf ,_bae :=_acb .(*_g .PdfObjectArray );if !_bae {_bd .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");return false ;};if _daa .Len ()!=_dbf .Len (){_bd .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",_daa .Len (),_dbf .Len ());return false ;};for _dca :=0;_dca < _daa .Len ();_dca ++{_bgc :=_g .TraceToDirectObject (_daa .Get (_dca ));_aba :=_g .TraceToDirectObject (_dbf .Get (_dca ));if _aed ,_ebc :=_bgc .(*_g .PdfObjectDictionary );_ebc {_dced ,_eed :=_aba .(*_g .PdfObjectDictionary );if !_eed {return false ;};if !CompareDictionariesDeep (_aed ,_dced ){return false ;};}else {if _bgc .WriteString ()!=_aba .WriteString (){_bd .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_bgc .WriteString (),_aba .WriteString ());return false ;};};};continue ;};if _fda .String ()!=_acb .String (){_bd .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",_dgg ,_fda .String (),_acb .String ());_bd .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_fda ,_acb );_bd .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_fda ,_acb );return false ;};};return true ;};func RunRenderTest (t *_c .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_eg :=_f .TrimSuffix (_b .Base (pdfPath ),_b .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_adf *_c .T ){_fcd :=_b .Join (outputDir ,_eg );_gcd :=_fcd +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _cac :=RenderPDFToPNGs (pdfPath ,0,_gcd );_cac !=nil {_adf .Skip (_cac );};for _ga :=1;true ;_ga ++{_gef :=_bf .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_fcd ,_ga );_egf :=_b .Join (baselineRenderPath ,_bf .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_eg ,_ga ));if _ ,_bc :=_ae .Stat (_gef );_bc !=nil {break ;};_adf .Logf ("\u0025\u0073",_egf );if saveBaseline {_adf .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_gef ,_egf );_eac :=CopyFile (_gef ,_egf );if _eac !=nil {_adf .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_egf ,_eac );};continue ;};_adf .Run (_bf .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_ga ),func (_aff *_c .T ){_aff .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_gef ,_egf );_fd ,_cce :=ComparePNGFiles (_gef ,_egf );if _ae .IsNotExist (_cce ){_aff .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_fd {_aff .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");};});};});};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_eb ,_aag :=HashFile (file1 );if _aag !=nil {return false ,_aag ;};_eba ,_aag :=HashFile (file2 );if _aag !=nil {return false ,_aag ;};if _eb ==_eba {return true ,nil ;};_bdd ,_aag :=ReadPNG (file1 );if _aag !=nil {return false ,_aag ;};_bff ,_aag :=ReadPNG (file2 );if _aag !=nil {return false ,_aag ;};if _bdd .Bounds ()!=_bff .Bounds (){return false ,nil ;};return CompareImages (_bdd ,_bff );};func CopyFile (src ,dst string )error {_gc ,_aab :=_ae .Open (src );if _aab !=nil {return _aab ;};defer _gc .Close ();_af ,_aab :=_ae .Create (dst );if _aab !=nil {return _aab ;};defer _af .Close ();_ ,_aab =_ag .Copy (_af ,_gc );return _aab ;};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_de :=_e .LookPath ("\u0067\u0073");_de !=nil {return ErrRenderNotSupported ;};return _e .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_bf .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();};func HashFile (file string )(string ,error ){_fe ,_cb :=_ae .Open (file );if _cb !=nil {return "",_cb ;};defer _fe .Close ();_cd :=_aa .New ();if _ ,_cb =_ag .Copy (_cd ,_fe );_cb !=nil {return "",_cb ;};return _ef .EncodeToString (_cd .Sum (nil )),nil ;};func ReadPNG (file string )(_d .Image ,error ){_dc ,_gd :=_ae .Open (file );if _gd !=nil {return nil ,_gd ;};defer _dc .Close ();return _fa .Decode (_dc );};func ParseIndirectObjects (rawpdf string )(map[int64 ]_g .PdfObject ,error ){_gcb :=_g .NewParserFromString (rawpdf );_gdc :=map[int64 ]_g .PdfObject {};for {_edg ,_db :=_gcb .ParseIndirectObject ();if _db !=nil {if _db ==_ag .EOF {break ;};return nil ,_db ;};switch _gdce :=_edg .(type ){case *_g .PdfIndirectObject :_gdc [_gdce .ObjectNumber ]=_edg ;case *_g .PdfObjectStream :_gdc [_gdce .ObjectNumber ]=_edg ;};};for _ ,_ee :=range _gdc {_df (_ee ,_gdc );};return _gdc ,nil ;};func _df (_dce _g .PdfObject ,_dg map[int64 ]_g .PdfObject )error {switch _ada :=_dce .(type ){case *_g .PdfIndirectObject :_bg :=_ada ;_df (_bg .PdfObject ,_dg );case *_g .PdfObjectDictionary :_dfd :=_ada ;for _ ,_fb :=range _dfd .Keys (){_cbg :=_dfd .Get (_fb );if _eeb ,_ccf :=_cbg .(*_g .PdfObjectReference );_ccf {_bb ,_gf :=_dg [_eeb .ObjectNumber ];if !_gf {return _bfc .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");};_dfd .Set (_fb ,_bb );}else {_df (_cbg ,_dg );};};case *_g .PdfObjectArray :_ebd :=_ada ;for _dfb ,_cf :=range _ebd .Elements (){if _ab ,_deg :=_cf .(*_g .PdfObjectReference );_deg {_dba ,_cfc :=_dg [_ab .ObjectNumber ];if !_cfc {return _bfc .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");};_ebd .Set (_dfb ,_dba );}else {_df (_cf ,_dg );};};};return nil ;};var (ErrRenderNotSupported =_bfc .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 CompareImages (img1 ,img2 _d .Image )(bool ,error ){_cc :=img1 .Bounds ();_ge :=0;for _ed :=0;_ed < _cc .Size ().X ;_ed ++{for _fc :=0;_fc < _cc .Size ().Y ;_fc ++{_agc ,_cg ,_cdd ,_ :=img1 .At (_ed ,_fc ).RGBA ();_dad ,_fg ,_dd ,_ :=img2 .At (_ed ,_fc ).RGBA ();if _agc !=_dad ||_cg !=_fg ||_cdd !=_dd {_ge ++;};};};_ac :=float64 (_ge )/float64 (_cc .Dx ()*_cc .Dy ());if _ac > 0.0001{_bf .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_ac ,_ge );return false ,nil ;};return true ,nil ;};