1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
//
// Copyright 2020 FoxyUtils ehf. All rights reserved.
//
// This is a commercial product and requires a license to operate.
// A trial license can be obtained at https://unidoc.io
//
// DO NOT EDIT: generated by unitwist Go source code obfuscator.
//
// Use of this source code is governed by the UniDoc End User License Agreement
// terms that can be accessed at https://unidoc.io/eula/
// Package ps implements various functionalities needed for handling Postscript for PDF uses, in particular
// for PDF function type 4.
//
// Package ps implements various functionalities needed for handling Postscript for PDF uses, in particular
// for PDF function type 4.
package ps ;import (_ef "bufio";_f "bytes";_c "errors";_ccb "fmt";_fc "github.com/unidoc/unipdf/v4/common";_ec "github.com/unidoc/unipdf/v4/core";_e "io";_cc "math";);
// Exec executes the program, typically leaving output values on the stack.
func (_bd *PSProgram )Exec (stack *PSStack )error {for _ ,_ac :=range *_bd {var _gb error ;switch _gfe :=_ac .(type ){case *PSInteger :_bec :=_gfe ;_gb =stack .Push (_bec );case *PSReal :_ba :=_gfe ;_gb =stack .Push (_ba );case *PSBoolean :_ab :=_gfe ;
_gb =stack .Push (_ab );case *PSProgram :_fe :=_gfe ;_gb =stack .Push (_fe );case *PSOperand :_dcd :=_gfe ;_gb =_dcd .Exec (stack );default:return ErrTypeCheck ;};if _gb !=nil {return _gb ;};};return nil ;};
// NewPSExecutor returns an initialized PSExecutor for an input `program`.
func NewPSExecutor (program *PSProgram )*PSExecutor {_g :=&PSExecutor {};_g .Stack =NewPSStack ();_g ._fd =program ;return _g ;};func (_aefd *PSParser )skipComments ()error {if _ ,_fcae :=_aefd .skipSpaces ();_fcae !=nil {return _fcae ;};_eaa :=true ;for {_dgg ,_aea :=_aefd ._cgea .Peek (1);
if _aea !=nil {_fc .Log .Debug ("\u0045\u0072\u0072\u006f\u0072\u0020\u0025\u0073",_aea .Error ());return _aea ;};if _eaa &&_dgg [0]!='%'{return nil ;};_eaa =false ;if (_dgg [0]!='\r')&&(_dgg [0]!='\n'){_aefd ._cgea .ReadByte ();}else {break ;};};return _aefd .skipComments ();
};func (_ced *PSOperand )lt (_dbf *PSStack )error {_cacb ,_effc :=_dbf .PopNumberAsFloat64 ();if _effc !=nil {return _effc ;};_ea ,_effc :=_dbf .PopNumberAsFloat64 ();if _effc !=nil {return _effc ;};if _cc .Abs (_ea -_cacb )< _cd {_dcac :=_dbf .Push (MakeBool (false ));
return _dcac ;}else if _ea < _cacb {_dea :=_dbf .Push (MakeBool (true ));return _dea ;}else {_fcd :=_dbf .Push (MakeBool (false ));return _fcd ;};};func (_febe *PSOperand )idiv (_aad *PSStack )error {_beaf ,_gef :=_aad .Pop ();if _gef !=nil {return _gef ;
};_gefb ,_gef :=_aad .Pop ();if _gef !=nil {return _gef ;};_bbbb ,_fea :=_beaf .(*PSInteger );if !_fea {return ErrTypeCheck ;};if _bbbb .Val ==0{return ErrUndefinedResult ;};_cda ,_fea :=_gefb .(*PSInteger );if !_fea {return ErrTypeCheck ;};_dab :=_cda .Val /_bbbb .Val ;
_gef =_aad .Push (MakeInteger (_dab ));return _gef ;};
// Execute executes the program for an input parameters `objects` and returns a slice of output objects.
func (_dde *PSExecutor )Execute (objects []PSObject )([]PSObject ,error ){for _ ,_fa :=range objects {_aa :=_dde .Stack .Push (_fa );if _aa !=nil {return nil ,_aa ;};};_b :=_dde ._fd .Exec (_dde .Stack );if _b !=nil {_fc .Log .Debug ("\u0045x\u0065c\u0020\u0066\u0061\u0069\u006c\u0065\u0064\u003a\u0020\u0025\u0076",_b );
return nil ,_b ;};_gd :=[]PSObject (*_dde .Stack );_dde .Stack .Empty ();return _gd ,nil ;};const _cd =0.000001;func (_df *PSProgram )Duplicate ()PSObject {_gffe :=&PSProgram {};for _ ,_ebg :=range *_df {_gffe .Append (_ebg .Duplicate ());};return _gffe ;
};
// Empty empties the stack.
func (_gbfb *PSStack )Empty (){*_gbfb =[]PSObject {}};func (_bgd *PSOperand )copy (_gcd *PSStack )error {_eg ,_cba :=_gcd .PopInteger ();if _cba !=nil {return _cba ;};if _eg < 0{return ErrRangeCheck ;};if _eg > len (*_gcd ){return ErrRangeCheck ;};*_gcd =append (*_gcd ,(*_gcd )[len (*_gcd )-_eg :]...);
return nil ;};func (_bae *PSOperand )cvi (_cde *PSStack )error {_adbb ,_ceg :=_cde .Pop ();if _ceg !=nil {return _ceg ;};if _gbf ,_ada :=_adbb .(*PSReal );_ada {_cfb :=int (_gbf .Val );_ceg =_cde .Push (MakeInteger (_cfb ));}else if _cea ,_cbf :=_adbb .(*PSInteger );
_cbf {_bfe :=_cea .Val ;_ceg =_cde .Push (MakeInteger (_bfe ));}else {return ErrTypeCheck ;};return _ceg ;};
// Parse parses the postscript and store as a program that can be executed.
func (_gebb *PSParser )Parse ()(*PSProgram ,error ){_gebb .skipSpaces ();_edbb ,_ebcf :=_gebb ._cgea .Peek (2);if _ebcf !=nil {return nil ,_ebcf ;};if _edbb [0]!='{'{return nil ,_c .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");
};_ceae ,_ebcf :=_gebb .parseFunction ();if _ebcf !=nil &&_ebcf !=_e .EOF {return nil ,_ebcf ;};return _ceae ,_ebcf ;};func (_cb *PSReal )DebugString ()string {return _ccb .Sprintf ("\u0072e\u0061\u006c\u003a\u0025\u002e\u0035f",_cb .Val );};var ErrTypeCheck =_c .New ("\u0074\u0079p\u0065\u0020\u0063h\u0065\u0063\u006b\u0020\u0065\u0072\u0072\u006f\u0072");
func (_bdb *PSOperand )add (_af *PSStack )error {_ca ,_ddd :=_af .Pop ();if _ddd !=nil {return _ddd ;};_cbbe ,_ddd :=_af .Pop ();if _ddd !=nil {return _ddd ;};_eff ,_cdf :=_ca .(*PSReal );_cgg ,_cbc :=_ca .(*PSInteger );if !_cdf &&!_cbc {return ErrTypeCheck ;
};_cga ,_abc :=_cbbe .(*PSReal );_efa ,_ga :=_cbbe .(*PSInteger );if !_abc &&!_ga {return ErrTypeCheck ;};if _cbc &&_ga {_ebba :=_cgg .Val +_efa .Val ;_geg :=_af .Push (MakeInteger (_ebba ));return _geg ;};var _gcf float64 ;if _cdf {_gcf =_eff .Val ;}else {_gcf =float64 (_cgg .Val );
};if _abc {_gcf +=_cga .Val ;}else {_gcf +=float64 (_efa .Val );};_ddd =_af .Push (MakeReal (_gcf ));return _ddd ;};func (_baf *PSOperand )and (_fbf *PSStack )error {_gca ,_caa :=_fbf .Pop ();if _caa !=nil {return _caa ;};_aca ,_caa :=_fbf .Pop ();if _caa !=nil {return _caa ;
};if _efe ,_gec :=_gca .(*PSBoolean );_gec {_acc ,_dac :=_aca .(*PSBoolean );if !_dac {return ErrTypeCheck ;};_caa =_fbf .Push (MakeBool (_efe .Val &&_acc .Val ));return _caa ;};if _eea ,_ed :=_gca .(*PSInteger );_ed {_bf ,_gba :=_aca .(*PSInteger );if !_gba {return ErrTypeCheck ;
};_caa =_fbf .Push (MakeInteger (_eea .Val &_bf .Val ));return _caa ;};return ErrTypeCheck ;};func (_ag *PSBoolean )String ()string {return _ccb .Sprintf ("\u0025\u0076",_ag .Val )};func (_gab *PSOperand )mul (_febd *PSStack )error {_aecc ,_bge :=_febd .Pop ();
if _bge !=nil {return _bge ;};_gafa ,_bge :=_febd .Pop ();if _bge !=nil {return _bge ;};_ccf ,_deg :=_aecc .(*PSReal );_fgb ,_acg :=_aecc .(*PSInteger );if !_deg &&!_acg {return ErrTypeCheck ;};_bfb ,_fef :=_gafa .(*PSReal );_ecc ,_gdeg :=_gafa .(*PSInteger );
if !_fef &&!_gdeg {return ErrTypeCheck ;};if _acg &&_gdeg {_ebe :=_fgb .Val *_ecc .Val ;_bdbf :=_febd .Push (MakeInteger (_ebe ));return _bdbf ;};var _cbga float64 ;if _deg {_cbga =_ccf .Val ;}else {_cbga =float64 (_fgb .Val );};if _fef {_cbga *=_bfb .Val ;
}else {_cbga *=float64 (_ecc .Val );};_bge =_febd .Push (MakeReal (_cbga ));return _bge ;};func (_gc *PSProgram )String ()string {_dca :="\u007b\u0020";for _ ,_gdb :=range *_gc {_dca +=_gdb .String ();_dca +="\u0020";};_dca +="\u007d";return _dca ;};func (_bb *PSOperand )abs (_abe *PSStack )error {_ebb ,_ff :=_abe .Pop ();
if _ff !=nil {return _ff ;};if _dfc ,_da :=_ebb .(*PSReal );_da {_aab :=_dfc .Val ;if _aab < 0{_ff =_abe .Push (MakeReal (-_aab ));}else {_ff =_abe .Push (MakeReal (_aab ));};}else if _ebbc ,_fedc :=_ebb .(*PSInteger );_fedc {_agf :=_ebbc .Val ;if _agf < 0{_ff =_abe .Push (MakeInteger (-_agf ));
}else {_ff =_abe .Push (MakeInteger (_agf ));};}else {return ErrTypeCheck ;};return _ff ;};func (_aaff *PSOperand )exp (_egf *PSStack )error {_aabb ,_ebf :=_egf .PopNumberAsFloat64 ();if _ebf !=nil {return _ebf ;};_edb ,_ebf :=_egf .PopNumberAsFloat64 ();
if _ebf !=nil {return _ebf ;};if _cc .Abs (_aabb )< 1&&_edb < 0{return ErrUndefinedResult ;};_cac :=_cc .Pow (_edb ,_aabb );_ebf =_egf .Push (MakeReal (_cac ));return _ebf ;};func _bdaf (_ead int )int {if _ead < 0{return -_ead ;};return _ead ;};func (_be *PSReal )String ()string {return _ccb .Sprintf ("\u0025\u002e\u0035\u0066",_be .Val )};
func (_cce *PSOperand )div (_daa *PSStack )error {_gdee ,_fec :=_daa .Pop ();if _fec !=nil {return _fec ;};_dddg ,_fec :=_daa .Pop ();if _fec !=nil {return _fec ;};_cfc ,_fgc :=_gdee .(*PSReal );_gdf ,_ccg :=_gdee .(*PSInteger );if !_fgc &&!_ccg {return ErrTypeCheck ;
};if _fgc &&_cfc .Val ==0{return ErrUndefinedResult ;};if _ccg &&_gdf .Val ==0{return ErrUndefinedResult ;};_adbf ,_ccd :=_dddg .(*PSReal );_adf ,_ebd :=_dddg .(*PSInteger );if !_ccd &&!_ebd {return ErrTypeCheck ;};var _ae float64 ;if _ccd {_ae =_adbf .Val ;
}else {_ae =float64 (_adf .Val );};if _fgc {_ae /=_cfc .Val ;}else {_ae /=float64 (_gdf .Val );};_fec =_daa .Push (MakeReal (_ae ));return _fec ;};func (_fgd *PSOperand )dup (_fde *PSStack )error {_aef ,_cfbg :=_fde .Pop ();if _cfbg !=nil {return _cfbg ;
};_cfbg =_fde .Push (_aef );if _cfbg !=nil {return _cfbg ;};_cfbg =_fde .Push (_aef .Duplicate ());return _cfbg ;};func (_gag *PSOperand )ge (_cfe *PSStack )error {_bcec ,_bfdca :=_cfe .PopNumberAsFloat64 ();if _bfdca !=nil {return _bfdca ;};_gae ,_bfdca :=_cfe .PopNumberAsFloat64 ();
if _bfdca !=nil {return _bfdca ;};if _cc .Abs (_gae -_bcec )< _cd {_gcgc :=_cfe .Push (MakeBool (true ));return _gcgc ;}else if _gae > _bcec {_fbda :=_cfe .Push (MakeBool (true ));return _fbda ;}else {_bfa :=_cfe .Push (MakeBool (false ));return _bfa ;
};};func (_bg *PSBoolean )DebugString ()string {return _ccb .Sprintf ("\u0062o\u006f\u006c\u003a\u0025\u0076",_bg .Val );};func (_eegd *PSOperand )sub (_gbgf *PSStack )error {_cge ,_ace :=_gbgf .Pop ();if _ace !=nil {return _ace ;};_ddbd ,_ace :=_gbgf .Pop ();
if _ace !=nil {return _ace ;};_fbag ,_fbgf :=_cge .(*PSReal );_dffa ,_ccc :=_cge .(*PSInteger );if !_fbgf &&!_ccc {return ErrTypeCheck ;};_dafe ,_cgge :=_ddbd .(*PSReal );_cdc ,_bcgg :=_ddbd .(*PSInteger );if !_cgge &&!_bcgg {return ErrTypeCheck ;};if _ccc &&_bcgg {_ggf :=_cdc .Val -_dffa .Val ;
_effg :=_gbgf .Push (MakeInteger (_ggf ));return _effg ;};var _agfd float64 =0;if _cgge {_agfd =_dafe .Val ;}else {_agfd =float64 (_cdc .Val );};if _fbgf {_agfd -=_fbag .Val ;}else {_agfd -=float64 (_dffa .Val );};_ace =_gbgf .Push (MakeReal (_agfd ));
return _ace ;};func (_fb *PSInteger )String ()string {return _ccb .Sprintf ("\u0025\u0064",_fb .Val )};
// PSExecutor has its own execution stack and is used to executre a PS routine (program).
type PSExecutor struct{Stack *PSStack ;_fd *PSProgram ;};func (_fbgc *PSParser )parseBool ()(*PSBoolean ,error ){_fgee ,_ccbf :=_fbgc ._cgea .Peek (4);if _ccbf !=nil {return MakeBool (false ),_ccbf ;};if (len (_fgee )>=4)&&(string (_fgee [:4])=="\u0074\u0072\u0075\u0065"){_fbgc ._cgea .Discard (4);
return MakeBool (true ),nil ;};_fgee ,_ccbf =_fbgc ._cgea .Peek (5);if _ccbf !=nil {return MakeBool (false ),_ccbf ;};if (len (_fgee )>=5)&&(string (_fgee [:5])=="\u0066\u0061\u006cs\u0065"){_fbgc ._cgea .Discard (5);return MakeBool (false ),nil ;};return MakeBool (false ),_c .New ("\u0075n\u0065\u0078\u0070\u0065c\u0074\u0065\u0064\u0020\u0062o\u006fl\u0065a\u006e\u0020\u0073\u0074\u0072\u0069\u006eg");
};
// PSReal represents a real number.
type PSReal struct{Val float64 ;};func (_gee *PSOperand )ceiling (_aabg *PSStack )error {_bfdc ,_fcf :=_aabg .Pop ();if _fcf !=nil {return _fcf ;};if _ebc ,_adb :=_bfdc .(*PSReal );_adb {_fcf =_aabg .Push (MakeReal (_cc .Ceil (_ebc .Val )));}else if _bc ,_dbc :=_bfdc .(*PSInteger );
_dbc {_fcf =_aabg .Push (MakeInteger (_bc .Val ));}else {_fcf =ErrTypeCheck ;};return _fcf ;};
// PSProgram defines a Postscript program which is a series of PS objects (arguments, commands, programs etc).
type PSProgram []PSObject ;func (_fed *PSOperand )Duplicate ()PSObject {_cgf :=*_fed ;return &_cgf };func (_gbc *PSOperand )exch (_cdg *PSStack )error {_edc ,_age :=_cdg .Pop ();if _age !=nil {return _age ;};_gbd ,_age :=_cdg .Pop ();if _age !=nil {return _age ;
};_age =_cdg .Push (_edc );if _age !=nil {return _age ;};_age =_cdg .Push (_gbd );return _age ;};var ErrStackUnderflow =_c .New ("\u0073t\u0061c\u006b\u0020\u0075\u006e\u0064\u0065\u0072\u0066\u006c\u006f\u0077");var ErrUndefinedResult =_c .New ("\u0075\u006e\u0064\u0065fi\u006e\u0065\u0064\u0020\u0072\u0065\u0073\u0075\u006c\u0074\u0020\u0065\u0072\u0072o\u0072");
func (_gbg *PSOperand )or (_dbca *PSStack )error {_fcca ,_acff :=_dbca .Pop ();if _acff !=nil {return _acff ;};_cdde ,_acff :=_dbca .Pop ();if _acff !=nil {return _acff ;};if _adfc ,_efeb :=_fcca .(*PSBoolean );_efeb {_bcc ,_abec :=_cdde .(*PSBoolean );
if !_abec {return ErrTypeCheck ;};_acff =_dbca .Push (MakeBool (_adfc .Val ||_bcc .Val ));return _acff ;};if _cdb ,_faa :=_fcca .(*PSInteger );_faa {_ccgc ,_bdc :=_cdde .(*PSInteger );if !_bdc {return ErrTypeCheck ;};_acff =_dbca .Push (MakeInteger (_cdb .Val |_ccgc .Val ));
return _acff ;};return ErrTypeCheck ;};
// NewPSStack returns an initialized PSStack.
func NewPSStack ()*PSStack {return &PSStack {}};func (_bcge *PSOperand )xor (_dge *PSStack )error {_eccg ,_bbed :=_dge .Pop ();if _bbed !=nil {return _bbed ;};_adae ,_bbed :=_dge .Pop ();if _bbed !=nil {return _bbed ;};if _bad ,_caf :=_eccg .(*PSBoolean );
_caf {_daec ,_ebfb :=_adae .(*PSBoolean );if !_ebfb {return ErrTypeCheck ;};_bbed =_dge .Push (MakeBool (_bad .Val !=_daec .Val ));return _bbed ;};if _fefb ,_gbea :=_eccg .(*PSInteger );_gbea {_ccbe ,_bgf :=_adae .(*PSInteger );if !_bgf {return ErrTypeCheck ;
};_bbed =_dge .Push (MakeInteger (_fefb .Val ^_ccbe .Val ));return _bbed ;};return ErrTypeCheck ;};func (_bbf *PSOperand )gt (_eee *PSStack )error {_cbbc ,_gcff :=_eee .PopNumberAsFloat64 ();if _gcff !=nil {return _gcff ;};_fee ,_gcff :=_eee .PopNumberAsFloat64 ();
if _gcff !=nil {return _gcff ;};if _cc .Abs (_fee -_cbbc )< _cd {_afa :=_eee .Push (MakeBool (false ));return _afa ;}else if _fee > _cbbc {_egd :=_eee .Push (MakeBool (true ));return _egd ;}else {_dcag :=_eee .Push (MakeBool (false ));return _dcag ;};};
func (_gac *PSOperand )ifelse (_bfc *PSStack )error {_bbe ,_cbfb :=_bfc .Pop ();if _cbfb !=nil {return _cbfb ;};_gea ,_cbfb :=_bfc .Pop ();if _cbfb !=nil {return _cbfb ;};_cdaa ,_cbfb :=_bfc .Pop ();if _cbfb !=nil {return _cbfb ;};_dfa ,_bcg :=_bbe .(*PSProgram );
if !_bcg {return ErrTypeCheck ;};_bee ,_bcg :=_gea .(*PSProgram );if !_bcg {return ErrTypeCheck ;};_fff ,_bcg :=_cdaa .(*PSBoolean );if !_bcg {return ErrTypeCheck ;};if _fff .Val {_egfb :=_bee .Exec (_bfc );return _egfb ;};_cbfb =_dfa .Exec (_bfc );return _cbfb ;
};func (_db *PSOperand )DebugString ()string {return _ccb .Sprintf ("\u006fp\u003a\u0027\u0025\u0073\u0027",*_db );};
// PSStack defines a stack of PSObjects. PSObjects can be pushed on or pull from the stack.
type PSStack []PSObject ;
// PopInteger specificially pops an integer from the top of the stack, returning the value as an int.
func (_ggb *PSStack )PopInteger ()(int ,error ){_eaagc ,_fga :=_ggb .Pop ();if _fga !=nil {return 0,_fga ;};if _ceda ,_feea :=_eaagc .(*PSInteger );_feea {return _ceda .Val ,nil ;};return 0,ErrTypeCheck ;};func (_gfbc *PSOperand )round (_cgdg *PSStack )error {_ebad ,_dcg :=_cgdg .Pop ();
if _dcg !=nil {return _dcg ;};if _dad ,_ebaa :=_ebad .(*PSReal );_ebaa {_dcg =_cgdg .Push (MakeReal (_cc .Floor (_dad .Val +0.5)));}else if _fcgf ,_bdd :=_ebad .(*PSInteger );_bdd {_dcg =_cgdg .Push (MakeInteger (_fcgf .Val ));}else {return ErrTypeCheck ;
};return _dcg ;};
// MakeReal returns a new PSReal object initialized with `val`.
func MakeReal (val float64 )*PSReal {_cgbf :=PSReal {};_cgbf .Val =val ;return &_cgbf };func (_cbag *PSOperand )index (_eba *PSStack )error {_dbg ,_gfcd :=_eba .Pop ();if _gfcd !=nil {return _gfcd ;};_dacc ,_bac :=_dbg .(*PSInteger );if !_bac {return ErrTypeCheck ;
};if _dacc .Val < 0{return ErrRangeCheck ;};if _dacc .Val > len (*_eba )-1{return ErrStackUnderflow ;};_abea :=(*_eba )[len (*_eba )-1-_dacc .Val ];_gfcd =_eba .Push (_abea .Duplicate ());return _gfcd ;};func (_gbag *PSOperand )log (_aeb *PSStack )error {_dba ,_dfg :=_aeb .PopNumberAsFloat64 ();
if _dfg !=nil {return _dfg ;};_bff :=_cc .Log10 (_dba );_dfg =_aeb .Push (MakeReal (_bff ));return _dfg ;};
// PSOperand represents a Postscript operand (text string).
type PSOperand string ;
// PSObject represents a postscript object.
type PSObject interface{
// Duplicate makes a fresh copy of the PSObject.
Duplicate ()PSObject ;
// DebugString returns a descriptive representation of the PSObject with more information than String()
// for debugging purposes.
DebugString ()string ;
// String returns a string representation of the PSObject.
String ()string ;};
// PopNumberAsFloat64 pops and return the numeric value of the top of the stack as a float64.
// Real or integer only.
func (_bbc *PSStack )PopNumberAsFloat64 ()(float64 ,error ){_egdd ,_gada :=_bbc .Pop ();if _gada !=nil {return 0,_gada ;};if _afca ,_afe :=_egdd .(*PSReal );_afe {return _afca .Val ,nil ;}else if _cbcf ,_aebd :=_egdd .(*PSInteger );_aebd {return float64 (_cbcf .Val ),nil ;
}else {return 0,ErrTypeCheck ;};};
// Push pushes an object on top of the stack.
func (_gfcdg *PSStack )Push (obj PSObject )error {if len (*_gfcdg )> 100{return ErrStackOverflow ;};*_gfcdg =append (*_gfcdg ,obj );return nil ;};func (_baee *PSOperand )eq (_bga *PSStack )error {_bce ,_fgf :=_bga .Pop ();if _fgf !=nil {return _fgf ;};
_cbg ,_fgf :=_bga .Pop ();if _fgf !=nil {return _fgf ;};_ggg ,_aafa :=_bce .(*PSBoolean );_gfbb ,_aac :=_cbg .(*PSBoolean );if _aafa ||_aac {var _daf error ;if _aafa &&_aac {_daf =_bga .Push (MakeBool (_ggg .Val ==_gfbb .Val ));}else {_daf =_bga .Push (MakeBool (false ));
};return _daf ;};var _fafd float64 ;var _gaf float64 ;if _bea ,_feg :=_bce .(*PSInteger );_feg {_fafd =float64 (_bea .Val );}else if _cdd ,_dae :=_bce .(*PSReal );_dae {_fafd =_cdd .Val ;}else {return ErrTypeCheck ;};if _daaa ,_gad :=_cbg .(*PSInteger );
_gad {_gaf =float64 (_daaa .Val );}else if _fca ,_ccad :=_cbg .(*PSReal );_ccad {_gaf =_fca .Val ;}else {return ErrTypeCheck ;};if _cc .Abs (_gaf -_fafd )< _cd {_fgf =_bga .Push (MakeBool (true ));}else {_fgf =_bga .Push (MakeBool (false ));};return _fgf ;
};
// Append appends an object to the PSProgram.
func (_ge *PSProgram )Append (obj PSObject ){*_ge =append (*_ge ,obj )};
// 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 _cf []float64 ;for _ ,_d :=range objects {if _dc ,_dg :=_d .(*PSInteger );_dg {_cf =append (_cf ,float64 (_dc .Val ));}else if _gge ,_dd :=_d .(*PSReal );_dd {_cf =append (_cf ,_gge .Val );
}else {return nil ,ErrTypeCheck ;};};return _cf ,nil ;};func (_eb *PSInteger )DebugString ()string {return _ccb .Sprintf ("\u0069\u006e\u0074\u003a\u0025\u0064",_eb .Val );};func (_fdg *PSOperand )pop (_dbac *PSStack )error {_ ,_dacd :=_dbac .Pop ();if _dacd !=nil {return _dacd ;
};return nil ;};func (_cg *PSProgram )DebugString ()string {_gff :="\u007b\u0020";for _ ,_cbbd :=range *_cg {_gff +=_cbbd .DebugString ();_gff +="\u0020";};_gff +="\u007d";return _gff ;};func (_fdd *PSOperand )floor (_adbe *PSStack )error {_bbb ,_ggd :=_adbe .Pop ();
if _ggd !=nil {return _ggd ;};if _baef ,_fbd :=_bbb .(*PSReal );_fbd {_ggd =_adbe .Push (MakeReal (_cc .Floor (_baef .Val )));}else if _ecfd ,_ecg :=_bbb .(*PSInteger );_ecg {_ggd =_adbe .Push (MakeInteger (_ecfd .Val ));}else {return ErrTypeCheck ;};return _ggd ;
};func (_gfbe *PSOperand )sin (_fgfg *PSStack )error {_ade ,_geae :=_fgfg .PopNumberAsFloat64 ();if _geae !=nil {return _geae ;};_faae :=_cc .Sin (_ade *_cc .Pi /180.0);_geae =_fgfg .Push (MakeReal (_faae ));return _geae ;};var ErrUnsupportedOperand =_c .New ("\u0075\u006e\u0073\u0075pp\u006f\u0072\u0074\u0065\u0064\u0020\u006f\u0070\u0065\u0072\u0061\u006e\u0064");
func (_dcc *PSParser )parseNumber ()(PSObject ,error ){_cgaf ,_gfeb :=_ec .ParseNumber (_dcc ._cgea );if _gfeb !=nil {return nil ,_gfeb ;};switch _gacg :=_cgaf .(type ){case *_ec .PdfObjectFloat :return MakeReal (float64 (*_gacg )),nil ;case *_ec .PdfObjectInteger :return MakeInteger (int (*_gacg )),nil ;
};return nil ,_ccb .Errorf ("\u0075n\u0068\u0061\u006e\u0064\u006c\u0065\u0064\u0020\u006e\u0075\u006db\u0065\u0072\u0020\u0074\u0079\u0070\u0065\u0020\u0025\u0054",_cgaf );};
// Exec executes the operand `op` in the state specified by `stack`.
func (_bda *PSOperand )Exec (stack *PSStack )error {_eeb :=ErrUnsupportedOperand ;switch *_bda {case "\u0061\u0062\u0073":_eeb =_bda .abs (stack );case "\u0061\u0064\u0064":_eeb =_bda .add (stack );case "\u0061\u006e\u0064":_eeb =_bda .and (stack );case "\u0061\u0074\u0061\u006e":_eeb =_bda .atan (stack );
case "\u0062\u0069\u0074\u0073\u0068\u0069\u0066\u0074":_eeb =_bda .bitshift (stack );case "\u0063e\u0069\u006c\u0069\u006e\u0067":_eeb =_bda .ceiling (stack );case "\u0063\u006f\u0070\u0079":_eeb =_bda .copy (stack );case "\u0063\u006f\u0073":_eeb =_bda .cos (stack );
case "\u0063\u0076\u0069":_eeb =_bda .cvi (stack );case "\u0063\u0076\u0072":_eeb =_bda .cvr (stack );case "\u0064\u0069\u0076":_eeb =_bda .div (stack );case "\u0064\u0075\u0070":_eeb =_bda .dup (stack );case "\u0065\u0071":_eeb =_bda .eq (stack );case "\u0065\u0078\u0063\u0068":_eeb =_bda .exch (stack );
case "\u0065\u0078\u0070":_eeb =_bda .exp (stack );case "\u0066\u006c\u006fo\u0072":_eeb =_bda .floor (stack );case "\u0067\u0065":_eeb =_bda .ge (stack );case "\u0067\u0074":_eeb =_bda .gt (stack );case "\u0069\u0064\u0069\u0076":_eeb =_bda .idiv (stack );
case "\u0069\u0066":_eeb =_bda .ifCondition (stack );case "\u0069\u0066\u0065\u006c\u0073\u0065":_eeb =_bda .ifelse (stack );case "\u0069\u006e\u0064e\u0078":_eeb =_bda .index (stack );case "\u006c\u0065":_eeb =_bda .le (stack );case "\u006c\u006f\u0067":_eeb =_bda .log (stack );
case "\u006c\u006e":_eeb =_bda .ln (stack );case "\u006c\u0074":_eeb =_bda .lt (stack );case "\u006d\u006f\u0064":_eeb =_bda .mod (stack );case "\u006d\u0075\u006c":_eeb =_bda .mul (stack );case "\u006e\u0065":_eeb =_bda .ne (stack );case "\u006e\u0065\u0067":_eeb =_bda .neg (stack );
case "\u006e\u006f\u0074":_eeb =_bda .not (stack );case "\u006f\u0072":_eeb =_bda .or (stack );case "\u0070\u006f\u0070":_eeb =_bda .pop (stack );case "\u0072\u006f\u0075n\u0064":_eeb =_bda .round (stack );case "\u0072\u006f\u006c\u006c":_eeb =_bda .roll (stack );
case "\u0073\u0069\u006e":_eeb =_bda .sin (stack );case "\u0073\u0071\u0072\u0074":_eeb =_bda .sqrt (stack );case "\u0073\u0075\u0062":_eeb =_bda .sub (stack );case "\u0074\u0072\u0075\u006e\u0063\u0061\u0074\u0065":_eeb =_bda .truncate (stack );case "\u0078\u006f\u0072":_eeb =_bda .xor (stack );
};return _eeb ;};func (_ad *PSInteger )Duplicate ()PSObject {_gde :=PSInteger {};_gde .Val =_ad .Val ;return &_gde };var ErrRangeCheck =_c .New ("\u0072\u0061\u006e\u0067\u0065\u0020\u0063\u0068\u0065\u0063\u006b\u0020e\u0072\u0072\u006f\u0072");func (_gdc *PSOperand )ln (_cfga *PSStack )error {_ddeb ,_ebfe :=_cfga .PopNumberAsFloat64 ();
if _ebfe !=nil {return _ebfe ;};_ffg :=_cc .Log (_ddeb );_ebfe =_cfga .Push (MakeReal (_ffg ));return _ebfe ;};func (_cbea *PSParser )skipSpaces ()(int ,error ){_adbfb :=0;for {_dbe ,_ebbcb :=_cbea ._cgea .Peek (1);if _ebbcb !=nil {return 0,_ebbcb ;};if _ec .IsWhiteSpace (_dbe [0]){_cbea ._cgea .ReadByte ();
_adbfb ++;}else {break ;};};return _adbfb ,nil ;};
// PSInteger represents an integer.
type PSInteger struct{Val int ;};
// PSParser is a basic Postscript parser.
type PSParser struct{_cgea *_ef .Reader };var ErrStackOverflow =_c .New ("\u0073\u0074\u0061\u0063\u006b\u0020\u006f\u0076\u0065r\u0066\u006c\u006f\u0077");
// Pop pops an object from the top of the stack.
func (_eaag *PSStack )Pop ()(PSObject ,error ){if len (*_eaag )< 1{return nil ,ErrStackUnderflow ;};_acea :=(*_eaag )[len (*_eaag )-1];*_eaag =(*_eaag )[0:len (*_eaag )-1];return _acea ,nil ;};func (_efg *PSReal )Duplicate ()PSObject {_fcb :=PSReal {};
_fcb .Val =_efg .Val ;return &_fcb };func (_cfg *PSOperand )ifCondition (_eec *PSStack )error {_cdgg ,_afae :=_eec .Pop ();if _afae !=nil {return _afae ;};_gaa ,_afae :=_eec .Pop ();if _afae !=nil {return _afae ;};_ffc ,_gdea :=_cdgg .(*PSProgram );if !_gdea {return ErrTypeCheck ;
};_bbbc ,_gdea :=_gaa .(*PSBoolean );if !_gdea {return ErrTypeCheck ;};if _bbbc .Val {_afc :=_ffc .Exec (_eec );return _afc ;};return nil ;};func (_cgca *PSOperand )cvr (_gbe *PSStack )error {_gcg ,_de :=_gbe .Pop ();if _de !=nil {return _de ;};if _fba ,_babg :=_gcg .(*PSReal );
_babg {_de =_gbe .Push (MakeReal (_fba .Val ));}else if _abg ,_cca :=_gcg .(*PSInteger );_cca {_de =_gbe .Push (MakeReal (float64 (_abg .Val )));}else {return ErrTypeCheck ;};return _de ;};
// NewPSParser returns a new instance of the PDF Postscript parser from input data.
func NewPSParser (content []byte )*PSParser {_ccga :=PSParser {};_fcbf :=_f .NewBuffer (content );_ccga ._cgea =_ef .NewReader (_fcbf );return &_ccga ;};func (_ggc *PSOperand )String ()string {return string (*_ggc )};func (_gce *PSOperand )le (_gafe *PSStack )error {_gbfc ,_ded :=_gafe .PopNumberAsFloat64 ();
if _ded !=nil {return _ded ;};_aec ,_ded :=_gafe .PopNumberAsFloat64 ();if _ded !=nil {return _ded ;};if _cc .Abs (_aec -_gbfc )< _cd {_afg :=_gafe .Push (MakeBool (true ));return _afg ;}else if _aec < _gbfc {_dff :=_gafe .Push (MakeBool (true ));return _dff ;
}else {_fdc :=_gafe .Push (MakeBool (false ));return _fdc ;};};func (_aaf *PSOperand )atan (_cgc *PSStack )error {_bfd ,_fbg :=_cgc .PopNumberAsFloat64 ();if _fbg !=nil {return _fbg ;};_gfc ,_fbg :=_cgc .PopNumberAsFloat64 ();if _fbg !=nil {return _fbg ;
};if _bfd ==0{var _ffd error ;if _gfc < 0{_ffd =_cgc .Push (MakeReal (270));}else {_ffd =_cgc .Push (MakeReal (90));};return _ffd ;};_faf :=_gfc /_bfd ;_cgd :=_cc .Atan (_faf )*180/_cc .Pi ;_fbg =_cgc .Push (MakeReal (_cgd ));return _fbg ;};func (_fbfc *PSOperand )neg (_eeec *PSStack )error {_bcd ,_fffc :=_eeec .Pop ();
if _fffc !=nil {return _fffc ;};if _cddb ,_fgfe :=_bcd .(*PSReal );_fgfe {_fffc =_eeec .Push (MakeReal (-_cddb .Val ));return _fffc ;}else if _bbg ,_ddbc :=_bcd .(*PSInteger );_ddbc {_fffc =_eeec .Push (MakeInteger (-_bbg .Val ));return _fffc ;}else {return ErrTypeCheck ;
};};
// DebugString returns a descriptive string representation of the stack - intended for debugging.
func (_gdfg *PSStack )DebugString ()string {_ecb :="\u005b\u0020";for _ ,_aaa :=range *_gdfg {_ecb +=_aaa .DebugString ();_ecb +="\u0020";};_ecb +="\u005d";return _ecb ;};func (_fge *PSParser )parseFunction ()(*PSProgram ,error ){_gebg ,_ :=_fge ._cgea .ReadByte ();
if _gebg !='{'{return nil ,_c .New ("\u0069\u006ev\u0061\u006c\u0069d\u0020\u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e");};_add :=NewPSProgram ();for {_fge .skipSpaces ();_fge .skipComments ();_cgb ,_eagc :=_fge ._cgea .Peek (2);if _eagc !=nil {if _eagc ==_e .EOF {break ;
};return nil ,_eagc ;};_fc .Log .Trace ("\u0050e\u0065k\u0020\u0073\u0074\u0072\u0069\u006e\u0067\u003a\u0020\u0025\u0073",string (_cgb ));if _cgb [0]=='}'{_fc .Log .Trace ("\u0045\u004f\u0046 \u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e");_fge ._cgea .ReadByte ();
break ;}else if _cgb [0]=='{'{_fc .Log .Trace ("\u0046u\u006e\u0063\u0074\u0069\u006f\u006e!");_eca ,_cdcb :=_fge .parseFunction ();if _cdcb !=nil {return nil ,_cdcb ;};_add .Append (_eca );}else if _ec .IsDecimalDigit (_cgb [0])||(_cgb [0]=='-'&&_ec .IsDecimalDigit (_cgb [1])){_fc .Log .Trace ("\u002d>\u004e\u0075\u006d\u0062\u0065\u0072!");
_dddd ,_aged :=_fge .parseNumber ();if _aged !=nil {return nil ,_aged ;};_add .Append (_dddd );}else {_fc .Log .Trace ("\u002d>\u004fp\u0065\u0072\u0061\u006e\u0064 \u006f\u0072 \u0062\u006f\u006f\u006c\u003f");_cgb ,_ =_fge ._cgea .Peek (5);_dbad :=string (_cgb );
_fc .Log .Trace ("\u0050\u0065\u0065k\u0020\u0073\u0074\u0072\u003a\u0020\u0025\u0073",_dbad );if (len (_dbad )> 4)&&(_dbad [:5]=="\u0066\u0061\u006cs\u0065"){_dgc ,_bgdb :=_fge .parseBool ();if _bgdb !=nil {return nil ,_bgdb ;};_add .Append (_dgc );}else if (len (_dbad )> 3)&&(_dbad [:4]=="\u0074\u0072\u0075\u0065"){_dbcb ,_ebgb :=_fge .parseBool ();
if _ebgb !=nil {return nil ,_ebgb ;};_add .Append (_dbcb );}else {_bfff ,_fda :=_fge .parseOperand ();if _fda !=nil {return nil ,_fda ;};_add .Append (_bfff );};};};return _add ,nil ;};func (_fcg *PSOperand )mod (_gcfc *PSStack )error {_fbab ,_ddb :=_gcfc .Pop ();
if _ddb !=nil {return _ddb ;};_abbg ,_ddb :=_gcfc .Pop ();if _ddb !=nil {return _ddb ;};_fbc ,_aee :=_fbab .(*PSInteger );if !_aee {return ErrTypeCheck ;};if _fbc .Val ==0{return ErrUndefinedResult ;};_fccg ,_aee :=_abbg .(*PSInteger );if !_aee {return ErrTypeCheck ;
};_cag :=_fccg .Val %_fbc .Val ;_ddb =_gcfc .Push (MakeInteger (_cag ));return _ddb ;};func (_aebf *PSOperand )ne (_gcc *PSStack )error {_gacb :=_aebf .eq (_gcc );if _gacb !=nil {return _gacb ;};_gacb =_aebf .not (_gcc );return _gacb ;};
// MakeInteger returns a new PSInteger object initialized with `val`.
func MakeInteger (val int )*PSInteger {_gdbc :=PSInteger {};_gdbc .Val =val ;return &_gdbc };
// PSBoolean represents a boolean value.
type PSBoolean struct{Val bool ;};
// MakeBool returns a new PSBoolean object initialized with `val`.
func MakeBool (val bool )*PSBoolean {_adeg :=PSBoolean {};_adeg .Val =val ;return &_adeg };func (_fafe *PSOperand )cos (_feb *PSStack )error {_dcab ,_abb :=_feb .PopNumberAsFloat64 ();if _abb !=nil {return _abb ;};_bab :=_cc .Cos (_dcab *_cc .Pi /180.0);
_abb =_feb .Push (MakeReal (_bab ));return _abb ;};func (_efec *PSOperand )sqrt (_cacc *PSStack )error {_agg ,_def :=_cacc .PopNumberAsFloat64 ();if _def !=nil {return _def ;};if _agg < 0{return ErrRangeCheck ;};_gfa :=_cc .Sqrt (_agg );_def =_cacc .Push (MakeReal (_gfa ));
return _def ;};func (_gcaa *PSOperand )not (_becg *PSStack )error {_gagg ,_eeg :=_becg .Pop ();if _eeg !=nil {return _eeg ;};if _gdfe ,_abgf :=_gagg .(*PSBoolean );_abgf {_eeg =_becg .Push (MakeBool (!_gdfe .Val ));return _eeg ;}else if _cagd ,_cab :=_gagg .(*PSInteger );
_cab {_eeg =_becg .Push (MakeInteger (^_cagd .Val ));return _eeg ;}else {return ErrTypeCheck ;};};
// String returns a string representation of the stack.
func (_edd *PSStack )String ()string {_fbad :="\u005b\u0020";for _ ,_cae :=range *_edd {_fbad +=_cae .String ();_fbad +="\u0020";};_fbad +="\u005d";return _fbad ;};func (_ege *PSParser )parseOperand ()(*PSOperand ,error ){var _fab []byte ;for {_ffgg ,_ceb :=_ege ._cgea .Peek (1);
if _ceb !=nil {if _ceb ==_e .EOF {break ;};return nil ,_ceb ;};if _ec .IsDelimiter (_ffgg [0]){break ;};if _ec .IsWhiteSpace (_ffgg [0]){break ;};_gggf ,_ :=_ege ._cgea .ReadByte ();_fab =append (_fab ,_gggf );};if len (_fab )==0{return nil ,_c .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 (_fab )),nil ;};
// MakeOperand returns a new PSOperand object based on string `val`.
func MakeOperand (val string )*PSOperand {_gaag :=PSOperand (val );return &_gaag };func (_bdcf *PSOperand )roll (_fffca *PSStack )error {_adc ,_febef :=_fffca .Pop ();if _febef !=nil {return _febef ;};_bfec ,_febef :=_fffca .Pop ();if _febef !=nil {return _febef ;
};_fbdaf ,_edf :=_adc .(*PSInteger );if !_edf {return ErrTypeCheck ;};_cddd ,_edf :=_bfec .(*PSInteger );if !_edf {return ErrTypeCheck ;};if _cddd .Val < 0{return ErrRangeCheck ;};if _cddd .Val ==0||_cddd .Val ==1{return nil ;};if _cddd .Val > len (*_fffca ){return ErrStackUnderflow ;
};for _ebcd :=0;_ebcd < _bdaf (_fbdaf .Val );_ebcd ++{var _eag []PSObject ;_eag =(*_fffca )[len (*_fffca )-(_cddd .Val ):len (*_fffca )];if _fbdaf .Val > 0{_eebe :=_eag [len (_eag )-1];_eag =append ([]PSObject {_eebe },_eag [0:len (_eag )-1]...);}else {_aabbe :=_eag [len (_eag )-_cddd .Val ];
_eag =append (_eag [1:],_aabbe );};_fege :=append ((*_fffca )[0:len (*_fffca )-_cddd .Val ],_eag ...);_fffca =&_fege ;};return nil ;};func (_geb *PSOperand )truncate (_ged *PSStack )error {_cbba ,_ebfd :=_ged .Pop ();if _ebfd !=nil {return _ebfd ;};if _fccd ,_bcdf :=_cbba .(*PSReal );
_bcdf {_ecd :=int (_fccd .Val );_ebfd =_ged .Push (MakeReal (float64 (_ecd )));}else if _fdb ,_cabg :=_cbba .(*PSInteger );_cabg {_ebfd =_ged .Push (MakeInteger (_fdb .Val ));}else {return ErrTypeCheck ;};return _ebfd ;};
// NewPSProgram returns an empty, initialized PSProgram.
func NewPSProgram ()*PSProgram {return &PSProgram {}};func (_ecf *PSOperand )bitshift (_fbgg *PSStack )error {_acf ,_fg :=_fbgg .PopInteger ();if _fg !=nil {return _fg ;};_fedd ,_fg :=_fbgg .PopInteger ();if _fg !=nil {return _fg ;};var _gfb int ;if _acf >=0{_gfb =_fedd <<uint (_acf );
}else {_gfb =_fedd >>uint (-_acf );};_fg =_fbgg .Push (MakeInteger (_gfb ));return _fg ;};func (_cbb *PSBoolean )Duplicate ()PSObject {_gf :=PSBoolean {};_gf .Val =_cbb .Val ;return &_gf };
|