Skip to content

Commit fd123c3

Browse files
authored
Merge pull request #36 from romanpickl/supportArrayOfContentStreams
support array of content streams and parse them as a single stream
2 parents da5b752 + a2a84ec commit fd123c3

File tree

1 file changed

+77
-61
lines changed

1 file changed

+77
-61
lines changed

ps.go

Lines changed: 77 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -49,77 +49,93 @@ func newDict() Value {
4949
// in PDF files, such as cmap files that describe the mapping from font code
5050
// points to Unicode code points.
5151
//
52-
// There is no support for executable blocks, among other limitations.
52+
// A stream can also be represented by an array of streams that has to be handled as a single stream
53+
// In the case of a simple stream read only once, otherwise get the length of the stream to handle it properly
5354
//
55+
// There is no support for executable blocks, among other limitations.
5456
func Interpret(strm Value, do func(stk *Stack, op string)) {
55-
rd := strm.Reader()
56-
b := newBuffer(rd, 0)
57-
b.allowEOF = true
58-
b.allowObjptr = false
59-
b.allowStream = false
6057
var stk Stack
6158
var dicts []dict
62-
Reading:
63-
for {
64-
tok := b.readToken()
65-
if tok == io.EOF {
66-
break
59+
s := strm
60+
strmlen := 1
61+
if strm.Kind() == Array {
62+
strmlen = strm.Len()
63+
}
64+
65+
for i := 0; i < strmlen; i++ {
66+
if strm.Kind() == Array {
67+
s = strm.Index(i)
6768
}
68-
if kw, ok := tok.(keyword); ok {
69-
switch kw {
70-
case "null", "[", "]", "<<", ">>":
69+
70+
rd := s.Reader()
71+
72+
b := newBuffer(rd, 0)
73+
b.allowEOF = true
74+
b.allowObjptr = false
75+
b.allowStream = false
76+
77+
Reading:
78+
for {
79+
tok := b.readToken()
80+
if tok == io.EOF {
7181
break
72-
default:
73-
for i := len(dicts) - 1; i >= 0; i-- {
74-
if v, ok := dicts[i][name(kw)]; ok {
75-
stk.Push(Value{nil, objptr{}, v})
76-
continue Reading
82+
}
83+
if kw, ok := tok.(keyword); ok {
84+
switch kw {
85+
case "null", "[", "]", "<<", ">>":
86+
break
87+
default:
88+
for i := len(dicts) - 1; i >= 0; i-- {
89+
if v, ok := dicts[i][name(kw)]; ok {
90+
stk.Push(Value{nil, objptr{}, v})
91+
continue Reading
92+
}
7793
}
94+
do(&stk, string(kw))
95+
continue
96+
case "dict":
97+
stk.Pop()
98+
stk.Push(Value{nil, objptr{}, make(dict)})
99+
continue
100+
case "currentdict":
101+
if len(dicts) == 0 {
102+
panic("no current dictionary")
103+
}
104+
stk.Push(Value{nil, objptr{}, dicts[len(dicts)-1]})
105+
continue
106+
case "begin":
107+
d := stk.Pop()
108+
if d.Kind() != Dict {
109+
panic("cannot begin non-dict")
110+
}
111+
dicts = append(dicts, d.data.(dict))
112+
continue
113+
case "end":
114+
if len(dicts) <= 0 {
115+
panic("mismatched begin/end")
116+
}
117+
dicts = dicts[:len(dicts)-1]
118+
continue
119+
case "def":
120+
if len(dicts) <= 0 {
121+
panic("def without open dict")
122+
}
123+
val := stk.Pop()
124+
key, ok := stk.Pop().data.(name)
125+
if !ok {
126+
panic("def of non-name")
127+
}
128+
dicts[len(dicts)-1][key] = val.data
129+
continue
130+
case "pop":
131+
stk.Pop()
132+
continue
78133
}
79-
do(&stk, string(kw))
80-
continue
81-
case "dict":
82-
stk.Pop()
83-
stk.Push(Value{nil, objptr{}, make(dict)})
84-
continue
85-
case "currentdict":
86-
if len(dicts) == 0 {
87-
panic("no current dictionary")
88-
}
89-
stk.Push(Value{nil, objptr{}, dicts[len(dicts)-1]})
90-
continue
91-
case "begin":
92-
d := stk.Pop()
93-
if d.Kind() != Dict {
94-
panic("cannot begin non-dict")
95-
}
96-
dicts = append(dicts, d.data.(dict))
97-
continue
98-
case "end":
99-
if len(dicts) <= 0 {
100-
panic("mismatched begin/end")
101-
}
102-
dicts = dicts[:len(dicts)-1]
103-
continue
104-
case "def":
105-
if len(dicts) <= 0 {
106-
panic("def without open dict")
107-
}
108-
val := stk.Pop()
109-
key, ok := stk.Pop().data.(name)
110-
if !ok {
111-
panic("def of non-name")
112-
}
113-
dicts[len(dicts)-1][key] = val.data
114-
continue
115-
case "pop":
116-
stk.Pop()
117-
continue
118134
}
135+
b.unreadToken(tok)
136+
obj := b.readObject()
137+
stk.Push(Value{nil, objptr{}, obj})
119138
}
120-
b.unreadToken(tok)
121-
obj := b.readObject()
122-
stk.Push(Value{nil, objptr{}, obj})
123139
}
124140
}
125141

0 commit comments

Comments
 (0)