2 Commits
v0.0.3 ... main

Author SHA1 Message Date
Adrian Zürcher
aafa89a703 uncomment function 2026-01-01 11:01:42 +01:00
Adrian Zürcher
ef0778c8b3 some improvments 2026-01-01 11:00:23 +01:00
20 changed files with 297 additions and 377 deletions

View File

@@ -19,22 +19,22 @@ type Logger interface {
// Dummy Logger does nothing.
type DummyLogger struct{}
func (this DummyLogger) Error(format string, args ...any) {
func (DummyLogger) Error(format string, args ...any) {
}
func (this DummyLogger) Warning(format string, args ...any) {
func (DummyLogger) Warning(format string, args ...any) {
}
func (this DummyLogger) Notice(format string, args ...any) {
func (DummyLogger) Notice(format string, args ...any) {
}
func (this DummyLogger) Info(format string, args ...any) {
func (DummyLogger) Info(format string, args ...any) {
}
func (this DummyLogger) Debug(format string, args ...any) {
func (DummyLogger) Debug(format string, args ...any) {
}
func (this DummyLogger) Trace(format string, args ...any) {
func (DummyLogger) Trace(format string, args ...any) {
}
// Simple Console Logger that the tests use.
@@ -59,45 +59,39 @@ func NewConsoleLogger(logLevel LogLevel) *ConsoleLogger {
return &logger
}
func (this ConsoleLogger) Error(format string, args ...any) {
if this.LogLevel >= LogLevelError {
prefix := "[ERROR] "
this.output(os.Stdout, prefix, format, args...)
func (cL ConsoleLogger) Error(format string, args ...any) {
if cL.LogLevel >= LogLevelError {
cL.output(os.Stdout, "[ERROR] ", format, args...)
}
}
func (this ConsoleLogger) Warning(format string, args ...any) {
if this.LogLevel >= LogLevelWarning {
prefix := "[WARNING] "
this.output(os.Stdout, prefix, format, args...)
func (cL ConsoleLogger) Warning(format string, args ...any) {
if cL.LogLevel >= LogLevelWarning {
cL.output(os.Stdout, "[WARNING] ", format, args...)
}
}
func (this ConsoleLogger) Notice(format string, args ...any) {
if this.LogLevel >= LogLevelNotice {
prefix := "[NOTICE] "
this.output(os.Stdout, prefix, format, args...)
func (cL ConsoleLogger) Notice(format string, args ...any) {
if cL.LogLevel >= LogLevelNotice {
cL.output(os.Stdout, "[NOTICE] ", format, args...)
}
}
func (this ConsoleLogger) Info(format string, args ...any) {
if this.LogLevel >= LogLevelInfo {
prefix := "[INFO] "
this.output(os.Stdout, prefix, format, args...)
func (cL ConsoleLogger) Info(format string, args ...any) {
if cL.LogLevel >= LogLevelInfo {
cL.output(os.Stdout, "[INFO] ", format, args...)
}
}
func (this ConsoleLogger) Debug(format string, args ...any) {
if this.LogLevel >= LogLevelDebug {
prefix := "[DEBUG] "
this.output(os.Stdout, prefix, format, args...)
func (cL ConsoleLogger) Debug(format string, args ...any) {
if cL.LogLevel >= LogLevelDebug {
cL.output(os.Stdout, "[DEBUG] ", format, args...)
}
}
func (this ConsoleLogger) Trace(format string, args ...any) {
if this.LogLevel >= LogLevelTrace {
prefix := "[TRACE] "
this.output(os.Stdout, prefix, format, args...)
func (cL ConsoleLogger) Trace(format string, args ...any) {
if cL.LogLevel >= LogLevelTrace {
cL.output(os.Stdout, "[TRACE] ", format, args...)
}
}
@@ -108,7 +102,7 @@ func SetLogger(logger Logger) {
}
// output writes `format`, `args` log message prefixed by the source file name, line and `prefix`
func (this ConsoleLogger) output(f *os.File, prefix string, format string, args ...any) {
func (ConsoleLogger) output(f *os.File, prefix string, format string, args ...any) {
_, file, line, ok := runtime.Caller(3)
if !ok {
file = "???"

View File

@@ -1,11 +0,0 @@
package common
import (
"time"
)
const timeFormat = "2 January 2006 at 15:04"
func UtcTimeFormat(t time.Time) string {
return t.Format(timeFormat) + " UTC"
}

View File

@@ -1,17 +0,0 @@
// Package common contains common properties used by the subpackages.
package common
import (
"time"
)
const releaseYear = 2018
const releaseMonth = 11
const releaseDay = 17
const releaseHour = 11
const releaseMin = 30
// Holds version information, when bumping this make sure to bump the released at stamp also.
const Version = "2.2.0"
var ReleasedAt = time.Date(releaseYear, releaseMonth, releaseDay, releaseHour, releaseMin, 0, 0, time.UTC)

View File

@@ -3,6 +3,7 @@ package contentstream
import (
"bytes"
"fmt"
"strings"
"gitea.tecamino.com/paadi/pdfmerge/internal/pdf/core"
)
@@ -111,7 +112,7 @@ func (s *ContentStreamParser) ExtractText() (string, error) {
}
inText := false
xPos, yPos := float64(-1), float64(-1)
txt := ""
var txt strings.Builder
for _, op := range *operations {
switch op.Operand {
case "BT":
@@ -122,7 +123,7 @@ func (s *ContentStreamParser) ExtractText() (string, error) {
if op.Operand == "Td" || op.Operand == "TD" || op.Operand == "T*" {
// Move to next line...
txt += "\n"
txt.WriteString("\n")
}
if op.Operand == "Tm" {
if len(op.Params) != 6 {
@@ -147,7 +148,7 @@ func (s *ContentStreamParser) ExtractText() (string, error) {
if yPos == -1 {
yPos = float64(*yfloat)
} else if yPos > float64(*yfloat) {
txt += "\n"
txt.WriteString("\n")
xPos = float64(*xfloat)
yPos = float64(*yfloat)
continue
@@ -155,7 +156,7 @@ func (s *ContentStreamParser) ExtractText() (string, error) {
if xPos == -1 {
xPos = float64(*xfloat)
} else if xPos < float64(*xfloat) {
txt += "\t"
txt.WriteString("\t")
xPos = float64(*xfloat)
}
}
@@ -170,14 +171,14 @@ func (s *ContentStreamParser) ExtractText() (string, error) {
for _, obj := range *paramList {
switch v := obj.(type) {
case *core.PdfObjectString:
txt += string(*v)
txt.WriteString(string(*v))
case *core.PdfObjectFloat:
if *v < -100 {
txt += " "
txt.WriteString(" ")
}
case *core.PdfObjectInteger:
if *v < -100 {
txt += " "
txt.WriteString(" ")
}
}
}
@@ -189,9 +190,9 @@ func (s *ContentStreamParser) ExtractText() (string, error) {
if !ok {
return "", fmt.Errorf("invalid parameter type, not string (%T)", op.Params[0])
}
txt += string(*param)
txt.WriteString(string(*param))
}
}
return txt, nil
return txt.String(), nil
}

View File

@@ -89,9 +89,9 @@ func NewCubicBezierPath() CubicBezierPath {
return bpath
}
func (this CubicBezierPath) AppendCurve(curve CubicBezierCurve) CubicBezierPath {
this.Curves = append(this.Curves, curve)
return this
func (c CubicBezierPath) AppendCurve(curve CubicBezierCurve) CubicBezierPath {
c.Curves = append(c.Curves, curve)
return c
}
func (bpath CubicBezierPath) Copy() CubicBezierPath {

View File

@@ -11,36 +11,36 @@ func NewPath() Path {
return path
}
func (this Path) AppendPoint(point Point) Path {
this.Points = append(this.Points, point)
return this
func (p Path) AppendPoint(point Point) Path {
p.Points = append(p.Points, point)
return p
}
func (this Path) RemovePoint(number int) Path {
if number < 1 || number > len(this.Points) {
return this
func (p Path) RemovePoint(number int) Path {
if number < 1 || number > len(p.Points) {
return p
}
idx := number - 1
this.Points = append(this.Points[:idx], this.Points[idx+1:]...)
return this
p.Points = append(p.Points[:idx], p.Points[idx+1:]...)
return p
}
func (this Path) Length() int {
return len(this.Points)
func (p Path) Length() int {
return len(p.Points)
}
func (this Path) GetPointNumber(number int) Point {
if number < 1 || number > len(this.Points) {
func (p Path) GetPointNumber(number int) Point {
if number < 1 || number > len(p.Points) {
return Point{}
}
return this.Points[number-1]
return p.Points[number-1]
}
func (path Path) Copy() Path {
func (p Path) Copy() Path {
pathcopy := Path{}
pathcopy.Points = []Point{}
for _, p := range path.Points {
for _, p := range p.Points {
pathcopy.Points = append(pathcopy.Points, p)
}
return pathcopy

View File

@@ -21,10 +21,10 @@ func (p Point) Add(dx, dy float64) Point {
}
// Add vector to a point.
func (this Point) AddVector(v Vector) Point {
this.X += v.Dx
this.Y += v.Dy
return this
func (p Point) AddVector(v Vector) Point {
p.X += v.Dx
p.Y += v.Dy
return p
}
func (p Point) String() string {

View File

@@ -43,13 +43,13 @@ func (v Vector) Rotate(phi float64) Vector {
}
// Change the sign of the vector: -vector.
func (this Vector) Flip() Vector {
mag := this.Magnitude()
theta := this.GetPolarAngle()
func (v Vector) Flip() Vector {
mag := v.Magnitude()
theta := v.GetPolarAngle()
this.Dx = mag * math.Cos(theta+math.Pi)
this.Dy = mag * math.Sin(theta+math.Pi)
return this
v.Dx = mag * math.Cos(theta+math.Pi)
v.Dy = mag * math.Sin(theta+math.Pi)
return v
}
func (v Vector) FlipY() Vector {
@@ -62,19 +62,19 @@ func (v Vector) FlipX() Vector {
return v
}
func (this Vector) Scale(factor float64) Vector {
mag := this.Magnitude()
theta := this.GetPolarAngle()
func (v Vector) Scale(factor float64) Vector {
mag := v.Magnitude()
theta := v.GetPolarAngle()
this.Dx = factor * mag * math.Cos(theta)
this.Dy = factor * mag * math.Sin(theta)
return this
v.Dx = factor * mag * math.Cos(theta)
v.Dy = factor * mag * math.Sin(theta)
return v
}
func (this Vector) Magnitude() float64 {
return math.Sqrt(math.Pow(this.Dx, 2.0) + math.Pow(this.Dy, 2.0))
func (v Vector) Magnitude() float64 {
return math.Sqrt(math.Pow(v.Dx, 2.0) + math.Pow(v.Dy, 2.0))
}
func (this Vector) GetPolarAngle() float64 {
return math.Atan2(this.Dy, this.Dx)
func (v Vector) GetPolarAngle() float64 {
return math.Atan2(v.Dy, v.Dx)
}

View File

@@ -10,21 +10,18 @@ import (
"gitea.tecamino.com/paadi/pdfmerge/internal/pdf/common"
)
// TODO (v3): Create a new type xrefType which can be an integer and can be used for improved type checking.
// TODO (v3): Unexport these constants and rename with camelCase.
const (
// XREF_TABLE_ENTRY indicates a normal xref table entry.
XREF_TABLE_ENTRY = iota
// xRefTableEntry indicates a normal xref table entry.
xRefTableEntry = iota
// XREF_OBJECT_STREAM indicates an xref entry in an xref object stream.
XREF_OBJECT_STREAM = iota
// xRefObjectStream indicates an xref entry in an xref object stream.
xRefObjectStream = iota
)
// XrefObject defines a cross reference entry which is a map between object number (with generation number) and the
// location of the actual object, either as a file offset (xref table entry), or as a location within an xref
// stream object (xref object stream).
// TODO (v3): Unexport.
type XrefObject struct {
type xRefObject struct {
xtype int
objectNumber int
generation int
@@ -36,32 +33,28 @@ type XrefObject struct {
}
// XrefTable is a map between object number and corresponding XrefObject.
// TODO (v3): Unexport.
// TODO: Consider changing to a slice, so can maintain the object order without sorting when analyzing.
type XrefTable map[int]XrefObject
type xRefTable map[int]xRefObject
// ObjectStream represents an object stream's information which can contain multiple indirect objects.
// The information specifies the number of objects and has information about offset locations for
// each object.
// TODO (v3): Unexport.
type ObjectStream struct {
N int // TODO (v3): Unexport.
type objectStream struct {
n int
ds []byte
offsets map[int]int64
}
// ObjectStreams defines a map between object numbers (object streams only) and underlying ObjectStream information.
type ObjectStreams map[int]ObjectStream
type ObjectStreams map[int]objectStream
// ObjectCache defines a map between object numbers and corresponding PdfObject. Serves as a cache for PdfObjects that
// have already been parsed.
// TODO (v3): Unexport.
type ObjectCache map[int]PdfObject
type objectCache map[int]PdfObject
// Get an object from an object stream.
func (parser *PdfParser) lookupObjectViaOS(sobjNumber int, objNum int) (PdfObject, error) {
var bufReader *bytes.Reader
var objstm ObjectStream
var objstm objectStream
var cached bool
objstm, cached = parser.objstms[sobjNumber]
@@ -149,7 +142,7 @@ func (parser *PdfParser) lookupObjectViaOS(sobjNumber int, objNum int) (PdfObjec
offsets[int(*onum)] = int64(*firstOffset + *offset)
}
objstm = ObjectStream{N: int(*N), ds: ds, offsets: offsets}
objstm = objectStream{n: int(*N), ds: ds, offsets: offsets}
parser.objstms[sobjNumber] = objstm
} else {
// Temporarily change the reader object to this decoded buffer.
@@ -246,7 +239,7 @@ func (parser *PdfParser) lookupByNumber(objNumber int, attemptRepairs bool) (Pdf
common.Log.Trace("Lookup obj number %d", objNumber)
switch xref.xtype {
case XREF_TABLE_ENTRY:
case xRefTableEntry:
common.Log.Trace("xrefobj obj num %d", xref.objectNumber)
common.Log.Trace("xrefobj gen %d", xref.generation)
common.Log.Trace("xrefobj offset %d", xref.offset)
@@ -283,7 +276,7 @@ func (parser *PdfParser) lookupByNumber(objNumber int, attemptRepairs bool) (Pdf
return nil, false, err
}
// Empty the cache.
parser.ObjCache = ObjectCache{}
parser.ObjCache = objectCache{}
// Try looking up again and return.
return parser.lookupByNumberWrapper(objNumber, false)
}
@@ -292,7 +285,7 @@ func (parser *PdfParser) lookupByNumber(objNumber int, attemptRepairs bool) (Pdf
common.Log.Trace("Returning obj")
parser.ObjCache[objNumber] = obj
return obj, false, nil
case XREF_OBJECT_STREAM:
case xRefObjectStream:
common.Log.Trace("xref from object stream!")
common.Log.Trace(">Load via OS!")
common.Log.Trace("Object stream available in object %d/%d", xref.osObjNumber, xref.osObjIndex)
@@ -361,7 +354,7 @@ func (parser *PdfParser) Trace(obj PdfObject) (PdfObject, error) {
return o, nil
}
func printXrefTable(xrefTable XrefTable) {
func printXrefTable(xrefTable xRefTable) {
common.Log.Debug("=X=X=X=")
common.Log.Debug("Xref table:")
i := 0

View File

@@ -35,10 +35,10 @@ type PdfParser struct {
rs io.ReadSeeker
reader *bufio.Reader
fileSize int64
xrefs XrefTable
xrefs xRefTable
objstms ObjectStreams
trailer *PdfObjectDictionary
ObjCache ObjectCache // TODO: Unexport (v3).
ObjCache objectCache
crypter *PdfCrypt
repairsAttempted bool // Avoid multiple attempts for repair.
@@ -740,8 +740,8 @@ func (parser *PdfParser) parseXrefTable() (*PdfObjectDictionary, error) {
// would be marked as free. But can still happen!
x, ok := parser.xrefs[curObjNum]
if !ok || gen > x.generation {
obj := XrefObject{objectNumber: curObjNum,
xtype: XREF_TABLE_ENTRY,
obj := xRefObject{objectNumber: curObjNum,
xtype: xRefTableEntry,
offset: first, generation: gen}
parser.xrefs[curObjNum] = obj
}
@@ -1000,16 +1000,16 @@ func (parser *PdfParser) parseXrefStream(xstm *PdfObjectInteger) (*PdfObjectDict
if xr, ok := parser.xrefs[objNum]; !ok || int(n3) > xr.generation {
// Only overload if not already loaded!
// or has a newer generation number. (should not happen)
obj := XrefObject{objectNumber: objNum,
xtype: XREF_TABLE_ENTRY, offset: n2, generation: int(n3)}
obj := xRefObject{objectNumber: objNum,
xtype: xRefTableEntry, offset: n2, generation: int(n3)}
parser.xrefs[objNum] = obj
}
case 2:
// Object type 2: Compressed object.
common.Log.Trace("- In use - compressed object")
if _, ok := parser.xrefs[objNum]; !ok {
obj := XrefObject{objectNumber: objNum,
xtype: XREF_OBJECT_STREAM, osObjNumber: int(n2), osObjIndex: int(n3)}
obj := xRefObject{objectNumber: objNum,
xtype: xRefObjectStream, osObjNumber: int(n2), osObjIndex: int(n3)}
parser.xrefs[objNum] = obj
common.Log.Trace("entry: %s", parser.xrefs[objNum])
}
@@ -1128,7 +1128,7 @@ func (parser *PdfParser) seekToEOFMarker(fSize int64) error {
// The earlier xrefs have higher precedence. If objects already
// loaded will ignore older versions.
func (parser *PdfParser) loadXrefs() (*PdfObjectDictionary, error) {
parser.xrefs = make(XrefTable)
parser.xrefs = make(xRefTable)
parser.objstms = make(ObjectStreams)
// Get the file size.
@@ -1482,30 +1482,13 @@ func (parser *PdfParser) ParseIndirectObject() (PdfObject, error) {
return &indirect, nil
}
// For testing purposes.
// TODO: Unexport (v3) or move to test files, if needed by external test cases.
func NewParserFromString(txt string) *PdfParser {
parser := PdfParser{}
buf := []byte(txt)
bufReader := bytes.NewReader(buf)
parser.rs = bufReader
bufferedReader := bufio.NewReader(bufReader)
parser.reader = bufferedReader
parser.fileSize = int64(len(txt))
return &parser
}
// NewParser creates a new parser for a PDF file via ReadSeeker. Loads the cross reference stream and trailer.
// An error is returned on failure.
func NewParser(rs io.ReadSeeker) (*PdfParser, error) {
parser := &PdfParser{}
parser.rs = rs
parser.ObjCache = make(ObjectCache)
parser.ObjCache = make(objectCache)
parser.streamLengthReferenceLookupInProgress = map[int64]bool{}
// Start by reading the xrefs (from bottom).

View File

@@ -3,6 +3,7 @@ package core
import (
"bytes"
"fmt"
"strings"
"gitea.tecamino.com/paadi/pdfmerge/internal/pdf/common"
)
@@ -315,15 +316,16 @@ func (array *PdfObjectArray) String() string {
// DefaultWriteString outputs the object as it is to be written to file.
func (array *PdfObjectArray) DefaultWriteString() string {
outStr := "["
var outStr strings.Builder
outStr.WriteString("[")
for ind, o := range *array {
outStr += o.DefaultWriteString()
outStr.WriteString(o.DefaultWriteString())
if ind < (len(*array) - 1) {
outStr += " "
outStr.WriteString(" ")
}
}
outStr += "]"
return outStr
outStr.WriteString("]")
return outStr.String()
}
// Append adds an PdfObject to the array.

View File

@@ -45,7 +45,7 @@ func (parser *PdfParser) repairLocateXref() (int64, error) {
// Useful when the cross reference is pointing to an object with the wrong number.
// Update the table.
func (parser *PdfParser) rebuildXrefTable() error {
newXrefs := XrefTable{}
newXrefs := xRefTable{}
for objNum, xref := range parser.xrefs {
obj, _, err := parser.lookupByNumberWrapper(objNum, false)
if err != nil {
@@ -92,7 +92,7 @@ func parseObjectNumberFromString(str string) (int, int, error) {
// Parse the entire file from top down.
// Goes through the file byte-by-byte looking for "<num> <generation> obj" patterns.
// N.B. This collects the XREF_TABLE_ENTRY data only.
func (parser *PdfParser) repairRebuildXrefsTopDown() (*XrefTable, error) {
func (parser *PdfParser) repairRebuildXrefsTopDown() (*xRefTable, error) {
if parser.repairsAttempted {
// Avoid multiple repairs (only try once).
return nil, fmt.Errorf("repair failed")
@@ -107,7 +107,7 @@ func (parser *PdfParser) repairRebuildXrefsTopDown() (*XrefTable, error) {
bufLen := 20
last := make([]byte, bufLen)
xrefTable := XrefTable{}
xrefTable := xRefTable{}
for {
b, err := parser.reader.ReadByte()
if err != nil {
@@ -164,8 +164,8 @@ func (parser *PdfParser) repairRebuildXrefsTopDown() (*XrefTable, error) {
// Create and insert the XREF entry if not existing, or the generation number is higher.
if curXref, has := xrefTable[objNum]; !has || curXref.generation < genNum {
// Make the entry for the cross ref table.
xrefEntry := XrefObject{}
xrefEntry.xtype = XREF_TABLE_ENTRY
xrefEntry := xRefObject{}
xrefEntry.xtype = xRefTableEntry
xrefEntry.objectNumber = int(objNum)
xrefEntry.generation = int(genNum)
xrefEntry.offset = objOffset

View File

@@ -49,7 +49,6 @@ func IsPrintable(char byte) bool {
}
// IsDelimiter checks if a character represents a delimiter.
// TODO (v3): Unexport.
func IsDelimiter(char byte) bool {
if char == '(' || char == ')' {
return true

View File

@@ -1,3 +0,0 @@
package extractor
var isTesting = false

View File

@@ -1619,33 +1619,6 @@ func (pc *PdfColorspaceLab) ImageToRGB(img Image) (Image, error) {
// ICC Based colors.
// Each component is defined in the range 0.0 - 1.0 where 1.0 is the primary intensity.
/*
type PdfColorICCBased []float64
func NewPdfColorICCBased(vals []float64) *PdfColorICCBased {
color := PdfColorICCBased{}
for _, val := range vals {
color = append(color, val)
}
return &color
}
func (this *PdfColorICCBased) GetNumComponents() int {
return len(*this)
}
// Convert to an integer format.
func (this *PdfColorICCBased) ToInteger(bits int) []uint32 {
maxVal := math.Pow(2, float64(bits)) - 1
ints := []uint32{}
for _, val := range *this {
ints = append(ints, uint32(maxVal*val))
}
return ints
}
*/
// See p. 157 for calculations...
// format [/ICCBased stream]

View File

@@ -157,7 +157,6 @@ func (im *Image) ToGoImage() (goimage.Image, error) {
aidx := 0
samples := im.GetSamples()
//bytesPerColor := colorComponents * int(this.BitsPerComponent) / 8
bytesPerColor := im.ColorComponents
for i := 0; i+bytesPerColor-1 < len(samples); i += bytesPerColor {
var c gocolor.Color

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()
}