-
Notifications
You must be signed in to change notification settings - Fork 67
/
pdfsizeopt_test.py
executable file
·2212 lines (2110 loc) · 105 KB
/
pdfsizeopt_test.py
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /bin/sh
""":" # pdfsizeopt_test: Tests for pdfsizeopt.
type -p python2.7 >/dev/null 2>&1 && exec python2.7 -- "$0" ${1+"$@"}
type -p python2.6 >/dev/null 2>&1 && exec python2.6 -- "$0" ${1+"$@"}
type -p python2.5 >/dev/null 2>&1 && exec python2.5 -- "$0" ${1+"$@"}
type -p python2.4 >/dev/null 2>&1 && exec python2.4 -- "$0" ${1+"$@"}
exec python -- "$0" ${1+"$@"}
This is a Python 2.x script, it works with Python 2.4, 2.5, 2.6 and 2.7. It
doesn't work with Python 3.x. Feel free to replace the #! line with
`#! /usr/bin/python', `#! /usr/bin/env python' or whatever suits you best.
"""
#
# pdfsizeopt_test.py: unit tests for pdfsizeopt.py
# by pts@fazekas.hu at Sun Apr 19 10:21:07 CEST 2009
#
__author__ = 'pts@fazekas.hu (Peter Szabo)'
# --- Setting up the import path.
import os
import os.path
import sys
if not ((2, 4) <= sys.version_info[:2] < (3, 0)):
sys.stderr.write(
'fatal: Python version 2.4, 2.5, 2.6 or 2.7 needed for: %s\n' % __file__)
sys.exit(1)
if os.path.isfile(os.path.join(
os.path.dirname(__file__), 'lib', 'pdfsizeopt', 'main.py')):
sys.path[:0] = [os.path.join(os.path.dirname(__file__), 'lib')]
# ---
import sys
import zlib
import unittest
from pdfsizeopt import cff
from pdfsizeopt import float_util
from pdfsizeopt import main
class PdfSizeOptTest(unittest.TestCase):
def assertRaisesX(self, exc_class, callable_obj, *args, **kwargs):
"""Like assertRaises, but rejects subclasses."""
try:
callable_obj(*args, **kwargs)
except exc_class, e:
# type(e) doesn't work instead of e.__class__ in Python 2.4. In Python
# >=2.5 they work equivalently.
if e.__class__ != exc_class: # True if exc_class is a superclass.
raise
return
self.fail('%s not raised.' % exc_class.__name__)
def testAssertRaisesX(self):
class C(ZeroDivisionError): pass
def RaiseC():
raise C()
self.assertRaisesX(ZeroDivisionError, lambda: 1 / 0)
self.assertRaisesX(C, RaiseC)
self.assertRaises(ZeroDivisionError, RaiseC)
try:
self.assertRaisesX(ZeroDivisionError, RaiseC)
self.fail('C was caught.')
except C:
pass
try:
self.assertRaisesX(C, lambda: 1 / 0)
self.fail('ZeroDivisionError was caught.')
except ZeroDivisionError:
pass
def testSerializePdfStringUnsafeAndParsePdfString(self):
e = main.PdfObj.SerializePdfStringUnsafe
p = lambda *args: main.PdfObj.ParsePdfString(*args)[0]
def Check(pdf_string_literal, data):
self.assertEqual(pdf_string_literal, e(data))
self.assertEqual(data, p(buffer(pdf_string_literal)))
def CheckParse(pdf_string_literal, data):
self.assertEqual(data, p(buffer(pdf_string_literal)))
Check('()', '')
Check('(Hello, World!)', 'Hello, World!')
Check('(\\\\Hello, \\(World!)', '\\Hello, (World!')
Check('(Hello, \\)World!\\\\)', 'Hello, )World!\\')
Check('(hi\\r)', 'hi\r')
Check('(hi\n)', 'hi\n')
Check('(hi\\r\n)', 'hi\r\n')
Check('(hir%\'"a)', 'hir%\'"a')
Check('(//)', '//')
Check('(\\r\\()', '\r(')
Check('(\\(\\\\\\r)', '(\\\r')
CheckParse('(hi\\\nr\\%\\\'\\"\\a)', 'hir%\'"a')
CheckParse('(hi\\\r\nx)', 'hix')
CheckParse('(hi\\\r\\nx)', 'hi\nx') # Same as in PostScript.
CheckParse('(hi\\n\\r\\t\\b\\f\\400\\500\\600\\700)',
'hi\n\r\t\b\f\00400\00500\00600\00700')
CheckParse('(hi\\08\\18\\28\\38\\079\\179\\279\\379)',
'hi\0008\0018\0028\0038\0079\0179\0279\0379')
CheckParse('(hi\\076\\154\\232\\310\\0123\\1234\\2345\\3000)',
'hi\076\154\232\310\0123\1234\2345\3000')
CheckParse('<>', '')
CheckParse('<\t\f>', '')
CheckParse('<fA3>', '\xfa\x30')
CheckParse('(\\0576\\057)', '/6/')
s = ''.join([c for c in map(chr, xrange(255, -1, -1)) if c not in '()\\\r'])
Check('(%s)' % s, s)
Check('(Hello, \\)\\(Wo\\\\rld!)', 'Hello, )(Wo\\rld!')
Check('((((foo\\\\))))', '(((foo\\)))')
Check('(((foo)) (\\(bar)d)', '((foo)) ((bar)d')
Check('((foo)\\) (bar))', '(foo)) (bar)')
# We escape \r\n as \\r\n, to prevent it from being parsed as just \n.
Check('(\nbar\\rbaz\\r\nquux\n\\rfoo)',
'\nbar\rbaz\r\nquux\n\rfoo')
self.assertRaisesX(TypeError, p, None)
self.assertRaisesX(TypeError, p, 42)
self.assertRaisesX(main.PdfTokenNotString, p, '/foo')
self.assertRaisesX(main.PdfTokenTruncated, p, '(')
self.assertRaisesX(main.PdfTokenTruncated, p, '(foo')
self.assertRaisesX(main.PdfTokenTruncated, p, '<f00')
self.assertRaisesX(main.PdfTokenTruncated, p, '(foo\\')
self.assertRaisesX(main.PdfTokenTruncated, p, '()', 2)
self.assertEqual('', p('?()/', 1, 3))
self.assertRaisesX(main.PdfTokenParseError, p, '()/', 0, 3)
self.assertRaisesX(main.PdfTokenParseError, p, '(\\n)/', 0, 5)
self.assertEqual(('', 2), main.PdfObj.ParsePdfString(
'()/', 0, 3, is_partial_ok=True))
self.assertEqual(('\n', 5), main.PdfObj.ParsePdfString(
'?(\\n)/', 1, 5, is_partial_ok=True))
self.assertRaisesX(ValueError, p, '()', 3) # Bad offsets.
self.assertEqual(('', 2), main.PdfObj.ParsePdfString(
'<>>>>>', is_partial_ok=True))
self.assertEqual(('', 3), main.PdfObj.ParsePdfString(
'?<>>>>>', start=1, is_partial_ok=True))
self.assertEqual(('\x8d\x50', 5), main.PdfObj.ParsePdfString(
'<8d5>]/', is_partial_ok=True))
self.assertRaisesX(main.PdfTokenTruncated, main.PdfObj.ParsePdfString,
'<8d5', is_partial_ok=True)
self.assertRaisesX(main.PdfTokenParseError, main.PdfObj.ParsePdfString,
'<6?', is_partial_ok=True)
self.assertRaisesX(main.PdfTokenParseError, main.PdfObj.ParsePdfString,
'<\n3\t1\r4f5C5]>')
self.assertRaisesX(main.PdfTokenParseError, main.PdfObj.ParsePdfString,
'<\n3\t1\r4f5C5]')
self.assertRaisesX(main.PdfTokenTruncated, main.PdfObj.ParsePdfString,
'<\n3\t1\r4f5C5')
def testRewriteToParsable(self):
e = main.PdfObj.RewriteToParsable
self.assertEqual(' [ ]', e('[]'))
self.assertEqual(' [ ]', e('[]<<'))
self.assertEqual(' true', e('true '))
self.assertEqual(' true', e('true'))
self.assertRaisesX(main.PdfTokenParseError, e, 'hi ')
eo = []
self.assertEqual(' false', e('\n\t\r \f\0false true ', end_ofs_out=eo))
self.assertEqual([11], eo)
self.assertEqual(' << true false null >>',
e('% hi\r<<%\ntrue false null>>baz'))
self.assertEqual(' << true >>',
e('<<true>>baz'))
self.assertEqual(' [ [ << [ << << >> >> ] >> ] ]',
e('[[<<[<<<<>>>>]>>]]'))
self.assertRaisesX(main.PdfTokenParseError,
e, '[[<<[<<<<>>]>>>>]]')
self.assertRaisesX(main.PdfTokenTruncated, e, '\t \n% foo')
self.assertRaisesX(main.PdfTokenTruncated, e, ' [\t')
self.assertRaisesX(main.PdfTokenTruncated, e, '\n<\f')
self.assertRaisesX(main.PdfTokenTruncated, e, '\t<<\n\r')
self.assertRaisesX(main.PdfTokenTruncated, e, '[<<')
self.assertRaisesX(main.PdfTokenParseError, e, '[<<]')
self.assertRaisesX(main.PdfTokenParseError, e, '[>>]')
self.assertEqual(' <>', e('()'))
self.assertEqual(' <>', e('<>'))
self.assertRaisesX(main.PdfTokenTruncated, e, '<<')
self.assertRaisesX(main.PdfTokenParseError, e, '>>')
self.assertRaisesX(main.PdfTokenTruncated, e, '[')
self.assertRaisesX(main.PdfTokenParseError, e, ']')
self.assertRaisesX(main.PdfTokenTruncated, e, '(foo')
self.assertRaisesX(main.PdfTokenTruncated, e, '(foo\\)bar')
self.assertEqual(' <face654389210b7d>', e('< f\nAc\tE\r654389210B7d\f>'))
self.assertEqual(' <48656c6c6f2c20576f726c6421>', e('(Hello, World!)'))
self.assertEqual(' <2828666f6f2929296261725c>', e('(((foo))\\)bar\\\\)'))
self.assertEqual(' <410a420a430a440a0a45>', e('(A\rB\nC\r\nD\n\rE)'))
self.assertEqual(' <0a280d2900>', e('(\\n(\\r)\\0)'))
self.assertEqual(' <0a280a2900780a790a0a7a>', e('(\n(\r)\0x\r\ny\n\rz)'))
self.assertEqual(' <466f6f42617242617a>', e('(Foo\\\nBar\\\rBaz)'))
self.assertEqual(' <466f6f4261720a42617a>', e('(Foo\\\r\nBar\\\n\rBaz)'))
self.assertEqual(' <2829%s>' % ''.join(['%02x' % {13: 10}.get(i, i)
for i in xrange(33)]),
e('(()%s)' % ''.join(map(chr, xrange(33)))))
self.assertEqual(' <face422829>', e('(\xfa\xCE\x42())'))
self.assertEqual(' <00210023>', e('(\0!\\0#)'))
self.assertEqual(' <073839380a>', e('(\78\98\12)'))
self.assertEqual(' <053031>', e('(\\501)'))
self.assertEqual(' <0a0a09080c>', e('(\n\r\t\b\f)'))
self.assertEqual(' <0a0d09080c>', e('(\\n\\r\\t\\b\\f)'))
self.assertEqual(' <236141>', e('(\\#\\a\\A)'))
self.assertEqual(' <61275c>', e("(a'\\\\)"))
self.assertEqual(' <314f5c60>', e('<\n3\t1\r4f5C6 >'))
self.assertEqual(' <0006073839050e170338043805380638073838380a3913391f39>',
e('(\0\6\7\8\9\05\16\27\38\48\58\68\78\88\129\239\379)'))
self.assertEqual(' <666f6f0a626172>', e('(foo\nbar)'))
self.assertEqual(' <666f6f0a626172>', e('(foo\\nbar)'))
self.assertEqual(' <666f6f626172>', e('(foo\\\nbar)'))
self.assertEqual(' <0006073839050e170338043805380638073838380a3913391f39'
'043031053031063031073031>',
e('(\\0\\6\\7\\8\\9\\05\\16\\27\\38\\48\\58\\68\\78\\88'
'\\129\\239\\379\\401\\501\\601\\701)'))
# PDF doesn't have \x
self.assertEqual(' <786661786243784445f8>', e('(\\xfa\\xbC\\xDE\xF8)'))
self.assertEqual(' 0', e('.'))
self.assertEqual(' 42', e('42'))
self.assertEqual(' 42', e('42 '))
self.assertEqual(' 0', e('00000 '))
self.assertEqual(' 0', e('+00000 '))
self.assertEqual(' 0', e('-00000 '))
self.assertEqual(' 0', e('00000.000 '))
self.assertEqual(' 0', e('+00000.000 '))
self.assertEqual(' 0', e('-00000.000 '))
self.assertEqual(' 12', e('00012 '))
self.assertEqual(' 12', e('+00012 '))
self.assertEqual(' -12', e('-00012 '))
self.assertEqual(' 12', e('00012. '))
self.assertEqual(' 12', e('00012.000 '))
self.assertEqual(' 12', e('+00012.000 '))
self.assertEqual(' -12', e('-12.000 '))
self.assertEqual(' 12.34', e('00012.340 '))
self.assertEqual(' 12.34', e('+00012.34 '))
self.assertEqual(' -12.34', e('-12.340 '))
self.assertEqual(' [ .34 -.34 .34 ]', e('[.34 -.34 +.34]'))
self.assertEqual(' [ .34 -.34 .34 ]', e('[00.34 -00.34 +00.34]'))
self.assertEqual(' [ 34 -34 34 ]', e('[34. -34. +34.]'))
self.assertEqual(' [ 34 -34 34 ]', e('[0034. -0034. +0034.]'))
self.assertEqual(' [ 34 -34 34 ]', e('[34.00 -34.00 +34.00]'))
self.assertEqual(' [ 34 -34 34 ]', e('[0034.00 -0034.00 +0034.00]'))
self.assertEqual(' [ 0 0 0 ]', e('[0. -0. +0.]'))
self.assertEqual(' [ 0 0 0 ]', e('[000. -000. +000.]'))
self.assertEqual(' [ 0 0 0 ]', e('[.0 -.0 +.0]'))
self.assertEqual(' [ 0 0 0 ]', e('[.000 -.000 +.000]'))
self.assertEqual(' [ 0 0 0 ]', e('[00.000 -00.000 +00.000]'))
self.assertEqual(' 12 345 R', e(' 12 345 R '))
end_ofs_out = []
self.assertEqual(' 5', e(' 5 endobj\t', end_ofs_out=end_ofs_out))
self.assertEqual([2], end_ofs_out)
end_ofs_out = []
self.assertEqual(' 5 endobz',
e(' 5 endobz\t',
end_ofs_out=end_ofs_out, do_terminate_obj=True))
self.assertEqual([10], end_ofs_out)
self.assertRaisesX(main.PdfTokenParseError, e, '/#')
self.assertRaisesX(main.PdfTokenTruncated, e, '/')
self.assertRaisesX(main.PdfTokenTruncated, e, '/ ')
# TODO(pts): PdfTokenParseError instead?
self.assertRaisesX(main.PdfTokenTruncated, e, '/%')
self.assertEqual(' /#2A', e('/*'))
self.assertEqual(' /STROZ#2F', e('/STR#4fZ#2f'))
self.assertEqual(' /STROZ', e('/STR#4FZ\r\n\t\t\t \t'))
end_ofs_out = []
self.assertEqual(' 5 /STROZ hi',
e(' 5 /STR#4FZ hi\r\n\t\t\t \t',
end_ofs_out=end_ofs_out, do_terminate_obj=True))
self.assertEqual([16], end_ofs_out)
end_ofs_out = []
self.assertRaisesX(main.PdfTokenParseError, e,
' 5 STR#4FZ\r\n\t\t\t \t',
end_ofs_out=end_ofs_out, do_terminate_obj=True)
end_ofs_out = []
self.assertEqual(' /Size', e('/Size 42 ', end_ofs_out=end_ofs_out))
self.assertEqual([5], end_ofs_out)
self.assertEqual(' [ /Size 42 ]', e('[/Size 42]'))
self.assertEqual(' [ true 42 ]', e('[true\n%korte\n42]'))
self.assertEqual(' [ true 42 ]', e('[true%korte\n42]'))
self.assertRaisesX(main.PdfTokenParseError, e, 'hello \n\t world\n\t')
self.assertEqual(' null', e('null \n\t false\n\t'))
# This is invalid PDF (null is not a name), but we don't catch it.
self.assertEqual(' << null false >>', e('\r<<null \n\t false\n\t>>'))
self.assertEqual(' << true >>', e('<<true>>'))
self.assertEqual(' true foo', e('true foo bar', do_terminate_obj=True))
self.assertRaisesX(main.PdfTokenTruncated, e,
'true foo', do_terminate_obj=True)
self.assertEqual(' <68656c296c6f0a0877286f72296c64>',
e('(hel\)lo\n\bw(or)ld)'))
self.assertRaisesX(main.PdfTokenTruncated, e, '(hel\)lo\n\bw(orld)')
self.assertEqual(' [ <68656c296c6f0a0877286f72296c64> ]',
e(' [ (hel\\051lo\\012\\010w\\050or\\051ld) ]<'))
self.assertRaisesX(main.PdfTokenTruncated, e, '>')
self.assertRaisesX(main.PdfTokenTruncated, e, '<')
self.assertRaisesX(main.PdfTokenTruncated, e, '< ')
self.assertRaisesX(main.PdfTokenParseError, e, '< <')
self.assertRaisesX(main.PdfTokenParseError, e, '> >')
self.assertRaisesX(main.PdfTokenParseError, e, '[ >>')
self.assertRaisesX(main.PdfTokenTruncated, e,
'[ (hel\\)lo\\n\\bw(or)ld) <')
self.assertRaisesX(main.PdfTokenTruncated, e, '<\n3\t1\r4f5C5')
self.assertRaisesX(main.PdfTokenParseError, e, '<\n3\t1\r4f5C5]>')
self.assertRaisesX(main.PdfTokenTruncated, e, '')
self.assertRaisesX(main.PdfTokenTruncated, e, '%hello')
# 'stream\r' is truncated, we're waiting for 'stream\r\n'.
self.assertRaisesX(main.PdfTokenTruncated, e,
'<<>>stream\r', do_terminate_obj=True)
self.assertEqual(' << >> blah', e('<<>>blah\r', do_terminate_obj=True))
self.assertEqual(' << >> stream', e('<<>>stream\n', do_terminate_obj=True))
self.assertEqual(' << >> stream',
e('<<>>stream\r\n', do_terminate_obj=True))
self.assertEqual(' << /Type /Catalog /Pages 3 0 R >>',
e('<</Type /Catalog /Pages 3 0 R\n>>'))
self.assertEqual(' 42', e(' 42 true R'))
eo = []
self.assertEqual(' 442 43 R', e('\t442%foo\r43\fR ', end_ofs_out=eo))
self.assertEqual(13, eo[0]) # spaces not included
eo = []
self.assertEqual(' 442 43 R', e('\t442%foo\r43\fR/', end_ofs_out=eo))
self.assertEqual(' 442 43 R', e('\t+442%foo\r+43\fR/', end_ofs_out=eo))
self.assertEqual(13, eo[0]) # spaces not included
self.assertEqual(' << /Pages -333 -1 R >>', e('<</Pages -333 -1 R\n>>'))
self.assertEqual(' << /Pages 0 55 R >>', e('<</Pages 0 55 R\n>>'))
def testSerializeDict(self):
# Toplevel whitespace is removed, but the newline inside the /DecodeParms
# value is kept.
self.assertEqual(
'<</BitsPerComponent 8/ColorSpace/DeviceGray/DecodeParms'
'<</Predictor 15\n/Columns 640>>/Filter/FlateDecode/Height 480'
'/Length 6638/Subtype/Image/Width 640/Z true>>',
main.PdfObj.SerializeDict(main.PdfObj.ParseSimplestDict(
'<</Subtype/Image\n/ColorSpace/DeviceGray\n/Width 640/Z true\n'
'/Height 480\n/BitsPerComponent 8\n/Filter/FlateDecode\n'
'/DecodeParms <</Predictor 15\n/Columns 640>>/Length 6638>>')))
def testIsGrayColorSpace(self):
e = main.PdfObj.IsGrayColorSpace
self.assertEqual(False, e('/DeviceRGB'))
self.assertEqual(False, e('/DeviceCMYK'))
self.assertEqual(False, e('/DeviceN'))
self.assertEqual(True, e(' /DeviceGray \r'))
self.assertEqual(False, e('\t[ /Indexed /DeviceGray'))
self.assertEqual(True, e('\t[ /Indexed /DeviceGray 5 <]'))
self.assertRaisesX(main.PdfTokenTruncated, e,
'\t[ /Indexed /DeviceRGB 5 (]')
self.assertEqual(True, e('\t[ /Indexed /DeviceRGB\f5 (A\101Azz\172)]'))
self.assertEqual(False, e('\t[ /Indexed\n/DeviceRGB 5 (A\101Bzz\172)]'))
self.assertEqual(False, e('\t[ /Indexed\n/DeviceRGB 5 (A\101Ayy\172)]'))
def testParseSimpleValue(self):
e = main.PdfObj.ParseSimpleValue
self.assertRaisesX(main.PdfTokenParseError, e, '')
self.assertRaisesX(main.PdfTokenParseError, e, '%hello\nworld')
self.assertRaisesX(main.PdfTokenParseError, e, 'win') # Unknown keyword.
self.assertRaisesX(main.PdfTokenParseError, e, 'foo bar')
self.assertRaisesX(main.PdfTokenParseError, e, '/foo bar')
self.assertEqual(True, e('true'))
self.assertEqual(False, e('false'))
self.assertEqual(True, e('true '))
self.assertEqual(False, e('\nfalse'))
self.assertEqual('[foo bar]', e('\f[foo bar]\n\r'))
self.assertEqual('<<foo bar\tbaz>>', e('\f<<foo bar\tbaz>>\n\r'))
self.assertEqual(None, e('null'))
self.assertEqual(0, e('0'))
self.assertEqual(42, e('42'))
self.assertEqual(42, e('00042'))
self.assertEqual(-137, e('-0137'))
self.assertEqual(137, e('+0137'))
self.assertEqual('3.14', e('3.14'))
self.assertEqual('5', e('5.'))
self.assertEqual('.5', e('+.5'))
self.assertEqual('0', e('.'))
self.assertEqual('0', e('-.'))
self.assertRaisesX(main.PdfTokenParseError, e, '5e6')
self.assertEqual('<48493f>', e('(HI?)'))
self.assertEqual('<28295c>', e('(()\\\\)'))
self.assertRaisesX(main.PdfTokenParseError, e, '(()\\\\)x')
self.assertRaisesX(main.PdfTokenTruncated, e, '(()\\\\')
self.assertEqual('<deadface>', e('<dea dF aCe>'))
self.assertEqual('<deadfac0>', e('<\fdeadFaC\r>'))
self.assertEqual('42 0 R', e('42\f\t0 \nR'))
self.assertRaisesX(main.PdfTokenParseError, e, '42 R')
self.assertRaisesX(main.PdfTokenParseError, e, 'foo#')
self.assertRaisesX(main.PdfTokenParseError, e, 'foo#a')
self.assertRaisesX(main.PdfTokenParseError, e, 'foo#ab')
self.assertEqual('/foo', e('/foo'))
self.assertRaisesX(main.PdfTokenParseError, e, '/foo#')
self.assertRaisesX(main.PdfTokenParseError, e, '/foo#a')
self.assertEqual('/foo#AB', e('/foo#ab'))
self.assertEqual('/foo42', e('/foo#34#32'))
self.assertEqual('/foo#2A', e('/foo*')) # Normalized.
self.assertEqual('/#FAce#5BB', e('/\xface#5b#42\f'))
def DoTestParseSimplestDict(self, e):
# e is either ParseSimplestDict or ParseDict, so (because the latter)
# this method should not test for PdfTokenNotSimplest.
self.assertEqual({}, e('<<\t\0>>'))
self.assertEqual({}, e('<<\n>>'))
self.assertEqual({'One': '/Two'}, e('<< /One/Two>>'))
self.assertEqual({'One': '2.2'}, e('<</One +2.2>>'))
self.assertRaisesX(main.PdfTokenParseError, e, '<</One Two>>')
self.assertEqual({'One': 234}, e('<</One 234>>'))
self.assertRaisesX(main.PdfTokenParseError, e,
'<</One/Two/Three Four/Five/Six>>')
self.assertEqual({'Five': '/Six', 'Three': '-4', 'One': '/Two'},
e('<</One/Two/Three -4.000/Five/Six>>'))
self.assertEqual({'A': True, 'C': None, 'B': False, 'E': '42.5', 'D': 42},
e('<<\n\r/A true/B\f\0false/C null/D\t42/E 42.5\r>>'))
self.assertEqual({'Data': '42 137 R'}, e('<</Data 42\t\t137\nR >>'))
self.assertEqual({'S': '<68656c6c6f2c20776f726c6421>'},
e('<</S(hello, world!)>>'))
self.assertEqual({'S': '<>'}, e('<</S()>>'))
self.assertEqual({'S': '<0a>'}, e('<</S\r(\n)>>'))
self.assertEqual({'S': '<3c3c5d3e3e5b>'}, e('<</S (<<]>>[)\n>>'))
self.assertEqual({'S': '<deadface50>'}, e('<</S<dEA Dfa CE5>>>'))
self.assertEqual({'A': '[42 \t?Foo>><<]'}, e('<</A[42 \t?Foo>><<]>>'))
self.assertEqual(
{'D': '<<]42[\f\t?Foo>>'}, e('<<\n/D\n<<]42[\f\t?Foo>>>>'))
self.assertRaisesX(main.PdfTokenParseError, e, '<</S<%\n>>>')
def testParseSimplestDict(self):
e = main.PdfObj.ParseSimplestDict
self.DoTestParseSimplestDict(e=e)
self.assertRaisesX(main.PdfTokenNotSimplest,
e, '<</Three[/Four()]>>')
self.assertRaisesX(main.PdfTokenNotSimplest, e, '<</S\r(\\n)>>')
self.assertRaisesX(main.PdfTokenNotSimplest, e, '<</A[()]>>')
self.assertRaisesX(main.PdfTokenNotSimplest, e, '<</A[%\n]>>')
self.assertRaisesX(main.PdfTokenNotSimplest, e, '<</D<<()>>>>')
self.assertRaisesX(main.PdfTokenNotSimplest, e, '<</D<<%\n>>>>')
self.assertRaisesX(main.PdfTokenNotSimplest, e, '<</?Answer! 42>>')
def testParseDict(self):
e = main.PdfObj.ParseDict
self.DoTestParseSimplestDict(e=e)
self.assertEqual({}, e('<<\0\r>>'))
self.assertEqual({}, e('<<\n>>'))
self.assertRaisesX(main.PdfTokenParseError, e, '<<')
self.assertEqual({'I': 5}, e('<</I%\n5>>'))
self.assertEqual({'N': '/Foo-42+_'}, e('<</N/Foo-42+_>>'))
self.assertEqual({'N': '/Foo-42+_#2A'}, e('<</N/Foo-42+_*>>'))
self.assertEqual({'N': '/Foo-42+_#2Ab'}, e('<</N/Foo-42+_#2ab>>'))
self.assertEqual({'Five': '/Six', 'Three': '[/Four]', 'One': '/Two'},
e('<</One/Two/Three[/Four]/Five/Six>>'))
self.assertRaisesX(main.PdfTokenParseError, e, '<</Foo bar#3f>>')
self.assertEqual({'#3FAnswer#21#20#0D': 42}, e('<</?Answer!#20#0d 42>>'))
self.assertEqual({'S': '<0a>'}, e('<</S\r(\\n)>>'))
self.assertRaisesX(main.PdfTokenParseError, e, '<</S\r(foo\\)>>')
self.assertEqual({'S': '<666f6f29626172>'}, e('<</S(foo\\)bar)>>'))
self.assertEqual(
{'S': '<2829>', 'T': '<42cab0>'}, e('<</S(())/T<42c Ab>>>'))
self.assertEqual({'S': '<282929285c>', 'T': 8},
e('<</S(()\\)\\(\\\\)/T 8>>'))
self.assertEqual({'A': '[\f()]'}, e('<</A[\f()]>>'))
self.assertEqual({'A': '[\t5 \r6\f]'}, e('<</A[\t5 \r6\f]>>'))
self.assertEqual({'A': '[12 34]'}, e('<</A[12%\n34]>>'))
# \t removed because there was a comment in the array
self.assertEqual({'A': '[]'}, e('<</A[\t%()\n]>>'))
self.assertEqual({'A': '<2829>', 'B': '[<<]'}, e('<</A(())/B[<<]>>'))
self.assertRaisesX(main.PdfTokenParseError, e, '<</A>>')
self.assertRaisesX(main.PdfTokenParseError, e, '<<5/A>>')
self.assertRaisesX(main.PdfTokenParseError, e, '<</A(())/B[()<<]>>')
self.assertRaisesX(main.PdfTokenParseError, e, '<</A[[>>]]>>')
self.assertEqual({'A': '[[/hi 5]/lah]'}, e('<</A[[/hi%x\t]z\r5] /lah]>>'))
self.assertEqual({'D': '<<()\t<>>>'}, e('<</D<<()\t<>>>>>'))
self.assertEqual({'D': '<<>>', 'S': '<>'}, e('<</D<<%\n>>/S()>>'))
# \t and \f removed because there was a comment in the dict
self.assertEqual({'D': '<</E<<>>>>'}, e('<</D<</E\t\f<<%>>\n>>>>>>'))
self.assertEqual({'A': '[[]]', 'q': '56 78 R'},
e('<</A[[]]/q\t56\r78%q\rR>>'))
self.assertRaisesX(main.PdfTokenParseError, e, '<<\r%>>')
def testParseArray(self):
e = main.PdfObj.ParseArray
self.assertRaisesX(main.PdfTokenParseError, e, '[%]')
self.assertEqual(['/Indexed', '/DeviceRGB', 42, '43 44 R'],
e('[\t/Indexed/DeviceRGB\f\r42\00043%42\n44\0R\n]'))
self.assertEqual(['[ ]', '[\t[\f]]', '<<\t [\f[ >>', True, False, None],
e('[[ ] [\t[\f]] <<\t [\f[ >> true%\nfalse\fnull]'))
def testParseValueRecursive(self):
e = main.PdfObj.ParseValueRecursive
self.assertEqual(None, e('null'))
self.assertEqual(True, e('true'))
self.assertEqual(False, e('false'))
self.assertEqual('foo', e(' foo % true\n\r '))
self.assertEqual(42, e('000042'))
self.assertEqual('0042.0', e('0042.0'))
self.assertEqual('/Font', e('\t/Font\f'))
self.assertEqual('<28282929>', e('((()))'))
self.assertEqual('<25>', e('(%)'))
self.assertEqual('<29282868656c292929296c6f295c>',
e('(\\)((hel\\)\\))\\)lo)\\\\)'))
self.assertEqual([], e('[]'))
self.assertEqual({}, e('<<>>'))
self.assertEqual({1: 2}, e('<<1 2>>'))
self.assertEqual({'bar': '/baz', 5: {'A': [67, '<>', 'foo']}},
e('<<5<</A[67()foo]>>bar /baz>>'))
self.assertEqual(['/pedal.#2A', '/pedal.#2A'], e('[/pedal.*/pedal.#2a]'))
self.assertEqual(
['/pedal.#2A', '/pedal.#232a'],
e('[/pedal.*/pedal.#2a]', do_expect_postscript_name_input=True))
self.assertEqual(
['/pedal.#2A', '<>', '/pedal.#232a'],
e('[/pedal.*()/pedal.#2a]', do_expect_postscript_name_input=True))
self.assertRaisesX(main.PdfTokenParseError, e, '1 2')
self.assertRaisesX(main.PdfTokenParseError, e, '[')
self.assertRaisesX(main.PdfTokenParseError, e, '[[]')
self.assertRaisesX(main.PdfTokenParseError, e, ']')
self.assertRaisesX(main.PdfTokenParseError, e, '3]')
self.assertRaisesX(main.PdfTokenParseError, e, '>>')
self.assertRaisesX(main.PdfTokenParseError, e, '<<]')
self.assertRaisesX(main.PdfTokenParseError, e, '[>>')
self.assertRaisesX(main.PdfTokenParseError, e, '(')
self.assertRaisesX(main.PdfTokenParseError, e, '(()')
self.assertRaisesX(main.PdfTokenParseError, e, '(()))')
self.assertRaisesX(main.PdfTokenParseError, e, '<12')
self.assertRaisesX(main.PdfTokenParseError, e, '<12<>>')
self.assertRaisesX(main.PdfTokenParseError, e, '<<1>>')
self.assertRaisesX(main.PdfTokenParseError, e, '<<%>>')
self.assertRaisesX(main.PdfTokenParseError, e, '[%]')
self.assertRaisesX(main.PdfTokenParseError, e, '/')
self.assertRaisesX(main.PdfTokenParseError, e, '//')
self.assertRaisesX(main.PdfTokenParseError, e, '//foo')
def testCompressValue(self):
e = main.PdfObj.CompressValue
self.assertEqual('', e('\t\f\0\r \n'))
self.assertEqual('foo bar', e(' foo\n\t bar\f'))
self.assertEqual('foo 123', e('foo%bar\r123'))
self.assertEqual('foo 123', e('foo%bar\n123'))
self.assertEqual(']foo/bar(\xba\xd0)>>', e(' ] foo\n\t /bar\f <bAd>>>'))
self.assertEqual('<<bAd CAFE>>', e('<<bAd CAFE>>'))
self.assertEqual('<<(\xba\xdc\xaf\xe0)>>', e('<<<bad CAFE>>>'))
self.assertEqual('()', e('()'))
self.assertEqual('<>', e('()', do_emit_strings_as_hex=True))
self.assertEqual('<fa>', e('(\xFa)', do_emit_strings_as_hex=True))
self.assertEqual('<7e>', e('(\\176)', do_emit_strings_as_hex=True))
self.assertEqual('(())', e(' <2829>\t', do_emit_safe_strings=False))
self.assertEqual('<2829>', e(' <2829>\t'))
self.assertEqual('<2829>', e(' (())\t'))
self.assertEqual('(\\)\\()', e(' <2928>\t', do_emit_safe_strings=False))
self.assertEqual('<2928>', e(' <2928>\t'))
self.assertEqual('[12 34]', e('[12%\n34]'))
self.assertEqual('[12 34]', e('[ 12 34 ]'))
self.assertEqual('<</A[12 34]>>', e(' << \t/A\f[12%\n34]>>\r'))
self.assertEqual('<2068656c6c6f090a>world', e(' ( hello\\t\n)world'))
self.assertEqual('( hello\t\n)world',
e(' ( hello\\t\n)world', do_emit_safe_strings=False))
self.assertEqual('<292828686929295c>world', e(' (\\)(\\(hi\\))\\\\)world'))
self.assertEqual('(\\)((hi))\\\\)world',
e(' (\\)(\\(hi\\))\\\\)world', do_emit_safe_strings=False))
self.assertEqual('/#FAce#5BB', e('/\xface#5b#42\f'))
self.assertEqual('/\xface#5BB', e('/\xface#5b#42\f', do_emit_safe_names=False))
self.assertRaisesX(main.PdfTokenParseError, e, '/#')
s = '/Kids[041\t 0\rR\f43\n0% 96 0 R\rR 42 0 R 97 0 Rs 42 0 R]( 98 0 R )\f'
t = '/Kids[041 0 R 43 0 R 42 0 R 97 0 Rs 42 0 R]<2039382030205220>'
self.assertEqual(t, e(s))
self.assertEqual(t, e(e(s)))
old_obj_nums = ['']
self.assertEqual(t, e(s, old_obj_nums_ret=old_obj_nums))
self.assertEqual(['', 41, 43, 42, 42], old_obj_nums)
uu = '/Kids[41 0 R 53 0 R 52 0 R 97 0 Rs 52 0 R]( 98 0 R )'
u = '/Kids[41 0 R 53 0 R 52 0 R 97 0 Rs 52 0 R]<2039382030205220>'
self.assertEqual(u, e(s, obj_num_map={43: 53, 42: 52}))
self.assertEqual(uu, e(s, obj_num_map={43: 53, 42: 52},
do_emit_safe_strings=False))
old_obj_nums = [None]
self.assertEqual(
u, e(s, obj_num_map={43: 53, 42: 52}, old_obj_nums_ret=old_obj_nums))
self.assertEqual([None, 41, 43, 42, 42], old_obj_nums)
self.assertEqual('<</Length 68/Filter/FlateDecode>>',
e('<</Length 68/Filter/FlateDecode >>'))
self.assertEqual('<</Type/Catalog/Pages 1 0 R>>',
e('<</Type/Catalog/Pages 1 0 R >>'))
self.assertEqual('[/Zoo#3C#3E 1]', e('[/Zoo#3c#3e 1]'))
self.assertEqual('[/Zoo#3C#3E(a)/foo#2A/bar#2A#5B/pedal.#2A]',
e('[/Zoo#3c#3e(a\\\n)/foo*/bar#2a#5b/pedal.#2a]'))
self.assertEqual('[/Zoo#3C#3E<0a>/foo#2A/bar#2A#5B/pedal.#2A]',
e('[/Zoo#3c#3e(\r\\\n)/foo*/bar#2a#5b/pedal.#2a]'))
self.assertEqual('[/Zoo#3C#3E<0d>/foo#2A/bar#2A#5B/pedal.#2A]',
e('[/Zoo#3c#3e(\\r\\\n)/foo*/bar#2a#5b/pedal.#2a]'))
self.assertEqual('[/Zoo#3C#3E<0a>/foo*/bar*#5B/pedal.*]',
e('[/Zoo#3c#3e(\n\\\n)/foo*/bar#2a#5b/pedal.#2a]',
do_emit_safe_names=False))
def testPdfObjParse(self):
obj = main.PdfObj(
'42 0 obj<</Length 3>>stream\r\nABC endstream endobj')
self.assertEqual('<</Length 3>>', obj.head)
self.assertEqual('ABC', obj.stream)
obj = main.PdfObj(
'42 0 obj<</Length%5 6\n3\r>>\t\f\0stream\r\nABC endstream endobj')
self.assertEqual('<</Length 3>>', obj.head)
self.assertEqual('ABC', obj.stream)
obj = main.PdfObj(
'42 0 obj<</Length 4>>stream\r\nABC endstream endobj')
self.assertEqual(
'ABC ', main.PdfObj(
'42 0 obj<</Length 99>>stream\r\nABC endstream endobj').stream)
self.assertEqual('<</Length 4>>', obj.head)
self.assertEqual('ABC ', obj.stream)
obj = main.PdfObj('42 0 obj<</Length 4>>endobj')
self.assertEqual('<<>>', obj.head)
self.assertEqual(None, obj.stream)
obj = main.PdfObj(
'42 0 obj<</T[/Length 99]/Length 3>>stream\r\nABC endstream endobj')
self.assertEqual('ABC', obj.stream)
obj = main.PdfObj(
'42 0 obj<</T()/Length 3>>stream\nABC endstream endobj')
self.assertEqual('ABC', obj.stream)
s = '41 0 obj<</T(>>\nendobj\n)/Length 3>>stream\nABD endstream endobj'
t = '42 0 obj<</T 5%>>endobj\n/Length 3>>stream\nABE endstream endobj'
end_ofs_out = []
obj = main.PdfObj(s, end_ofs_out=end_ofs_out)
self.assertEqual('<</T<3e3e0a656e646f626a0a>/Length 3>>', obj.head)
self.assertEqual([43, len(s)], end_ofs_out)
self.assertEqual('ABD', obj.stream)
end_ofs_out = []
obj = main.PdfObj(t + '\r\n\tANYTHING', end_ofs_out=end_ofs_out)
self.assertEqual('<</T 5/Length 3>>', obj.head)
self.assertEqual([43, len(t) + 1], end_ofs_out)
end_ofs_out = []
obj = main.PdfObj(
'%s\n%s' % (s, t), start=len(s) + 1, end_ofs_out=end_ofs_out)
self.assertEqual('ABE', obj.stream)
self.assertEqual([107, len(s) + 1 + len(t)], end_ofs_out)
obj = main.PdfObj('%s\n%s' % (s, t), start=len(s))
self.assertEqual('ABE', obj.stream)
self.assertEqual([107, len(s) + 1 + len(t)], end_ofs_out)
# Exception because start points to '#', not an `X Y obj'.
self.assertRaisesX(
main.PdfTokenParseError,
main.PdfObj, '%s#%s' % (s, t), start=len(s))
s = '22 0 obj<</Producer(A)/CreationDate(B)/Creator(C)>>\nendobj '
t = '23 0 obj'
end_ofs_out = []
obj = main.PdfObj(s + t, end_ofs_out=end_ofs_out)
self.assertEqual('<</Producer(A)/CreationDate(B)/Creator(C)>>', obj.head)
self.assertEqual([len(s)], end_ofs_out)
obj = main.PdfObj(
'42 0 obj[/Foo%]endobj\n42 43\t]\nendobj')
self.assertEqual('[/Foo 42 43]', obj.head)
obj = main.PdfObj('42 0 obj%hello\r \t\f%more\n/Foo%bello\nendobj')
self.assertEqual('/Foo', obj.head)
obj = main.PdfObj('42 0 obj/S()/Type/XObendobj endobj')
self.assertEqual('/S()/Type/XObendobj', obj.head)
obj = main.PdfObj('42 0 obj/Type/XObendobj endobj')
self.assertEqual('/Type/XObendobj', obj.head)
obj = main.PdfObj('42 0 obj/Type/XOb#6Aec#74#1a#20endobj endobj')
self.assertEqual('/Type/XObject#1A#20endobj', obj.head)
obj = main.PdfObj('42 0 obj(endobj+rest) endobj')
self.assertEqual('(endobj+rest)', obj.head)
obj = main.PdfObj('42 0 obj(endobj rest) endobj')
self.assertEqual('<656e646f626a2072657374>', obj.head)
obj = main.PdfObj('42 0 obj<</Type\n\n/XObject >>\n\rendobj')
self.assertEqual('<</Type/XObject>>', obj.head)
obj = main.PdfObj('42 0 obj<</Type\n%\n/XObject >>\n\rendobj')
self.assertEqual('<</Type/XObject>>', obj.head)
obj = main.PdfObj('42 0 obj<</BitsPerComponent\n\n4 \f>>\t\tendobj')
self.assertEqual('<</BitsPerComponent 4>>', obj.head)
obj = main.PdfObj('42 0 obj<</BitsPerComponent\n\n4\f/A ( ) >>\t\tendobj')
self.assertEqual('<</BitsPerComponent 4/A<20>>>', obj.head)
obj = main.PdfObj('42 0 obj<</BitsPerComponent\n\n4\f'
'/A ((\)\)endobj)x) >>\t\tendobj')
self.assertEqual('<</BitsPerComponent 4/A<282929656e646f626a2978>>>',
obj.head)
self.assertRaisesX( # An empty name token.
main.PdfTokenParseError, main.PdfObj, '42 0 obj<</A()/B/>>endobj')
self.assertRaisesX( # An empty name token.
main.PdfTokenParseError, main.PdfObj, '42 0 obj<</A/>>endobj')
obj = main.PdfObj('42 0 obj/foo\\bar endobj')
self.assertEqual('/foo#5Cbar', obj.head)
obj = main.PdfObj('42 0 obj/foo\vbar endobj')
self.assertEqual('/foo#0Bbar', obj.head)
obj = main.PdfObj('42 0 obj<</f*oo\\$ (*Length$) >>endobj')
self.assertEqual('<</f#2Aoo#5C#24(*Length$)>>', obj.head)
obj = main.PdfObj('42 0 obj<</A (/Length) >>endobj')
self.assertEqual('<</A<2f4c656e677468>>>', obj.head)
obj = main.PdfObj('42 0 obj<</A (/Length 5) >>endobj')
self.assertEqual('<</A<2f4c656e6774682035>>>', obj.head)
obj = main.PdfObj('42 0 obj<</A()/B<686a>>>endobj')
self.assertEqual('<</A()/B(hj)>>', obj.head)
obj = main.PdfObj('42 0 obj<</B<686a5>>>endobj')
self.assertEqual('<</B(hjP)>>', obj.head)
obj = main.PdfObj('0 0 obj<</A()/B<>/C(:)/D<3a3A4>>>endobj')
self.assertEqual('<</A()/B()/C(:)/D(::@)>>', obj.head)
obj = main.PdfObj('42 0 obj(}{)endobj')
self.assertEqual('<7d7b>', obj.head)
obj = main.PdfObj('42 0 obj/f*oo$ endobj')
self.assertEqual('/f#2Aoo#24', obj.head)
obj = main.PdfObj('42 0 obj(())endobj')
self.assertEqual('<2829>', obj.head)
obj = main.PdfObj('42 0 obj(\\n)endobj')
self.assertEqual('<0a>', obj.head)
obj = main.PdfObj('42 0 obj(\\100)endobj')
self.assertEqual('(@)', obj.head)
self.assertRaisesX(main.PdfTokenParseError, main.PdfObj,
'42 0 obj /foo# endobj')
self.assertRaisesX(main.PdfTokenParseError, main.PdfObj,
'42 0 obj /foo#b endobj')
self.assertRaisesX(main.PdfTokenParseError, main.PdfObj,
'42 0 obj /foo#bxar endobj')
self.assertRaisesX(main.PdfTokenParseError, main.PdfObj,
'42 0 obj [()<a endobj')
self.assertRaisesX(main.PdfTokenParseError, main.PdfObj,
'42 0 obj [()<g> endobj')
self.assertRaisesX(main.PdfTokenParseError, main.PdfObj,
'42 0 obj [<a endobj')
self.assertRaisesX(main.PdfTokenParseError, main.PdfObj,
'42 0 obj [<g> endobj')
self.assertRaisesX(main.PdfTokenParseError, main.PdfObj,
'42 0 obj %\n\nendobj')
end_ofs_out = []
obj = main.PdfObj('42 0 obj 5 endobj\r\n x', end_ofs_out=end_ofs_out)
self.assertEqual('5', obj.head)
self.assertEqual([20], end_ofs_out)
end_ofs_out = []
obj = main.PdfObj('42 0 obj () endobj\r\n x', end_ofs_out=end_ofs_out)
self.assertEqual('()', obj.head)
self.assertEqual([20], end_ofs_out)
# !!! TODO(pts): Fix bad numbers, all this to 0.
obj = main.PdfObj('42 0 obj[. -. . .]endobj')
self.assertEqual('[. -. . .]', obj.head)
expected_head = '<</Filter[/LZWDecode/ASCIIHexDecode]/Length 0>>'
# Syntax error, stream must be followed by a EOL according to
# pdf_reference_1-7.pdf.
self.assertRaisesX(main.PdfTokenParseError, main.PdfObj,
'42 0 obj<</Filter [/LZW /AHx]/Length 0>>'
'stream%\nendstream endobj')
obj = main.PdfObj('42 0 obj<</Filter [/LZW /AHx]/Length 0>>'
'stream\nendstream endobj')
self.assertEqual(expected_head, obj.head)
# Not allowed by pdf_reference_1-7.pdf, we are permissive.
obj = main.PdfObj('42 0 obj<</Filter [/LZW /AHx]/Length 0>>'
'stream endstream endobj')
self.assertEqual(expected_head, obj.head)
obj = main.PdfObj('42 0 obj<</Filter [/LZW /AHx]/Length 0>>'
'stream\r\nendstream endobj')
self.assertEqual(expected_head, obj.head)
self.assertEqual('', obj.stream)
obj = main.PdfObj('42 0 obj<</Filter [/LZW /AHx]/Length 0>>'
'stream \t\0\f \r\nendstream endobj')
self.assertEqual(expected_head, obj.head)
obj = main.PdfObj('42 0 obj<</Filter [/LZW /AHx]/Length 42'
'/DecodeParms 43/Foo /Bar>>endobj')
self.assertEqual('<</Foo/Bar>>', obj.head)
# TODO(pts): Add more tests.
def testParseTrailer(self):
F = main.PdfObj.ParseTrailer
self.assertEqual('<</Root 4 0 R>>',
F('trailer<</Root 4 0 R>>startxref ').head)
self.assertEqual('<</Root 4 0 R>>',
F('trailer<</Root 4 0 R>>xref ').head)
end_ofs_out = []
trailer = 'footrailer\t<< /Root\n4\n0\nR\r>>\fstartxref '
self.assertEqual('<</Root 4 0 R>>',
F(trailer, start=3, end_ofs_out=end_ofs_out).head)
self.assertEqual('startxref ', trailer[end_ofs_out[0]:])
end_ofs_out = []
trailer = 'footrailer\t<< /Root\n4\n0\nR/Hi(\\041>>xref )\r>>%hi\n\fxref\t'
self.assertEqual('<</Root 4 0 R/Hi<213e3e7872656620>>>',
F(trailer, start=3, end_ofs_out=end_ofs_out).head)
self.assertEqual('xref\t', trailer[end_ofs_out[0]:])
def testExpandAbbreviatoins(self):
F = main.PdfObj.ExpandAbbreviations
self.assertEqual('', F(''))
self.assertEqual('/Foo', F('/Foo'))
self.assertEqual(' /AHxy ', F(' /AHxy '))
self.assertEqual(' /ASCIIHexDecode ', F(' /AHx '))
self.assertEqual(' /ASCIIHexDecode ', F(' /ASCIIHexDecode '))
self.assertEqual('\t/ImageMask\f', F('\t/IM\f'))
self.assertEqual('[/ASCIIHexDecode/ASCII85Decode/LZWDecode '
'/FlateDecode/RunLengthDecode/CCITTFaxDecode/DCTDecode]',
F('[/AHx/A85/LZW /Fl/RL/CCF/DCT]'))
def testCheckSafePdfTokens(self):
F = main.PdfObj.CheckSafePdfTokens
F('')
F('<<')
F(']][<<[]>>-12.34 fooBar /Foo#2a (hello) <a>')
self.assertRaisesX(main.PdfTokenParseError, F, '\n')
self.assertRaisesX(main.PdfTokenParseError, F, 'foo\n')
self.assertRaisesX(main.PdfTokenParseError, F, '%')
self.assertRaisesX(main.PdfTokenParseError, F, '\1')
F('(\200)')
self.assertRaisesX(main.PdfTokenParseError, F, '\200')
self.assertRaisesX(main.PdfTokenParseError, F, '(\\200)')
self.assertRaisesX(main.PdfTokenParseError, F, '(\\101)')
self.assertRaisesX(main.PdfTokenParseError, F, '/pedal.*')
self.assertRaisesX(main.PdfTokenParseError, F, '<a')
self.assertRaisesX(main.PdfTokenParseError, F, '<ag>')
F('x<ab>y')
self.assertRaisesX(main.PdfTokenParseError, F, 'x<ag>y')
self.assertRaisesX(main.PdfTokenParseError, F, '< <')
self.assertRaisesX(main.PdfTokenParseError, F, '> >')
self.assertRaisesX(main.PdfTokenParseError, F, '<<<')
self.assertRaisesX(main.PdfTokenParseError, F, '>>>')
def testParseTokensToSafeSimple(self, is_simple_ok=True):
def F(data, **kwargs):
return main.PdfObj.ParseTokensToSafe(
buffer(data), is_simple_ok=is_simple_ok, **kwargs)[0]
self.assertEqual('hello world', F('hello world'))
self.assertEqual('foo', F('foo\t\f\0\r \n'))
self.assertEqual('world', F('%hello\rworld'))
self.assertEqual('($)', F('($)'))
self.assertEqual('<23>', F('(#)'))
self.assertEqual('', F(''))
self.assertEqual('', F('%hello'))
self.assertEqual('', F('%hello\n'))
self.assertEqual('/Foo#2C#2C 42', F('/Foo,, 42'))
self.assertEqual('/Foo#2C#2C 42', F('/Foo#2c#2C 42'))
self.assertEqual('/Foo#2C#2C 42', F('/Foo,#2c 42'))
self.assertRaisesX(main.PdfTokenParseError, F, '/')
# PdfTokenTruncated would be better.
self.assertRaisesX(main.PdfTokenParseError, F, '/#')
# PdfTokenTruncated would be better.
self.assertRaisesX(main.PdfTokenParseError, F, '/#2')
# PdfTokenTruncated would be better.
self.assertRaisesX(main.PdfTokenParseError, F, '#')
# PdfTokenTruncated would be better.
self.assertRaisesX(main.PdfTokenParseError, F, '#2')
self.assertRaisesX(main.PdfTokenTruncated, F, '(')
self.assertRaisesX(main.PdfTokenTruncated, F, '<')
self.assertEqual('[', F('['))
self.assertEqual('/#2A', F('/#2a'))
self.assertEqual('/J', F('/#4a'))
self.assertEqual('#2A', F('#2a'))
self.assertEqual('J', F('#4a'))
self.assertEqual('', F('\x00'))
self.assertEqual('\x01', F('\x01')) # !!! #01
self.assertEqual('\x7f', F('\x7f')) # !!! #7F
self.assertEqual('\x80', F('\x80')) # !!! #80
self.assertEqual('\xff', F('\xff')) # !!! #FF
self.assertEqual('#01', F('#01'))
self.assertEqual('#7F', F('#7f'))
self.assertEqual('#80', F('#80'))
self.assertEqual('#FF', F('#fF'))
self.assertRaisesX(main.PdfTokenParseError, F, '/')
self.assertRaisesX(main.PdfTokenParseError, F, '/\x00')
self.assertEqual('/#01', F('/\x01'))
self.assertEqual('/#7F', F('/\x7f'))
self.assertEqual('/#80', F('/\x80'))
self.assertEqual('/#FF', F('/\xff'))
self.assertEqual('/#00', F('/#00'))
self.assertEqual('/#01', F('/#01'))
self.assertEqual('/#7F', F('/#7f'))
self.assertEqual('/#80', F('/#80'))
self.assertEqual('/#FF', F('/#fF'))
# Most of these tests copied from testCompressValue. (Not everything was
# copied.)
self.assertEqual('', F('\t\f\0\r \n'))
self.assertEqual('foo bar', F(' foo\n\t bar\f'))
self.assertEqual('foo 123', F('foo%bar\r123'))
self.assertEqual('foo 123', F('foo%bar\n123'))
self.assertEqual(']foo/bar(\xba\xd0)>>', F(' ] foo\n\t /bar\f <bAd>>>'))
self.assertEqual('<<bAd CAFE>>', F('<<bAd CAFE>>'))
self.assertEqual('<<(\xba\xdc\xaf\xe0)>>', F('<<<bad CAFE>>>'))
self.assertEqual('()', F('()'))
self.assertEqual('<2829>', F(' <2829>\t'))
self.assertEqual('<2829>', F(' (())%hi'))
self.assertEqual('<2829>', F(' (())\t'))
self.assertEqual('<2928>', F(' <2928>\t'))
self.assertEqual('[12 34]', F('[12%\n34]'))
self.assertEqual('[12 34]', F('[ 12 34 ]'))
self.assertEqual('<</A[12 34]>>', F(' << \t/A\f[12%\n34]>>\r'))
self.assertEqual('<2068656c6c6f090a>world', F(' ( hello\\t\n)world'))
self.assertEqual('<292828686929295c>world', F(' (\\)(\\(hi\\))\\\\)world'))
self.assertEqual('/#FAce#5BB', F('/\xface#5b#42\f'))
self.assertRaisesX(main.PdfTokenParseError, F, '/#')
s = '/Kids[041\t 0\rR\f43\n0% 96 0 R\rR 42 0 R 97 0 Rs 42 0 R]( 98 0 R )\f'
t = '/Kids[041 0 R 43 0 R 42 0 R 97 0 Rs 42 0 R]<2039382030205220>'
self.assertEqual(t, F(s))
self.assertEqual(t, F(F(s)))
uu = '/Kids[41 0 R 53 0 R 52 0 R 97 0 Rs 52 0 R]( 98 0 R )'
u = '/Kids[41 0 R 53 0 R 52 0 R 97 0 Rs 52 0 R]<2039382030205220>'
self.assertEqual(u, F(u))
self.assertEqual(u, F(uu))
self.assertEqual('<</Length 68/Filter/FlateDecode>>',
F('<</Length 68/Filter/FlateDecode >>'))
self.assertEqual('<</Type/Catalog/Pages 1 0 R>>',
F('<</Type/Catalog/Pages 1 0 R >>'))
self.assertEqual('[/Zoo#3C#3E 1]', F('[/Zoo#3c#3e 1]'))
self.assertEqual('[/Zoo#3C#3E(a)/foo#2A/bar#2A#5B/pedal.#2A]',
F('[/Zoo#3c#3e(a\\\n)/foo*/bar#2a#5b/pedal.#2a]'))
self.assertEqual('[/Zoo#3C#3E<0a>/foo#2A/bar#2A#5B/pedal.#2A]',
F('[/Zoo#3c#3e(\r\\\n)/foo*/bar#2a#5b/pedal.#2a]'))
self.assertEqual('[/Zoo#3C#3E<0d>/foo#2A/bar#2A#5B/pedal.#2A]',
F('[/Zoo#3c#3e(\\r\\\n)/foo*/bar#2a#5b/pedal.#2a]'))
# Most of these tests copied from testRewriteToParsable. (Not everything
# was copied.)
self.assertEqual('[]', F('[]'))
self.assertEqual('[]<<', F('[]<<'))
self.assertEqual('true', F('true '))
self.assertEqual('true', F('true'))
self.assertEqual('hi', F('hi '))
eo = []
self.assertEqual('false true', F('\n\t\r \f\0false true ', end_ofs_out=eo))
self.assertEqual([17], eo)
self.assertEqual('<<true false null>>baz',
F('% hi\r<<%\ntrue false null>>baz'))
self.assertEqual('<<true>>baz',
F('<<true>>baz'))
self.assertEqual('[[<<[<<<<>>>>]>>]]', F('[[<<[<<<<>>>>]>>]]'))
# Unbalanced << and [. It's OK.
self.assertEqual('[[<<[<<<<>>]>>>>]]', F('[[<<[<<<<>>]>>>>]]'))
self.assertEqual('', F('\t \n% foo'))
self.assertEqual('[', F(' [\t'))
self.assertRaisesX(main.PdfTokenTruncated, F, '\n<\f')
self.assertEqual('<<', F('\t<<\n\r'))
self.assertEqual('[<<', F('[<<'))
self.assertEqual('[<<]', F('[<<]'))
self.assertEqual('[>>]', F('[>>]'))
self.assertEqual('()', F('()')) # Different from RewriteToParsable.
self.assertEqual('()', F('<>')) # Different from RewriteToParsable.
self.assertEqual('<<', F('<<'))
self.assertEqual('>>', F('>>'))
self.assertEqual('[', F('['))
self.assertEqual(']', F(']'))
self.assertRaisesX(main.PdfTokenTruncated, F, '(foo')
self.assertRaisesX(main.PdfTokenTruncated, F, '(foo\\)bar')
self.assertEqual('<face654389210b7d>', F('< f\nAc\tE\r654389210B7d\f>'))
self.assertEqual('<48656c6c6f2c20576f726c6421>', F('(Hello, World!)'))
self.assertEqual('<2828666f6f2929296261725c>', F('(((foo))\\)bar\\\\)'))
self.assertEqual('<410a420a430a440a0a45>', F('(A\rB\nC\r\nD\n\rE)'))
self.assertEqual('<0a280d2900>', F('(\\n(\\r)\\0)'))
self.assertEqual('<0a280a2900780a790a0a7a>', F('(\n(\r)\0x\r\ny\n\rz)'))
self.assertEqual('(FooBarBaz)', F('(Foo\\\nBar\\\rBa\\\r\nz)'))
self.assertEqual('<466f6f4261720a42617a>', F('(Foo\\\r\nBar\\\n\rBaz)'))
self.assertEqual('<2829%s>' % ''.join(['%02x' % {13: 10}.get(i, i)
for i in xrange(33)]),
F('(()%s)' % ''.join(map(chr, xrange(33)))))
self.assertEqual('<face422829>', F('(\xfa\xCE\x42())'))
self.assertEqual('<00210023>', F('(\0!\\0#)'))
self.assertEqual('<073839380a>', F('(\78\98\12)'))
self.assertEqual('(\x0501)', F('(\\501)'))
self.assertEqual('<0a0a09080c>', F('(\n\r\t\b\f)'))
self.assertEqual('<0a0d09080c>', F('(\\n\\r\\t\\b\\f)'))
self.assertEqual('<236141>', F('(\\#\\a\\A)'))
self.assertEqual('<61275c>', F("(a'\\\\)"))
self.assertEqual('<314f5c60>', F('<\n3\t1\r4f5C6 >'))
self.assertEqual('<0006073839050e170338043805380638073838380a3913391f39>',
F('(\0\6\7\8\9\05\16\27\38\48\58\68\78\88\129\239\379)'))
self.assertEqual('<666f6f0a626172>', F('(foo\nbar)'))
self.assertEqual('<666f6f0a626172>', F('(foo\\nbar)'))
self.assertEqual('(foobar)', F('(foo\\\nbar)'))
self.assertEqual('<0006073839050e170338043805380638073838380a3913391f39'
'043031053031063031073031>',
F('(\\0\\6\\7\\8\\9\\05\\16\\27\\38\\48\\58\\68\\78\\88'
'\\129\\239\\379\\401\\501\\601\\701)'))
# PDF doesn't have \x
self.assertEqual('(xfaxbCxDE\xf8)', F('(\\xfa\\xbC\\xDE\xF8)'))
self.assertEqual('0', F('0'))
self.assertEqual('.', F('.'))
self.assertEqual('42', F('42'))
self.assertEqual('42', F('42 '))
# Different from RewriteToParsable.
#self.assertEqual('0', F('00000 '))
#self.assertEqual('0', F('+00000 '))
#self.assertEqual('0', F('-00000 '))
#self.assertEqual('0', F('00000.000 '))
#self.assertEqual('0', F('+00000.000 '))
#self.assertEqual('0', F('-00000.000 '))
#self.assertEqual('12', F('00012 '))
#self.assertEqual('12', F('+00012 '))
#self.assertEqual('-12', F('-00012 '))
#self.assertEqual('12', F('00012. '))
#self.assertEqual('12', F('00012.000 '))
#self.assertEqual('12', F('+00012.000 '))
#self.assertEqual('-12', F('-12.000 '))
#self.assertEqual('12.34', F('00012.340 '))
#self.assertEqual('12.34', F('+00012.34 '))
#self.assertEqual('-12.34', F('-12.340 '))
#self.assertEqual('[ .34 -.34 .34 ]', F('[.34 -.34 +.34]'))
#self.assertEqual('[ .34 -.34 .34 ]', F('[00.34 -00.34 +00.34]'))
#self.assertEqual('[ 34 -34 34 ]', F('[34. -34. +34.]'))
#self.assertEqual('[ 34 -34 34 ]', F('[0034. -0034. +0034.]'))
#self.assertEqual('[ 34 -34 34 ]', F('[34.00 -34.00 +34.00]'))
#self.assertEqual('[ 34 -34 34 ]', F('[0034.00 -0034.00 +0034.00]'))
#self.assertEqual('[ 0 0 0 ]', F('[0. -0. +0.]'))
#self.assertEqual('[ 0 0 0 ]', F('[000. -000. +000.]'))
#self.assertEqual('[ 0 0 0 ]', F('[.0 -.0 +.0]'))
#self.assertEqual('[ 0 0 0 ]', F('[.000 -.000 +.000]'))
#self.assertEqual('[ 0 0 0 ]', F('[00.000 -00.000 +00.000]'))
self.assertEqual('12 345 R', F(' 12 345 R '))