mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-27 13:48:51 +08:00
40 lines
8.9 KiB
Go
40 lines
8.9 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 (_f "crypto/md5";_ee "encoding/hex";_af "errors";_fbc "fmt";_de "github.com/unidoc/unipdf/v3/common";_afc "github.com/unidoc/unipdf/v3/core";_b "image";_g "image/png";_ed "io";_fb "os";_fg "os/exec";_e "path/filepath";_fe "strings";
|
|
_d "testing";);func ParseIndirectObjects (rawpdf string )(map[int64 ]_afc .PdfObject ,error ){_eaf :=_afc .NewParserFromString (rawpdf );_gge :=map[int64 ]_afc .PdfObject {};for {_fge ,_cf :=_eaf .ParseIndirectObject ();if _cf !=nil {if _cf ==_ed .EOF {break ;
|
|
};return nil ,_cf ;};switch _ceaf :=_fge .(type ){case *_afc .PdfIndirectObject :_gge [_ceaf .ObjectNumber ]=_fge ;case *_afc .PdfObjectStream :_gge [_ceaf .ObjectNumber ]=_fge ;};};for _ ,_eff :=range _gge {_gec (_eff ,_gge );};return _gge ,nil ;};func CompareImages (img1 ,img2 _b .Image )(bool ,error ){_ce :=img1 .Bounds ();
|
|
_ae :=0;for _gca :=0;_gca < _ce .Size ().X ;_gca ++{for _aee :=0;_aee < _ce .Size ().Y ;_aee ++{_da ,_cd ,_cda ,_ :=img1 .At (_gca ,_aee ).RGBA ();_adb ,_afa ,_db ,_ :=img2 .At (_gca ,_aee ).RGBA ();if _da !=_adb ||_cd !=_afa ||_cda !=_db {_ae ++;};};};
|
|
_ea :=float64 (_ae )/float64 (_ce .Dx ()*_ce .Dy ());if _ea > 0.0001{_fbc .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_ea ,_ae );return false ,nil ;};return true ,nil ;
|
|
};func CompareDictionariesDeep (d1 ,d2 *_afc .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_de .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 ()));
|
|
_de .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_efb :=range d1 .Keys (){if _efb =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;
|
|
};_fc :=_afc .TraceToDirectObject (d1 .Get (_efb ));_aa :=_afc .TraceToDirectObject (d2 .Get (_efb ));if _fc ==nil {_de .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _aa ==nil {_de .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");
|
|
return false ;};switch _fgc :=_fc .(type ){case *_afc .PdfObjectDictionary :_gcb ,_ade :=_aa .(*_afc .PdfObjectDictionary );if !_ade {_de .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_fc ,_aa );
|
|
return false ;};if !CompareDictionariesDeep (_fgc ,_gcb ){return false ;};continue ;case *_afc .PdfObjectArray :_eecb ,_bdf :=_aa .(*_afc .PdfObjectArray );if !_bdf {_de .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");
|
|
return false ;};if _fgc .Len ()!=_eecb .Len (){_de .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",_fgc .Len (),_eecb .Len ());
|
|
return false ;};for _dbc :=0;_dbc < _fgc .Len ();_dbc ++{_fa :=_afc .TraceToDirectObject (_fgc .Get (_dbc ));_eg :=_afc .TraceToDirectObject (_eecb .Get (_dbc ));if _dcc ,_cef :=_fa .(*_afc .PdfObjectDictionary );_cef {_cdb ,_eeb :=_eg .(*_afc .PdfObjectDictionary );
|
|
if !_eeb {return false ;};if !CompareDictionariesDeep (_dcc ,_cdb ){return false ;};}else {if _fa .WriteString ()!=_eg .WriteString (){_de .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_fa .WriteString (),_eg .WriteString ());
|
|
return false ;};};};continue ;};if _fc .String ()!=_aa .String (){_de .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",_efb ,_fc .String (),_aa .String ());
|
|
_de .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_fc ,_aa );_de .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_fc ,_aa );return false ;
|
|
};};return true ;};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_gg :=_fg .LookPath ("\u0067\u0073");_gg !=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 ,_fbc .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();
|
|
};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_ef ,_ba :=HashFile (file1 );if _ba !=nil {return false ,_ba ;};_ddf ,_ba :=HashFile (file2 );if _ba !=nil {return false ,_ba ;};if _ef ==_ddf {return true ,nil ;};_afae ,_ba :=ReadPNG (file1 );
|
|
if _ba !=nil {return false ,_ba ;};_dda ,_ba :=ReadPNG (file2 );if _ba !=nil {return false ,_ba ;};if _afae .Bounds ()!=_dda .Bounds (){return false ,nil ;};return CompareImages (_afae ,_dda );};func _gec (_df _afc .PdfObject ,_eec map[int64 ]_afc .PdfObject )error {switch _ag :=_df .(type ){case *_afc .PdfIndirectObject :_aed :=_ag ;
|
|
_gec (_aed .PdfObject ,_eec );case *_afc .PdfObjectDictionary :_cfe :=_ag ;for _ ,_ab :=range _cfe .Keys (){_dg :=_cfe .Get (_ab );if _cdd ,_ggd :=_dg .(*_afc .PdfObjectReference );_ggd {_bg ,_ggf :=_eec [_cdd .ObjectNumber ];if !_ggf {return _af .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
|
|
};_cfe .Set (_ab ,_bg );}else {_gec (_dg ,_eec );};};case *_afc .PdfObjectArray :_edg :=_ag ;for _ac ,_ccfc :=range _edg .Elements (){if _bcdd ,_edd :=_ccfc .(*_afc .PdfObjectReference );_edd {_bge ,_abe :=_eec [_bcdd .ObjectNumber ];if !_abe {return _af .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
|
|
};_edg .Set (_ac ,_bge );}else {_gec (_ccfc ,_eec );};};};return nil ;};func CopyFile (src ,dst string )error {_ge ,_dd :=_fb .Open (src );if _dd !=nil {return _dd ;};defer _ge .Close ();_bd ,_dd :=_fb .Create (dst );if _dd !=nil {return _dd ;};defer _bd .Close ();
|
|
_ ,_dd =_ed .Copy (_bd ,_ge );return _dd ;};func HashFile (file string )(string ,error ){_cc ,_gc :=_fb .Open (file );if _gc !=nil {return "",_gc ;};defer _cc .Close ();_ad :=_f .New ();if _ ,_gc =_ed .Copy (_ad ,_cc );_gc !=nil {return "",_gc ;};return _ee .EncodeToString (_ad .Sum (nil )),nil ;
|
|
};func ReadPNG (file string )(_b .Image ,error ){_c ,_bc :=_fb .Open (file );if _bc !=nil {return nil ,_bc ;};defer _c .Close ();return _g .Decode (_c );};func RunRenderTest (t *_d .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_bda :=_fe .TrimSuffix (_e .Base (pdfPath ),_e .Ext (pdfPath ));
|
|
t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_bcd *_d .T ){_fd :=_e .Join (outputDir ,_bda );_adf :=_fd +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _aff :=RenderPDFToPNGs (pdfPath ,0,_adf );_aff !=nil {_bcd .Skip (_aff );};for _fff :=1;true ;_fff ++{_dc :=_fbc .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_fd ,_fff );
|
|
_fgf :=_e .Join (baselineRenderPath ,_fbc .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_bda ,_fff ));if _ ,_fee :=_fb .Stat (_dc );_fee !=nil {break ;};_bcd .Logf ("\u0025\u0073",_fgf );if saveBaseline {_bcd .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_dc ,_fgf );
|
|
_gea :=CopyFile (_dc ,_fgf );if _gea !=nil {_bcd .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_fgf ,_gea );};continue ;};_bcd .Run (_fbc .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_fff ),func (_feef *_d .T ){_feef .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_dc ,_fgf );
|
|
_cce ,_deb :=ComparePNGFiles (_dc ,_fgf );if _fb .IsNotExist (_deb ){_feef .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_cce {_feef .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");
|
|
};});};});};var (ErrRenderNotSupported =_af .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");
|
|
); |