some improvments

This commit is contained in:
Adrian Zürcher
2026-01-01 11:00:23 +01:00
parent 8f313c00f0
commit ef0778c8b3
20 changed files with 305 additions and 385 deletions

View File

@@ -37,24 +37,24 @@ func PSObjectArrayToFloat64Array(objects []PSObject) ([]float64, error) {
return vals, nil
}
func (this *PSExecutor) Execute(objects []PSObject) ([]PSObject, error) {
func (pE *PSExecutor) Execute(objects []PSObject) ([]PSObject, error) {
// Add the arguments on stack
// [obj1 obj2 ...]
for _, obj := range objects {
err := this.Stack.Push(obj)
err := pE.Stack.Push(obj)
if err != nil {
return nil, err
}
}
err := this.program.Exec(this.Stack)
err := pE.program.Exec(pE.Stack)
if err != nil {
common.Log.Debug("Exec failed: %v", err)
return nil, err
}
result := []PSObject(*this.Stack)
this.Stack.Empty()
result := []PSObject(*pE.Stack)
pE.Stack.Empty()
return result, nil
}

View File

@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"math"
"strings"
)
type PSObject interface {
@@ -17,18 +18,18 @@ type PSInteger struct {
Val int
}
func (this *PSInteger) Duplicate() PSObject {
func (pI *PSInteger) Duplicate() PSObject {
obj := PSInteger{}
obj.Val = this.Val
obj.Val = pI.Val
return &obj
}
func (this *PSInteger) DebugString() string {
return fmt.Sprintf("int:%d", this.Val)
func (pI *PSInteger) DebugString() string {
return fmt.Sprintf("int:%d", pI.Val)
}
func (this *PSInteger) String() string {
return fmt.Sprintf("%d", this.Val)
func (pI *PSInteger) String() string {
return fmt.Sprintf("%d", pI.Val)
}
// Real number.
@@ -36,17 +37,17 @@ type PSReal struct {
Val float64
}
func (this *PSReal) DebugString() string {
return fmt.Sprintf("real:%.5f", this.Val)
func (pR *PSReal) DebugString() string {
return fmt.Sprintf("real:%.5f", pR.Val)
}
func (this *PSReal) String() string {
return fmt.Sprintf("%.5f", this.Val)
func (pR *PSReal) String() string {
return fmt.Sprintf("%.5f", pR.Val)
}
func (this *PSReal) Duplicate() PSObject {
func (pR *PSReal) Duplicate() PSObject {
obj := PSReal{}
obj.Val = this.Val
obj.Val = pR.Val
return &obj
}
@@ -55,17 +56,17 @@ type PSBoolean struct {
Val bool
}
func (this *PSBoolean) DebugString() string {
return fmt.Sprintf("bool:%v", this.Val)
func (pB *PSBoolean) DebugString() string {
return fmt.Sprintf("bool:%v", pB.Val)
}
func (this *PSBoolean) String() string {
return fmt.Sprintf("%v", this.Val)
func (pB *PSBoolean) String() string {
return fmt.Sprintf("%v", pB.Val)
}
func (this *PSBoolean) Duplicate() PSObject {
func (pB *PSBoolean) Duplicate() PSObject {
obj := PSBoolean{}
obj.Val = this.Val
obj.Val = pB.Val
return &obj
}
@@ -76,42 +77,44 @@ func NewPSProgram() *PSProgram {
return &PSProgram{}
}
func (this *PSProgram) Append(obj PSObject) {
*this = append(*this, obj)
func (pP *PSProgram) Append(obj PSObject) {
*pP = append(*pP, obj)
}
func (this *PSProgram) DebugString() string {
s := "{ "
for _, obj := range *this {
s += obj.DebugString()
s += " "
func (pP *PSProgram) DebugString() string {
var s strings.Builder
s.WriteString("{ ")
for _, obj := range *pP {
s.WriteString(obj.DebugString())
s.WriteString(" ")
}
s += "}"
s.WriteString("}")
return s
return s.String()
}
func (this *PSProgram) String() string {
s := "{ "
for _, obj := range *this {
s += obj.String()
s += " "
func (pP *PSProgram) String() string {
var s strings.Builder
s.WriteString("{ ")
for _, obj := range *pP {
s.WriteString(obj.String())
s.WriteString(" ")
}
s += "}"
s.WriteString("}")
return s
return s.String()
}
func (this *PSProgram) Duplicate() PSObject {
func (pP *PSProgram) Duplicate() PSObject {
prog := &PSProgram{}
for _, obj := range *this {
for _, obj := range *pP {
prog.Append(obj.Duplicate())
}
return prog
}
func (this *PSProgram) Exec(stack *PSStack) error {
for _, obj := range *this {
func (pP *PSProgram) Exec(stack *PSStack) error {
for _, obj := range *pP {
var err error
if number, isInt := obj.(*PSInteger); isInt {
err = stack.Push(number)
@@ -136,118 +139,118 @@ func (this *PSProgram) Exec(stack *PSStack) error {
// Operand.
type PSOperand string
func (this *PSOperand) DebugString() string {
return fmt.Sprintf("op:'%s'", *this)
func (pO *PSOperand) DebugString() string {
return fmt.Sprintf("op:'%s'", *pO)
}
func (this *PSOperand) String() string {
return fmt.Sprintf("%s", *this)
func (pO *PSOperand) String() string {
return fmt.Sprintf("%s", *pO)
}
func (this *PSOperand) Duplicate() PSObject {
s := *this
func (pO *PSOperand) Duplicate() PSObject {
s := *pO
return &s
}
func (this *PSOperand) Exec(stack *PSStack) error {
func (pO *PSOperand) Exec(stack *PSStack) error {
err := errors.New("Unsupported operand")
switch *this {
switch *pO {
case "abs":
err = this.Abs(stack)
err = pO.Abs(stack)
case "add":
err = this.Add(stack)
err = pO.Add(stack)
case "and":
err = this.And(stack)
err = pO.And(stack)
case "atan":
err = this.Atan(stack)
err = pO.Atan(stack)
case "bitshift":
err = this.Bitshift(stack)
err = pO.Bitshift(stack)
case "ceiling":
err = this.Ceiling(stack)
err = pO.Ceiling(stack)
case "copy":
err = this.Copy(stack)
err = pO.Copy(stack)
case "cos":
err = this.Cos(stack)
err = pO.Cos(stack)
case "cvi":
err = this.Cvi(stack)
err = pO.Cvi(stack)
case "cvr":
err = this.Cvr(stack)
err = pO.Cvr(stack)
case "div":
err = this.Div(stack)
err = pO.Div(stack)
case "dup":
err = this.Dup(stack)
err = pO.Dup(stack)
case "eq":
err = this.Eq(stack)
err = pO.Eq(stack)
case "exch":
err = this.Exch(stack)
err = pO.Exch(stack)
case "exp":
err = this.Exp(stack)
err = pO.Exp(stack)
case "floor":
err = this.Floor(stack)
err = pO.Floor(stack)
case "ge":
err = this.Ge(stack)
err = pO.Ge(stack)
case "gt":
err = this.Gt(stack)
err = pO.Gt(stack)
case "idiv":
err = this.IDiv(stack)
err = pO.IDiv(stack)
case "if":
err = this.If(stack)
err = pO.If(stack)
case "ifelse":
err = this.IfElse(stack)
err = pO.IfElse(stack)
case "index":
err = this.Index(stack)
err = pO.Index(stack)
case "le":
err = this.Le(stack)
err = pO.Le(stack)
case "log":
err = this.Log(stack)
err = pO.Log(stack)
case "ln":
err = this.Ln(stack)
err = pO.Ln(stack)
case "lt":
err = this.Lt(stack)
err = pO.Lt(stack)
case "mod":
err = this.Mod(stack)
err = pO.Mod(stack)
case "mul":
err = this.Mul(stack)
err = pO.Mul(stack)
case "ne":
err = this.Ne(stack)
err = pO.Ne(stack)
case "neg":
err = this.Neg(stack)
err = pO.Neg(stack)
case "not":
err = this.Not(stack)
err = pO.Not(stack)
case "or":
err = this.Or(stack)
err = pO.Or(stack)
case "pop":
err = this.Pop(stack)
err = pO.Pop(stack)
case "round":
err = this.Round(stack)
err = pO.Round(stack)
case "roll":
err = this.Roll(stack)
err = pO.Roll(stack)
case "sin":
err = this.Sin(stack)
err = pO.Sin(stack)
case "sqrt":
err = this.Sqrt(stack)
err = pO.Sqrt(stack)
case "sub":
err = this.Sub(stack)
err = pO.Sub(stack)
case "truncate":
err = this.Truncate(stack)
err = pO.Truncate(stack)
case "xor":
err = this.Xor(stack)
err = pO.Xor(stack)
}
return err
@@ -257,7 +260,7 @@ func (this *PSOperand) Exec(stack *PSStack) error {
// Operation implementations
// Absolute value.
func (this *PSOperand) Abs(stack *PSStack) error {
func (*PSOperand) Abs(stack *PSStack) error {
obj, err := stack.Pop()
if err != nil {
return err
@@ -285,7 +288,7 @@ func (this *PSOperand) Abs(stack *PSStack) error {
}
// 5 27 add -> 32
func (this *PSOperand) Add(stack *PSStack) error {
func (*PSOperand) Add(stack *PSStack) error {
obj1, err := stack.Pop()
if err != nil {
return err
@@ -338,7 +341,7 @@ func (this *PSOperand) Add(stack *PSStack) error {
// bool1 bool2 and -> bool3
// if int: returns the bitwise "and" of the inputs
// int1 int2 and -> int3
func (this *PSOperand) And(stack *PSStack) error {
func (*PSOperand) And(stack *PSStack) error {
obj1, err := stack.Pop()
if err != nil {
return err
@@ -374,7 +377,7 @@ func (this *PSOperand) And(stack *PSStack) error {
// den num atan -> atan(num/den) in degrees.
// result is a real value.
func (this *PSOperand) Atan(stack *PSStack) error {
func (*PSOperand) Atan(stack *PSStack) error {
// Denominator
den, err := stack.PopNumberAsFloat64()
if err != nil {
@@ -409,7 +412,7 @@ func (this *PSOperand) Atan(stack *PSStack) error {
// bitshift
// int1 shift bitshift -> int2
func (this *PSOperand) Bitshift(stack *PSStack) error {
func (*PSOperand) Bitshift(stack *PSStack) error {
shift, err := stack.PopInteger()
if err != nil {
return err
@@ -435,7 +438,7 @@ func (this *PSOperand) Bitshift(stack *PSStack) error {
// Ceiling of number.
// num1 ceiling -> num2
// The type of the result is the same as of the operand.
func (this *PSOperand) Ceiling(stack *PSStack) error {
func (*PSOperand) Ceiling(stack *PSStack) error {
obj, err := stack.Pop()
if err != nil {
return err
@@ -454,7 +457,7 @@ func (this *PSOperand) Ceiling(stack *PSStack) error {
// Copy
// any1 ... anyn n copy -> any1 ... anyn any1 ... anyn
func (this *PSOperand) Copy(stack *PSStack) error {
func (*PSOperand) Copy(stack *PSStack) error {
n, err := stack.PopInteger()
if err != nil {
return err
@@ -475,7 +478,7 @@ func (this *PSOperand) Copy(stack *PSStack) error {
// Cosine
// angle cos -> real
// Angle is in degrees
func (this *PSOperand) Cos(stack *PSStack) error {
func (*PSOperand) Cos(stack *PSStack) error {
angle, err := stack.PopNumberAsFloat64()
if err != nil {
return err
@@ -487,7 +490,7 @@ func (this *PSOperand) Cos(stack *PSStack) error {
}
// Convert to integer
func (this *PSOperand) Cvi(stack *PSStack) error {
func (*PSOperand) Cvi(stack *PSStack) error {
obj, err := stack.Pop()
if err != nil {
return err
@@ -507,7 +510,7 @@ func (this *PSOperand) Cvi(stack *PSStack) error {
}
// Convert number tor real
func (this *PSOperand) Cvr(stack *PSStack) error {
func (*PSOperand) Cvr(stack *PSStack) error {
obj, err := stack.Pop()
if err != nil {
return err
@@ -524,7 +527,7 @@ func (this *PSOperand) Cvr(stack *PSStack) error {
return err
}
func (this *PSOperand) Div(stack *PSStack) error {
func (*PSOperand) Div(stack *PSStack) error {
obj1, err := stack.Pop()
if err != nil {
return err
@@ -573,7 +576,7 @@ func (this *PSOperand) Div(stack *PSStack) error {
}
// Duplicates the top object on the stack (dup)
func (this *PSOperand) Dup(stack *PSStack) error {
func (*PSOperand) Dup(stack *PSStack) error {
obj, err := stack.Pop()
if err != nil {
return err
@@ -591,7 +594,7 @@ func (this *PSOperand) Dup(stack *PSStack) error {
// Check for equality.
// any1 any2 eq bool
func (this *PSOperand) Eq(stack *PSStack) error {
func (*PSOperand) Eq(stack *PSStack) error {
obj1, err := stack.Pop()
if err != nil {
return err
@@ -646,7 +649,7 @@ func (this *PSOperand) Eq(stack *PSStack) error {
}
// Exchange the top two elements of the stack (exch)
func (this *PSOperand) Exch(stack *PSStack) error {
func (*PSOperand) Exch(stack *PSStack) error {
top, err := stack.Pop()
if err != nil {
return err
@@ -669,7 +672,7 @@ func (this *PSOperand) Exch(stack *PSStack) error {
// base exponent exp -> base^exp
// Raises base to exponent power.
// The result is a real number.
func (this *PSOperand) Exp(stack *PSStack) error {
func (*PSOperand) Exp(stack *PSStack) error {
exponent, err := stack.PopNumberAsFloat64()
if err != nil {
return err
@@ -691,7 +694,7 @@ func (this *PSOperand) Exp(stack *PSStack) error {
}
// Floor of number.
func (this *PSOperand) Floor(stack *PSStack) error {
func (*PSOperand) Floor(stack *PSStack) error {
obj, err := stack.Pop()
if err != nil {
return err
@@ -710,7 +713,7 @@ func (this *PSOperand) Floor(stack *PSStack) error {
// Greater than or equal
// num1 num2 ge -> bool; num1 >= num2
func (this *PSOperand) Ge(stack *PSStack) error {
func (*PSOperand) Ge(stack *PSStack) error {
num2, err := stack.PopNumberAsFloat64()
if err != nil {
return err
@@ -736,7 +739,7 @@ func (this *PSOperand) Ge(stack *PSStack) error {
// Greater than
// num1 num2 gt -> bool; num1 > num2
func (this *PSOperand) Gt(stack *PSStack) error {
func (*PSOperand) Gt(stack *PSStack) error {
num2, err := stack.PopNumberAsFloat64()
if err != nil {
return err
@@ -762,7 +765,7 @@ func (this *PSOperand) Gt(stack *PSStack) error {
// Integral division
// 25 3 div -> 8
func (this *PSOperand) IDiv(stack *PSStack) error {
func (*PSOperand) IDiv(stack *PSStack) error {
obj1, err := stack.Pop()
if err != nil {
return err
@@ -794,7 +797,7 @@ func (this *PSOperand) IDiv(stack *PSStack) error {
// If conditional
// bool proc if -> run proc() if bool is true
func (this *PSOperand) If(stack *PSStack) error {
func (*PSOperand) If(stack *PSStack) error {
obj1, err := stack.Pop()
if err != nil {
return err
@@ -825,7 +828,7 @@ func (this *PSOperand) If(stack *PSStack) error {
// If else conditional
// bool proc1 proc2 ifelse -> execute proc1() if bool is true, otherwise proc2()
func (this *PSOperand) IfElse(stack *PSStack) error {
func (*PSOperand) IfElse(stack *PSStack) error {
obj1, err := stack.Pop()
if err != nil {
return err
@@ -866,7 +869,7 @@ func (this *PSOperand) IfElse(stack *PSStack) error {
// Add a copy of the nth object in the stack to the top.
// any_n ... any_0 n index -> any_n ... any_0 any_n
// index from 0
func (this *PSOperand) Index(stack *PSStack) error {
func (*PSOperand) Index(stack *PSStack) error {
obj, err := stack.Pop()
if err != nil {
return err
@@ -893,7 +896,7 @@ func (this *PSOperand) Index(stack *PSStack) error {
// Less or equal
// num1 num2 le -> bool; num1 <= num2
func (this *PSOperand) Le(stack *PSStack) error {
func (*PSOperand) Le(stack *PSStack) error {
num2, err := stack.PopNumberAsFloat64()
if err != nil {
return err
@@ -918,7 +921,7 @@ func (this *PSOperand) Le(stack *PSStack) error {
}
// num log -> real
func (this *PSOperand) Log(stack *PSStack) error {
func (*PSOperand) Log(stack *PSStack) error {
// Value
val, err := stack.PopNumberAsFloat64()
if err != nil {
@@ -932,7 +935,7 @@ func (this *PSOperand) Log(stack *PSStack) error {
// num ln -> ln(num)
// The result is a real number.
func (this *PSOperand) Ln(stack *PSStack) error {
func (*PSOperand) Ln(stack *PSStack) error {
// Value
val, err := stack.PopNumberAsFloat64()
if err != nil {
@@ -946,7 +949,7 @@ func (this *PSOperand) Ln(stack *PSStack) error {
// Less than
// num1 num2 lt -> bool; num1 < num2
func (this *PSOperand) Lt(stack *PSStack) error {
func (*PSOperand) Lt(stack *PSStack) error {
num2, err := stack.PopNumberAsFloat64()
if err != nil {
return err
@@ -971,7 +974,7 @@ func (this *PSOperand) Lt(stack *PSStack) error {
}
// 12 10 mod -> 2
func (this *PSOperand) Mod(stack *PSStack) error {
func (*PSOperand) Mod(stack *PSStack) error {
obj1, err := stack.Pop()
if err != nil {
return err
@@ -1001,7 +1004,7 @@ func (this *PSOperand) Mod(stack *PSStack) error {
}
// 6 8 mul -> 48
func (this *PSOperand) Mul(stack *PSStack) error {
func (*PSOperand) Mul(stack *PSStack) error {
obj1, err := stack.Pop()
if err != nil {
return err
@@ -1051,21 +1054,21 @@ func (this *PSOperand) Mul(stack *PSStack) error {
// Not equal (inverse of eq)
// any1 any2 ne -> bool
func (this *PSOperand) Ne(stack *PSStack) error {
func (pO *PSOperand) Ne(stack *PSStack) error {
// Simply call equate and then negate the result.
// Implementing directly could be more efficient, but probably not a big deal in most cases.
err := this.Eq(stack)
err := pO.Eq(stack)
if err != nil {
return err
}
err = this.Not(stack)
err = pO.Not(stack)
return err
}
// Negate
// 6 neg -> -6
func (this *PSOperand) Neg(stack *PSStack) error {
func (*PSOperand) Neg(stack *PSStack) error {
obj, err := stack.Pop()
if err != nil {
return err
@@ -1086,7 +1089,7 @@ func (this *PSOperand) Neg(stack *PSStack) error {
// Logical/bitwise negation
// bool1 not -> bool2 (logical)
// int1 not -> int2 (bitwise)
func (this *PSOperand) Not(stack *PSStack) error {
func (*PSOperand) Not(stack *PSStack) error {
obj, err := stack.Pop()
if err != nil {
return err
@@ -1106,7 +1109,7 @@ func (this *PSOperand) Not(stack *PSStack) error {
// OR logical/bitwise operation.
// bool1 bool2 or -> bool3 (logical or)
// int1 int2 or -> int3 (bitwise or)
func (this *PSOperand) Or(stack *PSStack) error {
func (*PSOperand) Or(stack *PSStack) error {
obj1, err := stack.Pop()
if err != nil {
return err
@@ -1141,7 +1144,7 @@ func (this *PSOperand) Or(stack *PSStack) error {
}
// Remove the top element on the stack (pop)
func (this *PSOperand) Pop(stack *PSStack) error {
func (*PSOperand) Pop(stack *PSStack) error {
_, err := stack.Pop()
if err != nil {
return err
@@ -1151,7 +1154,7 @@ func (this *PSOperand) Pop(stack *PSStack) error {
// Round number off.
// num1 round -> num2
func (this *PSOperand) Round(stack *PSStack) error {
func (*PSOperand) Round(stack *PSStack) error {
obj, err := stack.Pop()
if err != nil {
return err
@@ -1173,7 +1176,7 @@ func (this *PSOperand) Round(stack *PSStack) error {
// 7 8 9 3 1 roll -> 9 7 8
// 7 8 9 3 -1 roll -> 8 9 7
// n j roll
func (this *PSOperand) Roll(stack *PSStack) error {
func (*PSOperand) Roll(stack *PSStack) error {
obj1, err := stack.Pop()
if err != nil {
return err
@@ -1228,7 +1231,7 @@ func (this *PSOperand) Roll(stack *PSStack) error {
// Sine.
// angle sin -> real
// Angle is in degrees
func (this *PSOperand) Sin(stack *PSStack) error {
func (*PSOperand) Sin(stack *PSStack) error {
angle, err := stack.PopNumberAsFloat64()
if err != nil {
return err
@@ -1242,7 +1245,7 @@ func (this *PSOperand) Sin(stack *PSStack) error {
// Square root.
// num sqrt -> real; real=sqrt(num)
// The result is a real number.
func (this *PSOperand) Sqrt(stack *PSStack) error {
func (*PSOperand) Sqrt(stack *PSStack) error {
val, err := stack.PopNumberAsFloat64()
if err != nil {
return err
@@ -1260,7 +1263,7 @@ func (this *PSOperand) Sqrt(stack *PSStack) error {
// 8.3 6.6 sub -> 1.7 (real)
// 8 6.3 sub -> 1.7 (real)
// 8 6 sub -> 2 (int)
func (this *PSOperand) Sub(stack *PSStack) error {
func (*PSOperand) Sub(stack *PSStack) error {
obj1, err := stack.Pop()
if err != nil {
return err
@@ -1311,7 +1314,7 @@ func (this *PSOperand) Sub(stack *PSStack) error {
// Truncate number.
// num1 truncate -> num2
// The resulting number is the same type as the input.
func (this *PSOperand) Truncate(stack *PSStack) error {
func (*PSOperand) Truncate(stack *PSStack) error {
obj, err := stack.Pop()
if err != nil {
return err
@@ -1332,7 +1335,7 @@ func (this *PSOperand) Truncate(stack *PSStack) error {
// XOR logical/bitwise operation.
// bool1 bool2 xor -> bool3 (logical xor)
// int1 int2 xor -> int3 (bitwise xor)
func (this *PSOperand) Xor(stack *PSStack) error {
func (*PSOperand) Xor(stack *PSStack) error {
obj1, err := stack.Pop()
if err != nil {
return err

View File

@@ -28,9 +28,9 @@ func NewPSParser(content []byte) *PSParser {
}
// Parse the postscript and store as a program that can be executed.
func (this *PSParser) Parse() (*PSProgram, error) {
this.skipSpaces()
bb, err := this.reader.Peek(2)
func (pP *PSParser) Parse() (*PSProgram, error) {
pP.skipSpaces()
bb, err := pP.reader.Peek(2)
if err != nil {
return nil, err
}
@@ -38,7 +38,7 @@ func (this *PSParser) Parse() (*PSProgram, error) {
return nil, fmt.Errorf("invalid PS Program not starting with {")
}
program, err := this.parseFunction()
program, err := pP.parseFunction()
if err != nil && err != io.EOF {
return nil, err
}
@@ -48,8 +48,8 @@ func (this *PSParser) Parse() (*PSProgram, error) {
// Detect the signature at the current parse position and parse
// the corresponding object.
func (this *PSParser) parseFunction() (*PSProgram, error) {
c, _ := this.reader.ReadByte()
func (pP *PSParser) parseFunction() (*PSProgram, error) {
c, _ := pP.reader.ReadByte()
if c != '{' {
return nil, errors.New("invalid function")
}
@@ -57,8 +57,8 @@ func (this *PSParser) parseFunction() (*PSProgram, error) {
function := NewPSProgram()
for {
this.skipSpaces()
bb, err := this.reader.Peek(2)
pP.skipSpaces()
bb, err := pP.reader.Peek(2)
if err != nil {
if err == io.EOF {
break
@@ -70,18 +70,18 @@ func (this *PSParser) parseFunction() (*PSProgram, error) {
// Determine type.
if bb[0] == '}' {
common.Log.Trace("EOF function")
this.reader.ReadByte()
pP.reader.ReadByte()
break
} else if bb[0] == '{' {
common.Log.Trace("Function!")
inlineF, err := this.parseFunction()
inlineF, err := pP.parseFunction()
if err != nil {
return nil, err
}
function.Append(inlineF)
} else if pdfcore.IsDecimalDigit(bb[0]) || (bb[0] == '-' && pdfcore.IsDecimalDigit(bb[1])) {
common.Log.Trace("->Number!")
number, err := this.parseNumber()
number, err := pP.parseNumber()
if err != nil {
return nil, err
}
@@ -89,24 +89,24 @@ func (this *PSParser) parseFunction() (*PSProgram, error) {
} else {
common.Log.Trace("->Operand or bool?")
// Let's peek farther to find out.
bb, _ = this.reader.Peek(5)
bb, _ = pP.reader.Peek(5)
peekStr := string(bb)
common.Log.Trace("Peek str: %s", peekStr)
if (len(peekStr) > 4) && (peekStr[:5] == "false") {
b, err := this.parseBool()
b, err := pP.parseBool()
if err != nil {
return nil, err
}
function.Append(b)
} else if (len(peekStr) > 3) && (peekStr[:4] == "true") {
b, err := this.parseBool()
b, err := pP.parseBool()
if err != nil {
return nil, err
}
function.Append(b)
} else {
operand, err := this.parseOperand()
operand, err := pP.parseOperand()
if err != nil {
return nil, err
}
@@ -120,15 +120,15 @@ func (this *PSParser) parseFunction() (*PSProgram, error) {
// Skip over any spaces. Returns the number of spaces skipped and
// an error if any.
func (this *PSParser) skipSpaces() (int, error) {
func (pP *PSParser) skipSpaces() (int, error) {
cnt := 0
for {
bb, err := this.reader.Peek(1)
bb, err := pP.reader.Peek(1)
if err != nil {
return 0, err
}
if pdfcore.IsWhiteSpace(bb[0]) {
this.reader.ReadByte()
pP.reader.ReadByte()
cnt++
} else {
break
@@ -140,13 +140,13 @@ func (this *PSParser) skipSpaces() (int, error) {
// Numeric objects.
// Integer or Real numbers.
func (this *PSParser) parseNumber() (PSObject, error) {
func (pP *PSParser) parseNumber() (PSObject, error) {
isFloat := false
allowSigns := true
numStr := ""
for {
common.Log.Trace("Parsing number \"%s\"", numStr)
bb, err := this.reader.Peek(1)
bb, err := pP.reader.Peek(1)
if err == io.EOF {
// GH: EOF handling. Handle EOF like end of line. Can happen with
// encoded object streams that the object is at the end.
@@ -159,20 +159,20 @@ func (this *PSParser) parseNumber() (PSObject, error) {
}
if allowSigns && (bb[0] == '-' || bb[0] == '+') {
// Only appear in the beginning, otherwise serves as a delimiter.
b, _ := this.reader.ReadByte()
b, _ := pP.reader.ReadByte()
numStr += string(b)
allowSigns = false // Only allowed in beginning, and after e (exponential).
} else if pdfcore.IsDecimalDigit(bb[0]) {
b, _ := this.reader.ReadByte()
b, _ := pP.reader.ReadByte()
numStr += string(b)
} else if bb[0] == '.' {
b, _ := this.reader.ReadByte()
b, _ := pP.reader.ReadByte()
numStr += string(b)
isFloat = true
} else if bb[0] == 'e' {
// Exponential number format.
// XXX Is this supported in PS?
b, _ := this.reader.ReadByte()
b, _ := pP.reader.ReadByte()
numStr += string(b)
isFloat = true
allowSigns = true
@@ -193,22 +193,22 @@ func (this *PSParser) parseNumber() (PSObject, error) {
}
// Parse bool object.
func (this *PSParser) parseBool() (*PSBoolean, error) {
bb, err := this.reader.Peek(4)
func (pP *PSParser) parseBool() (*PSBoolean, error) {
bb, err := pP.reader.Peek(4)
if err != nil {
return MakeBool(false), err
}
if (len(bb) >= 4) && (string(bb[:4]) == "true") {
this.reader.Discard(4)
pP.reader.Discard(4)
return MakeBool(true), nil
}
bb, err = this.reader.Peek(5)
bb, err = pP.reader.Peek(5)
if err != nil {
return MakeBool(false), err
}
if (len(bb) >= 5) && (string(bb[:5]) == "false") {
this.reader.Discard(5)
pP.reader.Discard(5)
return MakeBool(false), nil
}
@@ -216,10 +216,10 @@ func (this *PSParser) parseBool() (*PSBoolean, error) {
}
// An operand is a text command represented by a word.
func (this *PSParser) parseOperand() (*PSOperand, error) {
func (pP *PSParser) parseOperand() (*PSOperand, error) {
bytes := []byte{}
for {
bb, err := this.reader.Peek(1)
bb, err := pP.reader.Peek(1)
if err != nil {
if err == io.EOF {
break
@@ -233,7 +233,7 @@ func (this *PSParser) parseOperand() (*PSOperand, error) {
break
}
b, _ := this.reader.ReadByte()
b, _ := pP.reader.ReadByte()
bytes = append(bytes, b)
}

View File

@@ -1,5 +1,7 @@
package ps
import "strings"
type PSStack []PSObject
func NewPSStack() *PSStack {
@@ -60,24 +62,26 @@ func (stack *PSStack) PopNumberAsFloat64() (float64, error) {
}
}
func (this *PSStack) String() string {
s := "[ "
for _, obj := range *this {
s += obj.String()
s += " "
func (pS *PSStack) String() string {
var s strings.Builder
s.WriteString("[ ")
for _, obj := range *pS {
s.WriteString(obj.String())
s.WriteString(" ")
}
s += "]"
s.WriteString("]")
return s
return s.String()
}
func (this *PSStack) DebugString() string {
s := "[ "
for _, obj := range *this {
s += obj.DebugString()
s += " "
func (pS *PSStack) DebugString() string {
var s strings.Builder
s.WriteString("[ ")
for _, obj := range *pS {
s.WriteString(obj.DebugString())
s.WriteString(" ")
}
s += "]"
s.WriteString("]")
return s
return s.String()
}