unipdf/render/render.go

25 lines
31 KiB
Go
Raw Normal View History

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-12-06 13:03:03 +00:00
package render ;import (_f "errors";_e "fmt";_dce "github.com/adrg/sysfont";_df "github.com/unidoc/unipdf/v3/common";_ec "github.com/unidoc/unipdf/v3/contentstream";_dc "github.com/unidoc/unipdf/v3/core";_add "github.com/unidoc/unipdf/v3/internal/transform";_ad "github.com/unidoc/unipdf/v3/model";_da "github.com/unidoc/unipdf/v3/render/internal/context";_ca "github.com/unidoc/unipdf/v3/render/internal/context/imagerender";_c "image";_aa "image/draw";_d "image/jpeg";_fd "image/png";_fe "os";_g "path/filepath";_a "strings";);func _bc (_dde string ,_fead _c .Image )error {_gea ,_fdg :=_fe .Create (_dde );if _fdg !=nil {return _fdg ;};defer _gea .Close ();return _fd .Encode (_gea ,_fead );};
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-12-06 13:03:03 +00:00
func (_ef *ImageDevice )RenderToPath (page *_ad .PdfPage ,outputPath string )error {_fa ,_cgb :=_ef .Render (page );if _cgb !=nil {return _cgb ;};_dd :=_a .ToLower (_g .Ext (outputPath ));if _dd ==""{return _f .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 _dd {case "\u002e\u0070\u006e\u0067":return _bc (outputPath ,_fa );case "\u002e\u006a\u0070\u0067","\u002e\u006a\u0070e\u0067":return _ff (outputPath ,_fa ,100);};return _e .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",_dd );};
2020-10-12 14:17:59 +00:00
2020-10-19 10:58:10 +00:00
// ImageDevice is used to render PDF pages to image targets.
2020-12-06 13:03:03 +00:00
type ImageDevice struct{renderer };func (_ac renderer )renderContentStream (_eg _da .Context ,_eaa string ,_efe *_ad .PdfPageResources )error {_dfb ,_gd :=_ec .NewContentStreamParser (_eaa ).Parse ();if _gd !=nil {return _gd ;};_aba :=_eg .TextState ();_cd :=map[string ]*_da .TextFont {};_bcg :=_dce .NewFinder (&_dce .FinderOpts {Extensions :[]string {"\u002e\u0074\u0074\u0066","\u002e\u0074\u0074\u0063"}});_fed :=_ec .NewContentStreamProcessor (*_dfb );_fed .AddHandler (_ec .HandlerConditionEnumAllOperands ,"",func (_ga *_ec .ContentStreamOperation ,_aab _ec .GraphicsState ,_eag *_ad .PdfPageResources )error {_df .Log .Debug ("\u0050\u0072\u006f\u0063\u0065\u0073\u0073\u0069\u006e\u0067\u0020\u0025\u0073",_ga .Operand );switch _ga .Operand {case "\u0071":_eg .Push ();case "\u0051":_eg .Pop ();case "\u0063\u006d":if len (_ga .Params )!=6{return _cba ;};_daf ,_faa :=_dc .GetNumbersAsFloat (_ga .Params );if _faa !=nil {return _faa ;};_adcf :=_add .NewMatrix (_daf [0],_daf [1],_daf [2],_daf [3],_daf [4],_daf [5]);_df .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",_adcf );_eg .SetMatrix (_eg .Matrix ().Mult (_adcf ));_cc :=(_aab .CTM .ScalingFactorX ()+_aab .CTM .ScalingFactorY ())/2.0;_eg .SetLineWidth (_cc *_eg .LineWidth ());case "\u0077":if len (_ga .Params )!=1{return _cba ;};_fge ,_af :=_dc .GetNumbersAsFloat (_ga .Params );if _af !=nil {return _af ;};_dfe :=(_aab .CTM .ScalingFactorX ()+_aab .CTM .ScalingFactorY ())/2.0;_eg .SetLineWidth (_dfe *_fge [0]);case "\u004a":if len (_ga .Params )!=1{return _cba ;};_de ,_bd :=_dc .GetIntVal (_ga .Params [0]);if !_bd {return _gc ;};switch _de {case 0:_eg .SetLineCap (_da .LineCapButt );case 1:_eg .SetLineCap (_da .LineCapRound );case 2:_eg .SetLineCap (_da .LineCapSquare );default:_df .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",_de );return _cba ;};case "\u006a":if len (_ga .Params )!=1{return _cba ;};_eee ,_dfa :=_dc .GetIntVal (_ga .Params [0]);if !_dfa {return _gc ;};switch _eee {case 0:_eg .SetLineJoin (_da .LineJoinBevel );case 1:_eg .SetLineJoin (_da .LineJoinRound );case 2:_eg .SetLineJoin (_da .LineJoinBevel );default:_df .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",_eee );return _cba ;};case "\u004d":if len (_ga .Params )!=1{return _cba ;};_cce ,_ggd :=_dc .GetNumbersAsFloat (_ga .Params );if _ggd !=nil {return _ggd ;};_ =_cce ;_df .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 (_ga .Params )!=2{return _cba ;};_bg ,_eea :=_dc .GetArray (_ga .Params [0]);if !_eea {return _gc ;};_cgg ,_eea :=_dc .GetIntVal (_ga .Params [1]);if !_eea {return _gc ;};_fad ,_aca :=_dc .GetNumbersAsFloat (_bg .Elements ());if _aca !=nil {return _aca ;};_eg .SetDash (_fad ...);_ =_cgg ;_df .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":_df .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":_df .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\u0070\u0070\u006f\u0072\u0074\u0065\u0064");case "\u0067\u0073":if len (_ga .Params )!=1{return _cba ;};_gca ,_cga :=_dc .GetName (_ga .Params [0]);if !_cga {return _gc ;};if _gca ==nil {return _cba ;};_ffc ,_cga :=_eag .GetExtGState (*_gca );if !_cga {_df .Log .Debug ("\u0045\u0052\u0052OR\u003a\u0020\u0063\u006f\u0075\u006c\u0064\u0020\u006eo\u0074 \u0066i\u006ed\u0020\u0072\u0065\u0073\u006f\u0075\u0072\u0063\u
// Render converts the specified PDF page into an image and returns the result.
func (_fg *ImageDevice )Render (page *_ad .PdfPage )(_c .Image ,error ){_gg ,_aae :=page .GetMediaBox ();if _aae !=nil {return nil ,_aae ;};_adc ,_eb :=_gg .Llx +_gg .Width (),_gg .Lly +_gg .Height ();_ee :=_ca .NewContext (int (_adc ),int (_eb ));if _bf :=_fg .renderPage (_ee ,page );_bf !=nil {return nil ,_bf ;};_fea :=_ee .Image ();if _ge :=page .CropBox ;_ge !=nil {_dcg :=_c .Rect (0,0,int (_ge .Width ()),int (_ge .Height ()));_dcc :=_c .Pt (int (_ge .Llx ),int (_eb -_ge .Ury ));_cg :=_c .NewRGBA (_dcg );_aa .Draw (_cg ,_dcg ,_fea ,_dcc ,_aa .Src );_fea =_cg ;};return _fea ,nil ;};var (_gc =_f .New ("\u0074\u0079p\u0065\u0020\u0063h\u0065\u0063\u006b\u0020\u0065\u0072\u0072\u006f\u0072");_cba =_f .New ("\u0072\u0061\u006e\u0067\u0065\u0020\u0063\u0068\u0065\u0063\u006b\u0020e\u0072\u0072\u006f\u0072"););type renderer struct{};func _ff (_geb string ,_cb _c .Image ,_cgd int )error {_ba ,_cae :=_fe .Create (_geb );if _cae !=nil {return _cae ;};defer _ba .Close ();return _d .Encode (_ba ,_cb ,&_d .Options {Quality :_cgd });};func (_ab renderer )renderPage (_cbd _da .Context ,_bb *_ad .PdfPage )error {_ea ,_ebd :=_bb .GetAllContentStreams ();if _ebd !=nil {return _ebd ;};_cbd .Translate (0,float64 (_cbd .Height ()));_cbd .Scale (1,-1);_cbd .Push ();_cbd .SetRGBA (1,1,1,1);_cbd .DrawRectangle (0,0,float64 (_cbd .Width ()),float64 (_cbd .Height ()));_cbd .Fill ();_cbd .Pop ();_cbd .SetLineWidth (1.0);_cbd .SetRGBA (0,0,0,1);return _ab .renderContentStream (_cbd ,_ea ,_bb .Resources );};
// NewImageDevice returns a new image device.
func NewImageDevice ()*ImageDevice {return &ImageDevice {}};