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 (_e "crypto/md5";_fb "encoding/hex";_eb "errors";_b "fmt";_ed "github.com/unidoc/unipdf/v3/common";_ba "github.com/unidoc/unipdf/v3/core";_g "image";_dd "image/png";_fe "io";_df "os";_fa "os/exec";_a "path/filepath";_eg "strings";
|
|
_f "testing";);func HashFile (file string )(string ,error ){_fef ,_ag :=_df .Open (file );if _ag !=nil {return "",_ag ;};defer _fef .Close ();_aaf :=_e .New ();if _ ,_ag =_fe .Copy (_aaf ,_fef );_ag !=nil {return "",_ag ;};return _fb .EncodeToString (_aaf .Sum (nil )),nil ;
|
|
};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_ee :=_fa .LookPath ("\u0067\u0073");_ee !=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 ,_b .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();
|
|
};func CopyFile (src ,dst string )error {_bf ,_ef :=_df .Open (src );if _ef !=nil {return _ef ;};defer _bf .Close ();_ec ,_ef :=_df .Create (dst );if _ef !=nil {return _ef ;};defer _ec .Close ();_ ,_ef =_fe .Copy (_ec ,_bf );return _ef ;};func CompareImages (img1 ,img2 _g .Image )(bool ,error ){_ebe :=img1 .Bounds ();
|
|
_ab :=0;for _c :=0;_c < _ebe .Size ().X ;_c ++{for _gc :=0;_gc < _ebe .Size ().Y ;_gc ++{_bg ,_cg ,_ad ,_ :=img1 .At (_c ,_gc ).RGBA ();_dg ,_fab ,_efg ,_ :=img2 .At (_c ,_gc ).RGBA ();if _bg !=_dg ||_cg !=_fab ||_ad !=_efg {_ab ++;};};};_agc :=float64 (_ab )/float64 (_ebe .Dx ()*_ebe .Dy ());
|
|
if _agc > 0.0001{_b .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_agc ,_ab );return false ,nil ;};return true ,nil ;};func CompareDictionariesDeep (d1 ,d2 *_ba .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_ed .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 ()));
|
|
_ed .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_gaf :=range d1 .Keys (){if _gaf =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;
|
|
};_cgfg :=_ba .TraceToDirectObject (d1 .Get (_gaf ));_bgg :=_ba .TraceToDirectObject (d2 .Get (_gaf ));if _cgfg ==nil {_ed .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _bgg ==nil {_ed .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");
|
|
return false ;};switch _ae :=_cgfg .(type ){case *_ba .PdfObjectDictionary :_ca ,_aaa :=_bgg .(*_ba .PdfObjectDictionary );if !_aaa {_ed .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_cgfg ,_bgg );
|
|
return false ;};if !CompareDictionariesDeep (_ae ,_ca ){return false ;};continue ;case *_ba .PdfObjectArray :_bea ,_af :=_bgg .(*_ba .PdfObjectArray );if !_af {_ed .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");
|
|
return false ;};if _ae .Len ()!=_bea .Len (){_ed .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",_ae .Len (),_bea .Len ());
|
|
return false ;};for _aed :=0;_aed < _ae .Len ();_aed ++{_bfa :=_ba .TraceToDirectObject (_ae .Get (_aed ));_bef :=_ba .TraceToDirectObject (_bea .Get (_aed ));if _aaag ,_gee :=_bfa .(*_ba .PdfObjectDictionary );_gee {_efb ,_caa :=_bef .(*_ba .PdfObjectDictionary );
|
|
if !_caa {return false ;};if !CompareDictionariesDeep (_aaag ,_efb ){return false ;};}else {if _bfa .WriteString ()!=_bef .WriteString (){_ed .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_bfa .WriteString (),_bef .WriteString ());
|
|
return false ;};};};continue ;};if _cgfg .String ()!=_bgg .String (){_ed .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",_gaf ,_cgfg .String (),_bgg .String ());
|
|
_ed .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_cgfg ,_bgg );_ed .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_cgfg ,_bgg );return false ;
|
|
};};return true ;};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_bd ,_fbd :=HashFile (file1 );if _fbd !=nil {return false ,_fbd ;};_ddc ,_fbd :=HashFile (file2 );if _fbd !=nil {return false ,_fbd ;};if _bd ==_ddc {return true ,nil ;};_ga ,_fbd :=ReadPNG (file1 );
|
|
if _fbd !=nil {return false ,_fbd ;};_gca ,_fbd :=ReadPNG (file2 );if _fbd !=nil {return false ,_fbd ;};if _ga .Bounds ()!=_gca .Bounds (){return false ,nil ;};return CompareImages (_ga ,_gca );};func ReadPNG (file string )(_g .Image ,error ){_ff ,_dc :=_df .Open (file );
|
|
if _dc !=nil {return nil ,_dc ;};defer _ff .Close ();return _dd .Decode (_ff );};var (ErrRenderNotSupported =_eb .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 ]_ba .PdfObject ,error ){_fbb :=_ba .NewParserFromString (rawpdf );_ge :=map[int64 ]_ba .PdfObject {};for {_fabe ,_ea :=_fbb .ParseIndirectObject ();if _ea !=nil {if _ea ==_fe .EOF {break ;};return nil ,_ea ;
|
|
};switch _efa :=_fabe .(type ){case *_ba .PdfIndirectObject :_ge [_efa .ObjectNumber ]=_fabe ;case *_ba .PdfObjectStream :_ge [_efa .ObjectNumber ]=_fabe ;};};for _ ,_da :=range _ge {_bbc (_da ,_ge );};return _ge ,nil ;};func _bbc (_cgf _ba .PdfObject ,_dda map[int64 ]_ba .PdfObject )error {switch _ebeb :=_cgf .(type ){case *_ba .PdfIndirectObject :_edb :=_ebeb ;
|
|
_bbc (_edb .PdfObject ,_dda );case *_ba .PdfObjectDictionary :_fae :=_ebeb ;for _ ,_aab :=range _fae .Keys (){_gfa :=_fae .Get (_aab );if _dga ,_fd :=_gfa .(*_ba .PdfObjectReference );_fd {_eab ,_deb :=_dda [_dga .ObjectNumber ];if !_deb {return _eb .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
|
|
};_fae .Set (_aab ,_eab );}else {_bbc (_gfa ,_dda );};};case *_ba .PdfObjectArray :_dcg :=_ebeb ;for _fbdc ,_be :=range _dcg .Elements (){if _acg ,_gfac :=_be .(*_ba .PdfObjectReference );_gfac {_dca ,_bab :=_dda [_acg .ObjectNumber ];if !_bab {return _eb .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
|
|
};_dcg .Set (_fbdc ,_dca );}else {_bbc (_be ,_dda );};};};return nil ;};func RunRenderTest (t *_f .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_gf :=_eg .TrimSuffix (_a .Base (pdfPath ),_a .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_fea *_f .T ){_de :=_a .Join (outputDir ,_gf );
|
|
_dbd :=_de +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _cc :=RenderPDFToPNGs (pdfPath ,0,_dbd );_cc !=nil {_fea .Skip (_cc );};for _gg :=1;true ;_gg ++{_gd :=_b .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_de ,_gg );_bcc :=_a .Join (baselineRenderPath ,_b .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_gf ,_gg ));
|
|
if _ ,_ccg :=_df .Stat (_gd );_ccg !=nil {break ;};_fea .Logf ("\u0025\u0073",_bcc );if saveBaseline {_fea .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_gd ,_bcc );_ce :=CopyFile (_gd ,_bcc );if _ce !=nil {_fea .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_bcc ,_ce );
|
|
};continue ;};_fea .Run (_b .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_gg ),func (_bb *_f .T ){_bb .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_gd ,_bcc );_cca ,_cec :=ComparePNGFiles (_gd ,_bcc );
|
|
if _df .IsNotExist (_cec ){_bb .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_cca {_bb .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");
|
|
};});};});}; |