File: encoding.py

package info (click to toggle)
sketch 0.6.13-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,284 kB
  • ctags: 8,453
  • sloc: python: 34,711; ansic: 16,543; makefile: 83; sh: 26
file content (151 lines) | stat: -rw-r--r-- 7,783 bytes parent folder | download | duplicates (5)
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
# Sketch - A Python-based interactive drawing program
# Copyright (C) 1998, 1999 by Bernhard Herzog
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

import operator, string

notdef = '.notdef'

iso_latin_1 = (notdef, notdef, notdef, notdef, notdef, notdef, notdef,
	       notdef, notdef, notdef, notdef, notdef, notdef, notdef,
	       notdef, notdef, notdef, notdef, notdef, notdef, notdef,
	       notdef, notdef, notdef, notdef, notdef, notdef, notdef,
	       notdef, notdef, notdef, notdef, 'space', 'exclam',
	       'quotedbl', 'numbersign', 'dollar', 'percent',
	       'ampersand', 'quoteright', 'parenleft', 'parenright',
	       'asterisk', 'plus', 'comma', 'minus', 'period', 'slash',
	       'zero', 'one', 'two', 'three', 'four', 'five', 'six',
	       'seven', 'eight', 'nine', 'colon', 'semicolon', 'less',
	       'equal', 'greater', 'question', 'at', 'A', 'B', 'C', 'D',
	       'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
	       'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
	       'bracketleft', 'backslash', 'bracketright',
	       'asciicircum', 'underscore', 'quoteleft', 'a', 'b', 'c',
	       'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
	       'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
	       'z', 'braceleft', 'bar', 'braceright', 'asciitilde',
	       notdef, notdef, notdef, notdef, notdef, notdef, notdef,
	       notdef, notdef, notdef, notdef, notdef, notdef, notdef,
	       notdef, notdef, notdef, 'dotlessi', 'grave', 'acute',
	       'circumflex', 'tilde', 'macron', 'breve', 'dotaccent',
	       'dieresis', notdef, 'ring', 'cedilla', notdef,
	       'hungarumlaut', 'ogonek', 'caron', 'space', 'exclamdown',
	       'cent', 'sterling', 'currency', 'yen', 'brokenbar',
	       'section', 'dieresis', 'copyright', 'ordfeminine',
	       'guillemotleft', 'logicalnot', 'hyphen', 'registered',
	       'macron', 'degree', 'plusminus', 'twosuperior',
	       'threesuperior', 'acute', 'mu', 'paragraph',
	       'periodcentered', 'cedilla', 'onesuperior',
	       'ordmasculine', 'guillemotright', 'onequarter',
	       'onehalf', 'threequarters', 'questiondown', 'Agrave',
	       'Aacute', 'Acircumflex', 'Atilde', 'Adieresis', 'Aring',
	       'AE', 'Ccedilla', 'Egrave', 'Eacute', 'Ecircumflex',
	       'Edieresis', 'Igrave', 'Iacute', 'Icircumflex',
	       'Idieresis', 'Eth', 'Ntilde', 'Ograve', 'Oacute',
	       'Ocircumflex', 'Otilde', 'Odieresis', 'multiply',
	       'Oslash', 'Ugrave', 'Uacute', 'Ucircumflex', 'Udieresis',
	       'Yacute', 'Thorn', 'germandbls', 'agrave', 'aacute',
	       'acircumflex', 'atilde', 'adieresis', 'aring', 'ae',
	       'ccedilla', 'egrave', 'eacute', 'ecircumflex',
	       'edieresis', 'igrave', 'iacute', 'icircumflex',
	       'idieresis', 'eth', 'ntilde', 'ograve', 'oacute',
	       'ocircumflex', 'otilde', 'odieresis', 'divide', 'oslash',
	       'ugrave', 'uacute', 'ucircumflex', 'udieresis', 'yacute',
	       'thorn', 'ydieresis')


