mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-27 13:48:51 +08:00
40 lines
8.8 KiB
Go
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 (_g "crypto/md5";_fe "encoding/hex";_dd "errors";_e "fmt";_ae "github.com/unidoc/unipdf/v3/common";_dg "github.com/unidoc/unipdf/v3/core";_bb "image";_ab "image/png";_d "io";_b "os";_fb "os/exec";_ff "path/filepath";_a "strings";
|
|
_fg "testing";);func RunRenderTest (t *_fg .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_ddf :=_a .TrimSuffix (_ff .Base (pdfPath ),_ff .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_gdb *_fg .T ){_aedf :=_ff .Join (outputDir ,_ddf );
|
|
_gg :=_aedf +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _aa :=RenderPDFToPNGs (pdfPath ,0,_gg );_aa !=nil {_gdb .Skip (_aa );};for _egd :=1;true ;_egd ++{_bf :=_e .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_aedf ,_egd );_fee :=_ff .Join (baselineRenderPath ,_e .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_ddf ,_egd ));
|
|
if _ ,_ca :=_b .Stat (_bf );_ca !=nil {break ;};_gdb .Logf ("\u0025\u0073",_fee );if saveBaseline {_gdb .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_bf ,_fee );_dfa :=CopyFile (_bf ,_fee );if _dfa !=nil {_gdb .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_fee ,_dfa );
|
|
};continue ;};_gdb .Run (_e .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_egd ),func (_ege *_fg .T ){_ege .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_bf ,_fee );_dfd ,_gcc :=ComparePNGFiles (_bf ,_fee );
|
|
if _b .IsNotExist (_gcc ){_ege .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_dfd {_ege .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");
|
|
};});};});};func HashFile (file string )(string ,error ){_aed ,_df :=_b .Open (file );if _df !=nil {return "",_df ;};defer _aed .Close ();_cb :=_g .New ();if _ ,_df =_d .Copy (_cb ,_aed );_df !=nil {return "",_df ;};return _fe .EncodeToString (_cb .Sum (nil )),nil ;
|
|
};func CompareImages (img1 ,img2 _bb .Image )(bool ,error ){_abg :=img1 .Bounds ();_eac :=0;for _ac :=0;_ac < _abg .Size ().X ;_ac ++{for _gd :=0;_gd < _abg .Size ().Y ;_gd ++{_ffc ,_bgd ,_eb ,_ :=img1 .At (_ac ,_gd ).RGBA ();_bd ,_dde ,_ec ,_ :=img2 .At (_ac ,_gd ).RGBA ();
|
|
if _ffc !=_bd ||_bgd !=_dde ||_eb !=_ec {_eac ++;};};};_ga :=float64 (_eac )/float64 (_abg .Dx ()*_abg .Dy ());if _ga > 0.0001{_e .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_ga ,_eac );
|
|
return false ,nil ;};return true ,nil ;};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_dgd ,_gb :=HashFile (file1 );if _gb !=nil {return false ,_gb ;};_eg ,_gb :=HashFile (file2 );if _gb !=nil {return false ,_gb ;};if _dgd ==_eg {return true ,nil ;
|
|
};_bga ,_gb :=ReadPNG (file1 );if _gb !=nil {return false ,_gb ;};_fa ,_gb :=ReadPNG (file2 );if _gb !=nil {return false ,_gb ;};if _bga .Bounds ()!=_fa .Bounds (){return false ,nil ;};return CompareImages (_bga ,_fa );};func CompareDictionariesDeep (d1 ,d2 *_dg .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_ae .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 ()));
|
|
_ae .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_ggf :=range d1 .Keys (){if _ggf =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;
|
|
};_aag :=_dg .TraceToDirectObject (d1 .Get (_ggf ));_bcc :=_dg .TraceToDirectObject (d2 .Get (_ggf ));if _aag ==nil {_ae .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _bcc ==nil {_ae .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");
|
|
return false ;};switch _agd :=_aag .(type ){case *_dg .PdfObjectDictionary :_cfa ,_bfb :=_bcc .(*_dg .PdfObjectDictionary );if !_bfb {_ae .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_aag ,_bcc );
|
|
return false ;};if !CompareDictionariesDeep (_agd ,_cfa ){return false ;};continue ;case *_dg .PdfObjectArray :_ccc ,_cag :=_bcc .(*_dg .PdfObjectArray );if !_cag {_ae .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");
|
|
return false ;};if _agd .Len ()!=_ccc .Len (){_ae .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",_agd .Len (),_ccc .Len ());
|
|
return false ;};for _bge :=0;_bge < _agd .Len ();_bge ++{_ge :=_dg .TraceToDirectObject (_agd .Get (_bge ));_dfdd :=_dg .TraceToDirectObject (_ccc .Get (_bge ));if _ccg ,_aec :=_ge .(*_dg .PdfObjectDictionary );_aec {_be ,_acc :=_dfdd .(*_dg .PdfObjectDictionary );
|
|
if !_acc {return false ;};if !CompareDictionariesDeep (_ccg ,_be ){return false ;};}else {if _ge .WriteString ()!=_dfdd .WriteString (){_ae .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_ge .WriteString (),_dfdd .WriteString ());
|
|
return false ;};};};continue ;};if _aag .String ()!=_bcc .String (){_ae .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",_ggf ,_aag .String (),_bcc .String ());
|
|
_ae .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_aag ,_bcc );_ae .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_aag ,_bcc );return false ;
|
|
};};return true ;};func _eagb (_ef _dg .PdfObject ,_ad map[int64 ]_dg .PdfObject )error {switch _gdd :=_ef .(type ){case *_dg .PdfIndirectObject :_cga :=_gdd ;_eagb (_cga .PdfObject ,_ad );case *_dg .PdfObjectDictionary :_eab :=_gdd ;for _ ,_ebc :=range _eab .Keys (){_egeb :=_eab .Get (_ebc );
|
|
if _bcb ,_cf :=_egeb .(*_dg .PdfObjectReference );_cf {_gbc ,_dga :=_ad [_bcb .ObjectNumber ];if !_dga {return _dd .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
|
|
};_eab .Set (_ebc ,_gbc );}else {_eagb (_egeb ,_ad );};};case *_dg .PdfObjectArray :_ebg :=_gdd ;for _aeg ,_af :=range _ebg .Elements (){if _ed ,_fc :=_af .(*_dg .PdfObjectReference );_fc {_bfc ,_gde :=_ad [_ed .ObjectNumber ];if !_gde {return _dd .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
|
|
};_ebg .Set (_aeg ,_bfc );}else {_eagb (_af ,_ad );};};};return nil ;};func CopyFile (src ,dst string )error {_c ,_fea :=_b .Open (src );if _fea !=nil {return _fea ;};defer _c .Close ();_gc ,_fea :=_b .Create (dst );if _fea !=nil {return _fea ;};defer _gc .Close ();
|
|
_ ,_fea =_d .Copy (_gc ,_c );return _fea ;};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_ag :=_fb .LookPath ("\u0067\u0073");_ag !=nil {return ErrRenderNotSupported ;};return _fb .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_e .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();
|
|
};var (ErrRenderNotSupported =_dd .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 ParseIndirectObjects (rawpdf string )(map[int64 ]_dg .PdfObject ,error ){_cd :=_dg .NewParserFromString (rawpdf );_bgc :=map[int64 ]_dg .PdfObject {};for {_de ,_gge :=_cd .ParseIndirectObject ();if _gge !=nil {if _gge ==_d .EOF {break ;};return nil ,_gge ;
|
|
};switch _cdc :=_de .(type ){case *_dg .PdfIndirectObject :_bgc [_cdc .ObjectNumber ]=_de ;case *_dg .PdfObjectStream :_bgc [_cdc .ObjectNumber ]=_de ;};};for _ ,_fd :=range _bgc {_eagb (_fd ,_bgc );};return _bgc ,nil ;};func ReadPNG (file string )(_bb .Image ,error ){_fbb ,_bc :=_b .Open (file );
|
|
if _bc !=nil {return nil ,_bc ;};defer _fbb .Close ();return _ab .Decode (_fbb );}; |