mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-26 13:48:55 +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 (_cg "crypto/md5";_cga "encoding/hex";_ff "errors";_e "fmt";_gc "github.com/unidoc/unipdf/v3/common";_d "github.com/unidoc/unipdf/v3/core";_gd "image";_ga "image/png";_cf "io";_cb "os";_gf "os/exec";_fg "path/filepath";_f "strings";_g "testing";);func ParseIndirectObjects (rawpdf string )(map[int64 ]_d .PdfObject ,error ){_dad :=_d .NewParserFromString (rawpdf );_fbd :=map[int64 ]_d .PdfObject {};for {_fd ,_bff :=_dad .ParseIndirectObject ();if _bff !=nil {if _bff ==_cf .EOF {break ;};return nil ,_bff ;};switch _df :=_fd .(type ){case *_d .PdfIndirectObject :_fbd [_df .ObjectNumber ]=_fd ;case *_d .PdfObjectStream :_fbd [_df .ObjectNumber ]=_fd ;};};for _ ,_fed :=range _fbd {_fbb (_fed ,_fbd );};return _fbd ,nil ;};func ComparePNGFiles (file1 ,file2 string )(bool ,error ){_dg ,_ad :=HashFile (file1 );if _ad !=nil {return false ,_ad ;};_cd ,_ad :=HashFile (file2 );if _ad !=nil {return false ,_ad ;};if _dg ==_cd {return true ,nil ;};_ee ,_ad :=ReadPNG (file1 );if _ad !=nil {return false ,_ad ;};_dbe ,_ad :=ReadPNG (file2 );if _ad !=nil {return false ,_ad ;};if _ee .Bounds ()!=_dbe .Bounds (){return false ,nil ;};return CompareImages (_ee ,_dbe );};func CompareImages (img1 ,img2 _gd .Image )(bool ,error ){_a :=img1 .Bounds ();_aa :=0;for _ag :=0;_ag < _a .Size ().X ;_ag ++{for _bb :=0;_bb < _a .Size ().Y ;_bb ++{_bd ,_agc ,_bg ,_ :=img1 .At (_ag ,_bb ).RGBA ();_gdb ,_ce ,_ac ,_ :=img2 .At (_ag ,_bb ).RGBA ();if _bd !=_gdb ||_agc !=_ce ||_bg !=_ac {_aa ++;};};};_bbc :=float64 (_aa )/float64 (_a .Dx ()*_a .Dy ());if _bbc > 0.0001{_e .Printf ("\u0064\u0069\u0066f \u0066\u0072\u0061\u0063\u0074\u0069\u006f\u006e\u003a\u0020\u0025\u0076\u0020\u0028\u0025\u0064\u0029\u000a",_bbc ,_aa );return false ,nil ;};return true ,nil ;};func RunRenderTest (t *_g .T ,pdfPath ,outputDir ,baselineRenderPath string ,saveBaseline bool ){_da :=_f .TrimSuffix (_fg .Base (pdfPath ),_fg .Ext (pdfPath ));t .Run ("\u0072\u0065\u006e\u0064\u0065\u0072",func (_eaf *_g .T ){_gee :=_fg .Join (outputDir ,_da );_ed :=_gee +"\u002d%\u0064\u002e\u0070\u006e\u0067";if _gad :=RenderPDFToPNGs (pdfPath ,0,_ed );_gad !=nil {_eaf .Skip (_gad );};for _fb :=1;true ;_fb ++{_bf :=_e .Sprintf ("\u0025s\u002d\u0025\u0064\u002e\u0070\u006eg",_gee ,_fb );_cde :=_fg .Join (baselineRenderPath ,_e .Sprintf ("\u0025\u0073\u002d\u0025\u0064\u005f\u0065\u0078\u0070\u002e\u0070\u006e\u0067",_da ,_fb ));if _ ,_af :=_cb .Stat (_bf );_af !=nil {break ;};_eaf .Logf ("\u0025\u0073",_cde );if _ ,_fe :=_cb .Stat (_cde );_cb .IsNotExist (_fe ){if saveBaseline {_eaf .Logf ("\u0043\u006fp\u0079\u0069\u006eg\u0020\u0025\u0073\u0020\u002d\u003e\u0020\u0025\u0073",_bf ,_cde );CopyFile (_bf ,_cde );continue ;};break ;};_eaf .Run (_e .Sprintf ("\u0070\u0061\u0067\u0065\u0025\u0064",_fb ),func (_fac *_g .T ){_fac .Logf ("\u0043o\u006dp\u0061\u0072\u0069\u006e\u0067 \u0025\u0073 \u0076\u0073\u0020\u0025\u0073",_bf ,_cde );_bfc ,_fgc :=ComparePNGFiles (_bf ,_cde );if _cb .IsNotExist (_fgc ){_fac .Fatal ("\u0069m\u0061g\u0065\u0020\u0066\u0069\u006ce\u0020\u006di\u0073\u0073\u0069\u006e\u0067");}else if !_bfc {_fac .Fatal ("\u0077\u0072\u006f\u006eg \u0070\u0061\u0067\u0065\u0020\u0072\u0065\u006e\u0064\u0065\u0072\u0065\u0064");};});};});};func CompareDictionariesDeep (d1 ,d2 *_d .PdfObjectDictionary )bool {if len (d1 .Keys ())!=len (d2 .Keys ()){_gc .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 ()));_gc .Log .Debug ("\u0057\u0061s\u0020\u0027\u0025s\u0027\u0020\u0076\u0073\u0020\u0027\u0025\u0073\u0027",d1 .WriteString (),d2 .WriteString ());return false ;};for _ ,_fdd :=range d1 .Keys (){if _fdd =="\u0050\u0061\u0072\u0065\u006e\u0074"{continue ;};_bgdb :=_d .TraceToDirectObject (d1 .Get (_fdd ));_eed :=_d .TraceToDirectObject (d2 .Get (_fdd ));if _bgdb ==nil {_gc .Log .Debug ("\u00761\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};if _eed ==nil {_gc .Log .Debug ("\u00762\u0020\u0069\u0073\u0020\u006e\u0069l");return false ;};switch _add :=_bgdb .(type ){case *_d .PdfObjectDictionary :_dfc ,_dada :=_eed .(*_d .PdfObjectDictionary );if !_dada {_gc .Log .Debug ("\u0054\u0079\u0070\u0065 m\u0069\u0073\u006d\u0061\u0074\u0063\u0068\u0020\u0025\u0054\u0020\u0076\u0073\u0020%\u0054",_bgdb ,_eed );return false ;};if !CompareDictionariesDeep (_add ,_dfc ){return false ;};continue ;case *_d .PdfObjectArray :_cef ,_cgfc :=_eed .(*_d .PdfObjectArray );if !_cgfc {_gc .Log .Debug ("\u00762\u0020n\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");return false ;};if _add .Len ()!=_cef .Len (){_gc .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",_add .Len (),_cef .Len ());return false ;};for _facc :=0;_facc < _add .Len ();_facc ++{_age :=_d .TraceToDirectObject (_add .Get (_facc ));_bce :=_d .TraceToDirectObject (_cef .Get (_facc ));if _gbc ,_fc :=_age .(*_d .PdfObjectDictionary );_fc {_ddd ,_ba :=_bce .(*_d .PdfObjectDictionary );if !_ba {return false ;};if !CompareDictionariesDeep (_gbc ,_ddd ){return false ;};}else {if _age .WriteString ()!=_bce .WriteString (){_gc .Log .Debug ("M\u0069\u0073\u006d\u0061tc\u0068 \u0027\u0025\u0073\u0027\u0020!\u003d\u0020\u0027\u0025\u0073\u0027",_age .WriteString (),_bce .WriteString ());return false ;};};};continue ;};if _bgdb .String ()!=_eed .String (){_gc .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",_fdd ,_bgdb .String (),_eed .String ());_gc .Log .Debug ("\u0046o\u0072 \u0027\u0025\u0054\u0027\u0020\u002d\u0020\u0027\u0025\u0054\u0027",_bgdb ,_eed );_gc .Log .Debug ("\u0046\u006f\u0072\u0020\u0027\u0025\u002b\u0076\u0027\u0020\u002d\u0020'\u0025\u002b\u0076\u0027",_bgdb ,_eed );return false ;};};return true ;};func ReadPNG (file string )(_gd .Image ,error ){_bc ,_bcb :=_cb .Open (file );if _bcb !=nil {return nil ,_bcb ;};defer _bc .Close ();return _ga .Decode (_bc );};func HashFile (file string )(string ,error ){_ge ,_fa :=_cb .Open (file );if _fa !=nil {return "",_fa ;};defer _ge .Close ();_eg :=_cg .New ();if _ ,_fa =_cf .Copy (_eg ,_ge );_fa !=nil {return "",_fa ;};return _cga .EncodeToString (_eg .Sum (nil )),nil ;};func CopyFile (src ,dst string )error {_ea ,_gb :=_cb .Open (src );if _gb !=nil {return _gb ;};defer _ea .Close ();_fga ,_gb :=_cb .Create (dst );if _gb !=nil {return _gb ;};defer _fga .Close ();_ ,_gb =_cf .Copy (_fga ,_ea );return _gb ;};func _fbb (_gfb _d .PdfObject ,_eaff map[int64 ]_d .PdfObject )error {switch _cca :=_gfb .(type ){case *_d .PdfIndirectObject :_ccc :=_cca ;_fbb (_ccc .PdfObject ,_eaff );case *_d .PdfObjectDictionary :_gfd :=_cca ;for _ ,_eab :=range _gfd .Keys (){_gec :=_gfd .Get (_eab );if _bcad ,_fdc :=_gec .(*_d .PdfObjectReference );_fdc {_ffb ,_gde :=_eaff [_bcad .ObjectNumber ];if !_gde {return _ff .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");};_gfd .Set (_eab ,_ffb );}else {_fbb (_gec ,_eaff );};};case *_d .PdfObjectArray :_aee :=_cca ;for _dd ,_cgf :=range _aee .Elements (){if _bcaf ,_dgf :=_cgf .(*_d .PdfObjectReference );_dgf {_cdb ,_eec :=_eaff [_bcaf .ObjectNumber ];if !_eec {return _ff .New ("r\u0065\u0066\u0065\u0072\u0065\u006ec\u0065\u0020\u0074\u006f\u0020\u006f\u0075\u0074\u0073i\u0064\u0065\u0020o\u0062j\u0065\u0063\u0074");};_aee .Set (_dd ,_cdb );}else {_fbb (_cgf ,_eaff );};};};return nil ;};func RenderPDFToPNGs (pdfPath string ,dpi int ,outpathTpl string )error {if dpi <=0{dpi =100;};if _ ,_gea :=_gf .LookPath ("\u0067\u0073");_gea !=nil {return ErrRenderNotSupported ;};return _gf .Command ("\u0067\u0073","\u002d\u0073\u0044\u0045\u0056\u0049\u0043\u0045\u003d\u0070\u006e\u0067a\u006c\u0070\u0068\u0061","\u002d\u006f",outpathTpl ,_e .Sprintf ("\u002d\u0072\u0025\u0064",dpi ),pdfPath ).Run ();};var (ErrRenderNotSupported =_ff .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");); |