]> git.lyx.org Git - lyx.git/blob - src/Encoding.cpp
Add support for Farsi (thanks to Mostafa Vahedi).
[lyx.git] / src / Encoding.cpp
1 /**
2  * \file Encoding.cpp
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 "Lexer.h"
20 #include "LyXRC.h"
21
22 #include "support/FileName.h"
23 #include "support/lstrings.h"
24 #include "support/unicode.h"
25
26 #include <sstream>
27
28
29 namespace lyx {
30
31 using support::FileName;
32
33 #ifndef CXX_GLOBAL_CSTD
34 using std::strtol;
35 #endif
36
37 using std::endl;
38 using std::string;
39
40
41 Encodings encodings;
42
43 namespace {
44
45 char_type arabic_table[172][4] = {
46         {0xfe80, 0xfe80, 0xfe80, 0xfe80}, // 0x0621 = hamza
47         {0xfe81, 0xfe82, 0xfe81, 0xfe82}, // 0x0622 = ligature madda on alef
48         {0xfe83, 0xfe84, 0xfe83, 0xfe84}, // 0x0623 = ligature hamza on alef
49         {0xfe85, 0xfe86, 0xfe85, 0xfe86}, // 0x0624 = ligature hamza on waw
50         {0xfe87, 0xfe88, 0xfe87, 0xfe88}, // 0x0625 = ligature hamza under alef
51         {0xfe89, 0xfe8a, 0xfe8b, 0xfe8c}, // 0x0626 = ligature hamza on ya
52         {0xfe8d, 0xfe8e, 0xfe8d, 0xfe8e}, // 0x0627 = alef
53         {0xfe8f, 0xfe90, 0xfe91, 0xfe92}, // 0x0628 = baa
54         {0xfe93, 0xfe94, 0xfe93, 0xfe94}, // 0x0629 = taa marbuta
55         {0xfe95, 0xfe96, 0xfe97, 0xfe98}, // 0x062a = taa
56         {0xfe99, 0xfe9a, 0xfe9b, 0xfe9c}, // 0x062b = thaa
57         {0xfe9d, 0xfe9e, 0xfe9f, 0xfea0}, // 0x062c = jeem
58         {0xfea1, 0xfea2, 0xfea3, 0xfea4}, // 0x062d = haa
59         {0xfea5, 0xfea6, 0xfea7, 0xfea8}, // 0x062e = khaa
60         {0xfea9, 0xfeaa, 0xfea9, 0xfeaa}, // 0x062f = dal
61
62         {0xfeab, 0xfeac, 0xfeab, 0xfeac}, // 0x0630 = thal
63         {0xfead, 0xfeae, 0xfead, 0xfeae}, // 0x0631 = ra
64         {0xfeaf, 0xfeb0, 0xfeaf, 0xfeb0}, // 0x0632 = zain
65         {0xfeb1, 0xfeb2, 0xfeb3, 0xfeb4}, // 0x0633 = seen
66         {0xfeb5, 0xfeb6, 0xfeb7, 0xfeb8}, // 0x0634 = sheen
67         {0xfeb9, 0xfeba, 0xfebb, 0xfebc}, // 0x0635 = sad
68         {0xfebd, 0xfebe, 0xfebf, 0xfec0}, // 0x0636 = dad
69         {0xfec1, 0xfec2, 0xfec3, 0xfec4}, // 0x0637 = tah
70         {0xfec5, 0xfec6, 0xfec7, 0xfec8}, // 0x0638 = zah
71         {0xfec9, 0xfeca, 0xfecb, 0xfecc}, // 0x0639 = ain
72         {0xfecd, 0xfece, 0xfecf, 0xfed0}, // 0x063a = ghain
73         {0, 0, 0, 0}, // 0x063b
74         {0, 0, 0, 0}, // 0x063c
75         {0, 0, 0, 0}, // 0x063d
76         {0, 0, 0, 0}, // 0x063e
77         {0, 0, 0, 0}, // 0x063f
78
79         {0, 0, 0, 0}, // 0x0640
80         {0xfed1, 0xfed2, 0xfed3, 0xfed4}, // 0x0641 = fa
81         {0xfed5, 0xfed6, 0xfed7, 0xfed8}, // 0x0642 = qaf
82         {0xfed9, 0xfeda, 0xfedb, 0xfedc}, // 0x0643 = kaf
83         {0xfedd, 0xfede, 0xfedf, 0xfee0}, // 0x0644 = lam
84         {0xfee1, 0xfee2, 0xfee3, 0xfee4}, // 0x0645 = meem
85         {0xfee5, 0xfee6, 0xfee7, 0xfee8}, // 0x0646 = noon
86         {0xfee9, 0xfeea, 0xfeeb, 0xfeec}, // 0x0647 = ha
87         {0xfeed, 0xfeee, 0xfeed, 0xfeee}, // 0x0648 = waw
88         {0xfeef, 0xfef0, 0xfeef, 0xfef0}, // 0x0649 = alef maksura
89         {0xfef1, 0xfef2, 0xfef3, 0xfef4}, // 0x064a = ya
90         {0x065b, 0x065b, 0x065b, 0x065b}, // 0x064b = fathatan
91         {0x065c, 0x065c, 0x065c, 0x065c}, // 0x064c = dammatan
92         {0x064d, 0x064d, 0x064d, 0x064d}, // 0x064d = kasratan
93         {0x064e, 0x064e, 0x064e, 0x064e}, // 0x064e = fatha
94         {0x064f, 0x064f, 0x064f, 0x064f}, // 0x064f = damma
95
96         {0x0650, 0x0650, 0x0650, 0x0650}, // 0x0650 = kasra
97         {0x0651, 0x0651, 0x0651, 0x0651}, // 0x0651 = shadda
98         {0x0652, 0x0652, 0x0652, 0x0652}, // 0x0652 = sukun
99
100         {0, 0, 0, 0}, // 0x0653
101         {0, 0, 0, 0}, // 0x0654
102         {0, 0, 0, 0}, // 0x0655
103         {0, 0, 0, 0}, // 0x0656
104         {0, 0, 0, 0}, // 0x0657
105         {0, 0, 0, 0}, // 0x0658
106         {0, 0, 0, 0}, // 0x0659
107         {0, 0, 0, 0}, // 0x065a
108         {0, 0, 0, 0}, // 0x065b
109         {0, 0, 0, 0}, // 0x065c
110         {0, 0, 0, 0}, // 0x065d
111         {0, 0, 0, 0}, // 0x065e
112         {0, 0, 0, 0}, // 0x065f
113         {0, 0, 0, 0}, // 0x0660
114         {0, 0, 0, 0}, // 0x0661
115         {0, 0, 0, 0}, // 0x0662
116         {0, 0, 0, 0}, // 0x0663
117         {0, 0, 0, 0}, // 0x0664
118         {0, 0, 0, 0}, // 0x0665
119         {0, 0, 0, 0}, // 0x0666
120         {0, 0, 0, 0}, // 0x0667
121         {0, 0, 0, 0}, // 0x0668
122         {0, 0, 0, 0}, // 0x0669
123         {0, 0, 0, 0}, // 0x066a
124         {0, 0, 0, 0}, // 0x066b
125         {0, 0, 0, 0}, // 0x066c
126         {0, 0, 0, 0}, // 0x066d
127         {0, 0, 0, 0}, // 0x066e
128         {0, 0, 0, 0}, // 0x066f
129         {0, 0, 0, 0}, // 0x0670
130         {0, 0, 0, 0}, // 0x0671
131         {0, 0, 0, 0}, // 0x0672
132         {0, 0, 0, 0}, // 0x0673
133         {0, 0, 0, 0}, // 0x0674
134         {0, 0, 0, 0}, // 0x0675
135         {0, 0, 0, 0}, // 0x0676
136         {0, 0, 0, 0}, // 0x0677
137         {0, 0, 0, 0}, // 0x0678
138         {0, 0, 0, 0}, // 0x0679
139         {0, 0, 0, 0}, // 0x067a
140         {0, 0, 0, 0}, // 0x067b
141         {0, 0, 0, 0}, // 0x067c
142         {0, 0, 0, 0}, // 0x067d
143         {0xfb56, 0xfb57, 0xfb58, 0xfb59}, // 0x067e = peh 
144         {0, 0, 0, 0}, // 0x067f
145         {0, 0, 0, 0}, // 0x0680
146         {0, 0, 0, 0}, // 0x0681
147         {0, 0, 0, 0}, // 0x0682
148         {0, 0, 0, 0}, // 0x0683
149         {0, 0, 0, 0}, // 0x0684
150         {0, 0, 0, 0}, // 0x0685
151         {0xfb7a, 0xfb7b, 0xfb7c, 0xfb7d}, // 0x0686 = tcheh 
152         {0, 0, 0, 0}, // 0x0687
153         {0, 0, 0, 0}, // 0x0688
154         {0, 0, 0, 0}, // 0x0689
155         {0, 0, 0, 0}, // 0x068a
156         {0, 0, 0, 0}, // 0x068b
157         {0, 0, 0, 0}, // 0x068c
158         {0, 0, 0, 0}, // 0x068d
159         {0, 0, 0, 0}, // 0x068e
160         {0, 0, 0, 0}, // 0x068f
161         {0, 0, 0, 0}, // 0x0690
162         {0, 0, 0, 0}, // 0x0691
163         {0, 0, 0, 0}, // 0x0692
164         {0, 0, 0, 0}, // 0x0693
165         {0, 0, 0, 0}, // 0x0694
166         {0, 0, 0, 0}, // 0x0695
167         {0, 0, 0, 0}, // 0x0696
168         {0, 0, 0, 0}, // 0x0697
169         {0xfb8a, 0xfb8b, 0xfb8a, 0xfb8b}, // 0x0698 = jeh
170         {0, 0, 0, 0}, // 0x0699
171         {0, 0, 0, 0}, // 0x069a
172         {0, 0, 0, 0}, // 0x069b
173         {0, 0, 0, 0}, // 0x069c
174         {0, 0, 0, 0}, // 0x069d
175         {0, 0, 0, 0}, // 0x069e
176         {0, 0, 0, 0}, // 0x069f
177         {0, 0, 0, 0}, // 0x06a0
178         {0, 0, 0, 0}, // 0x06a1
179         {0, 0, 0, 0}, // 0x06a2
180         {0, 0, 0, 0}, // 0x06a3
181         {0, 0, 0, 0}, // 0x06a4
182         {0, 0, 0, 0}, // 0x06a5
183         {0, 0, 0, 0}, // 0x06a6
184         {0, 0, 0, 0}, // 0x06a7
185         {0, 0, 0, 0}, // 0x06a8
186         {0xfb8e, 0xfb8f, 0xfb90, 0xfb91}, // 0x06a9 = farsi kaf 
187         {0, 0, 0, 0}, // 0x06aa
188         {0, 0, 0, 0}, // 0x06ab
189         {0, 0, 0, 0}, // 0x06ac
190         {0, 0, 0, 0}, // 0x06ad
191         {0, 0, 0, 0}, // 0x06ae
192         {0xfb92, 0xfb93, 0xfb94, 0xfb95}, // 0x06af = gaf 
193         {0, 0, 0, 0}, // 0x06b0
194         {0, 0, 0, 0}, // 0x06b1
195         {0, 0, 0, 0}, // 0x06b2
196         {0, 0, 0, 0}, // 0x06b3
197         {0, 0, 0, 0}, // 0x06b4
198         {0, 0, 0, 0}, // 0x06b5
199         {0, 0, 0, 0}, // 0x06b6
200         {0, 0, 0, 0}, // 0x06b7
201         {0, 0, 0, 0}, // 0x06b8
202         {0, 0, 0, 0}, // 0x06b9
203         {0, 0, 0, 0}, // 0x06ba
204         {0, 0, 0, 0}, // 0x06bb
205         {0, 0, 0, 0}, // 0x06bc
206         {0, 0, 0, 0}, // 0x06bd
207         {0, 0, 0, 0}, // 0x06be
208         {0, 0, 0, 0}, // 0x06bf
209         {0, 0, 0, 0}, // 0x06c0
210         {0, 0, 0, 0}, // 0x06c1
211         {0, 0, 0, 0}, // 0x06c2
212         {0, 0, 0, 0}, // 0x06c3
213         {0, 0, 0, 0}, // 0x06c4
214         {0, 0, 0, 0}, // 0x06c5
215         {0, 0, 0, 0}, // 0x06c6
216         {0, 0, 0, 0}, // 0x06c7
217         {0, 0, 0, 0}, // 0x06c8
218         {0, 0, 0, 0}, // 0x06c9
219         {0, 0, 0, 0}, // 0x06ca
220         {0, 0, 0, 0}, // 0x06cb
221         {0xfbfc, 0xfbfd, 0xfbfe, 0xfbff} // 0x06cc = farsi yeh  
222 };
223
224
225 char_type const arabic_start = 0x0621;
226 char_type const arabic_end = 0x06cc;
227
228
229 /// Information about a single UCS4 character
230 struct CharInfo {
231         /// LaTeX command for this character
232         docstring command;
233         /// Needed LaTeX preamble (or feature)
234         string preamble;
235         /// Is this a combining character?
236         bool combining;
237         /// Is \c preamble a feature known by LaTeXFeatures, or a raw LaTeX
238         /// command?
239         bool feature;
240         /// Always force the LaTeX command, even if the encoding contains
241         /// this character?
242         bool force;
243 };
244
245
246 typedef std::map<char_type, CharInfo> CharInfoMap;
247 CharInfoMap unicodesymbols;
248
249
250 /// The highest code point in UCS4 encoding (1<<20 + 1<<16)
251 char_type const max_ucs4 = 0x110000;
252
253 } // namespace anon
254
255
256 Encoding::Encoding(string const & n, string const & l, string const & i,
257                    bool f, Encoding::Package p)
258         : Name_(n), LatexName_(l), iconvName_(i), fixedwidth_(f), package_(p)
259 {
260         if (n == "ascii") {
261                 // ASCII can encode 128 code points and nothing else
262                 start_encodable_ = 128;
263                 complete_ = true;
264         } else if (i == "UTF-8") {
265                 // UTF8 can encode all UCS4 code points
266                 start_encodable_ = max_ucs4;
267                 complete_ = true;
268         } else {
269                 complete_ = false;
270         }
271 }
272
273
274 void Encoding::init() const
275 {
276         start_encodable_ = 0;
277         // temporarily switch off lyxerr, since we will generate iconv errors
278         lyxerr.disable();
279         if (fixedwidth_) {
280                 // We do not need to check all UCS4 code points, it is enough
281                 // if we check all 256 code points of this encoding.
282                 for (unsigned short j = 0; j < 256; ++j) {
283                         char const c = j;
284                         std::vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, iconvName_);
285                         if (ucs4.size() == 1) {
286                                 char_type const c = ucs4[0];
287                                 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
288                                 if (it == unicodesymbols.end() || !it->second.force)
289                                         encodable_.insert(c);
290                         }
291                 }
292         } else {
293                 // We do not know how many code points this encoding has, and
294                 // they do not have a direct representation as a single byte,
295                 // therefore we need to check all UCS4 code points.
296                 // This is expensive!
297                 for (char_type c = 0; c < max_ucs4; ++c) {
298                         std::vector<char> const eightbit = ucs4_to_eightbit(&c, 1, iconvName_);
299                         if (!eightbit.empty()) {
300                                 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
301                                 if (it == unicodesymbols.end() || !it->second.force)
302                                         encodable_.insert(c);
303                         }
304                 }
305         }
306         lyxerr.enable();
307         CharSet::iterator it = encodable_.find(start_encodable_);
308         while (it != encodable_.end()) {
309                 encodable_.erase(it);
310                 ++start_encodable_;
311                 it = encodable_.find(start_encodable_);
312         }
313         complete_ = true;
314 }
315
316
317 docstring const Encoding::latexChar(char_type c) const
318 {
319         // assure the used encoding is properly initialized
320         if (!complete_)
321                 init();
322         BOOST_ASSERT(complete_);
323
324         if (c < start_encodable_)
325                 return docstring(1, c);
326         if (encodable_.find(c) == encodable_.end()) {
327                 // c cannot be encoded in this encoding
328                 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
329                 if (it == unicodesymbols.end())
330                         lyxerr << "Could not find LaTeX command for character 0x"
331                                << std::hex << c << std::dec
332                                << ".\nLaTeX export will fail."
333                                << endl;
334                 else
335                         return it->second.command;
336         }
337         return docstring(1, c);
338 }
339
340
341 void Encodings::validate(char_type c, LaTeXFeatures & features)
342 {
343         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
344         if (it != unicodesymbols.end() && !it->second.preamble.empty()) {
345                 if (it->second.feature)
346                         features.require(it->second.preamble);
347                 else
348                         features.addPreambleSnippet(it->second.preamble);
349         }
350 }
351
352
353 bool Encodings::isComposeChar_hebrew(char_type c)
354 {
355         return c <= 0x05c2 && c >= 0x05b0 &&
356                c != 0x05be && c != 0x05c0;
357 }
358
359
360 // Special Arabic letters are ones that do not get connected from left
361 // they are hamza, alef_madda, alef_hamza, waw_hamza, alef_hamza_under,
362 // alef, tah_marbota, dal, thal, rah, zai, wow, alef_maksoura
363
364 bool Encodings::is_arabic_special(char_type c)
365 {
366         return (c >= 0x0621 && c <= 0x0625) ||
367                 c == 0x0627 || c == 0x0629  ||
368                 c == 0x062f || c == 0x0648  ||
369                (c >= 0x0630 && c <= 0x0632) ||
370                 c == 0x0649 || c == 0x0698;
371 }
372
373
374 bool Encodings::isComposeChar_arabic(char_type c)
375 {
376         return c >= 0x064b && c <= 0x0652;
377 }
378
379
380 bool Encodings::is_arabic(char_type c)
381 {
382         return c >= arabic_start && c <= arabic_end &&
383                arabic_table[c-arabic_start][0];
384 }
385
386
387 char_type Encodings::transformChar(char_type c,
388                                       Encodings::Letter_Form form)
389 {
390         if (!is_arabic(c))
391                 return c;
392
393         return arabic_table[c-arabic_start][form];
394 }
395
396
397 bool Encodings::isCombiningChar(char_type c)
398 {
399         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
400         if (it != unicodesymbols.end())
401                 return it->second.combining;
402         return false;
403 }
404
405
406 Encoding const * Encodings::getFromLyXName(string const & name) const
407 {
408         EncodingList::const_iterator it = encodinglist.find(name);
409         if (it != encodinglist.end())
410                 return &it->second;
411         else
412                 return 0;
413 }
414
415
416 Encoding const * Encodings::getFromLaTeXName(string const & name) const
417 {
418         // We don't use std::find_if because it makes copies of the pairs in
419         // the map.
420         // This linear search is OK since we don't have many encodings.
421         // Users could even optimize it by putting the encodings they use
422         // most at the top of lib/encodings.
423         EncodingList::const_iterator const end = encodinglist.end();
424         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
425                 if (it->second.latexName() == name)
426                         return &it->second;
427         return 0;
428 }
429
430
431 Encodings::Encodings()
432 {
433 }
434
435
436 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
437 {
438         // We must read the symbolsfile first, because the Encoding
439         // constructor depends on it.
440         Lexer symbolslex(0, 0);
441         symbolslex.setFile(symbolsfile);
442         while (symbolslex.isOK()) {
443                 char_type symbol;
444                 CharInfo info;
445                 string flags;
446
447                 if (symbolslex.next(true)) {
448                         std::istringstream is(symbolslex.getString());
449                         // reading symbol directly does not work if
450                         // char_type == std::wchar_t.
451                         boost::uint32_t tmp;
452                         if(!(is >> std::hex >> tmp))
453                                 break;
454                         symbol = tmp;
455                 } else
456                         break;
457                 if (symbolslex.next(true))
458                         info.command = symbolslex.getDocString();
459                 else
460                         break;
461                 if (symbolslex.next(true))
462                         info.preamble = symbolslex.getString();
463                 else
464                         break;
465                 if (symbolslex.next(true))
466                         flags = symbolslex.getString();
467                 else
468                         break;
469
470                 info.combining = false;
471                 info.feature = false;
472                 info.force = false;
473                 while (!flags.empty()) {
474                         string flag;
475                         flags = support::split(flags, flag, ',');
476                         if (flag == "combining")
477                                 info.combining = true;
478                         else if (flag == "force")
479                                 info.force = true;
480                         else
481                                 lyxerr << "Ignoring unknown flag `" << flag
482                                        << "' for symbol `0x"
483                                        << std::hex << symbol << std::dec
484                                        << "'." << endl;
485                 }
486
487                 if (!info.preamble.empty())
488                         info.feature = info.preamble[0] != '\\';
489
490                 LYXERR(Debug::INFO)
491                         << "Read unicode symbol " << symbol << " '"
492                         << to_utf8(info.command) << "' '" << info.preamble
493                         << "' " << info.combining << ' ' << info.feature
494                         << endl;
495                 unicodesymbols[symbol] = info;
496         }
497
498         // Now read the encodings
499         enum Encodingtags {
500                 et_encoding = 1,
501                 et_end,
502                 et_last
503         };
504
505         struct keyword_item encodingtags[et_last - 1] = {
506                 { "encoding", et_encoding },
507                 { "end", et_end }
508         };
509
510         Lexer lex(encodingtags, et_last - 1);
511         lex.setFile(encfile);
512         while (lex.isOK()) {
513                 switch (lex.lex()) {
514                 case et_encoding:
515                 {
516                         lex.next();
517                         string const name = lex.getString();
518                         lex.next();
519                         string const latexname = lex.getString();
520                         lex.next();
521                         string const iconvname = lex.getString();
522                         lex.next();
523                         string const width = lex.getString();
524                         bool fixedwidth;
525                         if (width == "fixed")
526                                 fixedwidth = true;
527                         else if (width == "variable")
528                                 fixedwidth = false;
529                         else
530                                 lex.printError("Encodings::read: "
531                                                "Unknown width: `$$Token'");
532                         lex.next();
533                         string const p = lex.getString();
534                         Encoding::Package package;
535                         if (p == "none")
536                                 package = Encoding::none;
537                         else if (p == "inputenc")
538                                 package = Encoding::inputenc;
539                         else if (p == "CJK")
540                                 package = Encoding::CJK;
541                         else
542                                 lex.printError("Encodings::read: "
543                                                "Unknown package: `$$Token'");
544                         LYXERR(Debug::INFO) << "Reading encoding " << name << endl;
545                         encodinglist[name] = Encoding(name, latexname,
546                                                       iconvname, fixedwidth,
547                                                       package);
548                         if (lex.lex() != et_end)
549                                 lex.printError("Encodings::read: "
550                                                "missing end");
551                         break;
552                 }
553                 case et_end:
554                         lex.printError("Encodings::read: Misplaced end");
555                         break;
556                 case Lexer::LEX_FEOF:
557                         break;
558                 default:
559                         lex.printError("Encodings::read: "
560                                        "Unknown tag: `$$Token'");
561                         break;
562                 }
563         }
564 }
565
566
567 } // namespace lyx