source: proto/charsetcompiler/PDF_mod_translate.compiler @ 3903

Last change on this file since 3903 was 2822, checked in by cameron, 6 years ago

Generating character class equations for PDF escape processing

File size: 1.3 KB
Line 
1#
2#  Robert D. Cameron
3#  Jan. 21, 2013
4#
5
6import sys, string
7#
8#
9
10pdf_escape_table = [v for v in range(0,256)]
11pdf_escape_table[ord('n')] = 0xA
12pdf_escape_table[ord('r')] = 0xD
13pdf_escape_table[ord('t')] = 0x9
14pdf_escape_table[ord('b')] = 0x8
15pdf_escape_table[ord('f')] = 0xC
16
17
18def bit_change_class(tbl, bitno):
19        charvals_in_class = [v for v in range(0, 256) if ((v ^ tbl[v]) >> (7 - bitno)) & 1 == 1]
20        items = []
21        if charvals_in_class == []: return items
22        range0 = charvals_in_class[0]
23        range_pending = range0
24        for v in charvals_in_class[1:]:
25                if v == range_pending + 1: range_pending += 1
26                else:
27                        if range0 == range_pending: items.append(chr(range0))
28                        else: items.append('%s-%s' % (chr(range0), chr(range_pending)))
29                        range0 = v
30                        range_pending = range0
31        if range0 == range_pending: items.append(chr(range0))
32        else: items.append('%s-%s' % (chr(range0), chr(range_pending)))
33        return items
34
35def WritePDF_escape_defs(f):
36        defs="# PDF escape bit modifications"
37        for bit in range(0, 8):
38                cs = bit_change_class(pdf_escape_table, bit)
39                defs += "pdf_mod_bit_%i = [%s]\n" % (bit, string.join(cs, "").encode('string-escape'))
40        f.write(defs)
41
42if __name__ == "__main__":
43        args = sys.argv[1:]
44        if len(args) == 0:
45                WritePDF_escape_defs(sys.stdout)
46        elif len(args) == 1:
47                f = open(args[0], "w")
48                WritePDF_escape_defs(f)
49                f.close()
50
51
Note: See TracBrowser for help on using the repository browser.