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 (_e "crypto/md5";_ffd "encoding/hex";_ag "errors";_dc "fmt";_dcd "github.com/unidoc/unipdf/v3/common";_db "github.com/unidoc/unipdf/v3/core";_ge "image";_ff "image/png";_b "io";_a "os";_f "os/exec";_gg "path/filepath";_ed "strings";_d "testing";);func ReadPNG (file string )(_ge .Image ,error ){_ggd ,_af :=_a .Open (file );if _af !=nil {return nil ,_af ;};defer _ggd .Close ();return _ff .Decode (_ggd );};func CompareImages (img1 ,img2 _ge .Image )(bool ,error ){_bd :=img1 .Bounds ();_fa :=0;for _bc :=0;_bc < _bd .Size ().X ;_bc ++{for _bg :=0;_bg < _bd .Size ().Y ;_bg ++{_gc ,_gf ,_bf ,_ :=img1 .At (_bc ,_bg ).RGBA ();_afc ,_ac ,_fe ,_ :=img2 .At (_bc ,_bg ).RGBA ();if _gc !=_afc ||_gf !=_ac ||_bf !=_fe {_fa ++;};};};_afg :=float64 (_fa )/float64 (_bd .Dx ()*_bd .Dy ());if _afg > 0.0001{_dc .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_afg ,_fa );return false ,nil ;};return true ,nil ;};func _gbg (_ae _db .PdfObject ,_ce map[int64 ]_db .PdfObject )error {switch _ffb :=_ae .(type ){case *_db .PdfIndirectObject :_df :=_ffb ;_gbg (_df .PdfObject ,_ce );case *_db .PdfObjectDictionary :_bca :=_ffb ;for _ ,_efe :=range _bca .Keys (){_efb :=_bca .Get (_efe );if _ffbe ,_ga :=_efb .(*_db .PdfObjectReference );_ga {_agd ,_fgb :=_ce [_ffbe .ObjectNumber ];if !_fgb {return _ag .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");};_bca .Set (_efe ,_agd );}else {_gbg (_efb ,_ce );};};case *_db .PdfObjectArray :_ebg :=_ffb ;for _bebb ,_ccbf :=range _ebg .Elements (){if _afe ,_bce :=_ccbf .(*_db .PdfObjectReference );_bce {_fgbb ,_gcb :=_ce [_afe .ObjectNumber ];if !_gcb {return _ag .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 (_bebb ,_fgbb );}else {_gbg (_ccbf ,_ce );};};};return nil ;};func RunRenderTest (t *_d .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_ggb :=_ed .TrimSuffix (_gg .Base (pdfPath ),_gg .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_beb *_d .T ){_gfd :=_gg .Join (outputDir ,_ggb );_eb :=_gfd +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _cc :=RenderPDFToPNGs (pdfPath ,0,_eb );_cc !=nil {_beb .Skip (_cc );};for _aab :=1;true ;_aab ++{_bgbf :=_dc .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_gfd ,_aab );_gfdc :=_gg .Join (baselineRenderPath ,_dc .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_ggb ,_aab ));if _ ,_bb :=_a .Stat (_bgbf );_bb !=nil {break ;};_beb .Logf ("\u0025\u0073",_gfdc );if _ ,_bee :=_a .Stat (_gfdc );_a .IsNotExist (_bee ){if saveBaseline {_beb .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_bgbf ,_gfdc );CopyFile (_bgbf ,_gfdc );continue ;};break ;};_beb .Run (_dc .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_aab ),func (_dec *_d .T ){_dec .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_bgbf ,_gfdc );_bcf ,_bdf :=ComparePNGFiles (_bgbf ,_gfdc );if _a .IsNotExist (_bdf ){_dec .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_bcf {_dec .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");};});};});};func HashFile (file string )(string ,error ){_fg ,_ec :=_a .Open (file );if _ec !=nil {return "",_ec ;};defer _fg .Close ();_ad :=_e .New ();if _ ,_ec =_b .Copy (_ad ,_fg );_ec !=nil {return "",_ec ;};return _ffd .EncodeToString (_ad .Sum (nil )),nil ;};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_dbg :=_f .LookPath ("\u0067\u0073");_dbg !=nil {return ErrRenderNotSupported ;};return _f .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_dc .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();};var (ErrRenderNotSupported =_ag .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 ]_db .PdfObject ,error ){_ece :=_db .NewParserFromString (rawpdf );_aga :=map[int64 ]_db .PdfObject {};for {_cg ,_ebc :=_ece .ParseIndirectObject ();if _ebc !=nil {if _ebc ==_b .EOF {break ;};return nil ,_ebc ;};switch _cd :=_cg .(type ){case *_db .PdfIndirectObject :_aga [_cd .ObjectNumber ]=_cg ;case *_db .PdfObjectStream :_aga [_cd .ObjectNumber ]=_cg ;};};for _ ,_ebce :=range _aga {_gbg (_ebce ,_aga );};return _aga ,nil ;};func CopyFile (src ,dst string )error {_c ,_de :=_a .Open (src );if _de !=nil {return _de ;};defer _c .Close ();_be ,_de :=_a .Create (dst );if _de !=nil {return _de ;};defer _be .Close ();_ ,_de =_b .Copy (_be ,_c );return _de ;};func CompareDictionariesDeep (d1 ,d2 *_db .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_dcd .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 ()));_dcd .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_cde :=range d1 .Keys (){if _cde =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;};_dff :=_db .TraceToDirectObject (d1 .Get (_cde ));_bcfa :=_db .TraceToDirectObject (d2 .Get (_cde ));if _dff ==nil {_dcd .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _bcfa ==nil {_dcd .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};switch _fd :=_dff .(type ){case *_db .PdfObjectDictionary :_da ,_fc :=_bcfa .(*_db .PdfObjectDictionary );if !_fc {_dcd .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_dff ,_bcfa );return false ;};if !CompareDictionariesDeep (_fd ,_da ){return false ;};continue ;case *_db .PdfObjectArray :_ba ,_aaa :=_bcfa .(*_db .PdfObjectArray );if !_aaa {_dcd .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");return false ;};if _fd .Len ()!=_ba .Len (){_dcd .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",_fd .Len (),_ba .Len ());return false ;};for _dce :=0;_dce < _fd .Len ();_dce ++{_cf :=_db .TraceToDirectObject (_fd .Get (_dce ));_ffa :=_db .TraceToDirectObject (_ba .Get (_dce ));if _edc ,_eca :=_cf .(*_db .PdfObjectDictionary );_eca {_add ,_geg :=_ffa .(*_db .PdfObjectDictionary );if !_geg {return false ;};if !CompareDictionariesDeep (_edc ,_add ){return false ;};}else {if _cf .WriteString ()!=_ffa .WriteString (){_dcd .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_cf .WriteString (),_ffa .WriteString ());return false ;};};};continue ;};if _dff .String ()!=_bcfa .String (){_dcd .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",_cde ,_dff .String (),_bcfa .String ());_dcd .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_dff ,_bcfa );_dcd .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_dff ,_bcfa );return false ;};};return true ;};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_bfc ,_aa :=HashFile (file1 );if _aa !=nil {return false ,_aa ;};_adg ,_aa :=HashFile (file2 );if _aa !=nil {return false ,_aa ;};if _bfc ==_adg {return true ,nil ;};_bgb ,_aa :=ReadPNG (file1 );if _aa !=nil {return false ,_aa ;};_ef ,_aa :=ReadPNG (file2 );if _aa !=nil {return false ,_aa ;};if _bgb .Bounds ()!=_ef .Bounds (){return false ,nil ;};return CompareImages (_bgb ,_ef );}; |