2020-08-27 21:45:09 +00:00
|
|
|
//
|
|
|
|
// 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/
|
|
|
|
|
2024-07-25 19:53:55 +00:00
|
|
|
package testutils ;import (_f "crypto/md5";_fb "encoding/hex";_b "errors";_fe "fmt";_ab "github.com/unidoc/unipdf/v3/common";_ad "github.com/unidoc/unipdf/v3/core";_db "image";_ec "image/png";_d "io";_af "os";_fg "os/exec";_e "path/filepath";_c "strings";
|
|
|
|
_fd "testing";);func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_da :=_fg .LookPath ("\u0067\u0073");_da !=nil {return ErrRenderNotSupported ;};return _fg .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_fe .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();
|
|
|
|
};func HashFile (file string )(string ,error ){_ga ,_fa :=_af .Open (file );if _fa !=nil {return "",_fa ;};defer _ga .Close ();_gc :=_f .New ();if _ ,_fa =_d .Copy (_gc ,_ga );_fa !=nil {return "",_fa ;};return _fb .EncodeToString (_gc .Sum (nil )),nil ;
|
|
|
|
};func CopyFile (src ,dst string )error {_add ,_dd :=_af .Open (src );if _dd !=nil {return _dd ;};defer _add .Close ();_g ,_dd :=_af .Create (dst );if _dd !=nil {return _dd ;};defer _g .Close ();_ ,_dd =_d .Copy (_g ,_add );return _dd ;};func RunRenderTest (t *_fd .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_cg :=_c .TrimSuffix (_e .Base (pdfPath ),_e .Ext (pdfPath ));
|
|
|
|
t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_bb *_fd .T ){_de :=_e .Join (outputDir ,_cg );_dc :=_de +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _afd :=RenderPDFToPNGs (pdfPath ,0,_dc );_afd !=nil {_bb .Skip (_afd );};for _df :=1;true ;_df ++{_adcg :=_fe .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_de ,_df );
|
|
|
|
_dfd :=_e .Join (baselineRenderPath ,_fe .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_cg ,_df ));if _ ,_ade :=_af .Stat (_adcg );_ade !=nil {break ;};_bb .Logf ("\u0025\u0073",_dfd );if saveBaseline {_bb .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_adcg ,_dfd );
|
|
|
|
_afb :=CopyFile (_adcg ,_dfd );if _afb !=nil {_bb .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_dfd ,_afb );};continue ;};_bb .Run (_fe .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_df ),func (_deb *_fd .T ){_deb .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_adcg ,_dfd );
|
|
|
|
_ffc ,_gca :=ComparePNGFiles (_adcg ,_dfd );if _af .IsNotExist (_gca ){_deb .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_ffc {_deb .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");
|
|
|
|
};});};});};var (ErrRenderNotSupported =_b .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 *_ad .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_ab .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 ()));
|
|
|
|
_ab .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_ecg :=range d1 .Keys (){if _ecg =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;
|
|
|
|
};_gcd :=_ad .TraceToDirectObject (d1 .Get (_ecg ));_geg :=_ad .TraceToDirectObject (d2 .Get (_ecg ));if _gcd ==nil {_ab .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _geg ==nil {_ab .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");
|
|
|
|
return false ;};switch _dfg :=_gcd .(type ){case *_ad .PdfObjectDictionary :_dea ,_edf :=_geg .(*_ad .PdfObjectDictionary );if !_edf {_ab .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_gcd ,_geg );
|
|
|
|
return false ;};if !CompareDictionariesDeep (_dfg ,_dea ){return false ;};continue ;case *_ad .PdfObjectArray :_ef ,_efe :=_geg .(*_ad .PdfObjectArray );if !_efe {_ab .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");
|
|
|
|
return false ;};if _dfg .Len ()!=_ef .Len (){_ab .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",_dfg .Len (),_ef .Len ());
|
|
|
|
return false ;};for _dbc :=0;_dbc < _dfg .Len ();_dbc ++{_gbc :=_ad .TraceToDirectObject (_dfg .Get (_dbc ));_cgd :=_ad .TraceToDirectObject (_ef .Get (_dbc ));if _bee ,_ea :=_gbc .(*_ad .PdfObjectDictionary );_ea {_ebgg ,_aec :=_cgd .(*_ad .PdfObjectDictionary );
|
|
|
|
if !_aec {return false ;};if !CompareDictionariesDeep (_bee ,_ebgg ){return false ;};}else {if _gbc .WriteString ()!=_cgd .WriteString (){_ab .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_gbc .WriteString (),_cgd .WriteString ());
|
|
|
|
return false ;};};};continue ;};if _gcd .String ()!=_geg .String (){_ab .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",_ecg ,_gcd .String (),_geg .String ());
|
|
|
|
_ab .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_gcd ,_geg );_ab .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_gcd ,_geg );return false ;
|
|
|
|
};};return true ;};func ParseIndirectObjects (rawpdf string )(map[int64 ]_ad .PdfObject ,error ){_dbf :=_ad .NewParserFromString (rawpdf );_aba :=map[int64 ]_ad .PdfObject {};for {_afe ,_daf :=_dbf .ParseIndirectObject ();if _daf !=nil {if _daf ==_d .EOF {break ;
|
|
|
|
};return nil ,_daf ;};switch _ag :=_afe .(type ){case *_ad .PdfIndirectObject :_aba [_ag .ObjectNumber ]=_afe ;case *_ad .PdfObjectStream :_aba [_ag .ObjectNumber ]=_afe ;};};for _ ,_cbe :=range _aba {_gb (_cbe ,_aba );};return _aba ,nil ;};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_gcg ,_gab :=HashFile (file1 );
|
|
|
|
if _gab !=nil {return false ,_gab ;};_fgb ,_gab :=HashFile (file2 );if _gab !=nil {return false ,_gab ;};if _gcg ==_fgb {return true ,nil ;};_gag ,_gab :=ReadPNG (file1 );if _gab !=nil {return false ,_gab ;};_cb ,_gab :=ReadPNG (file2 );if _gab !=nil {return false ,_gab ;
|
|
|
|
};if _gag .Bounds ()!=_cb .Bounds (){return false ,nil ;};return CompareImages (_gag ,_cb );};func CompareImages (img1 ,img2 _db .Image )(bool ,error ){_eb :=img1 .Bounds ();_be :=0;for _ae :=0;_ae < _eb .Size ().X ;_ae ++{for _gcb :=0;_gcb < _eb .Size ().Y ;
|
|
|
|
_gcb ++{_ee ,_ce ,_ddf ,_ :=img1 .At (_ae ,_gcb ).RGBA ();_cee ,_edb ,_cc ,_ :=img2 .At (_ae ,_gcb ).RGBA ();if _ee !=_cee ||_ce !=_edb ||_ddf !=_cc {_be ++;};};};_adc :=float64 (_be )/float64 (_eb .Dx ()*_eb .Dy ());if _adc > 0.0001{_fe .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_adc ,_be );
|
|
|
|
return false ,nil ;};return true ,nil ;};func ReadPNG (file string )(_db .Image ,error ){_bd ,_ed :=_af .Open (file );if _ed !=nil {return nil ,_ed ;};defer _bd .Close ();return _ec .Decode (_bd );};func _gb (_gd _ad .PdfObject ,_fbg map[int64 ]_ad .PdfObject )error {switch _fbd :=_gd .(type ){case *_ad .PdfIndirectObject :_gdd :=_fbd ;
|
|
|
|
_gb (_gdd .PdfObject ,_fbg );case *_ad .PdfObjectDictionary :_aeb :=_fbd ;for _ ,_gaa :=range _aeb .Keys (){_ddc :=_aeb .Get (_gaa );if _ddg ,_bbb :=_ddc .(*_ad .PdfObjectReference );_bbb {_ebg ,_dbd :=_fbg [_ddg .ObjectNumber ];if !_dbd {return _b .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
|
|
|
|
};_aeb .Set (_gaa ,_ebg );}else {_gb (_ddc ,_fbg );};};case *_ad .PdfObjectArray :_aeg :=_fbd ;for _ffe ,_edbg :=range _aeg .Elements (){if _dba ,_fce :=_edbg .(*_ad .PdfObjectReference );_fce {_gfc ,_cbc :=_fbg [_dba .ObjectNumber ];if !_cbc {return _b .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
|
|
|
|
};_aeg .Set (_ffe ,_gfc );}else {_gb (_edbg ,_fbg );};};};return nil ;};
|