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