github.com/unidoc/unipdf/v3@v3.55.0/internal/testutils/testutils.go (about)

     1  //
     2  // Copyright 2020 FoxyUtils ehf. All rights reserved.
     3  //
     4  // This is a commercial product and requires a license to operate.
     5  // A trial license can be obtained at https://unidoc.io
     6  //
     7  // DO NOT EDIT: generated by unitwist Go source code obfuscator.
     8  //
     9  // Use of this source code is governed by the UniDoc End User License Agreement
    10  // terms that can be accessed at https://unidoc.io/eula/
    11  
    12  package testutils ;import (_d "crypto/md5";_bb "encoding/hex";_db "errors";_b "fmt";_fg "github.com/unidoc/unipdf/v3/common";_cf "github.com/unidoc/unipdf/v3/core";_eg "image";_efa "image/png";_c "io";_ef "os";_gg "os/exec";_g "path/filepath";_e "strings";
    13  _dg "testing";);func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_gcd :=_gg .LookPath ("\u0067\u0073");_gcd !=nil {return ErrRenderNotSupported ;};return _gg .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 ();
    14  };func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_fgb ,_gff :=HashFile (file1 );if _gff !=nil {return false ,_gff ;};_fb ,_gff :=HashFile (file2 );if _gff !=nil {return false ,_gff ;};if _fgb ==_fb {return true ,nil ;};_gca ,_gff :=ReadPNG (file1 );
    15  if _gff !=nil {return false ,_gff ;};_efg ,_gff :=ReadPNG (file2 );if _gff !=nil {return false ,_gff ;};if _gca .Bounds ()!=_efg .Bounds (){return false ,nil ;};return CompareImages (_gca ,_efg );};func HashFile (file string )(string ,error ){_df ,_aa :=_ef .Open (file );
    16  if _aa !=nil {return "",_aa ;};defer _df .Close ();_ad :=_d .New ();if _ ,_aa =_c .Copy (_ad ,_df );_aa !=nil {return "",_aa ;};return _bb .EncodeToString (_ad .Sum (nil )),nil ;};func CompareImages (img1 ,img2 _eg .Image )(bool ,error ){_eb :=img1 .Bounds ();
    17  _gfd :=0;for _bbe :=0;_bbe < _eb .Size ().X ;_bbe ++{for _cfg :=0;_cfg < _eb .Size ().Y ;_cfg ++{_ac ,_ca ,_cfgb ,_ :=img1 .At (_bbe ,_cfg ).RGBA ();_bf ,_cad ,_af ,_ :=img2 .At (_bbe ,_cfg ).RGBA ();if _ac !=_bf ||_ca !=_cad ||_cfgb !=_af {_gfd ++;};};
    18  };_fga :=float64 (_gfd )/float64 (_eb .Dx ()*_eb .Dy ());if _fga > 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",_fga ,_gfd );return false ,nil ;};return true ,nil ;
    19  };func ReadPNG (file string )(_eg .Image ,error ){_gc ,_gf :=_ef .Open (file );if _gf !=nil {return nil ,_gf ;};defer _gc .Close ();return _efa .Decode (_gc );};func ParseIndirectObjects (rawpdf string )(map[int64 ]_cf .PdfObject ,error ){_ggc :=_cf .NewParserFromString (rawpdf );
    20  _acc :=map[int64 ]_cf .PdfObject {};for {_dea ,_edcg :=_ggc .ParseIndirectObject ();if _edcg !=nil {if _edcg ==_c .EOF {break ;};return nil ,_edcg ;};switch _ffe :=_dea .(type ){case *_cf .PdfIndirectObject :_acc [_ffe .ObjectNumber ]=_dea ;case *_cf .PdfObjectStream :_acc [_ffe .ObjectNumber ]=_dea ;
    21  };};for _ ,_efd :=range _acc {_ab (_efd ,_acc );};return _acc ,nil ;};func _ab (_dad _cf .PdfObject ,_bc map[int64 ]_cf .PdfObject )error {switch _ebb :=_dad .(type ){case *_cf .PdfIndirectObject :_ba :=_ebb ;_ab (_ba .PdfObject ,_bc );case *_cf .PdfObjectDictionary :_adb :=_ebb ;
    22  for _ ,_edd :=range _adb .Keys (){_dc :=_adb .Get (_edd );if _ga ,_dd :=_dc .(*_cf .PdfObjectReference );_dd {_cd ,_ee :=_bc [_ga .ObjectNumber ];if !_ee {return _db .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
    23  };_adb .Set (_edd ,_cd );}else {_ab (_dc ,_bc );};};case *_cf .PdfObjectArray :_fff :=_ebb ;for _cag ,_cg :=range _fff .Elements (){if _cgc ,_eee :=_cg .(*_cf .PdfObjectReference );_eee {_ebg ,_fa :=_bc [_cgc .ObjectNumber ];if !_fa {return _db .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");
    24  };_fff .Set (_cag ,_ebg );}else {_ab (_cg ,_bc );};};};return nil ;};func CompareDictionariesDeep (d1 ,d2 *_cf .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_fg .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 ()));
    25  _fg .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_gab :=range d1 .Keys (){if _gab =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;
    26  };_ddf :=_cf .TraceToDirectObject (d1 .Get (_gab ));_abg :=_cf .TraceToDirectObject (d2 .Get (_gab ));if _ddf ==nil {_fg .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _abg ==nil {_fg .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");
    27  return false ;};switch _gfg :=_ddf .(type ){case *_cf .PdfObjectDictionary :_fce ,_fgaa :=_abg .(*_cf .PdfObjectDictionary );if !_fgaa {_fg .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_ddf ,_abg );
    28  return false ;};if !CompareDictionariesDeep (_gfg ,_fce ){return false ;};continue ;case *_cf .PdfObjectArray :_ade ,_ffeb :=_abg .(*_cf .PdfObjectArray );if !_ffeb {_fg .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");
    29  return false ;};if _gfg .Len ()!=_ade .Len (){_fg .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",_gfg .Len (),_ade .Len ());
    30  return false ;};for _daa :=0;_daa < _gfg .Len ();_daa ++{_ebd :=_cf .TraceToDirectObject (_gfg .Get (_daa ));_dba :=_cf .TraceToDirectObject (_ade .Get (_daa ));if _bac ,_ebe :=_ebd .(*_cf .PdfObjectDictionary );_ebe {_fab ,_dgcd :=_dba .(*_cf .PdfObjectDictionary );
    31  if !_dgcd {return false ;};if !CompareDictionariesDeep (_bac ,_fab ){return false ;};}else {if _ebd .WriteString ()!=_dba .WriteString (){_fg .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_ebd .WriteString (),_dba .WriteString ());
    32  return false ;};};};continue ;};if _ddf .String ()!=_abg .String (){_fg .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",_gab ,_ddf .String (),_abg .String ());
    33  _fg .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_ddf ,_abg );_fg .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_ddf ,_abg );return false ;
    34  };};return true ;};func RunRenderTest (t *_dg .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_bec :=_e .TrimSuffix (_g .Base (pdfPath ),_g .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_ge *_dg .T ){_edc :=_g .Join (outputDir ,_bec );
    35  _fc :=_edc +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _gcdd :=RenderPDFToPNGs (pdfPath ,0,_fc );_gcdd !=nil {_ge .Skip (_gcdd );};for _bd :=1;true ;_bd ++{_de :=_b .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_edc ,_bd );_ff :=_g .Join (baselineRenderPath ,_b .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_bec ,_bd ));
    36  if _ ,_egb :=_ef .Stat (_de );_egb !=nil {break ;};_ge .Logf ("\u0025\u0073",_ff );if saveBaseline {_ge .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_de ,_ff );_beb :=CopyFile (_de ,_ff );if _beb !=nil {_ge .Fatalf ("\u0045\u0052\u0052OR\u0020\u0063\u006f\u0070\u0079\u0069\u006e\u0067\u0020\u0074\u006f\u0020\u0025\u0073\u003a\u0020\u0025\u0076",_ff ,_beb );
    37  };continue ;};_ge .Run (_b .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_bd ),func (_eba *_dg .T ){_eba .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_de ,_ff );_bfd ,_fgc :=ComparePNGFiles (_de ,_ff );
    38  if _ef .IsNotExist (_fgc ){_eba .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_bfd {_eba .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");
    39  };});};});};func CopyFile (src ,dst string )error {_a ,_ed :=_ef .Open (src );if _ed !=nil {return _ed ;};defer _a .Close ();_be ,_ed :=_ef .Create (dst );if _ed !=nil {return _ed ;};defer _be .Close ();_ ,_ed =_c .Copy (_be ,_a );return _ed ;};var (ErrRenderNotSupported =_db .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");
    40  );