unipdf/internal/testutils/testutils.go
2024-09-14 00:10:30 +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 (_e "crypto/md5";_eae "encoding/hex";_a "errors";_ee "fmt";_ac "github.com/unidoc/unipdf/v3/common";_b "github.com/unidoc/unipdf/v3/core";_gg "image";_cg "image/png";_ge "io";_g "os";_cf "os/exec";_ec "path/filepath";_f "strings";
_ea "testing";);var (ErrRenderNotSupported =_a .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 *_ea .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_be :=_f .TrimSuffix (_ec .Base (pdfPath ),_ec .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_bdf *_ea .T ){_acfa :=_ec .Join (outputDir ,_be );
_cec :=_acfa +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _eg :=RenderPDFToPNGs (pdfPath ,0,_cec );_eg !=nil {_bdf .Skip (_eg );};for _egc :=1;true ;_egc ++{_ae :=_ee .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_acfa ,_egc );_bg :=_ec .Join (baselineRenderPath ,_ee .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_be ,_egc ));
if _ ,_ded :=_g .Stat (_ae );_ded !=nil {break ;};_bdf .Logf ("\u0025\u0073",_bg );if saveBaseline {_bdf .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_ae ,_bg );_ad :=CopyFile (_ae ,_bg );if _ad !=nil {_bdf .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_bg ,_ad );
};continue ;};_bdf .Run (_ee .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_egc ),func (_eff *_ea .T ){_eff .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_ae ,_bg );_df ,_deb :=ComparePNGFiles (_ae ,_bg );
if _g .IsNotExist (_deb ){_eff .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_df {_eff .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");
};});};});};func ParseIndirectObjects (rawpdf string )(map[int64 ]_b .PdfObject ,error ){_fg :=_b .NewParserFromString (rawpdf );_db :=map[int64 ]_b .PdfObject {};for {_bf ,_age :=_fg .ParseIndirectObject ();if _age !=nil {if _age ==_ge .EOF {break ;};
return nil ,_age ;};switch _ebf :=_bf .(type ){case *_b .PdfIndirectObject :_db [_ebf .ObjectNumber ]=_bf ;case *_b .PdfObjectStream :_db [_ebf .ObjectNumber ]=_bf ;};};for _ ,_ba :=range _db {_egb (_ba ,_db );};return _db ,nil ;};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;
};if _ ,_fa :=_cf .LookPath ("\u0067\u0073");_fa !=nil {return ErrRenderNotSupported ;};return _cf .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_ee .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();
};func CompareDictionariesDeep (d1 ,d2 *_b .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_ac .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 ()));
_ac .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_ege :=range d1 .Keys (){if _ege =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;
};_fc :=_b .TraceToDirectObject (d1 .Get (_ege ));_dee :=_b .TraceToDirectObject (d2 .Get (_ege ));if _fc ==nil {_ac .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _dee ==nil {_ac .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");
return false ;};switch _ga :=_fc .(type ){case *_b .PdfObjectDictionary :_efed ,_eaa :=_dee .(*_b .PdfObjectDictionary );if !_eaa {_ac .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_fc ,_dee );
return false ;};if !CompareDictionariesDeep (_ga ,_efed ){return false ;};continue ;case *_b .PdfObjectArray :_cfa ,_cddb :=_dee .(*_b .PdfObjectArray );if !_cddb {_ac .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");
return false ;};if _ga .Len ()!=_cfa .Len (){_ac .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",_ga .Len (),_cfa .Len ());
return false ;};for _eba :=0;_eba < _ga .Len ();_eba ++{_afb :=_b .TraceToDirectObject (_ga .Get (_eba ));_ff :=_b .TraceToDirectObject (_cfa .Get (_eba ));if _fac ,_afc :=_afb .(*_b .PdfObjectDictionary );_afc {_fcc ,_bb :=_ff .(*_b .PdfObjectDictionary );
if !_bb {return false ;};if !CompareDictionariesDeep (_fac ,_fcc ){return false ;};}else {if _afb .WriteString ()!=_ff .WriteString (){_ac .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_afb .WriteString (),_ff .WriteString ());
return false ;};};};continue ;};if _fc .String ()!=_dee .String (){_ac .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",_ege ,_fc .String (),_dee .String ());
_ac .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_fc ,_dee );_ac .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_fc ,_dee );return false ;
};};return true ;};func HashFile (file string )(string ,error ){_eb ,_gdc :=_g .Open (file );if _gdc !=nil {return "",_gdc ;};defer _eb .Close ();_ebb :=_e .New ();if _ ,_gdc =_ge .Copy (_ebb ,_eb );_gdc !=nil {return "",_gdc ;};return _eae .EncodeToString (_ebb .Sum (nil )),nil ;
};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_ag ,_caf :=HashFile (file1 );if _caf !=nil {return false ,_caf ;};_gfc ,_caf :=HashFile (file2 );if _caf !=nil {return false ,_caf ;};if _ag ==_gfc {return true ,nil ;};_acfg ,_caf :=ReadPNG (file1 );
if _caf !=nil {return false ,_caf ;};_bd ,_caf :=ReadPNG (file2 );if _caf !=nil {return false ,_caf ;};if _acfg .Bounds ()!=_bd .Bounds (){return false ,nil ;};return CompareImages (_acfg ,_bd );};func CopyFile (src ,dst string )error {_eaf ,_acg :=_g .Open (src );
if _acg !=nil {return _acg ;};defer _eaf .Close ();_d ,_acg :=_g .Create (dst );if _acg !=nil {return _acg ;};defer _d .Close ();_ ,_acg =_ge .Copy (_d ,_eaf );return _acg ;};func _egb (_af _b .PdfObject ,_ab map[int64 ]_b .PdfObject )error {switch _ced :=_af .(type ){case *_b .PdfIndirectObject :_gdg :=_ced ;
_egb (_gdg .PdfObject ,_ab );case *_b .PdfObjectDictionary :_efe :=_ced ;for _ ,_ed :=range _efe .Keys (){_dg :=_efe .Get (_ed );if _fab ,_bff :=_dg .(*_b .PdfObjectReference );_bff {_bed ,_dd :=_ab [_fab .ObjectNumber ];if !_dd {return _a .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
};_efe .Set (_ed ,_bed );}else {_egb (_dg ,_ab );};};case *_b .PdfObjectArray :_gdcf :=_ced ;for _gdd ,_edb :=range _gdcf .Elements (){if _bad ,_cafg :=_edb .(*_b .PdfObjectReference );_cafg {_fdb ,_dgd :=_ab [_bad .ObjectNumber ];if !_dgd {return _a .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
};_gdcf .Set (_gdd ,_fdb );}else {_egb (_edb ,_ab );};};};return nil ;};func ReadPNG (file string )(_gg .Image ,error ){_gd ,_gf :=_g .Open (file );if _gf !=nil {return nil ,_gf ;};defer _gd .Close ();return _cg .Decode (_gd );};func CompareImages (img1 ,img2 _gg .Image )(bool ,error ){_cd :=img1 .Bounds ();
_acf :=0;for _gfd :=0;_gfd < _cd .Size ().X ;_gfd ++{for _de :=0;_de < _cd .Size ().Y ;_de ++{_aa ,_fd ,_ce ,_ :=img1 .At (_gfd ,_de ).RGBA ();_gge ,_cc ,_eec ,_ :=img2 .At (_gfd ,_de ).RGBA ();if _aa !=_gge ||_fd !=_cc ||_ce !=_eec {_acf ++;};};};_eag :=float64 (_acf )/float64 (_cd .Dx ()*_cd .Dy ());
if _eag > 0.0001{_ee .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_eag ,_acf );return false ,nil ;};return true ,nil ;};