mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-27 13:48:51 +08:00
39 lines
8.8 KiB
Go
39 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 (_f "crypto/md5";_ce "encoding/hex";_ee "errors";_bg "fmt";_g "github.com/unidoc/unipdf/v3/common";_eg "github.com/unidoc/unipdf/v3/core";_dg "image";_cf "image/png";_fe "io";_cd "os";_e "os/exec";_a "path/filepath";_c "strings";
|
|
_d "testing";);var (ErrRenderNotSupported =_ee .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 ]_eg .PdfObject ,error ){_cgd :=_eg .NewParserFromString (rawpdf );_cfb :=map[int64 ]_eg .PdfObject {};for {_aff ,_da :=_cgd .ParseIndirectObject ();if _da !=nil {if _da ==_fe .EOF {break ;};return nil ,_da ;
|
|
};switch _ec :=_aff .(type ){case *_eg .PdfIndirectObject :_cfb [_ec .ObjectNumber ]=_aff ;case *_eg .PdfObjectStream :_cfb [_ec .ObjectNumber ]=_aff ;};};for _ ,_gbc :=range _cfb {_cef (_gbc ,_cfb );};return _cfb ,nil ;};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_gf ,_ae :=HashFile (file1 );
|
|
if _ae !=nil {return false ,_ae ;};_gag ,_ae :=HashFile (file2 );if _ae !=nil {return false ,_ae ;};if _gf ==_gag {return true ,nil ;};_gfe ,_ae :=ReadPNG (file1 );if _ae !=nil {return false ,_ae ;};_fd ,_ae :=ReadPNG (file2 );if _ae !=nil {return false ,_ae ;
|
|
};if _gfe .Bounds ()!=_fd .Bounds (){return false ,nil ;};return CompareImages (_gfe ,_fd );};func CompareImages (img1 ,img2 _dg .Image )(bool ,error ){_ebg :=img1 .Bounds ();_bf :=0;for _af :=0;_af < _ebg .Size ().X ;_af ++{for _cea :=0;_cea < _ebg .Size ().Y ;
|
|
_cea ++{_de ,_ad ,_bfe ,_ :=img1 .At (_af ,_cea ).RGBA ();_df ,_ba ,_gb ,_ :=img2 .At (_af ,_cea ).RGBA ();if _de !=_df ||_ad !=_ba ||_bfe !=_gb {_bf ++;};};};_ea :=float64 (_bf )/float64 (_ebg .Dx ()*_ebg .Dy ());if _ea > 0.0001{_bg .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_ea ,_bf );
|
|
return false ,nil ;};return true ,nil ;};func RunRenderTest (t *_d .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_fb :=_c .TrimSuffix (_a .Base (pdfPath ),_a .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_bc *_d .T ){_fag :=_a .Join (outputDir ,_fb );
|
|
_dgc :=_fag +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _bfc :=RenderPDFToPNGs (pdfPath ,0,_dgc );_bfc !=nil {_bc .Skip (_bfc );};for _gfc :=1;true ;_gfc ++{_gcb :=_bg .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_fag ,_gfc );_aee :=_a .Join (baselineRenderPath ,_bg .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_fb ,_gfc ));
|
|
if _ ,_cad :=_cd .Stat (_gcb );_cad !=nil {break ;};_bc .Logf ("\u0025\u0073",_aee );if saveBaseline {_bc .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_gcb ,_aee );_ff :=CopyFile (_gcb ,_aee );if _ff !=nil {_bc .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_aee ,_ff );
|
|
};continue ;};_bc .Run (_bg .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_gfc ),func (_bcg *_d .T ){_bcg .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_gcb ,_aee );_cg ,_cee :=ComparePNGFiles (_gcb ,_aee );
|
|
if _cd .IsNotExist (_cee ){_bcg .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_cg {_bcg .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");
|
|
};});};});};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_bbb :=_e .LookPath ("\u0067\u0073");_bbb !=nil {return ErrRenderNotSupported ;};return _e .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_bg .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();
|
|
};func _cef (_fbb _eg .PdfObject ,_bff map[int64 ]_eg .PdfObject )error {switch _ceg :=_fbb .(type ){case *_eg .PdfIndirectObject :_dc :=_ceg ;_cef (_dc .PdfObject ,_bff );case *_eg .PdfObjectDictionary :_ege :=_ceg ;for _ ,_ded :=range _ege .Keys (){_ada :=_ege .Get (_ded );
|
|
if _ffg ,_aea :=_ada .(*_eg .PdfObjectReference );_aea {_dec ,_gdc :=_bff [_ffg .ObjectNumber ];if !_gdc {return _ee .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
|
|
};_ege .Set (_ded ,_dec );}else {_cef (_ada ,_bff );};};case *_eg .PdfObjectArray :_ffc :=_ceg ;for _eaf ,_ab :=range _ffc .Elements (){if _efa ,_gbe :=_ab .(*_eg .PdfObjectReference );_gbe {_dbb ,_bfg :=_bff [_efa .ObjectNumber ];if !_bfg {return _ee .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
|
|
};_ffc .Set (_eaf ,_dbb );}else {_cef (_ab ,_bff );};};};return nil ;};func ReadPNG (file string )(_dg .Image ,error ){_ga ,_gc :=_cd .Open (file );if _gc !=nil {return nil ,_gc ;};defer _ga .Close ();return _cf .Decode (_ga );};func HashFile (file string )(string ,error ){_ca ,_bd :=_cd .Open (file );
|
|
if _bd !=nil {return "",_bd ;};defer _ca .Close ();_bgd :=_f .New ();if _ ,_bd =_fe .Copy (_bgd ,_ca );_bd !=nil {return "",_bd ;};return _ce .EncodeToString (_bgd .Sum (nil )),nil ;};func CompareDictionariesDeep (d1 ,d2 *_eg .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_g .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 ()));
|
|
_g .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_ag :=range d1 .Keys (){if _ag =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;};_abc :=_eg .TraceToDirectObject (d1 .Get (_ag ));
|
|
_fbd :=_eg .TraceToDirectObject (d2 .Get (_ag ));if _abc ==nil {_g .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _fbd ==nil {_g .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};switch _ge :=_abc .(type ){case *_eg .PdfObjectDictionary :_afd ,_cbf :=_fbd .(*_eg .PdfObjectDictionary );
|
|
if !_cbf {_g .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_abc ,_fbd );return false ;};if !CompareDictionariesDeep (_ge ,_afd ){return false ;};continue ;case *_eg .PdfObjectArray :_bbd ,_bbf :=_fbd .(*_eg .PdfObjectArray );
|
|
if !_bbf {_g .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");return false ;};if _ge .Len ()!=_bbd .Len (){_g .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",_ge .Len (),_bbd .Len ());
|
|
return false ;};for _cag :=0;_cag < _ge .Len ();_cag ++{_aa :=_eg .TraceToDirectObject (_ge .Get (_cag ));_ebe :=_eg .TraceToDirectObject (_bbd .Get (_cag ));if _agc ,_eca :=_aa .(*_eg .PdfObjectDictionary );_eca {_aba ,_dca :=_ebe .(*_eg .PdfObjectDictionary );
|
|
if !_dca {return false ;};if !CompareDictionariesDeep (_agc ,_aba ){return false ;};}else {if _aa .WriteString ()!=_ebe .WriteString (){_g .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_aa .WriteString (),_ebe .WriteString ());
|
|
return false ;};};};continue ;};if _abc .String ()!=_fbd .String (){_g .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",_ag ,_abc .String (),_fbd .String ());
|
|
_g .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_abc ,_fbd );_g .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_abc ,_fbd );return false ;
|
|
};};return true ;};func CopyFile (src ,dst string )error {_bb ,_fa :=_cd .Open (src );if _fa !=nil {return _fa ;};defer _bb .Close ();_dd ,_fa :=_cd .Create (dst );if _fa !=nil {return _fa ;};defer _dd .Close ();_ ,_fa =_fe .Copy (_dd ,_bb );return _fa ;
|
|
}; |