mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-26 13:48:55 +08:00
prepare release
This commit is contained in:
parent
13c908a7c9
commit
9107a86674
File diff suppressed because one or more lines are too long
124
common/common.go
124
common/common.go
@ -10,93 +10,93 @@
|
||||
// terms that can be accessed at https://unidoc.io/eula/
|
||||
|
||||
// Package common contains common properties used by the subpackages.
|
||||
package common ;import (_b "fmt";_fc "io";_f "os";_c "path/filepath";_e "runtime";_g "time";);
|
||||
package common ;import (_f "fmt";_ag "io";_cc "os";_c "path/filepath";_a "runtime";_e "time";);const _fcf =22;
|
||||
|
||||
// Warning does nothing for dummy logger.
|
||||
func (DummyLogger )Warning (format string ,args ...interface{}){};
|
||||
|
||||
// Warning logs warning message.
|
||||
func (_df WriterLogger )Warning (format string ,args ...interface{}){if _df .LogLevel >=LogLevelWarning {_bgd :="\u005b\u0057\u0041\u0052\u004e\u0049\u004e\u0047\u005d\u0020";_df .logToWriter (_df .Output ,_bgd ,format ,args ...);};};
|
||||
// Trace does nothing for dummy logger.
|
||||
func (DummyLogger )Trace (format string ,args ...interface{}){};
|
||||
|
||||
// Trace logs trace message.
|
||||
func (_cf ConsoleLogger )Trace (format string ,args ...interface{}){if _cf .LogLevel >=LogLevelTrace {_dac :="\u005b\u0054\u0052\u0041\u0043\u0045\u005d\u0020";_cf .output (_f .Stdout ,_dac ,format ,args ...);};};const _ad =9;
|
||||
func (_aea WriterLogger )Trace (format string ,args ...interface{}){if _aea .LogLevel >=LogLevelTrace {_fbg :="\u005b\u0054\u0052\u0041\u0043\u0045\u005d\u0020";_aea .logToWriter (_aea .Output ,_fbg ,format ,args ...);};};const _fce =20;func (_cd ConsoleLogger )output (_fc _ag .Writer ,_gbc string ,_efa string ,_ea ...interface{}){_cb (_fc ,_gbc ,_efa ,_ea ...);};
|
||||
|
||||
// Error logs error message.
|
||||
func (_bgc ConsoleLogger )Error (format string ,args ...interface{}){if _bgc .LogLevel >=LogLevelError {_fb :="\u005b\u0045\u0052\u0052\u004f\u0052\u005d\u0020";_bgc .output (_f .Stdout ,_fb ,format ,args ...);};};
|
||||
|
||||
// Error does nothing for dummy logger.
|
||||
func (DummyLogger )Error (format string ,args ...interface{}){};
|
||||
|
||||
// Info does nothing for dummy logger.
|
||||
func (DummyLogger )Info (format string ,args ...interface{}){};
|
||||
|
||||
// Warning logs warning message.
|
||||
func (_bac ConsoleLogger )Warning (format string ,args ...interface{}){if _bac .LogLevel >=LogLevelWarning {_fcb :="\u005b\u0057\u0041\u0052\u004e\u0049\u004e\u0047\u005d\u0020";_bac .output (_f .Stdout ,_fcb ,format ,args ...);};};
|
||||
|
||||
// LogLevel is the verbosity level for logging.
|
||||
type LogLevel int ;func _fbb (_gaac _fc .Writer ,_bed string ,_bdd string ,_dc ...interface{}){_ ,_edf ,_ebe ,_cfb :=_e .Caller (3);if !_cfb {_edf ="\u003f\u003f\u003f";_ebe =0;}else {_edf =_c .Base (_edf );};_gfe :=_b .Sprintf ("\u0025s\u0020\u0025\u0073\u003a\u0025\u0064 ",_bed ,_edf ,_ebe )+_bdd +"\u000a";_b .Fprintf (_gaac ,_gfe ,_dc ...);};
|
||||
|
||||
// NewWriterLogger creates new 'writer' logger.
|
||||
func NewWriterLogger (logLevel LogLevel ,writer _fc .Writer )*WriterLogger {_dg :=WriterLogger {Output :writer ,LogLevel :logLevel };return &_dg ;};func UtcTimeFormat (t _g .Time )string {return t .Format (_cag )+"\u0020\u0055\u0054\u0043"};
|
||||
// SetLogger sets 'logger' to be used by the unidoc unipdf library.
|
||||
func SetLogger (logger Logger ){Log =logger };
|
||||
|
||||
// DummyLogger does nothing.
|
||||
type DummyLogger struct{};
|
||||
|
||||
// Notice logs notice message.
|
||||
func (_ab WriterLogger )Notice (format string ,args ...interface{}){if _ab .LogLevel >=LogLevelNotice {_fde :="\u005bN\u004f\u0054\u0049\u0043\u0045\u005d ";_ab .logToWriter (_ab .Output ,_fde ,format ,args ...);};};
|
||||
// LogLevel is the verbosity level for logging.
|
||||
type LogLevel int ;func UtcTimeFormat (t _e .Time )string {return t .Format (_fgc )+"\u0020\u0055\u0054\u0043"};const _fgc ="\u0032\u0020\u004aan\u0075\u0061\u0072\u0079\u0020\u0032\u0030\u0030\u0036\u0020\u0061\u0074\u0020\u0031\u0035\u003a\u0030\u0034";
|
||||
|
||||
// NewConsoleLogger creates new console logger.
|
||||
func NewConsoleLogger (logLevel LogLevel )*ConsoleLogger {return &ConsoleLogger {LogLevel :logLevel }};const _aed =8;
|
||||
// Debug logs debug message.
|
||||
func (_agd WriterLogger )Debug (format string ,args ...interface{}){if _agd .LogLevel >=LogLevelDebug {_ee :="\u005b\u0044\u0045\u0042\u0055\u0047\u005d\u0020";_agd .logToWriter (_agd .Output ,_ee ,format ,args ...);};};
|
||||
|
||||
// Trace logs trace message.
|
||||
func (_fbf WriterLogger )Trace (format string ,args ...interface{}){if _fbf .LogLevel >=LogLevelTrace {_ae :="\u005b\u0054\u0052\u0041\u0043\u0045\u005d\u0020";_fbf .logToWriter (_fbf .Output ,_ae ,format ,args ...);};};
|
||||
|
||||
// Notice does nothing for dummy logger.
|
||||
func (DummyLogger )Notice (format string ,args ...interface{}){};
|
||||
|
||||
// SetLogger sets 'logger' to be used by the unidoc unipdf library.
|
||||
func SetLogger (logger Logger ){Log =logger };const (LogLevelTrace LogLevel =5;LogLevelDebug LogLevel =4;LogLevelInfo LogLevel =3;LogLevelNotice LogLevel =2;LogLevelWarning LogLevel =1;LogLevelError LogLevel =0;);
|
||||
|
||||
// Debug logs debug message.
|
||||
func (_fd ConsoleLogger )Debug (format string ,args ...interface{}){if _fd .LogLevel >=LogLevelDebug {_db :="\u005b\u0044\u0045\u0042\u0055\u0047\u005d\u0020";_fd .output (_f .Stdout ,_db ,format ,args ...);};};
|
||||
|
||||
// IsLogLevel returns true if log level is greater or equal than `level`.
|
||||
// Can be used to avoid resource intensive calls to loggers.
|
||||
func (_bfgf WriterLogger )IsLogLevel (level LogLevel )bool {return _bfgf .LogLevel >=level };const _dbg =2020;
|
||||
func (_gf ConsoleLogger )Trace (format string ,args ...interface{}){if _gf .LogLevel >=LogLevelTrace {_agf :="\u005b\u0054\u0052\u0041\u0043\u0045\u005d\u0020";_gf .output (_cc .Stdout ,_agf ,format ,args ...);};};
|
||||
|
||||
// Logger is the interface used for logging in the unipdf package.
|
||||
type Logger interface{Error (_d string ,_bb ...interface{});Warning (_ga string ,_da ...interface{});Notice (_fa string ,_eg ...interface{});Info (_fg string ,_bg ...interface{});Debug (_cb string ,_ba ...interface{});Trace (_bd string ,_bf ...interface{});IsLogLevel (_eb LogLevel )bool ;};
|
||||
|
||||
// Info logs info message.
|
||||
func (_ge WriterLogger )Info (format string ,args ...interface{}){if _ge .LogLevel >=LogLevelInfo {_de :="\u005bI\u004e\u0046\u004f\u005d\u0020";_ge .logToWriter (_ge .Output ,_de ,format ,args ...);};};
|
||||
|
||||
// Debug logs debug message.
|
||||
func (_fe WriterLogger )Debug (format string ,args ...interface{}){if _fe .LogLevel >=LogLevelDebug {_gd :="\u005b\u0044\u0045\u0042\u0055\u0047\u005d\u0020";_fe .logToWriter (_fe .Output ,_gd ,format ,args ...);};};const _dge =50;
|
||||
type Logger interface{Error (_fa string ,_ed ...interface{});Warning (_ef string ,_g ...interface{});Notice (_cg string ,_bc ...interface{});Info (_ba string ,_be ...interface{});Debug (_gc string ,_gb ...interface{});Trace (_beg string ,_fae ...interface{});IsLogLevel (_d LogLevel )bool ;};
|
||||
|
||||
// IsLogLevel returns true if log level is greater or equal than `level`.
|
||||
// Can be used to avoid resource intensive calls to loggers.
|
||||
func (_egg ConsoleLogger )IsLogLevel (level LogLevel )bool {return _egg .LogLevel >=level };var ReleasedAt =_g .Date (_dbg ,_ad ,_gbg ,_aed ,_dge ,0,0,_g .UTC );
|
||||
func (_fg ConsoleLogger )IsLogLevel (level LogLevel )bool {return _fg .LogLevel >=level };const _cac =2020;
|
||||
|
||||
// Debug does nothing for dummy logger.
|
||||
func (DummyLogger )Debug (format string ,args ...interface{}){};var Log Logger =DummyLogger {};
|
||||
// NewWriterLogger creates new 'writer' logger.
|
||||
func NewWriterLogger (logLevel LogLevel ,writer _ag .Writer )*WriterLogger {_gcc :=WriterLogger {Output :writer ,LogLevel :logLevel };return &_gcc ;};var Log Logger =DummyLogger {};
|
||||
|
||||
// WriterLogger is the logger that writes data to the Output writer
|
||||
type WriterLogger struct{LogLevel LogLevel ;Output _fc .Writer ;};func (_ed ConsoleLogger )output (_bfg _fc .Writer ,_ce string ,_ca string ,_fgg ...interface{}){_fbb (_bfg ,_ce ,_ca ,_fgg ...);};const Version ="\u0033\u002e\u0031\u0031\u002e\u0031";
|
||||
|
||||
// Error logs error message.
|
||||
func (_be WriterLogger )Error (format string ,args ...interface{}){if _be .LogLevel >=LogLevelError {_dga :="\u005b\u0045\u0052\u0052\u004f\u0052\u005d\u0020";_be .logToWriter (_be .Output ,_dga ,format ,args ...);};};
|
||||
|
||||
// Trace does nothing for dummy logger.
|
||||
func (DummyLogger )Trace (format string ,args ...interface{}){};
|
||||
|
||||
// ConsoleLogger is a logger that writes logs to the 'os.Stdout'
|
||||
type ConsoleLogger struct{LogLevel LogLevel ;};
|
||||
// Error does nothing for dummy logger.
|
||||
func (DummyLogger )Error (format string ,args ...interface{}){};
|
||||
|
||||
// IsLogLevel returns true from dummy logger.
|
||||
func (DummyLogger )IsLogLevel (level LogLevel )bool {return true };
|
||||
|
||||
// Info does nothing for dummy logger.
|
||||
func (DummyLogger )Info (format string ,args ...interface{}){};
|
||||
|
||||
// WriterLogger is the logger that writes data to the Output writer
|
||||
type WriterLogger struct{LogLevel LogLevel ;Output _ag .Writer ;};const (LogLevelTrace LogLevel =5;LogLevelDebug LogLevel =4;LogLevelInfo LogLevel =3;LogLevelNotice LogLevel =2;LogLevelWarning LogLevel =1;LogLevelError LogLevel =0;);
|
||||
|
||||
// Info logs info message.
|
||||
func (_cbb ConsoleLogger )Info (format string ,args ...interface{}){if _cbb .LogLevel >=LogLevelInfo {_ef :="\u005bI\u004e\u0046\u004f\u005d\u0020";_cbb .output (_f .Stdout ,_ef ,format ,args ...);};};const _gbg =14;const _cag ="\u0032\u0020\u004aan\u0075\u0061\u0072\u0079\u0020\u0032\u0030\u0030\u0036\u0020\u0061\u0074\u0020\u0031\u0035\u003a\u0030\u0034";func (_gaa WriterLogger )logToWriter (_bc _fc .Writer ,_gf string ,_abc string ,_ea ...interface{}){_fbb (_bc ,_gf ,_abc ,_ea );};
|
||||
func (_fbb WriterLogger )Info (format string ,args ...interface{}){if _fbb .LogLevel >=LogLevelInfo {_fade :="\u005bI\u004e\u0046\u004f\u005d\u0020";_fbb .logToWriter (_fbb .Output ,_fade ,format ,args ...);};};
|
||||
|
||||
// IsLogLevel returns true if log level is greater or equal than `level`.
|
||||
// Can be used to avoid resource intensive calls to loggers.
|
||||
func (_ff WriterLogger )IsLogLevel (level LogLevel )bool {return _ff .LogLevel >=level };func _cb (_egf _ag .Writer ,_edd string ,_eee string ,_bag ...interface{}){_ ,_bec ,_gg ,_cbd :=_a .Caller (3);if !_cbd {_bec ="\u003f\u003f\u003f";_gg =0;}else {_bec =_c .Base (_bec );};_ge :=_f .Sprintf ("\u0025s\u0020\u0025\u0073\u003a\u0025\u0064 ",_edd ,_bec ,_gg )+_eee +"\u000a";_f .Fprintf (_egf ,_ge ,_bag ...);};
|
||||
|
||||
// Debug does nothing for dummy logger.
|
||||
func (DummyLogger )Debug (format string ,args ...interface{}){};
|
||||
|
||||
// NewConsoleLogger creates new console logger.
|
||||
func NewConsoleLogger (logLevel LogLevel )*ConsoleLogger {return &ConsoleLogger {LogLevel :logLevel }};func (_bf WriterLogger )logToWriter (_dd _ag .Writer ,_af string ,_age string ,_agfg ...interface{}){_cb (_dd ,_af ,_age ,_agfg );};const Version ="\u0033\u002e\u0031\u0032\u002e\u0030";const _bac =9;var ReleasedAt =_e .Date (_cac ,_bac ,_fce ,_fcf ,_gde ,0,0,_e .UTC );
|
||||
|
||||
// Warning logs warning message.
|
||||
func (_agb ConsoleLogger )Warning (format string ,args ...interface{}){if _agb .LogLevel >=LogLevelWarning {_bg :="\u005b\u0057\u0041\u0052\u004e\u0049\u004e\u0047\u005d\u0020";_agb .output (_cc .Stdout ,_bg ,format ,args ...);};};
|
||||
|
||||
// Warning logs warning message.
|
||||
func (_aa WriterLogger )Warning (format string ,args ...interface{}){if _aa .LogLevel >=LogLevelWarning {_bb :="\u005b\u0057\u0041\u0052\u004e\u0049\u004e\u0047\u005d\u0020";_aa .logToWriter (_aa .Output ,_bb ,format ,args ...);};};
|
||||
|
||||
// ConsoleLogger is a logger that writes logs to the 'os.Stdout'
|
||||
type ConsoleLogger struct{LogLevel LogLevel ;};
|
||||
|
||||
// Debug logs debug message.
|
||||
func (_ca ConsoleLogger )Debug (format string ,args ...interface{}){if _ca .LogLevel >=LogLevelDebug {_de :="\u005b\u0044\u0045\u0042\u0055\u0047\u005d\u0020";_ca .output (_cc .Stdout ,_de ,format ,args ...);};};
|
||||
|
||||
// Notice logs notice message.
|
||||
func (_gb ConsoleLogger )Notice (format string ,args ...interface{}){if _gb .LogLevel >=LogLevelNotice {_af :="\u005bN\u004f\u0054\u0049\u0043\u0045\u005d ";_gb .output (_f .Stdout ,_af ,format ,args ...);};};
|
||||
func (_dc ConsoleLogger )Notice (format string ,args ...interface{}){if _dc .LogLevel >=LogLevelNotice {_fb :="\u005bN\u004f\u0054\u0049\u0043\u0045\u005d ";_dc .output (_cc .Stdout ,_fb ,format ,args ...);};};
|
||||
|
||||
// Error logs error message.
|
||||
func (_gd ConsoleLogger )Error (format string ,args ...interface{}){if _gd .LogLevel >=LogLevelError {_ce :="\u005b\u0045\u0052\u0052\u004f\u0052\u005d\u0020";_gd .output (_cc .Stdout ,_ce ,format ,args ...);};};
|
||||
|
||||
// Notice does nothing for dummy logger.
|
||||
func (DummyLogger )Notice (format string ,args ...interface{}){};const _gde =10;
|
||||
|
||||
// Info logs info message.
|
||||
func (_fad ConsoleLogger )Info (format string ,args ...interface{}){if _fad .LogLevel >=LogLevelInfo {_bcc :="\u005bI\u004e\u0046\u004f\u005d\u0020";_fad .output (_cc .Stdout ,_bcc ,format ,args ...);};};
|
||||
|
||||
// Notice logs notice message.
|
||||
func (_cf WriterLogger )Notice (format string ,args ...interface{}){if _cf .LogLevel >=LogLevelNotice {_eg :="\u005bN\u004f\u0054\u0049\u0043\u0045\u005d ";_cf .logToWriter (_cf .Output ,_eg ,format ,args ...);};};
|
||||
|
||||
// Error logs error message.
|
||||
func (_cee WriterLogger )Error (format string ,args ...interface{}){if _cee .LogLevel >=LogLevelError {_ae :="\u005b\u0045\u0052\u0052\u004f\u0052\u005d\u0020";_cee .logToWriter (_cee .Output ,_ae ,format ,args ...);};};
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -14,204 +14,204 @@
|
||||
// page content streams and XObject forms and thus also in annotation appearance streams.
|
||||
//
|
||||
// Also defines utility functions for drawing common shapes such as rectangles, lines and circles (ovals).
|
||||
package draw ;import (_ee "fmt";_df "github.com/unidoc/unipdf/v3/contentstream";_f "github.com/unidoc/unipdf/v3/core";_eb "github.com/unidoc/unipdf/v3/internal/transform";_a "github.com/unidoc/unipdf/v3/model";_e "math";);
|
||||
package draw ;import (_e "fmt";_gg "github.com/unidoc/unipdf/v3/contentstream";_cb "github.com/unidoc/unipdf/v3/core";_gd "github.com/unidoc/unipdf/v3/internal/transform";_c "github.com/unidoc/unipdf/v3/model";_a "math";);
|
||||
|
||||
// LineEndingStyle defines the line ending style for lines.
|
||||
// The currently supported line ending styles are None, Arrow (ClosedArrow) and Butt.
|
||||
type LineEndingStyle int ;
|
||||
|
||||
// PolyBezierCurve represents a composite curve that is the result of
|
||||
// joining multiple cubic Bezier curves.
|
||||
type PolyBezierCurve struct{Curves []CubicBezierCurve ;BorderWidth float64 ;BorderColor *_a .PdfColorDeviceRGB ;FillEnabled bool ;FillColor *_a .PdfColorDeviceRGB ;};
|
||||
|
||||
// GetPolarAngle returns the angle the magnitude of the vector forms with the
|
||||
// positive X-axis going counterclockwise.
|
||||
func (_egab Vector )GetPolarAngle ()float64 {return _e .Atan2 (_egab .Dy ,_egab .Dx )};
|
||||
|
||||
// RemovePoint removes the point at the index specified by number from the
|
||||
// path. The index is 1-based.
|
||||
func (_fcb Path )RemovePoint (number int )Path {if number < 1||number > len (_fcb .Points ){return _fcb ;};_bcg :=number -1;_fcb .Points =append (_fcb .Points [:_bcg ],_fcb .Points [_bcg +1:]...);return _fcb ;};
|
||||
|
||||
// Draw draws the polygon. A graphics state name can be specified for
|
||||
// setting the polygon properties (e.g. setting the opacity). Otherwise leave
|
||||
// empty (""). Returns the content stream as a byte array and the polygon
|
||||
// bounding box.
|
||||
func (_ffe Polygon )Draw (gsName string )([]byte ,*_a .PdfRectangle ,error ){_ebc :=_df .NewContentCreator ();_ebc .Add_q ();_ffe .FillEnabled =_ffe .FillEnabled &&_ffe .FillColor !=nil ;if _ffe .FillEnabled {_ebc .Add_rg (_ffe .FillColor .R (),_ffe .FillColor .G (),_ffe .FillColor .B ());};_ffe .BorderEnabled =_ffe .BorderEnabled &&_ffe .BorderColor !=nil ;if _ffe .BorderEnabled {_ebc .Add_RG (_ffe .BorderColor .R (),_ffe .BorderColor .G (),_ffe .BorderColor .B ());_ebc .Add_w (_ffe .BorderWidth );};if len (gsName )> 1{_ebc .Add_gs (_f .PdfObjectName (gsName ));};_bfd :=NewPath ();for _ ,_fbd :=range _ffe .Points {for _bdc ,_eab :=range _fbd {_bfd =_bfd .AppendPoint (_eab );if _bdc ==0{_ebc .Add_m (_eab .X ,_eab .Y );}else {_ebc .Add_l (_eab .X ,_eab .Y );};};_ebc .Add_h ();};if _ffe .FillEnabled &&_ffe .BorderEnabled {_ebc .Add_B ();}else if _ffe .FillEnabled {_ebc .Add_f ();}else if _ffe .BorderEnabled {_ebc .Add_S ();};_ebc .Add_Q ();return _ebc .Bytes (),_bfd .GetBoundingBox ().ToPdfRectangle (),nil ;};
|
||||
|
||||
// Copy returns a clone of the Bezier path.
|
||||
func (_cc CubicBezierPath )Copy ()CubicBezierPath {_ff :=CubicBezierPath {};_ff .Curves =[]CubicBezierCurve {};for _ ,_dc :=range _cc .Curves {_ff .Curves =append (_ff .Curves ,_dc );};return _ff ;};
|
||||
|
||||
// Point represents a two-dimensional point.
|
||||
type Point struct{X float64 ;Y float64 ;};
|
||||
|
||||
// ToPdfRectangle returns the bounding box as a PDF rectangle.
|
||||
func (_bac BoundingBox )ToPdfRectangle ()*_a .PdfRectangle {return &_a .PdfRectangle {Llx :_bac .X ,Lly :_bac .Y ,Urx :_bac .X +_bac .Width ,Ury :_bac .Y +_bac .Height };};
|
||||
|
||||
// ToPdfRectangle returns the rectangle as a PDF rectangle.
|
||||
func (_bgc Rectangle )ToPdfRectangle ()*_a .PdfRectangle {return &_a .PdfRectangle {Llx :_bgc .X ,Lly :_bgc .Y ,Urx :_bgc .X +_bgc .Width ,Ury :_bgc .Y +_bgc .Height };};
|
||||
|
||||
// Rotate returns a new Point at `p` rotated by `theta` degrees.
|
||||
func (_aa Point )Rotate (theta float64 )Point {_bgaa :=_eb .NewPoint (_aa .X ,_aa .Y ).Rotate (theta );return NewPoint (_bgaa .X ,_bgaa .Y );};func (_fcbb Point )String ()string {return _ee .Sprintf ("(\u0025\u002e\u0031\u0066\u002c\u0025\u002e\u0031\u0066\u0029",_fcbb .X ,_fcbb .Y );};
|
||||
|
||||
// DrawPathWithCreator makes the path with the content creator.
|
||||
// Adds the PDF commands to draw the path to the creator instance.
|
||||
func DrawPathWithCreator (path Path ,creator *_df .ContentCreator ){for _def ,_ebd :=range path .Points {if _def ==0{creator .Add_m (_ebd .X ,_ebd .Y );}else {creator .Add_l (_ebd .X ,_ebd .Y );};};};
|
||||
|
||||
// NewCubicBezierPath returns a new empty cubic Bezier path.
|
||||
func NewCubicBezierPath ()CubicBezierPath {_fe :=CubicBezierPath {};_fe .Curves =[]CubicBezierCurve {};return _fe ;};
|
||||
|
||||
// Polyline defines a slice of points that are connected as straight lines.
|
||||
type Polyline struct{Points []Point ;LineColor *_a .PdfColorDeviceRGB ;LineWidth float64 ;};
|
||||
|
||||
// LineStyle refers to how the line will be created.
|
||||
type LineStyle int ;
|
||||
|
||||
// GetBounds returns the bounding box of the Bezier curve.
|
||||
func (_cg CubicBezierCurve )GetBounds ()_a .PdfRectangle {_db :=_cg .P0 .X ;_dg :=_cg .P0 .X ;_b :=_cg .P0 .Y ;_ad :=_cg .P0 .Y ;for _eg :=0.0;_eg <=1.0;_eg +=0.001{Rx :=_cg .P0 .X *_e .Pow (1-_eg ,3)+_cg .P1 .X *3*_eg *_e .Pow (1-_eg ,2)+_cg .P2 .X *3*_e .Pow (_eg ,2)*(1-_eg )+_cg .P3 .X *_e .Pow (_eg ,3);Ry :=_cg .P0 .Y *_e .Pow (1-_eg ,3)+_cg .P1 .Y *3*_eg *_e .Pow (1-_eg ,2)+_cg .P2 .Y *3*_e .Pow (_eg ,2)*(1-_eg )+_cg .P3 .Y *_e .Pow (_eg ,3);if Rx < _db {_db =Rx ;};if Rx > _dg {_dg =Rx ;};if Ry < _b {_b =Ry ;};if Ry > _ad {_ad =Ry ;};};_g :=_a .PdfRectangle {};_g .Llx =_db ;_g .Lly =_b ;_g .Urx =_dg ;_g .Ury =_ad ;return _g ;};
|
||||
|
||||
// NewCubicBezierCurve returns a new cubic Bezier curve.
|
||||
func NewCubicBezierCurve (x0 ,y0 ,x1 ,y1 ,x2 ,y2 ,x3 ,y3 float64 )CubicBezierCurve {_c :=CubicBezierCurve {};_c .P0 =NewPoint (x0 ,y0 );_c .P1 =NewPoint (x1 ,y1 );_c .P2 =NewPoint (x2 ,y2 );_c .P3 =NewPoint (x3 ,y3 );return _c ;};
|
||||
|
||||
// Length returns the number of points in the path.
|
||||
func (_gf Path )Length ()int {return len (_gf .Points )};
|
||||
|
||||
// AddVector adds vector to a point.
|
||||
func (_bga Point )AddVector (v Vector )Point {_bga .X +=v .Dx ;_bga .Y +=v .Dy ;return _bga };
|
||||
|
||||
// Flip changes the sign of the vector: -vector.
|
||||
func (_cb Vector )Flip ()Vector {_gdb :=_cb .Magnitude ();_bfb :=_cb .GetPolarAngle ();_cb .Dx =_gdb *_e .Cos (_bfb +_e .Pi );_cb .Dy =_gdb *_e .Sin (_bfb +_e .Pi );return _cb ;};
|
||||
// Line defines a line shape between point 1 (X1,Y1) and point 2 (X2,Y2). The line ending styles can be none (regular line),
|
||||
// or arrows at either end. The line also has a specified width, color and opacity.
|
||||
type Line struct{X1 float64 ;Y1 float64 ;X2 float64 ;Y2 float64 ;LineColor *_c .PdfColorDeviceRGB ;Opacity float64 ;LineWidth float64 ;LineEndingStyle1 LineEndingStyle ;LineEndingStyle2 LineEndingStyle ;LineStyle LineStyle ;};
|
||||
|
||||
// Draw draws the circle. Can specify a graphics state (gsName) for setting opacity etc. Otherwise leave empty ("").
|
||||
// Returns the content stream as a byte array, the bounding box and an error on failure.
|
||||
func (_ab Circle )Draw (gsName string )([]byte ,*_a .PdfRectangle ,error ){_cga :=_ab .Width /2;_ge :=_ab .Height /2;if _ab .BorderEnabled {_cga -=_ab .BorderWidth /2;_ge -=_ab .BorderWidth /2;};_dfg :=0.551784;_dbb :=_cga *_dfg ;_ggad :=_ge *_dfg ;_de :=NewCubicBezierPath ();_de =_de .AppendCurve (NewCubicBezierCurve (-_cga ,0,-_cga ,_ggad ,-_dbb ,_ge ,0,_ge ));_de =_de .AppendCurve (NewCubicBezierCurve (0,_ge ,_dbb ,_ge ,_cga ,_ggad ,_cga ,0));_de =_de .AppendCurve (NewCubicBezierCurve (_cga ,0,_cga ,-_ggad ,_dbb ,-_ge ,0,-_ge ));_de =_de .AppendCurve (NewCubicBezierCurve (0,-_ge ,-_dbb ,-_ge ,-_cga ,-_ggad ,-_cga ,0));_de =_de .Offset (_cga ,_ge );if _ab .BorderEnabled {_de =_de .Offset (_ab .BorderWidth /2,_ab .BorderWidth /2);};if _ab .X !=0||_ab .Y !=0{_de =_de .Offset (_ab .X ,_ab .Y );};_ead :=_df .NewContentCreator ();_ead .Add_q ();if _ab .FillEnabled {_ead .Add_rg (_ab .FillColor .R (),_ab .FillColor .G (),_ab .FillColor .B ());};if _ab .BorderEnabled {_ead .Add_RG (_ab .BorderColor .R (),_ab .BorderColor .G (),_ab .BorderColor .B ());_ead .Add_w (_ab .BorderWidth );};if len (gsName )> 1{_ead .Add_gs (_f .PdfObjectName (gsName ));};DrawBezierPathWithCreator (_de ,_ead );_ead .Add_h ();if _ab .FillEnabled &&_ab .BorderEnabled {_ead .Add_B ();}else if _ab .FillEnabled {_ead .Add_f ();}else if _ab .BorderEnabled {_ead .Add_S ();};_ead .Add_Q ();_dee :=_de .GetBoundingBox ();if _ab .BorderEnabled {_dee .Height +=_ab .BorderWidth ;_dee .Width +=_ab .BorderWidth ;_dee .X -=_ab .BorderWidth /2;_dee .Y -=_ab .BorderWidth /2;};return _ead .Bytes (),_dee .ToPdfRectangle (),nil ;};
|
||||
func (_eb Circle )Draw (gsName string )([]byte ,*_c .PdfRectangle ,error ){_gcd :=_eb .Width /2;_ge :=_eb .Height /2;if _eb .BorderEnabled {_gcd -=_eb .BorderWidth /2;_ge -=_eb .BorderWidth /2;};_cg :=0.551784;_caf :=_gcd *_cg ;_gff :=_ge *_cg ;_ceb :=NewCubicBezierPath ();_ceb =_ceb .AppendCurve (NewCubicBezierCurve (-_gcd ,0,-_gcd ,_gff ,-_caf ,_ge ,0,_ge ));_ceb =_ceb .AppendCurve (NewCubicBezierCurve (0,_ge ,_caf ,_ge ,_gcd ,_gff ,_gcd ,0));_ceb =_ceb .AppendCurve (NewCubicBezierCurve (_gcd ,0,_gcd ,-_gff ,_caf ,-_ge ,0,-_ge ));_ceb =_ceb .AppendCurve (NewCubicBezierCurve (0,-_ge ,-_caf ,-_ge ,-_gcd ,-_gff ,-_gcd ,0));_ceb =_ceb .Offset (_gcd ,_ge );if _eb .BorderEnabled {_ceb =_ceb .Offset (_eb .BorderWidth /2,_eb .BorderWidth /2);};if _eb .X !=0||_eb .Y !=0{_ceb =_ceb .Offset (_eb .X ,_eb .Y );};_gaf :=_gg .NewContentCreator ();_gaf .Add_q ();if _eb .FillEnabled {_gaf .Add_rg (_eb .FillColor .R (),_eb .FillColor .G (),_eb .FillColor .B ());};if _eb .BorderEnabled {_gaf .Add_RG (_eb .BorderColor .R (),_eb .BorderColor .G (),_eb .BorderColor .B ());_gaf .Add_w (_eb .BorderWidth );};if len (gsName )> 1{_gaf .Add_gs (_cb .PdfObjectName (gsName ));};DrawBezierPathWithCreator (_ceb ,_gaf );_gaf .Add_h ();if _eb .FillEnabled &&_eb .BorderEnabled {_gaf .Add_B ();}else if _eb .FillEnabled {_gaf .Add_f ();}else if _eb .BorderEnabled {_gaf .Add_S ();};_gaf .Add_Q ();_gdg :=_ceb .GetBoundingBox ();if _eb .BorderEnabled {_gdg .Height +=_eb .BorderWidth ;_gdg .Width +=_eb .BorderWidth ;_gdg .X -=_eb .BorderWidth /2;_gdg .Y -=_eb .BorderWidth /2;};return _gaf .Bytes (),_gdg .ToPdfRectangle (),nil ;};
|
||||
|
||||
// Draw draws the line to PDF contentstream. Generates the content stream which can be used in page contents or
|
||||
// appearance stream of annotation. Returns the stream content, XForm bounding box (local), bounding box and an error
|
||||
// if one occurred.
|
||||
func (_fgf Line )Draw (gsName string )([]byte ,*_a .PdfRectangle ,error ){_aff ,_dff :=_fgf .X1 ,_fgf .X2 ;_ged ,_dea :=_fgf .Y1 ,_fgf .Y2 ;_cge :=_dea -_ged ;_gc :=_dff -_aff ;_eba :=_e .Atan2 (_cge ,_gc );L :=_e .Sqrt (_e .Pow (_gc ,2.0)+_e .Pow (_cge ,2.0));_fa :=_fgf .LineWidth ;_dge :=_e .Pi ;_bace :=1.0;if _gc < 0{_bace *=-1.0;};if _cge < 0{_bace *=-1.0;};VsX :=_bace *(-_fa /2*_e .Cos (_eba +_dge /2));VsY :=_bace *(-_fa /2*_e .Sin (_eba +_dge /2)+_fa *_e .Sin (_eba +_dge /2));V1X :=VsX +_fa /2*_e .Cos (_eba +_dge /2);V1Y :=VsY +_fa /2*_e .Sin (_eba +_dge /2);V2X :=VsX +_fa /2*_e .Cos (_eba +_dge /2)+L *_e .Cos (_eba );V2Y :=VsY +_fa /2*_e .Sin (_eba +_dge /2)+L *_e .Sin (_eba );V3X :=VsX +_fa /2*_e .Cos (_eba +_dge /2)+L *_e .Cos (_eba )+_fa *_e .Cos (_eba -_dge /2);V3Y :=VsY +_fa /2*_e .Sin (_eba +_dge /2)+L *_e .Sin (_eba )+_fa *_e .Sin (_eba -_dge /2);V4X :=VsX +_fa /2*_e .Cos (_eba -_dge /2);V4Y :=VsY +_fa /2*_e .Sin (_eba -_dge /2);_dbad :=NewPath ();_dbad =_dbad .AppendPoint (NewPoint (V1X ,V1Y ));_dbad =_dbad .AppendPoint (NewPoint (V2X ,V2Y ));_dbad =_dbad .AppendPoint (NewPoint (V3X ,V3Y ));_dbad =_dbad .AppendPoint (NewPoint (V4X ,V4Y ));_eeb :=_fgf .LineEndingStyle1 ;_aba :=_fgf .LineEndingStyle2 ;_gb :=3*_fa ;_deee :=3*_fa ;_ggg :=(_deee -_fa )/2;if _aba ==LineEndingStyleArrow {_ca :=_dbad .GetPointNumber (2);_adbb :=NewVectorPolar (_gb ,_eba +_dge );_caf :=_ca .AddVector (_adbb );_gfg :=NewVectorPolar (_deee /2,_eba +_dge /2);_bff :=NewVectorPolar (_gb ,_eba );_dgg :=NewVectorPolar (_ggg ,_eba +_dge /2);_fgbe :=_caf .AddVector (_dgg );_gcf :=_bff .Add (_gfg .Flip ());_afa :=_fgbe .AddVector (_gcf );_fbde :=_gfg .Scale (2).Flip ().Add (_gcf .Flip ());_fec :=_afa .AddVector (_fbde );_ccd :=_caf .AddVector (NewVectorPolar (_fa ,_eba -_dge /2));_cfb :=NewPath ();_cfb =_cfb .AppendPoint (_dbad .GetPointNumber (1));_cfb =_cfb .AppendPoint (_caf );_cfb =_cfb .AppendPoint (_fgbe );_cfb =_cfb .AppendPoint (_afa );_cfb =_cfb .AppendPoint (_fec );_cfb =_cfb .AppendPoint (_ccd );_cfb =_cfb .AppendPoint (_dbad .GetPointNumber (4));_dbad =_cfb ;};if _eeb ==LineEndingStyleArrow {_cab :=_dbad .GetPointNumber (1);_bab :=_dbad .GetPointNumber (_dbad .Length ());_bba :=NewVectorPolar (_fa /2,_eba +_dge +_dge /2);_cfc :=_cab .AddVector (_bba );_fd :=NewVectorPolar (_gb ,_eba ).Add (NewVectorPolar (_deee /2,_eba +_dge /2));_ddd :=_cfc .AddVector (_fd );_faf :=NewVectorPolar (_ggg ,_eba -_dge /2);_dae :=_ddd .AddVector (_faf );_edc :=NewVectorPolar (_gb ,_eba );_ace :=_bab .AddVector (_edc );_fcg :=NewVectorPolar (_ggg ,_eba +_dge +_dge /2);_gbe :=_ace .AddVector (_fcg );_gba :=_cfc ;_aca :=NewPath ();_aca =_aca .AppendPoint (_cfc );_aca =_aca .AppendPoint (_ddd );_aca =_aca .AppendPoint (_dae );for _ ,_gd :=range _dbad .Points [1:len (_dbad .Points )-1]{_aca =_aca .AppendPoint (_gd );};_aca =_aca .AppendPoint (_ace );_aca =_aca .AppendPoint (_gbe );_aca =_aca .AppendPoint (_gba );_dbad =_aca ;};_fcbg :=_df .NewContentCreator ();_fcbg .Add_q ().Add_rg (_fgf .LineColor .R (),_fgf .LineColor .G (),_fgf .LineColor .B ());if len (gsName )> 1{_fcbg .Add_gs (_f .PdfObjectName (gsName ));};_dbad =_dbad .Offset (_fgf .X1 ,_fgf .Y1 );_fggb :=_dbad .GetBoundingBox ();DrawPathWithCreator (_dbad ,_fcbg );if _fgf .LineStyle ==LineStyleDashed {_fcbg .Add_d ([]int64 {1,1},0).Add_S ().Add_f ().Add_Q ();}else {_fcbg .Add_f ().Add_Q ();};return _fcbg .Bytes (),_fggb .ToPdfRectangle (),nil ;};
|
||||
|
||||
// Add shifts the coordinates of the point with dx, dy and returns the result.
|
||||
func (_gga Point )Add (dx ,dy float64 )Point {_gga .X +=dx ;_gga .Y +=dy ;return _gga };
|
||||
|
||||
// Path consists of straight line connections between each point defined in an array of points.
|
||||
type Path struct{Points []Point ;};
|
||||
|
||||
// Rotate rotates the vector by the specified angle.
|
||||
func (_aeb Vector )Rotate (phi float64 )Vector {_gbg :=_aeb .Magnitude ();_dfd :=_aeb .GetPolarAngle ();return NewVectorPolar (_gbg ,_dfd +phi );};
|
||||
|
||||
// FlipY flips the sign of the Dy component of the vector.
|
||||
func (_fece Vector )FlipY ()Vector {_fece .Dy =-_fece .Dy ;return _fece };
|
||||
|
||||
// GetPointNumber returns the path point at the index specified by number.
|
||||
// The index is 1-based.
|
||||
func (_ae Path )GetPointNumber (number int )Point {if number < 1||number > len (_ae .Points ){return Point {};};return _ae .Points [number -1];};
|
||||
|
||||
// NewVectorPolar returns a new vector calculated from the specified
|
||||
// magnitude and angle.
|
||||
func NewVectorPolar (length float64 ,theta float64 )Vector {_deg :=Vector {};_deg .Dx =length *_e .Cos (theta );_deg .Dy =length *_e .Sin (theta );return _deg ;};
|
||||
// Copy returns a clone of the Bezier path.
|
||||
func (_cac CubicBezierPath )Copy ()CubicBezierPath {_b :=CubicBezierPath {};_b .Curves =[]CubicBezierCurve {};for _ ,_bf :=range _cac .Curves {_b .Curves =append (_b .Curves ,_bf );};return _b ;};
|
||||
|
||||
// Vector represents a two-dimensional vector.
|
||||
type Vector struct{Dx float64 ;Dy float64 ;};
|
||||
|
||||
// AppendPoint adds the specified point to the path.
|
||||
func (_bc Path )AppendPoint (point Point )Path {_bc .Points =append (_bc .Points ,point );return _bc };
|
||||
|
||||
// NewVector returns a new vector with the direction specified by dx and dy.
|
||||
func NewVector (dx ,dy float64 )Vector {_baa :=Vector {};_baa .Dx =dx ;_baa .Dy =dy ;return _baa };const (LineStyleSolid LineStyle =0;LineStyleDashed LineStyle =1;);
|
||||
|
||||
// BasicLine defines a line between point 1 (X1,Y1) and point 2 (X2,Y2). The line has a specified width, color and opacity.
|
||||
type BasicLine struct{X1 float64 ;Y1 float64 ;X2 float64 ;Y2 float64 ;LineColor *_a .PdfColorDeviceRGB ;Opacity float64 ;LineWidth float64 ;LineStyle LineStyle ;};
|
||||
|
||||
// Add adds the specified vector to the current one and returns the result.
|
||||
func (_ddf Vector )Add (other Vector )Vector {_ddf .Dx +=other .Dx ;_ddf .Dy +=other .Dy ;return _ddf };
|
||||
|
||||
// GetBoundingBox returns the bounding box of the Bezier path.
|
||||
func (_ac CubicBezierPath )GetBoundingBox ()Rectangle {_af :=Rectangle {};_ffa :=0.0;_ce :=0.0;_bb :=0.0;_bd :=0.0;for _dca ,_dba :=range _ac .Curves {_ba :=_dba .GetBounds ();if _dca ==0{_ffa =_ba .Llx ;_ce =_ba .Urx ;_bb =_ba .Lly ;_bd =_ba .Ury ;continue ;};if _ba .Llx < _ffa {_ffa =_ba .Llx ;};if _ba .Urx > _ce {_ce =_ba .Urx ;};if _ba .Lly < _bb {_bb =_ba .Lly ;};if _ba .Ury > _bd {_bd =_ba .Ury ;};};_af .X =_ffa ;_af .Y =_bb ;_af .Width =_ce -_ffa ;_af .Height =_bd -_bb ;return _af ;};
|
||||
|
||||
// Draw draws the polyline. A graphics state name can be specified for
|
||||
// setting the polyline properties (e.g. setting the opacity). Otherwise leave
|
||||
// empty (""). Returns the content stream as a byte array and the polyline
|
||||
// bounding box.
|
||||
func (_gea Polyline )Draw (gsName string )([]byte ,*_a .PdfRectangle ,error ){if _gea .LineColor ==nil {_gea .LineColor =_a .NewPdfColorDeviceRGB (0,0,0);};_eac :=NewPath ();for _ ,_bbg :=range _gea .Points {_eac =_eac .AppendPoint (_bbg );};_gec :=_df .NewContentCreator ();_gec .Add_q ();_gec .Add_RG (_gea .LineColor .R (),_gea .LineColor .G (),_gea .LineColor .B ());_gec .Add_w (_gea .LineWidth );if len (gsName )> 1{_gec .Add_gs (_f .PdfObjectName (gsName ));};DrawPathWithCreator (_eac ,_gec );_gec .Add_S ();_gec .Add_Q ();return _gec .Bytes (),_eac .GetBoundingBox ().ToPdfRectangle (),nil ;};const (LineEndingStyleNone LineEndingStyle =0;LineEndingStyleArrow LineEndingStyle =1;LineEndingStyleButt LineEndingStyle =2;);
|
||||
|
||||
// Rectangle is a shape with a specified Width and Height and a lower left corner at (X,Y) that can be
|
||||
// drawn to a PDF content stream. The rectangle can optionally have a border and a filling color.
|
||||
// The Width/Height includes the border (if any specified), i.e. is positioned inside.
|
||||
type Rectangle struct{X float64 ;Y float64 ;Width float64 ;Height float64 ;FillEnabled bool ;FillColor *_a .PdfColorDeviceRGB ;BorderEnabled bool ;BorderWidth float64 ;BorderColor *_a .PdfColorDeviceRGB ;Opacity float64 ;};
|
||||
|
||||
// Polygon is a multi-point shape that can be drawn to a PDF content stream.
|
||||
type Polygon struct{Points [][]Point ;FillEnabled bool ;FillColor *_a .PdfColorDeviceRGB ;BorderEnabled bool ;BorderColor *_a .PdfColorDeviceRGB ;BorderWidth float64 ;};
|
||||
|
||||
// AddOffsetXY adds X,Y offset to all points on a curve.
|
||||
func (_fg CubicBezierCurve )AddOffsetXY (offX ,offY float64 )CubicBezierCurve {_fg .P0 .X +=offX ;_fg .P1 .X +=offX ;_fg .P2 .X +=offX ;_fg .P3 .X +=offX ;_fg .P0 .Y +=offY ;_fg .P1 .Y +=offY ;_fg .P2 .Y +=offY ;_fg .P3 .Y +=offY ;return _fg ;};
|
||||
|
||||
// Scale scales the vector by the specified factor.
|
||||
func (_bgb Vector )Scale (factor float64 )Vector {_bbge :=_bgb .Magnitude ();_ecc :=_bgb .GetPolarAngle ();_bgb .Dx =factor *_bbge *_e .Cos (_ecc );_bgb .Dy =factor *_bbge *_e .Sin (_ecc );return _bgb ;};
|
||||
func (_cbd Vector )Scale (factor float64 )Vector {_efd :=_cbd .Magnitude ();_bfbd :=_cbd .GetPolarAngle ();_cbd .Dx =factor *_efd *_a .Cos (_bfbd );_cbd .Dy =factor *_efd *_a .Sin (_bfbd );return _cbd ;};
|
||||
|
||||
// AppendCurve appends the specified Bezier curve to the path.
|
||||
func (_ag CubicBezierPath )AppendCurve (curve CubicBezierCurve )CubicBezierPath {_ag .Curves =append (_ag .Curves ,curve );return _ag ;};
|
||||
func (_ca CubicBezierPath )AppendCurve (curve CubicBezierCurve )CubicBezierPath {_ca .Curves =append (_ca .Curves ,curve );return _ca ;};
|
||||
|
||||
// Draw draws the rectangle. Can specify a graphics state (gsName) for setting opacity etc.
|
||||
// Otherwise leave empty (""). Returns the content stream as a byte array, bounding box and an error on failure.
|
||||
func (_fgb Rectangle )Draw (gsName string )([]byte ,*_a .PdfRectangle ,error ){_bgf :=NewPath ();_bgf =_bgf .AppendPoint (NewPoint (0,0));_bgf =_bgf .AppendPoint (NewPoint (0,_fgb .Height ));_bgf =_bgf .AppendPoint (NewPoint (_fgb .Width ,_fgb .Height ));_bgf =_bgf .AppendPoint (NewPoint (_fgb .Width ,0));_bgf =_bgf .AppendPoint (NewPoint (0,0));if _fgb .X !=0||_fgb .Y !=0{_bgf =_bgf .Offset (_fgb .X ,_fgb .Y );};_feg :=_df .NewContentCreator ();_feg .Add_q ();if _fgb .FillEnabled {_feg .Add_rg (_fgb .FillColor .R (),_fgb .FillColor .G (),_fgb .FillColor .B ());};if _fgb .BorderEnabled {_feg .Add_RG (_fgb .BorderColor .R (),_fgb .BorderColor .G (),_fgb .BorderColor .B ());_feg .Add_w (_fgb .BorderWidth );};if len (gsName )> 1{_feg .Add_gs (_f .PdfObjectName (gsName ));};DrawPathWithCreator (_bgf ,_feg );_feg .Add_h ();if _fgb .FillEnabled &&_fgb .BorderEnabled {_feg .Add_B ();}else if _fgb .FillEnabled {_feg .Add_f ();}else if _fgb .BorderEnabled {_feg .Add_S ();};_feg .Add_Q ();return _feg .Bytes (),_bgf .GetBoundingBox ().ToPdfRectangle (),nil ;};
|
||||
// Rotate rotates the vector by the specified angle.
|
||||
func (_bfd Vector )Rotate (phi float64 )Vector {_ecc :=_bfd .Magnitude ();_aea :=_bfd .GetPolarAngle ();return NewVectorPolar (_ecc ,_aea +phi );};
|
||||
|
||||
// Circle represents a circle shape with fill and border properties that can be drawn to a PDF content stream.
|
||||
type Circle struct{X float64 ;Y float64 ;Width float64 ;Height float64 ;FillEnabled bool ;FillColor *_a .PdfColorDeviceRGB ;BorderEnabled bool ;BorderWidth float64 ;BorderColor *_a .PdfColorDeviceRGB ;Opacity float64 ;};
|
||||
// Point represents a two-dimensional point.
|
||||
type Point struct{X float64 ;Y float64 ;};
|
||||
|
||||
// Magnitude returns the magnitude of the vector.
|
||||
func (_bgd Vector )Magnitude ()float64 {return _a .Sqrt (_a .Pow (_bgd .Dx ,2.0)+_a .Pow (_bgd .Dy ,2.0))};
|
||||
|
||||
// Draw draws the basic line to PDF. Generates the content stream which can be used in page contents or appearance
|
||||
// stream of annotation. Returns the stream content, XForm bounding box (local), bounding box and an error if
|
||||
// one occurred.
|
||||
func (_ef BasicLine )Draw (gsName string )([]byte ,*_a .PdfRectangle ,error ){_fgc :=_ef .LineWidth ;_baf :=NewPath ();_baf =_baf .AppendPoint (NewPoint (_ef .X1 ,_ef .Y1 ));_baf =_baf .AppendPoint (NewPoint (_ef .X2 ,_ef .Y2 ));_ebac :=_df .NewContentCreator ();_fgd :=_baf .GetBoundingBox ();DrawPathWithCreator (_baf ,_ebac );if _ef .LineStyle ==LineStyleDashed {_ebac .Add_d ([]int64 {1,1},0);};_ebac .Add_RG (_ef .LineColor .R (),_ef .LineColor .G (),_ef .LineColor .B ()).Add_w (_fgc ).Add_S ().Add_Q ();return _ebac .Bytes (),_fgd .ToPdfRectangle (),nil ;};
|
||||
func (_gfd BasicLine )Draw (gsName string )([]byte ,*_c .PdfRectangle ,error ){_dbc :=_gfd .LineWidth ;_bb :=NewPath ();_bb =_bb .AppendPoint (NewPoint (_gfd .X1 ,_gfd .Y1 ));_bb =_bb .AppendPoint (NewPoint (_gfd .X2 ,_gfd .Y2 ));_accc :=_gg .NewContentCreator ();_aad :=_bb .GetBoundingBox ();DrawPathWithCreator (_bb ,_accc );if _gfd .LineStyle ==LineStyleDashed {_accc .Add_d ([]int64 {1,1},0);};_accc .Add_RG (_gfd .LineColor .R (),_gfd .LineColor .G (),_gfd .LineColor .B ()).Add_w (_dbc ).Add_S ().Add_Q ();return _accc .Bytes (),_aad .ToPdfRectangle (),nil ;};
|
||||
|
||||
// Magnitude returns the magnitude of the vector.
|
||||
func (_acac Vector )Magnitude ()float64 {return _e .Sqrt (_e .Pow (_acac .Dx ,2.0)+_e .Pow (_acac .Dy ,2.0));};
|
||||
|
||||
// GetBoundingBox returns the bounding box of the path.
|
||||
func (_fb Path )GetBoundingBox ()BoundingBox {_ecf :=BoundingBox {};_eed :=0.0;_cf :=0.0;_fgg :=0.0;_bg :=0.0;for _cd ,_ea :=range _fb .Points {if _cd ==0{_eed =_ea .X ;_cf =_ea .X ;_fgg =_ea .Y ;_bg =_ea .Y ;continue ;};if _ea .X < _eed {_eed =_ea .X ;};if _ea .X > _cf {_cf =_ea .X ;};if _ea .Y < _fgg {_fgg =_ea .Y ;};if _ea .Y > _bg {_bg =_ea .Y ;};};_ecf .X =_eed ;_ecf .Y =_fgg ;_ecf .Width =_cf -_eed ;_ecf .Height =_bg -_fgg ;return _ecf ;};
|
||||
|
||||
// NewVectorBetween returns a new vector with the direction specified by
|
||||
// the subtraction of point a from point b (b-a).
|
||||
func NewVectorBetween (a Point ,b Point )Vector {_fgcc :=Vector {};_fgcc .Dx =b .X -a .X ;_fgcc .Dy =b .Y -a .Y ;return _fgcc ;};
|
||||
|
||||
// NewPoint returns a new point with the coordinates x, y.
|
||||
func NewPoint (x ,y float64 )Point {return Point {X :x ,Y :y }};
|
||||
|
||||
// DrawBezierPathWithCreator makes the bezier path with the content creator.
|
||||
// Adds the PDF commands to draw the path to the creator instance.
|
||||
func DrawBezierPathWithCreator (bpath CubicBezierPath ,creator *_df .ContentCreator ){for _gfd ,_gfc :=range bpath .Curves {if _gfd ==0{creator .Add_m (_gfc .P0 .X ,_gfc .P0 .Y );};creator .Add_c (_gfc .P1 .X ,_gfc .P1 .Y ,_gfc .P2 .X ,_gfc .P2 .Y ,_gfc .P3 .X ,_gfc .P3 .Y );};};
|
||||
|
||||
// Copy returns a clone of the path.
|
||||
func (_ccg Path )Copy ()Path {_gg :=Path {};_gg .Points =[]Point {};for _ ,_bcf :=range _ccg .Points {_gg .Points =append (_gg .Points ,_bcf );};return _gg ;};
|
||||
|
||||
// BoundingBox represents the smallest rectangular area that encapsulates an object.
|
||||
type BoundingBox struct{X float64 ;Y float64 ;Width float64 ;Height float64 ;};
|
||||
|
||||
// Offset shifts the path with the specified offsets.
|
||||
func (_bf Path )Offset (offX ,offY float64 )Path {for _fga ,_bde :=range _bf .Points {_bf .Points [_fga ]=_bde .Add (offX ,offY );};return _bf ;};
|
||||
|
||||
// FlipX flips the sign of the Dx component of the vector.
|
||||
func (_dcg Vector )FlipX ()Vector {_dcg .Dx =-_dcg .Dx ;return _dcg };
|
||||
|
||||
// Offset shifts the Bezier path with the specified offsets.
|
||||
func (_ec CubicBezierPath )Offset (offX ,offY float64 )CubicBezierPath {for _da ,_fc :=range _ec .Curves {_ec .Curves [_da ]=_fc .AddOffsetXY (offX ,offY );};return _ec ;};
|
||||
// GetPolarAngle returns the angle the magnitude of the vector forms with the
|
||||
// positive X-axis going counterclockwise.
|
||||
func (_egf Vector )GetPolarAngle ()float64 {return _a .Atan2 (_egf .Dy ,_egf .Dx )};
|
||||
|
||||
// CubicBezierCurve is defined by:
|
||||
// R(t) = P0*(1-t)^3 + P1*3*t*(1-t)^2 + P2*3*t^2*(1-t) + P3*t^3
|
||||
// where P0 is the current point, P1, P2 control points and P3 the final point.
|
||||
type CubicBezierCurve struct{P0 Point ;P1 Point ;P2 Point ;P3 Point ;};
|
||||
|
||||
// Draw draws the composite Bezier curve. A graphics state name can be
|
||||
// specified for setting the curve properties (e.g. setting the opacity).
|
||||
// Otherwise leave empty (""). Returns the content stream as a byte array and
|
||||
// the curve bounding box.
|
||||
func (_bda PolyBezierCurve )Draw (gsName string )([]byte ,*_a .PdfRectangle ,error ){if _bda .BorderColor ==nil {_bda .BorderColor =_a .NewPdfColorDeviceRGB (0,0,0);};_aea :=NewCubicBezierPath ();for _ ,_bcb :=range _bda .Curves {_aea =_aea .AppendCurve (_bcb );};_dd :=_df .NewContentCreator ();_dd .Add_q ();_bda .FillEnabled =_bda .FillEnabled &&_bda .FillColor !=nil ;if _bda .FillEnabled {_dd .Add_rg (_bda .FillColor .R (),_bda .FillColor .G (),_bda .FillColor .B ());};_dd .Add_RG (_bda .BorderColor .R (),_bda .BorderColor .G (),_bda .BorderColor .B ());_dd .Add_w (_bda .BorderWidth );if len (gsName )> 1{_dd .Add_gs (_f .PdfObjectName (gsName ));};for _ ,_fce :=range _aea .Curves {_dd .Add_m (_fce .P0 .X ,_fce .P0 .Y );_dd .Add_c (_fce .P1 .X ,_fce .P1 .Y ,_fce .P2 .X ,_fce .P2 .Y ,_fce .P3 .X ,_fce .P3 .Y );};if _bda .FillEnabled {_dd .Add_h ();_dd .Add_B ();}else {_dd .Add_S ();};_dd .Add_Q ();return _dd .Bytes (),_aea .GetBoundingBox ().ToPdfRectangle (),nil ;};
|
||||
// GetBoundingBox returns the bounding box of the Bezier path.
|
||||
func (_ag CubicBezierPath )GetBoundingBox ()Rectangle {_ec :=Rectangle {};_af :=0.0;_ggb :=0.0;_ecb :=0.0;_ce :=0.0;for _fb ,_da :=range _ag .Curves {_gaa :=_da .GetBounds ();if _fb ==0{_af =_gaa .Llx ;_ggb =_gaa .Urx ;_ecb =_gaa .Lly ;_ce =_gaa .Ury ;continue ;};if _gaa .Llx < _af {_af =_gaa .Llx ;};if _gaa .Urx > _ggb {_ggb =_gaa .Urx ;};if _gaa .Lly < _ecb {_ecb =_gaa .Lly ;};if _gaa .Ury > _ce {_ce =_gaa .Ury ;};};_ec .X =_af ;_ec .Y =_ecb ;_ec .Width =_ggb -_af ;_ec .Height =_ce -_ecb ;return _ec ;};
|
||||
|
||||
// Line defines a line shape between point 1 (X1,Y1) and point 2 (X2,Y2). The line ending styles can be none (regular line),
|
||||
// or arrows at either end. The line also has a specified width, color and opacity.
|
||||
type Line struct{X1 float64 ;Y1 float64 ;X2 float64 ;Y2 float64 ;LineColor *_a .PdfColorDeviceRGB ;Opacity float64 ;LineWidth float64 ;LineEndingStyle1 LineEndingStyle ;LineEndingStyle2 LineEndingStyle ;LineStyle LineStyle ;};
|
||||
// NewPath returns a new empty path.
|
||||
func NewPath ()Path {return Path {}};
|
||||
|
||||
// Draw draws the polygon. A graphics state name can be specified for
|
||||
// setting the polygon properties (e.g. setting the opacity). Otherwise leave
|
||||
// empty (""). Returns the content stream as a byte array and the polygon
|
||||
// bounding box.
|
||||
func (_bae Polygon )Draw (gsName string )([]byte ,*_c .PdfRectangle ,error ){_cff :=_gg .NewContentCreator ();_cff .Add_q ();_bae .FillEnabled =_bae .FillEnabled &&_bae .FillColor !=nil ;if _bae .FillEnabled {_cff .Add_rg (_bae .FillColor .R (),_bae .FillColor .G (),_bae .FillColor .B ());};_bae .BorderEnabled =_bae .BorderEnabled &&_bae .BorderColor !=nil ;if _bae .BorderEnabled {_cff .Add_RG (_bae .BorderColor .R (),_bae .BorderColor .G (),_bae .BorderColor .B ());_cff .Add_w (_bae .BorderWidth );};if len (gsName )> 1{_cff .Add_gs (_cb .PdfObjectName (gsName ));};_fcf :=NewPath ();for _ ,_cgg :=range _bae .Points {for _bab ,_dd :=range _cgg {_fcf =_fcf .AppendPoint (_dd );if _bab ==0{_cff .Add_m (_dd .X ,_dd .Y );}else {_cff .Add_l (_dd .X ,_dd .Y );};};_cff .Add_h ();};if _bae .FillEnabled &&_bae .BorderEnabled {_cff .Add_B ();}else if _bae .FillEnabled {_cff .Add_f ();}else if _bae .BorderEnabled {_cff .Add_S ();};_cff .Add_Q ();return _cff .Bytes (),_fcf .GetBoundingBox ().ToPdfRectangle (),nil ;};
|
||||
|
||||
// BasicLine defines a line between point 1 (X1,Y1) and point 2 (X2,Y2). The line has a specified width, color and opacity.
|
||||
type BasicLine struct{X1 float64 ;Y1 float64 ;X2 float64 ;Y2 float64 ;LineColor *_c .PdfColorDeviceRGB ;Opacity float64 ;LineWidth float64 ;LineStyle LineStyle ;};
|
||||
|
||||
// Draw draws the rectangle. Can specify a graphics state (gsName) for setting opacity etc.
|
||||
// Otherwise leave empty (""). Returns the content stream as a byte array, bounding box and an error on failure.
|
||||
func (_ccb Rectangle )Draw (gsName string )([]byte ,*_c .PdfRectangle ,error ){_ee :=NewPath ();_ee =_ee .AppendPoint (NewPoint (0,0));_ee =_ee .AppendPoint (NewPoint (0,_ccb .Height ));_ee =_ee .AppendPoint (NewPoint (_ccb .Width ,_ccb .Height ));_ee =_ee .AppendPoint (NewPoint (_ccb .Width ,0));_ee =_ee .AppendPoint (NewPoint (0,0));if _ccb .X !=0||_ccb .Y !=0{_ee =_ee .Offset (_ccb .X ,_ccb .Y );};_dca :=_gg .NewContentCreator ();_dca .Add_q ();if _ccb .FillEnabled {_dca .Add_rg (_ccb .FillColor .R (),_ccb .FillColor .G (),_ccb .FillColor .B ());};if _ccb .BorderEnabled {_dca .Add_RG (_ccb .BorderColor .R (),_ccb .BorderColor .G (),_ccb .BorderColor .B ());_dca .Add_w (_ccb .BorderWidth );};if len (gsName )> 1{_dca .Add_gs (_cb .PdfObjectName (gsName ));};DrawPathWithCreator (_ee ,_dca );_dca .Add_h ();if _ccb .FillEnabled &&_ccb .BorderEnabled {_dca .Add_B ();}else if _ccb .FillEnabled {_dca .Add_f ();}else if _ccb .BorderEnabled {_dca .Add_S ();};_dca .Add_Q ();return _dca .Bytes (),_ee .GetBoundingBox ().ToPdfRectangle (),nil ;};
|
||||
|
||||
// GetPointNumber returns the path point at the index specified by number.
|
||||
// The index is 1-based.
|
||||
func (_bc Path )GetPointNumber (number int )Point {if number < 1||number > len (_bc .Points ){return Point {};};return _bc .Points [number -1];};
|
||||
|
||||
// Polygon is a multi-point shape that can be drawn to a PDF content stream.
|
||||
type Polygon struct{Points [][]Point ;FillEnabled bool ;FillColor *_c .PdfColorDeviceRGB ;BorderEnabled bool ;BorderColor *_c .PdfColorDeviceRGB ;BorderWidth float64 ;};
|
||||
|
||||
// NewVectorPolar returns a new vector calculated from the specified
|
||||
// magnitude and angle.
|
||||
func NewVectorPolar (length float64 ,theta float64 )Vector {_fcbe :=Vector {};_fcbe .Dx =length *_a .Cos (theta );_fcbe .Dy =length *_a .Sin (theta );return _fcbe ;};
|
||||
|
||||
// NewPoint returns a new point with the coordinates x, y.
|
||||
func NewPoint (x ,y float64 )Point {return Point {X :x ,Y :y }};
|
||||
|
||||
// Rotate returns a new Point at `p` rotated by `theta` degrees.
|
||||
func (_eae Point )Rotate (theta float64 )Point {_ecg :=_gd .NewPoint (_eae .X ,_eae .Y ).Rotate (theta );return NewPoint (_ecg .X ,_ecg .Y );};
|
||||
|
||||
// ToPdfRectangle returns the rectangle as a PDF rectangle.
|
||||
func (_ccfa Rectangle )ToPdfRectangle ()*_c .PdfRectangle {return &_c .PdfRectangle {Llx :_ccfa .X ,Lly :_ccfa .Y ,Urx :_ccfa .X +_ccfa .Width ,Ury :_ccfa .Y +_ccfa .Height };};
|
||||
|
||||
// DrawBezierPathWithCreator makes the bezier path with the content creator.
|
||||
// Adds the PDF commands to draw the path to the creator instance.
|
||||
func DrawBezierPathWithCreator (bpath CubicBezierPath ,creator *_gg .ContentCreator ){for _cffg ,_ece :=range bpath .Curves {if _cffg ==0{creator .Add_m (_ece .P0 .X ,_ece .P0 .Y );};creator .Add_c (_ece .P1 .X ,_ece .P1 .Y ,_ece .P2 .X ,_ece .P2 .Y ,_ece .P3 .X ,_ece .P3 .Y );};};
|
||||
|
||||
// CubicBezierPath represents a collection of cubic Bezier curves.
|
||||
type CubicBezierPath struct{Curves []CubicBezierCurve ;};
|
||||
|
||||
// NewPath returns a new empty path.
|
||||
func NewPath ()Path {return Path {}};
|
||||
// GetBounds returns the bounding box of the Bezier curve.
|
||||
func (_dcg CubicBezierCurve )GetBounds ()_c .PdfRectangle {_ad :=_dcg .P0 .X ;_aa :=_dcg .P0 .X ;_f :=_dcg .P0 .Y ;_fa :=_dcg .P0 .Y ;for _ga :=0.0;_ga <=1.0;_ga +=0.001{Rx :=_dcg .P0 .X *_a .Pow (1-_ga ,3)+_dcg .P1 .X *3*_ga *_a .Pow (1-_ga ,2)+_dcg .P2 .X *3*_a .Pow (_ga ,2)*(1-_ga )+_dcg .P3 .X *_a .Pow (_ga ,3);Ry :=_dcg .P0 .Y *_a .Pow (1-_ga ,3)+_dcg .P1 .Y *3*_ga *_a .Pow (1-_ga ,2)+_dcg .P2 .Y *3*_a .Pow (_ga ,2)*(1-_ga )+_dcg .P3 .Y *_a .Pow (_ga ,3);if Rx < _ad {_ad =Rx ;};if Rx > _aa {_aa =Rx ;};if Ry < _f {_f =Ry ;};if Ry > _fa {_fa =Ry ;};};_gdd :=_c .PdfRectangle {};_gdd .Llx =_ad ;_gdd .Lly =_f ;_gdd .Urx =_aa ;_gdd .Ury =_fa ;return _gdd ;};
|
||||
|
||||
// Circle represents a circle shape with fill and border properties that can be drawn to a PDF content stream.
|
||||
type Circle struct{X float64 ;Y float64 ;Width float64 ;Height float64 ;FillEnabled bool ;FillColor *_c .PdfColorDeviceRGB ;BorderEnabled bool ;BorderWidth float64 ;BorderColor *_c .PdfColorDeviceRGB ;Opacity float64 ;};
|
||||
|
||||
// Flip changes the sign of the vector: -vector.
|
||||
func (_bbc Vector )Flip ()Vector {_gddf :=_bbc .Magnitude ();_ecge :=_bbc .GetPolarAngle ();_bbc .Dx =_gddf *_a .Cos (_ecge +_a .Pi );_bbc .Dy =_gddf *_a .Sin (_ecge +_a .Pi );return _bbc ;};
|
||||
|
||||
// Polyline defines a slice of points that are connected as straight lines.
|
||||
type Polyline struct{Points []Point ;LineColor *_c .PdfColorDeviceRGB ;LineWidth float64 ;};
|
||||
|
||||
// Add adds the specified vector to the current one and returns the result.
|
||||
func (_dcc Vector )Add (other Vector )Vector {_dcc .Dx +=other .Dx ;_dcc .Dy +=other .Dy ;return _dcc };
|
||||
|
||||
// GetBoundingBox returns the bounding box of the path.
|
||||
func (_dfb Path )GetBoundingBox ()BoundingBox {_gc :=BoundingBox {};_aee :=0.0;_cf :=0.0;_cad :=0.0;_afg :=0.0;for _bd ,_ba :=range _dfb .Points {if _bd ==0{_aee =_ba .X ;_cf =_ba .X ;_cad =_ba .Y ;_afg =_ba .Y ;continue ;};if _ba .X < _aee {_aee =_ba .X ;};if _ba .X > _cf {_cf =_ba .X ;};if _ba .Y < _cad {_cad =_ba .Y ;};if _ba .Y > _afg {_afg =_ba .Y ;};};_gc .X =_aee ;_gc .Y =_cad ;_gc .Width =_cf -_aee ;_gc .Height =_afg -_cad ;return _gc ;};
|
||||
|
||||
// DrawPathWithCreator makes the path with the content creator.
|
||||
// Adds the PDF commands to draw the path to the creator instance.
|
||||
func DrawPathWithCreator (path Path ,creator *_gg .ContentCreator ){for _ffa ,_de :=range path .Points {if _ffa ==0{creator .Add_m (_de .X ,_de .Y );}else {creator .Add_l (_de .X ,_de .Y );};};};
|
||||
|
||||
// PolyBezierCurve represents a composite curve that is the result of
|
||||
// joining multiple cubic Bezier curves.
|
||||
type PolyBezierCurve struct{Curves []CubicBezierCurve ;BorderWidth float64 ;BorderColor *_c .PdfColorDeviceRGB ;FillEnabled bool ;FillColor *_c .PdfColorDeviceRGB ;};
|
||||
|
||||
// RemovePoint removes the point at the index specified by number from the
|
||||
// path. The index is 1-based.
|
||||
func (_ggbb Path )RemovePoint (number int )Path {if number < 1||number > len (_ggbb .Points ){return _ggbb ;};_ac :=number -1;_ggbb .Points =append (_ggbb .Points [:_ac ],_ggbb .Points [_ac +1:]...);return _ggbb ;};
|
||||
|
||||
// ToPdfRectangle returns the bounding box as a PDF rectangle.
|
||||
func (_fad BoundingBox )ToPdfRectangle ()*_c .PdfRectangle {return &_c .PdfRectangle {Llx :_fad .X ,Lly :_fad .Y ,Urx :_fad .X +_fad .Width ,Ury :_fad .Y +_fad .Height };};
|
||||
|
||||
// Path consists of straight line connections between each point defined in an array of points.
|
||||
type Path struct{Points []Point ;};const (LineEndingStyleNone LineEndingStyle =0;LineEndingStyleArrow LineEndingStyle =1;LineEndingStyleButt LineEndingStyle =2;);
|
||||
|
||||
// Length returns the number of points in the path.
|
||||
func (_cec Path )Length ()int {return len (_cec .Points )};
|
||||
|
||||
// FlipX flips the sign of the Dx component of the vector.
|
||||
func (_acg Vector )FlipX ()Vector {_acg .Dx =-_acg .Dx ;return _acg };
|
||||
|
||||
// LineStyle refers to how the line will be created.
|
||||
type LineStyle int ;
|
||||
|
||||
// Draw draws the line to PDF contentstream. Generates the content stream which can be used in page contents or
|
||||
// appearance stream of annotation. Returns the stream content, XForm bounding box (local), bounding box and an error
|
||||
// if one occurred.
|
||||
func (_gde Line )Draw (gsName string )([]byte ,*_c .PdfRectangle ,error ){_aaf ,_fcb :=_gde .X1 ,_gde .X2 ;_adgg ,_acca :=_gde .Y1 ,_gde .Y2 ;_cba :=_acca -_adgg ;_ef :=_fcb -_aaf ;_dab :=_a .Atan2 (_cba ,_ef );L :=_a .Sqrt (_a .Pow (_ef ,2.0)+_a .Pow (_cba ,2.0));_efe :=_gde .LineWidth ;_ccbg :=_a .Pi ;_fdd :=1.0;if _ef < 0{_fdd *=-1.0;};if _cba < 0{_fdd *=-1.0;};VsX :=_fdd *(-_efe /2*_a .Cos (_dab +_ccbg /2));VsY :=_fdd *(-_efe /2*_a .Sin (_dab +_ccbg /2)+_efe *_a .Sin (_dab +_ccbg /2));V1X :=VsX +_efe /2*_a .Cos (_dab +_ccbg /2);V1Y :=VsY +_efe /2*_a .Sin (_dab +_ccbg /2);V2X :=VsX +_efe /2*_a .Cos (_dab +_ccbg /2)+L *_a .Cos (_dab );V2Y :=VsY +_efe /2*_a .Sin (_dab +_ccbg /2)+L *_a .Sin (_dab );V3X :=VsX +_efe /2*_a .Cos (_dab +_ccbg /2)+L *_a .Cos (_dab )+_efe *_a .Cos (_dab -_ccbg /2);V3Y :=VsY +_efe /2*_a .Sin (_dab +_ccbg /2)+L *_a .Sin (_dab )+_efe *_a .Sin (_dab -_ccbg /2);V4X :=VsX +_efe /2*_a .Cos (_dab -_ccbg /2);V4Y :=VsY +_efe /2*_a .Sin (_dab -_ccbg /2);_dda :=NewPath ();_dda =_dda .AppendPoint (NewPoint (V1X ,V1Y ));_dda =_dda .AppendPoint (NewPoint (V2X ,V2Y ));_dda =_dda .AppendPoint (NewPoint (V3X ,V3Y ));_dda =_dda .AppendPoint (NewPoint (V4X ,V4Y ));_ddf :=_gde .LineEndingStyle1 ;_aeg :=_gde .LineEndingStyle2 ;_cadc :=3*_efe ;_bcg :=3*_efe ;_badf :=(_bcg -_efe )/2;if _aeg ==LineEndingStyleArrow {_adf :=_dda .GetPointNumber (2);_bfe :=NewVectorPolar (_cadc ,_dab +_ccbg );_eag :=_adf .AddVector (_bfe );_cga :=NewVectorPolar (_bcg /2,_dab +_ccbg /2);_ccc :=NewVectorPolar (_cadc ,_dab );_fbc :=NewVectorPolar (_badf ,_dab +_ccbg /2);_bac :=_eag .AddVector (_fbc );_cgf :=_ccc .Add (_cga .Flip ());_eaa :=_bac .AddVector (_cgf );_cbe :=_cga .Scale (2).Flip ().Add (_cgf .Flip ());_fe :=_eaa .AddVector (_cbe );_gcdb :=_eag .AddVector (NewVectorPolar (_efe ,_dab -_ccbg /2));_aeb :=NewPath ();_aeb =_aeb .AppendPoint (_dda .GetPointNumber (1));_aeb =_aeb .AppendPoint (_eag );_aeb =_aeb .AppendPoint (_bac );_aeb =_aeb .AppendPoint (_eaa );_aeb =_aeb .AppendPoint (_fe );_aeb =_aeb .AppendPoint (_gcdb );_aeb =_aeb .AppendPoint (_dda .GetPointNumber (4));_dda =_aeb ;};if _ddf ==LineEndingStyleArrow {_gafe :=_dda .GetPointNumber (1);_cfg :=_dda .GetPointNumber (_dda .Length ());_bea :=NewVectorPolar (_efe /2,_dab +_ccbg +_ccbg /2);_bg :=_gafe .AddVector (_bea );_dad :=NewVectorPolar (_cadc ,_dab ).Add (NewVectorPolar (_bcg /2,_dab +_ccbg /2));_aeed :=_bg .AddVector (_dad );_add :=NewVectorPolar (_badf ,_dab -_ccbg /2);_fgd :=_aeed .AddVector (_add );_efc :=NewVectorPolar (_cadc ,_dab );_adfb :=_cfg .AddVector (_efc );_bfb :=NewVectorPolar (_badf ,_dab +_ccbg +_ccbg /2);_bce :=_adfb .AddVector (_bfb );_baed :=_bg ;_gda :=NewPath ();_gda =_gda .AppendPoint (_bg );_gda =_gda .AppendPoint (_aeed );_gda =_gda .AppendPoint (_fgd );for _ ,_baedg :=range _dda .Points [1:len (_dda .Points )-1]{_gda =_gda .AppendPoint (_baedg );};_gda =_gda .AppendPoint (_adfb );_gda =_gda .AppendPoint (_bce );_gda =_gda .AppendPoint (_baed );_dda =_gda ;};_faa :=_gg .NewContentCreator ();_faa .Add_q ().Add_rg (_gde .LineColor .R (),_gde .LineColor .G (),_gde .LineColor .B ());if len (gsName )> 1{_faa .Add_gs (_cb .PdfObjectName (gsName ));};_dda =_dda .Offset (_gde .X1 ,_gde .Y1 );_bed :=_dda .GetBoundingBox ();DrawPathWithCreator (_dda ,_faa );if _gde .LineStyle ==LineStyleDashed {_faa .Add_d ([]int64 {1,1},0).Add_S ().Add_f ().Add_Q ();}else {_faa .Add_f ().Add_Q ();};return _faa .Bytes (),_bed .ToPdfRectangle (),nil ;};
|
||||
|
||||
// BoundingBox represents the smallest rectangular area that encapsulates an object.
|
||||
type BoundingBox struct{X float64 ;Y float64 ;Width float64 ;Height float64 ;};
|
||||
|
||||
// Draw draws the composite Bezier curve. A graphics state name can be
|
||||
// specified for setting the curve properties (e.g. setting the opacity).
|
||||
// Otherwise leave empty (""). Returns the content stream as a byte array and
|
||||
// the curve bounding box.
|
||||
func (_ab PolyBezierCurve )Draw (gsName string )([]byte ,*_c .PdfRectangle ,error ){if _ab .BorderColor ==nil {_ab .BorderColor =_c .NewPdfColorDeviceRGB (0,0,0);};_bad :=NewCubicBezierPath ();for _ ,_eg :=range _ab .Curves {_bad =_bad .AppendCurve (_eg );};_ccf :=_gg .NewContentCreator ();_ccf .Add_q ();_ab .FillEnabled =_ab .FillEnabled &&_ab .FillColor !=nil ;if _ab .FillEnabled {_ccf .Add_rg (_ab .FillColor .R (),_ab .FillColor .G (),_ab .FillColor .B ());};_ccf .Add_RG (_ab .BorderColor .R (),_ab .BorderColor .G (),_ab .BorderColor .B ());_ccf .Add_w (_ab .BorderWidth );if len (gsName )> 1{_ccf .Add_gs (_cb .PdfObjectName (gsName ));};for _ ,_be :=range _bad .Curves {_ccf .Add_m (_be .P0 .X ,_be .P0 .Y );_ccf .Add_c (_be .P1 .X ,_be .P1 .Y ,_be .P2 .X ,_be .P2 .Y ,_be .P3 .X ,_be .P3 .Y );};if _ab .FillEnabled {_ccf .Add_h ();_ccf .Add_B ();}else {_ccf .Add_S ();};_ccf .Add_Q ();return _ccf .Bytes (),_bad .GetBoundingBox ().ToPdfRectangle (),nil ;};
|
||||
|
||||
// AddVector adds vector to a point.
|
||||
func (_acc Point )AddVector (v Vector )Point {_acc .X +=v .Dx ;_acc .Y +=v .Dy ;return _acc };
|
||||
|
||||
// NewVectorBetween returns a new vector with the direction specified by
|
||||
// the subtraction of point a from point b (b-a).
|
||||
func NewVectorBetween (a Point ,b Point )Vector {_feb :=Vector {};_feb .Dx =b .X -a .X ;_feb .Dy =b .Y -a .Y ;return _feb ;};
|
||||
|
||||
// NewCubicBezierCurve returns a new cubic Bezier curve.
|
||||
func NewCubicBezierCurve (x0 ,y0 ,x1 ,y1 ,x2 ,y2 ,x3 ,y3 float64 )CubicBezierCurve {_d :=CubicBezierCurve {};_d .P0 =NewPoint (x0 ,y0 );_d .P1 =NewPoint (x1 ,y1 );_d .P2 =NewPoint (x2 ,y2 );_d .P3 =NewPoint (x3 ,y3 );return _d ;};
|
||||
|
||||
// Offset shifts the Bezier path with the specified offsets.
|
||||
func (_gf CubicBezierPath )Offset (offX ,offY float64 )CubicBezierPath {for _df ,_ff :=range _gf .Curves {_gf .Curves [_df ]=_ff .AddOffsetXY (offX ,offY );};return _gf ;};
|
||||
|
||||
// Rectangle is a shape with a specified Width and Height and a lower left corner at (X,Y) that can be
|
||||
// drawn to a PDF content stream. The rectangle can optionally have a border and a filling color.
|
||||
// The Width/Height includes the border (if any specified), i.e. is positioned inside.
|
||||
type Rectangle struct{X float64 ;Y float64 ;Width float64 ;Height float64 ;FillEnabled bool ;FillColor *_c .PdfColorDeviceRGB ;BorderEnabled bool ;BorderWidth float64 ;BorderColor *_c .PdfColorDeviceRGB ;Opacity float64 ;};
|
||||
|
||||
// Copy returns a clone of the path.
|
||||
func (_fc Path )Copy ()Path {_dcgg :=Path {};_dcgg .Points =[]Point {};for _ ,_fd :=range _fc .Points {_dcgg .Points =append (_dcgg .Points ,_fd );};return _dcgg ;};
|
||||
|
||||
// LineEndingStyle defines the line ending style for lines.
|
||||
// The currently supported line ending styles are None, Arrow (ClosedArrow) and Butt.
|
||||
type LineEndingStyle int ;
|
||||
|
||||
// Add shifts the coordinates of the point with dx, dy and returns the result.
|
||||
func (_db Point )Add (dx ,dy float64 )Point {_db .X +=dx ;_db .Y +=dy ;return _db };
|
||||
|
||||
// AddOffsetXY adds X,Y offset to all points on a curve.
|
||||
func (_dc CubicBezierCurve )AddOffsetXY (offX ,offY float64 )CubicBezierCurve {_dc .P0 .X +=offX ;_dc .P1 .X +=offX ;_dc .P2 .X +=offX ;_dc .P3 .X +=offX ;_dc .P0 .Y +=offY ;_dc .P1 .Y +=offY ;_dc .P2 .Y +=offY ;_dc .P3 .Y +=offY ;return _dc ;};func (_gb Point )String ()string {return _e .Sprintf ("(\u0025\u002e\u0031\u0066\u002c\u0025\u002e\u0031\u0066\u0029",_gb .X ,_gb .Y );};
|
||||
|
||||
// FlipY flips the sign of the Dy component of the vector.
|
||||
func (_fab Vector )FlipY ()Vector {_fab .Dy =-_fab .Dy ;return _fab };const (LineStyleSolid LineStyle =0;LineStyleDashed LineStyle =1;);
|
||||
|
||||
// Draw draws the polyline. A graphics state name can be specified for
|
||||
// setting the polyline properties (e.g. setting the opacity). Otherwise leave
|
||||
// empty (""). Returns the content stream as a byte array and the polyline
|
||||
// bounding box.
|
||||
func (_fcc Polyline )Draw (gsName string )([]byte ,*_c .PdfRectangle ,error ){if _fcc .LineColor ==nil {_fcc .LineColor =_c .NewPdfColorDeviceRGB (0,0,0);};_eff :=NewPath ();for _ ,_addg :=range _fcc .Points {_eff =_eff .AppendPoint (_addg );};_fca :=_gg .NewContentCreator ();_fca .Add_q ();_fca .Add_RG (_fcc .LineColor .R (),_fcc .LineColor .G (),_fcc .LineColor .B ());_fca .Add_w (_fcc .LineWidth );if len (gsName )> 1{_fca .Add_gs (_cb .PdfObjectName (gsName ));};DrawPathWithCreator (_eff ,_fca );_fca .Add_S ();_fca .Add_Q ();return _fca .Bytes (),_eff .GetBoundingBox ().ToPdfRectangle (),nil ;};
|
||||
|
||||
// AppendPoint adds the specified point to the path.
|
||||
func (_fg Path )AppendPoint (point Point )Path {_fg .Points =append (_fg .Points ,point );return _fg };
|
||||
|
||||
// NewVector returns a new vector with the direction specified by dx and dy.
|
||||
func NewVector (dx ,dy float64 )Vector {_fdb :=Vector {};_fdb .Dx =dx ;_fdb .Dy =dy ;return _fdb };
|
||||
|
||||
// NewCubicBezierPath returns a new empty cubic Bezier path.
|
||||
func NewCubicBezierPath ()CubicBezierPath {_cc :=CubicBezierPath {};_cc .Curves =[]CubicBezierCurve {};return _cc ;};
|
||||
|
||||
// Offset shifts the path with the specified offsets.
|
||||
func (_afe Path )Offset (offX ,offY float64 )Path {for _fdg ,_fgc :=range _afe .Points {_afe .Points [_fdg ]=_fgc .Add (offX ,offY );};return _afe ;};
|
1766
core/core.go
1766
core/core.go
File diff suppressed because one or more lines are too long
@ -9,64 +9,34 @@
|
||||
// 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 crypt ;import (_gd "crypto/aes";_b "crypto/cipher";_f "crypto/md5";_ac "crypto/rand";_e "crypto/rc4";_gc "fmt";_fb "github.com/unidoc/unipdf/v3/common";_ed "github.com/unidoc/unipdf/v3/core/security";_a "io";);func init (){_dce ("\u0041\u0045\u0053V\u0032",_c )};
|
||||
package crypt ;import (_fe "crypto/aes";_g "crypto/cipher";_d "crypto/md5";_dc "crypto/rand";_c "crypto/rc4";_e "fmt";_b "github.com/unidoc/unipdf/v3/common";_ef "github.com/unidoc/unipdf/v3/core/security";_f "io";);func init (){_acg ("\u0041\u0045\u0053V\u0032",_eca )};func _ge (_ba ,_ecc uint32 ,_aga []byte ,_ga bool )([]byte ,error ){_bf :=make ([]byte ,len (_aga )+5);for _gf :=0;_gf < len (_aga );_gf ++{_bf [_gf ]=_aga [_gf ];};for _aa :=0;_aa < 3;_aa ++{_bfe :=byte ((_ba >>uint32 (8*_aa ))&0xff);_bf [_aa +len (_aga )]=_bfe ;};for _dag :=0;_dag < 2;_dag ++{_ccf :=byte ((_ecc >>uint32 (8*_dag ))&0xff);_bf [_dag +len (_aga )+3]=_ccf ;};if _ga {_bf =append (_bf ,0x73);_bf =append (_bf ,0x41);_bf =append (_bf ,0x6C);_bf =append (_bf ,0x54);};_gag :=_d .New ();_gag .Write (_bf );_fegb :=_gag .Sum (nil );if len (_aga )+5< 16{return _fegb [0:len (_aga )+5],nil ;};return _fegb ,nil ;};
|
||||
|
||||
// NewFilterV2 creates a RC4-based filter with a specified key length (in bytes).
|
||||
func NewFilterV2 (length int )Filter {_fg ,_bfa :=_fec (FilterDict {Length :length });if _bfa !=nil {_fb .Log .Error ("E\u0052\u0052\u004f\u0052\u003a\u0020\u0063\u006f\u0075l\u0064\u0020\u006e\u006f\u0074\u0020\u0063re\u0061\u0074\u0065\u0020R\u0043\u0034\u0020\u0056\u0032\u0020\u0063\u0072\u0079pt\u0020\u0066i\u006c\u0074\u0065\u0072\u003a\u0020\u0025\u0076",_bfa );return filterV2 {_bb :length };};return _fg ;};
|
||||
// Name implements Filter interface.
|
||||
func (filterAESV2 )Name ()string {return "\u0041\u0045\u0053V\u0032"};func (filterIdentity )EncryptBytes (p []byte ,okey []byte )([]byte ,error ){return p ,nil };func _acg (_dec string ,_gfd filterFunc ){if _ ,_dfg :=_dd [_dec ];_dfg {panic ("\u0061l\u0072e\u0061\u0064\u0079\u0020\u0072e\u0067\u0069s\u0074\u0065\u0072\u0065\u0064");};_dd [_dec ]=_gfd ;};func _ag (_ae FilterDict )(Filter ,error ){if _ae .Length ==256{_b .Log .Debug ("\u0041\u0045S\u0056\u0033\u0020c\u0072\u0079\u0070\u0074\u0020f\u0069\u006c\u0074\u0065\u0072 l\u0065\u006e\u0067\u0074\u0068\u0020\u0061\u0070\u0070\u0065\u0061\u0072\u0073\u0020\u0074\u006f\u0020\u0062e\u0020i\u006e\u0020\u0062\u0069\u0074\u0073 ra\u0074\u0068\u0065\u0072\u0020\u0074\u0068\u0061\u006e\u0020\u0062\u0079te\u0073 \u002d\u0020\u0061\u0073s\u0075m\u0069n\u0067\u0020b\u0069\u0074s \u0028\u0025\u0064\u0029",_ae .Length );_ae .Length /=8;};if _ae .Length !=0&&_ae .Length !=32{return nil ,_e .Errorf ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0041\u0045\u0053\u0056\u0033\u0020\u0063\u0072\u0079\u0070\u0074\u0020\u0066\u0069\u006c\u0074e\u0072\u0020\u006c\u0065\u006eg\u0074\u0068 \u0028\u0025\u0064\u0029",_ae .Length );};return filterAESV3 {},nil ;};
|
||||
|
||||
// PDFVersion implements Filter interface.
|
||||
func (_bbef filterV2 )PDFVersion ()[2]int {return [2]int {}};
|
||||
|
||||
// KeyLength implements Filter interface.
|
||||
func (_gff filterV2 )KeyLength ()int {return _gff ._ggf };
|
||||
|
||||
// Name implements Filter interface.
|
||||
func (filterV2 )Name ()string {return "\u0056\u0032"};
|
||||
|
||||
// PDFVersion implements Filter interface.
|
||||
func (filterAESV2 )PDFVersion ()[2]int {return [2]int {1,5}};func init (){_acg ("\u0041\u0045\u0053V\u0033",_ag )};func (filterIdentity )PDFVersion ()[2]int {return [2]int {}};
|
||||
|
||||
// KeyLength implements Filter interface.
|
||||
func (filterAESV2 )KeyLength ()int {return 128/8};
|
||||
|
||||
// NewIdentity creates an identity filter that bypasses all data without changes.
|
||||
func NewIdentity ()Filter {return filterIdentity {}};
|
||||
|
||||
// HandlerVersion implements Filter interface.
|
||||
func (_ead filterV2 )HandlerVersion ()(V ,R int ){V ,R =2,3;return ;};
|
||||
|
||||
// NewFilterAESV3 creates an AES-based filter with a 256 bit key (AESV3).
|
||||
func NewFilterAESV3 ()Filter {_ad ,_d :=_ec (FilterDict {});if _d !=nil {_fb .Log .Error ("E\u0052\u0052\u004f\u0052\u003a\u0020\u0063\u006f\u0075l\u0064\u0020\u006e\u006f\u0074\u0020\u0063re\u0061\u0074\u0065\u0020A\u0045\u0053\u0020\u0056\u0033\u0020\u0063\u0072\u0079pt\u0020\u0066i\u006c\u0074\u0065\u0072\u003a\u0020\u0025\u0076",_d );return filterAESV3 {};};return _ad ;};
|
||||
|
||||
// NewFilter creates CryptFilter from a corresponding dictionary.
|
||||
func NewFilter (d FilterDict )(Filter ,error ){_cfg ,_acd :=_fff (d .CFM );if _acd !=nil {return nil ,_acd ;};_dcg ,_acd :=_cfg (d );if _acd !=nil {return nil ,_acd ;};return _dcg ,nil ;};type filterAESV2 struct{filterAES };func (filterIdentity )KeyLength ()int {return 0};
|
||||
|
||||
// KeyLength implements Filter interface.
|
||||
func (_gdc filterV2 )KeyLength ()int {return _gdc ._bb };func init (){_dce ("\u0041\u0045\u0053V\u0033",_ec )};func (filterIdentity )HandlerVersion ()(V ,R int ){return ;};
|
||||
|
||||
// NewFilterAESV2 creates an AES-based filter with a 128 bit key (AESV2).
|
||||
func NewFilterAESV2 ()Filter {_gcb ,_gdf :=_c (FilterDict {});if _gdf !=nil {_fb .Log .Error ("E\u0052\u0052\u004f\u0052\u003a\u0020\u0063\u006f\u0075l\u0064\u0020\u006e\u006f\u0074\u0020\u0063re\u0061\u0074\u0065\u0020A\u0045\u0053\u0020\u0056\u0032\u0020\u0063\u0072\u0079pt\u0020\u0066i\u006c\u0074\u0065\u0072\u003a\u0020\u0025\u0076",_gdf );return filterAESV2 {};};return _gcb ;};func _fff (_ca string )(filterFunc ,error ){_bg :=_ada [string (_ca )];if _bg ==nil {return nil ,_gc .Errorf ("\u0075\u006e\u0073\u0075p\u0070\u006f\u0072\u0074\u0065\u0064\u0020\u0063\u0072\u0079p\u0074 \u0066\u0069\u006c\u0074\u0065\u0072\u003a \u0025\u0071",_ca );};return _bg ,nil ;};
|
||||
|
||||
// PDFVersion implements Filter interface.
|
||||
func (filterAESV2 )PDFVersion ()[2]int {return [2]int {1,5}};
|
||||
|
||||
// PDFVersion implements Filter interface.
|
||||
func (_gaf filterV2 )PDFVersion ()[2]int {return [2]int {}};
|
||||
|
||||
// HandlerVersion implements Filter interface.
|
||||
func (filterAESV2 )HandlerVersion ()(V ,R int ){V ,R =4,4;return ;};type filterFunc func (_cee FilterDict )(Filter ,error );type filterAESV3 struct{filterAES };
|
||||
|
||||
// MakeKey implements Filter interface.
|
||||
func (filterAESV3 )MakeKey (_ ,_ uint32 ,ekey []byte )([]byte ,error ){return ekey ,nil };
|
||||
|
||||
// DecryptBytes implements Filter interface.
|
||||
func (filterV2 )DecryptBytes (buf []byte ,okey []byte )([]byte ,error ){_dda ,_bdfa :=_e .NewCipher (okey );if _bdfa !=nil {return nil ,_bdfa ;};_fb .Log .Trace ("\u0052\u00434\u0020\u0044\u0065c\u0072\u0079\u0070\u0074\u003a\u0020\u0025\u0020\u0078",buf );_dda .XORKeyStream (buf ,buf );_fb .Log .Trace ("\u0074o\u003a\u0020\u0025\u0020\u0078",buf );return buf ,nil ;};
|
||||
|
||||
// Name implements Filter interface.
|
||||
func (filterAESV2 )Name ()string {return "\u0041\u0045\u0053V\u0032"};func _fec (_eac FilterDict )(Filter ,error ){if _eac .Length %8!=0{return nil ,_gc .Errorf ("\u0063\u0072\u0079p\u0074\u0020\u0066\u0069\u006c\u0074\u0065\u0072\u0020\u006c\u0065\u006e\u0067\u0074\u0068\u0020\u006e\u006f\u0074\u0020\u006d\u0075\u006c\u0074\u0069\u0070\u006c\u0065\u0020o\u0066\u0020\u0038\u0020\u0028\u0025\u0064\u0029",_eac .Length );};if _eac .Length < 5||_eac .Length > 16{if _eac .Length ==40||_eac .Length ==64||_eac .Length ==128{_fb .Log .Debug ("\u0053\u0054\u0041\u004e\u0044AR\u0044\u0020V\u0049\u004f\u004c\u0041\u0054\u0049\u004f\u004e\u003a\u0020\u0043\u0072\u0079\u0070\u0074\u0020\u004c\u0065\u006e\u0067\u0074\u0068\u0020\u0061\u0070\u0070\u0065\u0061\u0072s\u0020\u0074\u006f \u0062\u0065\u0020\u0069\u006e\u0020\u0062\u0069\u0074\u0073\u0020\u0072\u0061t\u0068\u0065\u0072\u0020\u0074h\u0061\u006e\u0020\u0062\u0079\u0074\u0065\u0073\u0020-\u0020\u0061s\u0073u\u006d\u0069\u006e\u0067\u0020\u0062\u0069t\u0073\u0020\u0028\u0025\u0064\u0029",_eac .Length );_eac .Length /=8;}else {return nil ,_gc .Errorf ("\u0063\u0072\u0079\u0070\u0074\u0020\u0066\u0069\u006c\u0074\u0065\u0072\u0020\u006c\u0065\u006e\u0067\u0074h\u0020\u006e\u006f\u0074\u0020\u0069\u006e \u0072\u0061\u006e\u0067\u0065\u0020\u0034\u0030\u0020\u002d\u00201\u0032\u0038\u0020\u0062\u0069\u0074\u0020\u0028\u0025\u0064\u0029",_eac .Length );};};return filterV2 {_bb :_eac .Length },nil ;};func init (){_dce ("\u0056\u0032",_fec )};
|
||||
func (filterAESV3 )Name ()string {return "\u0041\u0045\u0053V\u0033"};func (filterAES )EncryptBytes (buf []byte ,okey []byte )([]byte ,error ){_ca ,_gd :=_fe .NewCipher (okey );if _gd !=nil {return nil ,_gd ;};_b .Log .Trace ("A\u0045\u0053\u0020\u0045nc\u0072y\u0070\u0074\u0020\u0028\u0025d\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );const _fc =_fe .BlockSize ;_efc :=_fc -len (buf )%_fc ;for _gda :=0;_gda < _efc ;_gda ++{buf =append (buf ,byte (_efc ));};_b .Log .Trace ("\u0050a\u0064d\u0065\u0064\u0020\u0074\u006f \u0025\u0064 \u0062\u0079\u0074\u0065\u0073",len (buf ));_fa :=make ([]byte ,_fc +len (buf ));_ac :=_fa [:_fc ];if _ ,_da :=_f .ReadFull (_dc .Reader ,_ac );_da !=nil {return nil ,_da ;};_eg :=_g .NewCBCEncrypter (_ca ,_ac );_eg .CryptBlocks (_fa [_fc :],buf );buf =_fa ;_b .Log .Trace ("\u0074\u006f\u0020(\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );return buf ,nil ;};
|
||||
|
||||
// HandlerVersion implements Filter interface.
|
||||
func (filterAESV3 )HandlerVersion ()(V ,R int ){V ,R =5,6;return ;};
|
||||
func (filterAESV3 )HandlerVersion ()(V ,R int ){V ,R =5,6;return ;};func _eca (_eff FilterDict )(Filter ,error ){if _eff .Length ==128{_b .Log .Debug ("\u0041\u0045S\u0056\u0032\u0020c\u0072\u0079\u0070\u0074\u0020f\u0069\u006c\u0074\u0065\u0072 l\u0065\u006e\u0067\u0074\u0068\u0020\u0061\u0070\u0070\u0065\u0061\u0072\u0073\u0020\u0074\u006f\u0020\u0062e\u0020i\u006e\u0020\u0062\u0069\u0074\u0073 ra\u0074\u0068\u0065\u0072\u0020\u0074\u0068\u0061\u006e\u0020\u0062\u0079te\u0073 \u002d\u0020\u0061\u0073s\u0075m\u0069n\u0067\u0020b\u0069\u0074s \u0028\u0025\u0064\u0029",_eff .Length );_eff .Length /=8;};if _eff .Length !=0&&_eff .Length !=16{return nil ,_e .Errorf ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0041\u0045\u0053\u0056\u0032\u0020\u0063\u0072\u0079\u0070\u0074\u0020\u0066\u0069\u006c\u0074e\u0072\u0020\u006c\u0065\u006eg\u0074\u0068 \u0028\u0025\u0064\u0029",_eff .Length );};return filterAESV2 {},nil ;};func (filterAES )DecryptBytes (buf []byte ,okey []byte )([]byte ,error ){_cee ,_acb :=_fe .NewCipher (okey );if _acb !=nil {return nil ,_acb ;};if len (buf )< 16{_b .Log .Debug ("\u0045R\u0052\u004f\u0052\u0020\u0041\u0045\u0053\u0020\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0062\u0075\u0066\u0020\u0025\u0073",buf );return buf ,_e .Errorf ("\u0041\u0045\u0053\u003a B\u0075\u0066\u0020\u006c\u0065\u006e\u0020\u003c\u0020\u0031\u0036\u0020\u0028\u0025d\u0029",len (buf ));};_de :=buf [:16];buf =buf [16:];if len (buf )%16!=0{_b .Log .Debug ("\u0020\u0069\u0076\u0020\u0028\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (_de ),_de );_b .Log .Debug ("\u0062\u0075\u0066\u0020\u0028\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );return buf ,_e .Errorf ("\u0041\u0045\u0053\u0020\u0062\u0075\u0066\u0020\u006c\u0065\u006e\u0067\u0074\u0068\u0020\u006e\u006f\u0074\u0020\u006d\u0075\u006c\u0074\u0069p\u006c\u0065\u0020\u006f\u0066 \u0031\u0036 \u0028\u0025\u0064\u0029",len (buf ));};_ecg :=_g .NewCBCDecrypter (_cee ,_de );_b .Log .Trace ("A\u0045\u0053\u0020\u0044ec\u0072y\u0070\u0074\u0020\u0028\u0025d\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );_b .Log .Trace ("\u0063\u0068\u006f\u0070\u0020\u0041\u0045\u0053\u0020\u0044\u0065c\u0072\u0079\u0070\u0074\u0020\u0028\u0025\u0064\u0029\u003a \u0025\u0020\u0078",len (buf ),buf );_ecg .CryptBlocks (buf ,buf );_b .Log .Trace ("\u0074\u006f\u0020(\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );if len (buf )==0{_b .Log .Trace ("\u0045\u006d\u0070\u0074\u0079\u0020b\u0075\u0066\u002c\u0020\u0072\u0065\u0074\u0075\u0072\u006e\u0069\u006e\u0067 \u0065\u006d\u0070\u0074\u0079\u0020\u0073t\u0072\u0069\u006e\u0067");return buf ,nil ;};_fb :=int (buf [len (buf )-1]);if _fb > len (buf ){_b .Log .Debug ("\u0049\u006c\u006c\u0065g\u0061\u006c\u0020\u0070\u0061\u0064\u0020\u006c\u0065\u006eg\u0074h\u0020\u0028\u0025\u0064\u0020\u003e\u0020%\u0064\u0029",_fb ,len (buf ));return buf ,_e .Errorf ("\u0069n\u0076a\u006c\u0069\u0064\u0020\u0070a\u0064\u0020l\u0065\u006e\u0067\u0074\u0068");};buf =buf [:len (buf )-_fb ];return buf ,nil ;};
|
||||
|
||||
// EncryptBytes implements Filter interface.
|
||||
func (filterV2 )EncryptBytes (buf []byte ,okey []byte )([]byte ,error ){_ddf ,_gcg :=_e .NewCipher (okey );if _gcg !=nil {return nil ,_gcg ;};_fb .Log .Trace ("\u0052\u00434\u0020\u0045\u006ec\u0072\u0079\u0070\u0074\u003a\u0020\u0025\u0020\u0078",buf );_ddf .XORKeyStream (buf ,buf );_fb .Log .Trace ("\u0074o\u003a\u0020\u0025\u0020\u0078",buf );return buf ,nil ;};func (filterIdentity )PDFVersion ()[2]int {return [2]int {}};
|
||||
|
||||
// FilterDict represents information from a CryptFilter dictionary.
|
||||
type FilterDict struct{CFM string ;AuthEvent _ed .AuthEvent ;Length int ;};type filterAES struct{};
|
||||
|
||||
// PDFVersion implements Filter interface.
|
||||
func (filterAESV3 )PDFVersion ()[2]int {return [2]int {2,0}};var _ Filter =filterV2 {};
|
||||
|
||||
// Name implements Filter interface.
|
||||
func (filterV2 )Name ()string {return "\u0056\u0032"};
|
||||
// HandlerVersion implements Filter interface.
|
||||
func (_ea filterV2 )HandlerVersion ()(V ,R int ){V ,R =2,3;return ;};func (filterIdentity )DecryptBytes (p []byte ,okey []byte )([]byte ,error ){return p ,nil };type filterFunc func (_cb FilterDict )(Filter ,error );
|
||||
|
||||
// Filter is a common interface for crypt filter methods.
|
||||
type Filter interface{
|
||||
@ -85,24 +55,54 @@ HandlerVersion ()(V ,R int );
|
||||
|
||||
// MakeKey generates a object encryption key based on file encryption key and object numbers.
|
||||
// Used only for legacy filters - AESV3 doesn't change the key for each object.
|
||||
MakeKey (_efc ,_be uint32 ,_ae []byte )([]byte ,error );
|
||||
MakeKey (_ace ,_aea uint32 ,_cf []byte )([]byte ,error );
|
||||
|
||||
// EncryptBytes encrypts a buffer using object encryption key, as returned by MakeKey.
|
||||
// Implementation may reuse a buffer and encrypt data in-place.
|
||||
EncryptBytes (_aee []byte ,_ba []byte )([]byte ,error );
|
||||
EncryptBytes (_agac []byte ,_ff []byte )([]byte ,error );
|
||||
|
||||
// DecryptBytes decrypts a buffer using object encryption key, as returned by MakeKey.
|
||||
// Implementation may reuse a buffer and decrypt data in-place.
|
||||
DecryptBytes (_eaa []byte ,_bae []byte )([]byte ,error );};type filterV2 struct{_bb int };func (filterAES )DecryptBytes (buf []byte ,okey []byte )([]byte ,error ){_eb ,_ea :=_gd .NewCipher (okey );if _ea !=nil {return nil ,_ea ;};if len (buf )< 16{_fb .Log .Debug ("\u0045R\u0052\u004f\u0052\u0020\u0041\u0045\u0053\u0020\u0069\u006e\u0076a\u006c\u0069\u0064\u0020\u0062\u0075\u0066\u0020\u0025\u0073",buf );return buf ,_gc .Errorf ("\u0041\u0045\u0053\u003a B\u0075\u0066\u0020\u006c\u0065\u006e\u0020\u003c\u0020\u0031\u0036\u0020\u0028\u0025d\u0029",len (buf ));};_dac :=buf [:16];buf =buf [16:];if len (buf )%16!=0{_fb .Log .Debug ("\u0020\u0069\u0076\u0020\u0028\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (_dac ),_dac );_fb .Log .Debug ("\u0062\u0075\u0066\u0020\u0028\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );return buf ,_gc .Errorf ("\u0041\u0045\u0053\u0020\u0062\u0075\u0066\u0020\u006c\u0065\u006e\u0067\u0074\u0068\u0020\u006e\u006f\u0074\u0020\u006d\u0075\u006c\u0074\u0069p\u006c\u0065\u0020\u006f\u0066 \u0031\u0036 \u0028\u0025\u0064\u0029",len (buf ));};_fc :=_b .NewCBCDecrypter (_eb ,_dac );_fb .Log .Trace ("A\u0045\u0053\u0020\u0044ec\u0072y\u0070\u0074\u0020\u0028\u0025d\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );_fb .Log .Trace ("\u0063\u0068\u006f\u0070\u0020\u0041\u0045\u0053\u0020\u0044\u0065c\u0072\u0079\u0070\u0074\u0020\u0028\u0025\u0064\u0029\u003a \u0025\u0020\u0078",len (buf ),buf );_fc .CryptBlocks (buf ,buf );_fb .Log .Trace ("\u0074\u006f\u0020(\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );if len (buf )==0{_fb .Log .Trace ("\u0045\u006d\u0070\u0074\u0079\u0020b\u0075\u0066\u002c\u0020\u0072\u0065\u0074\u0075\u0072\u006e\u0069\u006e\u0067 \u0065\u006d\u0070\u0074\u0079\u0020\u0073t\u0072\u0069\u006e\u0067");return buf ,nil ;};_bd :=int (buf [len (buf )-1]);if _bd > len (buf ){_fb .Log .Debug ("\u0049\u006c\u006c\u0065g\u0061\u006c\u0020\u0070\u0061\u0064\u0020\u006c\u0065\u006eg\u0074h\u0020\u0028\u0025\u0064\u0020\u003e\u0020%\u0064\u0029",_bd ,len (buf ));return buf ,_gc .Errorf ("\u0069n\u0076a\u006c\u0069\u0064\u0020\u0070a\u0064\u0020l\u0065\u006e\u0067\u0074\u0068");};buf =buf [:len (buf )-_bd ];return buf ,nil ;};type filterIdentity struct{};func (filterIdentity )Name ()string {return "\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079"};var _ Filter =filterAESV2 {};func (filterIdentity )EncryptBytes (p []byte ,okey []byte )([]byte ,error ){return p ,nil };
|
||||
DecryptBytes (_ee []byte ,_bc []byte )([]byte ,error );};
|
||||
|
||||
// Name implements Filter interface.
|
||||
func (filterAESV3 )Name ()string {return "\u0041\u0045\u0053V\u0033"};var _ Filter =filterAESV3 {};
|
||||
// PDFVersion implements Filter interface.
|
||||
func (filterAESV3 )PDFVersion ()[2]int {return [2]int {2,0}};func init (){_acg ("\u0056\u0032",_ad )};func (filterIdentity )Name ()string {return "\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079"};
|
||||
|
||||
// NewFilterAESV3 creates an AES-based filter with a 256 bit key (AESV3).
|
||||
func NewFilterAESV3 ()Filter {_dce ,_feg :=_ag (FilterDict {});if _feg !=nil {_b .Log .Error ("E\u0052\u0052\u004f\u0052\u003a\u0020\u0063\u006f\u0075l\u0064\u0020\u006e\u006f\u0074\u0020\u0063re\u0061\u0074\u0065\u0020A\u0045\u0053\u0020\u0056\u0033\u0020\u0063\u0072\u0079pt\u0020\u0066i\u006c\u0074\u0065\u0072\u003a\u0020\u0025\u0076",_feg );return filterAESV3 {};};return _dce ;};var (_dd =make (map[string ]filterFunc ););type filterAESV2 struct{filterAES };
|
||||
|
||||
// FilterDict represents information from a CryptFilter dictionary.
|
||||
type FilterDict struct{CFM string ;AuthEvent _ef .AuthEvent ;Length int ;};type filterAES struct{};var _ Filter =filterAESV3 {};var _ Filter =filterAESV2 {};
|
||||
|
||||
// MakeKey implements Filter interface.
|
||||
func (filterAESV2 )MakeKey (objNum ,genNum uint32 ,ekey []byte )([]byte ,error ){return _ceg (objNum ,genNum ,ekey ,true );};func (filterIdentity )MakeKey (objNum ,genNum uint32 ,fkey []byte )([]byte ,error ){return fkey ,nil };
|
||||
func (filterAESV2 )MakeKey (objNum ,genNum uint32 ,ekey []byte )([]byte ,error ){return _ge (objNum ,genNum ,ekey ,true );};
|
||||
|
||||
// MakeKey implements Filter interface.
|
||||
func (_faa filterV2 )MakeKey (objNum ,genNum uint32 ,ekey []byte )([]byte ,error ){return _ceg (objNum ,genNum ,ekey ,false );};func _c (_bc FilterDict )(Filter ,error ){if _bc .Length ==128{_fb .Log .Debug ("\u0041\u0045S\u0056\u0032\u0020c\u0072\u0079\u0070\u0074\u0020f\u0069\u006c\u0074\u0065\u0072 l\u0065\u006e\u0067\u0074\u0068\u0020\u0061\u0070\u0070\u0065\u0061\u0072\u0073\u0020\u0074\u006f\u0020\u0062e\u0020i\u006e\u0020\u0062\u0069\u0074\u0073 ra\u0074\u0068\u0065\u0072\u0020\u0074\u0068\u0061\u006e\u0020\u0062\u0079te\u0073 \u002d\u0020\u0061\u0073s\u0075m\u0069n\u0067\u0020b\u0069\u0074s \u0028\u0025\u0064\u0029",_bc .Length );_bc .Length /=8;};if _bc .Length !=0&&_bc .Length !=16{return nil ,_gc .Errorf ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0041\u0045\u0053\u0056\u0032\u0020\u0063\u0072\u0079\u0070\u0074\u0020\u0066\u0069\u006c\u0074e\u0072\u0020\u006c\u0065\u006eg\u0074\u0068 \u0028\u0025\u0064\u0029",_bc .Length );};return filterAESV2 {},nil ;};var (_ada =make (map[string ]filterFunc ););
|
||||
func (_ed filterV2 )MakeKey (objNum ,genNum uint32 ,ekey []byte )([]byte ,error ){return _ge (objNum ,genNum ,ekey ,false );};func (filterIdentity )KeyLength ()int {return 0};func (filterIdentity )MakeKey (objNum ,genNum uint32 ,fkey []byte )([]byte ,error ){return fkey ,nil };
|
||||
|
||||
// NewFilterV2 creates a RC4-based filter with a specified key length (in bytes).
|
||||
func NewFilterV2 (length int )Filter {_dga ,_bba :=_ad (FilterDict {Length :length });if _bba !=nil {_b .Log .Error ("E\u0052\u0052\u004f\u0052\u003a\u0020\u0063\u006f\u0075l\u0064\u0020\u006e\u006f\u0074\u0020\u0063re\u0061\u0074\u0065\u0020R\u0043\u0034\u0020\u0056\u0032\u0020\u0063\u0072\u0079pt\u0020\u0066i\u006c\u0074\u0065\u0072\u003a\u0020\u0025\u0076",_bba );return filterV2 {_ggf :length };};return _dga ;};type filterIdentity struct{};
|
||||
|
||||
// EncryptBytes implements Filter interface.
|
||||
func (filterV2 )EncryptBytes (buf []byte ,okey []byte )([]byte ,error ){_ebd ,_df :=_c .NewCipher (okey );if _df !=nil {return nil ,_df ;};_b .Log .Trace ("\u0052\u00434\u0020\u0045\u006ec\u0072\u0079\u0070\u0074\u003a\u0020\u0025\u0020\u0078",buf );_ebd .XORKeyStream (buf ,buf );_b .Log .Trace ("\u0074o\u003a\u0020\u0025\u0020\u0078",buf );return buf ,nil ;};
|
||||
|
||||
// NewFilter creates CryptFilter from a corresponding dictionary.
|
||||
func NewFilter (d FilterDict )(Filter ,error ){_dea ,_bbg :=_acga (d .CFM );if _bbg !=nil {return nil ,_bbg ;};_daa ,_bbg :=_dea (d );if _bbg !=nil {return nil ,_bbg ;};return _daa ,nil ;};func _ad (_bbe FilterDict )(Filter ,error ){if _bbe .Length %8!=0{return nil ,_e .Errorf ("\u0063\u0072\u0079p\u0074\u0020\u0066\u0069\u006c\u0074\u0065\u0072\u0020\u006c\u0065\u006e\u0067\u0074\u0068\u0020\u006e\u006f\u0074\u0020\u006d\u0075\u006c\u0074\u0069\u0070\u006c\u0065\u0020o\u0066\u0020\u0038\u0020\u0028\u0025\u0064\u0029",_bbe .Length );};if _bbe .Length < 5||_bbe .Length > 16{if _bbe .Length ==40||_bbe .Length ==64||_bbe .Length ==128{_b .Log .Debug ("\u0053\u0054\u0041\u004e\u0044AR\u0044\u0020V\u0049\u004f\u004c\u0041\u0054\u0049\u004f\u004e\u003a\u0020\u0043\u0072\u0079\u0070\u0074\u0020\u004c\u0065\u006e\u0067\u0074\u0068\u0020\u0061\u0070\u0070\u0065\u0061\u0072s\u0020\u0074\u006f \u0062\u0065\u0020\u0069\u006e\u0020\u0062\u0069\u0074\u0073\u0020\u0072\u0061t\u0068\u0065\u0072\u0020\u0074h\u0061\u006e\u0020\u0062\u0079\u0074\u0065\u0073\u0020-\u0020\u0061s\u0073u\u006d\u0069\u006e\u0067\u0020\u0062\u0069t\u0073\u0020\u0028\u0025\u0064\u0029",_bbe .Length );_bbe .Length /=8;}else {return nil ,_e .Errorf ("\u0063\u0072\u0079\u0070\u0074\u0020\u0066\u0069\u006c\u0074\u0065\u0072\u0020\u006c\u0065\u006e\u0067\u0074h\u0020\u006e\u006f\u0074\u0020\u0069\u006e \u0072\u0061\u006e\u0067\u0065\u0020\u0034\u0030\u0020\u002d\u00201\u0032\u0038\u0020\u0062\u0069\u0074\u0020\u0028\u0025\u0064\u0029",_bbe .Length );};};return filterV2 {_ggf :_bbe .Length },nil ;};type filterAESV3 struct{filterAES };func (filterIdentity )HandlerVersion ()(V ,R int ){return ;};type filterV2 struct{_ggf int };
|
||||
|
||||
// MakeKey implements Filter interface.
|
||||
func (filterAESV3 )MakeKey (_ ,_ uint32 ,ekey []byte )([]byte ,error ){return ekey ,nil };func _acga (_gdb string )(filterFunc ,error ){_fg :=_dd [string (_gdb )];if _fg ==nil {return nil ,_e .Errorf ("\u0075\u006e\u0073\u0075p\u0070\u006f\u0072\u0074\u0065\u0064\u0020\u0063\u0072\u0079p\u0074 \u0066\u0069\u006c\u0074\u0065\u0072\u003a \u0025\u0071",_gdb );};return _fg ,nil ;};
|
||||
|
||||
// KeyLength implements Filter interface.
|
||||
func (filterAESV3 )KeyLength ()int {return 256/8};func (filterIdentity )DecryptBytes (p []byte ,okey []byte )([]byte ,error ){return p ,nil };func _ceg (_cef ,_dc uint32 ,_fgb []byte ,_fcc bool )([]byte ,error ){_ee :=make ([]byte ,len (_fgb )+5);for _bcf :=0;_bcf < len (_fgb );_bcf ++{_ee [_bcf ]=_fgb [_bcf ];};for _ef :=0;_ef < 3;_ef ++{_gg :=byte ((_cef >>uint32 (8*_ef ))&0xff);_ee [_ef +len (_fgb )]=_gg ;};for _bdf :=0;_bdf < 2;_bdf ++{_fcd :=byte ((_dc >>uint32 (8*_bdf ))&0xff);_ee [_bdf +len (_fgb )+3]=_fcd ;};if _fcc {_ee =append (_ee ,0x73);_ee =append (_ee ,0x41);_ee =append (_ee ,0x6C);_ee =append (_ee ,0x54);};_eab :=_f .New ();_eab .Write (_ee );_efe :=_eab .Sum (nil );if len (_fgb )+5< 16{return _efe [0:len (_fgb )+5],nil ;};return _efe ,nil ;};func (filterAES )EncryptBytes (buf []byte ,okey []byte )([]byte ,error ){_gce ,_bf :=_gd .NewCipher (okey );if _bf !=nil {return nil ,_bf ;};_fb .Log .Trace ("A\u0045\u0053\u0020\u0045nc\u0072y\u0070\u0074\u0020\u0028\u0025d\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );const _cf =_gd .BlockSize ;_fe :=_cf -len (buf )%_cf ;for _ff :=0;_ff < _fe ;_ff ++{buf =append (buf ,byte (_fe ));};_fb .Log .Trace ("\u0050a\u0064d\u0065\u0064\u0020\u0074\u006f \u0025\u0064 \u0062\u0079\u0074\u0065\u0073",len (buf ));_edd :=make ([]byte ,_cf +len (buf ));_ccd :=_edd [:_cf ];if _ ,_fef :=_a .ReadFull (_ac .Reader ,_ccd );_fef !=nil {return nil ,_fef ;};_da :=_b .NewCBCEncrypter (_gce ,_ccd );_da .CryptBlocks (_edd [_cf :],buf );buf =_edd ;_fb .Log .Trace ("\u0074\u006f\u0020(\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );return buf ,nil ;};func _ec (_cc FilterDict )(Filter ,error ){if _cc .Length ==256{_fb .Log .Debug ("\u0041\u0045S\u0056\u0033\u0020c\u0072\u0079\u0070\u0074\u0020f\u0069\u006c\u0074\u0065\u0072 l\u0065\u006e\u0067\u0074\u0068\u0020\u0061\u0070\u0070\u0065\u0061\u0072\u0073\u0020\u0074\u006f\u0020\u0062e\u0020i\u006e\u0020\u0062\u0069\u0074\u0073 ra\u0074\u0068\u0065\u0072\u0020\u0074\u0068\u0061\u006e\u0020\u0062\u0079te\u0073 \u002d\u0020\u0061\u0073s\u0075m\u0069n\u0067\u0020b\u0069\u0074s \u0028\u0025\u0064\u0029",_cc .Length );_cc .Length /=8;};if _cc .Length !=0&&_cc .Length !=32{return nil ,_gc .Errorf ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0041\u0045\u0053\u0056\u0033\u0020\u0063\u0072\u0079\u0070\u0074\u0020\u0066\u0069\u006c\u0074e\u0072\u0020\u006c\u0065\u006eg\u0074\u0068 \u0028\u0025\u0064\u0029",_cc .Length );};return filterAESV3 {},nil ;};func _dce (_ag string ,_cfa filterFunc ){if _ ,_dae :=_ada [_ag ];_dae {panic ("\u0061l\u0072e\u0061\u0064\u0079\u0020\u0072e\u0067\u0069s\u0074\u0065\u0072\u0065\u0064");};_ada [_ag ]=_cfa ;};
|
||||
func (filterAESV3 )KeyLength ()int {return 256/8};
|
||||
|
||||
// HandlerVersion implements Filter interface.
|
||||
func (filterAESV2 )HandlerVersion ()(V ,R int ){V ,R =4,4;return ;};
|
||||
|
||||
// NewFilterAESV2 creates an AES-based filter with a 128 bit key (AESV2).
|
||||
func NewFilterAESV2 ()Filter {_gg ,_bb :=_eca (FilterDict {});if _bb !=nil {_b .Log .Error ("E\u0052\u0052\u004f\u0052\u003a\u0020\u0063\u006f\u0075l\u0064\u0020\u006e\u006f\u0074\u0020\u0063re\u0061\u0074\u0065\u0020A\u0045\u0053\u0020\u0056\u0032\u0020\u0063\u0072\u0079pt\u0020\u0066i\u006c\u0074\u0065\u0072\u003a\u0020\u0025\u0076",_bb );return filterAESV2 {};};return _gg ;};
|
||||
|
||||
// NewIdentity creates an identity filter that bypasses all data without changes.
|
||||
func NewIdentity ()Filter {return filterIdentity {}};var _ Filter =filterV2 {};
|
||||
|
||||
// DecryptBytes implements Filter interface.
|
||||
func (filterV2 )DecryptBytes (buf []byte ,okey []byte )([]byte ,error ){_gde ,_ega :=_c .NewCipher (okey );if _ega !=nil {return nil ,_ega ;};_b .Log .Trace ("\u0052\u00434\u0020\u0044\u0065c\u0072\u0079\u0070\u0074\u003a\u0020\u0025\u0020\u0078",buf );_gde .XORKeyStream (buf ,buf );_b .Log .Trace ("\u0074o\u003a\u0020\u0025\u0020\u0078",buf );return buf ,nil ;};
|
File diff suppressed because one or more lines are too long
2482
creator/creator.go
2482
creator/creator.go
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
26
fdf/fdf.go
26
fdf/fdf.go
File diff suppressed because one or more lines are too long
@ -10,28 +10,28 @@
|
||||
// terms that can be accessed at https://unidoc.io/eula/
|
||||
|
||||
// Package fjson provides support for loading PDF form field data from JSON data/files.
|
||||
package fjson ;import (_b "encoding/json";_c "github.com/unidoc/unipdf/v3/core";_ef "github.com/unidoc/unipdf/v3/model";_e "io";_bc "os";);type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;
|
||||
|
||||
// Options lists allowed values if present.
|
||||
Options []string `json:"options,omitempty"`;};
|
||||
|
||||
// LoadFromJSON loads JSON form data from `r`.
|
||||
func LoadFromJSON (r _e .Reader )(*FieldData ,error ){var _dc FieldData ;_be :=_b .NewDecoder (r ).Decode (&_dc ._a );if _be !=nil {return nil ,_be ;};return &_dc ,nil ;};
|
||||
|
||||
// JSON returns the field data as a string in JSON format.
|
||||
func (_fed FieldData )JSON ()(string ,error ){_cb ,_ece :=_b .MarshalIndent (_fed ._a ,"","\u0020\u0020\u0020\u0020");return string (_cb ),_ece ;};
|
||||
|
||||
// LoadFromJSONFile loads form field data from a JSON file.
|
||||
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_f ,_g :=_bc .Open (filePath );if _g !=nil {return nil ,_g ;};defer _f .Close ();return LoadFromJSON (_f );};
|
||||
|
||||
// LoadFromPDFFile loads form field data from a PDF file.
|
||||
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_cgg ,_aag :=_bc .Open (filePath );if _aag !=nil {return nil ,_aag ;};defer _cgg .Close ();return LoadFromPDF (_cgg );};
|
||||
|
||||
// FieldValues implements model.FieldValueProvider interface.
|
||||
func (_de *FieldData )FieldValues ()(map[string ]_c .PdfObject ,error ){_fec :=make (map[string ]_c .PdfObject );for _ ,_gea :=range _de ._a {if len (_gea .Value )> 0{_fec [_gea .Name ]=_c .MakeString (_gea .Value );};};return _fec ,nil ;};
|
||||
package fjson ;import (_e "encoding/json";_db "github.com/unidoc/unipdf/v3/core";_bb "github.com/unidoc/unipdf/v3/model";_bg "io";_b "os";);
|
||||
|
||||
// FieldData represents form field data loaded from JSON file.
|
||||
type FieldData struct{_a []fieldValue };
|
||||
type FieldData struct{_g []fieldValue };
|
||||
|
||||
// JSON returns the field data as a string in JSON format.
|
||||
func (_dgg FieldData )JSON ()(string ,error ){_fea ,_cgeb :=_e .MarshalIndent (_dgg ._g ,"","\u0020\u0020\u0020\u0020");return string (_fea ),_cgeb ;};
|
||||
|
||||
// LoadFromPDF loads form field data from a PDF.
|
||||
func LoadFromPDF (rs _e .ReadSeeker )(*FieldData ,error ){_ge ,_geg :=_ef .NewPdfReader (rs );if _geg !=nil {return nil ,_geg ;};if _ge .AcroForm ==nil {return nil ,nil ;};var _cg []fieldValue ;_ec :=_ge .AcroForm .AllFields ();for _ ,_fc :=range _ec {var _bg []string ;_gg :=make (map[string ]struct{});_aa ,_fcg :=_fc .FullName ();if _fcg !=nil {return nil ,_fcg ;};if _aaa ,_bce :=_fc .V .(*_c .PdfObjectString );_bce {_cg =append (_cg ,fieldValue {Name :_aa ,Value :_aaa .Decoded ()});continue ;};var _ce string ;for _ ,_ca :=range _fc .Annotations {_df ,_cdf :=_c .GetName (_ca .AS );if _cdf {_ce =_df .String ();};_ea ,_dcg :=_c .GetDict (_ca .AP );if !_dcg {continue ;};_beg ,_ :=_c .GetDict (_ea .Get ("\u004e"));for _ ,_baa :=range _beg .Keys (){_dg :=_baa .String ();if _ ,_bga :=_gg [_dg ];!_bga {_bg =append (_bg ,_dg );_gg [_dg ]=struct{}{};};};_eg ,_ :=_c .GetDict (_ea .Get ("\u0044"));for _ ,_ae :=range _eg .Keys (){_bgf :=_ae .String ();if _ ,_fcd :=_gg [_bgf ];!_fcd {_bg =append (_bg ,_bgf );_gg [_bgf ]=struct{}{};};};};_fg :=fieldValue {Name :_aa ,Value :_ce ,Options :_bg };_cg =append (_cg ,_fg );};_gd :=FieldData {_a :_cg };return &_gd ,nil ;};
|
||||
func LoadFromPDF (rs _bg .ReadSeeker )(*FieldData ,error ){_ec ,_ga :=_bb .NewPdfReader (rs );if _ga !=nil {return nil ,_ga ;};if _ec .AcroForm ==nil {return nil ,nil ;};var _ggf []fieldValue ;_bga :=_ec .AcroForm .AllFields ();for _ ,_be :=range _bga {var _ca []string ;_eb :=make (map[string ]struct{});_gd ,_f :=_be .FullName ();if _f !=nil {return nil ,_f ;};if _eg ,_ce :=_be .V .(*_db .PdfObjectString );_ce {_ggf =append (_ggf ,fieldValue {Name :_gd ,Value :_eg .Decoded ()});continue ;};var _ea string ;for _ ,_daf :=range _be .Annotations {_gga ,_ee :=_db .GetName (_daf .AS );if _ee {_ea =_gga .String ();};_fe ,_gaa :=_db .GetDict (_daf .AP );if !_gaa {continue ;};_ef ,_ :=_db .GetDict (_fe .Get ("\u004e"));for _ ,_af :=range _ef .Keys (){_baa :=_af .String ();if _ ,_cg :=_eb [_baa ];!_cg {_ca =append (_ca ,_baa );_eb [_baa ]=struct{}{};};};_ceb ,_ :=_db .GetDict (_fe .Get ("\u0044"));for _ ,_eae :=range _ceb .Keys (){_bae :=_eae .String ();if _ ,_dg :=_eb [_bae ];!_dg {_ca =append (_ca ,_bae );_eb [_bae ]=struct{}{};};};};_cge :=fieldValue {Name :_gd ,Value :_ea ,Options :_ca };_ggf =append (_ggf ,_cge );};_dd :=FieldData {_g :_ggf };return &_dd ,nil ;};
|
||||
|
||||
// LoadFromJSON loads JSON form data from `r`.
|
||||
func LoadFromJSON (r _bg .Reader )(*FieldData ,error ){var _ba FieldData ;_gg :=_e .NewDecoder (r ).Decode (&_ba ._g );if _gg !=nil {return nil ,_gg ;};return &_ba ,nil ;};
|
||||
|
||||
// LoadFromPDFFile loads form field data from a PDF file.
|
||||
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_fdc ,_afc :=_b .Open (filePath );if _afc !=nil {return nil ,_afc ;};defer _fdc .Close ();return LoadFromPDF (_fdc );};
|
||||
|
||||
// FieldValues implements model.FieldValueProvider interface.
|
||||
func (_de *FieldData )FieldValues ()(map[string ]_db .PdfObject ,error ){_fb :=make (map[string ]_db .PdfObject );for _ ,_cc :=range _de ._g {if len (_cc .Value )> 0{_fb [_cc .Name ]=_db .MakeString (_cc .Value );};};return _fb ,nil ;};
|
||||
|
||||
// LoadFromJSONFile loads form field data from a JSON file.
|
||||
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_c ,_ge :=_b .Open (filePath );if _ge !=nil {return nil ,_ge ;};defer _c .Close ();return LoadFromJSON (_c );};type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;
|
||||
|
||||
// Options lists allowed values if present.
|
||||
Options []string `json:"options,omitempty"`;};
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -9,4 +9,4 @@
|
||||
// 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 endian ;import (_a "encoding/binary";_g "unsafe";);var (ByteOrder _a .ByteOrder ;_fd bool ;);func init (){const _gc =int (_g .Sizeof (0));_fa :=1;_gd :=(*[_gc ]byte )(_g .Pointer (&_fa ));if _gd [0]==0{_fd =true ;ByteOrder =_a .BigEndian ;}else {ByteOrder =_a .LittleEndian ;};};func IsLittle ()bool {return !_fd };func IsBig ()bool {return _fd };
|
||||
package endian ;import (_a "encoding/binary";_g "unsafe";);func IsLittle ()bool {return !_gc };func init (){const _e =int (_g .Sizeof (0));_c :=1;_d :=(*[_e ]byte )(_g .Pointer (&_c ));if _d [0]==0{_gc =true ;ByteOrder =_a .BigEndian ;}else {ByteOrder =_a .LittleEndian ;};};var (ByteOrder _a .ByteOrder ;_gc bool ;);func IsBig ()bool {return _gc };
|
File diff suppressed because one or more lines are too long
@ -9,4 +9,4 @@
|
||||
// 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 basic ;import _a "github.com/unidoc/unipdf/v3/internal/jbig2/errors";func (_ca *NumSlice )AddInt (v int ){*_ca =append (*_ca ,float32 (v ))};func (_fa NumSlice )GetInt (i int )(int ,error ){const _gec ="\u0047\u0065\u0074\u0049\u006e\u0074";if i < 0||i > len (_fa )-1{return 0,_a .Errorf (_gec ,"\u0069n\u0064\u0065\u0078\u003a\u0020\u0027\u0025\u0064\u0027\u0020\u006fu\u0074\u0020\u006f\u0066\u0020\u0072\u0061\u006e\u0067\u0065",i );};_ag :=_fa [i ];return int (_ag +Sign (_ag )*0.5),nil ;};func Ceil (numerator ,denominator int )int {if numerator %denominator ==0{return numerator /denominator ;};return (numerator /denominator )+1;};func Max (x ,y int )int {if x > y {return x ;};return y ;};func (_e *IntSlice )Add (v int )error {if _e ==nil {return _a .Error ("\u0049\u006e\u0074S\u006c\u0069\u0063\u0065\u002e\u0041\u0064\u0064","\u0073\u006c\u0069\u0063\u0065\u0020\u006e\u006f\u0074\u0020\u0064\u0065f\u0069\u006e\u0065\u0064");};*_e =append (*_e ,v );return nil ;};func (_cbg *Stack )Push (v interface{}){_cbg .Data =append (_cbg .Data ,v )};func (_gf *NumSlice )Add (v float32 ){*_gf =append (*_gf ,v )};func (_de *Stack )Len ()int {return len (_de .Data )};func (_afb *Stack )Peek ()(_aa interface{},_cge bool ){return _afb .peek ()};func (_gg IntsMap )Get (key uint64 )(int ,bool ){_b ,_c :=_gg [key ];if !_c {return 0,false ;};if len (_b )==0{return 0,false ;};return _b [0],true ;};type IntsMap map[uint64 ][]int ;type IntSlice []int ;func (_cd NumSlice )Get (i int )(float32 ,error ){if i < 0||i > len (_cd )-1{return 0,_a .Errorf ("\u004e\u0075\u006dS\u006c\u0069\u0063\u0065\u002e\u0047\u0065\u0074","\u0069n\u0064\u0065\u0078\u003a\u0020\u0027\u0025\u0064\u0027\u0020\u006fu\u0074\u0020\u006f\u0066\u0020\u0072\u0061\u006e\u0067\u0065",i );};return _cd [i ],nil ;};type NumSlice []float32 ;func (_d IntsMap )Add (key uint64 ,value int ){_d [key ]=append (_d [key ],value )};func NewIntSlice (i int )*IntSlice {_dd :=IntSlice (make ([]int ,i ));return &_dd };func (_cg IntsMap )GetSlice (key uint64 )([]int ,bool ){_ge ,_f :=_cg [key ];if !_f {return nil ,false ;};return _ge ,true ;};func Sign (v float32 )float32 {if v >=0.0{return 1.0;};return -1.0;};func (_ac *Stack )top ()int {return len (_ac .Data )-1};func NewNumSlice (i int )*NumSlice {_ed :=NumSlice (make ([]float32 ,i ));return &_ed };func Min (x ,y int )int {if x < y {return x ;};return y ;};func (_ef *Stack )peek ()(interface{},bool ){_gfa :=_ef .top ();if _gfa ==-1{return nil ,false ;};return _ef .Data [_gfa ],true ;};func (_af NumSlice )GetIntSlice ()[]int {_fgg :=make ([]int ,len (_af ));for _gd ,_gc :=range _af {_fgg [_gd ]=int (_gc );};return _fgg ;};func (_ab IntsMap )Delete (key uint64 ){delete (_ab ,key )};func (_fb *IntSlice )Copy ()*IntSlice {_fg :=IntSlice (make ([]int ,len (*_fb )));copy (_fg ,*_fb );return &_fg ;};func (_bc IntSlice )Size ()int {return len (_bc )};func Abs (v int )int {if v > 0{return v ;};return -v ;};type Stack struct{Data []interface{};Aux *Stack ;};func (_ee *Stack )Pop ()(_ad interface{},_gfd bool ){_ad ,_gfd =_ee .peek ();if !_gfd {return nil ,_gfd ;};_ee .Data =_ee .Data [:_ee .top ()];return _ad ,true ;};func (_cb IntSlice )Get (index int )(int ,error ){if index > len (_cb )-1{return 0,_a .Errorf ("\u0049\u006e\u0074S\u006c\u0069\u0063\u0065\u002e\u0047\u0065\u0074","\u0069\u006e\u0064\u0065x:\u0020\u0025\u0064\u0020\u006f\u0075\u0074\u0020\u006f\u0066\u0020\u0072\u0061\u006eg\u0065",index );};return _cb [index ],nil ;};
|
||||
package basic ;import _f "github.com/unidoc/unipdf/v3/internal/jbig2/errors";func Sign (v float32 )float32 {if v >=0.0{return 1.0;};return -1.0;};type NumSlice []float32 ;func Ceil (numerator ,denominator int )int {if numerator %denominator ==0{return numerator /denominator ;};return (numerator /denominator )+1;};func (_a IntsMap )Add (key uint64 ,value int ){_a [key ]=append (_a [key ],value )};func (_ag IntSlice )Size ()int {return len (_ag )};func (_cf *IntSlice )Add (v int )error {if _cf ==nil {return _f .Error ("\u0049\u006e\u0074S\u006c\u0069\u0063\u0065\u002e\u0041\u0064\u0064","\u0073\u006c\u0069\u0063\u0065\u0020\u006e\u006f\u0074\u0020\u0064\u0065f\u0069\u006e\u0065\u0064");};*_cf =append (*_cf ,v );return nil ;};func (_ba *Stack )peek ()(interface{},bool ){_gab :=_ba .top ();if _gab ==-1{return nil ,false ;};return _ba .Data [_gab ],true ;};func (_gf *NumSlice )Add (v float32 ){*_gf =append (*_gf ,v )};func (_eb NumSlice )GetInt (i int )(int ,error ){const _gg ="\u0047\u0065\u0074\u0049\u006e\u0074";if i < 0||i > len (_eb )-1{return 0,_f .Errorf (_gg ,"\u0069n\u0064\u0065\u0078\u003a\u0020\u0027\u0025\u0064\u0027\u0020\u006fu\u0074\u0020\u006f\u0066\u0020\u0072\u0061\u006e\u0067\u0065",i );};_db :=_eb [i ];return int (_db +Sign (_db )*0.5),nil ;};func Min (x ,y int )int {if x < y {return x ;};return y ;};type Stack struct{Data []interface{};Aux *Stack ;};type IntsMap map[uint64 ][]int ;func (_dd IntSlice )Get (index int )(int ,error ){if index > len (_dd )-1{return 0,_f .Errorf ("\u0049\u006e\u0074S\u006c\u0069\u0063\u0065\u002e\u0047\u0065\u0074","\u0069\u006e\u0064\u0065x:\u0020\u0025\u0064\u0020\u006f\u0075\u0074\u0020\u006f\u0066\u0020\u0072\u0061\u006eg\u0065",index );};return _dd [index ],nil ;};func NewIntSlice (i int )*IntSlice {_c :=IntSlice (make ([]int ,i ));return &_c };func (_fd *Stack )top ()int {return len (_fd .Data )-1};func (_bb NumSlice )Get (i int )(float32 ,error ){if i < 0||i > len (_bb )-1{return 0,_f .Errorf ("\u004e\u0075\u006dS\u006c\u0069\u0063\u0065\u002e\u0047\u0065\u0074","\u0069n\u0064\u0065\u0078\u003a\u0020\u0027\u0025\u0064\u0027\u0020\u006fu\u0074\u0020\u006f\u0066\u0020\u0072\u0061\u006e\u0067\u0065",i );};return _bb [i ],nil ;};func (_be *Stack )Pop ()(_gcf interface{},_bbb bool ){_gcf ,_bbb =_be .peek ();if !_bbb {return nil ,_bbb ;};_be .Data =_be .Data [:_be .top ()];return _gcf ,true ;};func (_gc *Stack )Len ()int {return len (_gc .Data )};func (_afg *Stack )Push (v interface{}){_afg .Data =append (_afg .Data ,v )};func (_af *IntSlice )Copy ()*IntSlice {_dc :=IntSlice (make ([]int ,len (*_af )));copy (_dc ,*_af );return &_dc ;};func (_ecc *Stack )Peek ()(_ed interface{},_gb bool ){return _ecc .peek ()};func NewNumSlice (i int )*NumSlice {_ga :=NumSlice (make ([]float32 ,i ));return &_ga };type IntSlice []int ;func (_ff IntsMap )Delete (key uint64 ){delete (_ff ,key )};func (_ec NumSlice )GetIntSlice ()[]int {_cd :=make ([]int ,len (_ec ));for _de ,_bbg :=range _ec {_cd [_de ]=int (_bbg );};return _cd ;};func Abs (v int )int {if v > 0{return v ;};return -v ;};func (_ad IntsMap )GetSlice (key uint64 )([]int ,bool ){_d ,_aa :=_ad [key ];if !_aa {return nil ,false ;};return _d ,true ;};func (_fg IntsMap )Get (key uint64 )(int ,bool ){_e ,_b :=_fg [key ];if !_b {return 0,false ;};if len (_e )==0{return 0,false ;};return _e [0],true ;};func Max (x ,y int )int {if x > y {return x ;};return y ;};func (_ee *NumSlice )AddInt (v int ){*_ee =append (*_ee ,float32 (v ))};
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -9,4 +9,4 @@
|
||||
// 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 decoder ;import (_e "github.com/unidoc/unipdf/v3/internal/bitwise";_ef "github.com/unidoc/unipdf/v3/internal/jbig2/bitmap";_ec "github.com/unidoc/unipdf/v3/internal/jbig2/document";_be "github.com/unidoc/unipdf/v3/internal/jbig2/errors";_a "image";);func (_ab *Decoder )DecodeNextPage ()([]byte ,error ){_ab ._g ++;_cc :=_ab ._g ;return _ab .decodePage (_cc );};func (_d *Decoder )decodePage (_gf int )([]byte ,error ){const _dd ="\u0064\u0065\u0063\u006f\u0064\u0065\u0050\u0061\u0067\u0065";if _gf < 0{return nil ,_be .Errorf (_dd ,"\u0069n\u0076\u0061\u006c\u0069d\u0020\u0070\u0061\u0067\u0065 \u006eu\u006db\u0065\u0072\u003a\u0020\u0027\u0025\u0064'",_gf );};if _gf > int (_d ._bf .NumberOfPages ){return nil ,_be .Errorf (_dd ,"p\u0061\u0067\u0065\u003a\u0020\u0027%\u0064\u0027\u0020\u006e\u006f\u0074 \u0066\u006f\u0075\u006e\u0064\u0020\u0069n\u0020\u0074\u0068\u0065\u0020\u0064\u0065\u0063\u006f\u0064e\u0072",_gf );};_ae ,_ea :=_d ._bf .GetPage (_gf );if _ea !=nil {return nil ,_be .Wrap (_ea ,_dd ,"");};_dc ,_ea :=_ae .GetBitmap ();if _ea !=nil {return nil ,_be .Wrap (_ea ,_dd ,"");};_dc .InverseData ();if !_d ._c .UnpaddedData {return _dc .Data ,nil ;};return _dc .GetUnpaddedData ();};func (_cf *Decoder )PageNumber ()(int ,error ){const _ag ="\u0044e\u0063o\u0064\u0065\u0072\u002e\u0050a\u0067\u0065N\u0075\u006d\u0062\u0065\u0072";if _cf ._bf ==nil {return 0,_be .Error (_ag ,"d\u0065\u0063\u006f\u0064\u0065\u0072 \u006e\u006f\u0074\u0020\u0069\u006e\u0069\u0074\u0069a\u006c\u0069\u007ae\u0064 \u0079\u0065\u0074");};return int (_cf ._bf .NumberOfPages ),nil ;};func Decode (input []byte ,parameters Parameters ,globals *_ec .Globals )(*Decoder ,error ){_abb :=_e .NewReader (input );_aeg ,_eff :=_ec .DecodeDocument (_abb ,globals );if _eff !=nil {return nil ,_eff ;};return &Decoder {_ed :_abb ,_bf :_aeg ,_c :parameters },nil ;};func (_efb *Decoder )DecodePage (pageNumber int )([]byte ,error ){return _efb .decodePage (pageNumber )};func (_af *Decoder )decodePageImage (_dcf int )(_a .Image ,error ){const _agb ="\u0064e\u0063o\u0064\u0065\u0050\u0061\u0067\u0065\u0049\u006d\u0061\u0067\u0065";if _dcf < 0{return nil ,_be .Errorf (_agb ,"\u0069n\u0076\u0061\u006c\u0069d\u0020\u0070\u0061\u0067\u0065 \u006eu\u006db\u0065\u0072\u003a\u0020\u0027\u0025\u0064'",_dcf );};if _dcf > int (_af ._bf .NumberOfPages ){return nil ,_be .Errorf (_agb ,"p\u0061\u0067\u0065\u003a\u0020\u0027%\u0064\u0027\u0020\u006e\u006f\u0074 \u0066\u006f\u0075\u006e\u0064\u0020\u0069n\u0020\u0074\u0068\u0065\u0020\u0064\u0065\u0063\u006f\u0064e\u0072",_dcf );};_fac ,_bb :=_af ._bf .GetPage (_dcf );if _bb !=nil {return nil ,_be .Wrap (_bb ,_agb ,"");};_abd ,_bb :=_fac .GetBitmap ();if _bb !=nil {return nil ,_be .Wrap (_bb ,_agb ,"");};return _abd .ToImage (),nil ;};type Decoder struct{_ed _e .StreamReader ;_bf *_ec .Document ;_g int ;_c Parameters ;};func (_f *Decoder )DecodePageImage (pageNumber int )(_a .Image ,error ){const _bc ="\u0064\u0065\u0063od\u0065\u0072\u002e\u0044\u0065\u0063\u006f\u0064\u0065\u0050\u0061\u0067\u0065\u0049\u006d\u0061\u0067\u0065";_fa ,_bg :=_f .decodePageImage (pageNumber );if _bg !=nil {return nil ,_be .Wrap (_bg ,_bc ,"");};return _fa ,nil ;};type Parameters struct{UnpaddedData bool ;Color _ef .Color ;};
|
||||
package decoder ;import (_b "github.com/unidoc/unipdf/v3/internal/bitwise";_f "github.com/unidoc/unipdf/v3/internal/jbig2/bitmap";_e "github.com/unidoc/unipdf/v3/internal/jbig2/document";_af "github.com/unidoc/unipdf/v3/internal/jbig2/errors";_a "image";);func (_d *Decoder )DecodePageImage (pageNumber int )(_a .Image ,error ){const _cb ="\u0064\u0065\u0063od\u0065\u0072\u002e\u0044\u0065\u0063\u006f\u0064\u0065\u0050\u0061\u0067\u0065\u0049\u006d\u0061\u0067\u0065";_eg ,_fd :=_d .decodePageImage (pageNumber );if _fd !=nil {return nil ,_af .Wrap (_fd ,_cb ,"");};return _eg ,nil ;};func (_bc *Decoder )decodePageImage (_cdb int )(_a .Image ,error ){const _ec ="\u0064e\u0063o\u0064\u0065\u0050\u0061\u0067\u0065\u0049\u006d\u0061\u0067\u0065";if _cdb < 0{return nil ,_af .Errorf (_ec ,"\u0069n\u0076\u0061\u006c\u0069d\u0020\u0070\u0061\u0067\u0065 \u006eu\u006db\u0065\u0072\u003a\u0020\u0027\u0025\u0064'",_cdb );};if _cdb > int (_bc ._bg .NumberOfPages ){return nil ,_af .Errorf (_ec ,"p\u0061\u0067\u0065\u003a\u0020\u0027%\u0064\u0027\u0020\u006e\u006f\u0074 \u0066\u006f\u0075\u006e\u0064\u0020\u0069n\u0020\u0074\u0068\u0065\u0020\u0064\u0065\u0063\u006f\u0064e\u0072",_cdb );};_agd ,_be :=_bc ._bg .GetPage (_cdb );if _be !=nil {return nil ,_af .Wrap (_be ,_ec ,"");};_fab ,_be :=_agd .GetBitmap ();if _be !=nil {return nil ,_af .Wrap (_be ,_ec ,"");};return _fab .ToImage (),nil ;};type Decoder struct{_c _b .StreamReader ;_bg *_e .Document ;_ag int ;_fe Parameters ;};type Parameters struct{UnpaddedData bool ;Color _f .Color ;};func (_cd *Decoder )PageNumber ()(int ,error ){const _ba ="\u0044e\u0063o\u0064\u0065\u0072\u002e\u0050a\u0067\u0065N\u0075\u006d\u0062\u0065\u0072";if _cd ._bg ==nil {return 0,_af .Error (_ba ,"d\u0065\u0063\u006f\u0064\u0065\u0072 \u006e\u006f\u0074\u0020\u0069\u006e\u0069\u0074\u0069a\u006c\u0069\u007ae\u0064 \u0079\u0065\u0074");};return int (_cd ._bg .NumberOfPages ),nil ;};func (_ce *Decoder )decodePage (_fa int )([]byte ,error ){const _cc ="\u0064\u0065\u0063\u006f\u0064\u0065\u0050\u0061\u0067\u0065";if _fa < 0{return nil ,_af .Errorf (_cc ,"\u0069n\u0076\u0061\u006c\u0069d\u0020\u0070\u0061\u0067\u0065 \u006eu\u006db\u0065\u0072\u003a\u0020\u0027\u0025\u0064'",_fa );};if _fa > int (_ce ._bg .NumberOfPages ){return nil ,_af .Errorf (_cc ,"p\u0061\u0067\u0065\u003a\u0020\u0027%\u0064\u0027\u0020\u006e\u006f\u0074 \u0066\u006f\u0075\u006e\u0064\u0020\u0069n\u0020\u0074\u0068\u0065\u0020\u0064\u0065\u0063\u006f\u0064e\u0072",_fa );};_gb ,_cbe :=_ce ._bg .GetPage (_fa );if _cbe !=nil {return nil ,_af .Wrap (_cbe ,_cc ,"");};_ff ,_cbe :=_gb .GetBitmap ();if _cbe !=nil {return nil ,_af .Wrap (_cbe ,_cc ,"");};_ff .InverseData ();if !_ce ._fe .UnpaddedData {return _ff .Data ,nil ;};return _ff .GetUnpaddedData ();};func (_egf *Decoder )DecodeNextPage ()([]byte ,error ){_egf ._ag ++;_fb :=_egf ._ag ;return _egf .decodePage (_fb );};func Decode (input []byte ,parameters Parameters ,globals *_e .Globals )(*Decoder ,error ){_afg :=_b .NewReader (input );_bf ,_feb :=_e .DecodeDocument (_afg ,globals );if _feb !=nil {return nil ,_feb ;};return &Decoder {_c :_afg ,_bg :_bf ,_fe :parameters },nil ;};func (_eb *Decoder )DecodePage (pageNumber int )([]byte ,error ){return _eb .decodePage (pageNumber )};
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -9,4 +9,4 @@
|
||||
// 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 errors ;import _d "fmt";func _gf (_b ,_ec string )*processError {return &processError {_ee :"\u005b\u0055\u006e\u0069\u0050\u0044\u0046\u005d",_a :_b ,_dd :_ec };};func Wrap (err error ,processName ,message string )error {if _ecb ,_ed :=err .(*processError );_ed {_ecb ._ee ="";};_ef :=_gf (message ,processName );_ef ._ab =err ;return _ef ;};func Wrapf (err error ,processName ,message string ,arguments ...interface{})error {if _eca ,_efg :=err .(*processError );_efg {_eca ._ee ="";};_de :=_gf (_d .Sprintf (message ,arguments ...),processName );_de ._ab =err ;return _de ;};func (_g *processError )Error ()string {var _f string ;if _g ._ee !=""{_f =_g ._ee ;};_f +="\u0050r\u006f\u0063\u0065\u0073\u0073\u003a "+_g ._dd ;if _g ._a !=""{_f +="\u0020\u004d\u0065\u0073\u0073\u0061\u0067\u0065\u003a\u0020"+_g ._a ;};if _g ._ab !=nil {_f +="\u002e\u0020"+_g ._ab .Error ();};return _f ;};func Error (processName ,message string )error {return _gf (message ,processName )};func Errorf (processName ,message string ,arguments ...interface{})error {return _gf (_d .Sprintf (message ,arguments ...),processName );};type processError struct{_ee string ;_dd string ;_a string ;_ab error ;};
|
||||
package errors ;import _c "fmt";func Errorf (processName ,message string ,arguments ...interface{})error {return _a (_c .Sprintf (message ,arguments ...),processName );};func Wrapf (err error ,processName ,message string ,arguments ...interface{})error {if _ea ,_eag :=err .(*processError );_eag {_ea ._b ="";};_fg :=_a (_c .Sprintf (message ,arguments ...),processName );_fg ._f =err ;return _fg ;};type processError struct{_b string ;_d string ;_cb string ;_f error ;};func Wrap (err error ,processName ,message string )error {if _ce ,_ba :=err .(*processError );_ba {_ce ._b ="";};_be :=_a (message ,processName );_be ._f =err ;return _be ;};func Error (processName ,message string )error {return _a (message ,processName )};func _a (_bg ,_eg string )*processError {return &processError {_b :"\u005b\u0055\u006e\u0069\u0050\u0044\u0046\u005d",_cb :_bg ,_d :_eg };};func (_fd *processError )Error ()string {var _bd string ;if _fd ._b !=""{_bd =_fd ._b ;};_bd +="\u0050r\u006f\u0063\u0065\u0073\u0073\u003a "+_fd ._d ;if _fd ._cb !=""{_bd +="\u0020\u004d\u0065\u0073\u0073\u0061\u0067\u0065\u003a\u0020"+_fd ._cb ;};if _fd ._f !=nil {_bd +="\u002e\u0020"+_fd ._f .Error ();};return _bd ;};
|
@ -9,4 +9,4 @@
|
||||
// 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 jbig2 ;import (_g "github.com/unidoc/unipdf/v3/internal/bitwise";_a "github.com/unidoc/unipdf/v3/internal/jbig2/decoder";_cc "github.com/unidoc/unipdf/v3/internal/jbig2/document";_f "github.com/unidoc/unipdf/v3/internal/jbig2/document/segments";_gc "github.com/unidoc/unipdf/v3/internal/jbig2/errors";_e "sort";);func DecodeBytes (encoded []byte ,parameters _a .Parameters ,globals ...Globals )([]byte ,error ){var _ge Globals ;if len (globals )> 0{_ge =globals [0];};_ad ,_d :=_a .Decode (encoded ,parameters ,_ge .ToDocumentGlobals ());if _d !=nil {return nil ,_d ;};return _ad .DecodeNextPage ();};func (_fc Globals )ToDocumentGlobals ()*_cc .Globals {if _fc ==nil {return nil ;};_ff :=[]*_f .Header {};for _ ,_ee :=range _fc {_ff =append (_ff ,_ee );};_e .Slice (_ff ,func (_cf ,_b int )bool {return _ff [_cf ].SegmentNumber < _ff [_b ].SegmentNumber });return &_cc .Globals {Segments :_ff };};func DecodeGlobals (encoded []byte )(Globals ,error ){const _dg ="\u0044\u0065\u0063\u006f\u0064\u0065\u0047\u006c\u006f\u0062\u0061\u006c\u0073";_da :=_g .NewReader (encoded );_df ,_fb :=_cc .DecodeDocument (_da ,nil );if _fb !=nil {return nil ,_gc .Wrap (_fb ,_dg ,"");};if _df .GlobalSegments ==nil ||(_df .GlobalSegments .Segments ==nil ){return nil ,_gc .Error (_dg ,"\u006eo\u0020\u0067\u006c\u006f\u0062\u0061\u006c\u0020\u0073\u0065\u0067m\u0065\u006e\u0074\u0073\u0020\u0066\u006f\u0075\u006e\u0064");};_fg :=Globals {};for _ ,_fbf :=range _df .GlobalSegments .Segments {_fg [int (_fbf .SegmentNumber )]=_fbf ;};return _fg ,nil ;};type Globals map[int ]*_f .Header ;
|
||||
package jbig2 ;import (_d "github.com/unidoc/unipdf/v3/internal/bitwise";_g "github.com/unidoc/unipdf/v3/internal/jbig2/decoder";_c "github.com/unidoc/unipdf/v3/internal/jbig2/document";_f "github.com/unidoc/unipdf/v3/internal/jbig2/document/segments";_ec "github.com/unidoc/unipdf/v3/internal/jbig2/errors";_e "sort";);type Globals map[int ]*_f .Header ;func DecodeGlobals (encoded []byte )(Globals ,error ){const _ee ="\u0044\u0065\u0063\u006f\u0064\u0065\u0047\u006c\u006f\u0062\u0061\u006c\u0073";_cc :=_d .NewReader (encoded );_ga ,_eed :=_c .DecodeDocument (_cc ,nil );if _eed !=nil {return nil ,_ec .Wrap (_eed ,_ee ,"");};if _ga .GlobalSegments ==nil ||(_ga .GlobalSegments .Segments ==nil ){return nil ,_ec .Error (_ee ,"\u006eo\u0020\u0067\u006c\u006f\u0062\u0061\u006c\u0020\u0073\u0065\u0067m\u0065\u006e\u0074\u0073\u0020\u0066\u006f\u0075\u006e\u0064");};_fc :=Globals {};for _ ,_eeb :=range _ga .GlobalSegments .Segments {_fc [int (_eeb .SegmentNumber )]=_eeb ;};return _fc ,nil ;};func (_cg Globals )ToDocumentGlobals ()*_c .Globals {if _cg ==nil {return nil ;};_ed :=[]*_f .Header {};for _ ,_fae :=range _cg {_ed =append (_ed ,_fae );};_e .Slice (_ed ,func (_gd ,_gab int )bool {return _ed [_gd ].SegmentNumber < _ed [_gab ].SegmentNumber });return &_c .Globals {Segments :_ed };};func DecodeBytes (encoded []byte ,parameters _g .Parameters ,globals ...Globals )([]byte ,error ){var _fe Globals ;if len (globals )> 0{_fe =globals [0];};_be ,_fec :=_g .Decode (encoded ,parameters ,_fe .ToDocumentGlobals ());if _fec !=nil {return nil ,_fec ;};return _be .DecodeNextPage ();};
|
@ -9,4 +9,4 @@
|
||||
// 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 sampling ;import (_d "github.com/unidoc/unipdf/v3/internal/bitwise";_b "github.com/unidoc/unipdf/v3/internal/imageutil";_e "io";);func ResampleUint32 (data []uint32 ,bitsPerInputSample int ,bitsPerOutputSample int )[]uint32 {var _ec []uint32 ;_dee :=bitsPerOutputSample ;var _ce uint32 ;var _af uint32 ;_fff :=0;_ab :=0;_dfc :=0;for _dfc < len (data ){if _fff > 0{_ed :=_fff ;if _dee < _ed {_ed =_dee ;};_ce =(_ce <<uint (_ed ))|uint32 (_af >>uint (bitsPerInputSample -_ed ));_fff -=_ed ;if _fff > 0{_af =_af <<uint (_ed );}else {_af =0;};_dee -=_ed ;if _dee ==0{_ec =append (_ec ,_ce );_dee =bitsPerOutputSample ;_ce =0;_ab ++;};}else {_gad :=data [_dfc ];_dfc ++;_fd :=bitsPerInputSample ;if _dee < _fd {_fd =_dee ;};_fff =bitsPerInputSample -_fd ;_ce =(_ce <<uint (_fd ))|uint32 (_gad >>uint (_fff ));if _fd < bitsPerInputSample {_af =_gad <<uint (_fd );};_dee -=_fd ;if _dee ==0{_ec =append (_ec ,_ce );_dee =bitsPerOutputSample ;_ce =0;_ab ++;};};};for _fff >=bitsPerOutputSample {_eb :=_fff ;if _dee < _eb {_eb =_dee ;};_ce =(_ce <<uint (_eb ))|uint32 (_af >>uint (bitsPerInputSample -_eb ));_fff -=_eb ;if _fff > 0{_af =_af <<uint (_eb );}else {_af =0;};_dee -=_eb ;if _dee ==0{_ec =append (_ec ,_ce );_dee =bitsPerOutputSample ;_ce =0;_ab ++;};};if _dee > 0&&_dee < bitsPerOutputSample {_ce <<=uint (_dee );_ec =append (_ec ,_ce );};return _ec ;};func NewWriter (img _b .ImageBase )*Writer {return &Writer {_eg :_d .NewWriterMSB (img .Data ),_aea :img ,_cd :img .ColorComponents ,_cg :img .BytesPerLine *8!=img .ColorComponents *img .BitsPerComponent *img .Width };};func ResampleBytes (data []byte ,bitsPerSample int )[]uint32 {var _gg []uint32 ;_gfd :=bitsPerSample ;var _dc uint32 ;var _bd byte ;_gff :=0;_dd :=0;_df :=0;for _df < len (data ){if _gff > 0{_ae :=_gff ;if _gfd < _ae {_ae =_gfd ;};_dc =(_dc <<uint (_ae ))|uint32 (_bd >>uint (8-_ae ));_gff -=_ae ;if _gff > 0{_bd =_bd <<uint (_ae );}else {_bd =0;};_gfd -=_ae ;if _gfd ==0{_gg =append (_gg ,_dc );_gfd =bitsPerSample ;_dc =0;_dd ++;};}else {_gcc :=data [_df ];_df ++;_gd :=8;if _gfd < _gd {_gd =_gfd ;};_gff =8-_gd ;_dc =(_dc <<uint (_gd ))|uint32 (_gcc >>uint (_gff ));if _gd < 8{_bd =_gcc <<uint (_gd );};_gfd -=_gd ;if _gfd ==0{_gg =append (_gg ,_dc );_gfd =bitsPerSample ;_dc =0;_dd ++;};};};for _gff >=bitsPerSample {_fa :=_gff ;if _gfd < _fa {_fa =_gfd ;};_dc =(_dc <<uint (_fa ))|uint32 (_bd >>uint (8-_fa ));_gff -=_fa ;if _gff > 0{_bd =_bd <<uint (_fa );}else {_bd =0;};_gfd -=_fa ;if _gfd ==0{_gg =append (_gg ,_dc );_gfd =bitsPerSample ;_dc =0;_dd ++;};};return _gg ;};func (_gcd *Writer )WriteSamples (samples []uint32 )error {for _cea :=0;_cea < len (samples );_cea ++{if _gab :=_gcd .WriteSample (samples [_cea ]);_gab !=nil {return _gab ;};};return nil ;};func (_ff *Reader )ReadSample ()(uint32 ,error ){if _ff ._eae ==_ff ._c .Height {return 0,_e .EOF ;};_ga ,_gb :=_ff ._g .ReadBits (byte (_ff ._c .BitsPerComponent ));if _gb !=nil {return 0,_gb ;};_ff ._gc --;if _ff ._gc ==0{_ff ._gc =_ff ._c .ColorComponents ;_ff ._gf ++;};if _ff ._gf ==_ff ._c .Width {if _ff ._de {_ff ._g .ConsumeRemainingBits ();};_ff ._gf =0;_ff ._eae ++;};return uint32 (_ga ),nil ;};type SampleReader interface{ReadSample ()(uint32 ,error );ReadSamples (_ea []uint32 )error ;};func (_bg *Writer )WriteSample (sample uint32 )error {if _ ,_cfa :=_bg ._eg .WriteBits (uint64 (sample ),_bg ._aea .BitsPerComponent );_cfa !=nil {return _cfa ;};_bg ._cd --;if _bg ._cd ==0{_bg ._cd =_bg ._aea .ColorComponents ;_bg ._ffg ++;};if _bg ._ffg ==_bg ._aea .Width {if _bg ._cg {_bg ._eg .FinishByte ();};_bg ._ffg =0;};return nil ;};type Writer struct{_aea _b .ImageBase ;_eg *_d .Writer ;_ffg ,_cd int ;_cg bool ;};type Reader struct{_c _b .ImageBase ;_g *_d .Reader ;_gf ,_eae ,_gc int ;_de bool ;};func (_ca *Reader )ReadSamples (samples []uint32 )(_a error ){for _db :=0;_db < len (samples );_db ++{samples [_db ],_a =_ca .ReadSample ();if _a !=nil {return _a ;};};return nil ;};func NewReader (img _b .ImageBase )*Reader {return &Reader {_g :_d .NewReader (img .Data ),_c :img ,_gc :img .ColorComponents ,_de :img .BytesPerLine *8!=img .ColorComponents *img .BitsPerComponent *img .Width };};type SampleWriter interface{WriteSample (_cf uint32 )error ;WriteSamples (_gffe []uint32 )error ;};
|
||||
package sampling ;import (_c "github.com/unidoc/unipdf/v3/internal/bitwise";_g "github.com/unidoc/unipdf/v3/internal/imageutil";_b "io";);func ResampleUint32 (data []uint32 ,bitsPerInputSample int ,bitsPerOutputSample int )[]uint32 {var _cd []uint32 ;_be :=bitsPerOutputSample ;var _cda uint32 ;var _dfb uint32 ;_ff :=0;_cb :=0;_bc :=0;for _bc < len (data ){if _ff > 0{_eg :=_ff ;if _be < _eg {_eg =_be ;};_cda =(_cda <<uint (_eg ))|uint32 (_dfb >>uint (bitsPerInputSample -_eg ));_ff -=_eg ;if _ff > 0{_dfb =_dfb <<uint (_eg );}else {_dfb =0;};_be -=_eg ;if _be ==0{_cd =append (_cd ,_cda );_be =bitsPerOutputSample ;_cda =0;_cb ++;};}else {_ag :=data [_bc ];_bc ++;_dge :=bitsPerInputSample ;if _be < _dge {_dge =_be ;};_ff =bitsPerInputSample -_dge ;_cda =(_cda <<uint (_dge ))|uint32 (_ag >>uint (_ff ));if _dge < bitsPerInputSample {_dfb =_ag <<uint (_dge );};_be -=_dge ;if _be ==0{_cd =append (_cd ,_cda );_be =bitsPerOutputSample ;_cda =0;_cb ++;};};};for _ff >=bitsPerOutputSample {_fb :=_ff ;if _be < _fb {_fb =_be ;};_cda =(_cda <<uint (_fb ))|uint32 (_dfb >>uint (bitsPerInputSample -_fb ));_ff -=_fb ;if _ff > 0{_dfb =_dfb <<uint (_fb );}else {_dfb =0;};_be -=_fb ;if _be ==0{_cd =append (_cd ,_cda );_be =bitsPerOutputSample ;_cda =0;_cb ++;};};if _be > 0&&_be < bitsPerOutputSample {_cda <<=uint (_be );_cd =append (_cd ,_cda );};return _cd ;};func NewReader (img _g .ImageBase )*Reader {return &Reader {_cc :_c .NewReader (img .Data ),_d :img ,_ee :img .ColorComponents ,_bb :img .BytesPerLine *8!=img .ColorComponents *img .BitsPerComponent *img .Width };};type Writer struct{_bed _g .ImageBase ;_af *_c .Writer ;_bedc ,_ec int ;_bf bool ;};func (_fg *Reader )ReadSamples (samples []uint32 )(_a error ){for _cf :=0;_cf < len (samples );_cf ++{samples [_cf ],_a =_fg .ReadSample ();if _a !=nil {return _a ;};};return nil ;};func (_gb *Reader )ReadSample ()(uint32 ,error ){if _gb ._fd ==_gb ._d .Height {return 0,_b .EOF ;};_ef ,_dg :=_gb ._cc .ReadBits (byte (_gb ._d .BitsPerComponent ));if _dg !=nil {return 0,_dg ;};_gb ._ee --;if _gb ._ee ==0{_gb ._ee =_gb ._d .ColorComponents ;_gb ._gc ++;};if _gb ._gc ==_gb ._d .Width {if _gb ._bb {_gb ._cc .ConsumeRemainingBits ();};_gb ._gc =0;_gb ._fd ++;};return uint32 (_ef ),nil ;};type Reader struct{_d _g .ImageBase ;_cc *_c .Reader ;_gc ,_fd ,_ee int ;_bb bool ;};type SampleWriter interface{WriteSample (_cee uint32 )error ;WriteSamples (_cdf []uint32 )error ;};func (_dfg *Writer )WriteSample (sample uint32 )error {if _ ,_dee :=_dfg ._af .WriteBits (uint64 (sample ),_dfg ._bed .BitsPerComponent );_dee !=nil {return _dee ;};_dfg ._ec --;if _dfg ._ec ==0{_dfg ._ec =_dfg ._bed .ColorComponents ;_dfg ._bedc ++;};if _dfg ._bedc ==_dfg ._bed .Width {if _dfg ._bf {_dfg ._af .FinishByte ();};_dfg ._bedc =0;};return nil ;};type SampleReader interface{ReadSample ()(uint32 ,error );ReadSamples (_f []uint32 )error ;};func (_dfd *Writer )WriteSamples (samples []uint32 )error {for _gbfb :=0;_gbfb < len (samples );_gbfb ++{if _bd :=_dfd .WriteSample (samples [_gbfb ]);_bd !=nil {return _bd ;};};return nil ;};func ResampleBytes (data []byte ,bitsPerSample int )[]uint32 {var _ab []uint32 ;_de :=bitsPerSample ;var _ed uint32 ;var _gbf byte ;_df :=0;_fc :=0;_da :=0;for _da < len (data ){if _df > 0{_gcg :=_df ;if _de < _gcg {_gcg =_de ;};_ed =(_ed <<uint (_gcg ))|uint32 (_gbf >>uint (8-_gcg ));_df -=_gcg ;if _df > 0{_gbf =_gbf <<uint (_gcg );}else {_gbf =0;};_de -=_gcg ;if _de ==0{_ab =append (_ab ,_ed );_de =bitsPerSample ;_ed =0;_fc ++;};}else {_ce :=data [_da ];_da ++;_ea :=8;if _de < _ea {_ea =_de ;};_df =8-_ea ;_ed =(_ed <<uint (_ea ))|uint32 (_ce >>uint (_df ));if _ea < 8{_gbf =_ce <<uint (_ea );};_de -=_ea ;if _de ==0{_ab =append (_ab ,_ed );_de =bitsPerSample ;_ed =0;_fc ++;};};};for _df >=bitsPerSample {_eag :=_df ;if _de < _eag {_eag =_de ;};_ed =(_ed <<uint (_eag ))|uint32 (_gbf >>uint (8-_eag ));_df -=_eag ;if _df > 0{_gbf =_gbf <<uint (_eag );}else {_gbf =0;};_de -=_eag ;if _de ==0{_ab =append (_ab ,_ed );_de =bitsPerSample ;_ed =0;_fc ++;};};return _ab ;};func NewWriter (img _g .ImageBase )*Writer {return &Writer {_af :_c .NewWriterMSB (img .Data ),_bed :img ,_ec :img .ColorComponents ,_bf :img .BytesPerLine *8!=img .ColorComponents *img .BitsPerComponent *img .Width };};
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -9,4 +9,4 @@
|
||||
// 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 transform ;import (_a "fmt";_df "github.com/unidoc/unipdf/v3/common";_b "math";);func (_fbfg Matrix )Angle ()float64 {_cg :=_b .Atan2 (-_fbfg [1],_fbfg [0]);if _cg < 0.0{_cg +=2*_b .Pi ;};return _cg /_b .Pi *180.0;};func (_cc *Matrix )Shear (x ,y float64 ){_cc .Concat (ShearMatrix (x ,y ))};func ShearMatrix (x ,y float64 )Matrix {return NewMatrix (1,y ,x ,1,0,0)};func (_egbf *Point )transformByMatrix (_eba Matrix ){_egbf .X ,_egbf .Y =_eba .Transform (_egbf .X ,_egbf .Y );};func (_ed Point )Rotate (theta float64 )Point {_ba :=_b .Hypot (_ed .X ,_ed .Y );_abb :=_b .Atan2 (_ed .Y ,_ed .X );_edf ,_fda :=_b .Sincos (_abb +theta /180.0*_b .Pi );return Point {_ba *_fda ,_ba *_edf };};func (_f Matrix )Scale (xScale ,yScale float64 )Matrix {return _f .Mult (NewMatrix (xScale ,0,0,yScale ,0,0));};func (_cgg Matrix )Unrealistic ()bool {_feg ,_fd ,_ce ,_cag :=_b .Abs (_cgg [0]),_b .Abs (_cgg [1]),_b .Abs (_cgg [3]),_b .Abs (_cgg [4]);_gce :=_feg > _aff &&_cag > _aff ;_agg :=_fd > _aff &&_ce > _aff ;return !(_gce ||_agg );};func (_egb *Point )Transform (a ,b ,c ,d ,tx ,ty float64 ){_gbd :=NewMatrix (a ,b ,c ,d ,tx ,ty );_egb .transformByMatrix (_gbd );};func (_ga *Matrix )Clone ()Matrix {return NewMatrix (_ga [0],_ga [1],_ga [3],_ga [4],_ga [6],_ga [7])};func (_fc Matrix )Mult (b Matrix )Matrix {_fc .Concat (b );return _fc };func NewMatrix (a ,b ,c ,d ,tx ,ty float64 )Matrix {_eb :=Matrix {a ,b ,0,c ,d ,0,tx ,ty ,1};_eb .clampRange ();return _eb ;};func (_dbd *Matrix )clampRange (){for _be ,_dea :=range _dbd {if _dea > _ef {_df .Log .Debug ("\u0043L\u0041M\u0050\u003a\u0020\u0025\u0067\u0020\u002d\u003e\u0020\u0025\u0067",_dea ,_ef );_dbd [_be ]=_ef ;}else if _dea < -_ef {_df .Log .Debug ("\u0043L\u0041M\u0050\u003a\u0020\u0025\u0067\u0020\u002d\u003e\u0020\u0025\u0067",_dea ,-_ef );_dbd [_be ]=-_ef ;};};};func (_fa Matrix )ScalingFactorY ()float64 {return _b .Hypot (_fa [3],_fa [4])};func (_efe Point )Interpolate (b Point ,t float64 )Point {return Point {X :(1-t )*_efe .X +t *b .X ,Y :(1-t )*_efe .Y +t *b .Y };};type Point struct{X float64 ;Y float64 ;};const _cd =1.0e-6;func (_fbb Point )Displace (delta Point )Point {return Point {_fbb .X +delta .X ,_fbb .Y +delta .Y }};func (_fbf *Matrix )Concat (b Matrix ){*_fbf =Matrix {b [0]*_fbf [0]+b [1]*_fbf [3],b [0]*_fbf [1]+b [1]*_fbf [4],0,b [3]*_fbf [0]+b [4]*_fbf [3],b [3]*_fbf [1]+b [4]*_fbf [4],0,b [6]*_fbf [0]+b [7]*_fbf [3]+_fbf [6],b [6]*_fbf [1]+b [7]*_fbf [4]+_fbf [7],1};_fbf .clampRange ();};func (_bc Matrix )ScalingFactorX ()float64 {return _b .Hypot (_bc [0],_bc [1])};func (_ebf *Point )Set (x ,y float64 ){_ebf .X ,_ebf .Y =x ,y };func (_c Matrix )String ()string {_ge ,_gea ,_cf ,_cfb ,_gc ,_gec :=_c [0],_c [1],_c [3],_c [4],_c [6],_c [7];return _a .Sprintf ("\u005b\u00257\u002e\u0034\u0066\u002c%\u0037\u002e4\u0066\u002c\u0025\u0037\u002e\u0034\u0066\u002c%\u0037\u002e\u0034\u0066\u003a\u0025\u0037\u002e\u0034\u0066\u002c\u00257\u002e\u0034\u0066\u005d",_ge ,_gea ,_cf ,_cfb ,_gc ,_gec );};func TranslationMatrix (tx ,ty float64 )Matrix {return NewMatrix (1,0,0,1,tx ,ty )};func IdentityMatrix ()Matrix {return NewMatrix (1,0,0,1,0,0)};func (_afc Matrix )Inverse ()(Matrix ,bool ){_ab ,_eg :=_afc [0],_afc [1];_dc ,_bcf :=_afc [3],_afc [4];_bd ,_da :=_afc [6],_afc [7];_gebb :=_ab *_bcf -_eg *_dc ;if _b .Abs (_gebb )< _cd {return Matrix {},false ;};_ega ,_fe :=_bcf /_gebb ,-_eg /_gebb ;_egd ,_db :=-_dc /_gebb ,_ab /_gebb ;_gb :=-(_ega *_bd +_egd *_da );_bdd :=-(_fe *_bd +_db *_da );return NewMatrix (_ega ,_fe ,_egd ,_db ,_gb ,_bdd ),true ;};type Matrix [9]float64 ;func (_faa Point )String ()string {return _a .Sprintf ("(\u0025\u002e\u0032\u0066\u002c\u0025\u002e\u0032\u0066\u0029",_faa .X ,_faa .Y );};func RotationMatrix (angle float64 )Matrix {_e :=_b .Cos (angle );_g :=_b .Sin (angle );return NewMatrix (_e ,_g ,-_g ,_e ,0,0);};func (_dd Matrix )Rotate (theta float64 )Matrix {_de ,_ag :=_b .Sincos (theta /180.0*_b .Pi );return _dd .Mult (NewMatrix (_ag ,-_de ,_de ,_ag ,0,0));};func ScaleMatrix (x ,y float64 )Matrix {return NewMatrix (x ,0,0,y ,0,0)};const _aff =1e-6;const _ef =1e9;func (_af Matrix )Translation ()(float64 ,float64 ){return _af [6],_af [7]};func (_fb *Matrix )Set (a ,b ,c ,d ,tx ,ty float64 ){_fb [0],_fb [1]=a ,b ;_fb [3],_fb [4]=c ,d ;_fb [6],_fb [7]=tx ,ty ;_fb .clampRange ();};func (_gcc Point )Distance (b Point )float64 {return _b .Hypot (_gcc .X -b .X ,_gcc .Y -b .Y )};func (_geb Matrix )Transform (x ,y float64 )(float64 ,float64 ){_bf :=x *_geb [0]+y *_geb [3]+_geb [6];_ca :=x *_geb [1]+y *_geb [4]+_geb [7];return _bf ,_ca ;};func (_bb Matrix )Translate (tx ,ty float64 )Matrix {return NewMatrix (_bb [0],_bb [1],_bb [3],_bb [4],_bb [6]+tx ,_bb [7]+ty );};func NewMatrixFromTransforms (xScale ,yScale ,theta ,tx ,ty float64 )Matrix {return IdentityMatrix ().Scale (xScale ,yScale ).Rotate (theta ).Translate (tx ,ty );};func NewPoint (x ,y float64 )Point {return Point {X :x ,Y :y }};
|
||||
package transform ;import (_g "fmt";_a "github.com/unidoc/unipdf/v3/common";_e "math";);const _ece =1e9;func (_bfb *Point )transformByMatrix (_bba Matrix ){_bfb .X ,_bfb .Y =_bba .Transform (_bfb .X ,_bfb .Y )};func ScaleMatrix (x ,y float64 )Matrix {return NewMatrix (x ,0,0,y ,0,0)};func (_gaf Point )Rotate (theta float64 )Point {_fadd :=_e .Hypot (_gaf .X ,_gaf .Y );_dgb :=_e .Atan2 (_gaf .Y ,_gaf .X );_dbg ,_gca :=_e .Sincos (_dgb +theta /180.0*_e .Pi );return Point {_fadd *_gca ,_fadd *_dbg };};func TranslationMatrix (tx ,ty float64 )Matrix {return NewMatrix (1,0,0,1,tx ,ty )};func (_ac Matrix )String ()string {_f ,_gc ,_gf ,_b ,_bb ,_ge :=_ac [0],_ac [1],_ac [3],_ac [4],_ac [6],_ac [7];return _g .Sprintf ("\u005b\u00257\u002e\u0034\u0066\u002c%\u0037\u002e4\u0066\u002c\u0025\u0037\u002e\u0034\u0066\u002c%\u0037\u002e\u0034\u0066\u003a\u0025\u0037\u002e\u0034\u0066\u002c\u00257\u002e\u0034\u0066\u005d",_f ,_gc ,_gf ,_b ,_bb ,_ge );};func ShearMatrix (x ,y float64 )Matrix {return NewMatrix (1,y ,x ,1,0,0)};func IdentityMatrix ()Matrix {return NewMatrix (1,0,0,1,0,0)};func (_bf Matrix )Translation ()(float64 ,float64 ){return _bf [6],_bf [7]};func (_gfdc Point )String ()string {return _g .Sprintf ("(\u0025\u002e\u0032\u0066\u002c\u0025\u002e\u0032\u0066\u0029",_gfdc .X ,_gfdc .Y );};func (_gd Matrix )Mult (b Matrix )Matrix {_gd .Concat (b );return _gd };func (_gcd Point )Displace (delta Point )Point {return Point {_gcd .X +delta .X ,_gcd .Y +delta .Y }};func (_cgf *Matrix )clampRange (){for _fee ,_ecb :=range _cgf {if _ecb > _ece {_a .Log .Debug ("\u0043L\u0041M\u0050\u003a\u0020\u0025\u0067\u0020\u002d\u003e\u0020\u0025\u0067",_ecb ,_ece );_cgf [_fee ]=_ece ;}else if _ecb < -_ece {_a .Log .Debug ("\u0043L\u0041M\u0050\u003a\u0020\u0025\u0067\u0020\u002d\u003e\u0020\u0025\u0067",_ecb ,-_ece );_cgf [_fee ]=-_ece ;};};};type Point struct{X float64 ;Y float64 ;};func (_ad *Matrix )Clone ()Matrix {return NewMatrix (_ad [0],_ad [1],_ad [3],_ad [4],_ad [6],_ad [7])};func NewPoint (x ,y float64 )Point {return Point {X :x ,Y :y }};const _aag =1.0e-6;func (_cg Matrix )Scale (xScale ,yScale float64 )Matrix {return _cg .Mult (NewMatrix (xScale ,0,0,yScale ,0,0));};func (_bd Matrix )ScalingFactorY ()float64 {return _e .Hypot (_bd [3],_bd [4])};func (_bgd *Point )Set (x ,y float64 ){_bgd .X ,_bgd .Y =x ,y };func (_fc Matrix )ScalingFactorX ()float64 {return _e .Hypot (_fc [0],_fc [1])};type Matrix [9]float64 ;func (_eff Matrix )Unrealistic ()bool {_be ,_ga ,_dgg ,_fad :=_e .Abs (_eff [0]),_e .Abs (_eff [1]),_e .Abs (_eff [3]),_e .Abs (_eff [4]);_gfd :=_be > _ddf &&_fad > _ddf ;_bc :=_ga > _ddf &&_dgg > _ddf ;return !(_gfd ||_bc );};func NewMatrixFromTransforms (xScale ,yScale ,theta ,tx ,ty float64 )Matrix {return IdentityMatrix ().Scale (xScale ,yScale ).Rotate (theta ).Translate (tx ,ty );};func (_dgc Point )Distance (b Point )float64 {return _e .Hypot (_dgc .X -b .X ,_dgc .Y -b .Y )};func RotationMatrix (angle float64 )Matrix {_ab :=_e .Cos (angle );_dg :=_e .Sin (angle );return NewMatrix (_ab ,_dg ,-_dg ,_ab ,0,0);};func (_cd *Matrix )Concat (b Matrix ){*_cd =Matrix {b [0]*_cd [0]+b [1]*_cd [3],b [0]*_cd [1]+b [1]*_cd [4],0,b [3]*_cd [0]+b [4]*_cd [3],b [3]*_cd [1]+b [4]*_cd [4],0,b [6]*_cd [0]+b [7]*_cd [3]+_cd [6],b [6]*_cd [1]+b [7]*_cd [4]+_cd [7],1};_cd .clampRange ();};func (_fga Matrix )Angle ()float64 {_bg :=_e .Atan2 (-_fga [1],_fga [0]);if _bg < 0.0{_bg +=2*_e .Pi ;};return _bg /_e .Pi *180.0;};func (_dcb Matrix )Transform (x ,y float64 )(float64 ,float64 ){_df :=x *_dcb [0]+y *_dcb [3]+_dcb [6];_abe :=x *_dcb [1]+y *_dcb [4]+_dcb [7];return _df ,_abe ;};const _ddf =1e-6;func (_aa Matrix )Translate (tx ,ty float64 )Matrix {return NewMatrix (_aa [0],_aa [1],_aa [3],_aa [4],_aa [6]+tx ,_aa [7]+ty );};func (_dc Matrix )Rotate (theta float64 )Matrix {_fe ,_dgd :=_e .Sincos (theta /180.0*_e .Pi );return _dc .Mult (NewMatrix (_dgd ,-_fe ,_fe ,_dgd ,0,0));};func (_ea Matrix )Inverse ()(Matrix ,bool ){_dd ,_adg :=_ea [0],_ea [1];_cf ,_bdb :=_ea [3],_ea [4];_cb ,_fbe :=_ea [6],_ea [7];_cdc :=_dd *_bdb -_adg *_cf ;if _e .Abs (_cdc )< _aag {return Matrix {},false ;};_ec ,_fa :=_bdb /_cdc ,-_adg /_cdc ;_abed ,_de :=-_cf /_cdc ,_dd /_cdc ;_ef :=-(_ec *_cb +_abed *_fbe );_af :=-(_fa *_cb +_de *_fbe );return NewMatrix (_ec ,_fa ,_abed ,_de ,_ef ,_af ),true ;};func NewMatrix (a ,b ,c ,d ,tx ,ty float64 )Matrix {_c :=Matrix {a ,b ,0,c ,d ,0,tx ,ty ,1};_c .clampRange ();return _c ;};func (_db *Point )Transform (a ,b ,c ,d ,tx ,ty float64 ){_eg :=NewMatrix (a ,b ,c ,d ,tx ,ty );_db .transformByMatrix (_eg );};func (_dff Point )Interpolate (b Point ,t float64 )Point {return Point {X :(1-t )*_dff .X +t *b .X ,Y :(1-t )*_dff .Y +t *b .Y };};func (_fg *Matrix )Set (a ,b ,c ,d ,tx ,ty float64 ){_fg [0],_fg [1]=a ,b ;_fg [3],_fg [4]=c ,d ;_fg [6],_fg [7]=tx ,ty ;_fg .clampRange ();};func (_fb *Matrix )Shear (x ,y float64 ){_fb .Concat (ShearMatrix (x ,y ))};
|
File diff suppressed because one or more lines are too long
4558
model/model.go
4558
model/model.go
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -10,75 +10,98 @@
|
||||
// terms that can be accessed at https://unidoc.io/eula/
|
||||
|
||||
// Package sighandler implements digital signature handlers for PDF signature validation and signing.
|
||||
package sighandler ;import (_b "bytes";_ce "crypto";_cea "crypto/rand";_fbe "crypto/rsa";_fd "crypto/x509";_be "crypto/x509/pkix";_gg "encoding/asn1";_gc "errors";_fe "fmt";_d "github.com/unidoc/pkcs7";_fg "github.com/unidoc/timestamp";_a "github.com/unidoc/unipdf/v3/core";_ec "github.com/unidoc/unipdf/v3/model";_fc "hash";_g "io";_e "io/ioutil";_fb "net/http";_c "time";);
|
||||
|
||||
// NewAdobeX509RSASHA1 creates a new Adobe.PPKMS/Adobe.PPKLite adbe.x509.rsa_sha1 signature handler.
|
||||
// Both parameters may be nil for the signature validation.
|
||||
func NewAdobeX509RSASHA1 (privateKey *_fbe .PrivateKey ,certificate *_fd .Certificate )(_ec .SignatureHandler ,error ){return &adobeX509RSASHA1 {_cbf :certificate ,_ccd :privateKey },nil ;};type timestampInfo struct{Version int ;Policy _gg .RawValue ;MessageImprint struct{HashAlgorithm _be .AlgorithmIdentifier ;HashedMessage []byte ;};SerialNumber _gg .RawValue ;GeneralizedTime _c .Time ;};type docTimeStamp struct{_dcf string ;_eff _ce .Hash ;};
|
||||
|
||||
// Sign sets the Contents fields for the PdfSignature.
|
||||
func (_dbcf *adobeX509RSASHA1 )Sign (sig *_ec .PdfSignature ,digest _ec .Hasher )error {var _de []byte ;var _gac error ;if _dbcf ._gf !=nil {_de ,_gac =_dbcf ._gf (sig ,digest );if _gac !=nil {return _gac ;};}else {_ege ,_gfd :=digest .(_fc .Hash );if !_gfd {return _gc .New ("\u0068a\u0073h\u0020\u0074\u0079\u0070\u0065\u0020\u0065\u0072\u0072\u006f\u0072");};_bgc ,_ :=_cfd (_dbcf ._cbf .SignatureAlgorithm );_de ,_gac =_fbe .SignPKCS1v15 (_cea .Reader ,_dbcf ._ccd ,_bgc ,_ege .Sum (nil ));if _gac !=nil {return _gac ;};};_de ,_gac =_gg .Marshal (_de );if _gac !=nil {return _gac ;};sig .Contents =_a .MakeHexString (string (_de ));return nil ;};
|
||||
|
||||
// SignFunc represents a custom signing function. The function should return
|
||||
// the computed signature.
|
||||
type SignFunc func (_cfe *_ec .PdfSignature ,_cae _ec .Hasher )([]byte ,error );
|
||||
|
||||
// NewDigest creates a new digest.
|
||||
func (_edg *docTimeStamp )NewDigest (sig *_ec .PdfSignature )(_ec .Hasher ,error ){return _b .NewBuffer (nil ),nil ;};
|
||||
|
||||
// NewAdobeX509RSASHA1Custom creates a new Adobe.PPKMS/Adobe.PPKLite adbe.x509.rsa_sha1 signature handler
|
||||
// with a custom signing function. Both parameters may be nil for the signature validation.
|
||||
func NewAdobeX509RSASHA1Custom (certificate *_fd .Certificate ,signFunc SignFunc )(_ec .SignatureHandler ,error ){return &adobeX509RSASHA1 {_cbf :certificate ,_gf :signFunc },nil ;};func (_bd *adobeX509RSASHA1 )getCertificate (_fbb *_ec .PdfSignature )(*_fd .Certificate ,error ){if _bd ._cbf !=nil {return _bd ._cbf ,nil ;};var _gga []byte ;switch _cca :=_fbb .Cert .(type ){case *_a .PdfObjectString :_gga =_cca .Bytes ();case *_a .PdfObjectArray :if _cca .Len ()==0{return nil ,_gc .New ("\u006e\u006f\u0020s\u0069\u0067\u006e\u0061t\u0075\u0072\u0065\u0020\u0063\u0065\u0072t\u0069\u0066\u0069\u0063\u0061\u0074\u0065\u0073\u0020\u0066\u006f\u0075\u006e\u0064");};for _ ,_gb :=range _cca .Elements (){_cda ,_gce :=_a .GetString (_gb );if !_gce {return nil ,_fe .Errorf ("\u0069\u006ev\u0061\u006c\u0069\u0064\u0020\u0063\u0065\u0072\u0074\u0069\u0066\u0069\u0063\u0061\u0074\u0065\u0020\u006f\u0062j\u0065\u0063\u0074\u0020\u0074\u0079p\u0065\u0020\u0069\u006e\u0020\u0073\u0069\u0067\u006e\u0061\u0074\u0075\u0072\u0065 \u0063\u0065r\u0074\u0069\u0066\u0069c\u0061\u0074\u0065\u0020\u0063h\u0061\u0069\u006e\u003a\u0020\u0025\u0054",_gb );};_gga =append (_gga ,_cda .Bytes ()...);};default:return nil ,_fe .Errorf ("\u0069n\u0076\u0061l\u0069\u0064\u0020s\u0069\u0067\u006e\u0061\u0074\u0075\u0072e\u0020\u0063\u0065\u0072\u0074\u0069f\u0069\u0063\u0061\u0074\u0065\u0020\u006f\u0062\u006a\u0065\u0063t\u0020\u0074\u0079\u0070\u0065\u003a\u0020\u0025\u0054",_cca );};_eed ,_ced :=_fd .ParseCertificates (_gga );if _ced !=nil {return nil ,_ced ;};return _eed [0],nil ;};
|
||||
|
||||
// Sign sets the Contents fields.
|
||||
func (_baab *adobePKCS7Detached )Sign (sig *_ec .PdfSignature ,digest _ec .Hasher )error {if _baab ._fcc {_ee :=_baab ._ae ;if _ee <=0{_ee =8192;};sig .Contents =_a .MakeHexString (string (make ([]byte ,_ee )));return nil ;};_dba :=digest .(*_b .Buffer );_ab ,_ecf :=_d .NewSignedData (_dba .Bytes ());if _ecf !=nil {return _ecf ;};if _fcd :=_ab .AddSigner (_baab ._df ,_baab ._ac ,_d .SignerInfoConfig {});_fcd !=nil {return _fcd ;};_ab .Detach ();_bc ,_ecf :=_ab .Finish ();if _ecf !=nil {return _ecf ;};_cbb :=make ([]byte ,8192);copy (_cbb ,_bc );sig .Contents =_a .MakeHexString (string (_cbb ));return nil ;};
|
||||
|
||||
// NewDocTimeStamp creates a new DocTimeStamp signature handler.
|
||||
// The timestampServerURL parameter can be empty string for the signature validation.
|
||||
// The hashAlgorithm parameter can be crypto.SHA1, crypto.SHA256, crypto.SHA384, crypto.SHA512.
|
||||
func NewDocTimeStamp (timestampServerURL string ,hashAlgorithm _ce .Hash )(_ec .SignatureHandler ,error ){return &docTimeStamp {_dcf :timestampServerURL ,_eff :hashAlgorithm },nil ;};
|
||||
|
||||
// IsApplicable returns true if the signature handler is applicable for the PdfSignature.
|
||||
func (_dc *adobeX509RSASHA1 )IsApplicable (sig *_ec .PdfSignature )bool {if sig ==nil ||sig .Filter ==nil ||sig .SubFilter ==nil {return false ;};return (*sig .Filter =="A\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004d\u0053"||*sig .Filter =="\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065")&&*sig .SubFilter =="\u0061d\u0062e\u002e\u0078\u0035\u0030\u0039.\u0072\u0073a\u005f\u0073\u0068\u0061\u0031";};
|
||||
|
||||
// Validate validates PdfSignature.
|
||||
func (_ggb *docTimeStamp )Validate (sig *_ec .PdfSignature ,digest _ec .Hasher )(_ec .SignatureValidationResult ,error ){_bdb :=sig .Contents .Bytes ();_cge ,_gdge :=_d .Parse (_bdb );if _gdge !=nil {return _ec .SignatureValidationResult {},_gdge ;};if _gdge =_cge .Verify ();_gdge !=nil {return _ec .SignatureValidationResult {},_gdge ;};var _bcc timestampInfo ;_ ,_gdge =_gg .Unmarshal (_cge .Content ,&_bcc );if _gdge !=nil {return _ec .SignatureValidationResult {},_gdge ;};_gbd ,_gdge :=_gdg (_bcc .MessageImprint .HashAlgorithm .Algorithm );if _gdge !=nil {return _ec .SignatureValidationResult {},_gdge ;};_dd :=_gbd .New ();_ace :=digest .(*_b .Buffer );_dd .Write (_ace .Bytes ());_fgdf :=_dd .Sum (nil );_eab :=_ec .SignatureValidationResult {IsSigned :true ,IsVerified :_b .Equal (_fgdf ,_bcc .MessageImprint .HashedMessage ),GeneralizedTime :_bcc .GeneralizedTime };return _eab ,nil ;};
|
||||
package sighandler ;import (_f "bytes";_ff "crypto";_gf "crypto/rand";_ae "crypto/rsa";_gc "crypto/x509";_dc "crypto/x509/pkix";_b "encoding/asn1";_g "errors";_dg "fmt";_cd "github.com/unidoc/pkcs7";_gca "github.com/unidoc/timestamp";_ba "github.com/unidoc/unipdf/v3/core";_da "github.com/unidoc/unipdf/v3/model";_de "hash";_c "io";_a "io/ioutil";_eg "net/http";_d "time";);type adobePKCS7Detached struct{_fg *_ae .PrivateKey ;_fd *_gc .Certificate ;_ca bool ;_dgb int ;};
|
||||
|
||||
// InitSignature initialises the PdfSignature.
|
||||
func (_ea *docTimeStamp )InitSignature (sig *_ec .PdfSignature )error {_eba :=*_ea ;sig .Handler =&_eba ;sig .Filter =_a .MakeName ("\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065");sig .SubFilter =_a .MakeName ("\u0045\u0054\u0053I\u002e\u0052\u0046\u0043\u0033\u0031\u0036\u0031");sig .Reference =nil ;_bbe ,_afc :=_ea .NewDigest (sig );if _afc !=nil {return _afc ;};_bbe .Write ([]byte ("\u0063\u0061\u006c\u0063\u0075\u006ca\u0074\u0065\u0020\u0074\u0068\u0065\u0020\u0043\u006f\u006e\u0074\u0065\u006et\u0073\u0020\u0066\u0069\u0065\u006c\u0064 \u0073\u0069\u007a\u0065"));return _eba .Sign (sig ,_bbe );};
|
||||
func (_ffb *adobePKCS7Detached )InitSignature (sig *_da .PdfSignature )error {if !_ffb ._ca {if _ffb ._fd ==nil {return _g .New ("c\u0065\u0072\u0074\u0069\u0066\u0069c\u0061\u0074\u0065\u0020\u006d\u0075\u0073\u0074\u0020n\u006f\u0074\u0020b\u0065 \u006e\u0069\u006c");};if _ffb ._fg ==nil {return _g .New ("\u0070\u0072\u0069\u0076\u0061\u0074\u0065\u004b\u0065\u0079\u0020m\u0075\u0073\u0074\u0020\u006e\u006f\u0074\u0020\u0062\u0065 \u006e\u0069\u006c");};};_cad :=*_ffb ;sig .Handler =&_cad ;sig .Filter =_ba .MakeName ("\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065");sig .SubFilter =_ba .MakeName ("\u0061\u0064\u0062\u0065.p\u006b\u0063\u0073\u0037\u002e\u0064\u0065\u0074\u0061\u0063\u0068\u0065\u0064");sig .Reference =nil ;_bd ,_gd :=_cad .NewDigest (sig );if _gd !=nil {return _gd ;};_bd .Write ([]byte ("\u0063\u0061\u006c\u0063\u0075\u006ca\u0074\u0065\u0020\u0074\u0068\u0065\u0020\u0043\u006f\u006e\u0074\u0065\u006et\u0073\u0020\u0066\u0069\u0065\u006c\u0064 \u0073\u0069\u007a\u0065"));return _cad .Sign (sig ,_bd );};
|
||||
|
||||
// NewAdobePKCS7Detached creates a new Adobe.PPKMS/Adobe.PPKLite adbe.pkcs7.detached signature handler.
|
||||
// Both parameters may be nil for the signature validation.
|
||||
func NewAdobePKCS7Detached (privateKey *_fbe .PrivateKey ,certificate *_fd .Certificate )(_ec .SignatureHandler ,error ){return &adobePKCS7Detached {_df :certificate ,_ac :privateKey },nil ;};func (_cb *adobePKCS7Detached )getCertificate (_dg *_ec .PdfSignature )(*_fd .Certificate ,error ){if _cb ._df !=nil {return _cb ._df ,nil ;};var _eg []byte ;switch _aed :=_dg .Cert .(type ){case *_a .PdfObjectString :_eg =_aed .Bytes ();case *_a .PdfObjectArray :if _aed .Len ()==0{return nil ,_gc .New ("\u006e\u006f\u0020s\u0069\u0067\u006e\u0061t\u0075\u0072\u0065\u0020\u0063\u0065\u0072t\u0069\u0066\u0069\u0063\u0061\u0074\u0065\u0073\u0020\u0066\u006f\u0075\u006e\u0064");};for _ ,_fee :=range _aed .Elements (){_db ,_ef :=_a .GetString (_fee );if !_ef {return nil ,_fe .Errorf ("\u0069\u006ev\u0061\u006c\u0069\u0064\u0020\u0063\u0065\u0072\u0074\u0069\u0066\u0069\u0063\u0061\u0074\u0065\u0020\u006f\u0062j\u0065\u0063\u0074\u0020\u0074\u0079p\u0065\u0020\u0069\u006e\u0020\u0073\u0069\u0067\u006e\u0061\u0074\u0075\u0072\u0065 \u0063\u0065r\u0074\u0069\u0066\u0069c\u0061\u0074\u0065\u0020\u0063h\u0061\u0069\u006e\u003a\u0020\u0025\u0054",_fee );};_eg =append (_eg ,_db .Bytes ()...);};default:return nil ,_fe .Errorf ("\u0069n\u0076\u0061l\u0069\u0064\u0020s\u0069\u0067\u006e\u0061\u0074\u0075\u0072e\u0020\u0063\u0065\u0072\u0074\u0069f\u0069\u0063\u0061\u0074\u0065\u0020\u006f\u0062\u006a\u0065\u0063t\u0020\u0074\u0079\u0070\u0065\u003a\u0020\u0025\u0054",_aed );};_bb ,_gde :=_fd .ParseCertificates (_eg );if _gde !=nil {return nil ,_gde ;};return _bb [0],nil ;};type adobeX509RSASHA1 struct{_ccd *_fbe .PrivateKey ;_cbf *_fd .Certificate ;_gf SignFunc ;};func _cfd (_cfdd _fd .SignatureAlgorithm )(_ce .Hash ,bool ){var _fgd _ce .Hash ;switch _cfdd {case _fd .SHA1WithRSA :_fgd =_ce .SHA1 ;case _fd .SHA256WithRSA :_fgd =_ce .SHA256 ;case _fd .SHA384WithRSA :_fgd =_ce .SHA384 ;case _fd .SHA512WithRSA :_fgd =_ce .SHA512 ;default:return _ce .SHA1 ,false ;};return _fgd ,true ;};func (_ece *docTimeStamp )getCertificate (_gced *_ec .PdfSignature )(*_fd .Certificate ,error ){var _cbfc []byte ;switch _bgcg :=_gced .Cert .(type ){case *_a .PdfObjectString :_cbfc =_bgcg .Bytes ();case *_a .PdfObjectArray :if _bgcg .Len ()==0{return nil ,_gc .New ("\u006e\u006f\u0020s\u0069\u0067\u006e\u0061t\u0075\u0072\u0065\u0020\u0063\u0065\u0072t\u0069\u0066\u0069\u0063\u0061\u0074\u0065\u0073\u0020\u0066\u006f\u0075\u006e\u0064");};for _ ,_gfe :=range _bgcg .Elements (){_fde ,_bdc :=_a .GetString (_gfe );if !_bdc {return nil ,_fe .Errorf ("\u0069\u006ev\u0061\u006c\u0069\u0064\u0020\u0063\u0065\u0072\u0074\u0069\u0066\u0069\u0063\u0061\u0074\u0065\u0020\u006f\u0062j\u0065\u0063\u0074\u0020\u0074\u0079p\u0065\u0020\u0069\u006e\u0020\u0073\u0069\u0067\u006e\u0061\u0074\u0075\u0072\u0065 \u0063\u0065r\u0074\u0069\u0066\u0069c\u0061\u0074\u0065\u0020\u0063h\u0061\u0069\u006e\u003a\u0020\u0025\u0054",_gfe );};_cbfc =append (_cbfc ,_fde .Bytes ()...);};default:return nil ,_fe .Errorf ("\u0069n\u0076\u0061l\u0069\u0064\u0020s\u0069\u0067\u006e\u0061\u0074\u0075\u0072e\u0020\u0063\u0065\u0072\u0074\u0069f\u0069\u0063\u0061\u0074\u0065\u0020\u006f\u0062\u006a\u0065\u0063t\u0020\u0074\u0079\u0070\u0065\u003a\u0020\u0025\u0054",_bgcg );};_eee ,_cdg :=_fd .ParseCertificates (_cbfc );if _cdg !=nil {return nil ,_cdg ;};return _eee [0],nil ;};
|
||||
|
||||
// IsApplicable returns true if the signature handler is applicable for the PdfSignature
|
||||
func (_cg *adobePKCS7Detached )IsApplicable (sig *_ec .PdfSignature )bool {if sig ==nil ||sig .Filter ==nil ||sig .SubFilter ==nil {return false ;};return (*sig .Filter =="A\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004d\u0053"||*sig .Filter =="\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065")&&*sig .SubFilter =="\u0061\u0064\u0062\u0065.p\u006b\u0063\u0073\u0037\u002e\u0064\u0065\u0074\u0061\u0063\u0068\u0065\u0064";};type adobePKCS7Detached struct{_ac *_fbe .PrivateKey ;_df *_fd .Certificate ;_fcc bool ;_ae int ;};
|
||||
|
||||
// IsApplicable returns true if the signature handler is applicable for the PdfSignature.
|
||||
func (_gag *docTimeStamp )IsApplicable (sig *_ec .PdfSignature )bool {if sig ==nil ||sig .Filter ==nil ||sig .SubFilter ==nil {return false ;};return (*sig .Filter =="A\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004d\u0053"||*sig .Filter =="\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065")&&*sig .SubFilter =="\u0045\u0054\u0053I\u002e\u0052\u0046\u0043\u0033\u0031\u0036\u0031";};
|
||||
// Validate validates PdfSignature.
|
||||
func (_egf *adobePKCS7Detached )Validate (sig *_da .PdfSignature ,digest _da .Hasher )(_da .SignatureValidationResult ,error ){_af :=sig .Contents .Bytes ();_dec ,_ef :=_cd .Parse (_af );if _ef !=nil {return _da .SignatureValidationResult {},_ef ;};_bdd :=digest .(*_f .Buffer );_dec .Content =_bdd .Bytes ();if _ef =_dec .Verify ();_ef !=nil {return _da .SignatureValidationResult {},_ef ;};return _da .SignatureValidationResult {IsSigned :true ,IsVerified :true },nil ;};
|
||||
|
||||
// NewDigest creates a new digest.
|
||||
func (_ga *adobeX509RSASHA1 )NewDigest (sig *_ec .PdfSignature )(_ec .Hasher ,error ){_dbc ,_fcg :=_ga .getCertificate (sig );if _fcg !=nil {return nil ,_fcg ;};_gdb ,_ :=_cfd (_dbc .SignatureAlgorithm );return _gdb .New (),nil ;};
|
||||
func (_geb *adobeX509RSASHA1 )NewDigest (sig *_da .PdfSignature )(_da .Hasher ,error ){_fbd ,_db :=_geb .getCertificate (sig );if _db !=nil {return nil ,_db ;};_ab ,_ :=_eec (_fbd .SignatureAlgorithm );return _ab .New (),nil ;};
|
||||
|
||||
// IsApplicable returns true if the signature handler is applicable for the PdfSignature
|
||||
func (_ce *adobePKCS7Detached )IsApplicable (sig *_da .PdfSignature )bool {if sig ==nil ||sig .Filter ==nil ||sig .SubFilter ==nil {return false ;};return (*sig .Filter =="A\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004d\u0053"||*sig .Filter =="\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065")&&*sig .SubFilter =="\u0061\u0064\u0062\u0065.p\u006b\u0063\u0073\u0037\u002e\u0064\u0065\u0074\u0061\u0063\u0068\u0065\u0064";};func (_aee *adobePKCS7Detached )getCertificate (_daa *_da .PdfSignature )(*_gc .Certificate ,error ){if _aee ._fd !=nil {return _aee ._fd ,nil ;};var _aa []byte ;switch _gcag :=_daa .Cert .(type ){case *_ba .PdfObjectString :_aa =_gcag .Bytes ();case *_ba .PdfObjectArray :if _gcag .Len ()==0{return nil ,_g .New ("\u006e\u006f\u0020s\u0069\u0067\u006e\u0061t\u0075\u0072\u0065\u0020\u0063\u0065\u0072t\u0069\u0066\u0069\u0063\u0061\u0074\u0065\u0073\u0020\u0066\u006f\u0075\u006e\u0064");};for _ ,_be :=range _gcag .Elements (){_fb ,_ea :=_ba .GetString (_be );if !_ea {return nil ,_dg .Errorf ("\u0069\u006ev\u0061\u006c\u0069\u0064\u0020\u0063\u0065\u0072\u0074\u0069\u0066\u0069\u0063\u0061\u0074\u0065\u0020\u006f\u0062j\u0065\u0063\u0074\u0020\u0074\u0079p\u0065\u0020\u0069\u006e\u0020\u0073\u0069\u0067\u006e\u0061\u0074\u0075\u0072\u0065 \u0063\u0065r\u0074\u0069\u0066\u0069c\u0061\u0074\u0065\u0020\u0063h\u0061\u0069\u006e\u003a\u0020\u0025\u0054",_be );};_aa =append (_aa ,_fb .Bytes ()...);};default:return nil ,_dg .Errorf ("\u0069n\u0076\u0061l\u0069\u0064\u0020s\u0069\u0067\u006e\u0061\u0074\u0075\u0072e\u0020\u0063\u0065\u0072\u0074\u0069f\u0069\u0063\u0061\u0074\u0065\u0020\u006f\u0062\u006a\u0065\u0063t\u0020\u0074\u0079\u0070\u0065\u003a\u0020\u0025\u0054",_gcag );};_cf ,_aeg :=_gc .ParseCertificates (_aa );if _aeg !=nil {return nil ,_aeg ;};return _cf [0],nil ;};
|
||||
|
||||
// NewDocTimeStamp creates a new DocTimeStamp signature handler.
|
||||
// Both the timestamp server URL and the hash algorithm can be empty for the
|
||||
// signature validation.
|
||||
// The following hash algorithms are supported:
|
||||
// crypto.SHA1, crypto.SHA256, crypto.SHA384, crypto.SHA512.
|
||||
// NOTE: the handler will do a mock Sign when initializing the signature
|
||||
// in order to estimate the signature size. Use NewDocTimeStampWithOpts
|
||||
// for providing the signature size.
|
||||
func NewDocTimeStamp (timestampServerURL string ,hashAlgorithm _ff .Hash )(_da .SignatureHandler ,error ){return &docTimeStamp {_dbe :timestampServerURL ,_dece :hashAlgorithm },nil ;};
|
||||
|
||||
// NewEmptyAdobePKCS7Detached creates a new Adobe.PPKMS/Adobe.PPKLite adbe.pkcs7.detached
|
||||
// signature handler. The generated signature is empty and of size signatureLen.
|
||||
// The signatureLen parameter can be 0 for the signature validation.
|
||||
func NewEmptyAdobePKCS7Detached (signatureLen int )(_ec .SignatureHandler ,error ){return &adobePKCS7Detached {_fcc :true ,_ae :signatureLen },nil ;};
|
||||
func NewEmptyAdobePKCS7Detached (signatureLen int )(_da .SignatureHandler ,error ){return &adobePKCS7Detached {_ca :true ,_dgb :signatureLen },nil ;};func _eec (_gcg _gc .SignatureAlgorithm )(_ff .Hash ,bool ){var _deb _ff .Hash ;switch _gcg {case _gc .SHA1WithRSA :_deb =_ff .SHA1 ;case _gc .SHA256WithRSA :_deb =_ff .SHA256 ;case _gc .SHA384WithRSA :_deb =_ff .SHA384 ;case _gc .SHA512WithRSA :_deb =_ff .SHA512 ;default:return _ff .SHA1 ,false ;};return _deb ,true ;};func _bfa (_bae _b .ObjectIdentifier )(_ff .Hash ,error ){switch {case _bae .Equal (_cd .OIDDigestAlgorithmSHA1 ),_bae .Equal (_cd .OIDDigestAlgorithmECDSASHA1 ),_bae .Equal (_cd .OIDDigestAlgorithmDSA ),_bae .Equal (_cd .OIDDigestAlgorithmDSASHA1 ),_bae .Equal (_cd .OIDEncryptionAlgorithmRSA ):return _ff .SHA1 ,nil ;case _bae .Equal (_cd .OIDDigestAlgorithmSHA256 ),_bae .Equal (_cd .OIDDigestAlgorithmECDSASHA256 ):return _ff .SHA256 ,nil ;case _bae .Equal (_cd .OIDDigestAlgorithmSHA384 ),_bae .Equal (_cd .OIDDigestAlgorithmECDSASHA384 ):return _ff .SHA384 ,nil ;case _bae .Equal (_cd .OIDDigestAlgorithmSHA512 ),_bae .Equal (_cd .OIDDigestAlgorithmECDSASHA512 ):return _ff .SHA512 ,nil ;};return _ff .Hash (0),_cd .ErrUnsupportedAlgorithm ;};
|
||||
|
||||
// Validate validates PdfSignature.
|
||||
func (_cd *adobePKCS7Detached )Validate (sig *_ec .PdfSignature ,digest _ec .Hasher )(_ec .SignatureValidationResult ,error ){_cc :=sig .Contents .Bytes ();_ff ,_dbd :=_d .Parse (_cc );if _dbd !=nil {return _ec .SignatureValidationResult {},_dbd ;};_ca :=digest .(*_b .Buffer );_ff .Content =_ca .Bytes ();if _dbd =_ff .Verify ();_dbd !=nil {return _ec .SignatureValidationResult {},_dbd ;};return _ec .SignatureValidationResult {IsSigned :true ,IsVerified :true },nil ;};
|
||||
// InitSignature initialises the PdfSignature.
|
||||
func (_bg *adobeX509RSASHA1 )InitSignature (sig *_da .PdfSignature )error {if _bg ._ag ==nil {return _g .New ("c\u0065\u0072\u0074\u0069\u0066\u0069c\u0061\u0074\u0065\u0020\u006d\u0075\u0073\u0074\u0020n\u006f\u0074\u0020b\u0065 \u006e\u0069\u006c");};if _bg ._aac ==nil &&_bg ._fdf ==nil {return _g .New ("\u006d\u0075\u0073\u0074\u0020\u0070\u0072o\u0076\u0069\u0064e\u0020\u0065\u0069t\u0068\u0065r\u0020\u0061\u0020\u0070\u0072\u0069v\u0061te\u0020\u006b\u0065\u0079\u0020\u006f\u0072\u0020\u0061\u0020\u0073\u0069\u0067\u006e\u0069\u006e\u0067\u0020\u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e");};_ee :=*_bg ;sig .Handler =&_ee ;sig .Filter =_ba .MakeName ("\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065");sig .SubFilter =_ba .MakeName ("\u0061d\u0062e\u002e\u0078\u0035\u0030\u0039.\u0072\u0073a\u005f\u0073\u0068\u0061\u0031");sig .Cert =_ba .MakeString (string (_ee ._ag .Raw ));sig .Reference =nil ;_gcd ,_egc :=_ee .NewDigest (sig );if _egc !=nil {return _egc ;};_gcd .Write ([]byte ("\u0063\u0061\u006c\u0063\u0075\u006ca\u0074\u0065\u0020\u0074\u0068\u0065\u0020\u0043\u006f\u006e\u0074\u0065\u006et\u0073\u0020\u0066\u0069\u0065\u006c\u0064 \u0073\u0069\u007a\u0065"));return _ee .Sign (sig ,_gcd );};
|
||||
|
||||
// NewAdobeX509RSASHA1 creates a new Adobe.PPKMS/Adobe.PPKLite adbe.x509.rsa_sha1 signature handler.
|
||||
// Both parameters may be nil for the signature validation.
|
||||
func NewAdobeX509RSASHA1 (privateKey *_ae .PrivateKey ,certificate *_gc .Certificate )(_da .SignatureHandler ,error ){return &adobeX509RSASHA1 {_ag :certificate ,_aac :privateKey },nil ;};
|
||||
|
||||
// NewDigest creates a new digest.
|
||||
func (_baa *adobePKCS7Detached )NewDigest (sig *_ec .PdfSignature )(_ec .Hasher ,error ){return _b .NewBuffer (nil ),nil ;};
|
||||
func (_ec *docTimeStamp )NewDigest (sig *_da .PdfSignature )(_da .Hasher ,error ){return _f .NewBuffer (nil ),nil ;};
|
||||
|
||||
// InitSignature initialises the PdfSignature.
|
||||
func (_fccd *adobeX509RSASHA1 )InitSignature (sig *_ec .PdfSignature )error {if _fccd ._cbf ==nil {return _gc .New ("c\u0065\u0072\u0074\u0069\u0066\u0069c\u0061\u0074\u0065\u0020\u006d\u0075\u0073\u0074\u0020n\u006f\u0074\u0020b\u0065 \u006e\u0069\u006c");};if _fccd ._ccd ==nil &&_fccd ._gf ==nil {return _gc .New ("\u006d\u0075\u0073\u0074\u0020\u0070\u0072o\u0076\u0069\u0064e\u0020\u0065\u0069t\u0068\u0065r\u0020\u0061\u0020\u0070\u0072\u0069v\u0061te\u0020\u006b\u0065\u0079\u0020\u006f\u0072\u0020\u0061\u0020\u0073\u0069\u0067\u006e\u0069\u006e\u0067\u0020\u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e");};_eb :=*_fccd ;sig .Handler =&_eb ;sig .Filter =_a .MakeName ("\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065");sig .SubFilter =_a .MakeName ("\u0061d\u0062e\u002e\u0078\u0035\u0030\u0039.\u0072\u0073a\u005f\u0073\u0068\u0061\u0031");sig .Cert =_a .MakeString (string (_eb ._cbf .Raw ));sig .Reference =nil ;_ed ,_dbe :=_eb .NewDigest (sig );if _dbe !=nil {return _dbe ;};_ed .Write ([]byte ("\u0063\u0061\u006c\u0063\u0075\u006ca\u0074\u0065\u0020\u0074\u0068\u0065\u0020\u0043\u006f\u006e\u0074\u0065\u006et\u0073\u0020\u0066\u0069\u0065\u006c\u0064 \u0073\u0069\u007a\u0065"));return _eb .Sign (sig ,_ed );};
|
||||
// SignFunc represents a custom signing function. The function should return
|
||||
// the computed signature.
|
||||
type SignFunc func (_acd *_da .PdfSignature ,_df _da .Hasher )([]byte ,error );func (_cbd *adobeX509RSASHA1 )getCertificate (_ad *_da .PdfSignature )(*_gc .Certificate ,error ){if _cbd ._ag !=nil {return _cbd ._ag ,nil ;};var _baa []byte ;switch _bbd :=_ad .Cert .(type ){case *_ba .PdfObjectString :_baa =_bbd .Bytes ();case *_ba .PdfObjectArray :if _bbd .Len ()==0{return nil ,_g .New ("\u006e\u006f\u0020s\u0069\u0067\u006e\u0061t\u0075\u0072\u0065\u0020\u0063\u0065\u0072t\u0069\u0066\u0069\u0063\u0061\u0074\u0065\u0073\u0020\u0066\u006f\u0075\u006e\u0064");};for _ ,_cdd :=range _bbd .Elements (){_cde ,_fbaa :=_ba .GetString (_cdd );if !_fbaa {return nil ,_dg .Errorf ("\u0069\u006ev\u0061\u006c\u0069\u0064\u0020\u0063\u0065\u0072\u0074\u0069\u0066\u0069\u0063\u0061\u0074\u0065\u0020\u006f\u0062j\u0065\u0063\u0074\u0020\u0074\u0079p\u0065\u0020\u0069\u006e\u0020\u0073\u0069\u0067\u006e\u0061\u0074\u0075\u0072\u0065 \u0063\u0065r\u0074\u0069\u0066\u0069c\u0061\u0074\u0065\u0020\u0063h\u0061\u0069\u006e\u003a\u0020\u0025\u0054",_cdd );};_baa =append (_baa ,_cde .Bytes ()...);};default:return nil ,_dg .Errorf ("\u0069n\u0076\u0061l\u0069\u0064\u0020s\u0069\u0067\u006e\u0061\u0074\u0075\u0072e\u0020\u0063\u0065\u0072\u0074\u0069f\u0069\u0063\u0061\u0074\u0065\u0020\u006f\u0062\u006a\u0065\u0063t\u0020\u0074\u0079\u0070\u0065\u003a\u0020\u0025\u0054",_bbd );};_fdg ,_ga :=_gc .ParseCertificates (_baa );if _ga !=nil {return nil ,_ga ;};return _fdg [0],nil ;};
|
||||
|
||||
// Validate validates PdfSignature.
|
||||
func (_fff *adobeX509RSASHA1 )Validate (sig *_ec .PdfSignature ,digest _ec .Hasher )(_ec .SignatureValidationResult ,error ){_aa ,_bab :=_fff .getCertificate (sig );if _bab !=nil {return _ec .SignatureValidationResult {},_bab ;};_da :=sig .Contents .Bytes ();var _af []byte ;if _ ,_ebc :=_gg .Unmarshal (_da ,&_af );_ebc !=nil {return _ec .SignatureValidationResult {},_ebc ;};_dgg ,_ccc :=digest .(_fc .Hash );if !_ccc {return _ec .SignatureValidationResult {},_gc .New ("\u0068a\u0073h\u0020\u0074\u0079\u0070\u0065\u0020\u0065\u0072\u0072\u006f\u0072");};_bg ,_ :=_cfd (_aa .SignatureAlgorithm );if _fbf :=_fbe .VerifyPKCS1v15 (_aa .PublicKey .(*_fbe .PublicKey ),_bg ,_dgg .Sum (nil ),_af );_fbf !=nil {return _ec .SignatureValidationResult {},_fbf ;};return _ec .SignatureValidationResult {IsSigned :true ,IsVerified :true },nil ;};func _gdg (_geg _gg .ObjectIdentifier )(_ce .Hash ,error ){switch {case _geg .Equal (_d .OIDDigestAlgorithmSHA1 ),_geg .Equal (_d .OIDDigestAlgorithmECDSASHA1 ),_geg .Equal (_d .OIDDigestAlgorithmDSA ),_geg .Equal (_d .OIDDigestAlgorithmDSASHA1 ),_geg .Equal (_d .OIDEncryptionAlgorithmRSA ):return _ce .SHA1 ,nil ;case _geg .Equal (_d .OIDDigestAlgorithmSHA256 ),_geg .Equal (_d .OIDDigestAlgorithmECDSASHA256 ):return _ce .SHA256 ,nil ;case _geg .Equal (_d .OIDDigestAlgorithmSHA384 ),_geg .Equal (_d .OIDDigestAlgorithmECDSASHA384 ):return _ce .SHA384 ,nil ;case _geg .Equal (_d .OIDDigestAlgorithmSHA512 ),_geg .Equal (_d .OIDDigestAlgorithmECDSASHA512 ):return _ce .SHA512 ,nil ;};return _ce .Hash (0),_d .ErrUnsupportedAlgorithm ;};
|
||||
// NewAdobeX509RSASHA1Custom creates a new Adobe.PPKMS/Adobe.PPKLite adbe.x509.rsa_sha1 signature handler
|
||||
// with a custom signing function. Both parameters may be nil for the signature validation.
|
||||
func NewAdobeX509RSASHA1Custom (certificate *_gc .Certificate ,signFunc SignFunc )(_da .SignatureHandler ,error ){return &adobeX509RSASHA1 {_ag :certificate ,_fdf :signFunc },nil ;};type timestampInfo struct{Version int ;Policy _b .RawValue ;MessageImprint struct{HashAlgorithm _dc .AlgorithmIdentifier ;HashedMessage []byte ;};SerialNumber _b .RawValue ;GeneralizedTime _d .Time ;};
|
||||
|
||||
// Sign sets the Contents fields for the PdfSignature.
|
||||
func (_ffc *docTimeStamp )Sign (sig *_ec .PdfSignature ,digest _ec .Hasher )error {_fgc :=digest .(*_b .Buffer );_agb :=_ffc ._eff .New ();if _ ,_aee :=_g .Copy (_agb ,_fgc );_aee !=nil {return _aee ;};_dae :=_agb .Sum (nil );_fbc :=_fg .Request {HashAlgorithm :_ffc ._eff ,HashedMessage :_dae ,Certificates :true ,Extensions :nil ,ExtraExtensions :nil };_cega ,_caf :=_fbc .Marshal ();if _caf !=nil {return _caf ;};_daa ,_caf :=_fb .Post (_ffc ._dcf ,"a\u0070\u0070\u006c\u0069\u0063\u0061t\u0069\u006f\u006e\u002f\u0074\u0069\u006d\u0065\u0073t\u0061\u006d\u0070-\u0071u\u0065\u0072\u0079",_b .NewBuffer (_cega ));if _caf !=nil {return _caf ;};defer _daa .Body .Close ();_gfdf ,_caf :=_e .ReadAll (_daa .Body );if _caf !=nil {return _caf ;};if _daa .StatusCode !=_fb .StatusOK {return _fe .Errorf ("\u0068\u0074\u0074\u0070\u0020\u0073\u0074\u0061\u0074\u0075\u0073\u0020\u0063\u006f\u0064e\u0020n\u006f\u0074\u0020\u006f\u006b\u0020\u0028\u0067\u006f\u0074\u0020\u0025\u0064\u0029",_daa .StatusCode );};var _ad struct{Version _gg .RawValue ;Content _gg .RawValue ;};_ ,_caf =_gg .Unmarshal (_gfdf ,&_ad );if _caf !=nil {return _caf ;};sig .Contents =_a .MakeHexString (string (_ad .Content .FullBytes ));return nil ;};
|
||||
func (_caf *adobeX509RSASHA1 )Sign (sig *_da .PdfSignature ,digest _da .Hasher )error {var _fbae []byte ;var _dbf error ;if _caf ._fdf !=nil {_fbae ,_dbf =_caf ._fdf (sig ,digest );if _dbf !=nil {return _dbf ;};}else {_eee ,_fge :=digest .(_de .Hash );if !_fge {return _g .New ("\u0068a\u0073h\u0020\u0074\u0079\u0070\u0065\u0020\u0065\u0072\u0072\u006f\u0072");};_cg ,_ :=_eec (_caf ._ag .SignatureAlgorithm );_fbae ,_dbf =_ae .SignPKCS1v15 (_gf .Reader ,_caf ._aac ,_cg ,_eee .Sum (nil ));if _dbf !=nil {return _dbf ;};};_fbae ,_dbf =_b .Marshal (_fbae );if _dbf !=nil {return _dbf ;};sig .Contents =_ba .MakeHexString (string (_fbae ));return nil ;};
|
||||
|
||||
// Validate validates PdfSignature.
|
||||
func (_bgb *docTimeStamp )Validate (sig *_da .PdfSignature ,digest _da .Hasher )(_da .SignatureValidationResult ,error ){_cee :=sig .Contents .Bytes ();_aga ,_cae :=_cd .Parse (_cee );if _cae !=nil {return _da .SignatureValidationResult {},_cae ;};if _cae =_aga .Verify ();_cae !=nil {return _da .SignatureValidationResult {},_cae ;};var _bge timestampInfo ;_ ,_cae =_b .Unmarshal (_aga .Content ,&_bge );if _cae !=nil {return _da .SignatureValidationResult {},_cae ;};_cbe ,_cae :=_bfa (_bge .MessageImprint .HashAlgorithm .Algorithm );if _cae !=nil {return _da .SignatureValidationResult {},_cae ;};_dfc :=_cbe .New ();_gg :=digest .(*_f .Buffer );_dfc .Write (_gg .Bytes ());_egcc :=_dfc .Sum (nil );_ade :=_da .SignatureValidationResult {IsSigned :true ,IsVerified :_f .Equal (_egcc ,_bge .MessageImprint .HashedMessage ),GeneralizedTime :_bge .GeneralizedTime };return _ade ,nil ;};
|
||||
|
||||
// IsApplicable returns true if the signature handler is applicable for the PdfSignature.
|
||||
func (_cdc *adobeX509RSASHA1 )IsApplicable (sig *_da .PdfSignature )bool {if sig ==nil ||sig .Filter ==nil ||sig .SubFilter ==nil {return false ;};return (*sig .Filter =="A\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004d\u0053"||*sig .Filter =="\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065")&&*sig .SubFilter =="\u0061d\u0062e\u002e\u0078\u0035\u0030\u0039.\u0072\u0073a\u005f\u0073\u0068\u0061\u0031";};
|
||||
|
||||
// IsApplicable returns true if the signature handler is applicable for the PdfSignature.
|
||||
func (_ged *docTimeStamp )IsApplicable (sig *_da .PdfSignature )bool {if sig ==nil ||sig .Filter ==nil ||sig .SubFilter ==nil {return false ;};return (*sig .Filter =="A\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004d\u0053"||*sig .Filter =="\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065")&&*sig .SubFilter =="\u0045\u0054\u0053I\u002e\u0052\u0046\u0043\u0033\u0031\u0036\u0031";};
|
||||
|
||||
// DocTimeStampOpts defines options for configuring the timestamp handler.
|
||||
type DocTimeStampOpts struct{
|
||||
|
||||
// SignatureSize is the estimated size of the signature contents in bytes.
|
||||
// If not provided, a default signature size of 4192 is used.
|
||||
// The signing process will report the model.ErrSignNotEnoughSpace error
|
||||
// if the estimated signature size is smaller than the actual size of the
|
||||
// signature.
|
||||
SignatureSize int ;};
|
||||
|
||||
// InitSignature initialises the PdfSignature.
|
||||
func (_ba *adobePKCS7Detached )InitSignature (sig *_ec .PdfSignature )error {if !_ba ._fcc {if _ba ._df ==nil {return _gc .New ("c\u0065\u0072\u0074\u0069\u0066\u0069c\u0061\u0074\u0065\u0020\u006d\u0075\u0073\u0074\u0020n\u006f\u0074\u0020b\u0065 \u006e\u0069\u006c");};if _ba ._ac ==nil {return _gc .New ("\u0070\u0072\u0069\u0076\u0061\u0074\u0065\u004b\u0065\u0079\u0020m\u0075\u0073\u0074\u0020\u006e\u006f\u0074\u0020\u0062\u0065 \u006e\u0069\u006c");};};_cf :=*_ba ;sig .Handler =&_cf ;sig .Filter =_a .MakeName ("\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065");sig .SubFilter =_a .MakeName ("\u0061\u0064\u0062\u0065.p\u006b\u0063\u0073\u0037\u002e\u0064\u0065\u0074\u0061\u0063\u0068\u0065\u0064");sig .Reference =nil ;_cfc ,_fbed :=_cf .NewDigest (sig );if _fbed !=nil {return _fbed ;};_cfc .Write ([]byte ("\u0063\u0061\u006c\u0063\u0075\u006ca\u0074\u0065\u0020\u0074\u0068\u0065\u0020\u0043\u006f\u006e\u0074\u0065\u006et\u0073\u0020\u0066\u0069\u0065\u006c\u0064 \u0073\u0069\u007a\u0065"));return _cf .Sign (sig ,_cfc );};
|
||||
func (_bc *docTimeStamp )InitSignature (sig *_da .PdfSignature )error {_eb :=*_bc ;sig .Handler =&_eb ;sig .Filter =_ba .MakeName ("\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065");sig .SubFilter =_ba .MakeName ("\u0045\u0054\u0053I\u002e\u0052\u0046\u0043\u0033\u0031\u0036\u0031");sig .Reference =nil ;if _bc ._bgg > 0{sig .Contents =_ba .MakeHexString (string (make ([]byte ,_bc ._bgg )));}else {_dea ,_bfd :=_bc .NewDigest (sig );if _bfd !=nil {return _bfd ;};_dea .Write ([]byte ("\u0063\u0061\u006c\u0063\u0075\u006ca\u0074\u0065\u0020\u0074\u0068\u0065\u0020\u0043\u006f\u006e\u0074\u0065\u006et\u0073\u0020\u0066\u0069\u0065\u006c\u0064 \u0073\u0069\u007a\u0065"));if _bfd =_eb .Sign (sig ,_dea );_bfd !=nil {return _bfd ;};_bc ._bgg =_eb ._bgg ;};return nil ;};
|
||||
|
||||
// Sign sets the Contents fields for the PdfSignature.
|
||||
func (_dfd *docTimeStamp )Sign (sig *_da .PdfSignature ,digest _da .Hasher )error {_bfac :=digest .(*_f .Buffer );_afda :=_dfd ._dece .New ();if _ ,_dbb :=_c .Copy (_afda ,_bfac );_dbb !=nil {return _dbb ;};_bdg :=_gca .Request {HashAlgorithm :_dfd ._dece ,HashedMessage :_afda .Sum (nil ),Certificates :true ,Extensions :nil ,ExtraExtensions :nil };_ffc ,_aba :=_bdg .Marshal ();if _aba !=nil {return _aba ;};_bcc ,_aba :=_eg .Post (_dfd ._dbe ,"a\u0070\u0070\u006c\u0069\u0063\u0061t\u0069\u006f\u006e\u002f\u0074\u0069\u006d\u0065\u0073t\u0061\u006d\u0070-\u0071u\u0065\u0072\u0079",_f .NewBuffer (_ffc ));if _aba !=nil {return _aba ;};defer _bcc .Body .Close ();_agd ,_aba :=_a .ReadAll (_bcc .Body );if _aba !=nil {return _aba ;};if _bcc .StatusCode !=_eg .StatusOK {return _dg .Errorf ("\u0068\u0074\u0074\u0070\u0020\u0073\u0074\u0061\u0074\u0075\u0073\u0020\u0063\u006f\u0064e\u0020n\u006f\u0074\u0020\u006f\u006b\u0020\u0028\u0067\u006f\u0074\u0020\u0025\u0064\u0029",_bcc .StatusCode );};var _aab struct{Version _b .RawValue ;Content _b .RawValue ;};if _ ,_aba =_b .Unmarshal (_agd ,&_aab );_aba !=nil {return _aba ;};_ecc :=len (_aab .Content .FullBytes );if _dfd ._bgg > 0&&_ecc > _dfd ._bgg {return _da .ErrSignNotEnoughSpace ;};if _ecc > 0{_dfd ._bgg =_ecc +128;};sig .Contents =_ba .MakeHexString (string (_aab .Content .FullBytes ));return nil ;};func (_ed *docTimeStamp )getCertificate (_fef *_da .PdfSignature )(*_gc .Certificate ,error ){var _cba []byte ;switch _dag :=_fef .Cert .(type ){case *_ba .PdfObjectString :_cba =_dag .Bytes ();case *_ba .PdfObjectArray :if _dag .Len ()==0{return nil ,_g .New ("\u006e\u006f\u0020s\u0069\u0067\u006e\u0061t\u0075\u0072\u0065\u0020\u0063\u0065\u0072t\u0069\u0066\u0069\u0063\u0061\u0074\u0065\u0073\u0020\u0066\u006f\u0075\u006e\u0064");};for _ ,_dd :=range _dag .Elements (){_fgc ,_dda :=_ba .GetString (_dd );if !_dda {return nil ,_dg .Errorf ("\u0069\u006ev\u0061\u006c\u0069\u0064\u0020\u0063\u0065\u0072\u0074\u0069\u0066\u0069\u0063\u0061\u0074\u0065\u0020\u006f\u0062j\u0065\u0063\u0074\u0020\u0074\u0079p\u0065\u0020\u0069\u006e\u0020\u0073\u0069\u0067\u006e\u0061\u0074\u0075\u0072\u0065 \u0063\u0065r\u0074\u0069\u0066\u0069c\u0061\u0074\u0065\u0020\u0063h\u0061\u0069\u006e\u003a\u0020\u0025\u0054",_dd );};_cba =append (_cba ,_fgc .Bytes ()...);};default:return nil ,_dg .Errorf ("\u0069n\u0076\u0061l\u0069\u0064\u0020s\u0069\u0067\u006e\u0061\u0074\u0075\u0072e\u0020\u0063\u0065\u0072\u0074\u0069f\u0069\u0063\u0061\u0074\u0065\u0020\u006f\u0062\u006a\u0065\u0063t\u0020\u0074\u0079\u0070\u0065\u003a\u0020\u0025\u0054",_dag );};_fdd ,_def :=_gc .ParseCertificates (_cba );if _def !=nil {return nil ,_def ;};return _fdd [0],nil ;};
|
||||
|
||||
// NewDigest creates a new digest.
|
||||
func (_cb *adobePKCS7Detached )NewDigest (sig *_da .PdfSignature )(_da .Hasher ,error ){return _f .NewBuffer (nil ),nil ;};
|
||||
|
||||
// NewDocTimeStampWithOpts returns a new DocTimeStamp configured using the
|
||||
// specified options. If no options are provided, default options will be used.
|
||||
// Both the timestamp server URL and the hash algorithm can be empty for the
|
||||
// signature validation.
|
||||
// The following hash algorithms are supported:
|
||||
// crypto.SHA1, crypto.SHA256, crypto.SHA384, crypto.SHA512.
|
||||
func NewDocTimeStampWithOpts (timestampServerURL string ,hashAlgorithm _ff .Hash ,opts *DocTimeStampOpts )(_da .SignatureHandler ,error ){if opts ==nil {opts =&DocTimeStampOpts {};};if opts .SignatureSize <=0{opts .SignatureSize =4192;};return &docTimeStamp {_dbe :timestampServerURL ,_dece :hashAlgorithm ,_bgg :opts .SignatureSize },nil ;};
|
||||
|
||||
// Sign sets the Contents fields.
|
||||
func (_bb *adobePKCS7Detached )Sign (sig *_da .PdfSignature ,digest _da .Hasher )error {if _bb ._ca {_bde :=_bb ._dgb ;if _bde <=0{_bde =8192;};sig .Contents =_ba .MakeHexString (string (make ([]byte ,_bde )));return nil ;};_ge :=digest .(*_f .Buffer );_gdd ,_afd :=_cd .NewSignedData (_ge .Bytes ());if _afd !=nil {return _afd ;};if _fba :=_gdd .AddSigner (_bb ._fd ,_bb ._fg ,_cd .SignerInfoConfig {});_fba !=nil {return _fba ;};_gdd .Detach ();_ac ,_afd :=_gdd .Finish ();if _afd !=nil {return _afd ;};_cbg :=make ([]byte ,8192);copy (_cbg ,_ac );sig .Contents =_ba .MakeHexString (string (_cbg ));return nil ;};type docTimeStamp struct{_dbe string ;_dece _ff .Hash ;_bgg int ;};type adobeX509RSASHA1 struct{_aac *_ae .PrivateKey ;_ag *_gc .Certificate ;_fdf SignFunc ;};
|
||||
|
||||
// Validate validates PdfSignature.
|
||||
func (_fe *adobeX509RSASHA1 )Validate (sig *_da .PdfSignature ,digest _da .Hasher )(_da .SignatureValidationResult ,error ){_fc ,_bee :=_fe .getCertificate (sig );if _bee !=nil {return _da .SignatureValidationResult {},_bee ;};_cff :=sig .Contents .Bytes ();var _bgc []byte ;if _ ,_efc :=_b .Unmarshal (_cff ,&_bgc );_efc !=nil {return _da .SignatureValidationResult {},_efc ;};_cda ,_aef :=digest .(_de .Hash );if !_aef {return _da .SignatureValidationResult {},_g .New ("\u0068a\u0073h\u0020\u0074\u0079\u0070\u0065\u0020\u0065\u0072\u0072\u006f\u0072");};_dfe ,_ :=_eec (_fc .SignatureAlgorithm );if _gfa :=_ae .VerifyPKCS1v15 (_fc .PublicKey .(*_ae .PublicKey ),_dfe ,_cda .Sum (nil ),_bgc );_gfa !=nil {return _da .SignatureValidationResult {},_gfa ;};return _da .SignatureValidationResult {IsSigned :true ,IsVerified :true },nil ;};
|
||||
|
||||
// NewAdobePKCS7Detached creates a new Adobe.PPKMS/Adobe.PPKLite adbe.pkcs7.detached signature handler.
|
||||
// Both parameters may be nil for the signature validation.
|
||||
func NewAdobePKCS7Detached (privateKey *_ae .PrivateKey ,certificate *_gc .Certificate )(_da .SignatureHandler ,error ){return &adobePKCS7Detached {_fd :certificate ,_fg :privateKey },nil ;};
|
@ -9,11 +9,19 @@
|
||||
// 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 pdfutil ;import (_a "github.com/unidoc/unipdf/v3/common";_b "github.com/unidoc/unipdf/v3/contentstream";_ge "github.com/unidoc/unipdf/v3/contentstream/draw";_g "github.com/unidoc/unipdf/v3/model";);
|
||||
package pdfutil ;import (_bb "github.com/unidoc/unipdf/v3/common";_e "github.com/unidoc/unipdf/v3/contentstream";_c "github.com/unidoc/unipdf/v3/contentstream/draw";_d "github.com/unidoc/unipdf/v3/model";);
|
||||
|
||||
// NormalizePage rotates the contents of the passed in page according to its
|
||||
// Rotate entry (i.e. flattens the rotation). If rotation is applied, the
|
||||
// Rotate entry of the page is set to nil.
|
||||
// NormalizePage performs the following operations on the passed in page:
|
||||
// - Normalize the page rotation.
|
||||
// Rotates the contents of the page according to the Rotate entry, thus
|
||||
// flattening the rotation. The Rotate entry of the page is set to nil.
|
||||
// - Normalize the media box.
|
||||
// If the media box of the page is offsetted (Llx != 0 or Lly != 0),
|
||||
// the contents of the page are translated to (-Llx, -Lly). After
|
||||
// normalization, the media box is updated (Llx and Lly are set to 0 and
|
||||
// Urx and Ury are updated accordingly).
|
||||
// - Normalize the crop box.
|
||||
// The crop box of the page is updated based on the previous operations.
|
||||
// After normalization, the page should look the same if openend using a
|
||||
// PDF viewer.
|
||||
func NormalizePage (page *_g .PdfPage )error {if _ae :=page .Rotate ;_ae ==nil ||*_ae ==0||*_ae %90!=0{return nil ;};_gg ,_f :=page .GetMediaBox ();if _f !=nil {return _f ;};_e ,_f :=page .GetContentStreams ();if _f !=nil {return _f ;};_gc ,_ff :=_gg .Width (),_gg .Height ();_ag :=-float64 (*page .Rotate );_cc :=_ge .Path {Points :[]_ge .Point {_ge .NewPoint (0,0).Rotate (_ag ),_ge .NewPoint (_gc ,0).Rotate (_ag ),_ge .NewPoint (0,_ff ).Rotate (_ag ),_ge .NewPoint (_gc ,_ff ).Rotate (_ag )}}.GetBoundingBox ();_bb :=-_gg .Llx +(_cc .Width -_gc )/2+_gc /2;_ef :=_gg .Lly +(_cc .Height -_ff )/2+_ff /2;_d :=_b .NewContentCreator ();_d .Translate (_bb ,_ef );_d .RotateDeg (_ag );_d .Translate (-_gc /2,-_ff /2);_fg :=_d .Operations ().String ();*_gg =_g .PdfRectangle {Urx :_cc .Width ,Ury :_cc .Height };_e =append ([]string {_fg },_e ...);if _f =page .SetContentStreams (_e ,nil );_f !=nil {return _f ;};_a .Log .Debug ("\u0052o\u0074\u0061\u0074\u0065\u003d\u0025\u0066\u00b0\u0020\u0025\u0071 \u006d\u0062\u006f\u0078\u003d\u0025\u002e\u0032\u0066",_ag ,_fg ,*_gg );page .Rotate =nil ;return nil ;};
|
||||
func NormalizePage (page *_d .PdfPage )error {_df ,_a :=page .GetMediaBox ();if _a !=nil {return _a ;};_f ,_a :=page .GetContentStreams ();if _a !=nil {return _a ;};_ec :=page .Rotate ;_g :=_ec !=nil &&*_ec !=0&&*_ec %90==0;_bba ,_ag ,_bd ,_bbe :=_df .Llx ,_df .Lly ,_df .Width (),_df .Height ();_cc :=_bba !=0||_ag !=0;if !_g &&!_cc {return nil ;};_ca :=func (_ac ,_dd ,_be float64 )_c .BoundingBox {return _c .Path {Points :[]_c .Point {_c .NewPoint (0,0).Rotate (_be ),_c .NewPoint (_ac ,0).Rotate (_be ),_c .NewPoint (0,_dd ).Rotate (_be ),_c .NewPoint (_ac ,_dd ).Rotate (_be )}}.GetBoundingBox ();};_ee :=_e .NewContentCreator ();var _eee float64 ;if _g {_eee =-float64 (*page .Rotate );_de :=_ca (_bd ,_bbe ,_eee );_ee .Translate ((_de .Width -_bd )/2+_bd /2,(_de .Height -_bbe )/2+_bbe /2);_ee .RotateDeg (_eee );_ee .Translate (-_bd /2,-_bbe /2);_bd ,_bbe =_de .Width ,_de .Height ;};if _cc {_ee .Translate (-_bba ,-_ag );};_ge :=_ee .Operations ().String ();_f =append ([]string {_ge },_f ...);if _a =page .SetContentStreams (_f ,nil );_a !=nil {return _a ;};*_df =_d .PdfRectangle {Urx :_bd ,Ury :_bbe };if _bc :=page .CropBox ;_bc !=nil {_eca ,_ddf ,_gb ,_ab :=_bc .Llx -_bba ,_bc .Lly -_ag ,_bc .Width (),_bc .Height ();if _g {_ea :=_ca (_gb ,_ab ,_eee );_gb ,_ab =_ea .Width ,_ea .Height ;};*_bc =_d .PdfRectangle {Llx :_eca ,Lly :_ddf ,Urx :_eca +_gb ,Ury :_ddf +_ab };};_bb .Log .Debug ("\u0052\u006f\u0074\u0061\u0074\u0065\u003d\u0025\u0066\u00b0\u0020\u004f\u0070\u0073\u003d%\u0071 \u004d\u0065\u0064\u0069\u0061\u0042\u006f\u0078\u003d\u0025\u002e\u0032\u0066",_eee ,_ge ,_df );page .Rotate =nil ;return nil ;};
|
146
ps/ps.go
146
ps/ps.go
@ -14,35 +14,86 @@
|
||||
//
|
||||
// Package ps implements various functionalities needed for handling Postscript for PDF uses, in particular
|
||||
// for PDF function type 4.
|
||||
package ps ;import (_df "bufio";_c "bytes";_d "errors";_e "fmt";_dfe "github.com/unidoc/unipdf/v3/common";_cd "github.com/unidoc/unipdf/v3/core";_b "io";_bb "math";);
|
||||
package ps ;import (_dg "bufio";_ea "bytes";_b "errors";_c "fmt";_bb "github.com/unidoc/unipdf/v3/common";_a "github.com/unidoc/unipdf/v3/core";_d "io";_be "math";);func (_ggaa *PSOperand )round (_dbgg *PSStack )error {_edde ,_ddfe :=_dbgg .Pop ();if _ddfe !=nil {return _ddfe ;};if _eebf ,_gcc :=_edde .(*PSReal );_gcc {_ddfe =_dbgg .Push (MakeReal (_be .Floor (_eebf .Val +0.5)));}else if _bgfa ,_bdggf :=_edde .(*PSInteger );_bdggf {_ddfe =_dbgg .Push (MakeInteger (_bgfa .Val ));}else {return ErrTypeCheck ;};return _ddfe ;};
|
||||
|
||||
// NewPSParser returns a new instance of the PDF Postscript parser from input data.
|
||||
func NewPSParser (content []byte )*PSParser {_facc :=PSParser {};_acc :=_c .NewBuffer (content );_facc ._acfg =_df .NewReader (_acc );return &_facc ;};var ErrTypeCheck =_d .New ("\u0074\u0079p\u0065\u0020\u0063h\u0065\u0063\u006b\u0020\u0065\u0072\u0072\u006f\u0072");
|
||||
// Push pushes an object on top of the stack.
|
||||
func (_ecae *PSStack )Push (obj PSObject )error {if len (*_ecae )> 100{return ErrStackOverflow ;};*_ecae =append (*_ecae ,obj );return nil ;};func (_bfd *PSOperand )bitshift (_bgb *PSStack )error {_bac ,_bfgb :=_bgb .PopInteger ();if _bfgb !=nil {return _bfgb ;};_cbc ,_bfgb :=_bgb .PopInteger ();if _bfgb !=nil {return _bfgb ;};var _deg int ;if _bac >=0{_deg =_cbc <<uint (_bac );}else {_deg =_cbc >>uint (-_bac );};_bfgb =_bgb .Push (MakeInteger (_deg ));return _bfgb ;};func (_ggb *PSOperand )sin (_bcaf *PSStack )error {_cabc ,_ebb :=_bcaf .PopNumberAsFloat64 ();if _ebb !=nil {return _ebb ;};_dda :=_be .Sin (_cabc *_be .Pi /180.0);_ebb =_bcaf .Push (MakeReal (_dda ));return _ebb ;};func (_dbac *PSOperand )cos (_geb *PSStack )error {_gcf ,_gea :=_geb .PopNumberAsFloat64 ();if _gea !=nil {return _gea ;};_ccg :=_be .Cos (_gcf *_be .Pi /180.0);_gea =_geb .Push (MakeReal (_ccg ));return _gea ;};func (_eaaa *PSOperand )idiv (_ggff *PSStack )error {_bed ,_adfd :=_ggff .Pop ();if _adfd !=nil {return _adfd ;};_ecd ,_adfd :=_ggff .Pop ();if _adfd !=nil {return _adfd ;};_dgg ,_beea :=_bed .(*PSInteger );if !_beea {return ErrTypeCheck ;};if _dgg .Val ==0{return ErrUndefinedResult ;};_addb ,_beea :=_ecd .(*PSInteger );if !_beea {return ErrTypeCheck ;};_agca :=_addb .Val /_dgg .Val ;_adfd =_ggff .Push (MakeInteger (_agca ));return _adfd ;};func (_ccgg *PSOperand )ln (_fgea *PSStack )error {_aab ,_ddbg :=_fgea .PopNumberAsFloat64 ();if _ddbg !=nil {return _ddbg ;};_agcdee :=_be .Log (_aab );_ddbg =_fgea .Push (MakeReal (_agcdee ));return _ddbg ;};
|
||||
|
||||
// Pop pops an object from the top of the stack.
|
||||
func (_bcg *PSStack )Pop ()(PSObject ,error ){if len (*_bcg )< 1{return nil ,ErrStackUnderflow ;};_bdc :=(*_bcg )[len (*_bcg )-1];*_bcg =(*_bcg )[0:len (*_bcg )-1];return _bdc ,nil ;};func (_dba *PSProgram )String ()string {_ec :="\u007b\u0020";for _ ,_ag :=range *_dba {_ec +=_ag .String ();_ec +="\u0020";};_ec +="\u007d";return _ec ;};var ErrStackUnderflow =_d .New ("\u0073t\u0061c\u006b\u0020\u0075\u006e\u0064\u0065\u0072\u0066\u006c\u006f\u0077");
|
||||
// Exec executes the program, typically leaving output values on the stack.
|
||||
func (_gb *PSProgram )Exec (stack *PSStack )error {for _ ,_dbc :=range *_gb {var _fef error ;switch _bbf :=_dbc .(type ){case *PSInteger :_cg :=_bbf ;_fef =stack .Push (_cg );case *PSReal :_af :=_bbf ;_fef =stack .Push (_af );case *PSBoolean :_cdb :=_bbf ;_fef =stack .Push (_cdb );case *PSProgram :_ae :=_bbf ;_fef =stack .Push (_ae );case *PSOperand :_cc :=_bbf ;_fef =_cc .Exec (stack );default:return ErrTypeCheck ;};if _fef !=nil {return _fef ;};};return nil ;};func (_cgd *PSOperand )Duplicate ()PSObject {_df :=*_cgd ;return &_df };
|
||||
|
||||
// PopNumberAsFloat64 pops and return the numeric value of the top of the stack as a float64.
|
||||
// Real or integer only.
|
||||
func (_cecd *PSStack )PopNumberAsFloat64 ()(float64 ,error ){_acgg ,_eeec :=_cecd .Pop ();if _eeec !=nil {return 0,_eeec ;};if _aggd ,_ddbe :=_acgg .(*PSReal );_ddbe {return _aggd .Val ,nil ;}else if _acec ,_bfeg :=_acgg .(*PSInteger );_bfeg {return float64 (_acec .Val ),nil ;}else {return 0,ErrTypeCheck ;};};func (_bf *PSReal )Duplicate ()PSObject {_g :=PSReal {};_g .Val =_bf .Val ;return &_g };func (_beeg *PSOperand )roll (_gde *PSStack )error {_ged ,_abg :=_gde .Pop ();if _abg !=nil {return _abg ;};_ecd ,_abg :=_gde .Pop ();if _abg !=nil {return _abg ;};_caec ,_fdcf :=_ged .(*PSInteger );if !_fdcf {return ErrTypeCheck ;};_ecggb ,_fdcf :=_ecd .(*PSInteger );if !_fdcf {return ErrTypeCheck ;};if _ecggb .Val < 0{return ErrRangeCheck ;};if _ecggb .Val ==0||_ecggb .Val ==1{return nil ;};if _ecggb .Val > len (*_gde ){return ErrStackUnderflow ;};for _dbgf :=0;_dbgf < _dfg (_caec .Val );_dbgf ++{var _edd []PSObject ;_edd =(*_gde )[len (*_gde )-(_ecggb .Val ):len (*_gde )];if _caec .Val > 0{_gaed :=_edd [len (_edd )-1];_edd =append ([]PSObject {_gaed },_edd [0:len (_edd )-1]...);}else {_gfd :=_edd [len (_edd )-_ecggb .Val ];_edd =append (_edd [1:],_gfd );};_fcf :=append ((*_gde )[0:len (*_gde )-_ecggb .Val ],_edd ...);_gde =&_fcf ;};return nil ;};
|
||||
// Append appends an object to the PSProgram.
|
||||
func (_fe *PSProgram )Append (obj PSObject ){*_fe =append (*_fe ,obj )};func (_da *PSInteger )Duplicate ()PSObject {_baa :=PSInteger {};_baa .Val =_da .Val ;return &_baa };func (_aae *PSOperand )DebugString ()string {return _c .Sprintf ("\u006fp\u003a\u0027\u0025\u0073\u0027",*_aae );};func (_dcc *PSOperand )copy (_dfd *PSStack )error {_cdd ,_eff :=_dfd .PopInteger ();if _eff !=nil {return _eff ;};if _cdd < 0{return ErrRangeCheck ;};if _cdd > len (*_dfd ){return ErrRangeCheck ;};*_dfd =append (*_dfd ,(*_dfd )[len (*_dfd )-_cdd :]...);return nil ;};
|
||||
|
||||
// Exec executes the operand `op` in the state specified by `stack`.
|
||||
func (_cc *PSOperand )Exec (stack *PSStack )error {_bbc :=ErrUnsupportedOperand ;switch *_cc {case "\u0061\u0062\u0073":_bbc =_cc .abs (stack );case "\u0061\u0064\u0064":_bbc =_cc .add (stack );case "\u0061\u006e\u0064":_bbc =_cc .and (stack );case "\u0061\u0074\u0061\u006e":_bbc =_cc .atan (stack );case "\u0062\u0069\u0074\u0073\u0068\u0069\u0066\u0074":_bbc =_cc .bitshift (stack );case "\u0063e\u0069\u006c\u0069\u006e\u0067":_bbc =_cc .ceiling (stack );case "\u0063\u006f\u0070\u0079":_bbc =_cc .copy (stack );case "\u0063\u006f\u0073":_bbc =_cc .cos (stack );case "\u0063\u0076\u0069":_bbc =_cc .cvi (stack );case "\u0063\u0076\u0072":_bbc =_cc .cvr (stack );case "\u0064\u0069\u0076":_bbc =_cc .div (stack );case "\u0064\u0075\u0070":_bbc =_cc .dup (stack );case "\u0065\u0071":_bbc =_cc .eq (stack );case "\u0065\u0078\u0063\u0068":_bbc =_cc .exch (stack );case "\u0065\u0078\u0070":_bbc =_cc .exp (stack );case "\u0066\u006c\u006fo\u0072":_bbc =_cc .floor (stack );case "\u0067\u0065":_bbc =_cc .ge (stack );case "\u0067\u0074":_bbc =_cc .gt (stack );case "\u0069\u0064\u0069\u0076":_bbc =_cc .idiv (stack );case "\u0069\u0066":_bbc =_cc .ifCondition (stack );case "\u0069\u0066\u0065\u006c\u0073\u0065":_bbc =_cc .ifelse (stack );case "\u0069\u006e\u0064e\u0078":_bbc =_cc .index (stack );case "\u006c\u0065":_bbc =_cc .le (stack );case "\u006c\u006f\u0067":_bbc =_cc .log (stack );case "\u006c\u006e":_bbc =_cc .ln (stack );case "\u006c\u0074":_bbc =_cc .lt (stack );case "\u006d\u006f\u0064":_bbc =_cc .mod (stack );case "\u006d\u0075\u006c":_bbc =_cc .mul (stack );case "\u006e\u0065":_bbc =_cc .ne (stack );case "\u006e\u0065\u0067":_bbc =_cc .neg (stack );case "\u006e\u006f\u0074":_bbc =_cc .not (stack );case "\u006f\u0072":_bbc =_cc .or (stack );case "\u0070\u006f\u0070":_bbc =_cc .pop (stack );case "\u0072\u006f\u0075n\u0064":_bbc =_cc .round (stack );case "\u0072\u006f\u006c\u006c":_bbc =_cc .roll (stack );case "\u0073\u0069\u006e":_bbc =_cc .sin (stack );case "\u0073\u0071\u0072\u0074":_bbc =_cc .sqrt (stack );case "\u0073\u0075\u0062":_bbc =_cc .sub (stack );case "\u0074\u0072\u0075\u006e\u0063\u0061\u0074\u0065":_bbc =_cc .truncate (stack );case "\u0078\u006f\u0072":_bbc =_cc .xor (stack );};return _bbc ;};
|
||||
|
||||
// MakeInteger returns a new PSInteger object initialized with `val`.
|
||||
func MakeInteger (val int )*PSInteger {_ccdg :=PSInteger {};_ccdg .Val =val ;return &_ccdg };func (_gfca *PSOperand )exp (_gee *PSStack )error {_afc ,_fba :=_gee .PopNumberAsFloat64 ();if _fba !=nil {return _fba ;};_fabf ,_fba :=_gee .PopNumberAsFloat64 ();if _fba !=nil {return _fba ;};if _bb .Abs (_afc )< 1&&_fabf < 0{return ErrUndefinedResult ;};_egbf :=_bb .Pow (_fabf ,_afc );_fba =_gee .Push (MakeReal (_egbf ));return _fba ;};func (_eecb *PSOperand )ln (_ebbe *PSStack )error {_dag ,_adce :=_ebbe .PopNumberAsFloat64 ();if _adce !=nil {return _adce ;};_ccga :=_bb .Log (_dag );_adce =_ebbe .Push (MakeReal (_ccga ));return _adce ;};func (_abga *PSParser )parseNumber ()(PSObject ,error ){_bege ,_bfge :=_cd .ParseNumber (_abga ._acfg );if _bfge !=nil {return nil ,_bfge ;};switch _ddaf :=_bege .(type ){case *_cd .PdfObjectFloat :return MakeReal (float64 (*_ddaf )),nil ;case *_cd .PdfObjectInteger :return MakeInteger (int (*_ddaf )),nil ;};return nil ,_e .Errorf ("\u0075n\u0068\u0061\u006e\u0064\u006c\u0065\u0064\u0020\u006e\u0075\u006db\u0065\u0072\u0020\u0074\u0079\u0070\u0065\u0020\u0025\u0054",_bege );};
|
||||
|
||||
// PSReal represents a real number.
|
||||
type PSReal struct{Val float64 ;};
|
||||
func (_cee *PSOperand )Exec (stack *PSStack )error {_fa :=ErrUnsupportedOperand ;switch *_cee {case "\u0061\u0062\u0073":_fa =_cee .abs (stack );case "\u0061\u0064\u0064":_fa =_cee .add (stack );case "\u0061\u006e\u0064":_fa =_cee .and (stack );case "\u0061\u0074\u0061\u006e":_fa =_cee .atan (stack );case "\u0062\u0069\u0074\u0073\u0068\u0069\u0066\u0074":_fa =_cee .bitshift (stack );case "\u0063e\u0069\u006c\u0069\u006e\u0067":_fa =_cee .ceiling (stack );case "\u0063\u006f\u0070\u0079":_fa =_cee .copy (stack );case "\u0063\u006f\u0073":_fa =_cee .cos (stack );case "\u0063\u0076\u0069":_fa =_cee .cvi (stack );case "\u0063\u0076\u0072":_fa =_cee .cvr (stack );case "\u0064\u0069\u0076":_fa =_cee .div (stack );case "\u0064\u0075\u0070":_fa =_cee .dup (stack );case "\u0065\u0071":_fa =_cee .eq (stack );case "\u0065\u0078\u0063\u0068":_fa =_cee .exch (stack );case "\u0065\u0078\u0070":_fa =_cee .exp (stack );case "\u0066\u006c\u006fo\u0072":_fa =_cee .floor (stack );case "\u0067\u0065":_fa =_cee .ge (stack );case "\u0067\u0074":_fa =_cee .gt (stack );case "\u0069\u0064\u0069\u0076":_fa =_cee .idiv (stack );case "\u0069\u0066":_fa =_cee .ifCondition (stack );case "\u0069\u0066\u0065\u006c\u0073\u0065":_fa =_cee .ifelse (stack );case "\u0069\u006e\u0064e\u0078":_fa =_cee .index (stack );case "\u006c\u0065":_fa =_cee .le (stack );case "\u006c\u006f\u0067":_fa =_cee .log (stack );case "\u006c\u006e":_fa =_cee .ln (stack );case "\u006c\u0074":_fa =_cee .lt (stack );case "\u006d\u006f\u0064":_fa =_cee .mod (stack );case "\u006d\u0075\u006c":_fa =_cee .mul (stack );case "\u006e\u0065":_fa =_cee .ne (stack );case "\u006e\u0065\u0067":_fa =_cee .neg (stack );case "\u006e\u006f\u0074":_fa =_cee .not (stack );case "\u006f\u0072":_fa =_cee .or (stack );case "\u0070\u006f\u0070":_fa =_cee .pop (stack );case "\u0072\u006f\u0075n\u0064":_fa =_cee .round (stack );case "\u0072\u006f\u006c\u006c":_fa =_cee .roll (stack );case "\u0073\u0069\u006e":_fa =_cee .sin (stack );case "\u0073\u0071\u0072\u0074":_fa =_cee .sqrt (stack );case "\u0073\u0075\u0062":_fa =_cee .sub (stack );case "\u0074\u0072\u0075\u006e\u0063\u0061\u0074\u0065":_fa =_cee .truncate (stack );case "\u0078\u006f\u0072":_fa =_cee .xor (stack );};return _fa ;};
|
||||
|
||||
// PSProgram defines a Postscript program which is a series of PS objects (arguments, commands, programs etc).
|
||||
type PSProgram []PSObject ;
|
||||
|
||||
// PSBoolean represents a boolean value.
|
||||
type PSBoolean struct{Val bool ;};func (_eac *PSOperand )copy (_aed *PSStack )error {_eaa ,_cgf :=_aed .PopInteger ();if _cgf !=nil {return _cgf ;};if _eaa < 0{return ErrRangeCheck ;};if _eaa > len (*_aed ){return ErrRangeCheck ;};*_aed =append (*_aed ,(*_aed )[len (*_aed )-_eaa :]...);return nil ;};func _dfg (_ggb int )int {if _ggb < 0{return -_ggb ;};return _ggb ;};func (_bfce *PSOperand )mul (_gaf *PSStack )error {_bfbb ,_cdgf :=_gaf .Pop ();if _cdgf !=nil {return _cdgf ;};_cda ,_cdgf :=_gaf .Pop ();if _cdgf !=nil {return _cdgf ;};_ccc ,_afe :=_bfbb .(*PSReal );_gbd ,_bfcc :=_bfbb .(*PSInteger );if !_afe &&!_bfcc {return ErrTypeCheck ;};_ccd ,_bbbc :=_cda .(*PSReal );_ddd ,_gfg :=_cda .(*PSInteger );if !_bbbc &&!_gfg {return ErrTypeCheck ;};if _bfcc &&_gfg {_cge :=_gbd .Val *_ddd .Val ;_cce :=_gaf .Push (MakeInteger (_cge ));return _cce ;};var _dgd float64 =0;if _afe {_dgd =_ccc .Val ;}else {_dgd =float64 (_gbd .Val );};if _bbbc {_dgd *=_ccd .Val ;}else {_dgd *=float64 (_ddd .Val );};_cdgf =_gaf .Push (MakeReal (_dgd ));return _cdgf ;};
|
||||
// Execute executes the program for an input parameters `objects` and returns a slice of output objects.
|
||||
func (_ab *PSExecutor )Execute (objects []PSObject )([]PSObject ,error ){for _ ,_eaa :=range objects {_fc :=_ab .Stack .Push (_eaa );if _fc !=nil {return nil ,_fc ;};};_bc :=_ab ._ce .Exec (_ab .Stack );if _bc !=nil {_bb .Log .Debug ("\u0045x\u0065c\u0020\u0066\u0061\u0069\u006c\u0065\u0064\u003a\u0020\u0025\u0076",_bc );return nil ,_bc ;};_ee :=[]PSObject (*_ab .Stack );_ab .Stack .Empty ();return _ee ,nil ;};
|
||||
|
||||
// PSReal represents a real number.
|
||||
type PSReal struct{Val float64 ;};func (_dfbf *PSOperand )xor (_eaff *PSStack )error {_cgee ,_gef :=_eaff .Pop ();if _gef !=nil {return _gef ;};_cbga ,_gef :=_eaff .Pop ();if _gef !=nil {return _gef ;};if _cbeb ,_fcfc :=_cgee .(*PSBoolean );_fcfc {_efcc ,_ddga :=_cbga .(*PSBoolean );if !_ddga {return ErrTypeCheck ;};_gef =_eaff .Push (MakeBool (_cbeb .Val !=_efcc .Val ));return _gef ;};if _cfee ,_cfgg :=_cgee .(*PSInteger );_cfgg {_dfce ,_cdga :=_cbga .(*PSInteger );if !_cdga {return ErrTypeCheck ;};_gef =_eaff .Push (MakeInteger (_cfee .Val ^_dfce .Val ));return _gef ;};return ErrTypeCheck ;};
|
||||
|
||||
// MakeBool returns a new PSBoolean object initialized with `val`.
|
||||
func MakeBool (val bool )*PSBoolean {_bgbg :=PSBoolean {};_bgbg .Val =val ;return &_bgbg };func (_cbfc *PSOperand )mul (_ffef *PSStack )error {_cggd ,_bce :=_ffef .Pop ();if _bce !=nil {return _bce ;};_cdbe ,_bce :=_ffef .Pop ();if _bce !=nil {return _bce ;};_acge ,_ecc :=_cggd .(*PSReal );_cegg ,_efce :=_cggd .(*PSInteger );if !_ecc &&!_efce {return ErrTypeCheck ;};_dag ,_abba :=_cdbe .(*PSReal );_aeb ,_dgba :=_cdbe .(*PSInteger );if !_abba &&!_dgba {return ErrTypeCheck ;};if _efce &&_dgba {_faa :=_cegg .Val *_aeb .Val ;_faf :=_ffef .Push (MakeInteger (_faa ));return _faf ;};var _dbcc float64 =0;if _ecc {_dbcc =_acge .Val ;}else {_dbcc =float64 (_cegg .Val );};if _abba {_dbcc *=_dag .Val ;}else {_dbcc *=float64 (_aeb .Val );};_bce =_ffef .Push (MakeReal (_dbcc ));return _bce ;};var ErrTypeCheck =_b .New ("\u0074\u0079p\u0065\u0020\u0063h\u0065\u0063\u006b\u0020\u0065\u0072\u0072\u006f\u0072");func (_ff *PSOperand )eq (_ggaf *PSStack )error {_dab ,_aeg :=_ggaf .Pop ();if _aeg !=nil {return _aeg ;};_agcd ,_aeg :=_ggaf .Pop ();if _aeg !=nil {return _aeg ;};_ggf ,_dgc :=_dab .(*PSBoolean );_agd ,_agfa :=_agcd .(*PSBoolean );if _dgc ||_agfa {var _gcd error ;if _dgc &&_agfa {_gcd =_ggaf .Push (MakeBool (_ggf .Val ==_agd .Val ));}else {_gcd =_ggaf .Push (MakeBool (false ));};return _gcd ;};var _ebe float64 ;var _dfb float64 ;if _dbf ,_eaac :=_dab .(*PSInteger );_eaac {_ebe =float64 (_dbf .Val );}else if _edd ,_cbb :=_dab .(*PSReal );_cbb {_ebe =_edd .Val ;}else {return ErrTypeCheck ;};if _ecb ,_acg :=_agcd .(*PSInteger );_acg {_dfb =float64 (_ecb .Val );}else if _gac ,_add :=_agcd .(*PSReal );_add {_dfb =_gac .Val ;}else {return ErrTypeCheck ;};if _be .Abs (_dfb -_ebe )< _eaf {_aeg =_ggaf .Push (MakeBool (true ));}else {_aeg =_ggaf .Push (MakeBool (false ));};return _aeg ;};func (_cgfc *PSOperand )gt (_gfg *PSStack )error {_gbde ,_bca :=_gfg .PopNumberAsFloat64 ();if _bca !=nil {return _bca ;};_cge ,_bca :=_gfg .PopNumberAsFloat64 ();if _bca !=nil {return _bca ;};if _be .Abs (_cge -_gbde )< _eaf {_bfgd :=_gfg .Push (MakeBool (false ));return _bfgd ;}else if _cge > _gbde {_bgf :=_gfg .Push (MakeBool (true ));return _bgf ;}else {_aedg :=_gfg .Push (MakeBool (false ));return _aedg ;};};func (_cdgd *PSOperand )floor (_adf *PSStack )error {_cfbb ,_fgfa :=_adf .Pop ();if _fgfa !=nil {return _fgfa ;};if _beg ,_bba :=_cfbb .(*PSReal );_bba {_fgfa =_adf .Push (MakeReal (_be .Floor (_beg .Val )));}else if _dcg ,_agb :=_cfbb .(*PSInteger );_agb {_fgfa =_adf .Push (MakeInteger (_dcg .Val ));}else {return ErrTypeCheck ;};return _fgfa ;};var ErrStackOverflow =_b .New ("\u0073\u0074\u0061\u0063\u006b\u0020\u006f\u0076\u0065r\u0066\u006c\u006f\u0077");func (_bbd *PSBoolean )String ()string {return _c .Sprintf ("\u0025\u0076",_bbd .Val )};func (_fg *PSOperand )String ()string {return _c .Sprintf ("\u0025\u0073",*_fg )};func (_cdg *PSBoolean )DebugString ()string {return _c .Sprintf ("\u0062o\u006f\u006c\u003a\u0025\u0076",_cdg .Val );};
|
||||
|
||||
// PSParser is a basic Postscript parser.
|
||||
type PSParser struct{_bddg *_dg .Reader };func (_bad *PSParser )parseNumber ()(PSObject ,error ){_fbdg ,_fbfg :=_a .ParseNumber (_bad ._bddg );if _fbfg !=nil {return nil ,_fbfg ;};switch _fffg :=_fbdg .(type ){case *_a .PdfObjectFloat :return MakeReal (float64 (*_fffg )),nil ;case *_a .PdfObjectInteger :return MakeInteger (int (*_fffg )),nil ;};return nil ,_c .Errorf ("\u0075n\u0068\u0061\u006e\u0064\u006c\u0065\u0064\u0020\u006e\u0075\u006db\u0065\u0072\u0020\u0074\u0079\u0070\u0065\u0020\u0025\u0054",_fbdg );};func (_deed *PSOperand )abs (_ca *PSStack )error {_gf ,_cec :=_ca .Pop ();if _cec !=nil {return _cec ;};if _bbb ,_ad :=_gf .(*PSReal );_ad {_afd :=_bbb .Val ;if _afd < 0{_cec =_ca .Push (MakeReal (-_afd ));}else {_cec =_ca .Push (MakeReal (_afd ));};}else if _ga ,_bd :=_gf .(*PSInteger );_bd {_bdg :=_ga .Val ;if _bdg < 0{_cec =_ca .Push (MakeInteger (-_bdg ));}else {_cec =_ca .Push (MakeInteger (_bdg ));};}else {return ErrTypeCheck ;};return _cec ;};
|
||||
|
||||
// PopNumberAsFloat64 pops and return the numeric value of the top of the stack as a float64.
|
||||
// Real or integer only.
|
||||
func (_ccd *PSStack )PopNumberAsFloat64 ()(float64 ,error ){_fdgf ,_bbab :=_ccd .Pop ();if _bbab !=nil {return 0,_bbab ;};if _edff ,_aabf :=_fdgf .(*PSReal );_aabf {return _edff .Val ,nil ;}else if _dfaa ,_fdgb :=_fdgf .(*PSInteger );_fdgb {return float64 (_dfaa .Val ),nil ;}else {return 0,ErrTypeCheck ;};};
|
||||
|
||||
// Parse parses the postscript and store as a program that can be executed.
|
||||
func (_ggc *PSParser )Parse ()(*PSProgram ,error ){_ggc .skipSpaces ();_fgac ,_bfe :=_ggc ._bddg .Peek (2);if _bfe !=nil {return nil ,_bfe ;};if _fgac [0]!='{'{return nil ,_b .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0050\u0053\u0020\u0050\u0072\u006f\u0067\u0072\u0061\u006d\u0020\u006e\u006f\u0074\u0020\u0073t\u0061\u0072\u0074\u0069\u006eg\u0020\u0077i\u0074\u0068\u0020\u007b");};_geba ,_bfe :=_ggc .parseFunction ();if _bfe !=nil &&_bfe !=_d .EOF {return nil ,_bfe ;};return _geba ,_bfe ;};func (_eee *PSOperand )lt (_eeb *PSStack )error {_ffe ,_gdd :=_eeb .PopNumberAsFloat64 ();if _gdd !=nil {return _gdd ;};_eaba ,_gdd :=_eeb .PopNumberAsFloat64 ();if _gdd !=nil {return _gdd ;};if _be .Abs (_eaba -_ffe )< _eaf {_aaa :=_eeb .Push (MakeBool (false ));return _aaa ;}else if _eaba < _ffe {_ddg :=_eeb .Push (MakeBool (true ));return _ddg ;}else {_ffc :=_eeb .Push (MakeBool (false ));return _ffc ;};};
|
||||
|
||||
// NewPSExecutor returns an initialized PSExecutor for an input `program`.
|
||||
func NewPSExecutor (program *PSProgram )*PSExecutor {_cd :=&PSExecutor {};_cd .Stack =NewPSStack ();_cd ._ce =program ;return _cd ;};func (_aace *PSOperand )ne (_aag *PSStack )error {_beabf :=_aace .eq (_aag );if _beabf !=nil {return _beabf ;};_beabf =_aace .not (_aag );return _beabf ;};
|
||||
|
||||
// PopInteger specificially pops an integer from the top of the stack, returning the value as an int.
|
||||
func (_fgab *PSStack )PopInteger ()(int ,error ){_aged ,_dccg :=_fgab .Pop ();if _dccg !=nil {return 0,_dccg ;};if _aec ,_gecd :=_aged .(*PSInteger );_gecd {return _aec .Val ,nil ;};return 0,ErrTypeCheck ;};func (_cba *PSOperand )and (_bg *PSStack )error {_fdf ,_ecfe :=_bg .Pop ();if _ecfe !=nil {return _ecfe ;};_afe ,_ecfe :=_bg .Pop ();if _ecfe !=nil {return _ecfe ;};if _feg ,_adc :=_fdf .(*PSBoolean );_adc {_egea ,_fb :=_afe .(*PSBoolean );if !_fb {return ErrTypeCheck ;};_ecfe =_bg .Push (MakeBool (_feg .Val &&_egea .Val ));return _ecfe ;};if _bbbf ,_cgf :=_fdf .(*PSInteger );_cgf {_fbd ,_beab :=_afe .(*PSInteger );if !_beab {return ErrTypeCheck ;};_ecfe =_bg .Push (MakeInteger (_bbbf .Val &_fbd .Val ));return _ecfe ;};return ErrTypeCheck ;};func (_ag *PSInteger )DebugString ()string {return _c .Sprintf ("\u0069\u006e\u0074\u003a\u0025\u0064",_ag .Val );};
|
||||
|
||||
// Empty empties the stack.
|
||||
func (_gfgc *PSStack )Empty (){*_gfgc =[]PSObject {}};func (_cbf *PSOperand )ifCondition (_cbbe *PSStack )error {_agee ,_dga :=_cbbe .Pop ();if _dga !=nil {return _dga ;};_egeb ,_dga :=_cbbe .Pop ();if _dga !=nil {return _dga ;};_ddd ,_gcg :=_agee .(*PSProgram );if !_gcg {return ErrTypeCheck ;};_eec ,_gcg :=_egeb .(*PSBoolean );if !_gcg {return ErrTypeCheck ;};if _eec .Val {_bcde :=_ddd .Exec (_cbbe );return _bcde ;};return nil ;};
|
||||
|
||||
// MakeInteger returns a new PSInteger object initialized with `val`.
|
||||
func MakeInteger (val int )*PSInteger {_fefd :=PSInteger {};_fefd .Val =val ;return &_fefd };func (_fea *PSOperand )neg (_ade *PSStack )error {_gfa ,_cgfg :=_ade .Pop ();if _cgfg !=nil {return _cgfg ;};if _efg ,_bdd :=_gfa .(*PSReal );_bdd {_cgfg =_ade .Push (MakeReal (-_efg .Val ));return _cgfg ;}else if _gec ,_aebe :=_gfa .(*PSInteger );_aebe {_cgfg =_ade .Push (MakeInteger (-_gec .Val ));return _cgfg ;}else {return ErrTypeCheck ;};};func (_eae *PSReal )Duplicate ()PSObject {_eef :=PSReal {};_eef .Val =_eae .Val ;return &_eef };func (_cdggd *PSOperand )cvi (_abae *PSStack )error {_gd ,_bcd :=_abae .Pop ();if _bcd !=nil {return _bcd ;};if _fabg ,_abb :=_gd .(*PSReal );_abb {_ddf :=int (_fabg .Val );_bcd =_abae .Push (MakeInteger (_ddf ));}else if _bdgd ,_cbdc :=_gd .(*PSInteger );_cbdc {_gfd :=_bdgd .Val ;_bcd =_abae .Push (MakeInteger (_gfd ));}else {return ErrTypeCheck ;};return _bcd ;};var ErrUnsupportedOperand =_b .New ("\u0075\u006e\u0073\u0075pp\u006f\u0072\u0074\u0065\u0064\u0020\u006f\u0070\u0065\u0072\u0061\u006e\u0064");func (_dc *PSOperand )add (_ge *PSStack )error {_fac ,_gbe :=_ge .Pop ();if _gbe !=nil {return _gbe ;};_egf ,_gbe :=_ge .Pop ();if _gbe !=nil {return _gbe ;};_dba ,_bbbg :=_fac .(*PSReal );_agf ,_bdf :=_fac .(*PSInteger );if !_bbbg &&!_bdf {return ErrTypeCheck ;};_cbd ,_gbd :=_egf .(*PSReal );_ecf ,_eb :=_egf .(*PSInteger );if !_gbd &&!_eb {return ErrTypeCheck ;};if _bdf &&_eb {_ef :=_agf .Val +_ecf .Val ;_bfg :=_ge .Push (MakeInteger (_ef ));return _bfg ;};var _ed float64 =0;if _bbbg {_ed =_dba .Val ;}else {_ed =float64 (_agf .Val );};if _gbd {_ed +=_cbd .Val ;}else {_ed +=float64 (_ecf .Val );};_gbe =_ge .Push (MakeReal (_ed ));return _gbe ;};
|
||||
|
||||
// PSExecutor has its own execution stack and is used to executre a PS routine (program).
|
||||
type PSExecutor struct{Stack *PSStack ;_ce *PSProgram ;};func (_fcf *PSOperand )le (_bbfb *PSStack )error {_fcb ,_fbbb :=_bbfb .PopNumberAsFloat64 ();if _fbbb !=nil {return _fbbb ;};_gfdg ,_fbbb :=_bbfb .PopNumberAsFloat64 ();if _fbbb !=nil {return _fbbb ;};if _be .Abs (_gfdg -_fcb )< _eaf {_eba :=_bbfb .Push (MakeBool (true ));return _eba ;}else if _gfdg < _fcb {_gdg :=_bbfb .Push (MakeBool (true ));return _gdg ;}else {_fdc :=_bbfb .Push (MakeBool (false ));return _fdc ;};};func (_ddb *PSOperand )cvr (_bbdf *PSStack )error {_fcg ,_gae :=_bbdf .Pop ();if _gae !=nil {return _gae ;};if _gdb ,_fcga :=_fcg .(*PSReal );_fcga {_gae =_bbdf .Push (MakeReal (_gdb .Val ));}else if _cfb ,_gda :=_fcg .(*PSInteger );_gda {_gae =_bbdf .Push (MakeReal (float64 (_cfb .Val )));}else {return ErrTypeCheck ;};return _gae ;};func (_bacaa *PSParser )parseBool ()(*PSBoolean ,error ){_aegb ,_ebfg :=_bacaa ._bddg .Peek (4);if _ebfg !=nil {return MakeBool (false ),_ebfg ;};if (len (_aegb )>=4)&&(string (_aegb [:4])=="\u0074\u0072\u0075\u0065"){_bacaa ._bddg .Discard (4);return MakeBool (true ),nil ;};_aegb ,_ebfg =_bacaa ._bddg .Peek (5);if _ebfg !=nil {return MakeBool (false ),_ebfg ;};if (len (_aegb )>=5)&&(string (_aegb [:5])=="\u0066\u0061\u006cs\u0065"){_bacaa ._bddg .Discard (5);return MakeBool (false ),nil ;};return MakeBool (false ),_b .New ("\u0075n\u0065\u0078\u0070\u0065c\u0074\u0065\u0064\u0020\u0062o\u006fl\u0065a\u006e\u0020\u0073\u0074\u0072\u0069\u006eg");};func (_cb *PSReal )DebugString ()string {return _c .Sprintf ("\u0072e\u0061\u006c\u003a\u0025\u002e\u0035f",_cb .Val );};func (_dee *PSBoolean )Duplicate ()PSObject {_age :=PSBoolean {};_age .Val =_dee .Val ;return &_age };func (_ac *PSReal )String ()string {return _c .Sprintf ("\u0025\u002e\u0035\u0066",_ac .Val )};const _eaf =0.000001;func (_cf *PSProgram )String ()string {_cfe :="\u007b\u0020";for _ ,_dae :=range *_cf {_cfe +=_dae .String ();_cfe +="\u0020";};_cfe +="\u007d";return _cfe ;};func (_bef *PSOperand )exch (_ebf *PSStack )error {_gcfe ,_gcdb :=_ebf .Pop ();if _gcdb !=nil {return _gcdb ;};_efc ,_gcdb :=_ebf .Pop ();if _gcdb !=nil {return _gcdb ;};_gcdb =_ebf .Push (_gcfe );if _gcdb !=nil {return _gcdb ;};_gcdb =_ebf .Push (_efc );return _gcdb ;};
|
||||
|
||||
// NewPSProgram returns an empty, initialized PSProgram.
|
||||
func NewPSProgram ()*PSProgram {return &PSProgram {}};func (_geab *PSOperand )index (_ccb *PSStack )error {_bbef ,_cega :=_ccb .Pop ();if _cega !=nil {return _cega ;};_fbb ,_abc :=_bbef .(*PSInteger );if !_abc {return ErrTypeCheck ;};if _fbb .Val < 0{return ErrRangeCheck ;};if _fbb .Val > len (*_ccb )-1{return ErrStackUnderflow ;};_bbee :=(*_ccb )[len (*_ccb )-1-_fbb .Val ];_cega =_ccb .Push (_bbee .Duplicate ());return _cega ;};
|
||||
|
||||
// Pop pops an object from the top of the stack.
|
||||
func (_dde *PSStack )Pop ()(PSObject ,error ){if len (*_dde )< 1{return nil ,ErrStackUnderflow ;};_gdde :=(*_dde )[len (*_dde )-1];*_dde =(*_dde )[0:len (*_dde )-1];return _gdde ,nil ;};func (_eg *PSInteger )String ()string {return _c .Sprintf ("\u0025\u0064",_eg .Val )};func (_feab *PSOperand )sub (_fde *PSStack )error {_cbde ,_ggag :=_fde .Pop ();if _ggag !=nil {return _ggag ;};_bga ,_ggag :=_fde .Pop ();if _ggag !=nil {return _ggag ;};_egc ,_cff :=_cbde .(*PSReal );_cca ,_fce :=_cbde .(*PSInteger );if !_cff &&!_fce {return ErrTypeCheck ;};_gcag ,_dggb :=_bga .(*PSReal );_baca ,_cdbg :=_bga .(*PSInteger );if !_dggb &&!_cdbg {return ErrTypeCheck ;};if _fce &&_cdbg {_acgb :=_baca .Val -_cca .Val ;_bgfag :=_fde .Push (MakeInteger (_acgb ));return _bgfag ;};var _gcb float64 =0;if _dggb {_gcb =_gcag .Val ;}else {_gcb =float64 (_baca .Val );};if _cff {_gcb -=_egc .Val ;}else {_gcb -=float64 (_cca .Val );};_ggag =_fde .Push (MakeReal (_gcb ));return _ggag ;};func (_cgg *PSOperand )log (_bge *PSStack )error {_affb ,_ddbb :=_bge .PopNumberAsFloat64 ();if _ddbb !=nil {return _ddbb ;};_daa :=_be .Log10 (_affb );_ddbb =_bge .Push (MakeReal (_daa ));return _ddbb ;};
|
||||
|
||||
// PSOperand represents a Postscript operand (text string).
|
||||
type PSOperand string ;
|
||||
|
||||
// PSInteger represents an integer.
|
||||
type PSInteger struct{Val int ;};func (_cfd *PSOperand )ge (_fag *PSStack )error {_ecg ,_cad :=_fag .PopNumberAsFloat64 ();if _cad !=nil {return _cad ;};_eabb ,_cad :=_fag .PopNumberAsFloat64 ();if _cad !=nil {return _cad ;};if _be .Abs (_eabb -_ecg )< _eaf {_ecgc :=_fag .Push (MakeBool (true ));return _ecgc ;}else if _eabb > _ecg {_fgd :=_fag .Push (MakeBool (true ));return _fgd ;}else {_eaab :=_fag .Push (MakeBool (false ));return _eaab ;};};func (_cda *PSOperand )truncate (_ddac *PSStack )error {_adfe ,_dac :=_ddac .Pop ();if _dac !=nil {return _dac ;};if _dbb ,_ddff :=_adfe .(*PSReal );_ddff {_gbc :=int (_dbb .Val );_dac =_ddac .Push (MakeReal (float64 (_gbc )));}else if _ebdf ,_edf :=_adfe .(*PSInteger );_edf {_dac =_ddac .Push (MakeInteger (_ebdf .Val ));}else {return ErrTypeCheck ;};return _dac ;};func (_ecgca *PSOperand )pop (_fdcg *PSStack )error {_ ,_ddgf :=_fdcg .Pop ();if _ddgf !=nil {return _ddgf ;};return nil ;};
|
||||
|
||||
// PSStack defines a stack of PSObjects. PSObjects can be pushed on or pull from the stack.
|
||||
type PSStack []PSObject ;func (_eaad *PSOperand )div (_cbe *PSStack )error {_fga ,_gca :=_cbe .Pop ();if _gca !=nil {return _gca ;};_bgg ,_gca :=_cbe .Pop ();if _gca !=nil {return _gca ;};_fgf ,_dfeb :=_fga .(*PSReal );_fge ,_gde :=_fga .(*PSInteger );if !_dfeb &&!_gde {return ErrTypeCheck ;};if _dfeb &&_fgf .Val ==0{return ErrUndefinedResult ;};if _gde &&_fge .Val ==0{return ErrUndefinedResult ;};_afg ,_eab :=_bgg .(*PSReal );_daf ,_acb :=_bgg .(*PSInteger );if !_eab &&!_acb {return ErrTypeCheck ;};var _adb float64 =0;if _eab {_adb =_afg .Val ;}else {_adb =float64 (_daf .Val );};if _dfeb {_adb /=_fgf .Val ;}else {_adb /=float64 (_fge .Val );};_gca =_cbe .Push (MakeReal (_adb ));return _gca ;};func (_eabd *PSOperand )sqrt (_cfde *PSStack )error {_cbg ,_gabc :=_cfde .PopNumberAsFloat64 ();if _gabc !=nil {return _gabc ;};if _cbg < 0{return ErrRangeCheck ;};_ggae :=_be .Sqrt (_cbg );_gabc =_cfde .Push (MakeReal (_ggae ));return _gabc ;};func (_adfda *PSOperand )ifelse (_aff *PSStack )error {_bfdf ,_dgab :=_aff .Pop ();if _dgab !=nil {return _dgab ;};_ebd ,_dgab :=_aff .Pop ();if _dgab !=nil {return _dgab ;};_cab ,_dgab :=_aff .Pop ();if _dgab !=nil {return _dgab ;};_fdg ,_aaca :=_bfdf .(*PSProgram );if !_aaca {return ErrTypeCheck ;};_bdgg ,_aaca :=_ebd .(*PSProgram );if !_aaca {return ErrTypeCheck ;};_dabg ,_aaca :=_cab .(*PSBoolean );if !_aaca {return ErrTypeCheck ;};if _dabg .Val {_bfdfb :=_bdgg .Exec (_aff );return _bfdfb ;};_dgab =_fdg .Exec (_aff );return _dgab ;};
|
||||
|
||||
// MakeReal returns a new PSReal object initialized with `val`.
|
||||
func MakeReal (val float64 )*PSReal {_bdgb :=PSReal {};_bdgb .Val =val ;return &_bdgb };func (_ceg *PSOperand )atan (_bdga *PSStack )error {_dgb ,_aca :=_bdga .PopNumberAsFloat64 ();if _aca !=nil {return _aca ;};_dfe ,_aca :=_bdga .PopNumberAsFloat64 ();if _aca !=nil {return _aca ;};if _dgb ==0{var _gc error ;if _dfe < 0{_gc =_bdga .Push (MakeReal (270));}else {_gc =_bdga .Push (MakeReal (90));};return _gc ;};_egeg :=_dfe /_dgb ;_fab :=_be .Atan (_egeg )*180/_be .Pi ;_aca =_bdga .Push (MakeReal (_fab ));return _aca ;};func (_fff *PSOperand )roll (_dcf *PSStack )error {_dgd ,_bbae :=_dcf .Pop ();if _bbae !=nil {return _bbae ;};_accc ,_bbae :=_dcf .Pop ();if _bbae !=nil {return _bbae ;};_gacf ,_dabgc :=_dgd .(*PSInteger );if !_dabgc {return ErrTypeCheck ;};_fbfb ,_dabgc :=_accc .(*PSInteger );if !_dabgc {return ErrTypeCheck ;};if _fbfb .Val < 0{return ErrRangeCheck ;};if _fbfb .Val ==0||_fbfb .Val ==1{return nil ;};if _fbfb .Val > len (*_dcf ){return ErrStackUnderflow ;};for _fegc :=0;_fegc < _ggcb (_gacf .Val );_fegc ++{var _cdgf []PSObject ;_cdgf =(*_dcf )[len (*_dcf )-(_fbfb .Val ):len (*_dcf )];if _gacf .Val > 0{_cac :=_cdgf [len (_cdgf )-1];_cdgf =append ([]PSObject {_cac },_cdgf [0:len (_cdgf )-1]...);}else {_cde :=_cdgf [len (_cdgf )-_fbfb .Val ];_cdgf =append (_cdgf [1:],_cde );};_fbc :=append ((*_dcf )[0:len (*_dcf )-_fbfb .Val ],_cdgf ...);_dcf =&_fbc ;};return nil ;};func _ggcb (_baf int )int {if _baf < 0{return -_baf ;};return _baf ;};var ErrUndefinedResult =_b .New ("\u0075\u006e\u0064\u0065fi\u006e\u0065\u0064\u0020\u0072\u0065\u0073\u0075\u006c\u0074\u0020\u0065\u0072\u0072o\u0072");func (_fae *PSOperand )mod (_fabc *PSStack )error {_dfdc ,_gace :=_fabc .Pop ();if _gace !=nil {return _gace ;};_fgb ,_gace :=_fabc .Pop ();if _gace !=nil {return _gace ;};_bag ,_gab :=_dfdc .(*PSInteger );if !_gab {return ErrTypeCheck ;};if _bag .Val ==0{return ErrUndefinedResult ;};_bdc ,_gab :=_fgb .(*PSInteger );if !_gab {return ErrTypeCheck ;};_dbg :=_bdc .Val %_bag .Val ;_gace =_fabc .Push (MakeInteger (_dbg ));return _gace ;};func (_gabe *PSParser )skipSpaces ()(int ,error ){_bgd :=0;for {_gbef ,_dad :=_gabe ._bddg .Peek (1);if _dad !=nil {return 0,_dad ;};if _a .IsWhiteSpace (_gbef [0]){_gabe ._bddg .ReadByte ();_bgd ++;}else {break ;};};return _bgd ,nil ;};
|
||||
|
||||
// NewPSStack returns an initialized PSStack.
|
||||
func NewPSStack ()*PSStack {return &PSStack {}};
|
||||
func NewPSStack ()*PSStack {return &PSStack {}};func (_gce *PSOperand )exp (_gad *PSStack )error {_bcdf ,_aga :=_gad .PopNumberAsFloat64 ();if _aga !=nil {return _aga ;};_bee ,_aga :=_gad .PopNumberAsFloat64 ();if _aga !=nil {return _aga ;};if _be .Abs (_bcdf )< 1&&_bee < 0{return ErrUndefinedResult ;};_agcde :=_be .Pow (_bee ,_bcdf );_aga =_gad .Push (MakeReal (_agcde ));return _aga ;};func (_gfe *PSParser )parseFunction ()(*PSProgram ,error ){_fcfa ,_ :=_gfe ._bddg .ReadByte ();if _fcfa !='{'{return nil ,_b .New ("\u0069\u006ev\u0061\u006c\u0069d\u0020\u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e");};_aef :=NewPSProgram ();for {_gfe .skipSpaces ();_ecce ,_adeb :=_gfe ._bddg .Peek (2);if _adeb !=nil {if _adeb ==_d .EOF {break ;};return nil ,_adeb ;};_bb .Log .Trace ("\u0050e\u0065k\u0020\u0073\u0074\u0072\u0069\u006e\u0067\u003a\u0020\u0025\u0073",string (_ecce ));if _ecce [0]=='}'{_bb .Log .Trace ("\u0045\u004f\u0046 \u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e");_gfe ._bddg .ReadByte ();break ;}else if _ecce [0]=='{'{_bb .Log .Trace ("\u0046u\u006e\u0063\u0074\u0069\u006f\u006e!");_gge ,_geg :=_gfe .parseFunction ();if _geg !=nil {return nil ,_geg ;};_aef .Append (_gge );}else if _a .IsDecimalDigit (_ecce [0])||(_ecce [0]=='-'&&_a .IsDecimalDigit (_ecce [1])){_bb .Log .Trace ("\u002d>\u004e\u0075\u006d\u0062\u0065\u0072!");_fggc ,_gcgf :=_gfe .parseNumber ();if _gcgf !=nil {return nil ,_gcgf ;};_aef .Append (_fggc );}else {_bb .Log .Trace ("\u002d>\u004fp\u0065\u0072\u0061\u006e\u0064 \u006f\u0072 \u0062\u006f\u006f\u006c\u003f");_ecce ,_ =_gfe ._bddg .Peek (5);_gddg :=string (_ecce );_bb .Log .Trace ("\u0050\u0065\u0065k\u0020\u0073\u0074\u0072\u003a\u0020\u0025\u0073",_gddg );if (len (_gddg )> 4)&&(_gddg [:5]=="\u0066\u0061\u006cs\u0065"){_gag ,_fgfg :=_gfe .parseBool ();if _fgfg !=nil {return nil ,_fgfg ;};_aef .Append (_gag );}else if (len (_gddg )> 3)&&(_gddg [:4]=="\u0074\u0072\u0075\u0065"){_abcf ,_efb :=_gfe .parseBool ();if _efb !=nil {return nil ,_efb ;};_aef .Append (_abcf );}else {_ceef ,_dcd :=_gfe .parseOperand ();if _dcd !=nil {return nil ,_dcd ;};_aef .Append (_ceef );};};};return _aef ,nil ;};func (_aed *PSOperand )ceiling (_aac *PSStack )error {_dd ,_dfc :=_aac .Pop ();if _dfc !=nil {return _dfc ;};if _cgdf ,_egg :=_dd .(*PSReal );_egg {_dfc =_aac .Push (MakeReal (_be .Ceil (_cgdf .Val )));}else if _agc ,_dea :=_dd .(*PSInteger );_dea {_dfc =_aac .Push (MakeInteger (_agc .Val ));}else {_dfc =ErrTypeCheck ;};return _dfc ;};func (_dfa *PSOperand )or (_fgg *PSStack )error {_cgge ,_fcc :=_fgg .Pop ();if _fcc !=nil {return _fcc ;};_dcgf ,_fcc :=_fgg .Pop ();if _fcc !=nil {return _fcc ;};if _cef ,_gebf :=_cgge .(*PSBoolean );_gebf {_bff ,_dgf :=_dcgf .(*PSBoolean );if !_dgf {return ErrTypeCheck ;};_fcc =_fgg .Push (MakeBool (_cef .Val ||_bff .Val ));return _fcc ;};if _bage ,_aaba :=_cgge .(*PSInteger );_aaba {_dcge ,_fbf :=_dcgf .(*PSInteger );if !_fbf {return ErrTypeCheck ;};_fcc =_fgg .Push (MakeInteger (_bage .Val |_dcge .Val ));return _fcc ;};return ErrTypeCheck ;};
|
||||
|
||||
// NewPSParser returns a new instance of the PDF Postscript parser from input data.
|
||||
func NewPSParser (content []byte )*PSParser {_fbg :=PSParser {};_fgfe :=_ea .NewBuffer (content );_fbg ._bddg =_dg .NewReader (_fgfe );return &_fbg ;};
|
||||
|
||||
// PSBoolean represents a boolean value.
|
||||
type PSBoolean struct{Val bool ;};
|
||||
|
||||
// MakeOperand returns a new PSOperand object based on string `val`.
|
||||
func MakeOperand (val string )*PSOperand {_acae :=PSOperand (val );return &_acae };func (_g *PSProgram )DebugString ()string {_ded :="\u007b\u0020";for _ ,_gg :=range *_g {_ded +=_gg .DebugString ();_ded +="\u0020";};_ded +="\u007d";return _ded ;};
|
||||
|
||||
// PSObject represents a postscript object.
|
||||
type PSObject interface{
|
||||
@ -55,65 +106,14 @@ Duplicate ()PSObject ;
|
||||
DebugString ()string ;
|
||||
|
||||
// String returns a string representation of the PSObject.
|
||||
String ()string ;};func (_bfdb *PSOperand )ne (_daba *PSStack )error {_gcdf :=_bfdb .eq (_daba );if _gcdf !=nil {return _gcdf ;};_gcdf =_bfdb .not (_daba );return _gcdf ;};func (_aa *PSBoolean )Duplicate ()PSObject {_af :=PSBoolean {};_af .Val =_aa .Val ;return &_af };
|
||||
|
||||
// PopInteger specificially pops an integer from the top of the stack, returning the value as an int.
|
||||
func (_dcgb *PSStack )PopInteger ()(int ,error ){_ddff ,_gea :=_dcgb .Pop ();if _gea !=nil {return 0,_gea ;};if _ede ,_addb :=_ddff .(*PSInteger );_addb {return _ede .Val ,nil ;};return 0,ErrTypeCheck ;};
|
||||
|
||||
// PSStack defines a stack of PSObjects. PSObjects can be pushed on or pull from the stack.
|
||||
type PSStack []PSObject ;func (_ceca *PSOperand )exch (_cdbb *PSStack )error {_fabg ,_befc :=_cdbb .Pop ();if _befc !=nil {return _befc ;};_fdc ,_befc :=_cdbb .Pop ();if _befc !=nil {return _befc ;};_befc =_cdbb .Push (_fabg );if _befc !=nil {return _befc ;};_befc =_cdbb .Push (_fdc );return _befc ;};func (_cefg *PSOperand )round (_dbabg *PSStack )error {_gegg ,_gcaa :=_dbabg .Pop ();if _gcaa !=nil {return _gcaa ;};if _gaa ,_bbe :=_gegg .(*PSReal );_bbe {_gcaa =_dbabg .Push (MakeReal (_bb .Floor (_gaa .Val +0.5)));}else if _bdec ,_dfcd :=_gegg .(*PSInteger );_dfcd {_gcaa =_dbabg .Push (MakeInteger (_bdec .Val ));}else {return ErrTypeCheck ;};return _gcaa ;};func (_dgc *PSProgram )DebugString ()string {_bef :="\u007b\u0020";for _ ,_dge :=range *_dgc {_bef +=_dge .DebugString ();_bef +="\u0020";};_bef +="\u007d";return _bef ;};const _dg =0.000001;func (_gcg *PSOperand )dup (_eefa *PSStack )error {_acd ,_bfef :=_eefa .Pop ();if _bfef !=nil {return _bfef ;};_bfef =_eefa .Push (_acd );if _bfef !=nil {return _bfef ;};_bfef =_eefa .Push (_acd .Duplicate ());return _bfef ;};func (_bed *PSOperand )log (_dbec *PSStack )error {_eege ,_acef :=_dbec .PopNumberAsFloat64 ();if _acef !=nil {return _acef ;};_ebf :=_bb .Log10 (_eege );_acef =_dbec .Push (MakeReal (_ebf ));return _acef ;};func (_cef *PSOperand )eq (_cdb *PSStack )error {_efa ,_ecea :=_cdb .Pop ();if _ecea !=nil {return _ecea ;};_ebd ,_ecea :=_cdb .Pop ();if _ecea !=nil {return _ecea ;};_gfc ,_bdf :=_efa .(*PSBoolean );_gfcf ,_bfa :=_ebd .(*PSBoolean );if _bdf ||_bfa {var _cgfc error ;if _bdf &&_bfa {_cgfc =_cdb .Push (MakeBool (_gfc .Val ==_gfcf .Val ));}else {_cgfc =_cdb .Push (MakeBool (false ));};return _cgfc ;};var _ebbf float64 ;var _gdb float64 ;if _gddd ,_cded :=_efa .(*PSInteger );_cded {_ebbf =float64 (_gddd .Val );}else if _fab ,_fbc :=_efa .(*PSReal );_fbc {_ebbf =_fab .Val ;}else {return ErrTypeCheck ;};if _ffa ,_dea :=_ebd .(*PSInteger );_dea {_gdb =float64 (_ffa .Val );}else if _dbab ,_gegd :=_ebd .(*PSReal );_gegd {_gdb =_dbab .Val ;}else {return ErrTypeCheck ;};if _bb .Abs (_gdb -_ebbf )< _dg {_ecea =_cdb .Push (MakeBool (true ));}else {_ecea =_cdb .Push (MakeBool (false ));};return _ecea ;};
|
||||
|
||||
// PSInteger represents an integer.
|
||||
type PSInteger struct{Val int ;};func (_ff *PSOperand )Duplicate ()PSObject {_gd :=*_ff ;return &_gd };func (_bde *PSOperand )floor (_dbac *PSStack )error {_decf ,_ccf :=_dbac .Pop ();if _ccf !=nil {return _ccf ;};if _fdb ,_cae :=_decf .(*PSReal );_cae {_ccf =_dbac .Push (MakeReal (_bb .Floor (_fdb .Val )));}else if _daeb ,_fgf :=_decf .(*PSInteger );_fgf {_ccf =_dbac .Push (MakeInteger (_daeb .Val ));}else {return ErrTypeCheck ;};return _ccf ;};func (_dgb *PSOperand )bitshift (_dee *PSStack )error {_bfc ,_dcd :=_dee .PopInteger ();if _dcd !=nil {return _dcd ;};_ae ,_dcd :=_dee .PopInteger ();if _dcd !=nil {return _dcd ;};var _gc int ;if _bfc >=0{_gc =_ae <<uint (_bfc );}else {_gc =_ae >>uint (-_bfc );};_dcd =_dee .Push (MakeInteger (_gc ));return _dcd ;};func (_fd *PSInteger )String ()string {return _e .Sprintf ("\u0025\u0064",_fd .Val )};func (_becb *PSOperand )gt (_ddg *PSStack )error {_egbb ,_ceg :=_ddg .PopNumberAsFloat64 ();if _ceg !=nil {return _ceg ;};_dfcc ,_ceg :=_ddg .PopNumberAsFloat64 ();if _ceg !=nil {return _ceg ;};if _bb .Abs (_dfcc -_egbb )< _dg {_dgg :=_ddg .Push (MakeBool (false ));return _dgg ;}else if _dfcc > _egbb {_faaf :=_ddg .Push (MakeBool (true ));return _faaf ;}else {_bade :=_ddg .Push (MakeBool (false ));return _bade ;};};
|
||||
|
||||
// NewPSProgram returns an empty, initialized PSProgram.
|
||||
func NewPSProgram ()*PSProgram {return &PSProgram {}};func (_cf *PSInteger )Duplicate ()PSObject {_fbe :=PSInteger {};_fbe .Val =_cf .Val ;return &_fbe };func (_dbg *PSOperand )atan (_bad *PSStack )error {_dcf ,_ca :=_bad .PopNumberAsFloat64 ();if _ca !=nil {return _ca ;};_beb ,_ca :=_bad .PopNumberAsFloat64 ();if _ca !=nil {return _ca ;};if _dcf ==0{var _ed error ;if _beb < 0{_ed =_bad .Push (MakeReal (270));}else {_ed =_bad .Push (MakeReal (90));};return _ed ;};_aaa :=_beb /_dcf ;_fdg :=_bb .Atan (_aaa )*180/_bb .Pi ;_ca =_bad .Push (MakeReal (_fdg ));return _ca ;};func (_eee *PSOperand )String ()string {return _e .Sprintf ("\u0025\u0073",*_eee )};func (_dfd *PSOperand )sqrt (_cccf *PSStack )error {_gbgc ,_adfg :=_cccf .PopNumberAsFloat64 ();if _adfg !=nil {return _adfg ;};if _gbgc < 0{return ErrRangeCheck ;};_cccd :=_bb .Sqrt (_gbgc );_adfg =_cccf .Push (MakeReal (_cccd ));return _adfg ;};
|
||||
|
||||
// PSExecutor has its own execution stack and is used to executre a PS routine (program).
|
||||
type PSExecutor struct{Stack *PSStack ;_de *PSProgram ;};var ErrRangeCheck =_d .New ("\u0072\u0061\u006e\u0067\u0065\u0020\u0063\u0068\u0065\u0063\u006b\u0020e\u0072\u0072\u006f\u0072");func (_aae *PSParser )parseBool ()(*PSBoolean ,error ){_cdae ,_bafe :=_aae ._acfg .Peek (4);if _bafe !=nil {return MakeBool (false ),_bafe ;};if (len (_cdae )>=4)&&(string (_cdae [:4])=="\u0074\u0072\u0075\u0065"){_aae ._acfg .Discard (4);return MakeBool (true ),nil ;};_cdae ,_bafe =_aae ._acfg .Peek (5);if _bafe !=nil {return MakeBool (false ),_bafe ;};if (len (_cdae )>=5)&&(string (_cdae [:5])=="\u0066\u0061\u006cs\u0065"){_aae ._acfg .Discard (5);return MakeBool (false ),nil ;};return MakeBool (false ),_d .New ("\u0075n\u0065\u0078\u0070\u0065c\u0074\u0065\u0064\u0020\u0062o\u006fl\u0065a\u006e\u0020\u0073\u0074\u0072\u0069\u006eg");};var ErrStackOverflow =_d .New ("\u0073\u0074\u0061\u0063\u006b\u0020\u006f\u0076\u0065r\u0066\u006c\u006f\u0077");func (_bdbe *PSOperand )or (_gge *PSStack )error {_cadg ,_bga :=_gge .Pop ();if _bga !=nil {return _bga ;};_cgef ,_bga :=_gge .Pop ();if _bga !=nil {return _bga ;};if _bggb ,_agc :=_cadg .(*PSBoolean );_agc {_caed ,_ecc :=_cgef .(*PSBoolean );if !_ecc {return ErrTypeCheck ;};_bga =_gge .Push (MakeBool (_bggb .Val ||_caed .Val ));return _bga ;};if _cedg ,_ab :=_cadg .(*PSInteger );_ab {_eeea ,_gdac :=_cgef .(*PSInteger );if !_gdac {return ErrTypeCheck ;};_bga =_gge .Push (MakeInteger (_cedg .Val |_eeea .Val ));return _bga ;};return ErrTypeCheck ;};func (_gcd *PSOperand )idiv (_cdgg *PSStack )error {_fed ,_gcdg :=_cdgg .Pop ();if _gcdg !=nil {return _gcdg ;};_cgff ,_gcdg :=_cdgg .Pop ();if _gcdg !=nil {return _gcdg ;};_add ,_fbg :=_fed .(*PSInteger );if !_fbg {return ErrTypeCheck ;};if _add .Val ==0{return ErrUndefinedResult ;};_bab ,_fbg :=_cgff .(*PSInteger );if !_fbg {return ErrTypeCheck ;};_ccbfb :=_bab .Val /_add .Val ;_gcdg =_cdgg .Push (MakeInteger (_ccbfb ));return _gcdg ;};
|
||||
|
||||
// Parse parses the postscript and store as a program that can be executed.
|
||||
func (_dda *PSParser )Parse ()(*PSProgram ,error ){_dda .skipSpaces ();_daea ,_gbb :=_dda ._acfg .Peek (2);if _gbb !=nil {return nil ,_gbb ;};if _daea [0]!='{'{return nil ,_d .New ("\u0069\u006e\u0076\u0061\u006c\u0069\u0064\u0020\u0050\u0053\u0020\u0050\u0072\u006f\u0067\u0072\u0061\u006d\u0020\u006e\u006f\u0074\u0020\u0073t\u0061\u0072\u0074\u0069\u006eg\u0020\u0077i\u0074\u0068\u0020\u007b");};_beeb ,_gbb :=_dda .parseFunction ();if _gbb !=nil &&_gbb !=_b .EOF {return nil ,_gbb ;};return _beeb ,_gbb ;};func (_dga *PSOperand )not (_gbde *PSStack )error {_aegb ,_gce :=_gbde .Pop ();if _gce !=nil {return _gce ;};if _bge ,_fgda :=_aegb .(*PSBoolean );_fgda {_gce =_gbde .Push (MakeBool (!_bge .Val ));return _gce ;}else if _gcf ,_cac :=_aegb .(*PSInteger );_cac {_gce =_gbde .Push (MakeInteger (^_gcf .Val ));return _gce ;}else {return ErrTypeCheck ;};};
|
||||
String ()string ;};
|
||||
|
||||
// DebugString returns a descriptive string representation of the stack - intended for debugging.
|
||||
func (_babd *PSStack )DebugString ()string {_efg :="\u005b\u0020";for _ ,_bfdd :=range *_babd {_efg +=_bfdd .DebugString ();_efg +="\u0020";};_efg +="\u005d";return _efg ;};func (_fga *PSOperand )DebugString ()string {return _e .Sprintf ("\u006fp\u003a\u0027\u0025\u0073\u0027",*_fga );};func (_bbb *PSBoolean )DebugString ()string {return _e .Sprintf ("\u0062o\u006f\u006c\u003a\u0025\u0076",_bbb .Val );};var ErrUndefinedResult =_d .New ("\u0075\u006e\u0064\u0065fi\u006e\u0065\u0064\u0020\u0072\u0065\u0073\u0075\u006c\u0074\u0020\u0065\u0072\u0072o\u0072");func (_gg *PSOperand )cvr (_eb *PSStack )error {_dfaf ,_dfc :=_eb .Pop ();if _dfc !=nil {return _dfc ;};if _aef ,_cfg :=_dfaf .(*PSReal );_cfg {_dfc =_eb .Push (MakeReal (_aef .Val ));}else if _ace ,_cdg :=_dfaf .(*PSInteger );_cdg {_dfc =_eb .Push (MakeReal (float64 (_ace .Val )));}else {return ErrTypeCheck ;};return _dfc ;};func (_gfe *PSOperand )mod (_ebe *PSStack )error {_eegc ,_bdb :=_ebe .Pop ();if _bdb !=nil {return _bdb ;};_gcag ,_bdb :=_ebe .Pop ();if _bdb !=nil {return _bdb ;};_bgf ,_dde :=_eegc .(*PSInteger );if !_dde {return ErrTypeCheck ;};if _bgf .Val ==0{return ErrUndefinedResult ;};_ecb ,_dde :=_gcag .(*PSInteger );if !_dde {return ErrTypeCheck ;};_deac :=_ecb .Val %_bgf .Val ;_bdb =_ebe .Push (MakeInteger (_deac ));return _bdb ;};func (_dbde *PSOperand )sin (_febg *PSStack )error {_egcf ,_fcbc :=_febg .PopNumberAsFloat64 ();if _fcbc !=nil {return _fcbc ;};_cdf :=_bb .Sin (_egcf *_bb .Pi /180.0);_fcbc =_febg .Push (MakeReal (_cdf ));return _fcbc ;};
|
||||
|
||||
// PSOperand represents a Postscript operand (text string).
|
||||
type PSOperand string ;func (_febc *PSOperand )lt (_dcg *PSStack )error {_acb ,_fafe :=_dcg .PopNumberAsFloat64 ();if _fafe !=nil {return _fafe ;};_bcef ,_fafe :=_dcg .PopNumberAsFloat64 ();if _fafe !=nil {return _fafe ;};if _bb .Abs (_bcef -_acb )< _dg {_cba :=_dcg .Push (MakeBool (false ));return _cba ;}else if _bcef < _acb {_ced :=_dcg .Push (MakeBool (true ));return _ced ;}else {_cgd :=_dcg .Push (MakeBool (false ));return _cgd ;};};func (_dbfa *PSOperand )le (_eec *PSStack )error {_gef ,_ggff :=_eec .PopNumberAsFloat64 ();if _ggff !=nil {return _ggff ;};_effc ,_ggff :=_eec .PopNumberAsFloat64 ();if _ggff !=nil {return _ggff ;};if _bb .Abs (_effc -_gef )< _dg {_gdfd :=_eec .Push (MakeBool (true ));return _gdfd ;}else if _effc < _gef {_ddb :=_eec .Push (MakeBool (true ));return _ddb ;}else {_gdc :=_eec .Push (MakeBool (false ));return _gdc ;};};func (_bgcc *PSParser )parseFunction ()(*PSProgram ,error ){_bdeg ,_ :=_bgcc ._acfg .ReadByte ();if _bdeg !='{'{return nil ,_d .New ("\u0069\u006ev\u0061\u006c\u0069d\u0020\u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e");};_eaf :=NewPSProgram ();for {_bgcc .skipSpaces ();_bcb ,_bfde :=_bgcc ._acfg .Peek (2);if _bfde !=nil {if _bfde ==_b .EOF {break ;};return nil ,_bfde ;};_dfe .Log .Trace ("\u0050e\u0065k\u0020\u0073\u0074\u0072\u0069\u006e\u0067\u003a\u0020\u0025\u0073",string (_bcb ));if _bcb [0]=='}'{_dfe .Log .Trace ("\u0045\u004f\u0046 \u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e");_bgcc ._acfg .ReadByte ();break ;}else if _bcb [0]=='{'{_dfe .Log .Trace ("\u0046u\u006e\u0063\u0074\u0069\u006f\u006e!");_ffg ,_ddf :=_bgcc .parseFunction ();if _ddf !=nil {return nil ,_ddf ;};_eaf .Append (_ffg );}else if _cd .IsDecimalDigit (_bcb [0])||(_bcb [0]=='-'&&_cd .IsDecimalDigit (_bcb [1])){_dfe .Log .Trace ("\u002d>\u004e\u0075\u006d\u0062\u0065\u0072!");_eeed ,_cadgg :=_bgcc .parseNumber ();if _cadgg !=nil {return nil ,_cadgg ;};_eaf .Append (_eeed );}else {_dfe .Log .Trace ("\u002d>\u004fp\u0065\u0072\u0061\u006e\u0064 \u006f\u0072 \u0062\u006f\u006f\u006c\u003f");_bcb ,_ =_bgcc ._acfg .Peek (5);_dgaa :=string (_bcb );_dfe .Log .Trace ("\u0050\u0065\u0065k\u0020\u0073\u0074\u0072\u003a\u0020\u0025\u0073",_dgaa );if (len (_dgaa )> 4)&&(_dgaa [:5]=="\u0066\u0061\u006cs\u0065"){_aaf ,_fdgb :=_bgcc .parseBool ();if _fdgb !=nil {return nil ,_fdgb ;};_eaf .Append (_aaf );}else if (len (_dgaa )> 3)&&(_dgaa [:4]=="\u0074\u0072\u0075\u0065"){_ffgd ,_fda :=_bgcc .parseBool ();if _fda !=nil {return nil ,_fda ;};_eaf .Append (_ffgd );}else {_gaef ,_fedb :=_bgcc .parseOperand ();if _fedb !=nil {return nil ,_fedb ;};_eaf .Append (_gaef );};};};return _eaf ,nil ;};func (_fgdd *PSOperand )ceiling (_bee *PSStack )error {_dec ,_eg :=_bee .Pop ();if _eg !=nil {return _eg ;};if _bfd ,_bfb :=_dec .(*PSReal );_bfb {_eg =_bee .Push (MakeReal (_bb .Ceil (_bfd .Val )));}else if _cfc ,_fag :=_dec .(*PSInteger );_fag {_eg =_bee .Push (MakeInteger (_cfc .Val ));}else {_eg =ErrTypeCheck ;};return _eg ;};func (_ccge *PSOperand )cos (_eff *PSStack )error {_cca ,_gae :=_eff .PopNumberAsFloat64 ();if _gae !=nil {return _gae ;};_aeg :=_bb .Cos (_cca *_bb .Pi /180.0);_gae =_eff .Push (MakeReal (_aeg ));return _gae ;};
|
||||
|
||||
// Exec executes the program, typically leaving output values on the stack.
|
||||
func (_cg *PSProgram )Exec (stack *PSStack )error {for _ ,_baa :=range *_cg {var _bac error ;switch _ge :=_baa .(type ){case *PSInteger :_ee :=_ge ;_bac =stack .Push (_ee );case *PSReal :_fgd :=_ge ;_bac =stack .Push (_fgd );case *PSBoolean :_cbg :=_ge ;_bac =stack .Push (_cbg );case *PSProgram :_geg :=_ge ;_bac =stack .Push (_geg );case *PSOperand :_ce :=_ge ;_bac =_ce .Exec (stack );default:return ErrTypeCheck ;};if _bac !=nil {return _bac ;};};return nil ;};func (_aebg *PSOperand )xor (_ddeg *PSStack )error {_dcb ,_afec :=_ddeg .Pop ();if _afec !=nil {return _afec ;};_fcbf ,_afec :=_ddeg .Pop ();if _afec !=nil {return _afec ;};if _effb ,_efd :=_dcb .(*PSBoolean );_efd {_ebfd ,_acf :=_fcbf .(*PSBoolean );if !_acf {return ErrTypeCheck ;};_afec =_ddeg .Push (MakeBool (_effb .Val !=_ebfd .Val ));return _afec ;};if _ddcb ,_cga :=_dcb .(*PSInteger );_cga {_bgfa ,_bbef :=_fcbf .(*PSInteger );if !_bbef {return ErrTypeCheck ;};_afec =_ddeg .Push (MakeInteger (_ddcb .Val ^_bgfa .Val ));return _afec ;};return ErrTypeCheck ;};func (_adbd *PSOperand )ge (_eab *PSStack )error {_bea ,_bgc :=_eab .PopNumberAsFloat64 ();if _bgc !=nil {return _bgc ;};_acgb ,_bgc :=_eab .PopNumberAsFloat64 ();if _bgc !=nil {return _bgc ;};if _bb .Abs (_acgb -_bea )< _dg {_gca :=_eab .Push (MakeBool (true ));return _gca ;}else if _acgb > _bea {_dbf :=_eab .Push (MakeBool (true ));return _dbf ;}else {_cbgf :=_eab .Push (MakeBool (false ));return _cbgf ;};};func (_dbe *PSOperand )ifCondition (_ceb *PSStack )error {_adde ,_dab :=_ceb .Pop ();if _dab !=nil {return _dab ;};_cfa ,_dab :=_ceb .Pop ();if _dab !=nil {return _dab ;};_fca ,_gda :=_adde .(*PSProgram );if !_gda {return ErrTypeCheck ;};_bdg ,_gda :=_cfa .(*PSBoolean );if !_gda {return ErrTypeCheck ;};if _bdg .Val {_ggf :=_fca .Exec (_ceb );return _ggf ;};return nil ;};func (_ddec *PSOperand )pop (_cea *PSStack )error {_ ,_fagb :=_cea .Pop ();if _fagb !=nil {return _fagb ;};return nil ;};func (_agg *PSOperand )div (_deed *PSStack )error {_ebb ,_efb :=_deed .Pop ();if _efb !=nil {return _efb ;};_badd ,_efb :=_deed .Pop ();if _efb !=nil {return _efb ;};_caa ,_ege :=_ebb .(*PSReal );_ecg ,_ebba :=_ebb .(*PSInteger );if !_ege &&!_ebba {return ErrTypeCheck ;};if _ege &&_caa .Val ==0{return ErrUndefinedResult ;};if _ebba &&_ecg .Val ==0{return ErrUndefinedResult ;};_cad ,_egb :=_badd .(*PSReal );_faf ,_cec :=_badd .(*PSInteger );if !_egb &&!_cec {return ErrTypeCheck ;};var _aeb float64 =0;if _egb {_aeb =_cad .Val ;}else {_aeb =float64 (_faf .Val );};if _ege {_aeb /=_caa .Val ;}else {_aeb /=float64 (_ecg .Val );};_efb =_deed .Push (MakeReal (_aeb ));return _efb ;};func (_bg *PSReal )String ()string {return _e .Sprintf ("\u0025\u002e\u0035\u0066",_bg .Val )};func (_cb *PSReal )DebugString ()string {return _e .Sprintf ("\u0072e\u0061\u006c\u003a\u0025\u002e\u0035f",_cb .Val );};
|
||||
|
||||
// NewPSExecutor returns an initialized PSExecutor for an input `program`.
|
||||
func NewPSExecutor (program *PSProgram )*PSExecutor {_dgf :=&PSExecutor {};_dgf .Stack =NewPSStack ();_dgf ._de =program ;return _dgf ;};func (_cefb *PSOperand )ifelse (_adcf *PSStack )error {_feb ,_gbee :=_adcf .Pop ();if _gbee !=nil {return _gbee ;};_ecgg ,_gbee :=_adcf .Pop ();if _gbee !=nil {return _gbee ;};_fea ,_gbee :=_adcf .Pop ();if _gbee !=nil {return _gbee ;};_gbc ,_gdf :=_feb .(*PSProgram );if !_gdf {return ErrTypeCheck ;};_afg ,_gdf :=_ecgg .(*PSProgram );if !_gdf {return ErrTypeCheck ;};_eeg ,_gdf :=_fea .(*PSBoolean );if !_gdf {return ErrTypeCheck ;};if _eeg .Val {_bba :=_afg .Exec (_adcf );return _bba ;};_gbee =_gbc .Exec (_adcf );return _gbee ;};func (_adf *PSOperand )index (_gfa *PSStack )error {_beab ,_egc :=_gfa .Pop ();if _egc !=nil {return _egc ;};_gga ,_bgcb :=_beab .(*PSInteger );if !_bgcb {return ErrTypeCheck ;};if _gga .Val < 0{return ErrRangeCheck ;};if _gga .Val > len (*_gfa )-1{return ErrStackUnderflow ;};_bgd :=(*_gfa )[len (*_gfa )-1-_gga .Val ];_egc =_gfa .Push (_bgd .Duplicate ());return _egc ;};func (_affa *PSParser )skipSpaces ()(int ,error ){_gec :=0;for {_gedg ,_bfg :=_affa ._acfg .Peek (1);if _bfg !=nil {return 0,_bfg ;};if _cd .IsWhiteSpace (_gedg [0]){_affa ._acfg .ReadByte ();_gec ++;}else {break ;};};return _gec ,nil ;};func (_aab *PSOperand )cvi (_dbd *PSStack )error {_acg ,_bfdf :=_dbd .Pop ();if _bfdf !=nil {return _bfdf ;};if _baae ,_adg :=_acg .(*PSReal );_adg {_gba :=int (_baae .Val );_bfdf =_dbd .Push (MakeInteger (_gba ));}else if _agb ,_afb :=_acg .(*PSInteger );_afb {_dbad :=_agb .Val ;_bfdf =_dbd .Push (MakeInteger (_dbad ));}else {return ErrTypeCheck ;};return _bfdf ;};
|
||||
func (_ffa *PSStack )DebugString ()string {_cag :="\u005b\u0020";for _ ,_bcafb :=range *_ffa {_cag +=_bcafb .DebugString ();_cag +="\u0020";};_cag +="\u005d";return _cag ;};
|
||||
|
||||
// PSObjectArrayToFloat64Array converts []PSObject into a []float64 array. Each PSObject must represent a number,
|
||||
// otherwise a ErrTypeCheck error occurs.
|
||||
func PSObjectArrayToFloat64Array (objects []PSObject )([]float64 ,error ){var _db []float64 ;for _ ,_dbc :=range objects {if _da ,_dc :=_dbc .(*PSInteger );_dc {_db =append (_db ,float64 (_da .Val ));}else if _cdd ,_bc :=_dbc .(*PSReal );_bc {_db =append (_db ,_cdd .Val );}else {return nil ,ErrTypeCheck ;};};return _db ,nil ;};func (_bce *PSInteger )DebugString ()string {return _e .Sprintf ("\u0069\u006e\u0074\u003a\u0025\u0064",_bce .Val );};
|
||||
|
||||
// Empty empties the stack.
|
||||
func (_eed *PSStack )Empty (){*_eed =[]PSObject {}};func (_ceda *PSOperand )neg (_egg *PSStack )error {_gbg ,_fdbf :=_egg .Pop ();if _fdbf !=nil {return _fdbf ;};if _fdgg ,_cbc :=_gbg .(*PSReal );_cbc {_fdbf =_egg .Push (MakeReal (-_fdgg .Val ));return _fdbf ;}else if _bgcf ,_edb :=_gbg .(*PSInteger );_edb {_fdbf =_egg .Push (MakeInteger (-_bgcf .Val ));return _fdbf ;}else {return ErrTypeCheck ;};};
|
||||
|
||||
// Execute executes the program for an input parameters `objects` and returns a slice of output objects.
|
||||
func (_ea *PSExecutor )Execute (objects []PSObject )([]PSObject ,error ){for _ ,_ac :=range objects {_be :=_ea .Stack .Push (_ac );if _be !=nil {return nil ,_be ;};};_fa :=_ea ._de .Exec (_ea .Stack );if _fa !=nil {_dfe .Log .Debug ("\u0045x\u0065c\u0020\u0066\u0061\u0069\u006c\u0065\u0064\u003a\u0020\u0025\u0076",_fa );return nil ,_fa ;};_fg :=[]PSObject (*_ea .Stack );_ea .Stack .Empty ();return _fg ,nil ;};func (_cddb *PSOperand )abs (_fgg *PSStack )error {_eef ,_fe :=_fgg .Pop ();if _fe !=nil {return _fe ;};if _baaf ,_ef :=_eef .(*PSReal );_ef {_fc :=_baaf .Val ;if _fc < 0{_fe =_fgg .Push (MakeReal (-_fc ));}else {_fe =_fgg .Push (MakeReal (_fc ));};}else if _dfeg ,_bd :=_eef .(*PSInteger );_bd {_dcc :=_dfeg .Val ;if _dcc < 0{_fe =_fgg .Push (MakeInteger (-_dcc ));}else {_fe =_fgg .Push (MakeInteger (_dcc ));};}else {return ErrTypeCheck ;};return _fe ;};func (_egd *PSParser )parseOperand ()(*PSOperand ,error ){var _cdc []byte ;for {_cee ,_afd :=_egd ._acfg .Peek (1);if _afd !=nil {if _afd ==_b .EOF {break ;};return nil ,_afd ;};if _cd .IsDelimiter (_cee [0]){break ;};if _cd .IsWhiteSpace (_cee [0]){break ;};_aec ,_ :=_egd ._acfg .ReadByte ();_cdc =append (_cdc ,_aec );};if len (_cdc )==0{return nil ,_d .New ("\u0069\u006e\u0076al\u0069\u0064\u0020\u006f\u0070\u0065\u0072\u0061\u006e\u0064\u0020\u0028\u0065\u006d\u0070\u0074\u0079\u0029");};return MakeOperand (string (_cdc )),nil ;};func (_bcec *PSOperand )sub (_bfca *PSStack )error {_baf ,_fce :=_bfca .Pop ();if _fce !=nil {return _fce ;};_fafa ,_fce :=_bfca .Pop ();if _fce !=nil {return _fce ;};_afee ,_dce :=_baf .(*PSReal );_eddf ,_gab :=_baf .(*PSInteger );if !_dce &&!_gab {return ErrTypeCheck ;};_dcdd ,_cege :=_fafa .(*PSReal );_adge ,_edc :=_fafa .(*PSInteger );if !_cege &&!_edc {return ErrTypeCheck ;};if _gab &&_edc {_eabg :=_adge .Val -_eddf .Val ;_dbdc :=_bfca .Push (MakeInteger (_eabg ));return _dbdc ;};var _gedd float64 =0;if _cege {_gedd =_dcdd .Val ;}else {_gedd =float64 (_adge .Val );};if _dce {_gedd -=_afee .Val ;}else {_gedd -=float64 (_eddf .Val );};_fce =_bfca .Push (MakeReal (_gedd ));return _fce ;};
|
||||
func PSObjectArrayToFloat64Array (objects []PSObject )([]float64 ,error ){var _bbe []float64 ;for _ ,_de :=range objects {if _bea ,_db :=_de .(*PSInteger );_db {_bbe =append (_bbe ,float64 (_bea .Val ));}else if _aa ,_ba :=_de .(*PSReal );_ba {_bbe =append (_bbe ,_aa .Val );}else {return nil ,ErrTypeCheck ;};};return _bbe ,nil ;};func (_fdcb *PSOperand )not (_fade *PSStack )error {_cfg ,_gaced :=_fade .Pop ();if _gaced !=nil {return _gaced ;};if _eca ,_ecdg :=_cfg .(*PSBoolean );_ecdg {_gaced =_fade .Push (MakeBool (!_eca .Val ));return _gaced ;}else if _bec ,_adca :=_cfg .(*PSInteger );_adca {_gaced =_fade .Push (MakeInteger (^_bec .Val ));return _gaced ;}else {return ErrTypeCheck ;};};func (_fad *PSOperand )dup (_bbg *PSStack )error {_fca ,_acc :=_bbg .Pop ();if _acc !=nil {return _acc ;};_acc =_bbg .Push (_fca );if _acc !=nil {return _acc ;};_acc =_bbg .Push (_fca .Duplicate ());return _acc ;};var ErrRangeCheck =_b .New ("\u0072\u0061\u006e\u0067\u0065\u0020\u0063\u0068\u0065\u0063\u006b\u0020e\u0072\u0072\u006f\u0072");func (_cdgg *PSProgram )Duplicate ()PSObject {_bf :=&PSProgram {};for _ ,_ec :=range *_cdgg {_bf .Append (_ec .Duplicate ());};return _bf ;};var ErrStackUnderflow =_b .New ("\u0073t\u0061c\u006b\u0020\u0075\u006e\u0064\u0065\u0072\u0066\u006c\u006f\u0077");
|
||||
|
||||
// String returns a string representation of the stack.
|
||||
func (_cgaf *PSStack )String ()string {_beff :="\u005b\u0020";for _ ,_faag :=range *_cgaf {_beff +=_faag .String ();_beff +="\u0020";};_beff +="\u005d";return _beff ;};func (_cfe *PSProgram )Duplicate ()PSObject {_faa :=&PSProgram {};for _ ,_ga :=range *_cfe {_faa .Append (_ga .Duplicate ());};return _faa ;};
|
||||
|
||||
// PSParser is a basic Postscript parser.
|
||||
type PSParser struct{_acfg *_df .Reader };func (_ad *PSBoolean )String ()string {return _e .Sprintf ("\u0025\u0076",_ad .Val )};
|
||||
|
||||
// MakeReal returns a new PSReal object initialized with `val`.
|
||||
func MakeReal (val float64 )*PSReal {_gbdd :=PSReal {};_gbdd .Val =val ;return &_gbdd };func (_afa *PSOperand )truncate (_gebg *PSStack )error {_bgb ,_fac :=_gebg .Pop ();if _fac !=nil {return _fac ;};if _dgda ,_dbgb :=_bgb .(*PSReal );_dbgb {_bae :=int (_dgda .Val );_fac =_gebg .Push (MakeReal (float64 (_bae )));}else if _acde ,_bggg :=_bgb .(*PSInteger );_bggg {_fac =_gebg .Push (MakeInteger (_acde .Val ));}else {return ErrTypeCheck ;};return _fac ;};
|
||||
|
||||
// MakeBool returns a new PSBoolean object initialized with `val`.
|
||||
func MakeBool (val bool )*PSBoolean {_ecbg :=PSBoolean {};_ecbg .Val =val ;return &_ecbg };
|
||||
|
||||
// Push pushes an object on top of the stack.
|
||||
func (_aege *PSStack )Push (obj PSObject )error {if len (*_aege )> 100{return ErrStackOverflow ;};*_aege =append (*_aege ,obj );return nil ;};
|
||||
|
||||
// Append appends an object to the PSProgram.
|
||||
func (_dd *PSProgram )Append (obj PSObject ){*_dd =append (*_dd ,obj )};func (_fcg *PSOperand )and (_aff *PSStack )error {_ece ,_cbfg :=_aff .Pop ();if _cbfg !=nil {return _cbfg ;};_bgg ,_cbfg :=_aff .Pop ();if _cbfg !=nil {return _cbfg ;};if _eae ,_bfe :=_ece .(*PSBoolean );_bfe {_gbe ,_adb :=_bgg .(*PSBoolean );if !_adb {return ErrTypeCheck ;};_cbfg =_aff .Push (MakeBool (_eae .Val &&_gbe .Val ));return _cbfg ;};if _gdd ,_ffe :=_ece .(*PSInteger );_ffe {_ddc ,_def :=_bgg .(*PSInteger );if !_def {return ErrTypeCheck ;};_cbfg =_aff .Push (MakeInteger (_gdd .Val &_ddc .Val ));return _cbfg ;};return ErrTypeCheck ;};
|
||||
|
||||
// MakeOperand returns a new PSOperand object based on string `val`.
|
||||
func MakeOperand (val string )*PSOperand {_deacf :=PSOperand (val );return &_deacf };var ErrUnsupportedOperand =_d .New ("\u0075\u006e\u0073\u0075pp\u006f\u0072\u0074\u0065\u0064\u0020\u006f\u0070\u0065\u0072\u0061\u006e\u0064");func (_bec *PSOperand )add (_daf *PSStack )error {_adc ,_beg :=_daf .Pop ();if _beg !=nil {return _beg ;};_eca ,_beg :=_daf .Pop ();if _beg !=nil {return _beg ;};_gag ,_ccb :=_adc .(*PSReal );_ccg ,_geb :=_adc .(*PSInteger );if !_ccb &&!_geb {return ErrTypeCheck ;};_ccbf ,_fcb :=_eca .(*PSReal );_cgb ,_cde :=_eca .(*PSInteger );if !_fcb &&!_cde {return ErrTypeCheck ;};if _geb &&_cde {_fgc :=_ccg .Val +_cgb .Val ;_dfa :=_daf .Push (MakeInteger (_fgc ));return _dfa ;};var _cbf float64 =0;if _ccb {_cbf =_gag .Val ;}else {_cbf =float64 (_ccg .Val );};if _fcb {_cbf +=_ccbf .Val ;}else {_cbf +=float64 (_cgb .Val );};_beg =_daf .Push (MakeReal (_cbf ));return _beg ;};
|
||||
func (_baga *PSStack )String ()string {_effc :="\u005b\u0020";for _ ,_fcaa :=range *_baga {_effc +=_fcaa .String ();_effc +="\u0020";};_effc +="\u005d";return _effc ;};func (_cbdb *PSParser )parseOperand ()(*PSOperand ,error ){var _gebg []byte ;for {_cdc ,_gcff :=_cbdb ._bddg .Peek (1);if _gcff !=nil {if _gcff ==_d .EOF {break ;};return nil ,_gcff ;};if _a .IsDelimiter (_cdc [0]){break ;};if _a .IsWhiteSpace (_cdc [0]){break ;};_gfc ,_ :=_cbdb ._bddg .ReadByte ();_gebg =append (_gebg ,_gfc );};if len (_gebg )==0{return nil ,_b .New ("\u0069\u006e\u0076al\u0069\u0064\u0020\u006f\u0070\u0065\u0072\u0061\u006e\u0064\u0020\u0028\u0065\u006d\u0070\u0074\u0079\u0029");};return MakeOperand (string (_gebg )),nil ;};
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user