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 (_b "crypto/md5";_ce "encoding/hex";_bg "errors";_c "fmt";_bgc "github.com/unidoc/unipdf/v3/common";_df "github.com/unidoc/unipdf/v3/core";_gg "image";_ge "image/png";_f "io";_ef "os";_ec "os/exec";_g "path/filepath";_d "strings";
|
|
_ea "testing";);func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_cgc :=_ec .LookPath ("\u0067\u0073");_cgc !=nil {return ErrRenderNotSupported ;};return _ec .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_c .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();
|
|
};func HashFile (file string )(string ,error ){_eaa ,_bgb :=_ef .Open (file );if _bgb !=nil {return "",_bgb ;};defer _eaa .Close ();_ae :=_b .New ();if _ ,_bgb =_f .Copy (_ae ,_eaa );_bgb !=nil {return "",_bgb ;};return _ce .EncodeToString (_ae .Sum (nil )),nil ;
|
|
};func ParseIndirectObjects (rawpdf string )(map[int64 ]_df .PdfObject ,error ){_eb :=_df .NewParserFromString (rawpdf );_db :=map[int64 ]_df .PdfObject {};for {_cbf ,_agg :=_eb .ParseIndirectObject ();if _agg !=nil {if _agg ==_f .EOF {break ;};return nil ,_agg ;
|
|
};switch _ddg :=_cbf .(type ){case *_df .PdfIndirectObject :_db [_ddg .ObjectNumber ]=_cbf ;case *_df .PdfObjectStream :_db [_ddg .ObjectNumber ]=_cbf ;};};for _ ,_bc :=range _db {_bcf (_bc ,_db );};return _db ,nil ;};func CompareDictionariesDeep (d1 ,d2 *_df .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_bgc .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 ()));
|
|
_bgc .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_eca :=range d1 .Keys (){if _eca =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;
|
|
};_dba :=_df .TraceToDirectObject (d1 .Get (_eca ));_bgcd :=_df .TraceToDirectObject (d2 .Get (_eca ));if _dba ==nil {_bgc .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _bgcd ==nil {_bgc .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");
|
|
return false ;};switch _ee :=_dba .(type ){case *_df .PdfObjectDictionary :_dcd ,_ede :=_bgcd .(*_df .PdfObjectDictionary );if !_ede {_bgc .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_dba ,_bgcd );
|
|
return false ;};if !CompareDictionariesDeep (_ee ,_dcd ){return false ;};continue ;case *_df .PdfObjectArray :_gbg ,_fac :=_bgcd .(*_df .PdfObjectArray );if !_fac {_bgc .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");
|
|
return false ;};if _ee .Len ()!=_gbg .Len (){_bgc .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",_ee .Len (),_gbg .Len ());
|
|
return false ;};for _cff :=0;_cff < _ee .Len ();_cff ++{_gegg :=_df .TraceToDirectObject (_ee .Get (_cff ));_gcb :=_df .TraceToDirectObject (_gbg .Get (_cff ));if _bfe ,_eef :=_gegg .(*_df .PdfObjectDictionary );_eef {_gggd ,_dbcg :=_gcb .(*_df .PdfObjectDictionary );
|
|
if !_dbcg {return false ;};if !CompareDictionariesDeep (_bfe ,_gggd ){return false ;};}else {if _gegg .WriteString ()!=_gcb .WriteString (){_bgc .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_gegg .WriteString (),_gcb .WriteString ());
|
|
return false ;};};};continue ;};if _dba .String ()!=_bgcd .String (){_bgc .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",_eca ,_dba .String (),_bgcd .String ());
|
|
_bgc .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_dba ,_bgcd );_bgc .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_dba ,_bgcd );return false ;
|
|
};};return true ;};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_cb ,_afb :=HashFile (file1 );if _afb !=nil {return false ,_afb ;};_afc ,_afb :=HashFile (file2 );if _afb !=nil {return false ,_afb ;};if _cb ==_afc {return true ,nil ;};_geg ,_afb :=ReadPNG (file1 );
|
|
if _afb !=nil {return false ,_afb ;};_aa ,_afb :=ReadPNG (file2 );if _afb !=nil {return false ,_afb ;};if _geg .Bounds ()!=_aa .Bounds (){return false ,nil ;};return CompareImages (_geg ,_aa );};var (ErrRenderNotSupported =_bg .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 ReadPNG (file string )(_gg .Image ,error ){_fa ,_ag :=_ef .Open (file );if _ag !=nil {return nil ,_ag ;};defer _fa .Close ();return _ge .Decode (_fa );};func RunRenderTest (t *_ea .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_aag :=_d .TrimSuffix (_g .Base (pdfPath ),_g .Ext (pdfPath ));
|
|
t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_aaa *_ea .T ){_afa :=_g .Join (outputDir ,_aag );_aae :=_afa +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _bgcb :=RenderPDFToPNGs (pdfPath ,0,_aae );_bgcb !=nil {_aaa .Skip (_bgcb );};for _ff :=1;true ;
|
|
_ff ++{_ed :=_c .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_afa ,_ff );_eaf :=_g .Join (baselineRenderPath ,_c .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_aag ,_ff ));if _ ,_geff :=_ef .Stat (_ed );
|
|
_geff !=nil {break ;};_aaa .Logf ("\u0025\u0073",_eaf );if saveBaseline {_aaa .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_ed ,_eaf );_ac :=CopyFile (_ed ,_eaf );if _ac !=nil {_aaa .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_eaf ,_ac );
|
|
};continue ;};_aaa .Run (_c .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_ff ),func (_aac *_ea .T ){_aac .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_ed ,_eaf );_gb ,_aacd :=ComparePNGFiles (_ed ,_eaf );
|
|
if _ef .IsNotExist (_aacd ){_aac .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_gb {_aac .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");
|
|
};});};});};func CompareImages (img1 ,img2 _gg .Image )(bool ,error ){_gf :=img1 .Bounds ();_gc :=0;for _gdf :=0;_gdf < _gf .Size ().X ;_gdf ++{for _agb :=0;_agb < _gf .Size ().Y ;_agb ++{_fd ,_ecc ,_cg ,_ :=img1 .At (_gdf ,_agb ).RGBA ();_af ,_fg ,_ad ,_ :=img2 .At (_gdf ,_agb ).RGBA ();
|
|
if _fd !=_af ||_ecc !=_fg ||_cg !=_ad {_gc ++;};};};_fcf :=float64 (_gc )/float64 (_gf .Dx ()*_gf .Dy ());if _fcf > 0.0001{_c .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_fcf ,_gc );
|
|
return false ,nil ;};return true ,nil ;};func _bcf (_ead _df .PdfObject ,_fe map[int64 ]_df .PdfObject )error {switch _be :=_ead .(type ){case *_df .PdfIndirectObject :_cfb :=_be ;_bcf (_cfb .PdfObject ,_fe );case *_df .PdfObjectDictionary :_agbb :=_be ;
|
|
for _ ,_bed :=range _agbb .Keys (){_bb :=_agbb .Get (_bed );if _aacde ,_ggg :=_bb .(*_df .PdfObjectReference );_ggg {_bbd ,_cc :=_fe [_aacde .ObjectNumber ];if !_cc {return _bg .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
|
|
};_agbb .Set (_bed ,_bbd );}else {_bcf (_bb ,_fe );};};case *_df .PdfObjectArray :_dbc :=_be ;for _bcfb ,_gff :=range _dbc .Elements (){if _ab ,_de :=_gff .(*_df .PdfObjectReference );_de {_dc ,_bea :=_fe [_ab .ObjectNumber ];if !_bea {return _bg .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
|
|
};_dbc .Set (_bcfb ,_dc );}else {_bcf (_gff ,_fe );};};};return nil ;};func CopyFile (src ,dst string )error {_a ,_dd :=_ef .Open (src );if _dd !=nil {return _dd ;};defer _a .Close ();_gef ,_dd :=_ef .Create (dst );if _dd !=nil {return _dd ;};defer _gef .Close ();
|
|
_ ,_dd =_f .Copy (_gef ,_a );return _dd ;}; |