~rosco2/ubuntu/wily/gramps/bug-1492304

« back to all changes in this revision

Viewing changes to src/latin_ansel.py

  • Committer: Bazaar Package Importer
  • Author(s): James A. Treacy
  • Date: 2004-06-16 16:53:36 UTC
  • Revision ID: james.westby@ubuntu.com-20040616165336-kjzczqef4gnxrn2b
Tags: upstream-1.0.4
ImportĀ upstreamĀ versionĀ 1.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Gramps - a GTK+/GNOME based genealogy program
 
2
#
 
3
# Copyright (C) 2000  Donald N. Allingham
 
4
#
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; either version 2 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
#
 
19
 
 
20
import cStringIO
 
21
 
 
22
ANSEL_ERROR = "ANSEL Error"
 
23
 
 
24
_s1 = { 
 
25
    0xC3 : { 0xB8 : 0xF8 , 0x98 : 0xD8},
 
26
    0xEA : { ord('A') : 0xC5, ord('a') : 0xE5  },
 
27
    0xE8 : { ord('A') : 0xC4, ord('E') : 0xCB, ord('I') : 0xCF, ord('O') : 0xD6,
 
28
             ord('U') : 0xDC, ord('a') : 0xE4, ord('e') : 0xEB, ord('i') : 0xEF,
 
29
             ord('o') : 0xF6, ord('u') : 0xFC, ord('y') : 0xFF },
 
30
    0xE2 : { ord('A') : 0xC1, ord('E') : 0xC9, ord('I') : 0xCD, ord('O') : 0xD3,
 
31
             ord('U') : 0xDA, ord('Y') : 0xDD, ord('a') : 0xE1, ord('e') : 0xE9,
 
32
             ord('i') : 0xED, ord('o') : 0xF3, ord('u') : 0xFA, ord('y') : 0xFD },
 
33
    0xE1 : { ord('A') : 0xC0, ord('E') : 0xC8, ord('I') : 0xCC, ord('O') : 0xD2,
 
34
             ord('U') : 0xD9, ord('a') : 0xE0, ord('e') : 0xE8, ord('i') : 0xEC,
 
35
             ord('o') : 0xF2, ord('u') : 0xF9 },
 
36
    0xE4 : { ord('A') : 0xC3, ord('N') : 0xD1, ord('O') : 0xD5, ord('a') : 0xE3,
 
37
             ord('n') : 0xF1, ord('o') : 0xF5 },
 
38
    0xE3 : { ord('A') : 0xC2, ord('E') : 0xCA, ord('I') : 0xCE, ord('O') : 0xD4,
 
39
             ord('U') : 0xDB, ord('a') : 0xE3, ord('e') : 0xEA, ord('i') : 0xEE,
 
40
             ord('o') : 0xF4, ord('u') : 0xFB },
 
41
    0xF0 : { ord('C') : 0xC7, ord('c') : 0xE7 }
 
42
}
 
