mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-27 13:48:51 +08:00
41 lines
8.8 KiB
Go
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 (_e "crypto/md5";_ab "encoding/hex";_a "errors";_cd "fmt";_cg "github.com/unidoc/unipdf/v3/common";_ce "github.com/unidoc/unipdf/v3/core";_ge "image";_cb "image/png";_d "io";_g "os";_fa "os/exec";_c "path/filepath";_fb "strings";
|
|
_ef "testing";);func CompareImages (img1 ,img2 _ge .Image )(bool ,error ){_da :=img1 .Bounds ();_eg :=0;for _b :=0;_b < _da .Size ().X ;_b ++{for _cf :=0;_cf < _da .Size ().Y ;_cf ++{_ga ,_aeb ,_fg ,_ :=img1 .At (_b ,_cf ).RGBA ();_fd ,_eac ,_ada ,_ :=img2 .At (_b ,_cf ).RGBA ();
|
|
if _ga !=_fd ||_aeb !=_eac ||_fg !=_ada {_eg ++;};};};_efg :=float64 (_eg )/float64 (_da .Dx ()*_da .Dy ());if _efg > 0.0001{_cd .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_efg ,_eg );
|
|
return false ,nil ;};return true ,nil ;};func CopyFile (src ,dst string )error {_ec ,_ca :=_g .Open (src );if _ca !=nil {return _ca ;};defer _ec .Close ();_cba ,_ca :=_g .Create (dst );if _ca !=nil {return _ca ;};defer _cba .Close ();_ ,_ca =_d .Copy (_cba ,_ec );
|
|
return _ca ;};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 ReadPNG (file string )(_ge .Image ,error ){_dg ,_dc :=_g .Open (file );if _dc !=nil {return nil ,_dc ;};defer _dg .Close ();return _cb .Decode (_dg );};func ParseIndirectObjects (rawpdf string )(map[int64 ]_ce .PdfObject ,error ){_cfa :=_ce .NewParserFromString (rawpdf );
|
|
_eeb :=map[int64 ]_ce .PdfObject {};for {_cc ,_eda :=_cfa .ParseIndirectObject ();if _eda !=nil {if _eda ==_d .EOF {break ;};return nil ,_eda ;};switch _dea :=_cc .(type ){case *_ce .PdfIndirectObject :_eeb [_dea .ObjectNumber ]=_cc ;case *_ce .PdfObjectStream :_eeb [_dea .ObjectNumber ]=_cc ;
|
|
};};for _ ,_gc :=range _eeb {_edb (_gc ,_eeb );};return _eeb ,nil ;};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_ee ,_adb :=HashFile (file1 );if _adb !=nil {return false ,_adb ;};_cae ,_adb :=HashFile (file2 );if _adb !=nil {return false ,_adb ;
|
|
};if _ee ==_cae {return true ,nil ;};_fe ,_adb :=ReadPNG (file1 );if _adb !=nil {return false ,_adb ;};_ag ,_adb :=ReadPNG (file2 );if _adb !=nil {return false ,_adb ;};if _fe .Bounds ()!=_ag .Bounds (){return false ,nil ;};return CompareImages (_fe ,_ag );
|
|
};func _edb (_dge _ce .PdfObject ,_gca map[int64 ]_ce .PdfObject )error {switch _gfc :=_dge .(type ){case *_ce .PdfIndirectObject :_ceb :=_gfc ;_edb (_ceb .PdfObject ,_gca );case *_ce .PdfObjectDictionary :_eeg :=_gfc ;for _ ,_dbf :=range _eeg .Keys (){_bd :=_eeg .Get (_dbf );
|
|
if _gg ,_efeg :=_bd .(*_ce .PdfObjectReference );_efeg {_dfa ,_fec :=_gca [_gg .ObjectNumber ];if !_fec {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");
|
|
};_eeg .Set (_dbf ,_dfa );}else {_edb (_bd ,_gca );};};case *_ce .PdfObjectArray :_egf :=_gfc ;for _bg ,_cge :=range _egf .Elements (){if _adg ,_geb :=_cge .(*_ce .PdfObjectReference );_geb {_def ,_fdb :=_gca [_adg .ObjectNumber ];if !_fdb {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");
|
|
};_egf .Set (_bg ,_def );}else {_edb (_cge ,_gca );};};};return nil ;};func CompareDictionariesDeep (d1 ,d2 *_ce .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_cg .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 ()));
|
|
_cg .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_gd :=range d1 .Keys (){if _gd =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;};
|
|
_fdbe :=_ce .TraceToDirectObject (d1 .Get (_gd ));_fbg :=_ce .TraceToDirectObject (d2 .Get (_gd ));if _fdbe ==nil {_cg .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _fbg ==nil {_cg .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");
|
|
return false ;};switch _gec :=_fdbe .(type ){case *_ce .PdfObjectDictionary :_bf ,_ega :=_fbg .(*_ce .PdfObjectDictionary );if !_ega {_cg .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_fdbe ,_fbg );
|
|
return false ;};if !CompareDictionariesDeep (_gec ,_bf ){return false ;};continue ;case *_ce .PdfObjectArray :_fae ,_eaa :=_fbg .(*_ce .PdfObjectArray );if !_eaa {_cg .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");
|
|
return false ;};if _gec .Len ()!=_fae .Len (){_cg .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",_gec .Len (),_fae .Len ());
|
|
return false ;};for _ff :=0;_ff < _gec .Len ();_ff ++{_ffe :=_ce .TraceToDirectObject (_gec .Get (_ff ));_gdf :=_ce .TraceToDirectObject (_fae .Get (_ff ));if _ade ,_dfd :=_ffe .(*_ce .PdfObjectDictionary );_dfd {_bb ,_gce :=_gdf .(*_ce .PdfObjectDictionary );
|
|
if !_gce {return false ;};if !CompareDictionariesDeep (_ade ,_bb ){return false ;};}else {if _ffe .WriteString ()!=_gdf .WriteString (){_cg .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_ffe .WriteString (),_gdf .WriteString ());
|
|
return false ;};};};continue ;};if _fdbe .String ()!=_fbg .String (){_cg .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",_gd ,_fdbe .String (),_fbg .String ());
|
|
_cg .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_fdbe ,_fbg );_cg .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_fdbe ,_fbg );return false ;
|
|
};};return true ;};func HashFile (file string )(string ,error ){_fbc ,_ea :=_g .Open (file );if _ea !=nil {return "",_ea ;};defer _fbc .Close ();_ae :=_e .New ();if _ ,_ea =_d .Copy (_ae ,_fbc );_ea !=nil {return "",_ea ;};return _ab .EncodeToString (_ae .Sum (nil )),nil ;
|
|
};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_agb :=_fa .LookPath ("\u0067\u0073");_agb !=nil {return ErrRenderNotSupported ;};return _fa .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_cd .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();
|
|
};func RunRenderTest (t *_ef .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_dcb :=_fb .TrimSuffix (_c .Base (pdfPath ),_c .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_db *_ef .T ){_gb :=_c .Join (outputDir ,_dcb );
|
|
_cbaa :=_gb +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _fc :=RenderPDFToPNGs (pdfPath ,0,_cbaa );_fc !=nil {_db .Skip (_fc );};for _dgc :=1;true ;_dgc ++{_daf :=_cd .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_gb ,_dgc );_ecf :=_c .Join (baselineRenderPath ,_cd .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_dcb ,_dgc ));
|
|
if _ ,_efe :=_g .Stat (_daf );_efe !=nil {break ;};_db .Logf ("\u0025\u0073",_ecf );if saveBaseline {_db .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_daf ,_ecf );_ceg :=CopyFile (_daf ,_ecf );if _ceg !=nil {_db .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_ecf ,_ceg );
|
|
};continue ;};_db .Run (_cd .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_dgc ),func (_dcd *_ef .T ){_dcd .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_daf ,_ecf );_ed ,_de :=ComparePNGFiles (_daf ,_ecf );
|
|
if _g .IsNotExist (_de ){_dcd .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_ed {_dcd .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");
|
|
};});};});}; |