]> git.lyx.org Git - lyx.git/blob - src/encoding.C
Fix this bug:
[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 "LaTeXFeatures.h"
19 #include "lyxlex.h"
20 #include "lyxrc.h"
21
22 #include "support/filename.h"
23 #include "support/lstrings.h"
24 #include "support/unicode.h"
25
26
27 namespace lyx {
28
29 using support::FileName;
30
31 #ifndef CXX_GLOBAL_CSTD
32 using std::strtol;
33 #endif
34
35 using std::endl;
36 using std::string;
37
38
39 Encodings encodings;
40
41 namespace {
42
43 char_type arabic_table2[63][4] = {
44         {0x41, 0x41, 0x41, 0x41}, // 0xc1 = hamza
45         {0x42, 0xa1, 0x42, 0xa1}, // 0xc2 = ligature madda on alef
46         {0x43, 0xa2, 0x43, 0xa2}, // 0xc3 = ligature hamza on alef
47         {0x44, 0xa3, 0x44, 0xa3}, // 0xc4 = ligature hamza on waw
48         {0x45, 0xa4, 0x45, 0xa4}, // 0xc5 = ligature hamza under alef
49         {0x46, 0xf9, 0xf8, 0xa0}, // 0xc6 = ligature hamza on ya
50         {0x47, 0xa5, 0x47, 0xa5}, // 0xc7 = alef
51         {0x48, 0xae, 0xac, 0xad}, // 0xc8 = baa
52         {0x49, 0xb1, 0x49, 0xb1}, // 0xc9 = taa marbuta
53         {0x4a, 0xb4, 0xb2, 0xb3}, // 0xca = taa
54         {0x4b, 0xb7, 0xb5, 0xb6}, // 0xcb = thaa
55         {0x4c, 0xba, 0xb8, 0xb9}, // 0xcc = jeem
56         {0x4d, 0xbd, 0xbb, 0xbc}, // 0xcd = haa
57         {0x4e, 0xc0, 0xbe, 0xbf}, // 0xce = khaa
58         {0x4f, 0xa6, 0x4f, 0xa6}, // 0xcf = dal
59
60         {0x50, 0xa7, 0x50, 0xa7}, // 0xd0 = thal
61         {0x51, 0xa8, 0x51, 0xa8}, // 0xd1 = ra
62         {0x52, 0xa9, 0x52, 0xa9}, // 0xd2 = zain
63         {0x53, 0xc3, 0xc1, 0xc2}, // 0xd3 = seen
64         {0x54, 0xc6, 0xc4, 0xc5}, // 0xd4 = sheen
65         {0x55, 0xc9, 0xc7, 0xc8}, // 0xd5 = sad
66         {0x56, 0xcc, 0xca, 0xcb}, // 0xd6 = dad
67         {0x57, 0xcf, 0xcd, 0xce}, // 0xd7 = tah
68         {0x58, 0xd2, 0xd0, 0xd1}, // 0xd8 = zah
69         {0x59, 0xd5, 0xd3, 0xd4}, // 0xd9 = ain
70         {0x5a, 0xd8, 0xd6, 0xd7}, // 0xda = ghain
71         {0,0,0,0}, // 0xdb
72         {0,0,0,0}, // 0xdc
73         {0,0,0,0}, // 0xdd
74         {0,0,0,0}, // 0xde
75         {0,0,0,0}, // 0xdf
76
77         {0,0,0,0}, // 0xe0
78         {0x61, 0xdb, 0xd9, 0xda}, // 0xe1 = fa
79         {0x62, 0xde, 0xdc, 0xdd}, // 0xe2 = qaf
80         {0x63, 0xe1, 0xdf, 0xe0}, // 0xe3 = kaf
81         {0x64, 0xe4, 0xe2, 0xe3}, // 0xe4 = lam
82         {0x65, 0xe7, 0xe5, 0xe6}, // 0xe5 = meem
83         {0x66, 0xea, 0xe8, 0xe9}, // 0xe6 = noon
84         {0x67, 0xed, 0xeb, 0xec}, // 0xe7 = ha
85         {0x68, 0xaa, 0x68, 0xaa}, // 0xe8 = waw
86         {0x69, 0xab, 0x69, 0xab}, // 0xe9 = alef maksura
87         {0x6a, 0xf0, 0xee, 0xef}, // 0xea = ya
88         {0x6b, 0x6b, 0x6b, 0x6b}, // 0xeb = fathatan
89         {0x6c, 0x6c, 0x6c, 0x6c}, // 0xec = dammatan
90         {0x6d, 0x6d, 0x6d, 0x6d}, // 0xed = kasratan
91         {0x6e, 0x6e, 0x6e, 0x6e}, // 0xee = fatha
92         {0x6f, 0x6f, 0x6f, 0x6f}, // 0xef = damma
93
94         {0x70, 0x70, 0x70, 0x70}, // 0xf0 = kasra
95         {0x71, 0x71, 0x71, 0x71}, // 0xf1 = shadda
96         {0x72, 0x72, 0x72, 0x72}, // 0xf2 = sukun
97         {0,0,0,0}, // 0xf3
98         {0,0,0,0}, // 0xf4
99         {0,0,0,0}, // 0xf5
100         {0,0,0,0}, // 0xf6
101         {0,0,0,0}, // 0xf7
102         {0,0,0,0}, // 0xf8
103         {0,0,0,0}, // 0xf9
104         {0,0,0,0}, // 0xfa
105         {0,0,0,0}, // 0xfb
106         {0,0,0,0}, // 0xfc
107         {0,0,0,0}, // 0xfd
108         {0,0,0,0}, // 0xfe
109         {0,0,0,0}, // 0xff
110 };
111
112
113 char_type arabic_table[63][2] = {
114         {0xc1, 0xc1}, // 0xc1 = hamza
115         {0xc2, 0xc2}, // 0xc2 = ligature madda on alef
116         {0xc3, 0xc3}, // 0xc3 = ligature hamza on alef
117         {0xc4, 0xc4}, // 0xc4 = ligature hamza on waw
118         {0xc5, 0xc5}, // 0xc5 = ligature hamza under alef
119         {0xc6, 0xc0}, // 0xc6 = ligature hamza on ya
120         {0xc7, 0xc7}, // 0xc7 = alef
121         {0xc8, 0xeb}, // 0xc8 = baa
122         {0xc9, 0xc9}, // 0xc9 = taa marbuta
123         {0xca, 0xec}, // 0xca = taa
124         {0xcb, 0xed}, // 0xcb = thaa
125         {0xcc, 0xee}, // 0xcc = jeem
126         {0xcd, 0xef}, // 0xcd = haa
127         {0xce, 0xf0}, // 0xce = khaa
128         {0xcf, 0xcf}, // 0xcf = dal
129
130         {0xd0, 0xd0}, // 0xd0 = thal
131         {0xd1, 0xd1}, // 0xd1 = ra
132         {0xd2, 0xd2}, // 0xd2 = zain
133         {0xd3, 0xf1}, // 0xd3 = seen
134         {0xd4, 0xf2}, // 0xd4 = sheen
135         {0xd5, 0xf3}, // 0xd5 = sad
136         {0xd6, 0xf4}, // 0xd6 = dad
137         {0xd7, 0xd7}, // 0xd7 = tah
138         {0xd8, 0xd8}, // 0xd8 = zah
139         {0xd9, 0xf5}, // 0xd9 = ain
140         {0xda, 0xf6}, // 0xda = ghain
141         {0,0}, // 0xdb
142         {0,0}, // 0xdc
143         {0,0}, // 0xdd
144         {0,0}, // 0xde
145         {0,0}, // 0xdf
146
147         {0,0},  // 0xe0
148         {0xe1, 0xf7},   // 0xe1 = fa
149         {0xe2, 0xf8},   // 0xe2 = qaf
150         {0xe3, 0xf9},   // 0xe3 = kaf
151         {0xe4, 0xfa},   // 0xe4 = lam
152         {0xe5, 0xfb},   // 0xe5 = meem
153         {0xe6, 0xfc},   // 0xe6 = noon
154         {0xe7, 0xfd},   // 0xe7 = ha
155         {0xe8, 0xe8},   // 0xe8 = waw
156         {0xe9, 0xe9},   // 0xe9 = alef maksura
157         {0xea, 0xfe},   // 0xea = ya
158         {0xa8, 0xa8},   // 0xeb = fathatan
159         {0xa9, 0xa9},   // 0xec = dammatan
160         {0xaa, 0xaa},   // 0xed = kasratan
161         {0xab, 0xab},   // 0xee = fatha
162         {0xac, 0xac},   // 0xef = damma
163
164         {0xad, 0xad},   // 0xf0 = kasra
165         {0xae, 0xae},   // 0xf1 = shadda
166         {0xaf, 0xaf},   // 0xf2 = sukun
167         {0,0}, // 0xf3
168         {0,0}, // 0xf4
169         {0,0}, // 0xf5
170         {0,0}, // 0xf6
171         {0,0}, // 0xf7
172         {0,0}, // 0xf8
173         {0,0}, // 0xf9
174         {0,0}, // 0xfa
175         {0,0}, // 0xfb
176         {0,0}, // 0xfc
177         {0,0}, // 0xfd
178         {0,0}, // 0xfe
179         {0,0} // 0xff
180 };
181
182
183 char_type const arabic_start = 0xc1;
184
185
186 /// Information about a single UCS4 character
187 struct CharInfo {
188         /// LaTeX command for this character
189         docstring command;
190         /// Needed LaTeX preamble (or feature)
191         string preamble;
192         /// Is this a combining character?
193         bool combining;
194         /// Is \c preamble a feature known by LaTeXFeatures, or a raw LaTeX
195         /// command?
196         bool feature;
197         /// Always force the LaTeX command, even if the encoding contains
198         /// this character?
199         bool force;
200 };
201
202
203 typedef std::map<char_type, CharInfo> CharInfoMap;
204 CharInfoMap unicodesymbols;
205
206 } // namespace anon
207
208
209 Encoding::Encoding(string const & n, string const & l, string const & i)
210         : Name_(n), LatexName_(l), iconvName_(i)
211 {
212         if (n == "utf8")
213                 // UTF8 can encode all 1<<20 + 1<<16 UCS4 code points
214                 start_encodable_ = 0x110000;
215         else {
216                 start_encodable_ = 0;
217                 // temporarily switch off lyxerr, since we will generate iconv errors
218                 lyxerr.disable();
219                 for (unsigned short j = 0; j < 256; ++j) {
220                         char const c = j;
221                         std::vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, i);
222                         if (ucs4.size() == 1) {
223                                 char_type const c = ucs4[0];
224                                 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
225                                 if (it == unicodesymbols.end() || !it->second.force)
226                                         encodable_.insert(c);
227                         }
228                 }
229                 lyxerr.enable();
230                 CharSet::iterator it = encodable_.find(start_encodable_);
231                 while (it != encodable_.end()) {
232                         encodable_.erase(it);
233                         ++start_encodable_;
234                         it = encodable_.find(start_encodable_);
235                 }
236         }
237 }
238
239
240 docstring const Encoding::latexChar(char_type c) const
241 {
242         if (c < start_encodable_)
243                 return docstring(1, c);
244         if (encodable_.find(c) == encodable_.end()) {
245                 // c cannot be encoded in this encoding
246                 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
247                 if (it == unicodesymbols.end())
248                         lyxerr << "Could not find LaTeX command for character 0x"
249                                << std::hex << c << ".\nLaTeX export will fail."
250                                << endl;
251                 else
252                         return it->second.command;
253         }
254         return docstring(1, c);
255 }
256
257
258 void Encoding::validate(char_type c, LaTeXFeatures & features) const
259 {
260         if (c < start_encodable_)
261                 return;
262
263         if (encodable_.find(c) != encodable_.end())
264                 return;
265
266         // c cannot be encoded in this encoding
267         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
268         if (it != unicodesymbols.end() && !it->second.preamble.empty()) {
269                 if (it->second.feature)
270                         features.require(it->second.preamble);
271                 else
272                         features.addPreambleSnippet(it->second.preamble);
273         }
274 }
275
276
277 bool Encodings::isComposeChar_hebrew(char_type c)
278 {
279         return c <= 0xd2 && c >= 0xc0 &&
280                 c != 0xce && c != 0xd0;
281 }
282
283
284 // Special Arabic letters are ones that do not get connected from left
285 // they are hamza, alef_madda, alef_hamza, waw_hamza, alef_hamza_under,
286 // alef, tah_marbota, dal, thal, rah, zai, wow, alef_maksoura
287
288 bool Encodings::is_arabic_special(char_type c)
289 {
290         return  (c >= 0xc1 && c <= 0xc5) ||
291                  c == 0xc7 || c  == 0xc9  ||
292                  c == 0xcf || c  == 0xe8  ||
293                 (c >= 0xd0 && c <= 0xd2) ||
294                  c == 0xe9;
295 }
296
297 bool Encodings::isComposeChar_arabic(char_type c)
298 {
299         return c >= 0xeb && c <= 0xf2;
300 }
301
302
303 bool Encodings::is_arabic(char_type c)
304 {
305         return c >= arabic_start && arabic_table[c-arabic_start][0];
306 }
307
308
309 char_type Encodings::transformChar(char_type c,
310                                       Encodings::Letter_Form form)
311 {
312         if (!is_arabic(c))
313                 return c;
314
315         if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
316                 return arabic_table2[c-arabic_start][form];
317         else
318                 return arabic_table[c-arabic_start][form >> 1];
319 }
320
321
322 bool Encodings::isCombiningChar(char_type c)
323 {
324         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
325         if (it != unicodesymbols.end())
326                 return it->second.combining;
327         return false;
328 }
329
330
331 Encoding const * Encodings::getFromLyXName(string const & name) const
332 {
333         EncodingList::const_iterator it = encodinglist.find(name);
334         if (it != encodinglist.end())
335                 return &it->second;
336         else
337                 return 0;
338 }
339
340
341 Encoding const * Encodings::getFromLaTeXName(string const & name) const
342 {
343         // We don't use std::find_if because it makes copies of the pairs in
344         // the map.
345         // This linear search is OK since we don't have many encodings.
346         // Users could even optimize it by putting the encodings they use
347         // most at the top of lib/encodings.
348         EncodingList::const_iterator const end = encodinglist.end();
349         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
350                 if (it->second.latexName() == name)
351                         return &it->second;
352         return 0;
353 }
354
355
356 Encodings::Encodings()
357 {
358 }
359
360
361 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
362 {
363         // We must read the symbolsfile first, because the Encoding
364         // constructor depends on it.
365         LyXLex symbolslex(0, 0);
366         symbolslex.setFile(symbolsfile);
367         while (symbolslex.isOK()) {
368                 char_type symbol;
369                 CharInfo info;
370                 string flags;
371
372                 if (symbolslex.next(true)) {
373                         std::istringstream is(symbolslex.getString());
374                         // reading symbol directly does not work if
375                         // char_type == std::wchar_t.
376                         boost::uint32_t tmp;
377                         if(!(is >> std::hex >> tmp))
378                                 break;
379                         symbol = tmp;
380                 } else
381                         break;
382                 if (symbolslex.next(true))
383                         info.command = symbolslex.getDocString();
384                 else
385                         break;
386                 if (symbolslex.next(true))
387                         info.preamble = symbolslex.getString();
388                 else
389                         break;
390                 if (symbolslex.next(true))
391                         flags = symbolslex.getString();
392                 else
393                         break;
394
395                 info.combining = false;
396                 info.force = false;
397                 while (!flags.empty()) {
398                         string flag;
399                         flags = support::split(flags, flag, ',');
400                         if (flag == "combining")
401                                 info.combining = true;
402                         else if (flag == "force")
403                                 info.force = true;
404                         else
405                                 lyxerr << "Ignoring unknown flag `" << flag
406                                        << "' for symbol `0x" << std::hex
407                                        << symbol << "'." << endl;
408                 }
409
410                 if (!info.preamble.empty())
411                         info.feature = info.preamble[0] != '\\';
412
413                 lyxerr[Debug::INFO]
414                         << "Read unicode symbol " << symbol << " '"
415                         << to_utf8(info.command) << "' '" << info.preamble
416                         << "' " << info.combining << ' ' << info.feature
417                         << endl;
418                 unicodesymbols[symbol] = info;
419         }
420
421         // Now read the encodings
422         enum Encodingtags {
423                 et_encoding = 1,
424                 et_end,
425                 et_last
426         };
427
428         struct keyword_item encodingtags[et_last - 1] = {
429                 { "encoding", et_encoding },
430                 { "end", et_end }
431         };
432
433         LyXLex lex(encodingtags, et_last - 1);
434         lex.setFile(encfile);
435         while (lex.isOK()) {
436                 switch (lex.lex()) {
437                 case et_encoding:
438                 {
439                         lex.next();
440                         string const name = lex.getString();
441                         lex.next();
442                         string const latexname = lex.getString();
443                         lex.next();
444                         string const iconvname = lex.getString();
445                         lyxerr[Debug::INFO] << "Reading encoding " << name << endl;
446                         encodinglist[name] = Encoding(name, latexname, iconvname);
447                         if (lex.lex() != et_end)
448                                 lex.printError("Encodings::read: "
449                                                "missing end");
450                         break;
451                 }
452                 case et_end:
453                         lex.printError("Encodings::read: Misplaced end");
454                         break;
455                 case LyXLex::LEX_FEOF:
456                         break;
457                 default:
458                         lex.printError("Encodings::read: "
459                                        "Unknown tag: `$$Token'");
460                         break;
461                 }
462         }
463 }
464
465
466 } // namespace lyx