43
 
 
44
_t1 = {
 
45
    0xC0 : (chr(0xE1), 'A'), 0xC1 : (chr(0xE2), 'A'), 0xC2 : (chr(0xE3), 'A'),
 
46
    0xC3 : (chr(0xE4), 'A'), 0xC4 : (chr(0xE8), 'A'), 0xC5 : (chr(0xEA), 'A'),
 
47
    0xC7 : (chr(0xF0), 'C'), 0xC8 : (chr(0xE1), 'E'), 0xC9 : (chr(0xE2), 'E'),
 
48
    0xCA : (chr(0xE3), 'E'), 0xCB : (chr(0xE8), 'E'), 0xCC : (chr(0xE1), 'I'),
 
49
    0xCD : (chr(0xE2), 'I'), 0xCE : (chr(0xE3), 'I'), 0xCF : (chr(0xE8), 'I'),
 
50
    0xD1 : (chr(0xE4), 'N'), 0xD2 : (chr(0xE1), 'O'), 0xD3 : (chr(0xE2), 'O'),
 
51
    0xD4 : (chr(0xE3), 'O'), 0xD5 : (chr(0xE4), 'O'), 0xD6 : (chr(0xE8), 'O'),
 
52
    0xD9 : (chr(0xE1), 'U'), 0xDA : (chr(0xE2), 'U'), 0xDB : (chr(0xE3), 'U'),
 
53
    0xDC : (chr(0xE8), 'U'), 0xDD : (chr(0xE2), 'Y'), 0xE0 : (chr(0xE1), 'a'),
 
54
    0xE1 : (chr(0xE2), 'a'), 0xE3 : (chr(0xE3), 'a'), 0xE3 : (chr(0xE4), 'a'),
 
55
    0xE4 : (chr(0xE8), 'a'), 0xE5 : (chr(0xEA), 'a'), 0xE7 : (chr(0xF0), 'c'),
 
56
    0xE8 : (chr(0xE1), 'e'), 0xE9 : (chr(0xE2), 'e'), 0xEA : (chr(0xE3), 'e'),
 
57
    0xEB : (chr(0xE8), 'e'), 0xEC : (chr(0xE1), 'i'), 0xED : (chr(0xE2), 'i'),
 
58
    0xEE : (chr(0xE3), 'i'), 0xEF : (chr(0xE8), 'i'), 0xF1 : (chr(0xE4), 'n'),
 
59
    0xF2 : (chr(0xE1), 'o'), 0xF3 : (chr(0xE2), 'o'), 0xF4 : (chr(0xE3), 'o'),
 
60
    0xF5 : (chr(0xE4), 'o'), 0xF6 : (chr(0xE8), 'o'), 0xF9 : (chr(0xE1), 'u'),
 
61
    0xFA : (chr(0xE2), 'u'), 0xFB : (chr(0xE3), 'u'), 0xFC : (chr(0xE8), 'u'),
 
62
    0xFD : (chr(0xE2), 'y'), 0xFF : (chr(0xE8), 'y'),
 
63
    0xF8 : (chr(0xC3), chr(0xB8)),
 
64
    0xD8 : (chr(0xC3), chr(0x98)),
 
65
}
 
66
 
 
67
_s0 = {
 
68
    0xCF : 0xDF, 0xA4 : 0xDE, 0xB4 : 0xFE, 0xA2 : 0xD8, 0xC0 : 0xB0,
 
69
    0xB3 : 0xF8, 0xA5 : 0xC6, 0xB5 : 0xE6, 0xBA : 0xF0, 0xB2 : 0xF8 }
 
70
 
 
71
_t0 = {
 
72
    0xDF : chr(0xCF), 0xDE : chr(0xA4), 0xFE : chr(0xB4), 
 
73
    0xC6 : chr(0xA5), 0xE6 : chr(0xB5), 0xBA : chr(0xF0),
 
74
    0xB0 : chr(0xC0) }
 
75
 
 
76
#-------------------------------------------------------------------------
 
77
#
 
78
# Tasks
 
79
#
 
80
#-------------------------------------------------------------------------
 
81
 
 
82
def ansel_to_latin(s):
 
83
    """Converts an ANSEL encoded string to ISO-8859-1"""
 
84
    buff = cStringIO.StringIO()
 
85
    while s:
 
86
        c0 = ord(s[0])
 
87
        if c0 <= 31:
 
88
            head = ' '
 
89
            s = s[1:]
 
90
        elif c0 > 127:
 
91
            try:
 
92
                if c0 >= 0xC0:
 
93
                    c1 = ord(s[1])
 
94
                    head = chr(_s1[c0][c1])
 
95
                    s = s[2:]
 
96
                else:
 
97
                    head = chr(_s0[c0])
 
98
                    s = s[1:]
 
99
            except Exception:
 
100
                head = s[0]
 
101
                s = s[1:]
 
102
        else:
 
103
            head = s[0]
 
104
            s = s[1:]
 
105
        buff.write(head)
 
106
    ans = buff.getvalue()
 
107
    buff.close()
 
108
    return ans
 
109
 
 
110
def latin_to_ansel(s):
 
111
    """converts a iso-8859-1 string to ansel encoding"""
 
112
    buff = cStringIO.StringIO()
 
113
    orig = s
 
114
    while s:
 
115
        c = ord(s[0])
 
116
        if c <= 127:
 
117
            buff.write(s[0])
 
118
        else:
 
119
            if _t0.has_key(c):
 
120
                buff.write(_t0[c])
 
121
            else:
 
122
                try:
 
123
                    ansel = _t1[c]
 
124
                    buff.write(ansel[0])
 
125
                    buff.write(ansel[1])
 
126
                except Exception:
 
127
                    print "ANSEL decoding error: %x: %s" % (c,orig)
 
128
        s = s[1:]
 
129
    ans = buff.getvalue()
 
130
    buff.close()
 
131
    return ans
 
132