summaryrefslogtreecommitdiff
path: root/examples/rl2/decodegraphics.py
blob: e2f3a9f1bc8dbcb8c6803caaae87104a31b4ad9e (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# A part of pdfrw (https://github.com/pmaupin/pdfrw)
# Copyright (C) 2006-2009 Patrick Maupin, Austin, Texas
# MIT license -- See LICENSE.txt for details

'''
This file is an example parser that will parse a graphics stream
into a reportlab canvas.

Needs work on fonts and unicode, but works on a few PDFs.

Better to use Form XObjects for most things (see the example in rl1).

'''
from inspect import getargspec

from pdfrw import PdfTokens
from pdfrw.objects import PdfString

#############################################################################
# Graphics parsing


def parse_array(self, token='[', params=None):
    mylist = []
    for token in self.tokens:
        if token == ']':
            break
        mylist.append(token)
    self.params.append(mylist)


def parse_savestate(self, token='q', params=''):
    self.canv.saveState()


def parse_restorestate(self, token='Q', params=''):
    self.canv.restoreState()


def parse_transform(self, token='cm', params='ffffff'):
    self.canv.transform(*params)


def parse_linewidth(self, token='w', params='f'):
    self.canv.setLineWidth(*params)


def parse_linecap(self, token='J', params='i'):
    self.canv.setLineCap(*params)


def parse_linejoin(self, token='j', params='i'):
    self.canv.setLineJoin(*params)


def parse_miterlimit(self, token='M', params='f'):
    self.canv.setMiterLimit(*params)


def parse_dash(self, token='d', params='as'):  # Array, string
    self.canv.setDash(*params)


def parse_intent(self, token='ri', params='n'):
    # TODO: add logging
    pass


def parse_flatness(self, token='i', params='i'):
    # TODO: add logging
    pass


def parse_gstate(self, token='gs', params='n'):
    # TODO: add logging
    # Could parse stuff we care about from here later
    pass


def parse_move(self, token='m', params='ff'):
    if self.gpath is None:
        self.gpath = self.canv.beginPath()
    self.gpath.moveTo(*params)
    self.current_point = params


def parse_line(self, token='l', params='ff'):
    self.gpath.lineTo(*params)
    self.current_point = params


def parse_curve(self, token='c', params='ffffff'):
    self.gpath.curveTo(*params)
    self.current_point = params[-2:]


def parse_curve1(self, token='v', params='ffff'):
    parse_curve(self, token, tuple(self.current_point) + tuple(params))


def parse_curve2(self, token='y', params='ffff'):
    parse_curve(self, token, tuple(params) + tuple(params[-2:]))


def parse_close(self, token='h', params=''):
    self.gpath.close()


def parse_rect(self, token='re', params='ffff'):
    if self.gpath is None:
        self.gpath = self.canv.beginPath()
    self.gpath.rect(*params)
    self.current_point = params[-2:]


def parse_stroke(self, token='S', params=''):
    finish_path(self, 1, 0, 0)


def parse_close_stroke(self, token='s', params=''):
    self.gpath.close()
    finish_path(self, 1, 0, 0)


def parse_fill(self, token='f', params=''):
    finish_path(self, 0, 1, 1)


def parse_fill_compat(self, token='F', params=''):
    finish_path(self, 0, 1, 1)


def parse_fill_even_odd(self, token='f*', params=''):
    finish_path(self, 0, 1, 0)


def parse_fill_stroke_even_odd(self, token='B*', params=''):
    finish_path(self, 1, 1, 0)


def parse_fill_stroke(self, token='B', params=''):
    finish_path(self, 1, 1, 1)


def parse_close_fill_stroke_even_odd(self, token='b*', params=''):
    self.gpath.close()
    finish_path(self, 1, 1, 0)


def parse_close_fill_stroke(self, token='b', params=''):
    self.gpath.close()
    finish_path(self, 1, 1, 1)


def parse_nop(self, token='n', params=''):
    finish_path(self, 0, 0, 0)


def finish_path(self, stroke, fill, fillmode):
    if self.gpath is not None:
        canv = self.canv
        canv._fillMode, oldmode = fillmode, canv._fillMode
        canv.drawPath(self.gpath, stroke, fill)
        canv._fillMode = oldmode
        self.gpath = None


def parse_clip_path(self, token='W', params=''):
    # TODO: add logging
    pass


def parse_clip_path_even_odd(self, token='W*', params=''):
    # TODO: add logging
    pass


def parse_stroke_gray(self, token='G', params='f'):
    self.canv.setStrokeGray(*params)


def parse_fill_gray(self, token='g', params='f'):
    self.canv.setFillGray(*params)


def parse_stroke_rgb(self, token='RG', params='fff'):
    self.canv.setStrokeColorRGB(*params)


def parse_fill_rgb(self, token='rg', params='fff'):
    self.canv.setFillColorRGB(*params)


def parse_stroke_cmyk(self, token='K', params='ffff'):
    self.canv.setStrokeColorCMYK(*params)


def parse_fill_cmyk(self, token='k', params='ffff'):
    self.canv.setFillColorCMYK(*params)

#############################################################################
# Text parsing


def parse_begin_text(self, token='BT', params=''):
    assert self.tpath is None
    self.tpath = self.canv.beginText()


def parse_text_transform(self, token='Tm', params='ffffff'):
    path = self.tpath

    # Stoopid optimization to remove nop
    try:
        code = path._code
    except AttributeError:
        pass
    else:
        if code[-1] == '1 0 0 1 0 0 Tm':
            code.pop()

    path.setTextTransform(*params)


def parse_setfont(self, token='Tf', params='nf'):
    fontinfo = self.fontdict[params[0]]
    self.tpath._setFont(fontinfo.name, params[1])
    self.curfont = fontinfo


def parse_text_out(self, token='Tj', params='t'):
    text = params[0].decode(self.curfont.remap, self.curfont.twobyte)
    self.tpath.textOut(text)


def parse_TJ(self, token='TJ', params='a'):
    remap = self.curfont.remap
    twobyte = self.curfont.twobyte
    result = []
    for x in params[0]:
        if isinstance(x, PdfString):
            result.append(x.decode(remap, twobyte))
        else:
            # TODO: Adjust spacing between characters here
            int(x)
    text = ''.join(result)
    self.tpath.textOut(text)


def parse_end_text(self, token='ET', params=''):
    assert self.tpath is not None
    self.canv.drawText(self.tpath)
    self.tpath = None


def parse_move_cursor(self, token='Td', params='ff'):
    self.tpath.moveCursor(params[0], -params[1])


def parse_set_leading(self, token='TL', params='f'):
    self.tpath.setLeading(*params)


def parse_text_line(self, token='T*', params=''):
    self.tpath.textLine()


def parse_set_char_space(self, token='Tc', params='f'):
    self.tpath.setCharSpace(*params)


def parse_set_word_space(self, token='Tw', params='f'):
    self.tpath.setWordSpace(*params)


def parse_set_hscale(self, token='Tz', params='f'):
    self.tpath.setHorizScale(params[0] - 100)


def parse_set_rise(self, token='Ts', params='f'):
    self.tpath.setRise(*params)


def parse_xobject(self, token='Do', params='n'):
    # TODO: Need to do this
    pass


class FontInfo(object):
    ''' Pretty basic -- needs a lot of work to work right for all fonts
    '''
    lookup = {
                # WRONG -- have to learn about font stuff...
                'BitstreamVeraSans': 'Helvetica',
             }

    def __init__(self, source):
        name = source.BaseFont[1:]
        self.name = self.lookup.get(name, name)
        self.remap = chr
        self.twobyte = False
        info = source.ToUnicode
        if not info:
            return
        info = info.stream.split('beginbfchar')[1].split('endbfchar')[0]
        info = list(PdfTokens(info))
        assert not len(info) & 1
        info2 = []
        for x in info:
            assert x[0] == '<' and x[-1] == '>' and len(x) in (4, 6), x
            i = int(x[1:-1], 16)
            info2.append(i)
        self.remap = dict((x, chr(y)) for (x, y) in
                          zip(info2[::2], info2[1::2])).get
        self.twobyte = len(info[0]) > 4

#############################################################################
# Control structures


def findparsefuncs():

    def checkname(n):
        assert n.startswith('/')
        return n

    def checkarray(a):
        assert isinstance(a, list), a
        return a

    def checktext(t):
        assert isinstance(t, PdfString)
        return t

    fixparam = dict(f=float, i=int, n=checkname, a=checkarray,
                    s=str, t=checktext)
    fixcache = {}

    def fixlist(params):
        try:
            result = fixcache[params]
        except KeyError:
            result = tuple(fixparam[x] for x in params)
            fixcache[params] = result
        return result

    dispatch = {}
    expected_args = 'self token params'.split()
    for key, func in globals().items():
        if key.startswith('parse_'):
            args, varargs, keywords, defaults = getargspec(func)
            assert (args == expected_args and varargs is None and
                    keywords is None and len(defaults) == 2), (
                    key, args, varargs, keywords, defaults)
            token, params = defaults
            if params is not None:
                params = fixlist(params)
            value = func, params
            assert dispatch.setdefault(token, value) is value, repr(token)
    return dispatch


class _ParseClass(object):
    dispatch = findparsefuncs()

    @classmethod
    def parsepage(cls, page, canvas=None):
        self = cls()
        contents = page.Contents
        if contents.Filter is not None:
            raise SystemExit('Cannot parse graphics -- page encoded with %s'
                             % contents.Filter)
        dispatch = cls.dispatch.get
        self.tokens = tokens = iter(PdfTokens(contents.stream))
        self.params = params = []
        self.canv = canvas
        self.gpath = None
        self.tpath = None
        self.fontdict = dict((x, FontInfo(y)) for
                             (x, y) in page.Resources.Font.iteritems())

        for token in self.tokens:
            info = dispatch(token)
            if info is None:
                params.append(token)
                continue
            func, paraminfo = info
            if paraminfo is None:
                func(self, token, ())
                continue
            delta = len(params) - len(paraminfo)
            if delta:
                if delta < 0:
                    print ('Operator %s expected %s parameters, got %s' %
                           (token, len(paraminfo), params))
                    params[:] = []
                    continue
                else:
                    print ("Unparsed parameters/commands: %s" % params[:delta])
                del params[:delta]
            paraminfo = zip(paraminfo, params)
            try:
                params[:] = [x(y) for (x, y) in paraminfo]
            except:
                for i, (x, y) in enumerate(paraminfo):
                    try:
                        x(y)
                    except:
                        raise  # For now
                    continue
            func(self, token, params)
            params[:] = []


def debugparser(undisturbed=set('parse_array'.split())):
    def debugdispatch():
        def getvalue(oldval):
            name = oldval[0].__name__

            def myfunc(self, token, params):
                print ('%s called %s(%s)' % (token, name,
                       ', '.join(str(x) for x in params)))
            if name in undisturbed:
                myfunc = oldval[0]
            return myfunc, oldval[1]
        return dict((x, getvalue(y))
                    for (x, y) in _ParseClass.dispatch.iteritems())

    class _DebugParse(_ParseClass):
        dispatch = debugdispatch()

    return _DebugParse.parsepage

parsepage = _ParseClass.parsepage

if __name__ == '__main__':
    import sys
    from pdfreader import PdfReader
    parse = debugparser()
    fname, = sys.argv[1:]
    pdf = PdfReader(fname)
    for i, page in enumerate(pdf.pages):
        print ('\nPage %s ------------------------------------' % i)
        parse(page)