Files
pdfmerge/internal/pdf/ps/exec.go
Adrian Zürcher ef0778c8b3 some improvments
2026-01-01 11:00:23 +01:00

61 lines
1.2 KiB
Go

package ps
// A limited postscript parser for PDF function type 4.
import (
"fmt"
"gitea.tecamino.com/paadi/pdfmerge/internal/pdf/common"
)
// A PSExecutor has its own execution stack and is used to executre a PS routine (program).
type PSExecutor struct {
Stack *PSStack
program *PSProgram
}
func NewPSExecutor(program *PSProgram) *PSExecutor {
executor := &PSExecutor{}
executor.Stack = NewPSStack()
executor.program = program
return executor
}
func PSObjectArrayToFloat64Array(objects []PSObject) ([]float64, error) {
vals := []float64{}
for _, obj := range objects {
if number, is := obj.(*PSInteger); is {
vals = append(vals, float64(number.Val))
} else if number, is := obj.(*PSReal); is {
vals = append(vals, number.Val)
} else {
return nil, fmt.Errorf("Type error")
}
}
return vals, nil
}
func (pE *PSExecutor) Execute(objects []PSObject) ([]PSObject, error) {
// Add the arguments on stack
// [obj1 obj2 ...]
for _, obj := range objects {
err := pE.Stack.Push(obj)
if err != nil {
return nil, err
}
}
err := pE.program.Exec(pE.Stack)
if err != nil {
common.Log.Debug("Exec failed: %v", err)
return nil, err
}
result := []PSObject(*pE.Stack)
pE.Stack.Empty()
return result, nil
}