adobe_standard = (notdef, notdef, notdef, notdef, notdef, notdef,
                  notdef, notdef, notdef, notdef, notdef, notdef,
                  notdef, notdef, notdef, notdef, notdef, notdef,
                  notdef, notdef, notdef, notdef, notdef, notdef,
                  notdef, notdef, notdef, notdef, notdef, notdef,
                  notdef, notdef, 'space', 'exclam', 'quotedbl',
                  'numbersign', 'dollar', 'percent', 'ampersand',
                  'quoteright', 'parenleft', 'parenright', 'asterisk',
                  'plus', 'comma', 'hyphen', 'period', 'slash', 'zero',
                  'one', 'two', 'three', 'four', 'five', 'six', 'seven',
                  'eight', 'nine', 'colon', 'semicolon', 'less',
                  'equal', 'greater', 'question', 'at', 'A', 'B', 'C',
                  'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
                  'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
                  'Z', 'bracketleft', 'backslash', 'bracketright',
                  'asciicircum', 'underscore', 'quoteleft', 'a', 'b',
                  'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
                  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
                  'y', 'z', 'braceleft', 'bar', 'braceright',
                  'asciitilde', notdef, notdef, notdef, notdef, notdef,
                  notdef, notdef, notdef, notdef, notdef, notdef,
                  notdef, notdef, notdef, notdef, notdef, notdef,
                  notdef, notdef, notdef, notdef, notdef, notdef,
                  notdef, notdef, notdef, notdef, notdef, notdef,
                  notdef, notdef, notdef, notdef, notdef, 'exclamdown',
                  'cent', 'sterling', 'fraction', 'yen', 'florin',
                  'section', 'currency', 'quotesingle', 'quotedblleft',
                  'guillemotleft', 'guilsinglleft', 'guilsinglright',
                  'fi', 'fl', notdef, 'endash', 'dagger', 'daggerdbl',
                  'periodcentered', notdef, 'paragraph', 'bullet',
                  'quotesinglbase', 'quotedblbase', 'quotedblright',
                  'guillemotright', 'ellipsis', 'perthousand', notdef,
                  'questiondown', notdef, 'grave', 'acute',
                  'circumflex', 'tilde', 'macron', 'breve', 'dotaccent',
                  'dieresis', notdef, 'ring', 'cedilla', notdef,
                  'hungarumlaut', 'ogonek', 'caron', 'emdash', notdef,
                  notdef, notdef, notdef, notdef, notdef, notdef,
                  notdef, notdef, notdef, notdef, notdef, notdef,
                  notdef, notdef, notdef, 'AE', notdef, 'ordfeminine',
                  notdef, notdef, notdef, notdef, 'Lslash', 'Oslash',
                  'OE', 'ordmasculine', notdef, notdef, notdef, notdef,
                  notdef, 'ae', notdef, notdef, notdef, 'dotlessi',
                  notdef, notdef, 'lslash', 'oslash', 'oe',
                  'germandbls', notdef, notdef, notdef, notdef)

class Reencoder:

    def __init__(self, source, dest, notdef = None):
        self.source = source
        self.dest = dest
        if notdef is None:
            try:
                notdef = list(self.source).index('.notdef')
            except:
                notdef = 0
        self.notdef = notdef
        self.build_mapping()

    def build_mapping(self):
        dest = self.dest; source = self.source; notdef = self.notdef
        dict = {}
        length = len(dest)
        map(operator.setitem, [dict] * length, dest, range(length))
        dict[notdef] = self.notdef
        mapping = range(256)
        for i in mapping:
            if source[i] != dest[i]:
                if source[i] == notdef:
                    mapping[i] = notdef
                else:
                    mapping[i] = dict.get(source[i], notdef)
        if mapping == range(256):
            self.mapping = ''
        else:
            self.mapping = string.join(map(chr, mapping), '')

    def __call__(self, text):
        if self.mapping:
            return string.translate(text, self.mapping)
        else:
            return text