unipdf/internal/testutils/testutils.go
2020-10-05 19:28:24 +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";_gg "encoding/hex";_gc "errors";_gfd "fmt";_d "github.com/unidoc/unipdf/v3/common";_de "github.com/unidoc/unipdf/v3/core";_ge "image";_gf "image/png";_bg "io";_e "os";_b "os/exec";_cc "path/filepath";_a "strings";_f "testing";);func CompareImages (img1 ,img2 _ge .Image )(bool ,error ){_cg :=img1 .Bounds ();_bd :=0;for _dd :=0;_dd < _cg .Size ().X ;_dd ++{for _fd :=0;_fd < _cg .Size ().Y ;_fd ++{_aab ,_ggb ,_df ,_ :=img1 .At (_dd ,_fd ).RGBA ();_gcf ,_bdf ,_cb ,_ :=img2 .At (_dd ,_fd ).RGBA ();if _aab !=_gcf ||_ggb !=_bdf ||_df !=_cb {_bd ++;};};};_bf :=float64 (_bd )/float64 (_cg .Dx ()*_cg .Dy ());if _bf > 0.0001{_gfd .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_bf ,_bd );return false ,nil ;};return true ,nil ;};func HashFile (file string )(string ,error ){_af ,_acf :=_e .Open (file );if _acf !=nil {return "",_acf ;};defer _af .Close ();_ccf :=_g .New ();if _ ,_acf =_bg .Copy (_ccf ,_af );_acf !=nil {return "",_acf ;};return _gg .EncodeToString (_ccf .Sum (nil )),nil ;};func _gfc (_gfb _de .PdfObject ,_gd map[int64 ]_de .PdfObject )error {switch _ga :=_gfb .(type ){case *_de .PdfIndirectObject :_bda :=_ga ;_gfc (_bda .PdfObject ,_gd );case *_de .PdfObjectDictionary :_egc :=_ga ;for _ ,_fg :=range _egc .Keys (){_cga :=_egc .Get (_fg );if _cff ,_bbf :=_cga .(*_de .PdfObjectReference );_bbf {_bbd ,_gba :=_gd [_cff .ObjectNumber ];if !_gba {return _gc .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");};_egc .Set (_fg ,_bbd );}else {_gfc (_cga ,_gd );};};case *_de .PdfObjectArray :_dbf :=_ga ;for _add ,_bfb :=range _dbf .Elements (){if _fdc ,_bdg :=_bfb .(*_de .PdfObjectReference );_bdg {_bc ,_dea :=_gd [_fdc .ObjectNumber ];if !_dea {return _gc .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");};_dbf .Set (_add ,_bc );}else {_gfc (_bfb ,_gd );};};};return nil ;};var (ErrRenderNotSupported =_gc .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 RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_cf :=_b .LookPath ("\u0067\u0073");_cf !=nil {return ErrRenderNotSupported ;};return _b .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_gfd .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_bde ,_fa :=HashFile (file1 );if _fa !=nil {return false ,_fa ;};_eg ,_fa :=HashFile (file2 );if _fa !=nil {return false ,_fa ;};if _bde ==_eg {return true ,nil ;};_ff ,_fa :=ReadPNG (file1 );if _fa !=nil {return false ,_fa ;};_fe ,_fa :=ReadPNG (file2 );if _fa !=nil {return false ,_fa ;};if _ff .Bounds ()!=_fe .Bounds (){return false ,nil ;};return CompareImages (_ff ,_fe );};func RunRenderTest (t *_f .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_ec :=_a .TrimSuffix (_cc .Base (pdfPath ),_cc .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_ca *_f .T ){_fdg :=_cc .Join (outputDir ,_ec );_cad :=_fdg +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _dg :=RenderPDFToPNGs (pdfPath ,0,_cad );_dg !=nil {_ca .Skip (_dg );};for _fad :=1;true ;_fad ++{_faf :=_gfd .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_fdg ,_fad );_def :=_cc .Join (baselineRenderPath ,_gfd .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_ec ,_fad ));if _ ,_dc :=_e .Stat (_faf );_dc !=nil {break ;};_ca .Logf ("\u0025\u0073",_def );if _ ,_gb :=_e .Stat (_def );_e .IsNotExist (_gb ){if saveBaseline {_ca .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_faf ,_def );CopyFile (_faf ,_def );continue ;};break ;};_ca .Run (_gfd .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_fad ),func (_caa *_f .T ){_caa .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_faf ,_def );_gfa ,_da :=ComparePNGFiles (_faf ,_def );if _e .IsNotExist (_da ){_caa .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_gfa {_caa .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");};});};});};func CompareDictionariesDeep (d1 ,d2 *_de .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_d .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 ()));_d .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_ddd :=range d1 .Keys (){if _ddd =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;};_fff :=_de .TraceToDirectObject (d1 .Get (_ddd ));_fag :=_de .TraceToDirectObject (d2 .Get (_ddd ));if _fff ==nil {_d .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _fag ==nil {_d .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};switch _gca :=_fff .(type ){case *_de .PdfObjectDictionary :_dgc ,_bcg :=_fag .(*_de .PdfObjectDictionary );if !_bcg {_d .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_fff ,_fag );return false ;};if !CompareDictionariesDeep (_gca ,_dgc ){return false ;};continue ;case *_de .PdfObjectArray :_cffd ,_edb :=_fag .(*_de .PdfObjectArray );if !_edb {_d .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");return false ;};if _gca .Len ()!=_cffd .Len (){_d .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",_gca .Len (),_cffd .Len ());return false ;};for _bgf :=0;_bgf < _gca .Len ();_bgf ++{_dgf :=_de .TraceToDirectObject (_gca .Get (_bgf ));_gae :=_de .TraceToDirectObject (_cffd .Get (_bgf ));if _efe ,_ggd :=_dgf .(*_de .PdfObjectDictionary );_ggd {_gcfd ,_fed :=_gae .(*_de .PdfObjectDictionary );if !_fed {return false ;};if !CompareDictionariesDeep (_efe ,_gcfd ){return false ;};}else {if _dgf .WriteString ()!=_gae .WriteString (){_d .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_dgf .WriteString (),_gae .WriteString ());return false ;};};};continue ;};if _fff .String ()!=_fag .String (){_d .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",_ddd ,_fff .String (),_fag .String ());_d .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_fff ,_fag );_d .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_fff ,_fag );return false ;};};return true ;};func CopyFile (src ,dst string )error {_bgc ,_deg :=_e .Open (src );if _deg !=nil {return _deg ;};defer _bgc .Close ();_aa ,_deg :=_e .Create (dst );if _deg !=nil {return _deg ;};defer _aa .Close ();_ ,_deg =_bg .Copy (_aa ,_bgc );return _deg ;};func ReadPNG (file string )(_ge .Image ,error ){_ac ,_acb :=_e .Open (file );if _acb !=nil {return nil ,_acb ;};defer _ac .Close ();return _gf .Decode (_ac );};func ParseIndirectObjects (rawpdf string )(map[int64 ]_de .PdfObject ,error ){_ccfa :=_de .NewParserFromString (rawpdf );_db :=map[int64 ]_de .PdfObject {};for {_ffc ,_cd :=_ccfa .ParseIndirectObject ();if _cd !=nil {if _cd ==_bg .EOF {break ;};return nil ,_cd ;};switch _fb :=_ffc .(type ){case *_de .PdfIndirectObject :_db [_fb .ObjectNumber ]=_ffc ;case *_de .PdfObjectStream :_db [_fb .ObjectNumber ]=_ffc ;};};for _ ,_edg :=range _db {_gfc (_edg ,_db );};return _db ,nil ;};