mirror of
https://github.com/unidoc/unipdf.git
synced 2025-04-27 13:48:51 +08:00
Merge pull request #414 from unidoc-build/prep-rc-v3.12.1
Prepare release of UniPDF v3.12.1
This commit is contained in:
commit
6ea4d7271f
File diff suppressed because one or more lines are too long
140
common/common.go
140
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 (_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{}){};
|
||||
|
||||
// Trace does nothing for dummy logger.
|
||||
func (DummyLogger )Trace (format string ,args ...interface{}){};
|
||||
|
||||
// Trace logs trace message.
|
||||
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 ...);};
|
||||
|
||||
// SetLogger sets 'logger' to be used by the unidoc unipdf library.
|
||||
func SetLogger (logger Logger ){Log =logger };
|
||||
package common ;import (_e "fmt";_ec "io";_gb "os";_g "path/filepath";_ab "runtime";_a "time";);
|
||||
|
||||
// DummyLogger does nothing.
|
||||
type DummyLogger struct{};
|
||||
|
||||
// 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";
|
||||
|
||||
// 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 (_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 (_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 (_fg ConsoleLogger )IsLogLevel (level LogLevel )bool {return _fg .LogLevel >=level };const _cac =2020;
|
||||
func (_dg ConsoleLogger )IsLogLevel (level LogLevel )bool {return _dg .LogLevel >=level };func UtcTimeFormat (t _a .Time )string {return t .Format (_ccc )+"\u0020\u0055\u0054\u0043"};const _beg =9;const Version ="\u0033\u002e\u0031\u0032\u002e\u0031";
|
||||
|
||||
// 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 {};
|
||||
|
||||
// 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 (_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{}){};
|
||||
// Error logs error message.
|
||||
func (_fb ConsoleLogger )Error (format string ,args ...interface{}){if _fb .LogLevel >=LogLevelError {_ag :="\u005b\u0045\u0052\u0052\u004f\u0052\u005d\u0020";_fb .output (_gb .Stdout ,_ag ,format ,args ...);};};
|
||||
|
||||
// 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 );
|
||||
func NewConsoleLogger (logLevel LogLevel )*ConsoleLogger {return &ConsoleLogger {LogLevel :logLevel }};
|
||||
|
||||
// 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 does nothing for dummy logger.
|
||||
func (DummyLogger )Warning (format string ,args ...interface{}){};
|
||||
|
||||
// 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 ...);};};
|
||||
// Info logs info message.
|
||||
func (_ca ConsoleLogger )Info (format string ,args ...interface{}){if _ca .LogLevel >=LogLevelInfo {_abfg :="\u005bI\u004e\u0046\u004f\u005d\u0020";_ca .output (_gb .Stdout ,_abfg ,format ,args ...);};};var ReleasedAt =_a .Date (_abc ,_beg ,_eeef ,_ce ,_eb ,0,0,_a .UTC );
|
||||
|
||||
// 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 ...);};};
|
||||
// IsLogLevel returns true if log level is greater or equal than `level`.
|
||||
// Can be used to avoid resource intensive calls to loggers.
|
||||
func (_bb WriterLogger )IsLogLevel (level LogLevel )bool {return _bb .LogLevel >=level };func (_aa ConsoleLogger )output (_bd _ec .Writer ,_fa string ,_eee string ,_bga ...interface{}){_eed (_bd ,_fa ,_eee ,_bga ...);};
|
||||
|
||||
// Notice logs notice message.
|
||||
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 ...);};};
|
||||
// LogLevel is the verbosity level for logging.
|
||||
type LogLevel int ;var Log Logger =DummyLogger {};func _eed (_cc _ec .Writer ,_dbg string ,_dcd string ,_dca ...interface{}){_ ,_be ,_bef ,_dce :=_ab .Caller (3);if !_dce {_be ="\u003f\u003f\u003f";_bef =0;}else {_be =_g .Base (_be );};_gdf :=_e .Sprintf ("\u0025s\u0020\u0025\u0073\u003a\u0025\u0064 ",_dbg ,_be ,_bef )+_dcd +"\u000a";_e .Fprintf (_cc ,_gdf ,_dca ...);};
|
||||
|
||||
// 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 ...);};};
|
||||
func (DummyLogger )Notice (format string ,args ...interface{}){};
|
||||
|
||||
// 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 ...);};};
|
||||
func (_bcg WriterLogger )Error (format string ,args ...interface{}){if _bcg .LogLevel >=LogLevelError {_fce :="\u005b\u0045\u0052\u0052\u004f\u0052\u005d\u0020";_bcg .logToWriter (_bcg .Output ,_fce ,format ,args ...);};};
|
||||
|
||||
// Warning logs warning message.
|
||||
func (_ac ConsoleLogger )Warning (format string ,args ...interface{}){if _ac .LogLevel >=LogLevelWarning {_ba :="\u005b\u0057\u0041\u0052\u004e\u0049\u004e\u0047\u005d\u0020";_ac .output (_gb .Stdout ,_ba ,format ,args ...);};};
|
||||
|
||||
// Notice logs notice message.
|
||||
func (_bab WriterLogger )Notice (format string ,args ...interface{}){if _bab .LogLevel >=LogLevelNotice {_eca :="\u005bN\u004f\u0054\u0049\u0043\u0045\u005d ";_bab .logToWriter (_bab .Output ,_eca ,format ,args ...);};};
|
||||
|
||||
// Warning logs warning message.
|
||||
func (_ecg WriterLogger )Warning (format string ,args ...interface{}){if _ecg .LogLevel >=LogLevelWarning {_caa :="\u005b\u0057\u0041\u0052\u004e\u0049\u004e\u0047\u005d\u0020";_ecg .logToWriter (_ecg .Output ,_caa ,format ,args ...);};};const _eeef =28;
|
||||
|
||||
// Logger is the interface used for logging in the unipdf package.
|
||||
type Logger interface{Error (_ed string ,_ad ...interface{});Warning (_ee string ,_ef ...interface{});Notice (_gg string ,_c ...interface{});Info (_abf string ,_b ...interface{});Debug (_cd string ,_d ...interface{});Trace (_bc string ,_fc ...interface{});IsLogLevel (_edf LogLevel )bool ;};
|
||||
|
||||
// Trace logs trace message.
|
||||
func (_geg WriterLogger )Trace (format string ,args ...interface{}){if _geg .LogLevel >=LogLevelTrace {_gf :="\u005b\u0054\u0052\u0041\u0043\u0045\u005d\u0020";_geg .logToWriter (_geg .Output ,_gf ,format ,args ...);};};
|
||||
|
||||
// IsLogLevel returns true from dummy logger.
|
||||
func (DummyLogger )IsLogLevel (level LogLevel )bool {return true };
|
||||
|
||||
// Notice logs notice message.
|
||||
func (_da ConsoleLogger )Notice (format string ,args ...interface{}){if _da .LogLevel >=LogLevelNotice {_gc :="\u005bN\u004f\u0054\u0049\u0043\u0045\u005d ";_da .output (_gb .Stdout ,_gc ,format ,args ...);};};const _abc =2020;
|
||||
|
||||
// Info logs info message.
|
||||
func (_dc WriterLogger )Info (format string ,args ...interface{}){if _dc .LogLevel >=LogLevelInfo {_bdc :="\u005bI\u004e\u0046\u004f\u005d\u0020";_dc .logToWriter (_dc .Output ,_bdc ,format ,args ...);};};const _ccc ="\u0032\u0020\u004aan\u0075\u0061\u0072\u0079\u0020\u0032\u0030\u0030\u0036\u0020\u0061\u0074\u0020\u0031\u0035\u003a\u0030\u0034";
|
||||
|
||||
// Debug logs debug message.
|
||||
func (_fe ConsoleLogger )Debug (format string ,args ...interface{}){if _fe .LogLevel >=LogLevelDebug {_bg :="\u005b\u0044\u0045\u0042\u0055\u0047\u005d\u0020";_fe .output (_gb .Stdout ,_bg ,format ,args ...);};};
|
||||
|
||||
// Debug does nothing for dummy logger.
|
||||
func (DummyLogger )Debug (format string ,args ...interface{}){};const _ce =21;
|
||||
|
||||
// Debug logs debug message.
|
||||
func (_ge WriterLogger )Debug (format string ,args ...interface{}){if _ge .LogLevel >=LogLevelDebug {_dgg :="\u005b\u0044\u0045\u0042\u0055\u0047\u005d\u0020";_ge .logToWriter (_ge .Output ,_dgg ,format ,args ...);};};
|
||||
|
||||
// Trace logs trace message.
|
||||
func (_gd ConsoleLogger )Trace (format string ,args ...interface{}){if _gd .LogLevel >=LogLevelTrace {_af :="\u005b\u0054\u0052\u0041\u0043\u0045\u005d\u0020";_gd .output (_gb .Stdout ,_af ,format ,args ...);};};func (_gdb WriterLogger )logToWriter (_fg _ec .Writer ,_dcf string ,_db string ,_cae ...interface{}){_eed (_fg ,_dcf ,_db ,_cae );};
|
||||
|
||||
// Trace does nothing for dummy logger.
|
||||
func (DummyLogger )Trace (format string ,args ...interface{}){};
|
||||
|
||||
// Info does nothing for dummy logger.
|
||||
func (DummyLogger )Info (format string ,args ...interface{}){};
|
||||
|
||||
// NewWriterLogger creates new 'writer' logger.
|
||||
func NewWriterLogger (logLevel LogLevel ,writer _ec .Writer )*WriterLogger {_aba :=WriterLogger {Output :writer ,LogLevel :logLevel };return &_aba ;};
|
||||
|
||||
// SetLogger sets 'logger' to be used by the unidoc unipdf library.
|
||||
func SetLogger (logger Logger ){Log =logger };const _eb =50;
|
||||
|
||||
// WriterLogger is the logger that writes data to the Output writer
|
||||
type WriterLogger struct{LogLevel LogLevel ;Output _ec .Writer ;};
|
||||
|
||||
// Error does nothing for dummy logger.
|
||||
func (DummyLogger )Error (format string ,args ...interface{}){};const (LogLevelTrace LogLevel =5;LogLevelDebug LogLevel =4;LogLevelInfo LogLevel =3;LogLevelNotice LogLevel =2;LogLevelWarning LogLevel =1;LogLevelError LogLevel =0;);
|
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 (_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";);
|
||||
package draw ;import (_g "fmt";_c "github.com/unidoc/unipdf/v3/contentstream";_f "github.com/unidoc/unipdf/v3/core";_e "github.com/unidoc/unipdf/v3/internal/transform";_bg "github.com/unidoc/unipdf/v3/model";_b "math";);
|
||||
|
||||
// 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 ;};
|
||||
// ToPdfRectangle returns the bounding box as a PDF rectangle.
|
||||
func (_ggd BoundingBox )ToPdfRectangle ()*_bg .PdfRectangle {return &_bg .PdfRectangle {Llx :_ggd .X ,Lly :_ggd .Y ,Urx :_ggd .X +_ggd .Width ,Ury :_ggd .Y +_ggd .Height };};
|
||||
|
||||
// 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 (_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 ;};
|
||||
|
||||
// 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 ;};
|
||||
|
||||
// Scale scales the vector by the specified factor.
|
||||
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 (_ca CubicBezierPath )AppendCurve (curve CubicBezierCurve )CubicBezierPath {_ca .Curves =append (_ca .Curves ,curve );return _ca ;};
|
||||
|
||||
// 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 );};
|
||||
|
||||
// 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))};
|
||||
// RemovePoint removes the point at the index specified by number from the
|
||||
// path. The index is 1-based.
|
||||
func (_gd Path )RemovePoint (number int )Path {if number < 1||number > len (_gd .Points ){return _gd ;};_cbe :=number -1;_gd .Points =append (_gd .Points [:_cbe ],_gd .Points [_cbe +1:]...);return _gd ;};
|
||||
|
||||
// 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 (_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 ;};
|
||||
func (_aef BasicLine )Draw (gsName string )([]byte ,*_bg .PdfRectangle ,error ){_cfbe :=_aef .LineWidth ;_abbg :=NewPath ();_abbg =_abbg .AppendPoint (NewPoint (_aef .X1 ,_aef .Y1 ));_abbg =_abbg .AppendPoint (NewPoint (_aef .X2 ,_aef .Y2 ));_acd :=_c .NewContentCreator ();_dcg :=_abbg .GetBoundingBox ();DrawPathWithCreator (_abbg ,_acd );if _aef .LineStyle ==LineStyleDashed {_acd .Add_d ([]int64 {1,1},0);};_acd .Add_RG (_aef .LineColor .R (),_aef .LineColor .G (),_aef .LineColor .B ()).Add_w (_cfbe ).Add_S ().Add_Q ();return _acd .Bytes (),_dcg .ToPdfRectangle (),nil ;};
|
||||
|
||||
// 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 )};
|
||||
// Path consists of straight line connections between each point defined in an array of points.
|
||||
type Path struct{Points []Point ;};
|
||||
|
||||
// NewPoint returns a new point with the coordinates x, y.
|
||||
func NewPoint (x ,y float64 )Point {return Point {X :x ,Y :y }};
|
||||
|
||||
// ToPdfRectangle returns the rectangle as a PDF rectangle.
|
||||
func (_cda Rectangle )ToPdfRectangle ()*_bg .PdfRectangle {return &_bg .PdfRectangle {Llx :_cda .X ,Lly :_cda .Y ,Urx :_cda .X +_cda .Width ,Ury :_cda .Y +_cda .Height };};const (LineStyleSolid LineStyle =0;LineStyleDashed LineStyle =1;);
|
||||
|
||||
// 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 ;};
|
||||
|
||||
// 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 ;};
|
||||
|
||||
// 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 ;};
|
||||
|
||||
// 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 ;};
|
||||
// Add shifts the coordinates of the point with dx, dy and returns the result.
|
||||
func (_gda Point )Add (dx ,dy float64 )Point {_gda .X +=dx ;_gda .Y +=dy ;return _gda };
|
||||
|
||||
// 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 ;};
|
||||
func (_gdaf PolyBezierCurve )Draw (gsName string )([]byte ,*_bg .PdfRectangle ,error ){if _gdaf .BorderColor ==nil {_gdaf .BorderColor =_bg .NewPdfColorDeviceRGB (0,0,0);};_fbd :=NewCubicBezierPath ();for _ ,_ag :=range _gdaf .Curves {_fbd =_fbd .AppendCurve (_ag );};_ga :=_c .NewContentCreator ();_ga .Add_q ();_gdaf .FillEnabled =_gdaf .FillEnabled &&_gdaf .FillColor !=nil ;if _gdaf .FillEnabled {_ga .Add_rg (_gdaf .FillColor .R (),_gdaf .FillColor .G (),_gdaf .FillColor .B ());};_ga .Add_RG (_gdaf .BorderColor .R (),_gdaf .BorderColor .G (),_gdaf .BorderColor .B ());_ga .Add_w (_gdaf .BorderWidth );if len (gsName )> 1{_ga .Add_gs (_f .PdfObjectName (gsName ));};for _ ,_cdd :=range _fbd .Curves {_ga .Add_m (_cdd .P0 .X ,_cdd .P0 .Y );_ga .Add_c (_cdd .P1 .X ,_cdd .P1 .Y ,_cdd .P2 .X ,_cdd .P2 .Y ,_cdd .P3 .X ,_cdd .P3 .Y );};if _gdaf .FillEnabled {_ga .Add_h ();_ga .Add_B ();}else {_ga .Add_S ();};_ga .Add_Q ();return _ga .Bytes (),_fbd .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 };
|
||||
// NewVector returns a new vector with the direction specified by dx and dy.
|
||||
func NewVector (dx ,dy float64 )Vector {_cgg :=Vector {};_cgg .Dx =dx ;_cgg .Dy =dy ;return _cgg };
|
||||
|
||||
// Copy returns a clone of the path.
|
||||
func (_be Path )Copy ()Path {_de :=Path {};_de .Points =[]Point {};for _ ,_dge :=range _be .Points {_de .Points =append (_de .Points ,_dge );};return _de ;};
|
||||
|
||||
// Rotate returns a new Point at `p` rotated by `theta` degrees.
|
||||
func (_eed Point )Rotate (theta float64 )Point {_aeb :=_e .NewPoint (_eed .X ,_eed .Y ).Rotate (theta );return NewPoint (_aeb .X ,_aeb .Y );};
|
||||
|
||||
// NewPath returns a new empty path.
|
||||
func NewPath ()Path {return Path {}};
|
||||
|
||||
// Copy returns a clone of the Bezier path.
|
||||
func (_ee CubicBezierPath )Copy ()CubicBezierPath {_dg :=CubicBezierPath {};_dg .Curves =[]CubicBezierCurve {};for _ ,_abb :=range _ee .Curves {_dg .Curves =append (_dg .Curves ,_abb );};return _dg ;};
|
||||
|
||||
// NewCubicBezierCurve returns a new cubic Bezier curve.
|
||||
func NewCubicBezierCurve (x0 ,y0 ,x1 ,y1 ,x2 ,y2 ,x3 ,y3 float64 )CubicBezierCurve {_fa :=CubicBezierCurve {};_fa .P0 =NewPoint (x0 ,y0 );_fa .P1 =NewPoint (x1 ,y1 );_fa .P2 =NewPoint (x2 ,y2 );_fa .P3 =NewPoint (x3 ,y3 );return _fa ;};
|
||||
|
||||
// PolyBezierCurve represents a composite curve that is the result of
|
||||
// joining multiple cubic Bezier curves.
|
||||
type PolyBezierCurve struct{Curves []CubicBezierCurve ;BorderWidth float64 ;BorderColor *_bg .PdfColorDeviceRGB ;FillEnabled bool ;FillColor *_bg .PdfColorDeviceRGB ;};
|
||||
|
||||
// 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 *_bg .PdfColorDeviceRGB ;BorderEnabled bool ;BorderWidth float64 ;BorderColor *_bg .PdfColorDeviceRGB ;Opacity float64 ;};
|
||||
|
||||
// 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 ;};
|
||||
func NewVectorBetween (a Point ,b Point )Vector {_gdeg :=Vector {};_gdeg .Dx =b .X -a .X ;_gdeg .Dy =b .Y -a .Y ;return _gdeg ;};
|
||||
|
||||
// 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 ;};
|
||||
// AppendPoint adds the specified point to the path.
|
||||
func (_cd Path )AppendPoint (point Point )Path {_cd .Points =append (_cd .Points ,point );return _cd };
|
||||
|
||||
// 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 ;};
|
||||
// FlipX flips the sign of the Dx component of the vector.
|
||||
func (_cdae Vector )FlipX ()Vector {_cdae .Dx =-_cdae .Dx ;return _cdae };
|
||||
|
||||
// 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 ;};
|
||||
// Scale scales the vector by the specified factor.
|
||||
func (_fe Vector )Scale (factor float64 )Vector {_ggde :=_fe .Magnitude ();_eda :=_fe .GetPolarAngle ();_fe .Dx =factor *_ggde *_b .Cos (_eda );_fe .Dy =factor *_ggde *_b .Sin (_eda );return _fe ;};
|
||||
|
||||
// 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 ;};
|
||||
// AddVector adds vector to a point.
|
||||
func (_eeg Point )AddVector (v Vector )Point {_eeg .X +=v .Dx ;_eeg .Y +=v .Dy ;return _eeg };
|
||||
|
||||
// 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 (_gbc Polygon )Draw (gsName string )([]byte ,*_bg .PdfRectangle ,error ){_beb :=_c .NewContentCreator ();_beb .Add_q ();_gbc .FillEnabled =_gbc .FillEnabled &&_gbc .FillColor !=nil ;if _gbc .FillEnabled {_beb .Add_rg (_gbc .FillColor .R (),_gbc .FillColor .G (),_gbc .FillColor .B ());};_gbc .BorderEnabled =_gbc .BorderEnabled &&_gbc .BorderColor !=nil ;if _gbc .BorderEnabled {_beb .Add_RG (_gbc .BorderColor .R (),_gbc .BorderColor .G (),_gbc .BorderColor .B ());_beb .Add_w (_gbc .BorderWidth );};if len (gsName )> 1{_beb .Add_gs (_f .PdfObjectName (gsName ));};_fgd :=NewPath ();for _ ,_bcb :=range _gbc .Points {for _gag ,_edb :=range _bcb {_fgd =_fgd .AppendPoint (_edb );if _gag ==0{_beb .Add_m (_edb .X ,_edb .Y );}else {_beb .Add_l (_edb .X ,_edb .Y );};};_beb .Add_h ();};if _gbc .FillEnabled &&_gbc .BorderEnabled {_beb .Add_B ();}else if _gbc .FillEnabled {_beb .Add_f ();}else if _gbc .BorderEnabled {_beb .Add_S ();};_beb .Add_Q ();return _beb .Bytes (),_fgd .GetBoundingBox ().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 (_gdac Line )Draw (gsName string )([]byte ,*_bg .PdfRectangle ,error ){_aga ,_dfa :=_gdac .X1 ,_gdac .X2 ;_gaa ,_cf :=_gdac .Y1 ,_gdac .Y2 ;_gff :=_cf -_gaa ;_bga :=_dfa -_aga ;_dba :=_b .Atan2 (_gff ,_bga );L :=_b .Sqrt (_b .Pow (_bga ,2.0)+_b .Pow (_gff ,2.0));_edg :=_gdac .LineWidth ;_aa :=_b .Pi ;_ef :=1.0;if _bga < 0{_ef *=-1.0;};if _gff < 0{_ef *=-1.0;};VsX :=_ef *(-_edg /2*_b .Cos (_dba +_aa /2));VsY :=_ef *(-_edg /2*_b .Sin (_dba +_aa /2)+_edg *_b .Sin (_dba +_aa /2));V1X :=VsX +_edg /2*_b .Cos (_dba +_aa /2);V1Y :=VsY +_edg /2*_b .Sin (_dba +_aa /2);V2X :=VsX +_edg /2*_b .Cos (_dba +_aa /2)+L *_b .Cos (_dba );V2Y :=VsY +_edg /2*_b .Sin (_dba +_aa /2)+L *_b .Sin (_dba );V3X :=VsX +_edg /2*_b .Cos (_dba +_aa /2)+L *_b .Cos (_dba )+_edg *_b .Cos (_dba -_aa /2);V3Y :=VsY +_edg /2*_b .Sin (_dba +_aa /2)+L *_b .Sin (_dba )+_edg *_b .Sin (_dba -_aa /2);V4X :=VsX +_edg /2*_b .Cos (_dba -_aa /2);V4Y :=VsY +_edg /2*_b .Sin (_dba -_aa /2);_fba :=NewPath ();_fba =_fba .AppendPoint (NewPoint (V1X ,V1Y ));_fba =_fba .AppendPoint (NewPoint (V2X ,V2Y ));_fba =_fba .AppendPoint (NewPoint (V3X ,V3Y ));_fba =_fba .AppendPoint (NewPoint (V4X ,V4Y ));_ba :=_gdac .LineEndingStyle1 ;_cfb :=_gdac .LineEndingStyle2 ;_bea :=3*_edg ;_gc :=3*_edg ;_gba :=(_gc -_edg )/2;if _cfb ==LineEndingStyleArrow {_gea :=_fba .GetPointNumber (2);_bf :=NewVectorPolar (_bea ,_dba +_aa );_ffa :=_gea .AddVector (_bf );_cg :=NewVectorPolar (_gc /2,_dba +_aa /2);_fge :=NewVectorPolar (_bea ,_dba );_dbd :=NewVectorPolar (_gba ,_dba +_aa /2);_fac :=_ffa .AddVector (_dbd );_bgdd :=_fge .Add (_cg .Flip ());_afg :=_fac .AddVector (_bgdd );_gffa :=_cg .Scale (2).Flip ().Add (_bgdd .Flip ());_bfg :=_afg .AddVector (_gffa );_geb :=_ffa .AddVector (NewVectorPolar (_edg ,_dba -_aa /2));_gde :=NewPath ();_gde =_gde .AppendPoint (_fba .GetPointNumber (1));_gde =_gde .AppendPoint (_ffa );_gde =_gde .AppendPoint (_fac );_gde =_gde .AppendPoint (_afg );_gde =_gde .AppendPoint (_bfg );_gde =_gde .AppendPoint (_geb );_gde =_gde .AppendPoint (_fba .GetPointNumber (4));_fba =_gde ;};if _ba ==LineEndingStyleArrow {_cga :=_fba .GetPointNumber (1);_cba :=_fba .GetPointNumber (_fba .Length ());_bca :=NewVectorPolar (_edg /2,_dba +_aa +_aa /2);_abg :=_cga .AddVector (_bca );_beae :=NewVectorPolar (_bea ,_dba ).Add (NewVectorPolar (_gc /2,_dba +_aa /2));_bed :=_abg .AddVector (_beae );_caf :=NewVectorPolar (_gba ,_dba -_aa /2);_gaaa :=_bed .AddVector (_caf );_bgb :=NewVectorPolar (_bea ,_dba );_fd :=_cba .AddVector (_bgb );_ccb :=NewVectorPolar (_gba ,_dba +_aa +_aa /2);_dda :=_fd .AddVector (_ccb );_egde :=_abg ;_gaf :=NewPath ();_gaf =_gaf .AppendPoint (_abg );_gaf =_gaf .AppendPoint (_bed );_gaf =_gaf .AppendPoint (_gaaa );for _ ,_ffad :=range _fba .Points [1:len (_fba .Points )-1]{_gaf =_gaf .AppendPoint (_ffad );};_gaf =_gaf .AppendPoint (_fd );_gaf =_gaf .AppendPoint (_dda );_gaf =_gaf .AppendPoint (_egde );_fba =_gaf ;};_bgg :=_c .NewContentCreator ();_bgg .Add_q ().Add_rg (_gdac .LineColor .R (),_gdac .LineColor .G (),_gdac .LineColor .B ());if len (gsName )> 1{_bgg .Add_gs (_f .PdfObjectName (gsName ));};_fba =_fba .Offset (_gdac .X1 ,_gdac .Y1 );_fgc :=_fba .GetBoundingBox ();DrawPathWithCreator (_fba ,_bgg );if _gdac .LineStyle ==LineStyleDashed {_bgg .Add_d ([]int64 {1,1},0).Add_S ().Add_f ().Add_Q ();}else {_bgg .Add_f ().Add_Q ();};return _bgg .Bytes (),_fgc .ToPdfRectangle (),nil ;};
|
||||
|
||||
// GetBoundingBox returns the bounding box of the path.
|
||||
func (_acg Path )GetBoundingBox ()BoundingBox {_gbg :=BoundingBox {};_adf :=0.0;_fg :=0.0;_egd :=0.0;_deg :=0.0;for _ff ,_eac :=range _acg .Points {if _ff ==0{_adf =_eac .X ;_fg =_eac .X ;_egd =_eac .Y ;_deg =_eac .Y ;continue ;};if _eac .X < _adf {_adf =_eac .X ;};if _eac .X > _fg {_fg =_eac .X ;};if _eac .Y < _egd {_egd =_eac .Y ;};if _eac .Y > _deg {_deg =_eac .Y ;};};_gbg .X =_adf ;_gbg .Y =_egd ;_gbg .Width =_fg -_adf ;_gbg .Height =_deg -_egd ;return _gbg ;};
|
||||
|
||||
// Polygon is a multi-point shape that can be drawn to a PDF content stream.
|
||||
type Polygon struct{Points [][]Point ;FillEnabled bool ;FillColor *_bg .PdfColorDeviceRGB ;BorderEnabled bool ;BorderColor *_bg .PdfColorDeviceRGB ;BorderWidth float64 ;};
|
||||
|
||||
// NewCubicBezierPath returns a new empty cubic Bezier path.
|
||||
func NewCubicBezierPath ()CubicBezierPath {_ed :=CubicBezierPath {};_ed .Curves =[]CubicBezierCurve {};return _ed ;};
|
||||
|
||||
// Point represents a two-dimensional point.
|
||||
type Point struct{X float64 ;Y float64 ;};
|
||||
|
||||
// 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 };
|
||||
// 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 *_bg .PdfColorDeviceRGB ;Opacity float64 ;LineWidth float64 ;LineStyle LineStyle ;};
|
||||
|
||||
// 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 );};
|
||||
// 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 (_cc Circle )Draw (gsName string )([]byte ,*_bg .PdfRectangle ,error ){_da :=_cc .Width /2;_ge :=_cc .Height /2;if _cc .BorderEnabled {_da -=_cc .BorderWidth /2;_ge -=_cc .BorderWidth /2;};_fce :=0.551784;_faa :=_da *_fce ;_ggfg :=_ge *_fce ;_acgd :=NewCubicBezierPath ();_acgd =_acgd .AppendCurve (NewCubicBezierCurve (-_da ,0,-_da ,_ggfg ,-_faa ,_ge ,0,_ge ));_acgd =_acgd .AppendCurve (NewCubicBezierCurve (0,_ge ,_faa ,_ge ,_da ,_ggfg ,_da ,0));_acgd =_acgd .AppendCurve (NewCubicBezierCurve (_da ,0,_da ,-_ggfg ,_faa ,-_ge ,0,-_ge ));_acgd =_acgd .AppendCurve (NewCubicBezierCurve (0,-_ge ,-_faa ,-_ge ,-_da ,-_ggfg ,-_da ,0));_acgd =_acgd .Offset (_da ,_ge );if _cc .BorderEnabled {_acgd =_acgd .Offset (_cc .BorderWidth /2,_cc .BorderWidth /2);};if _cc .X !=0||_cc .Y !=0{_acgd =_acgd .Offset (_cc .X ,_cc .Y );};_dce :=_c .NewContentCreator ();_dce .Add_q ();if _cc .FillEnabled {_dce .Add_rg (_cc .FillColor .R (),_cc .FillColor .G (),_cc .FillColor .B ());};if _cc .BorderEnabled {_dce .Add_RG (_cc .BorderColor .R (),_cc .BorderColor .G (),_cc .BorderColor .B ());_dce .Add_w (_cc .BorderWidth );};if len (gsName )> 1{_dce .Add_gs (_f .PdfObjectName (gsName ));};DrawBezierPathWithCreator (_acgd ,_dce );_dce .Add_h ();if _cc .FillEnabled &&_cc .BorderEnabled {_dce .Add_B ();}else if _cc .FillEnabled {_dce .Add_f ();}else if _cc .BorderEnabled {_dce .Add_S ();};_dce .Add_Q ();_ffc :=_acgd .GetBoundingBox ();if _cc .BorderEnabled {_ffc .Height +=_cc .BorderWidth ;_ffc .Width +=_cc .BorderWidth ;_ffc .X -=_cc .BorderWidth /2;_ffc .Y -=_cc .BorderWidth /2;};return _dce .Bytes (),_ffc .ToPdfRectangle (),nil ;};
|
||||
|
||||
// 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;);
|
||||
func (_fbb Vector )FlipY ()Vector {_fbb .Dy =-_fbb .Dy ;return _fbb };
|
||||
|
||||
// BoundingBox represents the smallest rectangular area that encapsulates an object.
|
||||
type BoundingBox struct{X float64 ;Y float64 ;Width float64 ;Height float64 ;};
|
||||
|
||||
// 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 *_c .ContentCreator ){for _acdd ,_ec :=range path .Points {if _acdd ==0{creator .Add_m (_ec .X ,_ec .Y );}else {creator .Add_l (_ec .X ,_ec .Y );};};};
|
||||
|
||||
// Offset shifts the path with the specified offsets.
|
||||
func (_ce Path )Offset (offX ,offY float64 )Path {for _eg ,_db :=range _ce .Points {_ce .Points [_eg ]=_db .Add (offX ,offY );};return _ce ;};
|
||||
|
||||
// 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 *_bg .PdfColorDeviceRGB ;Opacity float64 ;LineWidth float64 ;LineEndingStyle1 LineEndingStyle ;LineEndingStyle2 LineEndingStyle ;LineStyle LineStyle ;};
|
||||
|
||||
// GetPointNumber returns the path point at the index specified by number.
|
||||
// The index is 1-based.
|
||||
func (_ac Path )GetPointNumber (number int )Point {if number < 1||number > len (_ac .Points ){return Point {};};return _ac .Points [number -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 ;};
|
||||
func (_dbab Polyline )Draw (gsName string )([]byte ,*_bg .PdfRectangle ,error ){if _dbab .LineColor ==nil {_dbab .LineColor =_bg .NewPdfColorDeviceRGB (0,0,0);};_eagd :=NewPath ();for _ ,_fdd :=range _dbab .Points {_eagd =_eagd .AppendPoint (_fdd );};_cdc :=_c .NewContentCreator ();_cdc .Add_q ();_cdc .Add_RG (_dbab .LineColor .R (),_dbab .LineColor .G (),_dbab .LineColor .B ());_cdc .Add_w (_dbab .LineWidth );if len (gsName )> 1{_cdc .Add_gs (_f .PdfObjectName (gsName ));};DrawPathWithCreator (_eagd ,_cdc );_cdc .Add_S ();_cdc .Add_Q ();return _cdc .Bytes (),_eagd .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 };
|
||||
// Magnitude returns the magnitude of the vector.
|
||||
func (_aebe Vector )Magnitude ()float64 {return _b .Sqrt (_b .Pow (_aebe .Dx ,2.0)+_b .Pow (_aebe .Dy ,2.0));};
|
||||
|
||||
// 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 };
|
||||
// Flip changes the sign of the vector: -vector.
|
||||
func (_ffae Vector )Flip ()Vector {_gdg :=_ffae .Magnitude ();_eaf :=_ffae .GetPolarAngle ();_ffae .Dx =_gdg *_b .Cos (_eaf +_b .Pi );_ffae .Dy =_gdg *_b .Sin (_eaf +_b .Pi );return _ffae ;};
|
||||
|
||||
// NewCubicBezierPath returns a new empty cubic Bezier path.
|
||||
func NewCubicBezierPath ()CubicBezierPath {_cc :=CubicBezierPath {};_cc .Curves =[]CubicBezierCurve {};return _cc ;};
|
||||
// GetBounds returns the bounding box of the Bezier curve.
|
||||
func (_bc CubicBezierCurve )GetBounds ()_bg .PdfRectangle {_cb :=_bc .P0 .X ;_dd :=_bc .P0 .X ;_ea :=_bc .P0 .Y ;_ca :=_bc .P0 .Y ;for _ae :=0.0;_ae <=1.0;_ae +=0.001{Rx :=_bc .P0 .X *_b .Pow (1-_ae ,3)+_bc .P1 .X *3*_ae *_b .Pow (1-_ae ,2)+_bc .P2 .X *3*_b .Pow (_ae ,2)*(1-_ae )+_bc .P3 .X *_b .Pow (_ae ,3);Ry :=_bc .P0 .Y *_b .Pow (1-_ae ,3)+_bc .P1 .Y *3*_ae *_b .Pow (1-_ae ,2)+_bc .P2 .Y *3*_b .Pow (_ae ,2)*(1-_ae )+_bc .P3 .Y *_b .Pow (_ae ,3);if Rx < _cb {_cb =Rx ;};if Rx > _dd {_dd =Rx ;};if Ry < _ea {_ea =Ry ;};if Ry > _ca {_ca =Ry ;};};_ab :=_bg .PdfRectangle {};_ab .Llx =_cb ;_ab .Lly =_ea ;_ab .Urx =_dd ;_ab .Ury =_ca ;return _ab ;};
|
||||
|
||||
// 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 ;};
|
||||
// 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 *_c .ContentCreator ){for _efg ,_cag :=range bpath .Curves {if _efg ==0{creator .Add_m (_cag .P0 .X ,_cag .P0 .Y );};creator .Add_c (_cag .P1 .X ,_cag .P1 .Y ,_cag .P2 .X ,_cag .P2 .Y ,_cag .P3 .X ,_cag .P3 .Y );};};
|
||||
|
||||
// AppendCurve appends the specified Bezier curve to the path.
|
||||
func (_eae CubicBezierPath )AppendCurve (curve CubicBezierCurve )CubicBezierPath {_eae .Curves =append (_eae .Curves ,curve );return _eae ;};
|
||||
|
||||
// LineStyle refers to how the line will be created.
|
||||
type LineStyle int ;
|
||||
|
||||
// Rotate rotates the vector by the specified angle.
|
||||
func (_dfd Vector )Rotate (phi float64 )Vector {_gdegb :=_dfd .Magnitude ();_ace :=_dfd .GetPolarAngle ();return NewVectorPolar (_gdegb ,_ace +phi );};
|
||||
|
||||
// AddOffsetXY adds X,Y offset to all points on a curve.
|
||||
func (_d CubicBezierCurve )AddOffsetXY (offX ,offY float64 )CubicBezierCurve {_d .P0 .X +=offX ;_d .P1 .X +=offX ;_d .P2 .X +=offX ;_d .P3 .X +=offX ;_d .P0 .Y +=offY ;_d .P1 .Y +=offY ;_d .P2 .Y +=offY ;_d .P3 .Y +=offY ;return _d ;};
|
||||
|
||||
// Length returns the number of points in the path.
|
||||
func (_ade Path )Length ()int {return len (_ade .Points )};
|
||||
|
||||
// CubicBezierPath represents a collection of cubic Bezier curves.
|
||||
type CubicBezierPath struct{Curves []CubicBezierCurve ;};
|
||||
|
||||
// Offset shifts the Bezier path with the specified offsets.
|
||||
func (_af CubicBezierPath )Offset (offX ,offY float64 )CubicBezierPath {for _gb ,_dc :=range _af .Curves {_af .Curves [_gb ]=_dc .AddOffsetXY (offX ,offY );};return _af ;};
|
||||
|
||||
// GetBoundingBox returns the bounding box of the Bezier path.
|
||||
func (_fb CubicBezierPath )GetBoundingBox ()Rectangle {_fc :=Rectangle {};_eb :=0.0;_eag :=0.0;_gge :=0.0;_gf :=0.0;for _ggf ,_ad :=range _fb .Curves {_df :=_ad .GetBounds ();if _ggf ==0{_eb =_df .Llx ;_eag =_df .Urx ;_gge =_df .Lly ;_gf =_df .Ury ;continue ;};if _df .Llx < _eb {_eb =_df .Llx ;};if _df .Urx > _eag {_eag =_df .Urx ;};if _df .Lly < _gge {_gge =_df .Lly ;};if _df .Ury > _gf {_gf =_df .Ury ;};};_fc .X =_eb ;_fc .Y =_gge ;_fc .Width =_eag -_eb ;_fc .Height =_gf -_gge ;return _fc ;};
|
||||
|
||||
// Polyline defines a slice of points that are connected as straight lines.
|
||||
type Polyline struct{Points []Point ;LineColor *_bg .PdfColorDeviceRGB ;LineWidth float64 ;};
|
||||
|
||||
// Add adds the specified vector to the current one and returns the result.
|
||||
func (_gbe Vector )Add (other Vector )Vector {_gbe .Dx +=other .Dx ;_gbe .Dy +=other .Dy ;return _gbe };
|
||||
|
||||
// Vector represents a two-dimensional vector.
|
||||
type Vector struct{Dx float64 ;Dy float64 ;};func (_gbf Point )String ()string {return _g .Sprintf ("(\u0025\u002e\u0031\u0066\u002c\u0025\u002e\u0031\u0066\u0029",_gbf .X ,_gbf .Y );};
|
||||
|
||||
// GetPolarAngle returns the angle the magnitude of the vector forms with the
|
||||
// positive X-axis going counterclockwise.
|
||||
func (_abc Vector )GetPolarAngle ()float64 {return _b .Atan2 (_abc .Dy ,_abc .Dx )};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 *_bg .PdfColorDeviceRGB ;BorderEnabled bool ;BorderWidth float64 ;BorderColor *_bg .PdfColorDeviceRGB ;Opacity float64 ;};
|
||||
|
||||
// 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 (_def Rectangle )Draw (gsName string )([]byte ,*_bg .PdfRectangle ,error ){_age :=NewPath ();_age =_age .AppendPoint (NewPoint (0,0));_age =_age .AppendPoint (NewPoint (0,_def .Height ));_age =_age .AppendPoint (NewPoint (_def .Width ,_def .Height ));_age =_age .AppendPoint (NewPoint (_def .Width ,0));_age =_age .AppendPoint (NewPoint (0,0));if _def .X !=0||_def .Y !=0{_age =_age .Offset (_def .X ,_def .Y );};_bgd :=_c .NewContentCreator ();_bgd .Add_q ();if _def .FillEnabled {_bgd .Add_rg (_def .FillColor .R (),_def .FillColor .G (),_def .FillColor .B ());};if _def .BorderEnabled {_bgd .Add_RG (_def .BorderColor .R (),_def .BorderColor .G (),_def .BorderColor .B ());_bgd .Add_w (_def .BorderWidth );};if len (gsName )> 1{_bgd .Add_gs (_f .PdfObjectName (gsName ));};DrawPathWithCreator (_age ,_bgd );_bgd .Add_h ();if _def .FillEnabled &&_def .BorderEnabled {_bgd .Add_B ();}else if _def .FillEnabled {_bgd .Add_f ();}else if _def .BorderEnabled {_bgd .Add_S ();};_bgd .Add_Q ();return _bgd .Bytes (),_age .GetBoundingBox ().ToPdfRectangle (),nil ;};
|
||||
|
||||
// NewVectorPolar returns a new vector calculated from the specified
|
||||
// magnitude and angle.
|
||||
func NewVectorPolar (length float64 ,theta float64 )Vector {_bcd :=Vector {};_bcd .Dx =length *_b .Cos (theta );_bcd .Dy =length *_b .Sin (theta );return _bcd ;};
|
1780
core/core.go
1780
core/core.go
File diff suppressed because one or more lines are too long
@ -9,34 +9,40 @@
|
||||
// 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 (_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 ;};
|
||||
|
||||
// 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 {}};
|
||||
package crypt ;import (_fe "crypto/aes";_g "crypto/cipher";_fd "crypto/md5";_c "crypto/rand";_ec "crypto/rc4";_a "fmt";_ff "github.com/unidoc/unipdf/v3/common";_ab "github.com/unidoc/unipdf/v3/core/security";_e "io";);func init (){_gfg ("\u0041\u0045\u0053V\u0032",_abf )};func _abf (_ac FilterDict )(Filter ,error ){if _ac .Length ==128{_ff .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",_ac .Length );_ac .Length /=8;};if _ac .Length !=0&&_ac .Length !=16{return nil ,_a .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",_ac .Length );};return filterAESV2 {},nil ;};type filterIdentity struct{};
|
||||
|
||||
// KeyLength implements Filter interface.
|
||||
func (_gff filterV2 )KeyLength ()int {return _gff ._ggf };
|
||||
func (filterAESV3 )KeyLength ()int {return 256/8};
|
||||
|
||||
// NewFilter creates CryptFilter from a corresponding dictionary.
|
||||
func NewFilter (d FilterDict )(Filter ,error ){_fbe ,_fbf :=_bae (d .CFM );if _fbf !=nil {return nil ,_fbf ;};_ca ,_fbf :=_fbe (d );if _fbf !=nil {return nil ,_fbf ;};return _ca ,nil ;};
|
||||
|
||||
// KeyLength implements Filter interface.
|
||||
func (_bbe filterV2 )KeyLength ()int {return _bbe ._ade };
|
||||
|
||||
// NewFilterAESV3 creates an AES-based filter with a 256 bit key (AESV3).
|
||||
func NewFilterAESV3 ()Filter {_fa ,_ce :=_dg (FilterDict {});if _ce !=nil {_ff .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",_ce );return filterAESV3 {};};return _fa ;};
|
||||
|
||||
// FilterDict represents information from a CryptFilter dictionary.
|
||||
type FilterDict struct{CFM string ;AuthEvent _ab .AuthEvent ;Length int ;};
|
||||
|
||||
// Name implements Filter interface.
|
||||
func (filterV2 )Name ()string {return "\u0056\u0032"};
|
||||
func (filterAESV2 )Name ()string {return "\u0041\u0045\u0053V\u0032"};var _ Filter =filterAESV2 {};func init (){_gfg ("\u0041\u0045\u0053V\u0033",_dg )};type filterV2 struct{_ade int };
|
||||
|
||||
// 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 {}};
|
||||
// MakeKey implements Filter interface.
|
||||
func (_ge filterV2 )MakeKey (objNum ,genNum uint32 ,ekey []byte )([]byte ,error ){return _ffb (objNum ,genNum ,ekey ,false );};
|
||||
|
||||
// NewFilterV2 creates a RC4-based filter with a specified key length (in bytes).
|
||||
func NewFilterV2 (length int )Filter {_gc ,_ef :=_cd (FilterDict {Length :length });if _ef !=nil {_ff .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",_ef );return filterV2 {_ade :length };};return _gc ;};
|
||||
|
||||
// MakeKey implements Filter interface.
|
||||
func (filterAESV2 )MakeKey (objNum ,genNum uint32 ,ekey []byte )([]byte ,error ){return _ffb (objNum ,genNum ,ekey ,true );};func (filterAES )DecryptBytes (buf []byte ,okey []byte )([]byte ,error ){_gg ,_bb :=_fe .NewCipher (okey );if _bb !=nil {return nil ,_bb ;};if len (buf )< 16{_ff .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 ,_a .Errorf ("\u0041\u0045\u0053\u003a B\u0075\u0066\u0020\u006c\u0065\u006e\u0020\u003c\u0020\u0031\u0036\u0020\u0028\u0025d\u0029",len (buf ));};_ga :=buf [:16];buf =buf [16:];if len (buf )%16!=0{_ff .Log .Debug ("\u0020\u0069\u0076\u0020\u0028\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (_ga ),_ga );_ff .Log .Debug ("\u0062\u0075\u0066\u0020\u0028\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );return buf ,_a .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 ));};_ecf :=_g .NewCBCDecrypter (_gg ,_ga );_ff .Log .Trace ("A\u0045\u0053\u0020\u0044ec\u0072y\u0070\u0074\u0020\u0028\u0025d\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );_ff .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 );_ecf .CryptBlocks (buf ,buf );_ff .Log .Trace ("\u0074\u006f\u0020(\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );if len (buf )==0{_ff .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 ;};_agd :=int (buf [len (buf )-1]);if _agd > len (buf ){_ff .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",_agd ,len (buf ));return buf ,_a .Errorf ("\u0069n\u0076a\u006c\u0069\u0064\u0020\u0070a\u0064\u0020l\u0065\u006e\u0067\u0074\u0068");};buf =buf [:len (buf )-_agd ];return buf ,nil ;};type filterAESV2 struct{filterAES };type filterFunc func (_cca FilterDict )(Filter ,error );
|
||||
|
||||
// KeyLength implements Filter interface.
|
||||
func (filterAESV2 )KeyLength ()int {return 128/8};
|
||||
|
||||
// Name implements Filter interface.
|
||||
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 _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 ;};
|
||||
|
||||
// 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 );
|
||||
func (filterAESV3 )Name ()string {return "\u0041\u0045\u0053V\u0033"};
|
||||
|
||||
// Filter is a common interface for crypt filter methods.
|
||||
type Filter interface{
|
||||
@ -55,54 +61,48 @@ 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 (_ace ,_aea uint32 ,_cf []byte )([]byte ,error );
|
||||
MakeKey (_db ,_gdg uint32 ,_cff []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 (_agac []byte ,_ff []byte )([]byte ,error );
|
||||
EncryptBytes (_deg []byte ,_ead []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 (_ee []byte ,_bc []byte )([]byte ,error );};
|
||||
DecryptBytes (_bg []byte ,_efa []byte )([]byte ,error );};func (filterAES )EncryptBytes (buf []byte ,okey []byte )([]byte ,error ){_eb ,_dc :=_fe .NewCipher (okey );if _dc !=nil {return nil ,_dc ;};_ff .Log .Trace ("A\u0045\u0053\u0020\u0045nc\u0072y\u0070\u0074\u0020\u0028\u0025d\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );const _fdg =_fe .BlockSize ;_af :=_fdg -len (buf )%_fdg ;for _ag :=0;_ag < _af ;_ag ++{buf =append (buf ,byte (_af ));};_ff .Log .Trace ("\u0050a\u0064d\u0065\u0064\u0020\u0074\u006f \u0025\u0064 \u0062\u0079\u0074\u0065\u0073",len (buf ));_cf :=make ([]byte ,_fdg +len (buf ));_da :=_cf [:_fdg ];if _ ,_b :=_e .ReadFull (_c .Reader ,_da );_b !=nil {return nil ,_b ;};_fc :=_g .NewCBCEncrypter (_eb ,_da );_fc .CryptBlocks (_cf [_fdg :],buf );buf =_cf ;_ff .Log .Trace ("\u0074\u006f\u0020(\u0025\u0064\u0029\u003a\u0020\u0025\u0020\u0078",len (buf ),buf );return buf ,nil ;};
|
||||
|
||||
// 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"};
|
||||
// Name implements Filter interface.
|
||||
func (filterV2 )Name ()string {return "\u0056\u0032"};func init (){_gfg ("\u0056\u0032",_cd )};
|
||||
|
||||
// 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 {};
|
||||
// NewFilterAESV2 creates an AES-based filter with a 128 bit key (AESV2).
|
||||
func NewFilterAESV2 ()Filter {_cc ,_cb :=_abf (FilterDict {});if _cb !=nil {_ff .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",_cb );return filterAESV2 {};};return _cc ;};func (filterIdentity )HandlerVersion ()(V ,R int ){return ;};
|
||||
|
||||
// MakeKey implements Filter interface.
|
||||
func (filterAESV2 )MakeKey (objNum ,genNum uint32 ,ekey []byte )([]byte ,error ){return _ge (objNum ,genNum ,ekey ,true );};
|
||||
|
||||
// MakeKey implements Filter interface.
|
||||
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 (filterAESV3 )MakeKey (_ ,_ uint32 ,ekey []byte )([]byte ,error ){return ekey ,nil };
|
||||
|
||||
// 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 ;};
|
||||
// HandlerVersion implements Filter interface.
|
||||
func (filterAESV3 )HandlerVersion ()(V ,R int ){V ,R =5,6;return ;};
|
||||
|
||||
// NewIdentity creates an identity filter that bypasses all data without changes.
|
||||
func NewIdentity ()Filter {return filterIdentity {}};var _ Filter =filterV2 {};
|
||||
// HandlerVersion implements Filter interface.
|
||||
func (_gag filterV2 )HandlerVersion ()(V ,R int ){V ,R =2,3;return ;};var _ Filter =filterV2 {};func (filterIdentity )PDFVersion ()[2]int {return [2]int {}};
|
||||
|
||||
// PDFVersion implements Filter interface.
|
||||
func (filterAESV2 )PDFVersion ()[2]int {return [2]int {1,5}};
|
||||
|
||||
// EncryptBytes implements Filter interface.
|
||||
func (filterV2 )EncryptBytes (buf []byte ,okey []byte )([]byte ,error ){_df ,_afg :=_ec .NewCipher (okey );if _afg !=nil {return nil ,_afg ;};_ff .Log .Trace ("\u0052\u00434\u0020\u0045\u006ec\u0072\u0079\u0070\u0074\u003a\u0020\u0025\u0020\u0078",buf );_df .XORKeyStream (buf ,buf );_ff .Log .Trace ("\u0074o\u003a\u0020\u0025\u0020\u0078",buf );return buf ,nil ;};func (filterIdentity )Name ()string {return "\u0049\u0064\u0065\u006e\u0074\u0069\u0074\u0079"};var _ Filter =filterAESV3 {};func _cd (_de FilterDict )(Filter ,error ){if _de .Length %8!=0{return nil ,_a .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",_de .Length );};if _de .Length < 5||_de .Length > 16{if _de .Length ==40||_de .Length ==64||_de .Length ==128{_ff .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",_de .Length );_de .Length /=8;}else {return nil ,_a .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",_de .Length );};};return filterV2 {_ade :_de .Length },nil ;};type filterAESV3 struct{filterAES };func (filterIdentity )KeyLength ()int {return 0};func _ffb (_fea ,_fb uint32 ,_feag []byte ,_gac bool )([]byte ,error ){_dcd :=make ([]byte ,len (_feag )+5);for _cbb :=0;_cbb < len (_feag );_cbb ++{_dcd [_cbb ]=_feag [_cbb ];};for _gd :=0;_gd < 3;_gd ++{_ae :=byte ((_fea >>uint32 (8*_gd ))&0xff);_dcd [_gd +len (_feag )]=_ae ;};for _def :=0;_def < 2;_def ++{_eg :=byte ((_fb >>uint32 (8*_def ))&0xff);_dcd [_def +len (_feag )+3]=_eg ;};if _gac {_dcd =append (_dcd ,0x73);_dcd =append (_dcd ,0x41);_dcd =append (_dcd ,0x6C);_dcd =append (_dcd ,0x54);};_ad :=_fd .New ();_ad .Write (_dcd );_ced :=_ad .Sum (nil );if len (_feag )+5< 16{return _ced [0:len (_feag )+5],nil ;};return _ced ,nil ;};func (filterIdentity )EncryptBytes (p []byte ,okey []byte )([]byte ,error ){return p ,nil };func (filterIdentity )DecryptBytes (p []byte ,okey []byte )([]byte ,error ){return p ,nil };func _dg (_ea FilterDict )(Filter ,error ){if _ea .Length ==256{_ff .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",_ea .Length );_ea .Length /=8;};if _ea .Length !=0&&_ea .Length !=32{return nil ,_a .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",_ea .Length );};return filterAESV3 {},nil ;};func _bae (_egd string )(filterFunc ,error ){_cce :=_cba [string (_egd )];if _cce ==nil {return nil ,_a .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",_egd );};return _cce ,nil ;};
|
||||
|
||||
// PDFVersion implements Filter interface.
|
||||
func (_aef filterV2 )PDFVersion ()[2]int {return [2]int {}};
|
||||
|
||||
// PDFVersion implements Filter interface.
|
||||
func (filterAESV3 )PDFVersion ()[2]int {return [2]int {2,0}};
|
||||
|
||||
// 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 ;};
|
||||
func (filterV2 )DecryptBytes (buf []byte ,okey []byte )([]byte ,error ){_cfa ,_eff :=_ec .NewCipher (okey );if _eff !=nil {return nil ,_eff ;};_ff .Log .Trace ("\u0052\u00434\u0020\u0044\u0065c\u0072\u0079\u0070\u0074\u003a\u0020\u0025\u0020\u0078",buf );_cfa .XORKeyStream (buf ,buf );_ff .Log .Trace ("\u0074o\u003a\u0020\u0025\u0020\u0078",buf );return buf ,nil ;};type filterAES struct{};func (filterIdentity )MakeKey (objNum ,genNum uint32 ,fkey []byte )([]byte ,error ){return fkey ,nil };func _gfg (_bgb string ,_ba filterFunc ){if _ ,_age :=_cba [_bgb ];_age {panic ("\u0061l\u0072e\u0061\u0064\u0079\u0020\u0072e\u0067\u0069s\u0074\u0065\u0072\u0065\u0064");};_cba [_bgb ]=_ba ;};var (_cba =make (map[string ]filterFunc ););
|
||||
|
||||
// NewIdentity creates an identity filter that bypasses all data without changes.
|
||||
func NewIdentity ()Filter {return filterIdentity {}};
|
File diff suppressed because one or more lines are too long
2574
creator/creator.go
2574
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
24
fdf/fdf.go
24
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 (_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{_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 _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"`;
|
||||
package fjson ;import (_g "encoding/json";_e "github.com/unidoc/unipdf/v3/core";_ea "github.com/unidoc/unipdf/v3/model";_fd "io";_f "os";);type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;
|
||||
|
||||
// Options lists allowed values if present.
|
||||
Options []string `json:"options,omitempty"`;};
|
||||
Options []string `json:"options,omitempty"`;};
|
||||
|
||||
// LoadFromPDF loads form field data from a PDF.
|
||||
func LoadFromPDF (rs _fd .ReadSeeker )(*FieldData ,error ){_c ,_cc :=_ea .NewPdfReader (rs );if _cc !=nil {return nil ,_cc ;};if _c .AcroForm ==nil {return nil ,nil ;};var _fda []fieldValue ;_gb :=_c .AcroForm .AllFields ();for _ ,_bd :=range _gb {var _ab []string ;_gae :=make (map[string ]struct{});_db ,_cg :=_bd .FullName ();if _cg !=nil {return nil ,_cg ;};if _bg ,_fg :=_bd .V .(*_e .PdfObjectString );_fg {_fda =append (_fda ,fieldValue {Name :_db ,Value :_bg .Decoded ()});continue ;};var _bb string ;for _ ,_ead :=range _bd .Annotations {_ad ,_dc :=_e .GetName (_ead .AS );if _dc {_bb =_ad .String ();};_cd ,_ee :=_e .GetDict (_ead .AP );if !_ee {continue ;};_ca ,_ :=_e .GetDict (_cd .Get ("\u004e"));for _ ,_ef :=range _ca .Keys (){_efg :=_ef .String ();if _ ,_dbg :=_gae [_efg ];!_dbg {_ab =append (_ab ,_efg );_gae [_efg ]=struct{}{};};};_ae ,_ :=_e .GetDict (_cd .Get ("\u0044"));for _ ,_efb :=range _ae .Keys (){_ba :=_efb .String ();if _ ,_bf :=_gae [_ba ];!_bf {_ab =append (_ab ,_ba );_gae [_ba ]=struct{}{};};};};_ge :=fieldValue {Name :_db ,Value :_bb ,Options :_ab };_fda =append (_fda ,_ge );};_eaf :=FieldData {_fb :_fda };return &_eaf ,nil ;};
|
||||
|
||||
// LoadFromJSONFile loads form field data from a JSON file.
|
||||
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_a ,_ga :=_f .Open (filePath );if _ga !=nil {return nil ,_ga ;};defer _a .Close ();return LoadFromJSON (_a );};
|
||||
|
||||
// FieldValues implements model.FieldValueProvider interface.
|
||||
func (_bfd *FieldData )FieldValues ()(map[string ]_e .PdfObject ,error ){_gc :=make (map[string ]_e .PdfObject );for _ ,_af :=range _bfd ._fb {if len (_af .Value )> 0{_gc [_af .Name ]=_e .MakeString (_af .Value );};};return _gc ,nil ;};
|
||||
|
||||
// JSON returns the field data as a string in JSON format.
|
||||
func (_bddc FieldData )JSON ()(string ,error ){_geb ,_bfe :=_g .MarshalIndent (_bddc ._fb ,"","\u0020\u0020\u0020\u0020");return string (_geb ),_bfe ;};
|
||||
|
||||
// LoadFromJSON loads JSON form data from `r`.
|
||||
func LoadFromJSON (r _fd .Reader )(*FieldData ,error ){var _d FieldData ;_eg :=_g .NewDecoder (r ).Decode (&_d ._fb );if _eg !=nil {return nil ,_eg ;};return &_d ,nil ;};
|
||||
|
||||
// LoadFromPDFFile loads form field data from a PDF file.
|
||||
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_fc ,_bfb :=_f .Open (filePath );if _bfb !=nil {return nil ,_bfb ;};defer _fc .Close ();return LoadFromPDF (_fc );};
|
||||
|
||||
// FieldData represents form field data loaded from JSON file.
|
||||
type FieldData struct{_fb []fieldValue };
|
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";);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 };
|
||||
package endian ;import (_a "encoding/binary";_da "unsafe";);var (ByteOrder _a .ByteOrder ;_e bool ;);func init (){const _b =int (_da .Sizeof (0));_dg :=1;_bd :=(*[_b ]byte )(_da .Pointer (&_dg ));if _bd [0]==0{_e =true ;ByteOrder =_a .BigEndian ;}else {ByteOrder =_a .LittleEndian ;};};func IsLittle ()bool {return !_e };func IsBig ()bool {return _e };
|
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 _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 ))};
|
||||
package basic ;import _g "github.com/unidoc/unipdf/v3/internal/jbig2/errors";func (_d IntsMap )Add (key uint64 ,value int ){_d [key ]=append (_d [key ],value )};func (_gf *NumSlice )Add (v float32 ){*_gf =append (*_gf ,v )};func (_ca IntsMap )GetSlice (key uint64 )([]int ,bool ){_gb ,_e :=_ca [key ];if !_e {return nil ,false ;};return _gb ,true ;};func NewIntSlice (i int )*IntSlice {_dc :=IntSlice (make ([]int ,i ));return &_dc };func (_fd IntsMap )Delete (key uint64 ){delete (_fd ,key )};func (_a *IntSlice )Add (v int )error {if _a ==nil {return _g .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");};*_a =append (*_a ,v );return nil ;};type IntsMap map[uint64 ][]int ;func NewNumSlice (i int )*NumSlice {_ef :=NumSlice (make ([]float32 ,i ));return &_ef };func (_cb *Stack )Len ()int {return len (_cb .Data )};func (_fde NumSlice )Get (i int )(float32 ,error ){if i < 0||i > len (_fde )-1{return 0,_g .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 _fde [i ],nil ;};func Ceil (numerator ,denominator int )int {if numerator %denominator ==0{return numerator /denominator ;};return (numerator /denominator )+1;};type NumSlice []float32 ;func (_ad *Stack )Peek ()(_gde interface{},_ee bool ){return _ad .peek ()};func (_ce IntSlice )Size ()int {return len (_ce )};func (_efb *Stack )top ()int {return len (_efb .Data )-1};type IntSlice []int ;func (_aa *IntSlice )Copy ()*IntSlice {_ab :=IntSlice (make ([]int ,len (*_aa )));copy (_ab ,*_aa );return &_ab ;};func Sign (v float32 )float32 {if v >=0.0{return 1.0;};return -1.0;};func Max (x ,y int )int {if x > y {return x ;};return y ;};func (_dg IntsMap )Get (key uint64 )(int ,bool ){_de ,_f :=_dg [key ];if !_f {return 0,false ;};if len (_de )==0{return 0,false ;};return _de [0],true ;};func (_dd IntSlice )Get (index int )(int ,error ){if index > len (_dd )-1{return 0,_g .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 (_gcb *Stack )Push (v interface{}){_gcb .Data =append (_gcb .Data ,v )};func Abs (v int )int {if v > 0{return v ;};return -v ;};func (_efa *NumSlice )AddInt (v int ){*_efa =append (*_efa ,float32 (v ))};type Stack struct{Data []interface{};Aux *Stack ;};func Min (x ,y int )int {if x < y {return x ;};return y ;};func (_ae NumSlice )GetIntSlice ()[]int {_cc :=make ([]int ,len (_ae ));for _ge ,_gc :=range _ae {_cc [_ge ]=int (_gc );};return _cc ;};func (_gbf *Stack )Pop ()(_gcg interface{},_dge bool ){_gcg ,_dge =_gbf .peek ();if !_dge {return nil ,_dge ;};_gbf .Data =_gbf .Data [:_gbf .top ()];return _gcg ,true ;};func (_dcd NumSlice )GetInt (i int )(int ,error ){const _gd ="\u0047\u0065\u0074\u0049\u006e\u0074";if i < 0||i > len (_dcd )-1{return 0,_g .Errorf (_gd ,"\u0069n\u0064\u0065\u0078\u003a\u0020\u0027\u0025\u0064\u0027\u0020\u006fu\u0074\u0020\u006f\u0066\u0020\u0072\u0061\u006e\u0067\u0065",i );};_fg :=_dcd [i ];return int (_fg +Sign (_fg )*0.5),nil ;};func (_fe *Stack )peek ()(interface{},bool ){_dda :=_fe .top ();if _dda ==-1{return nil ,false ;};return _fe .Data [_dda ],true ;};
|
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 (_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 )};
|
||||
package decoder ;import (_a "github.com/unidoc/unipdf/v3/internal/bitwise";_ac "github.com/unidoc/unipdf/v3/internal/jbig2/bitmap";_f "github.com/unidoc/unipdf/v3/internal/jbig2/document";_c "github.com/unidoc/unipdf/v3/internal/jbig2/errors";_g "image";);func (_bd *Decoder )DecodePageImage (pageNumber int )(_g .Image ,error ){const _de ="\u0064\u0065\u0063od\u0065\u0072\u002e\u0044\u0065\u0063\u006f\u0064\u0065\u0050\u0061\u0067\u0065\u0049\u006d\u0061\u0067\u0065";_bf ,_fg :=_bd .decodePageImage (pageNumber );if _fg !=nil {return nil ,_c .Wrap (_fg ,_de ,"");};return _bf ,nil ;};type Decoder struct{_b _a .StreamReader ;_gb *_f .Document ;_e int ;_ag Parameters ;};func (_aa *Decoder )DecodeNextPage ()([]byte ,error ){_aa ._e ++;_fb :=_aa ._e ;return _aa .decodePage (_fb );};type Parameters struct{UnpaddedData bool ;Color _ac .Color ;};func (_eg *Decoder )DecodePage (pageNumber int )([]byte ,error ){return _eg .decodePage (pageNumber )};func (_af *Decoder )decodePageImage (_abf int )(_g .Image ,error ){const _egb ="\u0064e\u0063o\u0064\u0065\u0050\u0061\u0067\u0065\u0049\u006d\u0061\u0067\u0065";if _abf < 0{return nil ,_c .Errorf (_egb ,"\u0069n\u0076\u0061\u006c\u0069d\u0020\u0070\u0061\u0067\u0065 \u006eu\u006db\u0065\u0072\u003a\u0020\u0027\u0025\u0064'",_abf );};if _abf > int (_af ._gb .NumberOfPages ){return nil ,_c .Errorf (_egb ,"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",_abf );};_ce ,_dg :=_af ._gb .GetPage (_abf );if _dg !=nil {return nil ,_c .Wrap (_dg ,_egb ,"");};_gbe ,_dg :=_ce .GetBitmap ();if _dg !=nil {return nil ,_c .Wrap (_dg ,_egb ,"");};return _gbe .ToImage (),nil ;};func (_bb *Decoder )decodePage (_bc int )([]byte ,error ){const _ec ="\u0064\u0065\u0063\u006f\u0064\u0065\u0050\u0061\u0067\u0065";if _bc < 0{return nil ,_c .Errorf (_ec ,"\u0069n\u0076\u0061\u006c\u0069d\u0020\u0070\u0061\u0067\u0065 \u006eu\u006db\u0065\u0072\u003a\u0020\u0027\u0025\u0064'",_bc );};if _bc > int (_bb ._gb .NumberOfPages ){return nil ,_c .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",_bc );};_bbg ,_fgc :=_bb ._gb .GetPage (_bc );if _fgc !=nil {return nil ,_c .Wrap (_fgc ,_ec ,"");};_abc ,_fgc :=_bbg .GetBitmap ();if _fgc !=nil {return nil ,_c .Wrap (_fgc ,_ec ,"");};_abc .InverseData ();if !_bb ._ag .UnpaddedData {return _abc .Data ,nil ;};return _abc .GetUnpaddedData ();};func Decode (input []byte ,parameters Parameters ,globals *_f .Globals )(*Decoder ,error ){_gd :=_a .NewReader (input );_ge ,_fe :=_f .DecodeDocument (_gd ,globals );if _fe !=nil {return nil ,_fe ;};return &Decoder {_b :_gd ,_gb :_ge ,_ag :parameters },nil ;};func (_ab *Decoder )PageNumber ()(int ,error ){const _fd ="\u0044e\u0063o\u0064\u0065\u0072\u002e\u0050a\u0067\u0065N\u0075\u006d\u0062\u0065\u0072";if _ab ._gb ==nil {return 0,_c .Error (_fd ,"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 (_ab ._gb .NumberOfPages ),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
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 _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 ;};
|
||||
package errors ;import _a "fmt";func Error (processName ,message string )error {return _cc (message ,processName )};func Wrapf (err error ,processName ,message string ,arguments ...interface{})error {if _cg ,_eb :=err .(*processError );_eb {_cg ._af ="";};_d :=_cc (_a .Sprintf (message ,arguments ...),processName );_d ._b =err ;return _d ;};type processError struct{_af string ;_c string ;_e string ;_b error ;};func Wrap (err error ,processName ,message string )error {if _ea ,_ef :=err .(*processError );_ef {_ea ._af ="";};_afc :=_cc (message ,processName );_afc ._b =err ;return _afc ;};func (_ae *processError )Error ()string {var _g string ;if _ae ._af !=""{_g =_ae ._af ;};_g +="\u0050r\u006f\u0063\u0065\u0073\u0073\u003a "+_ae ._c ;if _ae ._e !=""{_g +="\u0020\u004d\u0065\u0073\u0073\u0061\u0067\u0065\u003a\u0020"+_ae ._e ;};if _ae ._b !=nil {_g +="\u002e\u0020"+_ae ._b .Error ();};return _g ;};func Errorf (processName ,message string ,arguments ...interface{})error {return _cc (_a .Sprintf (message ,arguments ...),processName );};func _cc (_ff ,_cd string )*processError {return &processError {_af :"\u005b\u0055\u006e\u0069\u0050\u0044\u0046\u005d",_e :_ff ,_c :_cd };};
|
@ -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 (_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 ();};
|
||||
package jbig2 ;import (_c "github.com/unidoc/unipdf/v3/internal/bitwise";_g "github.com/unidoc/unipdf/v3/internal/jbig2/decoder";_f "github.com/unidoc/unipdf/v3/internal/jbig2/document";_d "github.com/unidoc/unipdf/v3/internal/jbig2/document/segments";_ca "github.com/unidoc/unipdf/v3/internal/jbig2/errors";_ac "sort";);func DecodeBytes (encoded []byte ,parameters _g .Parameters ,globals ...Globals )([]byte ,error ){var _b Globals ;if len (globals )> 0{_b =globals [0];};_cad ,_bb :=_g .Decode (encoded ,parameters ,_b .ToDocumentGlobals ());if _bb !=nil {return nil ,_bb ;};return _cad .DecodeNextPage ();};type Globals map[int ]*_d .Header ;func (_gdc Globals )ToDocumentGlobals ()*_f .Globals {if _gdc ==nil {return nil ;};_cf :=[]*_d .Header {};for _ ,_gb :=range _gdc {_cf =append (_cf ,_gb );};_ac .Slice (_cf ,func (_cg ,_bbb int )bool {return _cf [_cg ].SegmentNumber < _cf [_bbb ].SegmentNumber });return &_f .Globals {Segments :_cf };};func DecodeGlobals (encoded []byte )(Globals ,error ){const _fa ="\u0044\u0065\u0063\u006f\u0064\u0065\u0047\u006c\u006f\u0062\u0061\u006c\u0073";_af :=_c .NewReader (encoded );_gg ,_gd :=_f .DecodeDocument (_af ,nil );if _gd !=nil {return nil ,_ca .Wrap (_gd ,_fa ,"");};if _gg .GlobalSegments ==nil ||(_gg .GlobalSegments .Segments ==nil ){return nil ,_ca .Error (_fa ,"\u006eo\u0020\u0067\u006c\u006f\u0062\u0061\u006c\u0020\u0073\u0065\u0067m\u0065\u006e\u0074\u0073\u0020\u0066\u006f\u0075\u006e\u0064");};_ad :=Globals {};for _ ,_bc :=range _gg .GlobalSegments .Segments {_ad [int (_bc .SegmentNumber )]=_bc ;};return _ad ,nil ;};
|
@ -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 (_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 };};
|
||||
package sampling ;import (_a "github.com/unidoc/unipdf/v3/internal/bitwise";_g "github.com/unidoc/unipdf/v3/internal/imageutil";_e "io";);func (_eg *Reader )ReadSamples (samples []uint32 )(_fd error ){for _c :=0;_c < len (samples );_c ++{samples [_c ],_fd =_eg .ReadSample ();if _fd !=nil {return _fd ;};};return nil ;};func ResampleBytes (data []byte ,bitsPerSample int )[]uint32 {var _ff []uint32 ;_ab :=bitsPerSample ;var _ef uint32 ;var _ggd byte ;_bd :=0;_bf :=0;_ed :=0;for _ed < len (data ){if _bd > 0{_aff :=_bd ;if _ab < _aff {_aff =_ab ;};_ef =(_ef <<uint (_aff ))|uint32 (_ggd >>uint (8-_aff ));_bd -=_aff ;if _bd > 0{_ggd =_ggd <<uint (_aff );}else {_ggd =0;};_ab -=_aff ;if _ab ==0{_ff =append (_ff ,_ef );_ab =bitsPerSample ;_ef =0;_bf ++;};}else {_bb :=data [_ed ];_ed ++;_fef :=8;if _ab < _fef {_fef =_ab ;};_bd =8-_fef ;_ef =(_ef <<uint (_fef ))|uint32 (_bb >>uint (_bd ));if _fef < 8{_ggd =_bb <<uint (_fef );};_ab -=_fef ;if _ab ==0{_ff =append (_ff ,_ef );_ab =bitsPerSample ;_ef =0;_bf ++;};};};for _bd >=bitsPerSample {_ce :=_bd ;if _ab < _ce {_ce =_ab ;};_ef =(_ef <<uint (_ce ))|uint32 (_ggd >>uint (8-_ce ));_bd -=_ce ;if _bd > 0{_ggd =_ggd <<uint (_ce );}else {_ggd =0;};_ab -=_ce ;if _ab ==0{_ff =append (_ff ,_ef );_ab =bitsPerSample ;_ef =0;_bf ++;};};return _ff ;};type SampleReader interface{ReadSample ()(uint32 ,error );ReadSamples (_ea []uint32 )error ;};func ResampleUint32 (data []uint32 ,bitsPerInputSample int ,bitsPerOutputSample int )[]uint32 {var _dd []uint32 ;_efe :=bitsPerOutputSample ;var _gd uint32 ;var _ca uint32 ;_ag :=0;_ae :=0;_db :=0;for _db < len (data ){if _ag > 0{_ege :=_ag ;if _efe < _ege {_ege =_efe ;};_gd =(_gd <<uint (_ege ))|uint32 (_ca >>uint (bitsPerInputSample -_ege ));_ag -=_ege ;if _ag > 0{_ca =_ca <<uint (_ege );}else {_ca =0;};_efe -=_ege ;if _efe ==0{_dd =append (_dd ,_gd );_efe =bitsPerOutputSample ;_gd =0;_ae ++;};}else {_dg :=data [_db ];_db ++;_bbe :=bitsPerInputSample ;if _efe < _bbe {_bbe =_efe ;};_ag =bitsPerInputSample -_bbe ;_gd =(_gd <<uint (_bbe ))|uint32 (_dg >>uint (_ag ));if _bbe < bitsPerInputSample {_ca =_dg <<uint (_bbe );};_efe -=_bbe ;if _efe ==0{_dd =append (_dd ,_gd );_efe =bitsPerOutputSample ;_gd =0;_ae ++;};};};for _ag >=bitsPerOutputSample {_eac :=_ag ;if _efe < _eac {_eac =_efe ;};_gd =(_gd <<uint (_eac ))|uint32 (_ca >>uint (bitsPerInputSample -_eac ));_ag -=_eac ;if _ag > 0{_ca =_ca <<uint (_eac );}else {_ca =0;};_efe -=_eac ;if _efe ==0{_dd =append (_dd ,_gd );_efe =bitsPerOutputSample ;_gd =0;_ae ++;};};if _efe > 0&&_efe < bitsPerOutputSample {_gd <<=uint (_efe );_dd =append (_dd ,_gd );};return _dd ;};func (_bbb *Writer )WriteSample (sample uint32 )error {if _ ,_geg :=_bbb ._cc .WriteBits (uint64 (sample ),_bbb ._afb .BitsPerComponent );_geg !=nil {return _geg ;};_bbb ._ggf --;if _bbb ._ggf ==0{_bbb ._ggf =_bbb ._afb .ColorComponents ;_bbb ._cec ++;};if _bbb ._cec ==_bbb ._afb .Width {if _bbb ._fbd {_bbb ._cc .FinishByte ();};_bbb ._cec =0;};return nil ;};type SampleWriter interface{WriteSample (_gdf uint32 )error ;WriteSamples (_ge []uint32 )error ;};func (_aa *Writer )WriteSamples (samples []uint32 )error {for _fbc :=0;_fbc < len (samples );_fbc ++{if _fed :=_aa .WriteSample (samples [_fbc ]);_fed !=nil {return _fed ;};};return nil ;};func NewReader (img _g .ImageBase )*Reader {return &Reader {_fe :_a .NewReader (img .Data ),_f :img ,_ga :img .ColorComponents ,_dc :img .BytesPerLine *8!=img .ColorComponents *img .BitsPerComponent *img .Width };};type Reader struct{_f _g .ImageBase ;_fe *_a .Reader ;_b ,_gg ,_ga int ;_dc bool ;};func (_fb *Reader )ReadSample ()(uint32 ,error ){if _fb ._gg ==_fb ._f .Height {return 0,_e .EOF ;};_fg ,_af :=_fb ._fe .ReadBits (byte (_fb ._f .BitsPerComponent ));if _af !=nil {return 0,_af ;};_fb ._ga --;if _fb ._ga ==0{_fb ._ga =_fb ._f .ColorComponents ;_fb ._b ++;};if _fb ._b ==_fb ._f .Width {if _fb ._dc {_fb ._fe .ConsumeRemainingBits ();};_fb ._b =0;_fb ._gg ++;};return uint32 (_fg ),nil ;};type Writer struct{_afb _g .ImageBase ;_cc *_a .Writer ;_cec ,_ggf int ;_fbd bool ;};func NewWriter (img _g .ImageBase )*Writer {return &Writer {_cc :_a .NewWriterMSB (img .Data ),_afb :img ,_ggf :img .ColorComponents ,_fbd :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 (_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 ))};
|
||||
package transform ;import (_f "fmt";_fe "github.com/unidoc/unipdf/v3/common";_a "math";);func IdentityMatrix ()Matrix {return NewMatrix (1,0,0,1,0,0)};func (_fa Matrix )Mult (b Matrix )Matrix {_fa .Concat (b );return _fa };func (_af *Matrix )Shear (x ,y float64 ){_af .Concat (ShearMatrix (x ,y ))};func (_fbf Matrix )String ()string {_c ,_ab ,_dd ,_ac ,_eb ,_ca :=_fbf [0],_fbf [1],_fbf [3],_fbf [4],_fbf [6],_fbf [7];return _f .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",_c ,_ab ,_dd ,_ac ,_eb ,_ca );};func NewMatrix (a ,b ,c ,d ,tx ,ty float64 )Matrix {_b :=Matrix {a ,b ,0,c ,d ,0,tx ,ty ,1};_b .clampRange ();return _b ;};func (_ea Matrix )ScalingFactorX ()float64 {return _a .Hypot (_ea [0],_ea [1])};func (_cec *Point )Set (x ,y float64 ){_cec .X ,_cec .Y =x ,y };type Matrix [9]float64 ;func (_bc Matrix )Angle ()float64 {_afa :=_a .Atan2 (-_bc [1],_bc [0]);if _afa < 0.0{_afa +=2*_a .Pi ;};return _afa /_a .Pi *180.0;};func (_aea *Matrix )clampRange (){for _ddd ,_cc :=range _aea {if _cc > _cg {_fe .Log .Debug ("\u0043L\u0041M\u0050\u003a\u0020\u0025\u0067\u0020\u002d\u003e\u0020\u0025\u0067",_cc ,_cg );_aea [_ddd ]=_cg ;}else if _cc < -_cg {_fe .Log .Debug ("\u0043L\u0041M\u0050\u003a\u0020\u0025\u0067\u0020\u002d\u003e\u0020\u0025\u0067",_cc ,-_cg );_aea [_ddd ]=-_cg ;};};};func (_eg *Matrix )Clone ()Matrix {return NewMatrix (_eg [0],_eg [1],_eg [3],_eg [4],_eg [6],_eg [7])};func (_ccg Point )Interpolate (b Point ,t float64 )Point {return Point {X :(1-t )*_ccg .X +t *b .X ,Y :(1-t )*_ccg .Y +t *b .Y };};func RotationMatrix (angle float64 )Matrix {_fb :=_a .Cos (angle );_e :=_a .Sin (angle );return NewMatrix (_fb ,_e ,-_e ,_fb ,0,0);};func (_dda Matrix )Translate (tx ,ty float64 )Matrix {return NewMatrix (_dda [0],_dda [1],_dda [3],_dda [4],_dda [6]+tx ,_dda [7]+ty );};func (_ef Matrix )Translation ()(float64 ,float64 ){return _ef [6],_ef [7]};func (_g *Matrix )Set (a ,b ,c ,d ,tx ,ty float64 ){_g [0],_g [1]=a ,b ;_g [3],_g [4]=c ,d ;_g [6],_g [7]=tx ,ty ;_g .clampRange ();};func (_bg Point )String ()string {return _f .Sprintf ("(\u0025\u002e\u0032\u0066\u002c\u0025\u002e\u0032\u0066\u0029",_bg .X ,_bg .Y );};func (_ae Matrix )Inverse ()(Matrix ,bool ){_bb ,_fbd :=_ae [0],_ae [1];_ed ,_gd :=_ae [3],_ae [4];_aa ,_bd :=_ae [6],_ae [7];_egf :=_bb *_gd -_fbd *_ed ;if _a .Abs (_egf )< _db {return Matrix {},false ;};_bcd ,_gb :=_gd /_egf ,-_fbd /_egf ;_abe ,_abea :=-_ed /_egf ,_bb /_egf ;_cb :=-(_bcd *_aa +_abe *_bd );_cf :=-(_gb *_aa +_abea *_bd );return NewMatrix (_bcd ,_gb ,_abe ,_abea ,_cb ,_cf ),true ;};const _gc =1e-6;func (_cgb Point )Rotate (theta float64 )Point {_cbg :=_a .Hypot (_cgb .X ,_cgb .Y );_gac :=_a .Atan2 (_cgb .Y ,_cgb .X );_caeg ,_cd :=_a .Sincos (_gac +theta /180.0*_a .Pi );return Point {_cbg *_cd ,_cbg *_caeg };};const _cg =1e9;func (_ebd *Matrix )Concat (b Matrix ){*_ebd =Matrix {b [0]*_ebd [0]+b [1]*_ebd [3],b [0]*_ebd [1]+b [1]*_ebd [4],0,b [3]*_ebd [0]+b [4]*_ebd [3],b [3]*_ebd [1]+b [4]*_ebd [4],0,b [6]*_ebd [0]+b [7]*_ebd [3]+_ebd [6],b [6]*_ebd [1]+b [7]*_ebd [4]+_ebd [7],1};_ebd .clampRange ();};func (_fbg Matrix )ScalingFactorY ()float64 {return _a .Hypot (_fbg [3],_fbg [4])};func NewMatrixFromTransforms (xScale ,yScale ,theta ,tx ,ty float64 )Matrix {return IdentityMatrix ().Scale (xScale ,yScale ).Rotate (theta ).Translate (tx ,ty );};func (_ee Matrix )Rotate (theta float64 )Matrix {_ce ,_acg :=_a .Sincos (theta /180.0*_a .Pi );return _ee .Mult (NewMatrix (_acg ,-_ce ,_ce ,_acg ,0,0));};func TranslationMatrix (tx ,ty float64 )Matrix {return NewMatrix (1,0,0,1,tx ,ty )};const _db =1.0e-6;func (_fbe Matrix )Scale (xScale ,yScale float64 )Matrix {return _fbe .Mult (NewMatrix (xScale ,0,0,yScale ,0,0));};func (_df Point )Displace (delta Point )Point {return Point {_df .X +delta .X ,_df .Y +delta .Y }};func (_ba Matrix )Transform (x ,y float64 )(float64 ,float64 ){_ga :=x *_ba [0]+y *_ba [3]+_ba [6];_fee :=x *_ba [1]+y *_ba [4]+_ba [7];return _ga ,_fee ;};type Point struct{X float64 ;Y float64 ;};func (_ad *Point )Transform (a ,b ,c ,d ,tx ,ty float64 ){_ccd :=NewMatrix (a ,b ,c ,d ,tx ,ty );_ad .transformByMatrix (_ccd );};func (_cae Matrix )Unrealistic ()bool {_gg ,_fad ,_aed ,_fg :=_a .Abs (_cae [0]),_a .Abs (_cae [1]),_a .Abs (_cae [3]),_a .Abs (_cae [4]);_aaf :=_gg > _gc &&_fg > _gc ;_ccc :=_fad > _gc &&_aed > _gc ;return !(_aaf ||_ccc );};func ShearMatrix (x ,y float64 )Matrix {return NewMatrix (1,y ,x ,1,0,0)};func (_baa Point )Distance (b Point )float64 {return _a .Hypot (_baa .X -b .X ,_baa .Y -b .Y )};func (_aee *Point )transformByMatrix (_gf Matrix ){_aee .X ,_aee .Y =_gf .Transform (_aee .X ,_aee .Y )};func ScaleMatrix (x ,y float64 )Matrix {return NewMatrix (x ,0,0,y ,0,0)};func NewPoint (x ,y float64 )Point {return Point {X :x ,Y :y }};
|
File diff suppressed because one or more lines are too long
4679
model/model.go
4679
model/model.go
File diff suppressed because one or more lines are too long
@ -9,32 +9,30 @@
|
||||
// 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 optimize ;import (_cd "bytes";_d "crypto/md5";_bb "errors";_bf "fmt";_db "github.com/unidoc/unipdf/v3/common";_ca "github.com/unidoc/unipdf/v3/contentstream";_b "github.com/unidoc/unipdf/v3/core";_cf "github.com/unidoc/unipdf/v3/extractor";_bad "github.com/unidoc/unipdf/v3/internal/textencoding";_bd "github.com/unidoc/unipdf/v3/model";_dg "github.com/unidoc/unitype";_a "golang.org/x/image/draw";_f "image";_ba "math";);
|
||||
package optimize ;import (_c "bytes";_ab "crypto/md5";_aa "errors";_b "fmt";_dc "github.com/unidoc/unipdf/v3/common";_ge "github.com/unidoc/unipdf/v3/contentstream";_d "github.com/unidoc/unipdf/v3/core";_abb "github.com/unidoc/unipdf/v3/extractor";_ag "github.com/unidoc/unipdf/v3/internal/textencoding";_gf "github.com/unidoc/unipdf/v3/model";_ec "github.com/unidoc/unitype";_a "golang.org/x/image/draw";_e "image";_f "math";);
|
||||
|
||||
// Chain allows to use sequence of optimizers.
|
||||
// It implements interface model.Optimizer.
|
||||
type Chain struct{_bf []_gf .Optimizer };
|
||||
|
||||
// CleanFonts cleans up embedded fonts, reducing font sizes.
|
||||
type CleanFonts struct{
|
||||
|
||||
// Subset embedded fonts if encountered (if true).
|
||||
// Otherwise attempts to reduce the font program.
|
||||
Subset bool ;};func _fgcg (_ggce *_d .PdfObjectStream ,_gcc []rune ,_fga []_ec .GlyphIndex )error {_ggce ,_feb :=_d .GetStream (_ggce );if !_feb {_dc .Log .Debug ("\u0045\u006d\u0062\u0065\u0064\u0064\u0065\u0064\u0020\u0066\u006f\u006e\u0074\u0020\u006f\u0062\u006a\u0065c\u0074\u0020\u006e\u006f\u0074\u0020\u0066o\u0075\u006e\u0064\u0020\u002d\u002d\u0020\u0041\u0042\u004f\u0052T\u0020\u0073\u0075\u0062\u0073\u0065\u0074\u0074\u0069\u006e\u0067");return _aa .New ("\u0066\u006f\u006e\u0074fi\u006c\u0065\u0032\u0020\u006e\u006f\u0074\u0020\u0066\u006f\u0075\u006e\u0064");};_fbf ,_ggd :=_d .DecodeStream (_ggce );if _ggd !=nil {_dc .Log .Debug ("\u0044\u0065c\u006f\u0064\u0065 \u0065\u0072\u0072\u006f\u0072\u003a\u0020\u0025\u0076",_ggd );return _ggd ;};_def ,_ggd :=_ec .Parse (_c .NewReader (_fbf ));if _ggd !=nil {_dc .Log .Debug ("\u0045\u0072\u0072\u006f\u0072\u0020\u0070\u0061\u0072\u0073\u0069n\u0067\u0020\u0025\u0064\u0020\u0062\u0079\u0074\u0065\u0020f\u006f\u006e\u0074",len (_ggce .Stream ));return _ggd ;};_cda :=_fga ;if len (_gcc )> 0{_bbb :=_def .LookupRunes (_gcc );_cda =append (_cda ,_bbb ...);};_def ,_ggd =_def .SubsetKeepIndices (_cda );if _ggd !=nil {_dc .Log .Debug ("\u0045R\u0052\u004f\u0052\u0020s\u0075\u0062\u0073\u0065\u0074t\u0069n\u0067 \u0066\u006f\u006e\u0074\u003a\u0020\u0025v",_ggd );return _ggd ;};var _cgf _c .Buffer ;_ggd =_def .Write (&_cgf );if _ggd !=nil {_dc .Log .Debug ("\u0045\u0052\u0052\u004fR \u0057\u0072\u0069\u0074\u0069\u006e\u0067\u0020\u0066\u006f\u006e\u0074\u003a\u0020%\u0076",_ggd );return _ggd ;};if _cgf .Len ()> len (_fbf ){_dc .Log .Debug ("\u0052\u0065-\u0077\u0072\u0069\u0074\u0074\u0065\u006e\u0020\u0066\u006f\u006e\u0074\u0020\u0069\u0073\u0020\u006c\u0061\u0072\u0067\u0065\u0072\u0020\u0074\u0068\u0061\u006e\u0020\u006f\u0072\u0069\u0067\u0069\u006e\u0061\u006c\u0020\u002d\u0020\u0073\u006b\u0069\u0070");return nil ;};_fgac ,_ggd :=_d .MakeStream (_cgf .Bytes (),_d .NewFlateEncoder ());if _ggd !=nil {_dc .Log .Debug ("\u0045\u0052\u0052\u004fR \u0057\u0072\u0069\u0074\u0069\u006e\u0067\u0020\u0066\u006f\u006e\u0074\u003a\u0020%\u0076",_ggd );return _ggd ;};*_ggce =*_fgac ;_ggce .Set ("\u004ce\u006e\u0067\u0074\u0068\u0031",_d .MakeInteger (int64 (_cgf .Len ())));return nil ;};
|
||||
|
||||
// Optimize optimizes PDF objects to decrease PDF size.
|
||||
func (_bbeb *ObjectStreams )Optimize (objects []_b .PdfObject )(_ffa []_b .PdfObject ,_cbd error ){_dgcc :=&_b .PdfObjectStreams {};_ddd :=make ([]_b .PdfObject ,0,len (objects ));for _ ,_gbdf :=range objects {if _acd ,_dfac :=_gbdf .(*_b .PdfIndirectObject );_dfac &&_acd .GenerationNumber ==0{_dgcc .Append (_gbdf );}else {_ddd =append (_ddd ,_gbdf );};};if _dgcc .Len ()==0{return _ddd ,nil ;};_ffa =make ([]_b .PdfObject ,0,len (_ddd )+_dgcc .Len ()+1);if _dgcc .Len ()> 1{_ffa =append (_ffa ,_dgcc );};_ffa =append (_ffa ,_dgcc .Elements ()...);_ffa =append (_ffa ,_ddd ...);return _ffa ,nil ;};
|
||||
func (_fb *CleanContentstream )Optimize (objects []_d .PdfObject )(_abc []_d .PdfObject ,_ccc error ){_gg :=map[*_d .PdfObjectStream ]struct{}{};var _geg []*_d .PdfObjectStream ;_feg :=func (_ece *_d .PdfObjectStream ){if _ ,_gd :=_gg [_ece ];!_gd {_gg [_ece ]=struct{}{};_geg =append (_geg ,_ece );};};for _ ,_bed :=range objects {switch _bga :=_bed .(type ){case *_d .PdfIndirectObject :switch _bgd :=_bga .PdfObject .(type ){case *_d .PdfObjectDictionary :if _ed ,_af :=_d .GetName (_bgd .Get ("\u0054\u0079\u0070\u0065"));!_af ||_ed .String ()!="\u0050\u0061\u0067\u0065"{continue ;};if _bec ,_ba :=_d .GetStream (_bgd .Get ("\u0043\u006f\u006e\u0074\u0065\u006e\u0074\u0073"));_ba {_feg (_bec );}else if _bag ,_bgf :=_d .GetArray (_bgd .Get ("\u0043\u006f\u006e\u0074\u0065\u006e\u0074\u0073"));_bgf {for _ ,_ee :=range _bag .Elements (){if _cag ,_fea :=_d .GetStream (_ee );_fea {_feg (_cag );};};};};case *_d .PdfObjectStream :if _efe ,_dd :=_d .GetName (_bga .Get ("\u0054\u0079\u0070\u0065"));!_dd ||_efe .String ()!="\u0058O\u0062\u006a\u0065\u0063\u0074"{continue ;};if _cdg ,_bb :=_d .GetName (_bga .Get ("\u0053u\u0062\u0074\u0079\u0070\u0065"));!_bb ||_cdg .String ()!="\u0046\u006f\u0072\u006d"{continue ;};_feg (_bga );};};for _ ,_aab :=range _geg {_ccc =_fe (_aab );if _ccc !=nil {return nil ,_ccc ;};};return objects ,nil ;};func _fccf (_dedge _d .PdfObject )(_ddf string ,_daac []_d .PdfObject ){var _gge _c .Buffer ;switch _gacc :=_dedge .(type ){case *_d .PdfIndirectObject :_daac =append (_daac ,_gacc );_dedge =_gacc .PdfObject ;};switch _dadd :=_dedge .(type ){case *_d .PdfObjectStream :if _ecee ,_begg :=_d .DecodeStream (_dadd );_begg ==nil {_gge .Write (_ecee );_daac =append (_daac ,_dadd );};case *_d .PdfObjectArray :for _ ,_fag :=range _dadd .Elements (){switch _gffb :=_fag .(type ){case *_d .PdfObjectStream :if _cagd ,_bdfc :=_d .DecodeStream (_gffb );_bdfc ==nil {_gge .Write (_cagd );_daac =append (_daac ,_gffb );};};};};return _gge .String (),_daac ;};
|
||||
|
||||
// Optimize optimizes PDF objects to decrease PDF size.
|
||||
func (_bde *Chain )Optimize (objects []_b .PdfObject )(_cae []_b .PdfObject ,_dbc error ){_cae =objects ;for _ ,_cg :=range _bde ._fd {_cae ,_dbc =_cg .Optimize (_cae );if _dbc !=nil {return _cae ,_dbc ;};};return _cae ,nil ;};
|
||||
|
||||
// Append appends optimizers to the chain.
|
||||
func (_aa *Chain )Append (optimizers ..._bd .Optimizer ){_aa ._fd =append (_aa ._fd ,optimizers ...)};type content struct{_dde string ;_ddec *_bd .PdfPageResources ;};
|
||||
|
||||
// CombineDuplicateDirectObjects combines duplicated direct objects by its data hash.
|
||||
// It implements interface model.Optimizer.
|
||||
type CombineDuplicateDirectObjects struct{};func _agg (_dda []_b .PdfObject )[]*imageInfo {_bfd :=_b .PdfObjectName ("\u0053u\u0062\u0074\u0079\u0070\u0065");_age :=make (map[*_b .PdfObjectStream ]struct{});var _dgbg error ;var _aac []*imageInfo ;for _ ,_fdec :=range _dda {_becf ,_fcf :=_b .GetStream (_fdec );if !_fcf {continue ;};if _ ,_dfg :=_age [_becf ];_dfg {continue ;};_age [_becf ]=struct{}{};_cabc :=_becf .PdfObjectDictionary .Get (_bfd );_agfa ,_fcf :=_b .GetName (_cabc );if !_fcf ||string (*_agfa )!="\u0049\u006d\u0061g\u0065"{continue ;};_ggd :=&imageInfo {BitsPerComponent :8,Stream :_becf };if _ggd .ColorSpace ,_dgbg =_bd .DetermineColorspaceNameFromPdfObject (_becf .PdfObjectDictionary .Get ("\u0043\u006f\u006c\u006f\u0072\u0053\u0070\u0061\u0063\u0065"));_dgbg !=nil {_db .Log .Error ("\u0045\u0072\u0072\u006f\u0072\u0020\u0064\u0065\u0074\u0065r\u006d\u0069\u006e\u0065\u0020\u0063\u006fl\u006f\u0072\u0020\u0073\u0070\u0061\u0063\u0065\u0020\u0025\u0073",_dgbg );continue ;};if _egc ,_edg :=_b .GetIntVal (_becf .PdfObjectDictionary .Get ("\u0042\u0069t\u0073\u0050\u0065r\u0043\u006f\u006d\u0070\u006f\u006e\u0065\u006e\u0074"));_edg {_ggd .BitsPerComponent =_egc ;};if _fdd ,_ceb :=_b .GetIntVal (_becf .PdfObjectDictionary .Get ("\u0057\u0069\u0064t\u0068"));_ceb {_ggd .Width =_fdd ;};if _cgg ,_eff :=_b .GetIntVal (_becf .PdfObjectDictionary .Get ("\u0048\u0065\u0069\u0067\u0068\u0074"));_eff {_ggd .Height =_cgg ;};switch _ggd .ColorSpace {case "\u0044e\u0076\u0069\u0063\u0065\u0052\u0047B":_ggd .ColorComponents =3;case "\u0044\u0065\u0076\u0069\u0063\u0065\u0047\u0072\u0061\u0079":_ggd .ColorComponents =1;default:_db .Log .Warning ("\u004f\u0070\u0074\u0069\u006d\u0069\u007a\u0061t\u0069\u006f\u006e i\u0073\u0020\u006e\u006f\u0074\u0020s\u0075\u0070\u0070\u006f\u0072\u0074\u0065\u0064\u0020\u0066\u006f\u0072\u0020\u0063\u006fl\u006f\u0072\u0020\u0073\u0070\u0061\u0063\u0065 \u0025\u0073",_ggd .ColorSpace );continue ;};_aac =append (_aac ,_ggd );};return _aac ;};func _ddafb (_fcgg _b .PdfObject )(_cafff string ,_efce []_b .PdfObject ){var _bfe _cd .Buffer ;switch _fece :=_fcgg .(type ){case *_b .PdfIndirectObject :_efce =append (_efce ,_fece );_fcgg =_fece .PdfObject ;};switch _dfba :=_fcgg .(type ){case *_b .PdfObjectStream :if _eccd ,_aged :=_b .DecodeStream (_dfba );_aged ==nil {_bfe .Write (_eccd );_efce =append (_efce ,_dfba );};case *_b .PdfObjectArray :for _ ,_accd :=range _dfba .Elements (){switch _agd :=_accd .(type ){case *_b .PdfObjectStream :if _adgge ,_ccda :=_b .DecodeStream (_agd );_ccda ==nil {_bfe .Write (_adgge );_efce =append (_efce ,_agd );};};};};return _bfe .String (),_efce ;};
|
||||
|
||||
// ImagePPI optimizes images by scaling images such that the PPI (pixels per inch) is never higher than ImageUpperPPI.
|
||||
// TODO(a5i): Add support for inline images.
|
||||
// It implements interface model.Optimizer.
|
||||
type ImagePPI struct{ImageUpperPPI float64 ;};type objectStructure struct{_gfbb *_b .PdfObjectDictionary ;_bbff *_b .PdfObjectDictionary ;_fdeb []*_b .PdfIndirectObject ;};func _fgcd (_dcbd *_b .PdfObjectStream ,_aeb []rune ,_dgg []_dg .GlyphIndex )error {_dcbd ,_cdfe :=_b .GetStream (_dcbd );if !_cdfe {_db .Log .Debug ("\u0045\u006d\u0062\u0065\u0064\u0064\u0065\u0064\u0020\u0066\u006f\u006e\u0074\u0020\u006f\u0062\u006a\u0065c\u0074\u0020\u006e\u006f\u0074\u0020\u0066o\u0075\u006e\u0064\u0020\u002d\u002d\u0020\u0041\u0042\u004f\u0052T\u0020\u0073\u0075\u0062\u0073\u0065\u0074\u0074\u0069\u006e\u0067");return _bb .New ("\u0066\u006f\u006e\u0074fi\u006c\u0065\u0032\u0020\u006e\u006f\u0074\u0020\u0066\u006f\u0075\u006e\u0064");};_dcf ,_bdeg :=_b .DecodeStream (_dcbd );if _bdeg !=nil {_db .Log .Debug ("\u0044\u0065c\u006f\u0064\u0065 \u0065\u0072\u0072\u006f\u0072\u003a\u0020\u0025\u0076",_bdeg );return _bdeg ;};_gdd ,_bdeg :=_dg .Parse (_cd .NewReader (_dcf ));if _bdeg !=nil {_db .Log .Debug ("\u0045\u0072\u0072\u006f\u0072\u0020\u0070\u0061\u0072\u0073\u0069n\u0067\u0020\u0025\u0064\u0020\u0062\u0079\u0074\u0065\u0020f\u006f\u006e\u0074",len (_dcbd .Stream ));return _bdeg ;};_adb :=_dgg ;if len (_aeb )> 0{_ab :=_gdd .LookupRunes (_aeb );_adb =append (_adb ,_ab ...);};_gdd ,_bdeg =_gdd .SubsetKeepIndices (_adb );if _bdeg !=nil {_db .Log .Debug ("\u0045R\u0052\u004f\u0052\u0020s\u0075\u0062\u0073\u0065\u0074t\u0069n\u0067 \u0066\u006f\u006e\u0074\u003a\u0020\u0025v",_bdeg );return _bdeg ;};var _ace _cd .Buffer ;_bdeg =_gdd .Write (&_ace );if _bdeg !=nil {_db .Log .Debug ("\u0045\u0052\u0052\u004fR \u0057\u0072\u0069\u0074\u0069\u006e\u0067\u0020\u0066\u006f\u006e\u0074\u003a\u0020%\u0076",_bdeg );return _bdeg ;};if _ace .Len ()> len (_dcf ){_db .Log .Debug ("\u0052\u0065-\u0077\u0072\u0069\u0074\u0074\u0065\u006e\u0020\u0066\u006f\u006e\u0074\u0020\u0069\u0073\u0020\u006c\u0061\u0072\u0067\u0065\u0072\u0020\u0074\u0068\u0061\u006e\u0020\u006f\u0072\u0069\u0067\u0069\u006e\u0061\u006c\u0020\u002d\u0020\u0073\u006b\u0069\u0070");return nil ;};_ebd ,_bdeg :=_b .MakeStream (_ace .Bytes (),_b .NewFlateEncoder ());if _bdeg !=nil {_db .Log .Debug ("\u0045\u0052\u0052\u004fR \u0057\u0072\u0069\u0074\u0069\u006e\u0067\u0020\u0066\u006f\u006e\u0074\u003a\u0020%\u0076",_bdeg );return _bdeg ;};*_dcbd =*_ebd ;_dcbd .Set ("\u004ce\u006e\u0067\u0074\u0068\u0031",_b .MakeInteger (int64 (_ace .Len ())));return nil ;};
|
||||
|
||||
// CombineDuplicateStreams combines duplicated streams by its data hash.
|
||||
// It implements interface model.Optimizer.
|
||||
type CombineDuplicateStreams struct{};
|
||||
func (_be *Chain )Optimize (objects []_d .PdfObject )(_abg []_d .PdfObject ,_gbc error ){_abg =objects ;for _ ,_gc :=range _be ._bf {_abg ,_gbc =_gc .Optimize (_abg );if _gbc !=nil {return _abg ,_gbc ;};};return _abg ,nil ;};
|
||||
|
||||
// Optimize optimizes PDF objects to decrease PDF size.
|
||||
func (_ggcg *CombineDuplicateStreams )Optimize (objects []_b .PdfObject )(_cce []_b .PdfObject ,_ceg error ){_ffe :=make (map[_b .PdfObject ]_b .PdfObject );_cfca :=make (map[_b .PdfObject ]struct{});_ebgc :=make (map[string ][]*_b .PdfObjectStream );for _ ,_gcb :=range objects {if _aca ,_efe :=_gcb .(*_b .PdfObjectStream );_efe {_ecd :=_d .New ();_ecd .Write ([]byte (_aca .Stream ));_add :=string (_ecd .Sum (nil ));_ebgc [_add ]=append (_ebgc [_add ],_aca );};};for _ ,_caaf :=range _ebgc {if len (_caaf )< 2{continue ;};_fdc :=_caaf [0];for _bbcf :=1;_bbcf < len (_caaf );_bbcf ++{_bfgb :=_caaf [_bbcf ];_ffe [_bfgb ]=_fdc ;_cfca [_bfgb ]=struct{}{};};};_cce =make ([]_b .PdfObject ,0,len (objects )-len (_cfca ));for _ ,_eae :=range objects {if _ ,_bbcb :=_cfca [_eae ];_bbcb {continue ;};_cce =append (_cce ,_eae );};_ddaf (_cce ,_ffe );return _cce ,nil ;};
|
||||
func (_aff *CleanFonts )Optimize (objects []_d .PdfObject )(_febd []_d .PdfObject ,_beca error ){var _cagf map[*_d .PdfObjectStream ]struct{};if _aff .Subset {var _fgg error ;_cagf ,_fgg =_de (objects );if _fgg !=nil {return nil ,_fgg ;};};for _ ,_efga :=range objects {_ga ,_gaa :=_d .GetStream (_efga );if !_gaa {continue ;};if _ ,_beab :=_cagf [_ga ];_beab {continue ;};_ecb ,_efdb :=_d .NewEncoderFromStream (_ga );if _efdb !=nil {_dc .Log .Debug ("\u0045\u0052RO\u0052\u0020\u0067e\u0074\u0074\u0069\u006eg e\u006eco\u0064\u0065\u0072\u003a\u0020\u0025\u0076 -\u0020\u0069\u0067\u006e\u006f\u0072\u0069n\u0067",_efdb );continue ;};_gad ,_efdb :=_ecb .DecodeStream (_ga );if _efdb !=nil {_dc .Log .Debug ("\u0044\u0065\u0063\u006f\u0064\u0069\u006e\u0067\u0020\u0065r\u0072\u006f\u0072\u0020\u003a\u0020\u0025v\u0020\u002d\u0020\u0069\u0067\u006e\u006f\u0072\u0069\u006e\u0067",_efdb );continue ;};if len (_gad )< 4{continue ;};_bad :=string (_gad [:4]);if _bad =="\u004f\u0054\u0054\u004f"{continue ;};if _bad !="\u0000\u0001\u0000\u0000"&&_bad !="\u0074\u0072\u0075\u0065"{continue ;};_egdg ,_efdb :=_ec .Parse (_c .NewReader (_gad ));if _efdb !=nil {_dc .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u0020P\u0061\u0072\u0073\u0069\u006e\u0067\u0020\u0066\u006f\u006e\u0074\u003a\u0020%\u0076\u0020\u002d\u0020\u0069\u0067\u006eo\u0072\u0069\u006e\u0067",_efdb );continue ;};_efdb =_egdg .Optimize ();if _efdb !=nil {continue ;};var _gccf _c .Buffer ;_efdb =_egdg .Write (&_gccf );if _efdb !=nil {_dc .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u0020W\u0072\u0069\u0074\u0069\u006e\u0067\u0020\u0066\u006f\u006e\u0074\u003a\u0020%\u0076\u0020\u002d\u0020\u0069\u0067\u006eo\u0072\u0069\u006e\u0067",_efdb );continue ;};if _gccf .Len ()> len (_gad ){_dc .Log .Debug ("\u0052\u0065-\u0077\u0072\u0069\u0074\u0074\u0065\u006e\u0020\u0066\u006f\u006e\u0074\u0020\u0069\u0073\u0020\u006c\u0061\u0072\u0067\u0065\u0072\u0020\u0074\u0068\u0061\u006e\u0020\u006f\u0072\u0069\u0067\u0069\u006e\u0061\u006c\u0020\u002d\u0020\u0073\u006b\u0069\u0070");continue ;};_bba ,_efdb :=_d .MakeStream (_gccf .Bytes (),_d .NewFlateEncoder ());if _efdb !=nil {continue ;};*_ga =*_bba ;_ga .Set ("\u004ce\u006e\u0067\u0074\u0068\u0031",_d .MakeInteger (int64 (_gccf .Len ())));};return objects ,nil ;};func _dedg (_efgae []_d .PdfObject ){for _dfgaa ,_eea :=range _efgae {switch _caec :=_eea .(type ){case *_d .PdfIndirectObject :_caec .ObjectNumber =int64 (_dfgaa +1);_caec .GenerationNumber =0;case *_d .PdfObjectStream :_caec .ObjectNumber =int64 (_dfgaa +1);_caec .GenerationNumber =0;case *_d .PdfObjectStreams :_caec .ObjectNumber =int64 (_dfgaa +1);_caec .GenerationNumber =0;};};};
|
||||
|
||||
// New creates a optimizers chain from options.
|
||||
func New (options Options )*Chain {_agdc :=new (Chain );if options .CleanFonts ||options .SubsetFonts {_agdc .Append (&CleanFonts {Subset :options .SubsetFonts });};if options .CleanContentstream {_agdc .Append (new (CleanContentstream ));};if options .ImageUpperPPI > 0{_feabb :=new (ImagePPI );_feabb .ImageUpperPPI =options .ImageUpperPPI ;_agdc .Append (_feabb );};if options .ImageQuality > 0{_affc :=new (Image );_affc .ImageQuality =options .ImageQuality ;_agdc .Append (_affc );};if options .CombineDuplicateDirectObjects {_agdc .Append (new (CombineDuplicateDirectObjects ));};if options .CombineDuplicateStreams {_agdc .Append (new (CombineDuplicateStreams ));};if options .CombineIdenticalIndirectObjects {_agdc .Append (new (CombineIdenticalIndirectObjects ));};if options .UseObjectStreams {_agdc .Append (new (ObjectStreams ));};if options .CompressStreams {_agdc .Append (new (CompressStreams ));};return _agdc ;};func _db (_eb *_ge .ContentStreamOperations )*_ge .ContentStreamOperations {if _eb ==nil {return nil ;};_bg :=_ge .ContentStreamOperations {};for _ ,_cf :=range *_eb {switch _cf .Operand {case "\u0042\u0044\u0043","\u0042\u004d\u0043","\u0045\u004d\u0043":continue ;case "\u0054\u006d":if len (_cf .Params )==6{if _cg ,_ca :=_d .GetNumbersAsFloat (_cf .Params );_ca ==nil {if _cg [0]==1&&_cg [1]==0&&_cg [2]==0&&_cg [3]==1{_cf =&_ge .ContentStreamOperation {Params :[]_d .PdfObject {_cf .Params [4],_cf .Params [5]},Operand :"\u0054\u0064"};};};};};_bg =append (_bg ,_cf );};return &_bg ;};
|
||||
|
||||
// CleanContentstream cleans up redundant operands in content streams, including Page and XObject Form
|
||||
// contents. This process includes:
|
||||
@ -44,56 +42,58 @@ func (_ggcg *CombineDuplicateStreams )Optimize (objects []_b .PdfObject )(_cce [
|
||||
type CleanContentstream struct{};
|
||||
|
||||
// Optimize optimizes PDF objects to decrease PDF size.
|
||||
func (_dcg *CombineIdenticalIndirectObjects )Optimize (objects []_b .PdfObject )(_ffg []_b .PdfObject ,_efc error ){_bbca (objects );_eba :=make (map[_b .PdfObject ]_b .PdfObject );_dac :=make (map[_b .PdfObject ]struct{});_defe :=make (map[string ][]*_b .PdfIndirectObject );for _ ,_caeag :=range objects {_bga ,_adbc :=_caeag .(*_b .PdfIndirectObject );if !_adbc {continue ;};if _edc ,_eaed :=_bga .PdfObject .(*_b .PdfObjectDictionary );_eaed {if _bbd ,_cab :=_edc .Get ("\u0054\u0079\u0070\u0065").(*_b .PdfObjectName );_cab &&*_bbd =="\u0050\u0061\u0067\u0065"{continue ;};_beee :=_d .New ();_beee .Write ([]byte (_edc .WriteString ()));_ddc :=string (_beee .Sum (nil ));_defe [_ddc ]=append (_defe [_ddc ],_bga );};};for _ ,_cfe :=range _defe {if len (_cfe )< 2{continue ;};_gdb :=_cfe [0];for _ccd :=1;_ccd < len (_cfe );_ccd ++{_baa :=_cfe [_ccd ];_eba [_baa ]=_gdb ;_dac [_baa ]=struct{}{};};};_ffg =make ([]_b .PdfObject ,0,len (objects )-len (_dac ));for _ ,_bbab :=range objects {if _ ,_bece :=_dac [_bbab ];_bece {continue ;};_ffg =append (_ffg ,_bbab );};_ddaf (_ffg ,_eba );return _ffg ,nil ;};
|
||||
func (_dad *CompressStreams )Optimize (objects []_d .PdfObject )(_bbec []_d .PdfObject ,_cgbb error ){_bbec =make ([]_d .PdfObject ,len (objects ));copy (_bbec ,objects );for _ ,_edb :=range objects {_ddeb ,_caa :=_d .GetStream (_edb );if !_caa {continue ;};if _eed :=_ddeb .Get ("\u0046\u0069\u006c\u0074\u0065\u0072");_eed !=nil {if _ ,_bee :=_d .GetName (_eed );_bee {continue ;};if _eeg ,_aca :=_d .GetArray (_eed );_aca &&_eeg .Len ()> 0{continue ;};};_gcd :=_d .NewFlateEncoder ();var _bde []byte ;_bde ,_cgbb =_gcd .EncodeBytes (_ddeb .Stream );if _cgbb !=nil {return _bbec ,_cgbb ;};_ddd :=_gcd .MakeStreamDict ();if len (_bde )+len (_ddd .WriteString ())< len (_ddeb .Stream ){_ddeb .Stream =_bde ;_ddeb .PdfObjectDictionary .Merge (_ddd );_ddeb .PdfObjectDictionary .Set ("\u004c\u0065\u006e\u0067\u0074\u0068",_d .MakeInteger (int64 (len (_ddeb .Stream ))));};};return _bbec ,nil ;};
|
||||
|
||||
// CombineDuplicateStreams combines duplicated streams by its data hash.
|
||||
// It implements interface model.Optimizer.
|
||||
type CombineDuplicateStreams struct{};type content struct{_gbe string ;_fee *_gf .PdfPageResources ;};
|
||||
|
||||
// Optimize optimizes PDF objects to decrease PDF size.
|
||||
func (_bgdg *Image )Optimize (objects []_b .PdfObject )(_bbdd []_b .PdfObject ,_aab error ){if _bgdg .ImageQuality <=0{return objects ,nil ;};_gbc :=_agg (objects );if len (_gbc )==0{return objects ,nil ;};_adg :=make (map[_b .PdfObject ]_b .PdfObject );_becc :=make (map[_b .PdfObject ]struct{});for _ ,_dcc :=range _gbc {_fdcb :=_dcc .Stream .PdfObjectDictionary .Get (_b .PdfObjectName ("\u0053\u004d\u0061s\u006b"));_becc [_fdcb ]=struct{}{};};for _ebb ,_dfcb :=range _gbc {_cfb :=_dfcb .Stream ;if _ ,_ggeg :=_becc [_cfb ];_ggeg {continue ;};_aege ,_ggfg :=_b .NewEncoderFromStream (_cfb );if _ggfg !=nil {_db .Log .Warning ("\u0045\u0072\u0072\u006f\u0072 \u0067\u0065\u0074\u0020\u0065\u006e\u0063\u006f\u0064\u0065\u0072\u0020\u0066o\u0072\u0020\u0074\u0068\u0065\u0020\u0069\u006d\u0061\u0067\u0065\u0020\u0073\u0074\u0072\u0065\u0061\u006d\u0020\u0025\u0073");continue ;};_cddd ,_ggfg :=_aege .DecodeStream (_cfb );if _ggfg !=nil {_db .Log .Warning ("\u0045\u0072\u0072\u006f\u0072\u0020\u0064\u0065\u0063\u006f\u0064\u0065\u0020\u0074\u0068e\u0020i\u006d\u0061\u0067\u0065\u0020\u0073\u0074\u0072\u0065\u0061\u006d\u0020\u0025\u0073");continue ;};_feg :=_b .NewDCTEncoder ();_feg .ColorComponents =_dfcb .ColorComponents ;_feg .Quality =_bgdg .ImageQuality ;_feg .BitsPerComponent =_dfcb .BitsPerComponent ;_feg .Width =_dfcb .Width ;_feg .Height =_dfcb .Height ;_dgf ,_ggfg :=_feg .EncodeBytes (_cddd );if _ggfg !=nil {_db .Log .Debug ("\u0045R\u0052\u004f\u0052\u003a\u0020\u0025v",_ggfg );return nil ,_ggfg ;};var _dfe _b .StreamEncoder ;_dfe =_feg ;{_dfa :=_b .NewFlateEncoder ();_bdcf :=_b .NewMultiEncoder ();_bdcf .AddEncoder (_dfa );_bdcf .AddEncoder (_feg );_ffbd ,_dgae :=_bdcf .EncodeBytes (_cddd );if _dgae !=nil {return nil ,_dgae ;};if len (_ffbd )< len (_dgf ){_db .Log .Debug ("\u004d\u0075\u006c\u0074\u0069\u0020\u0065\u006e\u0063\u0020\u0069\u006d\u0070\u0072\u006f\u0076\u0065\u0073\u003a\u0020\u0025\u0064\u0020\u0074o\u0020\u0025\u0064\u0020\u0028o\u0072\u0069g\u0020\u0025\u0064\u0029",len (_dgf ),len (_ffbd ),len (_cfb .Stream ));_dgf =_ffbd ;_dfe =_bdcf ;};};_gga :=len (_cfb .Stream );if _gga < len (_dgf ){continue ;};_agfe :=&_b .PdfObjectStream {Stream :_dgf };_agfe .PdfObjectReference =_cfb .PdfObjectReference ;_agfe .PdfObjectDictionary =_b .MakeDict ();_agfe .Merge (_cfb .PdfObjectDictionary );_agfe .Merge (_dfe .MakeStreamDict ());_agfe .Set ("\u004c\u0065\u006e\u0067\u0074\u0068",_b .MakeInteger (int64 (len (_dgf ))));_adg [_cfb ]=_agfe ;_gbc [_ebb ].Stream =_agfe ;};_bbdd =make ([]_b .PdfObject ,len (objects ));copy (_bbdd ,objects );_ddaf (_bbdd ,_adg );return _bbdd ,nil ;};
|
||||
func (_dge *ImagePPI )Optimize (objects []_d .PdfObject )(_bbg []_d .PdfObject ,_bbeea error ){if _dge .ImageUpperPPI <=0{return objects ,nil ;};_cgag :=_ccd (objects );if len (_cgag )==0{return objects ,nil ;};_fegd :=make (map[_d .PdfObject ]struct{});for _ ,_ddag :=range _cgag {_fbg :=_ddag .Stream .PdfObjectDictionary .Get (_d .PdfObjectName ("\u0053\u004d\u0061s\u006b"));_fegd [_fbg ]=struct{}{};};_ffa :=make (map[*_d .PdfObjectStream ]*imageInfo );for _ ,_dcg :=range _cgag {_ffa [_dcg .Stream ]=_dcg ;};var _gdcd *_d .PdfObjectDictionary ;for _ ,_faf :=range objects {if _agbeg ,_cdd :=_d .GetDict (_faf );_gdcd ==nil &&_cdd {if _bac ,_gcec :=_d .GetName (_agbeg .Get (_d .PdfObjectName ("\u0054\u0079\u0070\u0065")));_gcec &&*_bac =="\u0043a\u0074\u0061\u006c\u006f\u0067"{_gdcd =_agbeg ;};};};if _gdcd ==nil {return objects ,nil ;};_caeaa ,_fgbc :=_d .GetDict (_gdcd .Get (_d .PdfObjectName ("\u0050\u0061\u0067e\u0073")));if !_fgbc {return objects ,nil ;};_fdgb ,_cgfc :=_d .GetArray (_caeaa .Get (_d .PdfObjectName ("\u004b\u0069\u0064\u0073")));if !_cgfc {return objects ,nil ;};_cfe :=make (map[string ]*imageInfo );for _ ,_fae :=range _fdgb .Elements (){_cafgg ,_gega :=_d .GetDict (_fae );if !_gega {continue ;};_bbf ,_dfdb :=_d .GetArray (_cafgg .Get ("\u0043\u006f\u006e\u0074\u0065\u006e\u0074\u0073"));if !_dfdb {continue ;};_badg ,_gfca :=_d .GetDict (_cafgg .Get ("\u0052e\u0073\u006f\u0075\u0072\u0063\u0065s"));if !_gfca {continue ;};_ggab ,_egce :=_d .GetDict (_badg .Get ("\u0058O\u0062\u006a\u0065\u0063\u0074"));if !_egce {continue ;};_gedc :=_ggab .Keys ();for _ ,_geec :=range _gedc {if _bdf ,_ddda :=_d .GetStream (_ggab .Get (_geec ));_ddda {if _begc ,_accd :=_ffa [_bdf ];_accd {_cfe [string (_geec )]=_begc ;};};};for _ ,_cafb :=range _bbf .Elements (){if _eecg ,_adc :=_d .GetStream (_cafb );_adc {_dca ,_eff :=_d .NewEncoderFromStream (_eecg );if _eff !=nil {return nil ,_eff ;};_febg ,_eff :=_dca .DecodeStream (_eecg );if _eff !=nil {return nil ,_eff ;};_bdbc :=_ge .NewContentStreamParser (string (_febg ));_bff ,_eff :=_bdbc .Parse ();if _eff !=nil {return nil ,_eff ;};_fecd ,_edc :=1.0,1.0;for _ ,_dgd :=range *_bff {if _dgd .Operand =="\u0051"{_fecd ,_edc =1.0,1.0;};if _dgd .Operand =="\u0063\u006d"&&len (_dgd .Params )==6{if _ggbe ,_abgb :=_d .GetFloatVal (_dgd .Params [0]);_abgb {_fecd =_fecd *_ggbe ;};if _aadb ,_cdgd :=_d .GetFloatVal (_dgd .Params [3]);_cdgd {_edc =_edc *_aadb ;};if _ead ,_ddca :=_d .GetIntVal (_dgd .Params [0]);_ddca {_fecd =_fecd *float64 (_ead );};if _fef ,_gffff :=_d .GetIntVal (_dgd .Params [3]);_gffff {_edc =_edc *float64 (_fef );};};if _dgd .Operand =="\u0044\u006f"&&len (_dgd .Params )==1{_ffde ,_aagb :=_d .GetName (_dgd .Params [0]);if !_aagb {continue ;};if _bce ,_cfaa :=_cfe [string (*_ffde )];_cfaa {_adb ,_eefa :=_fecd /72.0,_edc /72.0;_cge ,_bcef :=float64 (_bce .Width )/_adb ,float64 (_bce .Height )/_eefa ;if _adb ==0||_eefa ==0{_cge =72.0;_bcef =72.0;};_bce .PPI =_f .Max (_bce .PPI ,_cge );_bce .PPI =_f .Max (_bce .PPI ,_bcef );};};};};};};for _ ,_bede :=range _cgag {if _ ,_aaab :=_fegd [_bede .Stream ];_aaab {continue ;};if _bede .PPI <=_dge .ImageUpperPPI {continue ;};_cbag :=_dge .ImageUpperPPI /_bede .PPI ;if _fdd :=_fgace (_bede .Stream ,_cbag );_fdd !=nil {_dc .Log .Debug ("\u0045\u0072\u0072\u006f\u0072 \u0073\u0063\u0061\u006c\u0065\u0020\u0069\u006d\u0061\u0067\u0065\u0020\u006be\u0065\u0070\u0020\u006f\u0072\u0069\u0067\u0069\u006e\u0061\u006c\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_fdd );}else {if _gbg ,_cdbc :=_d .GetStream (_bede .Stream .PdfObjectDictionary .Get (_d .PdfObjectName ("\u0053\u004d\u0061s\u006b")));_cdbc {if _ffe :=_fgace (_gbg ,_cbag );_ffe !=nil {return nil ,_ffe ;};};};};return objects ,nil ;};
|
||||
|
||||
// Options describes PDF optimization parameters.
|
||||
type Options struct{CombineDuplicateStreams bool ;CombineDuplicateDirectObjects bool ;ImageUpperPPI float64 ;ImageQuality int ;UseObjectStreams bool ;CombineIdenticalIndirectObjects bool ;CompressStreams bool ;CleanFonts bool ;SubsetFonts bool ;CleanContentstream bool ;};func _ebc (_bfff _b .PdfObject )[]content {if _bfff ==nil {return nil ;};_gge ,_ebcd :=_b .GetArray (_bfff );if !_ebcd {_db .Log .Debug ("\u0041\u006e\u006e\u006fts\u0020\u006e\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");return nil ;};var _bge []content ;for _ ,_gba :=range _gge .Elements (){_bbc ,_bfcd :=_b .GetDict (_gba );if !_bfcd {_db .Log .Debug ("I\u0067\u006e\u006f\u0072\u0069\u006eg\u0020\u006e\u006f\u006e\u002d\u0064i\u0063\u0074\u0020\u0065\u006c\u0065\u006de\u006e\u0074\u0020\u0069\u006e\u0020\u0041\u006e\u006e\u006ft\u0073");continue ;};_edf ,_bfcd :=_b .GetDict (_bbc .Get ("\u0041\u0050"));if !_bfcd {_db .Log .Debug ("\u004e\u006f\u0020\u0041P \u0065\u006e\u0074\u0072\u0079\u0020\u002d\u0020\u0073\u006b\u0069\u0070\u0070\u0069n\u0067");continue ;};_ebg :=_b .TraceToDirectObject (_edf .Get ("\u004e"));if _ebg ==nil {_db .Log .Debug ("N\u006f\u0020\u004e\u0020en\u0074r\u0079\u0020\u002d\u0020\u0073k\u0069\u0070\u0070\u0069\u006e\u0067");continue ;};var _aegb *_b .PdfObjectStream ;switch _bgef :=_ebg .(type ){case *_b .PdfObjectDictionary :_ee ,_bffa :=_b .GetName (_bbc .Get ("\u0041\u0053"));if !_bffa {_db .Log .Debug ("\u004e\u006f\u0020\u0041S \u0065\u006e\u0074\u0072\u0079\u0020\u002d\u0020\u0073\u006b\u0069\u0070\u0070\u0069n\u0067");continue ;};_aegb ,_bffa =_b .GetStream (_bgef .Get (*_ee ));if !_bffa {_db .Log .Debug ("\u0046o\u0072\u006d\u0020\u006eo\u0074\u0020\u0066\u006f\u0075n\u0064 \u002d \u0073\u006b\u0069\u0070\u0070\u0069\u006eg");continue ;};case *_b .PdfObjectStream :_aegb =_bgef ;};if _aegb ==nil {_db .Log .Debug ("\u0046\u006f\u0072m\u0020\u006e\u006f\u0074 \u0066\u006f\u0075\u006e\u0064\u0020\u0028n\u0069\u006c\u0029\u0020\u002d\u0020\u0073\u006b\u0069\u0070\u0070\u0069\u006e\u0067");continue ;};_cb ,_bab :=_bd .NewXObjectFormFromStream (_aegb );if _bab !=nil {_db .Log .Debug ("\u0045\u0072\u0072\u006f\u0072\u0020l\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0066\u006f\u0072\u006d\u003a\u0020%\u0076\u0020\u002d\u0020\u0069\u0067\u006eo\u0072\u0069\u006e\u0067",_bab );continue ;};_bgd ,_bab :=_cb .GetContentStream ();if _bab !=nil {_db .Log .Debug ("E\u0072\u0072\u006f\u0072\u0020\u0064e\u0063\u006f\u0064\u0069\u006e\u0067\u0020\u0063\u006fn\u0074\u0065\u006et\u0073:\u0020\u0025\u0076",_bab );continue ;};_bge =append (_bge ,content {_dde :string (_bgd ),_ddec :_cb .Resources });};return _bge ;};
|
||||
// ImagePPI optimizes images by scaling images such that the PPI (pixels per inch) is never higher than ImageUpperPPI.
|
||||
// TODO(a5i): Add support for inline images.
|
||||
// It implements interface model.Optimizer.
|
||||
type ImagePPI struct{ImageUpperPPI float64 ;};type objectStructure struct{_egdb *_d .PdfObjectDictionary ;_daf *_d .PdfObjectDictionary ;_aae []*_d .PdfIndirectObject ;};func _bcd (_egb []_d .PdfObject ,_ffaa map[_d .PdfObject ]_d .PdfObject ){if _ffaa ==nil ||len (_ffaa )==0{return ;};for _dfga ,_ecdd :=range _egb {if _caeg ,_ffeb :=_ffaa [_ecdd ];_ffeb {_egb [_dfga ]=_caeg ;continue ;};_ffaa [_ecdd ]=_ecdd ;switch _ceed :=_ecdd .(type ){case *_d .PdfObjectArray :_bdbf :=make ([]_d .PdfObject ,_ceed .Len ());copy (_bdbf ,_ceed .Elements ());_bcd (_bdbf ,_ffaa );for _cccb ,_acbb :=range _bdbf {_ceed .Set (_cccb ,_acbb );};case *_d .PdfObjectStreams :_bcd (_ceed .Elements (),_ffaa );case *_d .PdfObjectStream :_fca :=[]_d .PdfObject {_ceed .PdfObjectDictionary };_bcd (_fca ,_ffaa );_ceed .PdfObjectDictionary =_fca [0].(*_d .PdfObjectDictionary );case *_d .PdfObjectDictionary :_dbb :=_ceed .Keys ();_feec :=make ([]_d .PdfObject ,len (_dbb ));for _aac ,_agdf :=range _dbb {_feec [_aac ]=_ceed .Get (_agdf );};_bcd (_feec ,_ffaa );for _fegc ,_ffcf :=range _dbb {_ceed .Set (_ffcf ,_feec [_fegc ]);};case *_d .PdfIndirectObject :_acd :=[]_d .PdfObject {_ceed .PdfObject };_bcd (_acd ,_ffaa );_ceed .PdfObject =_acd [0];};};};func _de (_bedb []_d .PdfObject )(_feab map[*_d .PdfObjectStream ]struct{},_bea error ){_feab =map[*_d .PdfObjectStream ]struct{}{};_beaf :=map[*_gf .PdfFont ]struct{}{};_eda :=_fdb (_bedb );for _ ,_eg :=range _eda ._aae {_daa ,_efd :=_d .GetDict (_eg .PdfObject );if !_efd {continue ;};_ggb ,_efd :=_d .GetDict (_daa .Get ("\u0052e\u0073\u006f\u0075\u0072\u0063\u0065s"));if !_efd {continue ;};_efg ,_ :=_fccf (_daa .Get ("\u0043\u006f\u006e\u0074\u0065\u006e\u0074\u0073"));_fc ,_dg :=_gf .NewPdfPageResourcesFromDict (_ggb );if _dg !=nil {return nil ,_dg ;};_ebd :=[]content {{_gbe :_efg ,_fee :_fc }};_cfc :=_afc (_daa .Get ("\u0041\u006e\u006e\u006f\u0074\u0073"));if _cfc !=nil {_ebd =append (_ebd ,_cfc ...);};for _ ,_ac :=range _ebd {_ggc ,_eeb :=_abb .NewFromContents (_ac ._gbe ,_ac ._fee );if _eeb !=nil {return nil ,_eeb ;};_cgb ,_ ,_ ,_eeb :=_ggc .ExtractPageText ();if _eeb !=nil {return nil ,_eeb ;};for _ ,_fg :=range _cgb .Marks ().Elements (){if _fg .Font ==nil {continue ;};if _ ,_dbf :=_beaf [_fg .Font ];!_dbf {_beaf [_fg .Font ]=struct{}{};};};};};_agb :=map[*_d .PdfObjectStream ][]*_gf .PdfFont {};for _dfd :=range _beaf {_cdb :=_dfd .FontDescriptor ();if _cdb ==nil ||_cdb .FontFile2 ==nil {continue ;};_fcd ,_dfa :=_d .GetStream (_cdb .FontFile2 );if !_dfa {continue ;};_agb [_fcd ]=append (_agb [_fcd ],_dfd );};for _ae :=range _agb {var _egd []rune ;var _ff []_ec .GlyphIndex ;for _ ,_cfa :=range _agb [_ae ]{switch _eba :=_cfa .Encoder ().(type ){case *_ag .IdentityEncoder :_fgc :=_eba .RegisteredRunes ();_feabg :=make ([]_ec .GlyphIndex ,len (_fgc ));for _dfg ,_eaa :=range _fgc {_feabg [_dfg ]=_ec .GlyphIndex (_eaa );};_ff =append (_ff ,_feabg ...);case *_ag .TrueTypeFontEncoder :_dfc :=_eba .RegisteredRunes ();_egd =append (_egd ,_dfc ...);case _ag .SimpleEncoder :_ggbf :=_eba .Charcodes ();for _ ,_ad :=range _ggbf {_cgbf ,_fec :=_eba .CharcodeToRune (_ad );if !_fec {_dc .Log .Debug ("\u0043\u0068a\u0072\u0063\u006f\u0064\u0065\u003c\u002d\u003e\u0072\u0075\u006e\u0065\u0020\u006e\u006f\u0074\u0020\u0066\u006f\u0075\u006e\u0064: \u0025\u0064",_ad );continue ;};_egd =append (_egd ,_cgbf );};};};_bea =_fgcg (_ae ,_egd ,_ff );if _bea !=nil {_dc .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u0020\u0073\u0075\u0062\u0073\u0065\u0074\u0074\u0069\u006eg\u0020f\u006f\u006e\u0074\u0020\u0073\u0074\u0072\u0065\u0061\u006d\u003a\u0020\u0025\u0076",_bea );return nil ,_bea ;};_feab [_ae ]=struct{}{};};return _feab ,nil ;};
|
||||
|
||||
// Image optimizes images by rewrite images into JPEG format with quality equals to ImageQuality.
|
||||
// TODO(a5i): Add support for inline images.
|
||||
// It implements interface model.Optimizer.
|
||||
type Image struct{ImageQuality int ;};
|
||||
|
||||
// Append appends optimizers to the chain.
|
||||
func (_gb *Chain )Append (optimizers ..._gf .Optimizer ){_gb ._bf =append (_gb ._bf ,optimizers ...)};func _ccd (_fgb []_d .PdfObject )[]*imageInfo {_gce :=_d .PdfObjectName ("\u0053u\u0062\u0074\u0079\u0070\u0065");_cce :=make (map[*_d .PdfObjectStream ]struct{});var _fbd error ;var _eec []*imageInfo ;for _ ,_agd :=range _fgb {_fed ,_bfg :=_d .GetStream (_agd );if !_bfg {continue ;};if _ ,_acc :=_cce [_fed ];_acc {continue ;};_cce [_fed ]=struct{}{};_gbdc :=_fed .PdfObjectDictionary .Get (_gce );_fgd ,_bfg :=_d .GetName (_gbdc );if !_bfg ||string (*_fgd )!="\u0049\u006d\u0061g\u0065"{continue ;};_cggf :=&imageInfo {BitsPerComponent :8,Stream :_fed };if _cggf .ColorSpace ,_fbd =_gf .DetermineColorspaceNameFromPdfObject (_fed .PdfObjectDictionary .Get ("\u0043\u006f\u006c\u006f\u0072\u0053\u0070\u0061\u0063\u0065"));_fbd !=nil {_dc .Log .Error ("\u0045\u0072\u0072\u006f\u0072\u0020\u0064\u0065\u0074\u0065r\u006d\u0069\u006e\u0065\u0020\u0063\u006fl\u006f\u0072\u0020\u0073\u0070\u0061\u0063\u0065\u0020\u0025\u0073",_fbd );continue ;};if _ggcd ,_bbee :=_d .GetIntVal (_fed .PdfObjectDictionary .Get ("\u0042\u0069t\u0073\u0050\u0065r\u0043\u006f\u006d\u0070\u006f\u006e\u0065\u006e\u0074"));_bbee {_cggf .BitsPerComponent =_ggcd ;};if _cee ,_egaa :=_d .GetIntVal (_fed .PdfObjectDictionary .Get ("\u0057\u0069\u0064t\u0068"));_egaa {_cggf .Width =_cee ;};if _gccb ,_ddb :=_d .GetIntVal (_fed .PdfObjectDictionary .Get ("\u0048\u0065\u0069\u0067\u0068\u0074"));_ddb {_cggf .Height =_gccb ;};switch _cggf .ColorSpace {case "\u0044e\u0076\u0069\u0063\u0065\u0052\u0047B":_cggf .ColorComponents =3;case "\u0044\u0065\u0076\u0069\u0063\u0065\u0047\u0072\u0061\u0079":_cggf .ColorComponents =1;default:_dc .Log .Warning ("\u004f\u0070\u0074\u0069\u006d\u0069\u007a\u0061t\u0069\u006f\u006e i\u0073\u0020\u006e\u006f\u0074\u0020s\u0075\u0070\u0070\u006f\u0072\u0074\u0065\u0064\u0020\u0066\u006f\u0072\u0020\u0063\u006fl\u006f\u0072\u0020\u0073\u0070\u0061\u0063\u0065 \u0025\u0073",_cggf .ColorSpace );continue ;};_eec =append (_eec ,_cggf );};return _eec ;};
|
||||
|
||||
// ObjectStreams groups PDF objects to object streams.
|
||||
// It implements interface model.Optimizer.
|
||||
type ObjectStreams struct{};type imageInfo struct{ColorSpace _b .PdfObjectName ;BitsPerComponent int ;ColorComponents int ;Width int ;Height int ;Stream *_b .PdfObjectStream ;PPI float64 ;};
|
||||
type ObjectStreams struct{};type imageInfo struct{ColorSpace _d .PdfObjectName ;BitsPerComponent int ;ColorComponents int ;Width int ;Height int ;Stream *_d .PdfObjectStream ;PPI float64 ;};
|
||||
|
||||
// Optimize optimizes PDF objects to decrease PDF size.
|
||||
func (_geg *CompressStreams )Optimize (objects []_b .PdfObject )(_bcg []_b .PdfObject ,_baba error ){_bcg =make ([]_b .PdfObject ,len (objects ));copy (_bcg ,objects );for _ ,_efg :=range objects {_cdaa ,_dga :=_b .GetStream (_efg );if !_dga {continue ;};if _gfb :=_cdaa .Get ("\u0046\u0069\u006c\u0074\u0065\u0072");_gfb !=nil {if _ ,_gdbf :=_b .GetName (_gfb );_gdbf {continue ;};if _fgb ,_afc :=_b .GetArray (_gfb );_afc &&_fgb .Len ()> 0{continue ;};};_gbad :=_b .NewFlateEncoder ();var _dcff []byte ;_dcff ,_baba =_gbad .EncodeBytes (_cdaa .Stream );if _baba !=nil {return _bcg ,_baba ;};_fab :=_gbad .MakeStreamDict ();if len (_dcff )+len (_fab .WriteString ())< len (_cdaa .Stream ){_cdaa .Stream =_dcff ;_cdaa .PdfObjectDictionary .Merge (_fab );_cdaa .PdfObjectDictionary .Set ("\u004c\u0065\u006e\u0067\u0074\u0068",_b .MakeInteger (int64 (len (_cdaa .Stream ))));};};return _bcg ,nil ;};
|
||||
func (_cgd *CombineIdenticalIndirectObjects )Optimize (objects []_d .PdfObject )(_ada []_d .PdfObject ,_cga error ){_dedg (objects );_ffc :=make (map[_d .PdfObject ]_d .PdfObject );_fgacc :=make (map[_d .PdfObject ]struct{});_egc :=make (map[string ][]*_d .PdfIndirectObject );for _ ,_fdg :=range objects {_egg ,_fdf :=_fdg .(*_d .PdfIndirectObject );if !_fdf {continue ;};if _aag ,_gfbg :=_egg .PdfObject .(*_d .PdfObjectDictionary );_gfbg {if _cbf ,_ffg :=_aag .Get ("\u0054\u0079\u0070\u0065").(*_d .PdfObjectName );_ffg &&*_cbf =="\u0050\u0061\u0067\u0065"{continue ;};_gbd :=_ab .New ();_gbd .Write ([]byte (_aag .WriteString ()));_ecag :=string (_gbd .Sum (nil ));_egc [_ecag ]=append (_egc [_ecag ],_egg );};};for _ ,_eee :=range _egc {if len (_eee )< 2{continue ;};_bbba :=_eee [0];for _egga :=1;_egga < len (_eee );_egga ++{_ebf :=_eee [_egga ];_ffc [_ebf ]=_bbba ;_fgacc [_ebf ]=struct{}{};};};_ada =make ([]_d .PdfObject ,0,len (objects )-len (_fgacc ));for _ ,_dac :=range objects {if _ ,_dedf :=_fgacc [_dac ];_dedf {continue ;};_ada =append (_ada ,_dac );};_bcd (_ada ,_ffc );return _ada ,nil ;};func _fgace (_dedfe *_d .PdfObjectStream ,_beg float64 )error {_abd ,_bgc :=_gf .NewXObjectImageFromStream (_dedfe );if _bgc !=nil {return _bgc ;};_ccdd ,_bgc :=_abd .ToImage ();if _bgc !=nil {return _bgc ;};_gfd ,_bgc :=_ccdd .ToGoImage ();if _bgc !=nil {return _bgc ;};_cafg :=int (_f .RoundToEven (float64 (_ccdd .Width )*_beg ));_bfb :=int (_f .RoundToEven (float64 (_ccdd .Height )*_beg ));_daed :=_e .Rect (0,0,_cafg ,_bfb );var _ebg _a .Image ;var _gdfg func (_e .Image )(*_gf .Image ,error );switch _abd .ColorSpace .String (){case "\u0044e\u0076\u0069\u0063\u0065\u0052\u0047B":_ebg =_e .NewRGBA (_daed );_gdfg =_gf .ImageHandling .NewImageFromGoImage ;case "\u0044\u0065\u0076\u0069\u0063\u0065\u0047\u0072\u0061\u0079":_ebg =_e .NewGray (_daed );_gdfg =_gf .ImageHandling .NewGrayImageFromGoImage ;default:return _b .Errorf ("\u006f\u0070\u0074\u0069\u006d\u0069\u007a\u0061t\u0069\u006f\u006e i\u0073\u0020\u006e\u006f\u0074\u0020s\u0075\u0070\u0070\u006f\u0072\u0074\u0065\u0064\u0020\u0066\u006f\u0072\u0020\u0063\u006fl\u006f\u0072\u0020\u0073\u0070\u0061\u0063\u0065 \u0025\u0073",_abd .ColorSpace .String ());};_a .CatmullRom .Scale (_ebg ,_ebg .Bounds (),_gfd ,_gfd .Bounds (),_a .Over ,&_a .Options {});if _ccdd ,_bgc =_gdfg (_ebg );_bgc !=nil {return _bgc ;};_cec :=_d .MakeDict ();_cec .Set ("\u0051u\u0061\u006c\u0069\u0074\u0079",_d .MakeInteger (100));_cec .Set ("\u0050r\u0065\u0064\u0069\u0063\u0074\u006fr",_d .MakeInteger (1));_abd .Filter .UpdateParams (_cec );if _bgc =_abd .SetImage (_ccdd ,nil );_bgc !=nil {return _bgc ;};_abd .ToPdfObject ();return nil ;};
|
||||
|
||||
// CompressStreams compresses uncompressed streams.
|
||||
// It implements interface model.Optimizer.
|
||||
type CompressStreams struct{};
|
||||
|
||||
// Optimize optimizes PDF objects to decrease PDF size.
|
||||
func (_aaac *CleanFonts )Optimize (objects []_b .PdfObject )(_acf []_b .PdfObject ,_bgc error ){var _bce map[*_b .PdfObjectStream ]struct{};if _aaac .Subset {var _bgcc error ;_bce ,_bgcc =_ge (objects );if _bgcc !=nil {return nil ,_bgcc ;};};for _ ,_beb :=range objects {_dab ,_abb :=_b .GetStream (_beb );if !_abb {continue ;};if _ ,_gcf :=_bce [_dab ];_gcf {continue ;};_fe ,_ed :=_b .NewEncoderFromStream (_dab );if _ed !=nil {_db .Log .Debug ("\u0045\u0052RO\u0052\u0020\u0067e\u0074\u0074\u0069\u006eg e\u006eco\u0064\u0065\u0072\u003a\u0020\u0025\u0076 -\u0020\u0069\u0067\u006e\u006f\u0072\u0069n\u0067",_ed );continue ;};_bed ,_ed :=_fe .DecodeStream (_dab );if _ed !=nil {_db .Log .Debug ("\u0044\u0065\u0063\u006f\u0064\u0069\u006e\u0067\u0020\u0065r\u0072\u006f\u0072\u0020\u003a\u0020\u0025v\u0020\u002d\u0020\u0069\u0067\u006e\u006f\u0072\u0069\u006e\u0067",_ed );continue ;};if len (_bed )< 4{continue ;};_egf :=string (_bed [:4]);if _egf =="\u004f\u0054\u0054\u004f"{continue ;};if _egf !="\u0000\u0001\u0000\u0000"&&_egf !="\u0074\u0072\u0075\u0065"{continue ;};_beg ,_ed :=_dg .Parse (_cd .NewReader (_bed ));if _ed !=nil {_db .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u0020P\u0061\u0072\u0073\u0069\u006e\u0067\u0020\u0066\u006f\u006e\u0074\u003a\u0020%\u0076\u0020\u002d\u0020\u0069\u0067\u006eo\u0072\u0069\u006e\u0067",_ed );continue ;};_ed =_beg .Optimize ();if _ed !=nil {continue ;};var _bdc _cd .Buffer ;_ed =_beg .Write (&_bdc );if _ed !=nil {_db .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u0020W\u0072\u0069\u0074\u0069\u006e\u0067\u0020\u0066\u006f\u006e\u0074\u003a\u0020%\u0076\u0020\u002d\u0020\u0069\u0067\u006eo\u0072\u0069\u006e\u0067",_ed );continue ;};if _bdc .Len ()> len (_bed ){_db .Log .Debug ("\u0052\u0065-\u0077\u0072\u0069\u0074\u0074\u0065\u006e\u0020\u0066\u006f\u006e\u0074\u0020\u0069\u0073\u0020\u006c\u0061\u0072\u0067\u0065\u0072\u0020\u0074\u0068\u0061\u006e\u0020\u006f\u0072\u0069\u0067\u0069\u006e\u0061\u006c\u0020\u002d\u0020\u0073\u006b\u0069\u0070");continue ;};_fdb ,_ed :=_b .MakeStream (_bdc .Bytes (),_b .NewFlateEncoder ());if _ed !=nil {continue ;};*_dab =*_fdb ;_dab .Set ("\u004ce\u006e\u0067\u0074\u0068\u0031",_b .MakeInteger (int64 (_bdc .Len ())));};return objects ,nil ;};
|
||||
|
||||
// CleanFonts cleans up embedded fonts, reducing font sizes.
|
||||
type CleanFonts struct{
|
||||
|
||||
// Subset embedded fonts if encountered (if true).
|
||||
// Otherwise attempts to reduce the font program.
|
||||
Subset bool ;};func _ge (_cc []_b .PdfObject )(_bca map[*_b .PdfObjectStream ]struct{},_bcb error ){_bca =map[*_b .PdfObjectStream ]struct{}{};_cee :=map[*_bd .PdfFont ]struct{}{};_agf :=_fbb (_cc );for _ ,_cdf :=range _agf ._fdeb {_fb ,_ea :=_b .GetDict (_cdf .PdfObject );if !_ea {continue ;};_bff ,_ea :=_b .GetDict (_fb .Get ("\u0052e\u0073\u006f\u0075\u0072\u0063\u0065s"));if !_ea {continue ;};_eab ,_ :=_ddafb (_fb .Get ("\u0043\u006f\u006e\u0074\u0065\u006e\u0074\u0073"));_ceda ,_aed :=_bd .NewPdfPageResourcesFromDict (_bff );if _aed !=nil {return nil ,_aed ;};_gd :=[]content {{_dde :_eab ,_ddec :_ceda }};_aef :=_ebc (_fb .Get ("\u0041\u006e\u006e\u006f\u0074\u0073"));if _aef !=nil {_gd =append (_gd ,_aef ...);};for _ ,_dgc :=range _gd {_efa ,_bccd :=_cf .NewFromContents (_dgc ._dde ,_dgc ._ddec );if _bccd !=nil {return nil ,_bccd ;};_dae ,_ ,_ ,_bccd :=_efa .ExtractPageText ();if _bccd !=nil {return nil ,_bccd ;};for _ ,_fde :=range _dae .Marks ().Elements (){if _fde .Font ==nil {continue ;};if _ ,_gb :=_cee [_fde .Font ];!_gb {_cee [_fde .Font ]=struct{}{};};};};};_ddg :=map[*_b .PdfObjectStream ][]*_bd .PdfFont {};for _faa :=range _cee {_gcg :=_faa .FontDescriptor ();if _gcg ==nil ||_gcg .FontFile2 ==nil {continue ;};_ecb ,_ecf :=_b .GetStream (_gcg .FontFile2 );if !_ecf {continue ;};_ddg [_ecb ]=append (_ddg [_ecb ],_faa );};for _ffb :=range _ddg {var _aeg []rune ;var _bda []_dg .GlyphIndex ;for _ ,_dgb :=range _ddg [_ffb ]{switch _ga :=_dgb .Encoder ().(type ){case *_bad .IdentityEncoder :_gca :=_ga .RegisteredRunes ();_ggc :=make ([]_dg .GlyphIndex ,len (_gca ));for _dgbb ,_fgc :=range _gca {_ggc [_dgbb ]=_dg .GlyphIndex (_fgc );};_bda =append (_bda ,_ggc ...);case *_bad .TrueTypeFontEncoder :_bg :=_ga .RegisteredRunes ();_aeg =append (_aeg ,_bg ...);case _bad .SimpleEncoder :_bee :=_ga .Charcodes ();for _ ,_aaa :=range _bee {_fac ,_cca :=_ga .CharcodeToRune (_aaa );if !_cca {_db .Log .Debug ("\u0043\u0068a\u0072\u0063\u006f\u0064\u0065\u003c\u002d\u003e\u0072\u0075\u006e\u0065\u0020\u006e\u006f\u0074\u0020\u0066\u006f\u0075\u006e\u0064: \u0025\u0064",_aaa );continue ;};_aeg =append (_aeg ,_fac );};};};_bcb =_fgcd (_ffb ,_aeg ,_bda );if _bcb !=nil {_db .Log .Debug ("\u0045\u0052\u0052\u004f\u0052\u0020\u0073\u0075\u0062\u0073\u0065\u0074\u0074\u0069\u006eg\u0020f\u006f\u006e\u0074\u0020\u0073\u0074\u0072\u0065\u0061\u006d\u003a\u0020\u0025\u0076",_bcb );return nil ,_bcb ;};_bca [_ffb ]=struct{}{};};return _bca ,nil ;};
|
||||
// Options describes PDF optimization parameters.
|
||||
type Options struct{CombineDuplicateStreams bool ;CombineDuplicateDirectObjects bool ;ImageUpperPPI float64 ;ImageQuality int ;UseObjectStreams bool ;CombineIdenticalIndirectObjects bool ;CompressStreams bool ;CleanFonts bool ;SubsetFonts bool ;CleanContentstream bool ;};func _afc (_eef _d .PdfObject )[]content {if _eef ==nil {return nil ;};_beb ,_ega :=_d .GetArray (_eef );if !_ega {_dc .Log .Debug ("\u0041\u006e\u006e\u006fts\u0020\u006e\u006f\u0074\u0020\u0061\u006e\u0020\u0061\u0072\u0072\u0061\u0079");return nil ;};var _bgdb []content ;for _ ,_cdgg :=range _beb .Elements (){_gfc ,_bd :=_d .GetDict (_cdgg );if !_bd {_dc .Log .Debug ("I\u0067\u006e\u006f\u0072\u0069\u006eg\u0020\u006e\u006f\u006e\u002d\u0064i\u0063\u0074\u0020\u0065\u006c\u0065\u006de\u006e\u0074\u0020\u0069\u006e\u0020\u0041\u006e\u006e\u006ft\u0073");continue ;};_ecf ,_bd :=_d .GetDict (_gfc .Get ("\u0041\u0050"));if !_bd {_dc .Log .Debug ("\u004e\u006f\u0020\u0041P \u0065\u006e\u0074\u0072\u0079\u0020\u002d\u0020\u0073\u006b\u0069\u0070\u0070\u0069n\u0067");continue ;};_aaba :=_d .TraceToDirectObject (_ecf .Get ("\u004e"));if _aaba ==nil {_dc .Log .Debug ("N\u006f\u0020\u004e\u0020en\u0074r\u0079\u0020\u002d\u0020\u0073k\u0069\u0070\u0070\u0069\u006e\u0067");continue ;};var _aeb *_d .PdfObjectStream ;switch _dec :=_aaba .(type ){case *_d .PdfObjectDictionary :_bgg ,_aaa :=_d .GetName (_gfc .Get ("\u0041\u0053"));if !_aaa {_dc .Log .Debug ("\u004e\u006f\u0020\u0041S \u0065\u006e\u0074\u0072\u0079\u0020\u002d\u0020\u0073\u006b\u0069\u0070\u0070\u0069n\u0067");continue ;};_aeb ,_aaa =_d .GetStream (_dec .Get (*_bgg ));if !_aaa {_dc .Log .Debug ("\u0046o\u0072\u006d\u0020\u006eo\u0074\u0020\u0066\u006f\u0075n\u0064 \u002d \u0073\u006b\u0069\u0070\u0070\u0069\u006eg");continue ;};case *_d .PdfObjectStream :_aeb =_dec ;};if _aeb ==nil {_dc .Log .Debug ("\u0046\u006f\u0072m\u0020\u006e\u006f\u0074 \u0066\u006f\u0075\u006e\u0064\u0020\u0028n\u0069\u006c\u0029\u0020\u002d\u0020\u0073\u006b\u0069\u0070\u0070\u0069\u006e\u0067");continue ;};_cba ,_aaf :=_gf .NewXObjectFormFromStream (_aeb );if _aaf !=nil {_dc .Log .Debug ("\u0045\u0072\u0072\u006f\u0072\u0020l\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0066\u006f\u0072\u006d\u003a\u0020%\u0076\u0020\u002d\u0020\u0069\u0067\u006eo\u0072\u0069\u006e\u0067",_aaf );continue ;};_gee ,_aaf :=_cba .GetContentStream ();if _aaf !=nil {_dc .Log .Debug ("E\u0072\u0072\u006f\u0072\u0020\u0064e\u0063\u006f\u0064\u0069\u006e\u0067\u0020\u0063\u006fn\u0074\u0065\u006et\u0073:\u0020\u0025\u0076",_aaf );continue ;};_bgdb =append (_bgdb ,content {_gbe :string (_gee ),_fee :_cba .Resources });};return _bgdb ;};
|
||||
|
||||
// Optimize optimizes PDF objects to decrease PDF size.
|
||||
func (_gcc *CombineDuplicateDirectObjects )Optimize (objects []_b .PdfObject )(_deg []_b .PdfObject ,_fge error ){_bbca (objects );_beed :=make (map[string ][]*_b .PdfObjectDictionary );var _fceb func (_bdf *_b .PdfObjectDictionary );_fceb =func (_dad *_b .PdfObjectDictionary ){for _ ,_cgaa :=range _dad .Keys (){_dabd :=_dad .Get (_cgaa );if _fff ,_cgb :=_dabd .(*_b .PdfObjectDictionary );_cgb {_caea :=_d .New ();_caea .Write ([]byte (_fff .WriteString ()));_ecg :=string (_caea .Sum (nil ));_beed [_ecg ]=append (_beed [_ecg ],_fff );_fceb (_fff );};};};for _ ,_gee :=range objects {_bbe ,_acc :=_gee .(*_b .PdfIndirectObject );if !_acc {continue ;};if _fgg ,_dfc :=_bbe .PdfObject .(*_b .PdfObjectDictionary );_dfc {_fceb (_fgg );};};_cdff :=make ([]_b .PdfObject ,0,len (_beed ));_bgb :=make (map[_b .PdfObject ]_b .PdfObject );for _ ,_bccc :=range _beed {if len (_bccc )< 2{continue ;};_aceg :=_b .MakeDict ();_aceg .Merge (_bccc [0]);_acfg :=_b .MakeIndirectObject (_aceg );_cdff =append (_cdff ,_acfg );for _cafa :=0;_cafa < len (_bccc );_cafa ++{_caff :=_bccc [_cafa ];_bgb [_caff ]=_acfg ;};};_deg =make ([]_b .PdfObject ,len (objects ));copy (_deg ,objects );_deg =append (_cdff ,_deg ...);_ddaf (_deg ,_bgb );return _deg ,nil ;};
|
||||
|
||||
// New creates a optimizers chain from options.
|
||||
func New (options Options )*Chain {_ddgb :=new (Chain );if options .CleanFonts ||options .SubsetFonts {_ddgb .Append (&CleanFonts {Subset :options .SubsetFonts });};if options .CleanContentstream {_ddgb .Append (new (CleanContentstream ));};if options .ImageUpperPPI > 0{_cff :=new (ImagePPI );_cff .ImageUpperPPI =options .ImageUpperPPI ;_ddgb .Append (_cff );};if options .ImageQuality > 0{_abab :=new (Image );_abab .ImageQuality =options .ImageQuality ;_ddgb .Append (_abab );};if options .CombineDuplicateDirectObjects {_ddgb .Append (new (CombineDuplicateDirectObjects ));};if options .CombineDuplicateStreams {_ddgb .Append (new (CombineDuplicateStreams ));};if options .CombineIdenticalIndirectObjects {_ddgb .Append (new (CombineIdenticalIndirectObjects ));};if options .UseObjectStreams {_ddgb .Append (new (ObjectStreams ));};if options .CompressStreams {_ddgb .Append (new (CompressStreams ));};return _ddgb ;};
|
||||
|
||||
// Optimize optimizes PDF objects to decrease PDF size.
|
||||
func (_cbb *ImagePPI )Optimize (objects []_b .PdfObject )(_becb []_b .PdfObject ,_fcc error ){if _cbb .ImageUpperPPI <=0{return objects ,nil ;};_dec :=_agg (objects );if len (_dec )==0{return objects ,nil ;};_bebd :=make (map[_b .PdfObject ]struct{});for _ ,_adc :=range _dec {_ffge :=_adc .Stream .PdfObjectDictionary .Get (_b .PdfObjectName ("\u0053\u004d\u0061s\u006b"));_bebd [_ffge ]=struct{}{};};_fddb :=make (map[*_b .PdfObjectStream ]*imageInfo );for _ ,_dbfa :=range _dec {_fddb [_dbfa .Stream ]=_dbfa ;};var _cbf *_b .PdfObjectDictionary ;for _ ,_aba :=range objects {if _bccef ,_ggg :=_b .GetDict (_aba );_cbf ==nil &&_ggg {if _bae ,_ggcc :=_b .GetName (_bccef .Get (_b .PdfObjectName ("\u0054\u0079\u0070\u0065")));_ggcc &&*_bae =="\u0043a\u0074\u0061\u006c\u006f\u0067"{_cbf =_bccef ;};};};if _cbf ==nil {return objects ,nil ;};_fbg ,_gbg :=_b .GetDict (_cbf .Get (_b .PdfObjectName ("\u0050\u0061\u0067e\u0073")));if !_gbg {return objects ,nil ;};_ddgg ,_acfd :=_b .GetArray (_fbg .Get (_b .PdfObjectName ("\u004b\u0069\u0064\u0073")));if !_acfd {return objects ,nil ;};_fddg :=make (map[string ]*imageInfo );for _ ,_bgf :=range _ddgg .Elements (){_fda ,_ecc :=_b .GetDict (_bgf );if !_ecc {continue ;};_bcge ,_fcfa :=_b .GetArray (_fda .Get ("\u0043\u006f\u006e\u0074\u0065\u006e\u0074\u0073"));if !_fcfa {continue ;};_dbce ,_edfg :=_b .GetDict (_fda .Get ("\u0052e\u0073\u006f\u0075\u0072\u0063\u0065s"));if !_edfg {continue ;};_dca ,_daa :=_b .GetDict (_dbce .Get ("\u0058O\u0062\u006a\u0065\u0063\u0074"));if !_daa {continue ;};_fgeb :=_dca .Keys ();for _ ,_ddb :=range _fgeb {if _gbd ,_ddbb :=_b .GetStream (_dca .Get (_ddb ));_ddbb {if _gff ,_ada :=_fddb [_gbd ];_ada {_fddg [string (_ddb )]=_gff ;};};};for _ ,_bag :=range _bcge .Elements (){if _bgdc ,_fddd :=_b .GetStream (_bag );_fddd {_ebca ,_fcd :=_b .NewEncoderFromStream (_bgdc );if _fcd !=nil {return nil ,_fcd ;};_ccg ,_fcd :=_ebca .DecodeStream (_bgdc );if _fcd !=nil {return nil ,_fcd ;};_fbc :=_ca .NewContentStreamParser (string (_ccg ));_bccdb ,_fcd :=_fbc .Parse ();if _fcd !=nil {return nil ,_fcd ;};_eea ,_acfc :=1.0,1.0;for _ ,_aga :=range *_bccdb {if _aga .Operand =="\u0051"{_eea ,_acfc =1.0,1.0;};if _aga .Operand =="\u0063\u006d"&&len (_aga .Params )==6{if _agfc ,_bfa :=_b .GetFloatVal (_aga .Params [0]);_bfa {_eea =_eea *_agfc ;};if _dbfab ,_abf :=_b .GetFloatVal (_aga .Params [3]);_abf {_acfc =_acfc *_dbfab ;};if _cbc ,_aag :=_b .GetIntVal (_aga .Params [0]);_aag {_eea =_eea *float64 (_cbc );};if _fec ,_cbe :=_b .GetIntVal (_aga .Params [3]);_cbe {_acfc =_acfc *float64 (_fec );};};if _aga .Operand =="\u0044\u006f"&&len (_aga .Params )==1{_eead ,_gbf :=_b .GetName (_aga .Params [0]);if !_gbf {continue ;};if _cfef ,_ecfg :=_fddg [string (*_eead )];_ecfg {_gfc ,_gce :=_eea /72.0,_acfc /72.0;_acaa ,_ddce :=float64 (_cfef .Width )/_gfc ,float64 (_cfef .Height )/_gce ;if _gfc ==0||_gce ==0{_acaa =72.0;_ddce =72.0;};_cfef .PPI =_ba .Max (_cfef .PPI ,_acaa );_cfef .PPI =_ba .Max (_cfef .PPI ,_ddce );};};};};};};for _ ,_bdcb :=range _dec {if _ ,_cbfe :=_bebd [_bdcb .Stream ];_cbfe {continue ;};if _bdcb .PPI <=_cbb .ImageUpperPPI {continue ;};_adf :=_cbb .ImageUpperPPI /_bdcb .PPI ;if _caffe :=_dfb (_bdcb .Stream ,_adf );_caffe !=nil {_db .Log .Debug ("\u0045\u0072\u0072\u006f\u0072 \u0073\u0063\u0061\u006c\u0065\u0020\u0069\u006d\u0061\u0067\u0065\u0020\u006be\u0065\u0070\u0020\u006f\u0072\u0069\u0067\u0069\u006e\u0061\u006c\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_caffe );}else {if _bdg ,_gfg :=_b .GetStream (_bdcb .Stream .PdfObjectDictionary .Get (_b .PdfObjectName ("\u0053\u004d\u0061s\u006b")));_gfg {if _cbbf :=_dfb (_bdg ,_adf );_cbbf !=nil {return nil ,_cbbf ;};};};};return objects ,nil ;};func _eb (_ac *_b .PdfObjectStream )error {_def ,_cfc :=_b .DecodeStream (_ac );if _cfc !=nil {return _cfc ;};_af :=_ca .NewContentStreamParser (string (_def ));_df ,_cfc :=_af .Parse ();if _cfc !=nil {return _cfc ;};_df =_dc (_df );_da :=_df .Bytes ();if len (_da )>=len (_def ){return nil ;};_bc ,_cfc :=_b .MakeStream (_df .Bytes (),_b .NewFlateEncoder ());if _cfc !=nil {return _cfc ;};_ac .Stream =_bc .Stream ;_ac .Merge (_bc .PdfObjectDictionary );return nil ;};
|
||||
func (_gcee *ObjectStreams )Optimize (objects []_d .PdfObject )(_gcbb []_d .PdfObject ,_fefc error ){_eggf :=&_d .PdfObjectStreams {};_ffce :=make ([]_d .PdfObject ,0,len (objects ));for _ ,_gag :=range objects {if _gfeg ,_bdef :=_gag .(*_d .PdfIndirectObject );_bdef &&_gfeg .GenerationNumber ==0{_eggf .Append (_gag );}else {_ffce =append (_ffce ,_gag );};};if _eggf .Len ()==0{return _ffce ,nil ;};_gcbb =make ([]_d .PdfObject ,0,len (_ffce )+_eggf .Len ()+1);if _eggf .Len ()> 1{_gcbb =append (_gcbb ,_eggf );};_gcbb =append (_gcbb ,_eggf .Elements ()...);_gcbb =append (_gcbb ,_ffce ...);return _gcbb ,nil ;};
|
||||
|
||||
// CombineIdenticalIndirectObjects combines identical indirect objects.
|
||||
// It implements interface model.Optimizer.
|
||||
type CombineIdenticalIndirectObjects struct{};
|
||||
|
||||
// Chain allows to use sequence of optimizers.
|
||||
// CombineDuplicateDirectObjects combines duplicated direct objects by its data hash.
|
||||
// It implements interface model.Optimizer.
|
||||
type Chain struct{_fd []_bd .Optimizer };
|
||||
|
||||
// Image optimizes images by rewrite images into JPEG format with quality equals to ImageQuality.
|
||||
// TODO(a5i): Add support for inline images.
|
||||
// It implements interface model.Optimizer.
|
||||
type Image struct{ImageQuality int ;};func _ddaf (_aedg []_b .PdfObject ,_bdaa map[_b .PdfObject ]_b .PdfObject ){if _bdaa ==nil ||len (_bdaa )==0{return ;};for _adgg ,_bdcff :=range _aedg {if _ged ,_cdb :=_bdaa [_bdcff ];_cdb {_aedg [_adgg ]=_ged ;continue ;};_bdaa [_bdcff ]=_bdcff ;switch _ede :=_bdcff .(type ){case *_b .PdfObjectArray :_cad :=make ([]_b .PdfObject ,_ede .Len ());copy (_cad ,_ede .Elements ());_ddaf (_cad ,_bdaa );for _abe ,_faabe :=range _cad {_ede .Set (_abe ,_faabe );};case *_b .PdfObjectStreams :_ddaf (_ede .Elements (),_bdaa );case *_b .PdfObjectStream :_dcd :=[]_b .PdfObject {_ede .PdfObjectDictionary };_ddaf (_dcd ,_bdaa );_ede .PdfObjectDictionary =_dcd [0].(*_b .PdfObjectDictionary );case *_b .PdfObjectDictionary :_ece :=_ede .Keys ();_cgbb :=make ([]_b .PdfObject ,len (_ece ));for _fdga ,_gae :=range _ece {_cgbb [_fdga ]=_ede .Get (_gae );};_ddaf (_cgbb ,_bdaa );for _gddd ,_cdbb :=range _ece {_ede .Set (_cdbb ,_cgbb [_gddd ]);};case *_b .PdfIndirectObject :_bbb :=[]_b .PdfObject {_ede .PdfObject };_ddaf (_bbb ,_bdaa );_ede .PdfObject =_bbb [0];};};};func _dfb (_aefd *_b .PdfObjectStream ,_gcbe float64 )error {_ggb ,_fgbb :=_bd .NewXObjectImageFromStream (_aefd );if _fgbb !=nil {return _fgbb ;};_dee ,_fgbb :=_ggb .ToImage ();if _fgbb !=nil {return _fgbb ;};_bbcc ,_fgbb :=_dee .ToGoImage ();if _fgbb !=nil {return _fgbb ;};_dea :=int (_ba .RoundToEven (float64 (_dee .Width )*_gcbe ));_bfce :=int (_ba .RoundToEven (float64 (_dee .Height )*_gcbe ));_dgfc :=_f .Rect (0,0,_dea ,_bfce );var _fgce _a .Image ;var _bcgg func (_f .Image )(*_bd .Image ,error );switch _ggb .ColorSpace .String (){case "\u0044e\u0076\u0069\u0063\u0065\u0052\u0047B":_fgce =_f .NewRGBA (_dgfc );_bcgg =_bd .ImageHandling .NewImageFromGoImage ;case "\u0044\u0065\u0076\u0069\u0063\u0065\u0047\u0072\u0061\u0079":_fgce =_f .NewGray (_dgfc );_bcgg =_bd .ImageHandling .NewGrayImageFromGoImage ;default:return _bf .Errorf ("\u006f\u0070\u0074\u0069\u006d\u0069\u007a\u0061t\u0069\u006f\u006e i\u0073\u0020\u006e\u006f\u0074\u0020s\u0075\u0070\u0070\u006f\u0072\u0074\u0065\u0064\u0020\u0066\u006f\u0072\u0020\u0063\u006fl\u006f\u0072\u0020\u0073\u0070\u0061\u0063\u0065 \u0025\u0073",_ggb .ColorSpace .String ());};_a .CatmullRom .Scale (_fgce ,_fgce .Bounds (),_bbcc ,_bbcc .Bounds (),_a .Over ,&_a .Options {});if _dee ,_fgbb =_bcgg (_fgce );_fgbb !=nil {return _fgbb ;};_abd :=_b .MakeDict ();_abd .Set ("\u0051u\u0061\u006c\u0069\u0074\u0079",_b .MakeInteger (100));_abd .Set ("\u0050r\u0065\u0064\u0069\u0063\u0074\u006fr",_b .MakeInteger (1));_ggb .Filter .UpdateParams (_abd );if _fgbb =_ggb .SetImage (_dee ,nil );_fgbb !=nil {return _fgbb ;};_ggb .ToPdfObject ();return nil ;};
|
||||
type CombineDuplicateDirectObjects struct{};func _fdb (_dgde []_d .PdfObject )objectStructure {_baa :=objectStructure {};_gfa :=false ;for _ ,_gdb :=range _dgde {switch _eacf :=_gdb .(type ){case *_d .PdfIndirectObject :_bgag ,_fbfd :=_d .GetDict (_eacf );if !_fbfd {continue ;};_dagg ,_fbfd :=_d .GetName (_bgag .Get ("\u0054\u0079\u0070\u0065"));if !_fbfd {continue ;};switch _dagg .String (){case "\u0043a\u0074\u0061\u006c\u006f\u0067":_baa ._egdb =_bgag ;_gfa =true ;};};if _gfa {break ;};};if !_gfa {return _baa ;};_cgab ,_dfeg :=_d .GetDict (_baa ._egdb .Get ("\u0050\u0061\u0067e\u0073"));if !_dfeg {return _baa ;};_baa ._daf =_cgab ;_efcc ,_dfeg :=_d .GetArray (_cgab .Get ("\u004b\u0069\u0064\u0073"));if !_dfeg {return _baa ;};for _ ,_cbd :=range _efcc .Elements (){_aacc ,_bdcc :=_d .GetIndirect (_cbd );if !_bdcc {break ;};_baa ._aae =append (_baa ._aae ,_aacc );};return _baa ;};func _fe (_da *_d .PdfObjectStream )error {_cd ,_ea :=_d .DecodeStream (_da );if _ea !=nil {return _ea ;};_ecd :=_ge .NewContentStreamParser (string (_cd ));_cc ,_ea :=_ecd .Parse ();if _ea !=nil {return _ea ;};_cc =_db (_cc );_gbce :=_cc .Bytes ();if len (_gbce )>=len (_cd ){return nil ;};_eac ,_ea :=_d .MakeStream (_cc .Bytes (),_d .NewFlateEncoder ());if _ea !=nil {return _ea ;};_da .Stream =_eac .Stream ;_da .Merge (_eac .PdfObjectDictionary );return nil ;};
|
||||
|
||||
// Optimize optimizes PDF objects to decrease PDF size.
|
||||
func (_ae *CleanContentstream )Optimize (objects []_b .PdfObject )(_aea []_b .PdfObject ,_dbf error ){_caf :=map[*_b .PdfObjectStream ]struct{}{};var _dd []*_b .PdfObjectStream ;_fc :=func (_fg *_b .PdfObjectStream ){if _ ,_ag :=_caf [_fg ];!_ag {_caf [_fg ]=struct{}{};_dd =append (_dd ,_fg );};};for _ ,_gc :=range objects {switch _ce :=_gc .(type ){case *_b .PdfIndirectObject :switch _cfg :=_ce .PdfObject .(type ){case *_b .PdfObjectDictionary :if _gg ,_fdg :=_b .GetName (_cfg .Get ("\u0054\u0079\u0070\u0065"));!_fdg ||_gg .String ()!="\u0050\u0061\u0067\u0065"{continue ;};if _bfg ,_ggf :=_b .GetStream (_cfg .Get ("\u0043\u006f\u006e\u0074\u0065\u006e\u0074\u0073"));_ggf {_fc (_bfg );}else if _dff ,_ad :=_b .GetArray (_cfg .Get ("\u0043\u006f\u006e\u0074\u0065\u006e\u0074\u0073"));_ad {for _ ,_bded :=range _dff .Elements (){if _dbg ,_cga :=_b .GetStream (_bded );_cga {_fc (_dbg );};};};};case *_b .PdfObjectStream :if _ced ,_eg :=_b .GetName (_ce .Get ("\u0054\u0079\u0070\u0065"));!_eg ||_ced .String ()!="\u0058O\u0062\u006a\u0065\u0063\u0074"{continue ;};if _bcc ,_ec :=_b .GetName (_ce .Get ("\u0053u\u0062\u0074\u0079\u0070\u0065"));!_ec ||_bcc .String ()!="\u0046\u006f\u0072\u006d"{continue ;};_fc (_ce );};};for _ ,_be :=range _dd {_dbf =_eb (_be );if _dbf !=nil {return nil ,_dbf ;};};return objects ,nil ;};func _bbca (_gfgf []_b .PdfObject ){for _bedf ,_geec :=range _gfgf {switch _cac :=_geec .(type ){case *_b .PdfIndirectObject :_cac .ObjectNumber =int64 (_bedf +1);_cac .GenerationNumber =0;case *_b .PdfObjectStream :_cac .ObjectNumber =int64 (_bedf +1);_cac .GenerationNumber =0;case *_b .PdfObjectStreams :_cac .ObjectNumber =int64 (_bedf +1);_cac .GenerationNumber =0;};};};func _fbb (_edgf []_b .PdfObject )objectStructure {_gfa :=objectStructure {};_ade :=false ;for _ ,_bea :=range _edgf {switch _fgbc :=_bea .(type ){case *_b .PdfIndirectObject :_cgbf ,_eaec :=_b .GetDict (_fgbc );if !_eaec {continue ;};_cddg ,_eaec :=_b .GetName (_cgbf .Get ("\u0054\u0079\u0070\u0065"));if !_eaec {continue ;};switch _cddg .String (){case "\u0043a\u0074\u0061\u006c\u006f\u0067":_gfa ._gfbb =_cgbf ;_ade =true ;};};if _ade {break ;};};if !_ade {return _gfa ;};_efde ,_bcee :=_b .GetDict (_gfa ._gfbb .Get ("\u0050\u0061\u0067e\u0073"));if !_bcee {return _gfa ;};_gfa ._bbff =_efde ;_gdea ,_bcee :=_b .GetArray (_efde .Get ("\u004b\u0069\u0064\u0073"));if !_bcee {return _gfa ;};for _ ,_eaa :=range _gdea .Elements (){_fffd ,_ggfgg :=_b .GetIndirect (_eaa );if !_ggfgg {break ;};_gfa ._fdeb =append (_gfa ._fdeb ,_fffd );};return _gfa ;};func _dc (_cda *_ca .ContentStreamOperations )*_ca .ContentStreamOperations {if _cda ==nil {return nil ;};_de :=_ca .ContentStreamOperations {};for _ ,_bfc :=range *_cda {switch _bfc .Operand {case "\u0042\u0044\u0043","\u0042\u004d\u0043","\u0045\u004d\u0043":continue ;case "\u0054\u006d":if len (_bfc .Params )==6{if _ff ,_g :=_b .GetNumbersAsFloat (_bfc .Params );_g ==nil {if _ff [0]==1&&_ff [1]==0&&_ff [2]==0&&_ff [3]==1{_bfc =&_ca .ContentStreamOperation {Params :[]_b .PdfObject {_bfc .Params [4],_bfc .Params [5]},Operand :"\u0054\u0064"};};};};};_de =append (_de ,_bfc );};return &_de ;};
|
||||
func (_cbbg *Image )Optimize (objects []_d .PdfObject )(_fde []_d .PdfObject ,_bfe error ){if _cbbg .ImageQuality <=0{return objects ,nil ;};_cbfg :=_ccd (objects );if len (_cbfg )==0{return objects ,nil ;};_ecdf :=make (map[_d .PdfObject ]_d .PdfObject );_gda :=make (map[_d .PdfObject ]struct{});for _ ,_efde :=range _cbfg {_afg :=_efde .Stream .PdfObjectDictionary .Get (_d .PdfObjectName ("\u0053\u004d\u0061s\u006b"));_gda [_afg ]=struct{}{};};for _efec ,_acaa :=range _cbfg {_ceba :=_acaa .Stream ;if _ ,_bdb :=_gda [_ceba ];_bdb {continue ;};_cdga ,_afb :=_d .NewEncoderFromStream (_ceba );if _afb !=nil {_dc .Log .Warning ("\u0045\u0072\u0072\u006f\u0072 \u0067\u0065\u0074\u0020\u0065\u006e\u0063\u006f\u0064\u0065\u0072\u0020\u0066o\u0072\u0020\u0074\u0068\u0065\u0020\u0069\u006d\u0061\u0067\u0065\u0020\u0073\u0074\u0072\u0065\u0061\u006d\u0020\u0025\u0073");continue ;};_cfca ,_afb :=_cdga .DecodeStream (_ceba );if _afb !=nil {_dc .Log .Warning ("\u0045\u0072\u0072\u006f\u0072\u0020\u0064\u0065\u0063\u006f\u0064\u0065\u0020\u0074\u0068e\u0020i\u006d\u0061\u0067\u0065\u0020\u0073\u0074\u0072\u0065\u0061\u006d\u0020\u0025\u0073");continue ;};_ced :=_d .NewDCTEncoder ();_ced .ColorComponents =_acaa .ColorComponents ;_ced .Quality =_cbbg .ImageQuality ;_ced .BitsPerComponent =_acaa .BitsPerComponent ;_ced .Width =_acaa .Width ;_ced .Height =_acaa .Height ;_fgf ,_afb :=_ced .EncodeBytes (_cfca );if _afb !=nil {_dc .Log .Debug ("\u0045R\u0052\u004f\u0052\u003a\u0020\u0025v",_afb );return nil ,_afb ;};var _abba _d .StreamEncoder ;_abba =_ced ;{_gfbd :=_d .NewFlateEncoder ();_cggfc :=_d .NewMultiEncoder ();_cggfc .AddEncoder (_gfbd );_cggfc .AddEncoder (_ced );_gdf ,_gde :=_cggfc .EncodeBytes (_cfca );if _gde !=nil {return nil ,_gde ;};if len (_gdf )< len (_fgf ){_dc .Log .Debug ("\u004d\u0075\u006c\u0074\u0069\u0020\u0065\u006e\u0063\u0020\u0069\u006d\u0070\u0072\u006f\u0076\u0065\u0073\u003a\u0020\u0025\u0064\u0020\u0074o\u0020\u0025\u0064\u0020\u0028o\u0072\u0069g\u0020\u0025\u0064\u0029",len (_fgf ),len (_gdf ),len (_ceba .Stream ));_fgf =_gdf ;_abba =_cggfc ;};};_cea :=len (_ceba .Stream );if _cea < len (_fgf ){continue ;};_ggbd :=&_d .PdfObjectStream {Stream :_fgf };_ggbd .PdfObjectReference =_ceba .PdfObjectReference ;_ggbd .PdfObjectDictionary =_d .MakeDict ();_ggbd .Merge (_ceba .PdfObjectDictionary );_ggbd .Merge (_abba .MakeStreamDict ());_ggbd .Set ("\u004c\u0065\u006e\u0067\u0074\u0068",_d .MakeInteger (int64 (len (_fgf ))));_ecdf [_ceba ]=_ggbd ;_cbfg [_efec ].Stream =_ggbd ;};_fde =make ([]_d .PdfObject ,len (objects ));copy (_fde ,objects );_bcd (_fde ,_ecdf );return _fde ,nil ;};
|
||||
|
||||
// Optimize optimizes PDF objects to decrease PDF size.
|
||||
func (_abcf *CombineDuplicateStreams )Optimize (objects []_d .PdfObject )(_ded []_d .PdfObject ,_aebd error ){_ecc :=make (map[_d .PdfObject ]_d .PdfObject );_badf :=make (map[_d .PdfObject ]struct{});_acb :=make (map[string ][]*_d .PdfObjectStream );for _ ,_cbe :=range objects {if _aaaf ,_caea :=_cbe .(*_d .PdfObjectStream );_caea {_gfff :=_ab .New ();_gfff .Write ([]byte (_aaaf .Stream ));_cgg :=string (_gfff .Sum (nil ));_acb [_cgg ]=append (_acb [_cgg ],_aaaf );};};for _ ,_fdc :=range _acb {if len (_fdc )< 2{continue ;};_aeba :=_fdc [0];for _edab :=1;_edab < len (_fdc );_edab ++{_agf :=_fdc [_edab ];_ecc [_agf ]=_aeba ;_badf [_agf ]=struct{}{};};};_ded =make ([]_d .PdfObject ,0,len (objects )-len (_badf ));for _ ,_cef :=range objects {if _ ,_cbb :=_badf [_cef ];_cbb {continue ;};_ded =append (_ded ,_cef );};_bcd (_ded ,_ecc );return _ded ,nil ;};
|
||||
|
||||
// Optimize optimizes PDF objects to decrease PDF size.
|
||||
func (_bdc *CombineDuplicateDirectObjects )Optimize (objects []_d .PdfObject )(_eca []_d .PdfObject ,_abf error ){_dedg (objects );_cgfa :=make (map[string ][]*_d .PdfObjectDictionary );var _age func (_aabc *_d .PdfObjectDictionary );_age =func (_dbc *_d .PdfObjectDictionary ){for _ ,_dag :=range _dbc .Keys (){_bbe :=_dbc .Get (_dag );if _bge ,_ffd :=_bbe .(*_d .PdfObjectDictionary );_ffd {_caf :=_ab .New ();_caf .Write ([]byte (_bge .WriteString ()));_cdfe :=string (_caf .Sum (nil ));_cgfa [_cdfe ]=append (_cgfa [_cdfe ],_bge );_age (_bge );};};};for _ ,_aed :=range objects {_bcg ,_ddg :=_aed .(*_d .PdfIndirectObject );if !_ddg {continue ;};if _abbb ,_gcb :=_bcg .PdfObject .(*_d .PdfObjectDictionary );_gcb {_age (_abbb );};};_ceb :=make ([]_d .PdfObject ,0,len (_cgfa ));_ddc :=make (map[_d .PdfObject ]_d .PdfObject );for _ ,_aabcc :=range _cgfa {if len (_aabcc )< 2{continue ;};_fd :=_d .MakeDict ();_fd .Merge (_aabcc [0]);_fda :=_d .MakeIndirectObject (_fd );_ceb =append (_ceb ,_fda );for _dae :=0;_dae < len (_aabcc );_dae ++{_bdg :=_aabcc [_dae ];_ddc [_bdg ]=_fda ;};};_eca =make ([]_d .PdfObject ,len (objects ));copy (_eca ,objects );_eca =append (_ceb ,_eca ...);_bcd (_eca ,_ddc );return _eca ,nil ;};
|
@ -10,64 +10,43 @@
|
||||
// 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 (_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 (_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 );};
|
||||
|
||||
// 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 (_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 )(_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 ;};
|
||||
|
||||
// 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 (_ec *docTimeStamp )NewDigest (sig *_da .PdfSignature )(_da .Hasher ,error ){return _f .NewBuffer (nil ),nil ;};
|
||||
|
||||
// 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 ;};
|
||||
package sighandler ;import (_g "bytes";_dd "crypto";_e "crypto/rand";_ab "crypto/rsa";_ag "crypto/x509";_fcd "crypto/x509/pkix";_ad "encoding/asn1";_gc "errors";_db "fmt";_ge "github.com/unidoc/pkcs7";_af "github.com/unidoc/timestamp";_bd "github.com/unidoc/unipdf/v3/core";_c "github.com/unidoc/unipdf/v3/model";_b "hash";_d "io";_fcg "io/ioutil";_fc "net/http";_f "time";);
|
||||
|
||||
// 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 ;};
|
||||
func NewAdobeX509RSASHA1Custom (certificate *_ag .Certificate ,signFunc SignFunc )(_c .SignatureHandler ,error ){return &adobeX509RSASHA1 {_dea :certificate ,_dcd :signFunc },nil ;};
|
||||
|
||||
// Sign sets the Contents fields for the PdfSignature.
|
||||
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 ;};
|
||||
// NewDigest creates a new digest.
|
||||
func (_fa *adobeX509RSASHA1 )NewDigest (sig *_c .PdfSignature )(_c .Hasher ,error ){_dba ,_dad :=_fa .getCertificate (sig );if _dad !=nil {return nil ,_dad ;};_fda ,_ :=_dgc (_dba .SignatureAlgorithm );return _fda .New (),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 *_ab .PrivateKey ,certificate *_ag .Certificate )(_c .SignatureHandler ,error ){return &adobePKCS7Detached {_gg :certificate ,_fd :privateKey },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 ;};
|
||||
func (_daa *adobeX509RSASHA1 )Validate (sig *_c .PdfSignature ,digest _c .Hasher )(_c .SignatureValidationResult ,error ){_fea ,_bcf :=_daa .getCertificate (sig );if _bcf !=nil {return _c .SignatureValidationResult {},_bcf ;};_ae :=sig .Contents .Bytes ();var _bcb []byte ;if _ ,_cfc :=_ad .Unmarshal (_ae ,&_bcb );_cfc !=nil {return _c .SignatureValidationResult {},_cfc ;};_aaf ,_ff :=digest .(_b .Hash );if !_ff {return _c .SignatureValidationResult {},_gc .New ("\u0068a\u0073h\u0020\u0074\u0079\u0070\u0065\u0020\u0065\u0072\u0072\u006f\u0072");};_cgg ,_ :=_dgc (_fea .SignatureAlgorithm );if _bg :=_ab .VerifyPKCS1v15 (_fea .PublicKey .(*_ab .PublicKey ),_cgg ,_aaf .Sum (nil ),_bcb );_bg !=nil {return _c .SignatureValidationResult {},_bg ;};return _c .SignatureValidationResult {IsSigned :true ,IsVerified :true },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";};
|
||||
// NewDigest creates a new digest.
|
||||
func (_daga *docTimeStamp )NewDigest (sig *_c .PdfSignature )(_c .Hasher ,error ){return _g .NewBuffer (nil ),nil ;};
|
||||
|
||||
// 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";};
|
||||
// Validate validates PdfSignature.
|
||||
func (_dde *docTimeStamp )Validate (sig *_c .PdfSignature ,digest _c .Hasher )(_c .SignatureValidationResult ,error ){_fff :=sig .Contents .Bytes ();_bgg ,_fdg :=_ge .Parse (_fff );if _fdg !=nil {return _c .SignatureValidationResult {},_fdg ;};if _fdg =_bgg .Verify ();_fdg !=nil {return _c .SignatureValidationResult {},_fdg ;};var _bbf timestampInfo ;_ ,_fdg =_ad .Unmarshal (_bgg .Content ,&_bbf );if _fdg !=nil {return _c .SignatureValidationResult {},_fdg ;};_feaa ,_fdg :=_gfd (_bbf .MessageImprint .HashAlgorithm .Algorithm );if _fdg !=nil {return _c .SignatureValidationResult {},_fdg ;};_fbd :=_feaa .New ();_acd :=digest .(*_g .Buffer );_fbd .Write (_acd .Bytes ());_fcdc :=_fbd .Sum (nil );_bcfb :=_c .SignatureValidationResult {IsSigned :true ,IsVerified :_g .Equal (_fcdc ,_bbf .MessageImprint .HashedMessage ),GeneralizedTime :_bbf .GeneralizedTime };return _bcfb ,nil ;};
|
||||
|
||||
// InitSignature initialises the PdfSignature.
|
||||
func (_da *adobeX509RSASHA1 )InitSignature (sig *_c .PdfSignature )error {if _da ._dea ==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 _da ._gd ==nil &&_da ._dcd ==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");};_cad :=*_da ;sig .Handler =&_cad ;sig .Filter =_bd .MakeName ("\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065");sig .SubFilter =_bd .MakeName ("\u0061d\u0062e\u002e\u0078\u0035\u0030\u0039.\u0072\u0073a\u005f\u0073\u0068\u0061\u0031");sig .Cert =_bd .MakeString (string (_cad ._dea .Raw ));sig .Reference =nil ;_cadb ,_cc :=_cad .NewDigest (sig );if _cc !=nil {return _cc ;};_cadb .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 ,_cadb );};func (_efg *adobeX509RSASHA1 )getCertificate (_fcgc *_c .PdfSignature )(*_ag .Certificate ,error ){if _efg ._dea !=nil {return _efg ._dea ,nil ;};var _agf []byte ;switch _eg :=_fcgc .Cert .(type ){case *_bd .PdfObjectString :_agf =_eg .Bytes ();case *_bd .PdfObjectArray :if _eg .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 _ ,_feg :=range _eg .Elements (){_dag ,_ddg :=_bd .GetString (_feg );if !_ddg {return nil ,_db .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",_feg );};_agf =append (_agf ,_dag .Bytes ()...);};default:return nil ,_db .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",_eg );};_bc ,_eaf :=_ag .ParseCertificates (_agf );if _eaf !=nil {return nil ,_eaf ;};return _bc [0],nil ;};
|
||||
|
||||
// Sign sets the Contents fields for the PdfSignature.
|
||||
func (_ccc *adobeX509RSASHA1 )Sign (sig *_c .PdfSignature ,digest _c .Hasher )error {var _bae []byte ;var _ce error ;if _ccc ._dcd !=nil {_bae ,_ce =_ccc ._dcd (sig ,digest );if _ce !=nil {return _ce ;};}else {_adb ,_df :=digest .(_b .Hash );if !_df {return _gc .New ("\u0068a\u0073h\u0020\u0074\u0079\u0070\u0065\u0020\u0065\u0072\u0072\u006f\u0072");};_ddgc ,_ :=_dgc (_ccc ._dea .SignatureAlgorithm );_bae ,_ce =_ab .SignPKCS1v15 (_e .Reader ,_ccc ._gd ,_ddgc ,_adb .Sum (nil ));if _ce !=nil {return _ce ;};};_bae ,_ce =_ad .Marshal (_bae );if _ce !=nil {return _ce ;};sig .Contents =_bd .MakeHexString (string (_bae ));return nil ;};
|
||||
|
||||
// InitSignature initialises the PdfSignature.
|
||||
func (_ea *adobePKCS7Detached )InitSignature (sig *_c .PdfSignature )error {if !_ea ._ga {if _ea ._gg ==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 _ea ._fd ==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");};};_adg :=*_ea ;sig .Handler =&_adg ;sig .Filter =_bd .MakeName ("\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065");sig .SubFilter =_bd .MakeName ("\u0061\u0064\u0062\u0065.p\u006b\u0063\u0073\u0037\u002e\u0064\u0065\u0074\u0061\u0063\u0068\u0065\u0064");sig .Reference =nil ;_gga ,_bf :=_adg .NewDigest (sig );if _bf !=nil {return _bf ;};_gga .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 _adg .Sign (sig ,_gga );};
|
||||
|
||||
// Sign sets the Contents fields.
|
||||
func (_dec *adobePKCS7Detached )Sign (sig *_c .PdfSignature ,digest _c .Hasher )error {if _dec ._ga {_dg :=_dec ._cg ;if _dg <=0{_dg =8192;};sig .Contents =_bd .MakeHexString (string (make ([]byte ,_dg )));return nil ;};_bfb :=digest .(*_g .Buffer );_gfg ,_afg :=_ge .NewSignedData (_bfb .Bytes ());if _afg !=nil {return _afg ;};if _ac :=_gfg .AddSigner (_dec ._gg ,_dec ._fd ,_ge .SignerInfoConfig {});_ac !=nil {return _ac ;};_gfg .Detach ();_abd ,_afg :=_gfg .Finish ();if _afg !=nil {return _afg ;};_ca :=make ([]byte ,8192);copy (_ca ,_abd );sig .Contents =_bd .MakeHexString (string (_ca ));return nil ;};type adobeX509RSASHA1 struct{_gd *_ab .PrivateKey ;_dea *_ag .Certificate ;_dcd SignFunc ;};
|
||||
|
||||
// SignFunc represents a custom signing function. The function should return
|
||||
// the computed signature.
|
||||
type SignFunc func (_efe *_c .PdfSignature ,_ded _c .Hasher )([]byte ,error );
|
||||
|
||||
// DocTimeStampOpts defines options for configuring the timestamp handler.
|
||||
type DocTimeStampOpts struct{
|
||||
@ -79,29 +58,50 @@ type DocTimeStampOpts struct{
|
||||
// signature.
|
||||
SignatureSize int ;};
|
||||
|
||||
// InitSignature initialises the PdfSignature.
|
||||
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 ;};
|
||||
func NewDocTimeStampWithOpts (timestampServerURL string ,hashAlgorithm _dd .Hash ,opts *DocTimeStampOpts )(_c .SignatureHandler ,error ){if opts ==nil {opts =&DocTimeStampOpts {};};if opts .SignatureSize <=0{opts .SignatureSize =4192;};return &docTimeStamp {_ed :timestampServerURL ,_ede :hashAlgorithm ,_abf :opts .SignatureSize },nil ;};type adobePKCS7Detached struct{_fd *_ab .PrivateKey ;_gg *_ag .Certificate ;_ga bool ;_cg int ;};
|
||||
|
||||
// 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 ;};
|
||||
func (_efc *adobePKCS7Detached )Validate (sig *_c .PdfSignature ,digest _c .Hasher )(_c .SignatureValidationResult ,error ){_bb :=sig .Contents .Bytes ();_dca ,_gf :=_ge .Parse (_bb );if _gf !=nil {return _c .SignatureValidationResult {},_gf ;};_fg :=digest .(*_g .Buffer );_dca .Content =_fg .Bytes ();if _gf =_dca .Verify ();_gf !=nil {return _c .SignatureValidationResult {},_gf ;};return _c .SignatureValidationResult {IsSigned :true ,IsVerified :true },nil ;};func (_bdc *adobePKCS7Detached )getCertificate (_de *_c .PdfSignature )(*_ag .Certificate ,error ){if _bdc ._gg !=nil {return _bdc ._gg ,nil ;};var _ef []byte ;switch _dc :=_de .Cert .(type ){case *_bd .PdfObjectString :_ef =_dc .Bytes ();case *_bd .PdfObjectArray :if _dc .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 _ ,_ec :=range _dc .Elements (){_cgd ,_gb :=_bd .GetString (_ec );if !_gb {return nil ,_db .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",_ec );};_ef =append (_ef ,_cgd .Bytes ()...);};default:return nil ,_db .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",_dc );};_eac ,_cge :=_ag .ParseCertificates (_ef );if _cge !=nil {return nil ,_cge ;};return _eac [0],nil ;};func _dgc (_bbg _ag .SignatureAlgorithm )(_dd .Hash ,bool ){var _fe _dd .Hash ;switch _bbg {case _ag .SHA1WithRSA :_fe =_dd .SHA1 ;case _ag .SHA256WithRSA :_fe =_dd .SHA256 ;case _ag .SHA384WithRSA :_fe =_dd .SHA384 ;case _ag .SHA512WithRSA :_fe =_dd .SHA512 ;default:return _dd .SHA1 ,false ;};return _fe ,true ;};
|
||||
|
||||
// NewAdobePKCS7Detached creates a new Adobe.PPKMS/Adobe.PPKLite adbe.pkcs7.detached signature handler.
|
||||
// Sign sets the Contents fields for the PdfSignature.
|
||||
func (_fcb *docTimeStamp )Sign (sig *_c .PdfSignature ,digest _c .Hasher )error {_eba :=digest .(*_g .Buffer );_fag :=_fcb ._ede .New ();if _ ,_afd :=_d .Copy (_fag ,_eba );_afd !=nil {return _afd ;};_aafg :=_af .Request {HashAlgorithm :_fcb ._ede ,HashedMessage :_fag .Sum (nil ),Certificates :true ,Extensions :nil ,ExtraExtensions :nil };_age ,_cdb :=_aafg .Marshal ();if _cdb !=nil {return _cdb ;};_fed ,_cdb :=_fc .Post (_fcb ._ed ,"a\u0070\u0070\u006c\u0069\u0063\u0061t\u0069\u006f\u006e\u002f\u0074\u0069\u006d\u0065\u0073t\u0061\u006d\u0070-\u0071u\u0065\u0072\u0079",_g .NewBuffer (_age ));if _cdb !=nil {return _cdb ;};defer _fed .Body .Close ();_cef ,_cdb :=_fcg .ReadAll (_fed .Body );if _cdb !=nil {return _cdb ;};if _fed .StatusCode !=_fc .StatusOK {return _db .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",_fed .StatusCode );};var _fbe struct{Version _ad .RawValue ;Content _ad .RawValue ;};if _ ,_cdb =_ad .Unmarshal (_cef ,&_fbe );_cdb !=nil {return _cdb ;};_cbe :=len (_fbe .Content .FullBytes );if _fcb ._abf > 0&&_cbe > _fcb ._abf {return _c .ErrSignNotEnoughSpace ;};if _cbe > 0{_fcb ._abf =_cbe +128;};sig .Contents =_bd .MakeHexString (string (_fbe .Content .FullBytes ));return nil ;};func (_afa *docTimeStamp )getCertificate (_geg *_c .PdfSignature )(*_ag .Certificate ,error ){var _eb []byte ;switch _bfd :=_geg .Cert .(type ){case *_bd .PdfObjectString :_eb =_bfd .Bytes ();case *_bd .PdfObjectArray :if _bfd .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 _ ,_cac :=range _bfd .Elements (){_fdb ,_dbc :=_bd .GetString (_cac );if !_dbc {return nil ,_db .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",_cac );};_eb =append (_eb ,_fdb .Bytes ()...);};default:return nil ,_db .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",_bfd );};_cb ,_cd :=_ag .ParseCertificates (_eb );if _cd !=nil {return nil ,_cd ;};return _cb [0],nil ;};
|
||||
|
||||
// NewAdobeX509RSASHA1 creates a new Adobe.PPKMS/Adobe.PPKLite adbe.x509.rsa_sha1 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 ;};
|
||||
func NewAdobeX509RSASHA1 (privateKey *_ab .PrivateKey ,certificate *_ag .Certificate )(_c .SignatureHandler ,error ){return &adobeX509RSASHA1 {_dea :certificate ,_gd :privateKey },nil ;};
|
||||
|
||||
// IsApplicable returns true if the signature handler is applicable for the PdfSignature
|
||||
func (_cf *adobePKCS7Detached )IsApplicable (sig *_c .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 timestampInfo struct{Version int ;Policy _ad .RawValue ;MessageImprint struct{HashAlgorithm _fcd .AlgorithmIdentifier ;HashedMessage []byte ;};SerialNumber _ad .RawValue ;GeneralizedTime _f .Time ;};
|
||||
|
||||
// InitSignature initialises the PdfSignature.
|
||||
func (_fae *docTimeStamp )InitSignature (sig *_c .PdfSignature )error {_gge :=*_fae ;sig .Handler =&_gge ;sig .Filter =_bd .MakeName ("\u0041\u0064\u006f\u0062\u0065\u002e\u0050\u0050\u004b\u004c\u0069\u0074\u0065");sig .SubFilter =_bd .MakeName ("\u0045\u0054\u0053I\u002e\u0052\u0046\u0043\u0033\u0031\u0036\u0031");sig .Reference =nil ;if _fae ._abf > 0{sig .Contents =_bd .MakeHexString (string (make ([]byte ,_fae ._abf )));}else {_fb ,_gcd :=_fae .NewDigest (sig );if _gcd !=nil {return _gcd ;};_fb .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 _gcd =_gge .Sign (sig ,_fb );_gcd !=nil {return _gcd ;};_fae ._abf =_gge ._abf ;};return nil ;};
|
||||
|
||||
// NewDigest creates a new digest.
|
||||
func (_gbb *adobePKCS7Detached )NewDigest (sig *_c .PdfSignature )(_c .Hasher ,error ){return _g .NewBuffer (nil ),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 _dd .Hash )(_c .SignatureHandler ,error ){return &docTimeStamp {_ed :timestampServerURL ,_ede :hashAlgorithm },nil ;};
|
||||
|
||||
// IsApplicable returns true if the signature handler is applicable for the PdfSignature.
|
||||
func (_gfa *docTimeStamp )IsApplicable (sig *_c .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";};
|
||||
|
||||
// IsApplicable returns true if the signature handler is applicable for the PdfSignature.
|
||||
func (_dbg *adobeX509RSASHA1 )IsApplicable (sig *_c .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";};type docTimeStamp struct{_ed string ;_ede _dd .Hash ;_abf int ;};
|
||||
|
||||
// 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 )(_c .SignatureHandler ,error ){return &adobePKCS7Detached {_ga :true ,_cg :signatureLen },nil ;};func _gfd (_be _ad .ObjectIdentifier )(_dd .Hash ,error ){switch {case _be .Equal (_ge .OIDDigestAlgorithmSHA1 ),_be .Equal (_ge .OIDDigestAlgorithmECDSASHA1 ),_be .Equal (_ge .OIDDigestAlgorithmDSA ),_be .Equal (_ge .OIDDigestAlgorithmDSASHA1 ),_be .Equal (_ge .OIDEncryptionAlgorithmRSA ):return _dd .SHA1 ,nil ;case _be .Equal (_ge .OIDDigestAlgorithmSHA256 ),_be .Equal (_ge .OIDDigestAlgorithmECDSASHA256 ):return _dd .SHA256 ,nil ;case _be .Equal (_ge .OIDDigestAlgorithmSHA384 ),_be .Equal (_ge .OIDDigestAlgorithmECDSASHA384 ):return _dd .SHA384 ,nil ;case _be .Equal (_ge .OIDDigestAlgorithmSHA512 ),_be .Equal (_ge .OIDDigestAlgorithmECDSASHA512 ):return _dd .SHA512 ,nil ;};return _dd .Hash (0),_ge .ErrUnsupportedAlgorithm ;};
|
@ -9,7 +9,7 @@
|
||||
// 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 (_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";);
|
||||
package pdfutil ;import (_a "github.com/unidoc/unipdf/v3/common";_cd "github.com/unidoc/unipdf/v3/contentstream";_c "github.com/unidoc/unipdf/v3/contentstream/draw";_b "github.com/unidoc/unipdf/v3/model";);
|
||||
|
||||
// NormalizePage performs the following operations on the passed in page:
|
||||
// - Normalize the page rotation.
|
||||
@ -24,4 +24,4 @@ package pdfutil ;import (_bb "github.com/unidoc/unipdf/v3/common";_e "github.com
|
||||
// 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 *_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 ;};
|
||||
func NormalizePage (page *_b .PdfPage )error {_cc ,_fd :=page .GetMediaBox ();if _fd !=nil {return _fd ;};_e ,_fd :=page .GetContentStreams ();if _fd !=nil {return _fd ;};_fdb :=page .Rotate ;_d :=_fdb !=nil &&*_fdb !=0&&*_fdb %90==0;_cc .Normalize ();_df ,_cdf ,_cg ,_be :=_cc .Llx ,_cc .Lly ,_cc .Width (),_cc .Height ();_g :=_df !=0||_cdf !=0;if !_d &&!_g {return nil ;};_fb :=func (_fbe ,_ab ,_cga float64 )_c .BoundingBox {return _c .Path {Points :[]_c .Point {_c .NewPoint (0,0).Rotate (_cga ),_c .NewPoint (_fbe ,0).Rotate (_cga ),_c .NewPoint (0,_ab ).Rotate (_cga ),_c .NewPoint (_fbe ,_ab ).Rotate (_cga )}}.GetBoundingBox ();};_db :=_cd .NewContentCreator ();var _bg float64 ;if _d {_bg =-float64 (*page .Rotate );_gg :=_fb (_cg ,_be ,_bg );_db .Translate ((_gg .Width -_cg )/2+_cg /2,(_gg .Height -_be )/2+_be /2);_db .RotateDeg (_bg );_db .Translate (-_cg /2,-_be /2);_cg ,_be =_gg .Width ,_gg .Height ;};if _g {_db .Translate (-_df ,-_cdf );};_bed :=_db .Operations ().String ();_e =append ([]string {_bed },_e ...);if _fd =page .SetContentStreams (_e ,nil );_fd !=nil {return _fd ;};*_cc =_b .PdfRectangle {Urx :_cg ,Ury :_be };if _ac :=page .CropBox ;_ac !=nil {_ac .Normalize ();_ga ,_fdbd ,_dfa ,_fg :=_ac .Llx -_df ,_ac .Lly -_cdf ,_ac .Width (),_ac .Height ();if _d {_gc :=_fb (_dfa ,_fg ,_bg );_dfa ,_fg =_gc .Width ,_gc .Height ;};*_ac =_b .PdfRectangle {Llx :_ga ,Lly :_fdbd ,Urx :_ga +_dfa ,Ury :_fdbd +_fg };};_a .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",_bg ,_bed ,_cc );page .Rotate =nil ;return nil ;};
|
144
ps/ps.go
144
ps/ps.go
@ -14,86 +14,83 @@
|
||||
//
|
||||
// Package ps implements various functionalities needed for handling Postscript for PDF uses, in particular
|
||||
// for PDF function type 4.
|
||||
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 ;};
|
||||
package ps ;import (_c "bufio";_f "bytes";_e "errors";_dc "fmt";_cd "github.com/unidoc/unipdf/v3/common";_dce "github.com/unidoc/unipdf/v3/core";_d "io";_a "math";);
|
||||
|
||||
// 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 ;};
|
||||
// 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 _fe []float64 ;for _ ,_ae :=range objects {if _ee ,_db :=_ae .(*PSInteger );_db {_fe =append (_fe ,float64 (_ee .Val ));}else if _eeb ,_af :=_ae .(*PSReal );_af {_fe =append (_fe ,_eeb .Val );}else {return nil ,ErrTypeCheck ;};};return _fe ,nil ;};
|
||||
|
||||
// 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 };
|
||||
|
||||
// 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 ;};
|
||||
// NewPSParser returns a new instance of the PDF Postscript parser from input data.
|
||||
func NewPSParser (content []byte )*PSParser {_cbaa :=PSParser {};_fbg :=_f .NewBuffer (content );_cbaa ._dadg =_c .NewReader (_fbg );return &_cbaa ;};func (_fff *PSOperand )exp (_dbgf *PSStack )error {_ecg ,_aeg :=_dbgf .PopNumberAsFloat64 ();if _aeg !=nil {return _aeg ;};_ebd ,_aeg :=_dbgf .PopNumberAsFloat64 ();if _aeg !=nil {return _aeg ;};if _a .Abs (_ecg )< 1&&_ebd < 0{return ErrUndefinedResult ;};_ggd :=_a .Pow (_ebd ,_ecg );_aeg =_dbgf .Push (MakeReal (_ggd ));return _aeg ;};
|
||||
|
||||
// Exec executes the operand `op` in the state specified by `stack`.
|
||||
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 ;};
|
||||
func (_cga *PSOperand )Exec (stack *PSStack )error {_abf :=ErrUnsupportedOperand ;switch *_cga {case "\u0061\u0062\u0073":_abf =_cga .abs (stack );case "\u0061\u0064\u0064":_abf =_cga .add (stack );case "\u0061\u006e\u0064":_abf =_cga .and (stack );case "\u0061\u0074\u0061\u006e":_abf =_cga .atan (stack );case "\u0062\u0069\u0074\u0073\u0068\u0069\u0066\u0074":_abf =_cga .bitshift (stack );case "\u0063e\u0069\u006c\u0069\u006e\u0067":_abf =_cga .ceiling (stack );case "\u0063\u006f\u0070\u0079":_abf =_cga .copy (stack );case "\u0063\u006f\u0073":_abf =_cga .cos (stack );case "\u0063\u0076\u0069":_abf =_cga .cvi (stack );case "\u0063\u0076\u0072":_abf =_cga .cvr (stack );case "\u0064\u0069\u0076":_abf =_cga .div (stack );case "\u0064\u0075\u0070":_abf =_cga .dup (stack );case "\u0065\u0071":_abf =_cga .eq (stack );case "\u0065\u0078\u0063\u0068":_abf =_cga .exch (stack );case "\u0065\u0078\u0070":_abf =_cga .exp (stack );case "\u0066\u006c\u006fo\u0072":_abf =_cga .floor (stack );case "\u0067\u0065":_abf =_cga .ge (stack );case "\u0067\u0074":_abf =_cga .gt (stack );case "\u0069\u0064\u0069\u0076":_abf =_cga .idiv (stack );case "\u0069\u0066":_abf =_cga .ifCondition (stack );case "\u0069\u0066\u0065\u006c\u0073\u0065":_abf =_cga .ifelse (stack );case "\u0069\u006e\u0064e\u0078":_abf =_cga .index (stack );case "\u006c\u0065":_abf =_cga .le (stack );case "\u006c\u006f\u0067":_abf =_cga .log (stack );case "\u006c\u006e":_abf =_cga .ln (stack );case "\u006c\u0074":_abf =_cga .lt (stack );case "\u006d\u006f\u0064":_abf =_cga .mod (stack );case "\u006d\u0075\u006c":_abf =_cga .mul (stack );case "\u006e\u0065":_abf =_cga .ne (stack );case "\u006e\u0065\u0067":_abf =_cga .neg (stack );case "\u006e\u006f\u0074":_abf =_cga .not (stack );case "\u006f\u0072":_abf =_cga .or (stack );case "\u0070\u006f\u0070":_abf =_cga .pop (stack );case "\u0072\u006f\u0075n\u0064":_abf =_cga .round (stack );case "\u0072\u006f\u006c\u006c":_abf =_cga .roll (stack );case "\u0073\u0069\u006e":_abf =_cga .sin (stack );case "\u0073\u0071\u0072\u0074":_abf =_cga .sqrt (stack );case "\u0073\u0075\u0062":_abf =_cga .sub (stack );case "\u0074\u0072\u0075\u006e\u0063\u0061\u0074\u0065":_abf =_cga .truncate (stack );case "\u0078\u006f\u0072":_abf =_cga .xor (stack );};return _abf ;};func (_bce *PSOperand )dup (_dcf *PSStack )error {_agd ,_ebca :=_dcf .Pop ();if _ebca !=nil {return _ebca ;};_ebca =_dcf .Push (_agd );if _ebca !=nil {return _ebca ;};_ebca =_dcf .Push (_agd .Duplicate ());return _ebca ;};func (_eb *PSOperand )String ()string {return _dc .Sprintf ("\u0025\u0073",*_eb )};func (_ega *PSOperand )eq (_eeg *PSStack )error {_bgf ,_adee :=_eeg .Pop ();if _adee !=nil {return _adee ;};_fgge ,_adee :=_eeg .Pop ();if _adee !=nil {return _adee ;};_eea ,_da :=_bgf .(*PSBoolean );_fec ,_dad :=_fgge .(*PSBoolean );if _da ||_dad {var _fbe error ;if _da &&_dad {_fbe =_eeg .Push (MakeBool (_eea .Val ==_fec .Val ));}else {_fbe =_eeg .Push (MakeBool (false ));};return _fbe ;};var _bcb float64 ;var _gcd float64 ;if _bd ,_bcgc :=_bgf .(*PSInteger );_bcgc {_bcb =float64 (_bd .Val );}else if _gbd ,_gab :=_bgf .(*PSReal );_gab {_bcb =_gbd .Val ;}else {return ErrTypeCheck ;};if _egbg ,_fgf :=_fgge .(*PSInteger );_fgf {_gcd =float64 (_egbg .Val );}else if _ceg ,_gdb :=_fgge .(*PSReal );_gdb {_gcd =_ceg .Val ;}else {return ErrTypeCheck ;};if _a .Abs (_gcd -_bcb )< _g {_adee =_eeg .Push (MakeBool (true ));}else {_adee =_eeg .Push (MakeBool (false ));};return _adee ;};func (_edga *PSParser )parseNumber ()(PSObject ,error ){_ggbd ,_bdcc :=_dce .ParseNumber (_edga ._dadg );if _bdcc !=nil {return nil ,_bdcc ;};switch _bef :=_ggbd .(type ){case *_dce .PdfObjectFloat :return MakeReal (float64 (*_bef )),nil ;case *_dce .PdfObjectInteger :return MakeInteger (int (*_bef )),nil ;};return nil ,_dc .Errorf ("\u0075n\u0068\u0061\u006e\u0064\u006c\u0065\u0064\u0020\u006e\u0075\u006db\u0065\u0072\u0020\u0074\u0079\u0070\u0065\u0020\u0025\u0054",_ggbd );};
|
||||
|
||||
// PSOperand represents a Postscript operand (text string).
|
||||
type PSOperand string ;func (_ff *PSReal )Duplicate ()PSObject {_fc :=PSReal {};_fc .Val =_ff .Val ;return &_fc };func (_cbd *PSProgram )String ()string {_cda :="\u007b\u0020";for _ ,_bb :=range *_cbd {_cda +=_bb .String ();_cda +="\u0020";};_cda +="\u007d";return _cda ;};func (_eed *PSOperand )floor (_cceb *PSStack )error {_bbd ,_gga :=_cceb .Pop ();if _gga !=nil {return _gga ;};if _ecdg ,_fcfgd :=_bbd .(*PSReal );_fcfgd {_gga =_cceb .Push (MakeReal (_a .Floor (_ecdg .Val )));}else if _caa ,_abg :=_bbd .(*PSInteger );_abg {_gga =_cceb .Push (MakeInteger (_caa .Val ));}else {return ErrTypeCheck ;};return _gga ;};func (_baae *PSOperand )le (_cgac *PSStack )error {_gae ,_abb :=_cgac .PopNumberAsFloat64 ();if _abb !=nil {return _abb ;};_bgc ,_abb :=_cgac .PopNumberAsFloat64 ();if _abb !=nil {return _abb ;};if _a .Abs (_bgc -_gae )< _g {_fgb :=_cgac .Push (MakeBool (true ));return _fgb ;}else if _bgc < _gae {_cfbe :=_cgac .Push (MakeBool (true ));return _cfbe ;}else {_bdd :=_cgac .Push (MakeBool (false ));return _bdd ;};};var ErrStackOverflow =_e .New ("\u0073\u0074\u0061\u0063\u006b\u0020\u006f\u0076\u0065r\u0066\u006c\u006f\u0077");func (_efeg *PSOperand )or (_ddec *PSStack )error {_ffee ,_bdda :=_ddec .Pop ();if _bdda !=nil {return _bdda ;};_cdf ,_bdda :=_ddec .Pop ();if _bdda !=nil {return _bdda ;};if _abbf ,_faf :=_ffee .(*PSBoolean );_faf {_cfg ,_ecab :=_cdf .(*PSBoolean );if !_ecab {return ErrTypeCheck ;};_bdda =_ddec .Push (MakeBool (_abbf .Val ||_cfg .Val ));return _bdda ;};if _efa ,_gdcb :=_ffee .(*PSInteger );_gdcb {_cdfe ,_cegf :=_cdf .(*PSInteger );if !_cegf {return ErrTypeCheck ;};_bdda =_ddec .Push (MakeInteger (_efa .Val |_cdfe .Val ));return _bdda ;};return ErrTypeCheck ;};func (_gbf *PSOperand )not (_afd *PSStack )error {_bbeg ,_dde :=_afd .Pop ();if _dde !=nil {return _dde ;};if _efc ,_ffa :=_bbeg .(*PSBoolean );_ffa {_dde =_afd .Push (MakeBool (!_efc .Val ));return _dde ;}else if _ccfg ,_ceffd :=_bbeg .(*PSInteger );_ceffd {_dde =_afd .Push (MakeInteger (^_ccfg .Val ));return _dde ;}else {return ErrTypeCheck ;};};func (_cb *PSBoolean )String ()string {return _dc .Sprintf ("\u0025\u0076",_cb .Val )};func (_ccf *PSOperand )cvr (_fcfg *PSStack )error {_ecf ,_gbg :=_fcfg .Pop ();if _gbg !=nil {return _gbg ;};if _dfb ,_abfc :=_ecf .(*PSReal );_abfc {_gbg =_fcfg .Push (MakeReal (_dfb .Val ));}else if _cfae ,_aac :=_ecf .(*PSInteger );_aac {_gbg =_fcfg .Push (MakeReal (float64 (_cfae .Val )));}else {return ErrTypeCheck ;};return _gbg ;};var ErrTypeCheck =_e .New ("\u0074\u0079p\u0065\u0020\u0063h\u0065\u0063\u006b\u0020\u0065\u0072\u0072\u006f\u0072");func (_gcgb *PSParser )parseFunction ()(*PSProgram ,error ){_afa ,_ :=_gcgb ._dadg .ReadByte ();if _afa !='{'{return nil ,_e .New ("\u0069\u006ev\u0061\u006c\u0069d\u0020\u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e");};_eada :=NewPSProgram ();for {_gcgb .skipSpaces ();_gegd ,_bgfg :=_gcgb ._dadg .Peek (2);if _bgfg !=nil {if _bgfg ==_d .EOF {break ;};return nil ,_bgfg ;};_cd .Log .Trace ("\u0050e\u0065k\u0020\u0073\u0074\u0072\u0069\u006e\u0067\u003a\u0020\u0025\u0073",string (_gegd ));if _gegd [0]=='}'{_cd .Log .Trace ("\u0045\u004f\u0046 \u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e");_gcgb ._dadg .ReadByte ();break ;}else if _gegd [0]=='{'{_cd .Log .Trace ("\u0046u\u006e\u0063\u0074\u0069\u006f\u006e!");_gdea ,_dcc :=_gcgb .parseFunction ();if _dcc !=nil {return nil ,_dcc ;};_eada .Append (_gdea );}else if _dce .IsDecimalDigit (_gegd [0])||(_gegd [0]=='-'&&_dce .IsDecimalDigit (_gegd [1])){_cd .Log .Trace ("\u002d>\u004e\u0075\u006d\u0062\u0065\u0072!");_ebag ,_deca :=_gcgb .parseNumber ();if _deca !=nil {return nil ,_deca ;};_eada .Append (_ebag );}else {_cd .Log .Trace ("\u002d>\u004fp\u0065\u0072\u0061\u006e\u0064 \u006f\u0072 \u0062\u006f\u006f\u006c\u003f");_gegd ,_ =_gcgb ._dadg .Peek (5);_fbaa :=string (_gegd );_cd .Log .Trace ("\u0050\u0065\u0065k\u0020\u0073\u0074\u0072\u003a\u0020\u0025\u0073",_fbaa );if (len (_fbaa )> 4)&&(_fbaa [:5]=="\u0066\u0061\u006cs\u0065"){_gecc ,_acf :=_gcgb .parseBool ();if _acf !=nil {return nil ,_acf ;};_eada .Append (_gecc );}else if (len (_fbaa )> 3)&&(_fbaa [:4]=="\u0074\u0072\u0075\u0065"){_ece ,_dace :=_gcgb .parseBool ();if _dace !=nil {return nil ,_dace ;};_eada .Append (_ece );}else {_abbd ,_ceb :=_gcgb .parseOperand ();if _ceb !=nil {return nil ,_ceb ;};_eada .Append (_abbd );};};};return _eada ,nil ;};
|
||||
|
||||
// Execute executes the program for an input parameters `objects` and returns a slice of output objects.
|
||||
func (_aef *PSExecutor )Execute (objects []PSObject )([]PSObject ,error ){for _ ,_ba :=range objects {_gg :=_aef .Stack .Push (_ba );if _gg !=nil {return nil ,_gg ;};};_fee :=_aef ._gc .Exec (_aef .Stack );if _fee !=nil {_cd .Log .Debug ("\u0045x\u0065c\u0020\u0066\u0061\u0069\u006c\u0065\u0064\u003a\u0020\u0025\u0076",_fee );return nil ,_fee ;};_cea :=[]PSObject (*_aef .Stack );_aef .Stack .Empty ();return _cea ,nil ;};func (_bgff *PSOperand )idiv (_cac *PSStack )error {_afg ,_eab :=_cac .Pop ();if _eab !=nil {return _eab ;};_dcg ,_eab :=_cac .Pop ();if _eab !=nil {return _eab ;};_eegc ,_cgf :=_afg .(*PSInteger );if !_cgf {return ErrTypeCheck ;};if _eegc .Val ==0{return ErrUndefinedResult ;};_gdg ,_cgf :=_dcg .(*PSInteger );if !_cgf {return ErrTypeCheck ;};_gcge :=_gdg .Val /_eegc .Val ;_eab =_cac .Push (MakeInteger (_gcge ));return _eab ;};func (_dgf *PSOperand )abs (_dbg *PSStack )error {_fb ,_eg :=_dbg .Pop ();if _eg !=nil {return _eg ;};if _dgfc ,_bf :=_fb .(*PSReal );_bf {_ebc :=_dgfc .Val ;if _ebc < 0{_eg =_dbg .Push (MakeReal (-_ebc ));}else {_eg =_dbg .Push (MakeReal (_ebc ));};}else if _egf ,_bea :=_fb .(*PSInteger );_bea {_ceac :=_egf .Val ;if _ceac < 0{_eg =_dbg .Push (MakeInteger (-_ceac ));}else {_eg =_dbg .Push (MakeInteger (_ceac ));};}else {return ErrTypeCheck ;};return _eg ;};func (_cce *PSOperand )atan (_ede *PSStack )error {_fdcc ,_dbea :=_ede .PopNumberAsFloat64 ();if _dbea !=nil {return _dbea ;};_ea ,_dbea :=_ede .PopNumberAsFloat64 ();if _dbea !=nil {return _dbea ;};if _fdcc ==0{var _gca error ;if _ea < 0{_gca =_ede .Push (MakeReal (270));}else {_gca =_ede .Push (MakeReal (90));};return _gca ;};_egc :=_ea /_fdcc ;_feb :=_a .Atan (_egc )*180/_a .Pi ;_dbea =_ede .Push (MakeReal (_feb ));return _dbea ;};func (_cdce *PSOperand )cos (_ebf *PSStack )error {_ag ,_abd :=_ebf .PopNumberAsFloat64 ();if _abd !=nil {return _abd ;};_febg :=_a .Cos (_ag *_a .Pi /180.0);_abd =_ebf .Push (MakeReal (_febg ));return _abd ;};
|
||||
|
||||
// PSExecutor has its own execution stack and is used to executre a PS routine (program).
|
||||
type PSExecutor struct{Stack *PSStack ;_gc *PSProgram ;};func (_gcb *PSReal )String ()string {return _dc .Sprintf ("\u0025\u002e\u0035\u0066",_gcb .Val )};
|
||||
|
||||
// MakeInteger returns a new PSInteger object initialized with `val`.
|
||||
func MakeInteger (val int )*PSInteger {_babb :=PSInteger {};_babb .Val =val ;return &_babb };func (_dbf *PSParser )skipSpaces ()(int ,error ){_gcc :=0;for {_fggc ,_ceea :=_dbf ._dadg .Peek (1);if _ceea !=nil {return 0,_ceea ;};if _dce .IsWhiteSpace (_fggc [0]){_dbf ._dadg .ReadByte ();_gcc ++;}else {break ;};};return _gcc ,nil ;};func (_eff *PSParser )parseOperand ()(*PSOperand ,error ){var _acgg []byte ;for {_gbged ,_dga :=_eff ._dadg .Peek (1);if _dga !=nil {if _dga ==_d .EOF {break ;};return nil ,_dga ;};if _dce .IsDelimiter (_gbged [0]){break ;};if _dce .IsWhiteSpace (_gbged [0]){break ;};_dgbd ,_ :=_eff ._dadg .ReadByte ();_acgg =append (_acgg ,_dgbd );};if len (_acgg )==0{return nil ,_e .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 (_acgg )),nil ;};func (_cca *PSOperand )truncate (_gcea *PSStack )error {_cfe ,_dcef :=_gcea .Pop ();if _dcef !=nil {return _dcef ;};if _cde ,_bbcf :=_cfe .(*PSReal );_bbcf {_eef :=int (_cde .Val );_dcef =_gcea .Push (MakeReal (float64 (_eef )));}else if _dafg ,_cfca :=_cfe .(*PSInteger );_cfca {_dcef =_gcea .Push (MakeInteger (_dafg .Val ));}else {return ErrTypeCheck ;};return _dcef ;};func (_feec *PSBoolean )DebugString ()string {return _dc .Sprintf ("\u0062o\u006f\u006c\u003a\u0025\u0076",_feec .Val );};func (_aa *PSOperand )Duplicate ()PSObject {_fgg :=*_aa ;return &_fgg };func (_cfa *PSOperand )ceiling (_dced *PSStack )error {_edf ,_cbde :=_dced .Pop ();if _cbde !=nil {return _cbde ;};if _cba ,_edec :=_edf .(*PSReal );_edec {_cbde =_dced .Push (MakeReal (_a .Ceil (_cba .Val )));}else if _dba ,_fgd :=_edf .(*PSInteger );_fgd {_cbde =_dced .Push (MakeInteger (_dba .Val ));}else {_cbde =ErrTypeCheck ;};return _cbde ;};var ErrUnsupportedOperand =_e .New ("\u0075\u006e\u0073\u0075pp\u006f\u0072\u0074\u0065\u0064\u0020\u006f\u0070\u0065\u0072\u0061\u006e\u0064");
|
||||
|
||||
// Parse parses the postscript and store as a program that can be executed.
|
||||
func (_agbef *PSParser )Parse ()(*PSProgram ,error ){_agbef .skipSpaces ();_gbfg ,_ffb :=_agbef ._dadg .Peek (2);if _ffb !=nil {return nil ,_ffb ;};if _gbfg [0]!='{'{return nil ,_e .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");};_cab ,_ffb :=_agbef .parseFunction ();if _ffb !=nil &&_ffb !=_d .EOF {return nil ,_ffb ;};return _cab ,_ffb ;};func (_bg *PSReal )DebugString ()string {return _dc .Sprintf ("\u0072e\u0061\u006c\u003a\u0025\u002e\u0035f",_bg .Val );};const _g =0.000001;
|
||||
|
||||
// NewPSProgram returns an empty, initialized PSProgram.
|
||||
func NewPSProgram ()*PSProgram {return &PSProgram {}};func (_be *PSOperand )DebugString ()string {return _dc .Sprintf ("\u006fp\u003a\u0027\u0025\u0073\u0027",*_be );};func (_fac *PSOperand )ln (_ecda *PSStack )error {_cgcf ,_cbag :=_ecda .PopNumberAsFloat64 ();if _cbag !=nil {return _cbag ;};_bdc :=_a .Log (_cgcf );_cbag =_ecda .Push (MakeReal (_bdc ));return _cbag ;};func (_eega *PSOperand )neg (_daed *PSStack )error {_ccdb ,_ead :=_daed .Pop ();if _ead !=nil {return _ead ;};if _bfba ,_fbb :=_ccdb .(*PSReal );_fbb {_ead =_daed .Push (MakeReal (-_bfba .Val ));return _ead ;}else if _dbd ,_ecgg :=_ccdb .(*PSInteger );_ecgg {_ead =_daed .Push (MakeInteger (-_dbd .Val ));return _ead ;}else {return ErrTypeCheck ;};};func (_fdba *PSOperand )ge (_dff *PSStack )error {_ded ,_fda :=_dff .PopNumberAsFloat64 ();if _fda !=nil {return _fda ;};_ddb ,_fda :=_dff .PopNumberAsFloat64 ();if _fda !=nil {return _fda ;};if _a .Abs (_ddb -_ded )< _g {_gbbd :=_dff .Push (MakeBool (true ));return _gbbd ;}else if _ddb > _ded {_dfe :=_dff .Push (MakeBool (true ));return _dfe ;}else {_egff :=_dff .Push (MakeBool (false ));return _egff ;};};func (_bab *PSOperand )exch (_adb *PSStack )error {_aec ,_cfb :=_adb .Pop ();if _cfb !=nil {return _cfb ;};_eba ,_cfb :=_adb .Pop ();if _cfb !=nil {return _cfb ;};_cfb =_adb .Push (_aec );if _cfb !=nil {return _cfb ;};_cfb =_adb .Push (_eba );return _cfb ;};func (_fefe *PSOperand )sqrt (_daca *PSStack )error {_edg ,_ebg :=_daca .PopNumberAsFloat64 ();if _ebg !=nil {return _ebg ;};if _edg < 0{return ErrRangeCheck ;};_edb :=_a .Sqrt (_edg );_ebg =_daca .Push (MakeReal (_edb ));return _ebg ;};func (_dbc *PSOperand )roll (_ebec *PSStack )error {_faaa ,_facg :=_ebec .Pop ();if _facg !=nil {return _facg ;};_beae ,_facg :=_ebec .Pop ();if _facg !=nil {return _facg ;};_gff ,_ffc :=_faaa .(*PSInteger );if !_ffc {return ErrTypeCheck ;};_dca ,_ffc :=_beae .(*PSInteger );if !_ffc {return ErrTypeCheck ;};if _dca .Val < 0{return ErrRangeCheck ;};if _dca .Val ==0||_dca .Val ==1{return nil ;};if _dca .Val > len (*_ebec ){return ErrStackUnderflow ;};for _bec :=0;_bec < _dgad (_gff .Val );_bec ++{var _ggb []PSObject ;_ggb =(*_ebec )[len (*_ebec )-(_dca .Val ):len (*_ebec )];if _gff .Val > 0{_gba :=_ggb [len (_ggb )-1];_ggb =append ([]PSObject {_gba },_ggb [0:len (_ggb )-1]...);}else {_cbab :=_ggb [len (_ggb )-_dca .Val ];_ggb =append (_ggb [1:],_cbab );};_bfaf :=append ((*_ebec )[0:len (*_ebec )-_dca .Val ],_ggb ...);_ebec =&_bfaf ;};return nil ;};func (_efegb *PSOperand )sin (_ffec *PSStack )error {_cdaa ,_adbf :=_ffec .PopNumberAsFloat64 ();if _adbf !=nil {return _adbf ;};_ffca :=_a .Sin (_cdaa *_a .Pi /180.0);_adbf =_ffec .Push (MakeReal (_ffca ));return _adbf ;};func (_aaa *PSOperand )ifCondition (_efg *PSStack )error {_add ,_ggec :=_efg .Pop ();if _ggec !=nil {return _ggec ;};_gee ,_ggec :=_efg .Pop ();if _ggec !=nil {return _ggec ;};_bgb ,_daf :=_add .(*PSProgram );if !_daf {return ErrTypeCheck ;};_gfb ,_daf :=_gee .(*PSBoolean );if !_daf {return ErrTypeCheck ;};if _gfb .Val {_fbdg :=_bgb .Exec (_efg );return _fbdg ;};return nil ;};
|
||||
|
||||
// PSStack defines a stack of PSObjects. PSObjects can be pushed on or pull from the stack.
|
||||
type PSStack []PSObject ;
|
||||
|
||||
// PSBoolean represents a boolean value.
|
||||
type PSBoolean struct{Val bool ;};func (_bebb *PSOperand )log (_bagd *PSStack )error {_gcbf ,_faec :=_bagd .PopNumberAsFloat64 ();if _faec !=nil {return _faec ;};_bbg :=_a .Log10 (_gcbf );_faec =_bagd .Push (MakeReal (_bbg ));return _faec ;};
|
||||
|
||||
// MakeOperand returns a new PSOperand object based on string `val`.
|
||||
func MakeOperand (val string )*PSOperand {_ggbf :=PSOperand (val );return &_ggbf };var ErrUndefinedResult =_e .New ("\u0075\u006e\u0064\u0065fi\u006e\u0065\u0064\u0020\u0072\u0065\u0073\u0075\u006c\u0074\u0020\u0065\u0072\u0072o\u0072");func (_gdf *PSOperand )copy (_edd *PSStack )error {_baa ,_ebbd :=_edd .PopInteger ();if _ebbd !=nil {return _ebbd ;};if _baa < 0{return ErrRangeCheck ;};if _baa > len (*_edd ){return ErrRangeCheck ;};*_edd =append (*_edd ,(*_edd )[len (*_edd )-_baa :]...);return nil ;};func (_eee *PSInteger )Duplicate ()PSObject {_ga :=PSInteger {};_ga .Val =_eee .Val ;return &_ga };func (_fg *PSInteger )String ()string {return _dc .Sprintf ("\u0025\u0064",_fg .Val )};
|
||||
|
||||
// MakeBool returns a new PSBoolean object initialized with `val`.
|
||||
func MakeBool (val bool )*PSBoolean {_egg :=PSBoolean {};_egg .Val =val ;return &_egg };func (_gfd *PSOperand )div (_agfd *PSStack )error {_ffe ,_cec :=_agfd .Pop ();if _cec !=nil {return _cec ;};_deee ,_cec :=_agfd .Pop ();if _cec !=nil {return _cec ;};_gbgb ,_baf :=_ffe .(*PSReal );_aeb ,_cefb :=_ffe .(*PSInteger );if !_baf &&!_cefb {return ErrTypeCheck ;};if _baf &&_gbgb .Val ==0{return ErrUndefinedResult ;};if _cefb &&_aeb .Val ==0{return ErrUndefinedResult ;};_fbda ,_cad :=_deee .(*PSReal );_ac ,_agb :=_deee .(*PSInteger );if !_cad &&!_agb {return ErrTypeCheck ;};var _fcaf float64 =0;if _cad {_fcaf =_fbda .Val ;}else {_fcaf =float64 (_ac .Val );};if _baf {_fcaf /=_gbgb .Val ;}else {_fcaf /=float64 (_aeb .Val );};_cec =_agfd .Push (MakeReal (_fcaf ));return _cec ;};func (_ed *PSOperand )and (_df *PSStack )error {_ecc ,_afc :=_df .Pop ();if _afc !=nil {return _afc ;};_dee ,_afc :=_df .Pop ();if _afc !=nil {return _afc ;};if _bc ,_ged :=_ecc .(*PSBoolean );_ged {_fdc ,_geg :=_dee .(*PSBoolean );if !_geg {return ErrTypeCheck ;};_afc =_df .Push (MakeBool (_bc .Val &&_fdc .Val ));return _afc ;};if _ebb ,_ca :=_ecc .(*PSInteger );_ca {_cc ,_fcf :=_dee .(*PSInteger );if !_fcf {return ErrTypeCheck ;};_afc =_df .Push (MakeInteger (_ebb .Val &_cc .Val ));return _afc ;};return ErrTypeCheck ;};func (_bgfd *PSOperand )mod (_dec *PSStack )error {_bfa ,_bdba :=_dec .Pop ();if _bdba !=nil {return _bdba ;};_gbe ,_bdba :=_dec .Pop ();if _bdba !=nil {return _bdba ;};_ecdb ,_gbc :=_bfa .(*PSInteger );if !_gbc {return ErrTypeCheck ;};if _ecdb .Val ==0{return ErrUndefinedResult ;};_bfag ,_gbc :=_gbe .(*PSInteger );if !_gbc {return ErrTypeCheck ;};_caad :=_bfag .Val %_ecdb .Val ;_bdba =_dec .Push (MakeInteger (_caad ));return _bdba ;};func (_bac *PSBoolean )Duplicate ()PSObject {_cg :=PSBoolean {};_cg .Val =_bac .Val ;return &_cg };
|
||||
|
||||
// Append appends an object to the PSProgram.
|
||||
func (_fd *PSProgram )Append (obj PSObject ){*_fd =append (*_fd ,obj )};func (_gce *PSInteger )DebugString ()string {return _dc .Sprintf ("\u0069\u006e\u0074\u003a\u0025\u0064",_gce .Val );};func (_ffad *PSOperand )round (_ecae *PSStack )error {_abe ,_fdccf :=_ecae .Pop ();if _fdccf !=nil {return _fdccf ;};if _fbdc ,_bed :=_abe .(*PSReal );_bed {_fdccf =_ecae .Push (MakeReal (_a .Floor (_fbdc .Val +0.5)));}else if _bdbf ,_cead :=_abe .(*PSInteger );_cead {_fdccf =_ecae .Push (MakeInteger (_bdbf .Val ));}else {return ErrTypeCheck ;};return _fdccf ;};func (_gbge *PSOperand )index (_fae *PSStack )error {_ace ,_cfc :=_fae .Pop ();if _cfc !=nil {return _cfc ;};_fcad ,_gag :=_ace .(*PSInteger );if !_gag {return ErrTypeCheck ;};if _fcad .Val < 0{return ErrRangeCheck ;};if _fcad .Val > len (*_fae )-1{return ErrStackUnderflow ;};_bag :=(*_fae )[len (*_fae )-1-_fcad .Val ];_cfc =_fae .Push (_bag .Duplicate ());return _cfc ;};func (_bae *PSOperand )pop (_bba *PSStack )error {_ ,_ggae :=_bba .Pop ();if _ggae !=nil {return _ggae ;};return nil ;};func _dgad (_acd int )int {if _acd < 0{return -_acd ;};return _acd ;};
|
||||
|
||||
// Push pushes an object on top of the stack.
|
||||
func (_bfc *PSStack )Push (obj PSObject )error {if len (*_bfc )> 100{return ErrStackOverflow ;};*_bfc =append (*_bfc ,obj );return nil ;};
|
||||
|
||||
// PSReal represents a real number.
|
||||
type PSReal struct{Val float64 ;};
|
||||
|
||||
// PSInteger represents an integer.
|
||||
type PSInteger struct{Val int ;};
|
||||
|
||||
// PopInteger specificially pops an integer from the top of the stack, returning the value as an int.
|
||||
func (_fecf *PSStack )PopInteger ()(int ,error ){_adda ,_ggeg :=_fecf .Pop ();if _ggeg !=nil {return 0,_ggeg ;};if _ddgc ,_eag :=_adda .(*PSInteger );_eag {return _ddgc .Val ,nil ;};return 0,ErrTypeCheck ;};
|
||||
|
||||
// PSProgram defines a Postscript program which is a series of PS objects (arguments, commands, programs etc).
|
||||
type PSProgram []PSObject ;
|
||||
|
||||
// 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 );};
|
||||
// String returns a string representation of the stack.
|
||||
func (_dgbcd *PSStack )String ()string {_dgfe :="\u005b\u0020";for _ ,_gac :=range *_dgbcd {_dgfe +=_gac .String ();_dgfe +="\u0020";};_dgfe +="\u005d";return _dgfe ;};func (_deb *PSOperand )xor (_cee *PSStack )error {_gbbb ,_bddc :=_cee .Pop ();if _bddc !=nil {return _bddc ;};_beg ,_bddc :=_cee .Pop ();if _bddc !=nil {return _bddc ;};if _fea ,_aae :=_gbbb .(*PSBoolean );_aae {_afcg ,_deg :=_beg .(*PSBoolean );if !_deg {return ErrTypeCheck ;};_bddc =_cee .Push (MakeBool (_fea .Val !=_afcg .Val ));return _bddc ;};if _fdbe ,_agbe :=_gbbb .(*PSInteger );_agbe {_edgc ,_ccg :=_beg .(*PSInteger );if !_ccg {return ErrTypeCheck ;};_bddc =_cee .Push (MakeInteger (_fdbe .Val ^_edgc .Val ));return _bddc ;};return ErrTypeCheck ;};func (_fa *PSProgram )DebugString ()string {_gf :="\u007b\u0020";for _ ,_fge :=range *_fa {_gf +=_fge .DebugString ();_gf +="\u0020";};_gf +="\u007d";return _gf ;};func (_gde *PSOperand )add (_gge *PSStack )error {_ad ,_dd :=_gge .Pop ();if _dd !=nil {return _dd ;};_gcg ,_dd :=_gge .Pop ();if _dd !=nil {return _dd ;};_gcef ,_gb :=_ad .(*PSReal );_cgdc ,_fca :=_ad .(*PSInteger );if !_gb &&!_fca {return ErrTypeCheck ;};_gea ,_aefa :=_gcg .(*PSReal );_bbb ,_dge :=_gcg .(*PSInteger );if !_aefa &&!_dge {return ErrTypeCheck ;};if _fca &&_dge {_bbc :=_cgdc .Val +_bbb .Val ;_de :=_gge .Push (MakeInteger (_bbc ));return _de ;};var _eebb float64 =0;if _gb {_eebb =_gcef .Val ;}else {_eebb =float64 (_cgdc .Val );};if _aefa {_eebb +=_gea .Val ;}else {_eebb +=float64 (_bbb .Val );};_dd =_gge .Push (MakeReal (_eebb ));return _dd ;};
|
||||
|
||||
// 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 ;};
|
||||
func (_ddg *PSStack )Empty (){*_ddg =[]PSObject {}};
|
||||
|
||||
// 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 ;};
|
||||
// NewPSExecutor returns an initialized PSExecutor for an input `program`.
|
||||
func NewPSExecutor (program *PSProgram )*PSExecutor {_ge :=&PSExecutor {};_ge .Stack =NewPSStack ();_ge ._gc =program ;return _ge ;};
|
||||
|
||||
// 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 ;};
|
||||
// DebugString returns a descriptive string representation of the stack - intended for debugging.
|
||||
func (_dcb *PSStack )DebugString ()string {_cgg :="\u005b\u0020";for _ ,_bfab :=range *_dcb {_cgg +=_bfab .DebugString ();_cgg +="\u0020";};_cgg +="\u005d";return _cgg ;};
|
||||
|
||||
// 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 ;};
|
||||
func (_gcdg *PSStack )Pop ()(PSObject ,error ){if len (*_gcdg )< 1{return nil ,ErrStackUnderflow ;};_gbda :=(*_gcdg )[len (*_gcdg )-1];*_gcdg =(*_gcdg )[0:len (*_gcdg )-1];return _gbda ,nil ;};var ErrStackUnderflow =_e .New ("\u0073t\u0061c\u006b\u0020\u0075\u006e\u0064\u0065\u0072\u0066\u006c\u006f\u0077");
|
||||
|
||||
// NewPSStack returns an initialized 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 ;};
|
||||
func NewPSStack ()*PSStack {return &PSStack {}};
|
||||
|
||||
// PSObject represents a postscript object.
|
||||
type PSObject interface{
|
||||
@ -106,14 +103,17 @@ Duplicate ()PSObject ;
|
||||
DebugString ()string ;
|
||||
|
||||
// String returns a string representation of the PSObject.
|
||||
String ()string ;};
|
||||
String ()string ;};func (_fef *PSOperand )lt (_bbbf *PSStack )error {_dae ,_aegb :=_bbbf .PopNumberAsFloat64 ();if _aegb !=nil {return _aegb ;};_gfg ,_aegb :=_bbbf .PopNumberAsFloat64 ();if _aegb !=nil {return _aegb ;};if _a .Abs (_gfg -_dae )< _g {_aab :=_bbbf .Push (MakeBool (false ));return _aab ;}else if _gfg < _dae {_ceff :=_bbbf .Push (MakeBool (true ));return _ceff ;}else {_faa :=_bbbf .Push (MakeBool (false ));return _faa ;};};func (_dfg *PSOperand )sub (_cadc *PSStack )error {_fba ,_gcgeg :=_cadc .Pop ();if _gcgeg !=nil {return _gcgeg ;};_fffa ,_gcgeg :=_cadc .Pop ();if _gcgeg !=nil {return _gcgeg ;};_eebf ,_efga :=_fba .(*PSReal );_egbe ,_gdag :=_fba .(*PSInteger );if !_efga &&!_gdag {return ErrTypeCheck ;};_aceg ,_dada :=_fffa .(*PSReal );_dcgc ,_ccfa :=_fffa .(*PSInteger );if !_dada &&!_ccfa {return ErrTypeCheck ;};if _gdag &&_ccfa {_ffff :=_dcgc .Val -_egbe .Val ;_bde :=_cadc .Push (MakeInteger (_ffff ));return _bde ;};var _edee float64 =0;if _dada {_edee =_aceg .Val ;}else {_edee =float64 (_dcgc .Val );};if _efga {_edee -=_eebf .Val ;}else {_edee -=float64 (_egbe .Val );};_gcgeg =_cadc .Push (MakeReal (_edee ));return _gcgeg ;};func (_fgde *PSParser )parseBool ()(*PSBoolean ,error ){_aaec ,_acg :=_fgde ._dadg .Peek (4);if _acg !=nil {return MakeBool (false ),_acg ;};if (len (_aaec )>=4)&&(string (_aaec [:4])=="\u0074\u0072\u0075\u0065"){_fgde ._dadg .Discard (4);return MakeBool (true ),nil ;};_aaec ,_acg =_fgde ._dadg .Peek (5);if _acg !=nil {return MakeBool (false ),_acg ;};if (len (_aaec )>=5)&&(string (_aaec [:5])=="\u0066\u0061\u006cs\u0065"){_fgde ._dadg .Discard (5);return MakeBool (false ),nil ;};return MakeBool (false ),_e .New ("\u0075n\u0065\u0078\u0070\u0065c\u0074\u0065\u0064\u0020\u0062o\u006fl\u0065a\u006e\u0020\u0073\u0074\u0072\u0069\u006eg");};func (_cef *PSOperand )bitshift (_egb *PSStack )error {_efe ,_fbd :=_egb .PopInteger ();if _fbd !=nil {return _fbd ;};_fdb ,_fbd :=_egb .PopInteger ();if _fbd !=nil {return _fbd ;};var _gdcf int ;if _efe >=0{_gdcf =_fdb <<uint (_efe );}else {_gdcf =_fdb >>uint (-_efe );};_fbd =_egb .Push (MakeInteger (_gdcf ));return _fbd ;};func (_bdb *PSOperand )gt (_aca *PSStack )error {_cfbd ,_dfa :=_aca .PopNumberAsFloat64 ();if _dfa !=nil {return _dfa ;};_cgc ,_dfa :=_aca .PopNumberAsFloat64 ();if _dfa !=nil {return _dfa ;};if _a .Abs (_cgc -_cfbd )< _g {_ebff :=_aca .Push (MakeBool (false ));return _ebff ;}else if _cgc > _cfbd {_eac :=_aca .Push (MakeBool (true ));return _eac ;}else {_fgdc :=_aca .Push (MakeBool (false ));return _fgdc ;};};func (_eeea *PSProgram )Duplicate ()PSObject {_gec :=&PSProgram {};for _ ,_cdc :=range *_eeea {_gec .Append (_cdc .Duplicate ());};return _gec ;};
|
||||
|
||||
// DebugString returns a descriptive string representation of the stack - intended for debugging.
|
||||
func (_ffa *PSStack )DebugString ()string {_cag :="\u005b\u0020";for _ ,_bcafb :=range *_ffa {_cag +=_bcafb .DebugString ();_cag +="\u0020";};_cag +="\u005d";return _cag ;};
|
||||
// PopNumberAsFloat64 pops and return the numeric value of the top of the stack as a float64.
|
||||
// Real or integer only.
|
||||
func (_bcf *PSStack )PopNumberAsFloat64 ()(float64 ,error ){_abed ,_ggaa :=_bcf .Pop ();if _ggaa !=nil {return 0,_ggaa ;};if _age ,_cacf :=_abed .(*PSReal );_cacf {return _age .Val ,nil ;}else if _baba ,_gegb :=_abed .(*PSInteger );_gegb {return float64 (_baba .Val ),nil ;}else {return 0,ErrTypeCheck ;};};func (_cdae *PSOperand )mul (_bfb *PSStack )error {_bcee ,_ebe :=_bfb .Pop ();if _ebe !=nil {return _ebe ;};_gdfb ,_ebe :=_bfb .Pop ();if _ebe !=nil {return _ebe ;};_gfe ,_aga :=_bcee .(*PSReal );_dffg ,_fgeb :=_bcee .(*PSInteger );if !_aga &&!_fgeb {return ErrTypeCheck ;};_aed ,_gcbfc :=_gdfb .(*PSReal );_addg ,_eaf :=_gdfb .(*PSInteger );if !_gcbfc &&!_eaf {return ErrTypeCheck ;};if _fgeb &&_eaf {_babe :=_dffg .Val *_addg .Val ;_agag :=_bfb .Push (MakeInteger (_babe ));return _agag ;};var _cfag float64 =0;if _aga {_cfag =_gfe .Val ;}else {_cfag =float64 (_dffg .Val );};if _gcbfc {_cfag *=_aed .Val ;}else {_cfag *=float64 (_addg .Val );};_ebe =_bfb .Push (MakeReal (_cfag ));return _ebe ;};
|
||||
|
||||
// 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 _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");
|
||||
// Exec executes the program, typically leaving output values on the stack.
|
||||
func (_cbf *PSProgram )Exec (stack *PSStack )error {for _ ,_fde :=range *_cbf {var _cgd error ;switch _ec :=_fde .(type ){case *PSInteger :_dgb :=_ec ;_cgd =stack .Push (_dgb );case *PSReal :_gdc :=_ec ;_cgd =stack .Push (_gdc );case *PSBoolean :_ecd :=_ec ;_cgd =stack .Push (_ecd );case *PSProgram :_ef :=_ec ;_cgd =stack .Push (_ef );case *PSOperand :_cf :=_ec ;_cgd =_cf .Exec (stack );default:return ErrTypeCheck ;};if _cgd !=nil {return _cgd ;};};return nil ;};func (_bga *PSOperand )cvi (_eca *PSStack )error {_fcg ,_beb :=_eca .Pop ();if _beb !=nil {return _beb ;};if _gbb ,_dgbc :=_fcg .(*PSReal );_dgbc {_agf :=int (_gbb .Val );_beb =_eca .Push (MakeInteger (_agf ));}else if _dgec ,_ade :=_fcg .(*PSInteger );_ade {_bcg :=_dgec .Val ;_beb =_eca .Push (MakeInteger (_bcg ));}else {return ErrTypeCheck ;};return _beb ;};func (_dac *PSOperand )ne (_dbb *PSStack )error {_agda :=_dac .eq (_dbb );if _agda !=nil {return _agda ;};_agda =_dac .not (_dbb );return _agda ;};
|
||||
|
||||
// String returns a string representation of the stack.
|
||||
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 ;};
|
||||
// PSParser is a basic Postscript parser.
|
||||
type PSParser struct{_dadg *_c .Reader };var ErrRangeCheck =_e .New ("\u0072\u0061\u006e\u0067\u0065\u0020\u0063\u0068\u0065\u0063\u006b\u0020e\u0072\u0072\u006f\u0072");func (_gdfa *PSOperand )ifelse (_cae *PSStack )error {_fgaf ,_cbb :=_cae .Pop ();if _cbb !=nil {return _cbb ;};_ccd ,_cbb :=_cae .Pop ();if _cbb !=nil {return _cbb ;};_gda ,_cbb :=_cae .Pop ();if _cbb !=nil {return _cbb ;};_ffeb ,_fgfg :=_fgaf .(*PSProgram );if !_fgfg {return ErrTypeCheck ;};_fgfb ,_fgfg :=_ccd .(*PSProgram );if !_fgfg {return ErrTypeCheck ;};_bbe ,_fgfg :=_gda .(*PSBoolean );if !_fgfg {return ErrTypeCheck ;};if _bbe .Val {_ada :=_fgfb .Exec (_cae );return _ada ;};_cbb =_ffeb .Exec (_cae );return _cbb ;};
|
||||
|
||||
// MakeReal returns a new PSReal object initialized with `val`.
|
||||
func MakeReal (val float64 )*PSReal {_fbac :=PSReal {};_fbac .Val =val ;return &_fbac };
|
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