mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-27 13:48:51 +08:00
12 lines
8.7 KiB
Go
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";_eb "encoding/hex";_f "errors";_e "fmt";_gcd "github.com/unidoc/unipdf/v3/common";_bg "github.com/unidoc/unipdf/v3/core";_ga "image";_bc "image/png";_de "io";_bb "os";_a "os/exec";_da "path/filepath";_gc "strings";_d "testing";);var (ErrRenderNotSupported =_f .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 _ ,_cg :=_a .LookPath ("\u0067\u0073");_cg !=nil {return ErrRenderNotSupported ;};return _a .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 ();};func ParseIndirectObjects (rawpdf string )(map[int64 ]_bg .PdfObject ,error ){_aa :=_bg .NewParserFromString (rawpdf );_gbc :=map[int64 ]_bg .PdfObject {};for {_be ,_bdd :=_aa .ParseIndirectObject ();if _bdd !=nil {if _bdd ==_de .EOF {break ;};return nil ,_bdd ;};switch _bec :=_be .(type ){case *_bg .PdfIndirectObject :_gbc [_bec .ObjectNumber ]=_be ;case *_bg .PdfObjectStream :_gbc [_bec .ObjectNumber ]=_be ;};};for _ ,_ggf :=range _gbc {_dc (_ggf ,_gbc );};return _gbc ,nil ;};func _dc (_fef _bg .PdfObject ,_cdg map[int64 ]_bg .PdfObject )error {switch _cgb :=_fef .(type ){case *_bg .PdfIndirectObject :_eac :=_cgb ;_dc (_eac .PdfObject ,_cdg );case *_bg .PdfObjectDictionary :_gcfc :=_cgb ;for _ ,_gff :=range _gcfc .Keys (){_fb :=_gcfc .Get (_gff );if _eaf ,_dab :=_fb .(*_bg .PdfObjectReference );_dab {_cga ,_ecg :=_cdg [_eaf .ObjectNumber ];if !_ecg {return _f .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");};_gcfc .Set (_gff ,_cga );}else {_dc (_fb ,_cdg );};};case *_bg .PdfObjectArray :_ebg :=_cgb ;for _cdd ,_ffe :=range _ebg .Elements (){if _fcg ,_bf :=_ffe .(*_bg .PdfObjectReference );_bf {_ge ,_cabb :=_cdg [_fcg .ObjectNumber ];if !_cabb {return _f .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 (_cdd ,_ge );}else {_dc (_ffe ,_cdg );};};};return nil ;};func RunRenderTest (t *_d .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_eg :=_gc .TrimSuffix (_da .Base (pdfPath ),_da .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_ec *_d .T ){_cab :=_da .Join (outputDir ,_eg );_gf :=_cab +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _fa :=RenderPDFToPNGs (pdfPath ,0,_gf );_fa !=nil {_ec .Skip (_fa );};for _dfe :=1;true ;_dfe ++{_cf :=_e .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_cab ,_dfe );_ef :=_da .Join (baselineRenderPath ,_e .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_eg ,_dfe ));if _ ,_gbf :=_bb .Stat (_cf );_gbf !=nil {break ;};_ec .Logf ("\u0025\u0073",_ef );if _ ,_fg :=_bb .Stat (_ef );_bb .IsNotExist (_fg ){if saveBaseline {_ec .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_cf ,_ef );CopyFile (_cf ,_ef );continue ;};break ;};_ec .Run (_e .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_dfe ),func (_ecf *_d .T ){_ecf .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_cf ,_ef );_cfg ,_dd :=ComparePNGFiles (_cf ,_ef );if _bb .IsNotExist (_dd ){_ecf .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_cfg {_ecf .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");};});};});};func HashFile (file string )(string ,error ){_gcf ,_df :=_bb .Open (file );if _df !=nil {return "",_df ;};defer _gcf .Close ();_ea :=_g .New ();if _ ,_df =_de .Copy (_ea ,_gcf );_df !=nil {return "",_df ;};return _eb .EncodeToString (_ea .Sum (nil )),nil ;};func CompareDictionariesDeep (d1 ,d2 *_bg .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_gcd .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 ()));_gcd .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_gffb :=range d1 .Keys (){if _gffb =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;};_daaf :=_bg .TraceToDirectObject (d1 .Get (_gffb ));_deg :=_bg .TraceToDirectObject (d2 .Get (_gffb ));if _daaf ==nil {_gcd .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _deg ==nil {_gcd .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};switch _gba :=_daaf .(type ){case *_bg .PdfObjectDictionary :_gbcd ,_bdg :=_deg .(*_bg .PdfObjectDictionary );if !_bdg {_gcd .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_daaf ,_deg );return false ;};if !CompareDictionariesDeep (_gba ,_gbcd ){return false ;};continue ;case *_bg .PdfObjectArray :_bce ,_ce :=_deg .(*_bg .PdfObjectArray );if !_ce {_gcd .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");return false ;};if _gba .Len ()!=_bce .Len (){_gcd .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",_gba .Len (),_bce .Len ());return false ;};for _cfaa :=0;_cfaa < _gba .Len ();_cfaa ++{_fd :=_bg .TraceToDirectObject (_gba .Get (_cfaa ));_ba :=_bg .TraceToDirectObject (_bce .Get (_cfaa ));if _ae ,_cdde :=_fd .(*_bg .PdfObjectDictionary );_cdde {_fcgb ,_edc :=_ba .(*_bg .PdfObjectDictionary );if !_edc {return false ;};if !CompareDictionariesDeep (_ae ,_fcgb ){return false ;};}else {if _fd .WriteString ()!=_ba .WriteString (){_gcd .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_fd .WriteString (),_ba .WriteString ());return false ;};};};continue ;};if _daaf .String ()!=_deg .String (){_gcd .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",_gffb ,_daaf .String (),_deg .String ());_gcd .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_daaf ,_deg );_gcd .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_daaf ,_deg );return false ;};};return true ;};func CopyFile (src ,dst string )error {_bca ,_fc :=_bb .Open (src );if _fc !=nil {return _fc ;};defer _bca .Close ();_bgd ,_fc :=_bb .Create (dst );if _fc !=nil {return _fc ;};defer _bgd .Close ();_ ,_fc =_de .Copy (_bgd ,_bca );return _fc ;};func CompareImages (img1 ,img2 _ga .Image )(bool ,error ){_add :=img1 .Bounds ();_bgc :=0;for _db :=0;_db < _add .Size ().X ;_db ++{for _ab :=0;_ab < _add .Size ().Y ;_ab ++{_ff ,_gg ,_dbe ,_ :=img1 .At (_db ,_ab ).RGBA ();_daa ,_bd ,_ggg ,_ :=img2 .At (_db ,_ab ).RGBA ();if _ff !=_daa ||_gg !=_bd ||_dbe !=_ggg {_bgc ++;};};};_ac :=float64 (_bgc )/float64 (_add .Dx ()*_add .Dy ());if _ac > 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",_ac ,_bgc );return false ,nil ;};return true ,nil ;};func ReadPNG (file string )(_ga .Image ,error ){_gb ,_gcdb :=_bb .Open (file );if _gcdb !=nil {return nil ,_gcdb ;};defer _gb .Close ();return _bc .Decode (_gb );};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_c ,_dea :=HashFile (file1 );if _dea !=nil {return false ,_dea ;};_bgg ,_dea :=HashFile (file2 );if _dea !=nil {return false ,_dea ;};if _c ==_bgg {return true ,nil ;};_ed ,_dea :=ReadPNG (file1 );if _dea !=nil {return false ,_dea ;};_fe ,_dea :=ReadPNG (file2 );if _dea !=nil {return false ,_dea ;};if _ed .Bounds ()!=_fe .Bounds (){return false ,nil ;};return CompareImages (_ed ,_fe );}; |