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