]> git.lyx.org Git - lyx.git/blob - src/encoding.C
* support/os_unix.C (canAutoOpen, autoOpenFile): on Mac OS X, use
[lyx.git] / src / encoding.C
1 /**
2  * \file encoding.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Dekel Tsur
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "encoding.h"
16
17 #include "debug.h"
18 #include "lyxlex.h"
19 #include "lyxrc.h"
20
21 #include "support/filename.h"
22
23
24 namespace lyx {
25
26 #ifndef CXX_GLOBAL_CSTD
27 using std::strtol;
28 #endif
29
30 using std::endl;
31 using std::string;
32
33
34 Encodings encodings;
35
36 namespace {
37
38 char_type arabic_table2[63][4] = {
39         {0x41, 0x41, 0x41, 0x41}, // 0xc1 = hamza
40         {0x42, 0xa1, 0x42, 0xa1}, // 0xc2 = ligature madda on alef
41         {0x43, 0xa2, 0x43, 0xa2}, // 0xc3 = ligature hamza on alef
42         {0x44, 0xa3, 0x44, 0xa3}, // 0xc4 = ligature hamza on waw
43         {0x45, 0xa4, 0x45, 0xa4}, // 0xc5 = ligature hamza under alef
44         {0x46, 0xf9, 0xf8, 0xa0}, // 0xc6 = ligature hamza on ya
45         {0x47, 0xa5, 0x47, 0xa5}, // 0xc7 = alef
46         {0x48, 0xae, 0xac, 0xad}, // 0xc8 = baa
47         {0x49, 0xb1, 0x49, 0xb1}, // 0xc9 = taa marbuta
48         {0x4a, 0xb4, 0xb2, 0xb3}, // 0xca = taa
49         {0x4b, 0xb7, 0xb5, 0xb6}, // 0xcb = thaa
50         {0x4c, 0xba, 0xb8, 0xb9}, // 0xcc = jeem
51         {0x4d, 0xbd, 0xbb, 0xbc}, // 0xcd = haa
52         {0x4e, 0xc0, 0xbe, 0xbf}, // 0xce = khaa
53         {0x4f, 0xa6, 0x4f, 0xa6}, // 0xcf = dal
54
55         {0x50, 0xa7, 0x50, 0xa7}, // 0xd0 = thal
56         {0x51, 0xa8, 0x51, 0xa8}, // 0xd1 = ra
57         {0x52, 0xa9, 0x52, 0xa9}, // 0xd2 = zain
58         {0x53, 0xc3, 0xc1, 0xc2}, // 0xd3 = seen
59         {0x54, 0xc6, 0xc4, 0xc5}, // 0xd4 = sheen
60         {0x55, 0xc9, 0xc7, 0xc8}, // 0xd5 = sad
61         {0x56, 0xcc, 0xca, 0xcb}, // 0xd6 = dad
62         {0x57, 0xcf, 0xcd, 0xce}, // 0xd7 = tah
63         {0x58, 0xd2, 0xd0, 0xd1}, // 0xd8 = zah
64         {0x59, 0xd5, 0xd3, 0xd4}, // 0xd9 = ain
65         {0x5a, 0xd8, 0xd6, 0xd7}, // 0xda = ghain
66         {0,0,0,0}, // 0xdb
67         {0,0,0,0}, // 0xdc
68         {0,0,0,0}, // 0xdd
69         {0,0,0,0}, // 0xde
70         {0,0,0,0}, // 0xdf
71
72         {0,0,0,0}, // 0xe0
73         {0x61, 0xdb, 0xd9, 0xda}, // 0xe1 = fa
74         {0x62, 0xde, 0xdc, 0xdd}, // 0xe2 = qaf
75         {0x63, 0xe1, 0xdf, 0xe0}, // 0xe3 = kaf
76         {0x64, 0xe4, 0xe2, 0xe3}, // 0xe4 = lam
77         {0x65, 0xe7, 0xe5, 0xe6}, // 0xe5 = meem
78         {0x66, 0xea, 0xe8, 0xe9}, // 0xe6 = noon
79         {0x67, 0xed, 0xeb, 0xec}, // 0xe7 = ha
80         {0x68, 0xaa, 0x68, 0xaa}, // 0xe8 = waw
81         {0x69, 0xab, 0x69, 0xab}, // 0xe9 = alef maksura
82         {0x6a, 0xf0, 0xee, 0xef}, // 0xea = ya
83         {0x6b, 0x6b, 0x6b, 0x6b}, // 0xeb = fathatan
84         {0x6c, 0x6c, 0x6c, 0x6c}, // 0xec = dammatan
85         {0x6d, 0x6d, 0x6d, 0x6d}, // 0xed = kasratan
86         {0x6e, 0x6e, 0x6e, 0x6e}, // 0xee = fatha
87         {0x6f, 0x6f, 0x6f, 0x6f}, // 0xef = damma
88
89         {0x70, 0x70, 0x70, 0x70}, // 0xf0 = kasra
90         {0x71, 0x71, 0x71, 0x71}, // 0xf1 = shadda
91         {0x72, 0x72, 0x72, 0x72}, // 0xf2 = sukun
92         {0,0,0,0}, // 0xf3
93         {0,0,0,0}, // 0xf4
94         {0,0,0,0}, // 0xf5
95         {0,0,0,0}, // 0xf6
96         {0,0,0,0}, // 0xf7
97         {0,0,0,0}, // 0xf8
98         {0,0,0,0}, // 0xf9
99         {0,0,0,0}, // 0xfa
100         {0,0,0,0}, // 0xfb
101         {0,0,0,0}, // 0xfc
102         {0,0,0,0}, // 0xfd
103         {0,0,0,0}, // 0xfe
104         {0,0,0,0}, // 0xff
105 };
106
107
108 char_type arabic_table[63][2] = {
109         {0xc1, 0xc1}, // 0xc1 = hamza
110         {0xc2, 0xc2}, // 0xc2 = ligature madda on alef
111         {0xc3, 0xc3}, // 0xc3 = ligature hamza on alef
112         {0xc4, 0xc4}, // 0xc4 = ligature hamza on waw
113         {0xc5, 0xc5}, // 0xc5 = ligature hamza under alef
114         {0xc6, 0xc0}, // 0xc6 = ligature hamza on ya
115         {0xc7, 0xc7}, // 0xc7 = alef
116         {0xc8, 0xeb}, // 0xc8 = baa
117         {0xc9, 0xc9}, // 0xc9 = taa marbuta
118         {0xca, 0xec}, // 0xca = taa
119         {0xcb, 0xed}, // 0xcb = thaa
120         {0xcc, 0xee}, // 0xcc = jeem
121         {0xcd, 0xef}, // 0xcd = haa
122         {0xce, 0xf0}, // 0xce = khaa
123         {0xcf, 0xcf}, // 0xcf = dal
124
125         {0xd0, 0xd0}, // 0xd0 = thal
126         {0xd1, 0xd1}, // 0xd1 = ra
127         {0xd2, 0xd2}, // 0xd2 = zain
128         {0xd3, 0xf1}, // 0xd3 = seen
129         {0xd4, 0xf2}, // 0xd4 = sheen
130         {0xd5, 0xf3}, // 0xd5 = sad
131         {0xd6, 0xf4}, // 0xd6 = dad
132         {0xd7, 0xd7}, // 0xd7 = tah
133         {0xd8, 0xd8}, // 0xd8 = zah
134         {0xd9, 0xf5}, // 0xd9 = ain
135         {0xda, 0xf6}, // 0xda = ghain
136         {0,0}, // 0xdb
137         {0,0}, // 0xdc
138         {0,0}, // 0xdd
139         {0,0}, // 0xde
140         {0,0}, // 0xdf
141
142         {0,0},  // 0xe0
143         {0xe1, 0xf7},   // 0xe1 = fa
144         {0xe2, 0xf8},   // 0xe2 = qaf
145         {0xe3, 0xf9},   // 0xe3 = kaf
146         {0xe4, 0xfa},   // 0xe4 = lam
147         {0xe5, 0xfb},   // 0xe5 = meem
148         {0xe6, 0xfc},   // 0xe6 = noon
149         {0xe7, 0xfd},   // 0xe7 = ha
150         {0xe8, 0xe8},   // 0xe8 = waw
151         {0xe9, 0xe9},   // 0xe9 = alef maksura
152         {0xea, 0xfe},   // 0xea = ya
153         {0xa8, 0xa8},   // 0xeb = fathatan
154         {0xa9, 0xa9},   // 0xec = dammatan
155         {0xaa, 0xaa},   // 0xed = kasratan
156         {0xab, 0xab},   // 0xee = fatha
157         {0xac, 0xac},   // 0xef = damma
158
159         {0xad, 0xad},   // 0xf0 = kasra
160         {0xae, 0xae},   // 0xf1 = shadda
161         {0xaf, 0xaf},   // 0xf2 = sukun
162         {0,0}, // 0xf3
163         {0,0}, // 0xf4
164         {0,0}, // 0xf5
165         {0,0}, // 0xf6
166         {0,0}, // 0xf7
167         {0,0}, // 0xf8
168         {0,0}, // 0xf9
169         {0,0}, // 0xfa
170         {0,0}, // 0xfb
171         {0,0}, // 0xfc
172         {0,0}, // 0xfd
173         {0,0}, // 0xfe
174         {0,0} // 0xff
175 };
176
177
178 char_type const arabic_start = 0xc1;
179
180 } // namespace anon
181
182
183
184 bool Encodings::isComposeChar_hebrew(char_type c)
185 {
186         return c <= 0xd2 && c >= 0xc0 &&
187                 c != 0xce && c != 0xd0;
188 }
189
190
191 // Special Arabic letters are ones that do not get connected from left
192 // they are hamza, alef_madda, alef_hamza, waw_hamza, alef_hamza_under,
193 // alef, tah_marbota, dal, thal, rah, zai, wow, alef_maksoura
194
195 bool Encodings::is_arabic_special(char_type c)
196 {
197         return  (c >= 0xc1 && c <= 0xc5) ||
198                  c == 0xc7 || c  == 0xc9  ||
199                  c == 0xcf || c  == 0xe8  ||
200                 (c >= 0xd0 && c <= 0xd2) ||
201                  c == 0xe9;
202 }
203
204 bool Encodings::isComposeChar_arabic(char_type c)
205 {
206         return c >= 0xeb && c <= 0xf2;
207 }
208
209
210 bool Encodings::is_arabic(char_type c)
211 {
212         return c >= arabic_start && arabic_table[c-arabic_start][0];
213 }
214
215
216 char_type Encodings::transformChar(char_type c,
217                                       Encodings::Letter_Form form)
218 {
219         if (!is_arabic(c))
220                 return c;
221
222         if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
223                 return arabic_table2[c-arabic_start][form];
224         else
225                 return arabic_table[c-arabic_start][form >> 1];
226 }
227
228
229 Encoding const * Encodings::getFromLyXName(string const & name) const
230 {
231         EncodingList::const_iterator it = encodinglist.find(name);
232         if (it != encodinglist.end())
233                 return &it->second;
234         else
235                 return 0;
236 }
237
238
239 namespace {
240
241 class LaTeXNamesEqual : public std::unary_function<std::pair<std::string, Encoding>, bool> {
242         public:
243                 LaTeXNamesEqual(string const & LaTeXName)
244                         : LaTeXName_(LaTeXName) {}
245                 bool operator()(std::pair<std::string, Encoding> const & encoding) const
246                 {
247                         return encoding.second.latexName() == LaTeXName_;
248                 }
249         private:
250                 string LaTeXName_;
251 };
252
253 } // namespace anon
254
255
256 Encoding const * Encodings::getFromLaTeXName(string const & name) const
257 {
258         EncodingList::const_iterator const it =
259                 std::find_if(encodinglist.begin(), encodinglist.end(),
260                              LaTeXNamesEqual(name));
261         if (it != encodinglist.end())
262                 return &it->second;
263         else
264                 return 0;
265 }
266
267
268 Encodings::Encodings()
269 {
270 }
271
272 void Encodings::read(support::FileName const & filename)
273 {
274         enum Encodingtags {
275                 et_encoding = 1,
276                 et_end,
277                 et_last
278         };
279
280         struct keyword_item encodingtags[et_last - 1] = {
281                 { "encoding", et_encoding },
282                 { "end", et_end }
283         };
284
285         LyXLex lex(encodingtags, et_last - 1);
286         lex.setFile(filename);
287         while (lex.isOK()) {
288                 switch (lex.lex()) {
289                 case et_encoding:
290                 {
291                         lex.next();
292                         string const name = lex.getString();
293                         lex.next();
294                         string const latexname = lex.getString();
295                         lex.next();
296                         string const iconvname = lex.getString();
297                         lyxerr[Debug::INFO] << "Reading encoding " << name << endl;
298                         encodinglist[name] = Encoding(name, latexname, iconvname);
299                         if (lex.lex() != et_end)
300                                 lex.printError("Encodings::read: "
301                                                "missing end");
302                         break;
303                 }
304                 case et_end:
305                         lex.printError("Encodings::read: Misplaced end");
306                         break;
307                 case LyXLex::LEX_FEOF:
308                         break;
309                 default:
310                         lex.printError("Encodings::read: "
311                                        "Unknown tag: `$$Token'");
312                         break;
313                 }
314         }
315 }
316
317
318 } // namespace lyx