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