]> git.lyx.org Git - lyx.git/blob - src/Encoding.cpp
69437bf2f5192adb260084690ea2f2f272cada16
[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 "Buffer.h"
18 #include "InsetIterator.h"
19 #include "LaTeXFeatures.h"
20 #include "Lexer.h"
21 #include "LyXRC.h"
22
23 #include "support/debug.h"
24 #include "support/FileName.h"
25 #include "support/lstrings.h"
26 #include "support/textutils.h"
27 #include "support/unicode.h"
28
29 #include <boost/cstdint.hpp>
30
31 #include <sstream>
32
33 using namespace std;
34 using namespace lyx::support;
35
36 namespace lyx {
37
38 Encodings encodings;
39
40 Encodings::MathCommandSet Encodings::mathcmd;
41 Encodings::TextCommandSet Encodings::textcmd;
42 Encodings::MathSymbolSet  Encodings::mathsym;
43
44 namespace {
45
46 char_type arabic_table[172][4] = {
47         {0xfe80, 0xfe80, 0xfe80, 0xfe80}, // 0x0621 = hamza
48         {0xfe81, 0xfe82, 0xfe81, 0xfe82}, // 0x0622 = ligature madda on alef
49         {0xfe83, 0xfe84, 0xfe83, 0xfe84}, // 0x0623 = ligature hamza on alef
50         {0xfe85, 0xfe86, 0xfe85, 0xfe86}, // 0x0624 = ligature hamza on waw
51         {0xfe87, 0xfe88, 0xfe87, 0xfe88}, // 0x0625 = ligature hamza under alef
52         {0xfe89, 0xfe8a, 0xfe8b, 0xfe8c}, // 0x0626 = ligature hamza on ya
53         {0xfe8d, 0xfe8e, 0xfe8d, 0xfe8e}, // 0x0627 = alef
54         {0xfe8f, 0xfe90, 0xfe91, 0xfe92}, // 0x0628 = baa
55         {0xfe93, 0xfe94, 0xfe93, 0xfe94}, // 0x0629 = taa marbuta
56         {0xfe95, 0xfe96, 0xfe97, 0xfe98}, // 0x062a = taa
57         {0xfe99, 0xfe9a, 0xfe9b, 0xfe9c}, // 0x062b = thaa
58         {0xfe9d, 0xfe9e, 0xfe9f, 0xfea0}, // 0x062c = jeem
59         {0xfea1, 0xfea2, 0xfea3, 0xfea4}, // 0x062d = haa
60         {0xfea5, 0xfea6, 0xfea7, 0xfea8}, // 0x062e = khaa
61         {0xfea9, 0xfeaa, 0xfea9, 0xfeaa}, // 0x062f = dal
62
63         {0xfeab, 0xfeac, 0xfeab, 0xfeac}, // 0x0630 = thal
64         {0xfead, 0xfeae, 0xfead, 0xfeae}, // 0x0631 = ra
65         {0xfeaf, 0xfeb0, 0xfeaf, 0xfeb0}, // 0x0632 = zain
66         {0xfeb1, 0xfeb2, 0xfeb3, 0xfeb4}, // 0x0633 = seen
67         {0xfeb5, 0xfeb6, 0xfeb7, 0xfeb8}, // 0x0634 = sheen
68         {0xfeb9, 0xfeba, 0xfebb, 0xfebc}, // 0x0635 = sad
69         {0xfebd, 0xfebe, 0xfebf, 0xfec0}, // 0x0636 = dad
70         {0xfec1, 0xfec2, 0xfec3, 0xfec4}, // 0x0637 = tah
71         {0xfec5, 0xfec6, 0xfec7, 0xfec8}, // 0x0638 = zah
72         {0xfec9, 0xfeca, 0xfecb, 0xfecc}, // 0x0639 = ain
73         {0xfecd, 0xfece, 0xfecf, 0xfed0}, // 0x063a = ghain
74         {0, 0, 0, 0}, // 0x063b
75         {0, 0, 0, 0}, // 0x063c
76         {0, 0, 0, 0}, // 0x063d
77         {0, 0, 0, 0}, // 0x063e
78         {0, 0, 0, 0}, // 0x063f
79
80         {0, 0, 0, 0}, // 0x0640
81         {0xfed1, 0xfed2, 0xfed3, 0xfed4}, // 0x0641 = fa
82         {0xfed5, 0xfed6, 0xfed7, 0xfed8}, // 0x0642 = qaf
83         {0xfed9, 0xfeda, 0xfedb, 0xfedc}, // 0x0643 = kaf
84         {0xfedd, 0xfede, 0xfedf, 0xfee0}, // 0x0644 = lam
85         {0xfee1, 0xfee2, 0xfee3, 0xfee4}, // 0x0645 = meem
86         {0xfee5, 0xfee6, 0xfee7, 0xfee8}, // 0x0646 = noon
87         {0xfee9, 0xfeea, 0xfeeb, 0xfeec}, // 0x0647 = ha
88         {0xfeed, 0xfeee, 0xfeed, 0xfeee}, // 0x0648 = waw
89         {0xfeef, 0xfef0, 0xfeef, 0xfef0}, // 0x0649 = alef maksura
90         {0xfef1, 0xfef2, 0xfef3, 0xfef4}, // 0x064a = ya
91         {0x065b, 0x065b, 0x065b, 0x065b}, // 0x064b = fathatan
92         {0x065c, 0x065c, 0x065c, 0x065c}, // 0x064c = dammatan
93         {0x064d, 0x064d, 0x064d, 0x064d}, // 0x064d = kasratan
94         {0x064e, 0x064e, 0x064e, 0x064e}, // 0x064e = fatha
95         {0x064f, 0x064f, 0x064f, 0x064f}, // 0x064f = damma
96
97         {0x0650, 0x0650, 0x0650, 0x0650}, // 0x0650 = kasra
98         {0x0651, 0x0651, 0x0651, 0x0651}, // 0x0651 = shadda
99         {0x0652, 0x0652, 0x0652, 0x0652}, // 0x0652 = sukun
100
101         {0, 0, 0, 0}, // 0x0653
102         {0, 0, 0, 0}, // 0x0654
103         {0, 0, 0, 0}, // 0x0655
104         {0, 0, 0, 0}, // 0x0656
105         {0, 0, 0, 0}, // 0x0657
106         {0, 0, 0, 0}, // 0x0658
107         {0, 0, 0, 0}, // 0x0659
108         {0, 0, 0, 0}, // 0x065a
109         {0, 0, 0, 0}, // 0x065b
110         {0, 0, 0, 0}, // 0x065c
111         {0, 0, 0, 0}, // 0x065d
112         {0, 0, 0, 0}, // 0x065e
113         {0, 0, 0, 0}, // 0x065f
114         {0, 0, 0, 0}, // 0x0660
115         {0, 0, 0, 0}, // 0x0661
116         {0, 0, 0, 0}, // 0x0662
117         {0, 0, 0, 0}, // 0x0663
118         {0, 0, 0, 0}, // 0x0664
119         {0, 0, 0, 0}, // 0x0665
120         {0, 0, 0, 0}, // 0x0666
121         {0, 0, 0, 0}, // 0x0667
122         {0, 0, 0, 0}, // 0x0668
123         {0, 0, 0, 0}, // 0x0669
124         {0, 0, 0, 0}, // 0x066a
125         {0, 0, 0, 0}, // 0x066b
126         {0, 0, 0, 0}, // 0x066c
127         {0, 0, 0, 0}, // 0x066d
128         {0, 0, 0, 0}, // 0x066e
129         {0, 0, 0, 0}, // 0x066f
130         {0, 0, 0, 0}, // 0x0670
131         {0, 0, 0, 0}, // 0x0671
132         {0, 0, 0, 0}, // 0x0672
133         {0, 0, 0, 0}, // 0x0673
134         {0, 0, 0, 0}, // 0x0674
135         {0, 0, 0, 0}, // 0x0675
136         {0, 0, 0, 0}, // 0x0676
137         {0, 0, 0, 0}, // 0x0677
138         {0, 0, 0, 0}, // 0x0678
139         {0, 0, 0, 0}, // 0x0679
140         {0, 0, 0, 0}, // 0x067a
141         {0, 0, 0, 0}, // 0x067b
142         {0, 0, 0, 0}, // 0x067c
143         {0, 0, 0, 0}, // 0x067d
144         {0xfb56, 0xfb57, 0xfb58, 0xfb59}, // 0x067e = peh 
145         {0, 0, 0, 0}, // 0x067f
146         {0, 0, 0, 0}, // 0x0680
147         {0, 0, 0, 0}, // 0x0681
148         {0, 0, 0, 0}, // 0x0682
149         {0, 0, 0, 0}, // 0x0683
150         {0, 0, 0, 0}, // 0x0684
151         {0, 0, 0, 0}, // 0x0685
152         {0xfb7a, 0xfb7b, 0xfb7c, 0xfb7d}, // 0x0686 = tcheh 
153         {0, 0, 0, 0}, // 0x0687
154         {0, 0, 0, 0}, // 0x0688
155         {0, 0, 0, 0}, // 0x0689
156         {0, 0, 0, 0}, // 0x068a
157         {0, 0, 0, 0}, // 0x068b
158         {0, 0, 0, 0}, // 0x068c
159         {0, 0, 0, 0}, // 0x068d
160         {0, 0, 0, 0}, // 0x068e
161         {0, 0, 0, 0}, // 0x068f
162         {0, 0, 0, 0}, // 0x0690
163         {0, 0, 0, 0}, // 0x0691
164         {0, 0, 0, 0}, // 0x0692
165         {0, 0, 0, 0}, // 0x0693
166         {0, 0, 0, 0}, // 0x0694
167         {0, 0, 0, 0}, // 0x0695
168         {0, 0, 0, 0}, // 0x0696
169         {0, 0, 0, 0}, // 0x0697
170         {0xfb8a, 0xfb8b, 0xfb8a, 0xfb8b}, // 0x0698 = jeh
171         {0, 0, 0, 0}, // 0x0699
172         {0, 0, 0, 0}, // 0x069a
173         {0, 0, 0, 0}, // 0x069b
174         {0, 0, 0, 0}, // 0x069c
175         {0, 0, 0, 0}, // 0x069d
176         {0, 0, 0, 0}, // 0x069e
177         {0, 0, 0, 0}, // 0x069f
178         {0, 0, 0, 0}, // 0x06a0
179         {0, 0, 0, 0}, // 0x06a1
180         {0, 0, 0, 0}, // 0x06a2
181         {0, 0, 0, 0}, // 0x06a3
182         {0, 0, 0, 0}, // 0x06a4
183         {0, 0, 0, 0}, // 0x06a5
184         {0, 0, 0, 0}, // 0x06a6
185         {0, 0, 0, 0}, // 0x06a7
186         {0, 0, 0, 0}, // 0x06a8
187         {0xfb8e, 0xfb8f, 0xfb90, 0xfb91}, // 0x06a9 = farsi kaf 
188         {0, 0, 0, 0}, // 0x06aa
189         {0, 0, 0, 0}, // 0x06ab
190         {0, 0, 0, 0}, // 0x06ac
191         {0, 0, 0, 0}, // 0x06ad
192         {0, 0, 0, 0}, // 0x06ae
193         {0xfb92, 0xfb93, 0xfb94, 0xfb95}, // 0x06af = gaf 
194         {0, 0, 0, 0}, // 0x06b0
195         {0, 0, 0, 0}, // 0x06b1
196         {0, 0, 0, 0}, // 0x06b2
197         {0, 0, 0, 0}, // 0x06b3
198         {0, 0, 0, 0}, // 0x06b4
199         {0, 0, 0, 0}, // 0x06b5
200         {0, 0, 0, 0}, // 0x06b6
201         {0, 0, 0, 0}, // 0x06b7
202         {0, 0, 0, 0}, // 0x06b8
203         {0, 0, 0, 0}, // 0x06b9
204         {0, 0, 0, 0}, // 0x06ba
205         {0, 0, 0, 0}, // 0x06bb
206         {0, 0, 0, 0}, // 0x06bc
207         {0, 0, 0, 0}, // 0x06bd
208         {0, 0, 0, 0}, // 0x06be
209         {0, 0, 0, 0}, // 0x06bf
210         {0, 0, 0, 0}, // 0x06c0
211         {0, 0, 0, 0}, // 0x06c1
212         {0, 0, 0, 0}, // 0x06c2
213         {0, 0, 0, 0}, // 0x06c3
214         {0, 0, 0, 0}, // 0x06c4
215         {0, 0, 0, 0}, // 0x06c5
216         {0, 0, 0, 0}, // 0x06c6
217         {0, 0, 0, 0}, // 0x06c7
218         {0, 0, 0, 0}, // 0x06c8
219         {0, 0, 0, 0}, // 0x06c9
220         {0, 0, 0, 0}, // 0x06ca
221         {0, 0, 0, 0}, // 0x06cb
222         {0xfbfc, 0xfbfd, 0xfbfe, 0xfbff} // 0x06cc = farsi yeh  
223 };
224
225
226 char_type const arabic_start = 0x0621;
227 char_type const arabic_end = 0x06cc;
228
229
230 /// Information about a single UCS4 character
231 struct CharInfo {
232         /// LaTeX command (text mode) for this character
233         docstring textcommand;
234         /// LaTeX command (math mode) for this character
235         docstring mathcommand;
236         /// Needed LaTeX preamble (or feature) for text mode
237         string textpreamble;
238         /// Needed LaTeX preamble (or feature) for math mode
239         string mathpreamble;
240         /// Is this a combining character?
241         bool combining;
242         /// Is \c textpreamble a feature known by LaTeXFeatures, or a raw LaTeX
243         /// command?
244         bool textfeature;
245         /// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX
246         /// command?
247         bool mathfeature;
248         /// Always force the LaTeX command, even if the encoding contains
249         /// this character?
250         bool force;
251 };
252
253
254 typedef map<char_type, CharInfo> CharInfoMap;
255 CharInfoMap unicodesymbols;
256
257 typedef std::set<char_type> CharSet;
258 CharSet forced;
259
260 typedef std::set<char_type> MathAlphaSet;
261 MathAlphaSet mathalpha;
262
263
264 /// The highest code point in UCS4 encoding (1<<20 + 1<<16)
265 char_type const max_ucs4 = 0x110000;
266
267 } // namespace anon
268
269
270 EncodingException::EncodingException(char_type c)
271         : failed_char(c), par_id(0), pos(0)
272 {
273 }
274
275
276 const char * EncodingException::what() const throw()
277 {
278         return "Could not find LaTeX command for a character";
279 }
280
281
282 Encoding::Encoding(string const & n, string const & l, string const & g,
283                    string const & i, bool f, Encoding::Package p)
284         : name_(n), latexName_(l), guiName_(g), iconvName_(i), fixedwidth_(f), package_(p)
285 {
286         if (n == "ascii") {
287                 // ASCII can encode 128 code points and nothing else
288                 start_encodable_ = 128;
289                 complete_ = true;
290         } else if (i == "UTF-8") {
291                 // UTF8 can encode all UCS4 code points
292                 start_encodable_ = max_ucs4;
293                 complete_ = true;
294         } else {
295                 complete_ = false;
296         }
297 }
298
299
300 void Encoding::init() const
301 {
302         if (complete_)
303                 return;
304
305         start_encodable_ = 0;
306         // temporarily switch off lyxerr, since we will generate iconv errors
307         lyxerr.disable();
308         if (fixedwidth_) {
309                 // We do not need to check all UCS4 code points, it is enough
310                 // if we check all 256 code points of this encoding.
311                 for (unsigned short j = 0; j < 256; ++j) {
312                         char const c = char(j);
313                         vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, iconvName_);
314                         if (ucs4.size() != 1)
315                                 continue;
316                         char_type const uc = ucs4[0];
317                         CharInfoMap::const_iterator const it = unicodesymbols.find(uc);
318                         if (it == unicodesymbols.end() || !it->second.force)
319                                 encodable_.insert(uc);
320                 }
321         } else {
322                 // We do not know how many code points this encoding has, and
323                 // they do not have a direct representation as a single byte,
324                 // therefore we need to check all UCS4 code points.
325                 // This is expensive!
326                 for (char_type c = 0; c < max_ucs4; ++c) {
327                         vector<char> const eightbit = ucs4_to_eightbit(&c, 1, iconvName_);
328                         if (!eightbit.empty()) {
329                                 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
330                                 if (it == unicodesymbols.end() || !it->second.force)
331                                         encodable_.insert(c);
332                         }
333                 }
334         }
335         lyxerr.enable();
336         CharSet::iterator it = encodable_.find(start_encodable_);
337         while (it != encodable_.end()) {
338                 encodable_.erase(it);
339                 ++start_encodable_;
340                 it = encodable_.find(start_encodable_);
341         }
342         complete_ = true;
343 }
344
345
346 docstring Encoding::latexChar(char_type c, bool for_mathed) const
347 {
348         // assure the used encoding is properly initialized
349         init();
350
351         if (iconvName_ == "UTF-8" && package_ == none)
352                 return docstring(1, c);
353         if (c < start_encodable_ && !encodings.isForced(c))
354                 return docstring(1, c);
355         if (encodable_.find(c) != encodable_.end())
356                 return docstring(1, c);
357         if (for_mathed)
358                 return docstring();
359
360         // c cannot (or should not) be encoded in this encoding
361         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
362         if (it == unicodesymbols.end())
363                 throw EncodingException(c);
364         // at least one of mathcommand and textcommand is nonempty
365         if (it->second.textcommand.empty())
366                 return "\\ensuremath{" + it->second.mathcommand + '}';
367         return it->second.textcommand;
368 }
369
370
371 vector<char_type> Encoding::symbolsList() const
372 {
373         // assure the used encoding is properly initialized
374         init();
375
376         // first all encodable characters
377         vector<char_type> symbols(encodable_.begin(), encodable_.end());
378         // add those below start_encodable_
379         for (char_type c = 0; c < start_encodable_; ++c)
380                 symbols.push_back(c);
381         // now the ones from the unicodesymbols file
382         CharInfoMap::const_iterator const end = unicodesymbols.end();
383         CharInfoMap::const_iterator it = unicodesymbols.begin();
384         for (; it != end; ++it)
385                 symbols.push_back(it->first);
386         return symbols;
387 }
388
389
390 bool Encodings::latexMathChar(char_type c, bool mathmode,
391                         Encoding const * encoding, docstring & command)
392 {
393         if (encoding)
394                 command = encoding->latexChar(c, true);
395
396         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
397         if (it == unicodesymbols.end()) {
398                 if (!encoding || command.empty())
399                         throw EncodingException(c);
400                 if (mathmode)
401                         addMathSym(c);
402                 return false;
403         }
404         // at least one of mathcommand and textcommand is nonempty
405         bool use_math = (mathmode && !it->second.mathcommand.empty()) ||
406                         (!mathmode && it->second.textcommand.empty());
407         if (use_math) {
408                 command = it->second.mathcommand;
409                 addMathCmd(c);
410         } else {
411                 if (!encoding || command.empty()) {
412                         command = it->second.textcommand;
413                         addTextCmd(c);
414                 }
415                 if (mathmode)
416                         addMathSym(c);
417         }
418         return use_math;
419 }
420
421
422 char_type Encodings::fromLaTeXCommand(docstring const & cmd, bool & combining)
423 {
424         CharInfoMap::const_iterator const end = unicodesymbols.end();
425         CharInfoMap::const_iterator it = unicodesymbols.begin();
426         for (combining = false; it != end; ++it) {
427                 docstring const math = it->second.mathcommand;
428                 docstring const text = it->second.textcommand;
429                 if (math == cmd || text == cmd) {
430                         combining = it->second.combining;
431                         return it->first;
432                 }
433         }
434         return 0;
435 }
436
437
438 docstring Encodings::fromLaTeXCommand(docstring const & cmd, docstring & rem)
439 {
440         docstring symbols;
441         size_t i = 0;
442         size_t const cmdend = cmd.size();
443         CharInfoMap::const_iterator const uniend = unicodesymbols.end();
444         for (size_t j = 0; j < cmdend; ++j) {
445                 // Also get the char after a backslash
446                 if (j + 1 < cmdend && cmd[j] == '\\')
447                         ++j;
448                 // If a macro argument follows, get it, too
449                 if (j + 1 < cmdend && cmd[j + 1] == '{') {
450                         size_t k = j + 1;
451                         int count = 1;
452                         while (k < cmdend && count && k != docstring::npos) {
453                                 k = cmd.find_first_of(from_ascii("{}"), k + 1);
454                                 if (cmd[k] == '{')
455                                         ++count;
456                                 else
457                                         --count;
458                         }
459                         if (k != docstring::npos)
460                                 j = k;
461                 }
462                 // Start with this substring and try augmenting it when it is
463                 // the prefix of some command in the unicodesymbols file
464                 docstring const subcmd = cmd.substr(i, j - i + 1);
465
466                 CharInfoMap::const_iterator it = unicodesymbols.begin();
467                 size_t unicmd_size = 0;
468                 char_type c = 0;
469                 for (; it != uniend; ++it) {
470                         docstring const math = it->second.mathcommand;
471                         docstring const text = it->second.textcommand;
472                         size_t cur_size = max(math.size(), text.size());
473                         // The current math or text unicode command cannot
474                         // match, or we already matched a longer one
475                         if (cur_size < subcmd.size() || cur_size <= unicmd_size)
476                                 continue;
477
478                         docstring tmp = subcmd;
479                         size_t k = j;
480                         while (prefixIs(math, tmp) || prefixIs(text, tmp)) {
481                                 ++k;
482                                 if (k >= cmdend || cur_size <= tmp.size())
483                                         break;
484                                 tmp += cmd[k];
485                         }
486                         // No match
487                         if (k == j)
488                                 continue;
489
490                         // The last added char caused a mismatch, because
491                         // we didn't exhaust the chars in cmd and didn't
492                         // exceed the maximum size of the current unicmd
493                         if (k < cmdend && cur_size > tmp.size())
494                                 tmp.resize(tmp.size() - 1);
495
496                         // If this is an exact match, we found a (longer)
497                         // matching command in the unicodesymbols file
498                         if ((math == tmp || text == tmp)
499                             && (tmp[0] != '\\'
500                                    || (tmp.size() == 2 && !isAlphaASCII(tmp[1]))
501                                    || k == cmdend || !isAlphaASCII(cmd[k]))) {
502                                 c = it->first;
503                                 j = k - 1;
504                                 i = j + 1;
505                                 unicmd_size = cur_size;
506                         }
507                 }
508                 if (unicmd_size)
509                         symbols += c;
510                 else if (j + 1 == cmdend)
511                         // No luck. Return what remains
512                         rem = cmd.substr(i);
513         }
514         return symbols;
515 }
516
517
518 void Encodings::initUnicodeMath(Buffer const & buffer)
519 {
520 #ifdef TEX2LYX
521         // The code below is not needed in tex2lyx and requires additional stuff
522         (void)buffer;
523 #else
524         mathcmd.clear();
525         textcmd.clear();
526         mathsym.clear();
527
528         Inset & inset = buffer.inset();
529         InsetIterator it = inset_iterator_begin(inset);
530         InsetIterator const end = inset_iterator_end(inset);
531
532         for (; it != end; ++it)
533                 it->initUnicodeMath();
534 #endif
535 }
536
537
538 void Encodings::validate(char_type c, LaTeXFeatures & features, bool for_mathed)
539 {
540 #ifdef TEX2LYX
541         // The code below is not needed in tex2lyx and requires additional stuff
542         (void)c;
543         (void)features;
544         (void)for_mathed;
545 #else
546         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
547         if (it != unicodesymbols.end()) {
548                 // In mathed, c could be used both in textmode and mathmode
549                 bool const use_math = (for_mathed && isMathCmd(c)) ||
550                                       (!for_mathed && it->second.textcommand.empty());
551                 bool const use_text = (for_mathed && isTextCmd(c)) ||
552                                       (!for_mathed && !it->second.textcommand.empty());
553                 if (use_math) {
554                         if (!it->second.mathpreamble.empty()) {
555                                 if (it->second.mathfeature) {
556                                         string feats = it->second.mathpreamble;
557                                         while (!feats.empty()) {
558                                                 string feat;
559                                                 feats = split(feats, feat, ',');
560                                                 features.require(feat);
561                                         }
562                                 } else
563                                         features.addPreambleSnippet(it->second.mathpreamble);
564                         }
565                 }
566                 if (use_text) {
567                         if (!it->second.textpreamble.empty()) {
568                                 if (it->second.textfeature) {
569                                         string feats = it->second.textpreamble;
570                                         while (!feats.empty()) {
571                                                 string feat;
572                                                 feats = split(feats, feat, ',');
573                                                 features.require(feat);
574                                         }
575                                 } else
576                                         features.addPreambleSnippet(it->second.textpreamble);
577                         }
578                 }
579         }
580         if (for_mathed && isMathSym(c)) {
581                 features.require("relsize");
582                 features.require("lyxmathsym");
583         }
584 #endif
585 }
586
587
588 bool Encodings::isHebrewComposeChar(char_type c)
589 {
590         return c <= 0x05c2 && c >= 0x05b0 && c != 0x05be && c != 0x05c0;
591 }
592
593
594 // Special Arabic letters are ones that do not get connected from left
595 // they are hamza, alef_madda, alef_hamza, waw_hamza, alef_hamza_under,
596 // alef, tah_marbota, dal, thal, rah, zai, wow, alef_maksoura
597
598 bool Encodings::isArabicSpecialChar(char_type c)
599 {
600         return (c >= 0x0621 && c <= 0x0625) || (c >= 0x0630 && c <= 0x0632)
601                 || c == 0x0627 || c == 0x0629 || c == 0x062f || c == 0x0648
602                 || c == 0x0649 || c == 0x0698;
603 }
604
605
606 bool Encodings::isArabicComposeChar(char_type c)
607 {
608         return c >= 0x064b && c <= 0x0652;
609 }
610
611
612 bool Encodings::isArabicChar(char_type c)
613 {
614         return c >= arabic_start && c <= arabic_end
615                 && arabic_table[c-arabic_start][0];
616 }
617
618
619 char_type Encodings::transformChar(char_type c, Encodings::LetterForm form)
620 {
621         return isArabicChar(c) ? arabic_table[c-arabic_start][form] : c;
622 }
623
624
625 bool Encodings::isCombiningChar(char_type c)
626 {
627         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
628         if (it != unicodesymbols.end())
629                 return it->second.combining;
630         return false;
631 }
632
633
634 bool Encodings::isKnownScriptChar(char_type const c, string & preamble)
635 {
636         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
637
638         if (it == unicodesymbols.end())
639                 return false;
640
641         if (it->second.textpreamble != "textgreek" && it->second.textpreamble != "textcyr")
642                 return false;
643
644         if (preamble.empty()) {
645                 preamble = it->second.textpreamble;
646                 return true;
647         }
648         return it->second.textpreamble == preamble;
649 }
650
651
652 bool Encodings::isForced(char_type c)
653 {
654         return (!forced.empty() && forced.find(c) != forced.end());
655 }
656
657
658 bool Encodings::isMathAlpha(char_type c)
659 {
660         return mathalpha.count(c);
661 }
662
663
664 Encoding const * Encodings::fromLyXName(string const & name) const
665 {
666         EncodingList::const_iterator const it = encodinglist.find(name);
667         return it != encodinglist.end() ? &it->second : 0;
668 }
669
670
671 Encoding const * Encodings::fromLaTeXName(string const & name) const
672 {
673         // We don't use find_if because it makes copies of the pairs in
674         // the map.
675         // This linear search is OK since we don't have many encodings.
676         // Users could even optimize it by putting the encodings they use
677         // most at the top of lib/encodings.
678         EncodingList::const_iterator const end = encodinglist.end();
679         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
680                 if (it->second.latexName() == name)
681                         return &it->second;
682         return 0;
683 }
684
685
686 Encodings::Encodings()
687 {
688 }
689
690
691 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
692 {
693         // We must read the symbolsfile first, because the Encoding
694         // constructor depends on it.
695         Lexer symbolslex;
696         symbolslex.setFile(symbolsfile);
697         bool getNextToken = true;
698         while (symbolslex.isOK()) {
699                 char_type symbol;
700                 CharInfo info;
701                 string flags;
702
703                 if (getNextToken) {
704                         if (!symbolslex.next(true))
705                                 break;
706                 } else
707                         getNextToken = true;
708
709                 istringstream is(symbolslex.getString());
710                 // reading symbol directly does not work if
711                 // char_type == wchar_t.
712                 boost::uint32_t tmp;
713                 if(!(is >> hex >> tmp))
714                         break;
715                 symbol = tmp;
716
717                 if (!symbolslex.next(true))
718                         break;
719                 info.textcommand = symbolslex.getDocString();
720                 if (!symbolslex.next(true))
721                         break;
722                 info.textpreamble = symbolslex.getString();
723                 if (!symbolslex.next(true))
724                         break;
725                 flags = symbolslex.getString();
726
727                 info.combining = false;
728                 info.textfeature = false;
729                 info.force = false;
730                 while (!flags.empty()) {
731                         string flag;
732                         flags = split(flags, flag, ',');
733                         if (flag == "combining") {
734                                 info.combining = true;
735                         } else if (flag == "force") {
736                                 info.force = true;
737                                 forced.insert(symbol);
738                         } else if (flag == "mathalpha") {
739                                 mathalpha.insert(symbol);
740                         } else {
741                                 lyxerr << "Ignoring unknown flag `" << flag
742                                        << "' for symbol `0x"
743                                        << hex << symbol << dec
744                                        << "'." << endl;
745                         }
746                 }
747                 // mathcommand and mathpreamble have been added for 1.6.0.
748                 // make them optional so that old files still work.
749                 int const lineno = symbolslex.lineNumber();
750                 bool breakout = false;
751                 if (symbolslex.next(true)) {
752                         if (symbolslex.lineNumber() != lineno) {
753                                 // line in old format without mathcommand and mathpreamble
754                                 getNextToken = false;
755                         } else {
756                                 info.mathcommand = symbolslex.getDocString();
757                                 if (symbolslex.next(true)) {
758                                         if (symbolslex.lineNumber() != lineno) {
759                                                 // line in new format with mathcommand only
760                                                 getNextToken = false;
761                                         } else {
762                                                 // line in new format with mathcommand and mathpreamble
763                                                 info.mathpreamble = symbolslex.getString();
764                                         }
765                                 } else
766                                         breakout = true;
767                         }
768                 } else {
769                         breakout = true;
770                 }
771
772                 if (!info.textpreamble.empty())
773                         info.textfeature = info.textpreamble[0] != '\\';
774                 if (!info.mathpreamble.empty())
775                         info.mathfeature = info.mathpreamble[0] != '\\';
776
777                 LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '"
778                         << to_utf8(info.textcommand) << "' '" << info.textpreamble
779                         << "' " << info.combining << ' ' << info.textfeature
780                         << " '" << to_utf8(info.mathcommand) << "' '"
781                         << info.mathpreamble << "' " << info.mathfeature);
782
783                 // we assume that at least one command is nonempty when using unicodesymbols
784                 if (!info.textcommand.empty() || !info.mathcommand.empty())
785                         unicodesymbols[symbol] = info;
786
787                 if (breakout)
788                         break;
789         }
790
791         // Now read the encodings
792         enum {
793                 et_encoding = 1,
794                 et_end,
795         };
796
797         LexerKeyword encodingtags[] = {
798                 { "encoding", et_encoding },
799                 { "end", et_end }
800         };
801
802         Lexer lex(encodingtags);
803         lex.setFile(encfile);
804         lex.setContext("Encodings::read");
805         while (lex.isOK()) {
806                 switch (lex.lex()) {
807                 case et_encoding:
808                 {
809                         lex.next();
810                         string const name = lex.getString();
811                         lex.next();
812                         string const latexname = lex.getString();
813                         lex.next();
814                         string const guiname = lex.getString();
815                         lex.next();
816                         string const iconvname = lex.getString();
817                         lex.next();
818                         string const width = lex.getString();
819                         bool fixedwidth = false;
820                         if (width == "fixed")
821                                 fixedwidth = true;
822                         else if (width == "variable")
823                                 fixedwidth = false;
824                         else
825                                 lex.printError("Unknown width");
826
827                         lex.next();
828                         string const p = lex.getString();
829                         Encoding::Package package = Encoding::none;
830                         if (p == "none")
831                                 package = Encoding::none;
832                         else if (p == "inputenc")
833                                 package = Encoding::inputenc;
834                         else if (p == "CJK")
835                                 package = Encoding::CJK;
836                         else if (p == "japanese")
837                                 package = Encoding::japanese;
838                         else
839                                 lex.printError("Unknown package");
840
841                         LYXERR(Debug::INFO, "Reading encoding " << name);
842                         encodinglist[name] = Encoding(name, latexname,
843                                 guiname, iconvname, fixedwidth, package);
844
845                         if (lex.lex() != et_end)
846                                 lex.printError("Missing end");
847                         break;
848                 }
849                 case et_end:
850                         lex.printError("Misplaced end");
851                         break;
852                 case Lexer::LEX_FEOF:
853                         break;
854                 default:
855                         lex.printError("Unknown tag");
856                         break;
857                 }
858         }
859 }
860
861
862 } // namespace lyx