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-11-11 18:48:37 +00:00
package render ;import (_a "errors";_g "fmt";_dg "github.com/adrg/sysfont";_e "github.com/unidoc/unipdf/v3/common";_ac "github.com/unidoc/unipdf/v3/contentstream";_be "github.com/unidoc/unipdf/v3/core";_cc "github.com/unidoc/unipdf/v3/internal/transform";_bb "github.com/unidoc/unipdf/v3/model";_fg "github.com/unidoc/unipdf/v3/render/internal/context";_aec "github.com/unidoc/unipdf/v3/render/internal/context/imagerender";_ca "image";_d "image/draw";_fe "image/jpeg";_cf "image/png";_ae "os";_c "path/filepath";_f "strings";);var (_dfa =_a .New ("\u0074\u0079p\u0065\u0020\u0063h\u0065\u0063\u006b\u0020\u0065\u0072\u0072\u006f\u0072");_ag =_a .New ("\u0072\u0061\u006e\u0067\u0065\u0020\u0063\u0068\u0065\u0063\u006b\u0020e\u0072\u0072\u006f\u0072"););func (_gc renderer )renderPage (_efg _fg .Context ,_acd *_bb .PdfPage )error {_gg ,_beb :=_acd .GetAllContentStreams ();if _beb !=nil {return _beb ;};_efg .Translate (0,float64 (_efg .Height ()));_efg .Scale (1,-1);_efg .Push ();_efg .SetRGBA (1,1,1,1);_efg .DrawRectangle (0,0,float64 (_efg .Width ()),float64 (_efg .Height ()));_efg .Fill ();_efg .Pop ();_efg .SetLineWidth (1.0);_efg .SetRGBA (0,0,0,1);return _gc .renderContentStream (_efg ,_gg ,_acd .Resources );};
2020-09-28 23:18:17 +00:00
2020-10-19 10:58:10 +00:00
// Render converts the specified PDF page into an image and returns the result.
2020-11-11 18:48:37 +00:00
func (_bbf *ImageDevice )Render (page *_bb .PdfPage )(_ca .Image ,error ){_cg ,_ga :=page .GetMediaBox ();if _ga !=nil {return nil ,_ga ;};_aeg ,_dgg :=_cg .Llx +_cg .Width (),_cg .Lly +_cg .Height ();_acg :=_aec .NewContext (int (_aeg ),int (_dgg ));if _ge :=_bbf .renderPage (_acg ,page );_ge !=nil {return nil ,_ge ;};_ee :=_acg .Image ();if _bd :=page .CropBox ;_bd !=nil {_cfg :=_ca .Rect (0,0,int (_bd .Width ()),int (_bd .Height ()));_ad :=_ca .Pt (int (_bd .Llx ),int (_dgg -_bd .Ury ));_db :=_ca .NewRGBA (_cfg );_d .Draw (_db ,_cfg ,_ee ,_ad ,_d .Src );_ee =_db ;};return _ee ,nil ;};func (_caa renderer )renderContentStream (_aaa _fg .Context ,_cce string ,_cb *_bb .PdfPageResources )error {_fge ,_efgg :=_ac .NewContentStreamParser (_cce ).Parse ();if _efgg !=nil {return _efgg ;};_gb :=_aaa .TextState ();_gca :=map[string ]*_fg .TextFont {};_eb :=_dg .NewFinder (&_dg .FinderOpts {Extensions :[]string {"\u002e\u0074\u0074\u0066","\u002e\u0074\u0074\u0063"}});_fc :=_ac .NewContentStreamProcessor (*_fge );_fc .AddHandler (_ac .HandlerConditionEnumAllOperands ,"",func (_dd *_ac .ContentStreamOperation ,_bbg _ac .GraphicsState ,_bbb *_bb .PdfPageResources )error {_e .Log .Debug ("\u0050\u0072\u006f\u0063\u0065\u0073\u0073\u0069\u006e\u0067\u0020\u0025\u0073",_dd .Operand );switch _dd .Operand {case "\u0071":_aaa .Push ();case "\u0051":_aaa .Pop ();case "\u0063\u006d":if len (_dd .Params )!=6{return _ag ;};_da ,_bcf :=_be .GetNumbersAsFloat (_dd .Params );if _bcf !=nil {return _bcf ;};_ed :=_cc .NewMatrix (_da [0],_da [1],_da [2],_da [3],_da [4],_da [5]);_e .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",_ed );_aaa .SetMatrix (_aaa .Matrix ().Mult (_ed ));_aaag :=(_bbg .CTM .ScalingFactorX ()+_bbg .CTM .ScalingFactorY ())/2.0;_aaa .SetLineWidth (_aaag *_aaa .LineWidth ());case "\u0077":if len (_dd .Params )!=1{return _ag ;};_bfe ,_bcg :=_be .GetNumbersAsFloat (_dd .Params );if _bcg !=nil {return _bcg ;};_bfc :=(_bbg .CTM .ScalingFactorX ()+_bbg .CTM .ScalingFactorY ())/2.0;_aaa .SetLineWidth (_bfc *_bfe [0]);case "\u004a":if len (_dd .Params )!=1{return _ag ;};_edg ,_dc :=_be .GetIntVal (_dd .Params [0]);if !_dc {return _dfa ;};switch _edg {case 0:_aaa .SetLineCap (_fg .LineCapButt );case 1:_aaa .SetLineCap (_fg .LineCapRound );case 2:_aaa .SetLineCap (_fg .LineCapSquare );default:_e .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",_edg );return _ag ;};case "\u006a":if len (_dd .Params )!=1{return _ag ;};_ce ,_fgc :=_be .GetIntVal (_dd .Params [0]);if !_fgc {return _dfa ;};switch _ce {case 0:_aaa .SetLineJoin (_fg .LineJoinBevel );case 1:_aaa .SetLineJoin (_fg .LineJoinRound );case 2:_aaa .SetLineJoin (_fg .LineJoinBevel );default:_e .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",_ce );return _ag ;};case "\u004d":if len (_dd .Params )!=1{return _ag ;};_efa ,_ff :=_be .GetNumbersAsFloat (_dd .Params );if _ff !=nil {return _ff ;};_ =_efa ;_e .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 (_dd .Params )!=2{return _ag ;};_cad ,_eeb :=_be .GetArray (_dd .Params [0]);if !_eeb {return _dfa ;};_ccf ,_eeb :=_be .GetIntVal (_dd .Params [1]);if !_eeb {return _dfa ;};_fgb ,_dga :=_be .GetNumbersAsFloat (_cad .Elements ());if _dga !=nil {return _dga ;};_aaa .SetDash (_fgb ...);_ =_ccf ;_e .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":_e .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":_e .Log .Debug ("\u
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-11-11 18:48:37 +00:00
func (_dgb *ImageDevice )RenderToPath (page *_bb .PdfPage ,outputPath string )error {_bf ,_ec :=_dgb .Render (page );if _ec !=nil {return _ec ;};_bc :=_f .ToLower (_c .Ext (outputPath ));if _bc ==""{return _a .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 _bc {case "\u002e\u0070\u006e\u0067":return _eee (outputPath ,_bf );case "\u002e\u006a\u0070\u0067","\u002e\u006a\u0070e\u0067":return _bg (outputPath ,_bf ,100);};return _g .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",_bc );};func _eee (_aa string ,_gad _ca .Image )error {_ab ,_cfc :=_ae .Create (_aa );if _cfc !=nil {return _cfc ;};defer _ab .Close ();return _cf .Encode (_ab ,_gad );};
2020-10-12 14:17:59 +00:00
// NewImageDevice returns a new image device.
func NewImageDevice ()*ImageDevice {return &ImageDevice {}};
2020-10-19 10:58:10 +00:00
// ImageDevice is used to render PDF pages to image targets.
2020-11-11 18:48:37 +00:00
type ImageDevice struct{renderer };type renderer struct{};