2020-08-27 21:45:09 +00:00
|
|
|
//
|
|
|
|
// 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/
|
|
|
|
|
2020-10-12 14:17:59 +00:00
|
|
|
package render ;import (_b "errors";_ac "fmt";_ab "github.com/adrg/sysfont";_ba "github.com/unidoc/unipdf/v3/common";_d "github.com/unidoc/unipdf/v3/contentstream";_ae "github.com/unidoc/unipdf/v3/core";_ec "github.com/unidoc/unipdf/v3/internal/transform";_gg "github.com/unidoc/unipdf/v3/model";_da "github.com/unidoc/unipdf/v3/render/internal/context";_dd "github.com/unidoc/unipdf/v3/render/internal/context/imagerender";_g "image";_cc "image/draw";_ad "image/jpeg";_e "image/png";_a "os";_fd "path/filepath";_c "strings";);func (_ccf renderer )renderPage (_cgg _da .Context ,_bg *_gg .PdfPage )error {_fc ,_cee :=_bg .GetAllContentStreams ();if _cee !=nil {return _cee ;};_cgg .Translate (0,float64 (_cgg .Height ()));_cgg .Scale (1,-1);_cgg .Push ();_cgg .SetRGBA (1,1,1,1);_cgg .DrawRectangle (0,0,float64 (_cgg .Width ()),float64 (_cgg .Height ()));_cgg .Fill ();_cgg .Pop ();_cgg .SetLineWidth (1.0);_cgg .SetRGBA (0,0,0,1);return _ccf .renderContentStream (_cgg ,_fc ,_bg .Resources );};func _eg (_aec string ,_dc _g .Image ,_cgc int )error {_bd ,_abb :=_a .Create (_aec );if _abb !=nil {return _abb ;};defer _bd .Close ();return _ad .Encode (_bd ,_dc ,&_ad .Options {Quality :_cgc });};
|
2020-09-28 23:18:17 +00:00
|
|
|
|
|
|
|
// ImageDevice is used to render PDF pages to image targets.
|
2020-10-12 14:17:59 +00:00
|
|
|
type ImageDevice struct{renderer };var (_ga =_b .New ("\u0074\u0079p\u0065\u0020\u0063h\u0065\u0063\u006b\u0020\u0065\u0072\u0072\u006f\u0072");_fbf =_b .New ("\u0072\u0061\u006e\u0067\u0065\u0020\u0063\u0068\u0065\u0063\u006b\u0020e\u0072\u0072\u006f\u0072"););func _ag (_ff string ,_ce _g .Image )error {_dg ,_db :=_a .Create (_ff );if _db !=nil {return _db ;};defer _dg .Close ();return _e .Encode (_dg ,_ce );};type renderer struct{};func (_cb renderer )renderContentStream (_bf _da .Context ,_dda string ,_gac *_gg .PdfPageResources )error {_egc ,_ecb :=_d .NewContentStreamParser (_dda ).Parse ();if _ecb !=nil {return _ecb ;};_bce :=_bf .TextState ();_be :=map[string ]*_da .TextFont {};_acf :=_ab .NewFinder (&_ab .FinderOpts {Extensions :[]string {"\u002e\u0074\u0074\u0066","\u002e\u0074\u0074\u0063"}});_fdb :=_d .NewContentStreamProcessor (*_egc );_fdb .AddHandler (_d .HandlerConditionEnumAllOperands ,"",func (_bad *_d .ContentStreamOperation ,_aba _d .GraphicsState ,_dcb *_gg .PdfPageResources )error {_ba .Log .Debug ("\u0050\u0072\u006f\u0063\u0065\u0073\u0073\u0069\u006e\u0067\u0020\u0025\u0073",_bad .Operand );switch _bad .Operand {case "\u0071":_bf .Push ();case "\u0051":_bf .Pop ();case "\u0063\u006d":if len (_bad .Params )!=6{return _fbf ;};_eb ,_acd :=_ae .GetNumbersAsFloat (_bad .Params );if _acd !=nil {return _acd ;};_ggc :=_ec .NewMatrix (_eb [0],_eb [1],_eb [2],_eb [3],_eb [4],_eb [5]);_ba .Log .Debug ("\u0047\u0072\u0061\u0070\u0068\u0069\u0063\u0073\u0020\u0073\u0074a\u0074\u0065\u0020\u006d\u0061\u0074\u0072\u0069\u0078\u003a \u0025\u002b\u0076",_ggc );_bf .SetMatrix (_bf .Matrix ().Mult (_ggc ));_fbc :=(_aba .CTM .ScalingFactorX ()+_aba .CTM .ScalingFactorY ())/2.0;_bf .SetLineWidth (_fbc *_bf .LineWidth ());case "\u0077":if len (_bad .Params )!=1{return _fbf ;};_bcc ,_bea :=_ae .GetNumbersAsFloat (_bad .Params );if _bea !=nil {return _bea ;};_ceb :=(_aba .CTM .ScalingFactorX ()+_aba .CTM .ScalingFactorY ())/2.0;_bf .SetLineWidth (_ceb *_bcc [0]);case "\u004a":if len (_bad .Params )!=1{return _fbf ;};_ede ,_dcf :=_ae .GetIntVal (_bad .Params [0]);if !_dcf {return _ga ;};switch _ede {case 0:_bf .SetLineCap (_da .LineCapButt );case 1:_bf .SetLineCap (_da .LineCapRound );case 2:_bf .SetLineCap (_da .LineCapSquare );default:_ba .Log .Debug ("\u0049\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u006c\u0069\u006ee\u0020\u0063\u0061\u0070\u0020\u0073\u0074\u0079\u006c\u0065:\u0020\u0025\u0064",_ede );return _fbf ;};case "\u006a":if len (_bad .Params )!=1{return _fbf ;};_ffd ,_bag :=_ae .GetIntVal (_bad .Params [0]);if !_bag {return _ga ;};switch _ffd {case 0:_bf .SetLineJoin (_da .LineJoinBevel );case 1:_bf .SetLineJoin (_da .LineJoinRound );case 2:_bf .SetLineJoin (_da .LineJoinBevel );default:_ba .Log .Debug ("I\u006e\u0076\u0061\u006c\u0069\u0064 \u006c\u0069\u006e\u0065\u0020\u006a\u006f\u0069\u006e \u0073\u0074\u0079l\u0065:\u0020\u0025\u0064",_ffd );return _fbf ;};case "\u004d":if len (_bad .Params )!=1{return _fbf ;};_cef ,_cgcd :=_ae .GetNumbersAsFloat (_bad .Params );if _cgcd !=nil {return _cgcd ;};_ =_cef ;_ba .Log .Debug ("\u004di\u0074\u0065\u0072\u0020l\u0069\u006d\u0069\u0074\u0020n\u006ft\u0020s\u0075\u0070\u0070\u006f\u0072\u0074\u0065d");case "\u0064":if len (_bad .Params )!=2{return _fbf ;};_efc ,_bge :=_ae .GetArray (_bad .Params [0]);if !_bge {return _ga ;};_ddd ,_bge :=_ae .GetIntVal (_bad .Params [1]);if !_bge {return _ga ;};_cf ,_fcd :=_ae .GetNumbersAsFloat (_efc .Elements ());if _fcd !=nil {return _fcd ;};_bf .SetDash (_cf ...);_ =_ddd ;_ba .Log .Debug ("\u004c\u0069n\u0065\u0020\u0064\u0061\u0073\u0068\u0020\u0070\u0068\u0061\u0073\u0065\u0020\u006e\u006f\u0074\u0020\u0073\u0075\u0070\u0070\u006frt\u0065\u0064");case "\u0072\u0069":_ba .Log .Debug ("\u0052\u0065\u006e\u0064\u0065\u0072\u0069\u006e\u0067\u0020i\u006e\u0074\u0065\u006e\u0074\u0020\u006eo\u0074\u0020\u0073\u0075\u0070\u0070\u006f\u0072\u0074\u0065\u0064");case "\u0069":_ba .Log .Debug ("\u0046\u006c\u0061\u0074\u006e\u0065\u0073\u0073\u0020\u0074\u006f\u006c\u0065\u0072\u0061n\u0063e\u0020\u006e\u006f\u0074\u0020\u0073\u0075\
|
2020-10-05 19:28:24 +00:00
|
|
|
|
|
|
|
// RenderToPath converts the specified PDF page into an image and saves the
|
|
|
|
// result at the specified location.
|
2020-10-12 14:17:59 +00:00
|
|
|
func (_af *ImageDevice )RenderToPath (page *_gg .PdfPage ,outputPath string )error {_gb ,_bc :=_af .Render (page );if _bc !=nil {return _bc ;};_cg :=_c .ToLower (_fd .Ext (outputPath ));if _cg ==""{return _b .New ("\u0063\u006ful\u0064\u0020\u006eo\u0074\u0020\u0072\u0065cog\u006eiz\u0065\u0020\u006f\u0075\u0074\u0070\u0075t \u0066\u0069\u006c\u0065\u0020\u0074\u0079p\u0065");};switch _cg {case "\u002e\u0070\u006e\u0067":return _ag (outputPath ,_gb );case "\u002e\u006a\u0070\u0067","\u002e\u006a\u0070e\u0067":return _eg (outputPath ,_gb ,100);};return _ac .Errorf ("\u0075\u006e\u0072\u0065\u0063\u006fg\u006e\u0069\u007a\u0065\u0064\u0020\u006f\u0075\u0074\u0070\u0075\u0074\u0020f\u0069\u006c\u0065\u0020\u0074\u0079\u0070e\u003a\u0020\u0025\u0073",_cg );};
|
|
|
|
|
|
|
|
// NewImageDevice returns a new image device.
|
|
|
|
func NewImageDevice ()*ImageDevice {return &ImageDevice {}};
|
|
|
|
|
|
|
|
// Render converts the specified PDF page into an image and returns the result.
|
|
|
|
func (_fb *ImageDevice )Render (page *_gg .PdfPage )(_g .Image ,error ){_ca ,_fe :=page .GetMediaBox ();if _fe !=nil {return nil ,_fe ;};_aee ,_cd :=_ca .Llx +_ca .Width (),_ca .Lly +_ca .Height ();_bab :=_dd .NewContext (int (_aee ),int (_cd ));if _gd :=_fb .renderPage (_bab ,page );_gd !=nil {return nil ,_gd ;};_ef :=_bab .Image ();if _fg :=page .CropBox ;_fg !=nil {_cac :=_g .Rect (0,0,int (_fg .Width ()),int (_fg .Height ()));_ed :=_g .Pt (int (_fg .Llx ),int (_cd -_fg .Ury ));_abd :=_g .NewRGBA (_cac );_cc .Draw (_abd ,_cac ,_ef ,_ed ,_cc .Src );_ef =_abd ;};return _ef ,nil ;};
|