OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The PDFium Authors. All rights reserved. | 2 # Copyright 2014 The PDFium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Expands a hand-written PDF testcase (template) into a valid PDF file. | 6 """Expands a hand-written PDF testcase (template) into a valid PDF file. |
7 | 7 |
8 There are several places in a PDF file where byte-offsets are required. This | 8 There are several places in a PDF file where byte-offsets are required. This |
9 script replaces {{name}}-style variables in the input with calculated results | 9 script replaces {{name}}-style variables in the input with calculated results |
10 | 10 |
11 {{header}} - expands to the header comment required for PDF files. | 11 {{header}} - expands to the header comment required for PDF files. |
12 {{xref}} - expands to a generated xref table, noting the offset. | 12 {{xref}} - expands to a generated xref table, noting the offset. |
13 {{startxref} - expands to a startxref directive followed by correct offset. | 13 {{startxref} - expands to a startxref directive followed by correct offset. |
14 {{object x y}} - expands to |x y obj| declaration, noting the offset.""" | 14 {{object x y}} - expands to |x y obj| declaration, noting the offset.""" |
15 | 15 |
16 import optparse | 16 import optparse |
17 import os | 17 import os |
18 import re | 18 import re |
19 import sys | 19 import sys |
20 | 20 |
21 class TemplateProcessor: | 21 class TemplateProcessor: |
22 HEADER_TOKEN = '{{header}}' | 22 HEADER_TOKEN = '{{header}}' |
23 HEADER_REPLACEMENT = '%PDF-1.7\n%\xa0\xf2\xa4\xf4' | 23 HEADER_REPLACEMENT = '%PDF-1.7\n%\xa0\xf2\xa4\xf4' |
24 | 24 |
25 XREF_TOKEN = '{{xref}}' | 25 XREF_TOKEN = '{{xref}}' |
26 XREF_REPLACEMENT = 'xref\n%d %d\n' | 26 XREF_REPLACEMENT = 'xref\n%d %d\n' |
27 XREF_REPLACEMENT_N = '%010d %05d n\n' | 27 |
28 XREF_REPLACEMENT_F = '0000000000 65536 f\n' | 28 # XREF rows must be exactly 20 bytes - space required. |
| 29 XREF_REPLACEMENT_N = '%010d %05d n \n' |
| 30 XREF_REPLACEMENT_F = '0000000000 65535 f \n' |
29 | 31 |
30 STARTXREF_TOKEN= '{{startxref}}' | 32 STARTXREF_TOKEN= '{{startxref}}' |
31 STARTXREF_REPLACEMENT = 'startxref\n%d' | 33 STARTXREF_REPLACEMENT = 'startxref\n%d' |
32 | 34 |
33 OBJECT_PATTERN = r'\{\{object\s+(\d+)\s+(\d+)\}\}' | 35 OBJECT_PATTERN = r'\{\{object\s+(\d+)\s+(\d+)\}\}' |
34 OBJECT_REPLACEMENT = r'\1 \2 obj' | 36 OBJECT_REPLACEMENT = r'\1 \2 obj' |
35 | 37 |
36 def __init__(self): | 38 def __init__(self): |
37 self.offset = 0 | 39 self.offset = 0 |
38 self.xref_offset = 0 | 40 self.xref_offset = 0 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 except IOError: | 82 except IOError: |
81 print >> sys.stderr, 'failed to process %s' % input_filename | 83 print >> sys.stderr, 'failed to process %s' % input_filename |
82 | 84 |
83 def main(): | 85 def main(): |
84 for arg in sys.argv[1:]: | 86 for arg in sys.argv[1:]: |
85 expand_file(arg) | 87 expand_file(arg) |
86 return 0 | 88 return 0 |
87 | 89 |
88 if __name__ == '__main__': | 90 if __name__ == '__main__': |
89 sys.exit(main()) | 91 sys.exit(main()) |
OLD | NEW |