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/
|
|
|
|
|
2021-01-26 01:31:56 +00:00
|
|
|
package render ;import (_b "errors";_ce "fmt";_fg "github.com/adrg/sysfont";_gb "github.com/unidoc/unipdf/v3/common";_d "github.com/unidoc/unipdf/v3/contentstream";_f "github.com/unidoc/unipdf/v3/core";_g "github.com/unidoc/unipdf/v3/internal/license";_ca "github.com/unidoc/unipdf/v3/internal/transform";_cag "github.com/unidoc/unipdf/v3/model";_db "github.com/unidoc/unipdf/v3/render/internal/context";_bg "github.com/unidoc/unipdf/v3/render/internal/context/imagerender";_eb "image";_e "image/color";_bd "image/draw";_ac "image/jpeg";_cfb "image/png";_ab "os";_cf "path/filepath";_c "strings";);func (_de renderer )renderContentStream (_gcd _db .Context ,_afd string ,_fa *_cag .PdfPageResources )error {_fga ,_dd :=_d .NewContentStreamParser (_afd ).Parse ();if _dd !=nil {return _dd ;};_gab :=_gcd .TextState ();_dac :=map[string ]*_db .TextFont {};_gg :=_fg .NewFinder (&_fg .FinderOpts {Extensions :[]string {"\u002e\u0074\u0074\u0066","\u002e\u0074\u0074\u0063"}});_be :=_d .NewContentStreamProcessor (*_fga );_be .AddHandler (_d .HandlerConditionEnumAllOperands ,"",func (_df *_d .ContentStreamOperation ,_eaa _d .GraphicsState ,_cb *_cag .PdfPageResources )error {_gb .Log .Debug ("\u0050\u0072\u006f\u0063\u0065\u0073\u0073\u0069\u006e\u0067\u0020\u0025\u0073",_df .Operand );switch _df .Operand {case "\u0071":_gcd .Push ();case "\u0051":_gcd .Pop ();case "\u0063\u006d":if len (_df .Params )!=6{return _bc ;};_ae ,_gbb :=_f .GetNumbersAsFloat (_df .Params );if _gbb !=nil {return _gbb ;};_cg :=_ca .NewMatrix (_ae [0],_ae [1],_ae [2],_ae [3],_ae [4],_ae [5]);_gb .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",_cg );_gcd .SetMatrix (_gcd .Matrix ().Mult (_cg ));_cgd :=(_eaa .CTM .ScalingFactorX ()+_eaa .CTM .ScalingFactorY ())/2.0;_gcd .SetLineWidth (_cgd *_gcd .LineWidth ());case "\u0077":if len (_df .Params )!=1{return _bc ;};_bbg ,_ggg :=_f .GetNumbersAsFloat (_df .Params );if _ggg !=nil {return _ggg ;};_bdd :=(_eaa .CTM .ScalingFactorX ()+_eaa .CTM .ScalingFactorY ())/2.0;_gcd .SetLineWidth (_bdd *_bbg [0]);case "\u004a":if len (_df .Params )!=1{return _bc ;};_aee ,_bcb :=_f .GetIntVal (_df .Params [0]);if !_bcb {return _abg ;};switch _aee {case 0:_gcd .SetLineCap (_db .LineCapButt );case 1:_gcd .SetLineCap (_db .LineCapRound );case 2:_gcd .SetLineCap (_db .LineCapSquare );default:_gb .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",_aee );return _bc ;};case "\u006a":if len (_df .Params )!=1{return _bc ;};_ad ,_bcc :=_f .GetIntVal (_df .Params [0]);if !_bcc {return _abg ;};switch _ad {case 0:_gcd .SetLineJoin (_db .LineJoinBevel );case 1:_gcd .SetLineJoin (_db .LineJoinRound );case 2:_gcd .SetLineJoin (_db .LineJoinBevel );default:_gb .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",_ad );return _bc ;};case "\u004d":if len (_df .Params )!=1{return _bc ;};_acc ,_cgg :=_f .GetNumbersAsFloat (_df .Params );if _cgg !=nil {return _cgg ;};_ =_acc ;_gb .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 (_df .Params )!=2{return _bc ;};_ceg ,_dc :=_f .GetArray (_df .Params [0]);if !_dc {return _abg ;};_dgf ,_dc :=_f .GetIntVal (_df .Params [1]);if !_dc {return _abg ;};_gf ,_gd :=_f .GetNumbersAsFloat (_ceg .Elements ());if _gd !=nil {return _gd ;};_gcd .SetDash (_gf ...);_ =_dgf ;_gb .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":_gb .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":_gb .Log .Debug ("\u0046\u006c\u
|
2020-12-06 13:03:03 +00:00
|
|
|
|
2021-01-07 14:20:10 +00:00
|
|
|
// RenderToPath converts the specified PDF page into an image and saves the
|
|
|
|
// result at the specified location.
|
2021-01-26 01:31:56 +00:00
|
|
|
func (_af *ImageDevice )RenderToPath (page *_cag .PdfPage ,outputPath string )error {_eg ,_ace :=_af .Render (page );if _ace !=nil {return _ace ;};_dg :=_c .ToLower (_cf .Ext (outputPath ));if _dg ==""{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 _dg {case "\u002e\u0070\u006e\u0067":return _dfc (outputPath ,_eg );case "\u002e\u006a\u0070\u0067","\u002e\u006a\u0070e\u0067":return _dgab (outputPath ,_eg ,100);};return _ce .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",_dg );};
|
|
|
|
|
|
|
|
// ImageDevice is used to render PDF pages to image targets.
|
|
|
|
type ImageDevice struct{renderer };func (_bb renderer )renderPage (_da _db .Context ,_ec *_cag .PdfPage )error {_dad ,_aa :=_ec .GetAllContentStreams ();if _aa !=nil {return _aa ;};_da .Translate (0,float64 (_da .Height ()));_da .Scale (1,-1);_da .Push ();_da .SetRGBA (1,1,1,1);_da .DrawRectangle (0,0,float64 (_da .Width ()),float64 (_da .Height ()));_da .Fill ();_da .Pop ();_da .SetLineWidth (1.0);_da .SetRGBA (0,0,0,1);return _bb .renderContentStream (_da ,_dad ,_ec .Resources );};func _dgab (_gdd string ,_gga _eb .Image ,_ffg int )error {_cce ,_fac :=_ab .Create (_gdd );if _fac !=nil {return _fac ;};defer _cce .Close ();return _ac .Encode (_cce ,_gga ,&_ac .Options {Quality :_ffg });};type renderer struct{};func _dfc (_geg string ,_dgc _eb .Image )error {_fbd ,_dcg :=_ab .Create (_geg );if _dcg !=nil {return _dcg ;};defer _fbd .Close ();return _cfb .Encode (_fbd ,_dgc );};
|
2020-12-06 13:03:03 +00:00
|
|
|
|
|
|
|
// NewImageDevice returns a new image device.
|
2021-01-26 01:31:56 +00:00
|
|
|
func NewImageDevice ()*ImageDevice {const _eba ="r\u0065\u006e\u0064\u0065r.\u004ee\u0077\u0049\u006d\u0061\u0067e\u0044\u0065\u0076\u0069\u0063\u0065";_g .TrackUse (_eba );return &ImageDevice {};};var (_abg =_b .New ("\u0074\u0079p\u0065\u0020\u0063h\u0065\u0063\u006b\u0020\u0065\u0072\u0072\u006f\u0072");_bc =_b .New ("\u0072\u0061\u006e\u0067\u0065\u0020\u0063\u0068\u0065\u0063\u006b\u0020e\u0072\u0072\u006f\u0072"););func _aec (_fdc *_cag .Image ,_cge _e .Color )_eb .Image {_gee ,_gccg :=int (_fdc .Width ),int (_fdc .Height );_bbed :=_eb .NewRGBA (_eb .Rect (0,0,_gee ,_gccg ));for _fcca :=0;_fcca < _gccg ;_fcca ++{for _agfa :=0;_agfa < _gee ;_agfa ++{_ggac ,_eda :=_fdc .ColorAt (_agfa ,_fcca );if _eda !=nil {_gb .Log .Debug ("\u0057\u0041\u0052\u004e\u003a\u0020\u0063o\u0075\u006c\u0064\u0020\u006e\u006f\u0074\u0020\u0072\u0065\u0074\u0072\u0069\u0065v\u0065 \u0069\u006d\u0061\u0067\u0065\u0020m\u0061\u0073\u006b\u0020\u0076\u0061\u006cu\u0065\u0020\u0061\u0074\u0020\u0028\u0025\u0064\u002c\u0020\u0025\u0064\u0029\u002e\u0020\u004f\u0075\u0074\u0070\u0075\u0074\u0020\u006da\u0079\u0020\u0062\u0065\u0020\u0069\u006e\u0063\u006f\u0072\u0072\u0065\u0063t\u002e",_agfa ,_fcca );continue ;};_gfb ,_gbe ,_efg ,_ :=_ggac .RGBA ();var _efb _e .Color ;if _gfb +_gbe +_efg ==0{_efb =_ggac ;}else {_efb =_e .Transparent ;};_bbed .Set (_agfa ,_fcca ,_efb );};};return _bbed ;};
|
|
|
|
|
|
|
|
// Render converts the specified PDF page into an image and returns the result.
|
|
|
|
func (_cd *ImageDevice )Render (page *_cag .PdfPage )(_eb .Image ,error ){_fc ,_cc :=page .GetMediaBox ();if _cc !=nil {return nil ,_cc ;};_ba ,_gc :=_fc .Llx +_fc .Width (),_fc .Lly +_fc .Height ();_bac :=_bg .NewContext (int (_ba ),int (_gc ));if _gce :=_cd .renderPage (_bac ,page );_gce !=nil {return nil ,_gce ;};_ga :=_bac .Image ();if _gad :=page .CropBox ;_gad !=nil {_ea :=_eb .Rect (0,0,int (_gad .Width ()),int (_gad .Height ()));_fcc :=_eb .Pt (int (_gad .Llx ),int (_gc -_gad .Ury ));_fd :=_eb .NewRGBA (_ea );_bd .Draw (_fd ,_ea ,_ga ,_fcc ,_bd .Src );_ga =_fd ;};return _ga ,nil ;};
|