]> git.lyx.org Git - lyx.git/blob - src/Encoding.cpp
* Encoding.cpp (fromLaTeXCommand): fix bug when the string passed
[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.size() == 1 && !isAlphaASCII(tmp[0]))
500                                     || k == cmdend || !isAlphaASCII(cmd[k]))) {
501                                 c = it->first;
502                                 j = k - 1;
503                                 i = j + 1;
504                                 unicmd_size = cur_size;
505                         }
506                 }
507                 if (unicmd_size)
508                         symbols += c;
509                 else if (j + 1 == cmdend)
510                         // No luck. Return what remains
511                         rem = cmd.substr(i);
512         }
513         return symbols;
514 }
515
516
517 void Encodings::initUnicodeMath(Buffer const & buffer)
518 {
519 #ifdef TEX2LYX
520         // The code below is not needed in tex2lyx and requires additional stuff
521         (void)buffer;
522 #else
523         mathcmd.clear();
524         textcmd.clear();
525         mathsym.clear();
526
527         Inset & inset = buffer.inset();
528         InsetIterator it = inset_iterator_begin(inset);
529         InsetIterator const end = inset_iterator_end(inset);
530
531         for (; it != end; ++it)
532                 it->initUnicodeMath();
533 #endif
534 }
535
536
537 void Encodings::validate(char_type c, LaTeXFeatures & features, bool for_mathed)
538 {
539 #ifdef TEX2LYX
540         // The code below is not needed in tex2lyx and requires additional stuff
541         (void)c;
542         (void)features;
543         (void)for_mathed;
544 #else
545         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
546         if (it != unicodesymbols.end()) {
547                 // In mathed, c could be used both in textmode and mathmode
548                 bool const use_math = (for_mathed && isMathCmd(c)) ||
549                                       (!for_mathed && it->second.textcommand.empty());
550                 bool const use_text = (for_mathed && isTextCmd(c)) ||
551                                       (!for_mathed && !it->second.textcommand.empty());
552                 if (use_math) {
553                         if (!it->second.mathpreamble.empty()) {
554                                 if (it->second.mathfeature) {
555                                         string feats = it->second.mathpreamble;
556                                         while (!feats.empty()) {
557                                                 string feat;
558                                                 feats = split(feats, feat, ',');
559                                                 features.require(feat);
560                                         }
561                                 } else
562                                         features.addPreambleSnippet(it->second.mathpreamble);
563                         }
564                 }
565                 if (use_text) {
566                         if (!it->second.textpreamble.empty()) {
567                                 if (it->second.textfeature) {
568                                         string feats = it->second.textpreamble;
569                                         while (!feats.empty()) {
570                                                 string feat;
571                                                 feats = split(feats, feat, ',');
572                                                 features.require(feat);
573                                         }
574                                 } else
575                                         features.addPreambleSnippet(it->second.textpreamble);
576                         }
577                 }
578         }
579         if (for_mathed && isMathSym(c)) {
580                 features.require("relsize");
581                 features.require("lyxmathsym");
582         }
583 #endif
584 }
585
586
587 bool Encodings::isHebrewComposeChar(char_type c)
588 {
589         return c <= 0x05c2 && c >= 0x05b0 && c != 0x05be && c != 0x05c0;
590 }
591
592
593 // Special Arabic letters are ones that do not get connected from left
594 // they are hamza, alef_madda, alef_hamza, waw_hamza, alef_hamza_under,
595 // alef, tah_marbota, dal, thal, rah, zai, wow, alef_maksoura
596
597 bool Encodings::isArabicSpecialChar(char_type c)
598 {
599         return (c >= 0x0621 && c <= 0x0625) || (c >= 0x0630 && c <= 0x0632)
600                 || c == 0x0627 || c == 0x0629 || c == 0x062f || c == 0x0648
601                 || c == 0x0649 || c == 0x0698;
602 }
603
604
605 bool Encodings::isArabicComposeChar(char_type c)
606 {
607         return c >= 0x064b && c <= 0x0652;
608 }
609
610
611 bool Encodings::isArabicChar(char_type c)
612 {
613         return c >= arabic_start && c <= arabic_end
614                 && arabic_table[c-arabic_start][0];
615 }
616
617
618 char_type Encodings::transformChar(char_type c, Encodings::LetterForm form)
619 {
620         return isArabicChar(c) ? arabic_table[c-arabic_start][form] : c;
621 }
622
623
624 bool Encodings::isCombiningChar(char_type c)
625 {
626         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
627         if (it != unicodesymbols.end())
628                 return it->second.combining;
629         return false;
630 }
631
632
633 bool Encodings::isKnownScriptChar(char_type const c, string & preamble)
634 {
635         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
636
637         if (it == unicodesymbols.end())
638                 return false;
639
640         if (it->second.textpreamble != "textgreek" && it->second.textpreamble != "textcyr")
641                 return false;
642
643         if (preamble.empty()) {
644                 preamble = it->second.textpreamble;
645                 return true;
646         }
647         return it->second.textpreamble == preamble;
648 }
649
650
651 bool Encodings::isForced(char_type c)
652 {
653         return (!forced.empty() && forced.find(c) != forced.end());
654 }
655
656
657 bool Encodings::isMathAlpha(char_type c)
658 {
659         return mathalpha.count(c);
660 }
661
662
663 Encoding const * Encodings::fromLyXName(string const & name) const
664 {
665         EncodingList::const_iterator const it = encodinglist.find(name);
666         return it != encodinglist.end() ? &it->second : 0;
667 }
668
669
670 Encoding const * Encodings::fromLaTeXName(string const & name) const
671 {
672         // We don't use find_if because it makes copies of the pairs in
673         // the map.
674         // This linear search is OK since we don't have many encodings.
675         // Users could even optimize it by putting the encodings they use
676         // most at the top of lib/encodings.
677         EncodingList::const_iterator const end = encodinglist.end();
678         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
679                 if (it->second.latexName() == name)
680                         return &it->second;
681         return 0;
682 }
683
684
685 Encodings::Encodings()
686 {
687 }
688
689
690 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
691 {
692         // We must read the symbolsfile first, because the Encoding
693         // constructor depends on it.
694         Lexer symbolslex;
695         symbolslex.setFile(symbolsfile);
696         bool getNextToken = true;
697         while (symbolslex.isOK()) {
698                 char_type symbol;
699                 CharInfo info;
700                 string flags;
701
702                 if (getNextToken) {
703                         if (!symbolslex.next(true))
704                                 break;
705                 } else
706                         getNextToken = true;
707
708                 istringstream is(symbolslex.getString());
709                 // reading symbol directly does not work if
710                 // char_type == wchar_t.
711                 boost::uint32_t tmp;
712                 if(!(is >> hex >> tmp))
713                         break;
714                 symbol = tmp;
715
716                 if (!symbolslex.next(true))
717                         break;
718                 info.textcommand = symbolslex.getDocString();
719                 if (!symbolslex.next(true))
720                         break;
721                 info.textpreamble = symbolslex.getString();
722                 if (!symbolslex.next(true))
723                         break;
724                 flags = symbolslex.getString();
725
726                 info.combining = false;
727                 info.textfeature = false;
728                 info.force = false;
729                 while (!flags.empty()) {
730                         string flag;
731                         flags = split(flags, flag, ',');
732                         if (flag == "combining") {
733                                 info.combining = true;
734                         } else if (flag == "force") {
735                                 info.force = true;
736                                 forced.insert(symbol);
737                         } else if (flag == "mathalpha") {
738                                 mathalpha.insert(symbol);
739                         } else {
740                                 lyxerr << "Ignoring unknown flag `" << flag
741                                        << "' for symbol `0x"
742                                        << hex << symbol << dec
743                                        << "'." << endl;
744                         }
745                 }
746                 // mathcommand and mathpreamble have been added for 1.6.0.
747                 // make them optional so that old files still work.
748                 int const lineno = symbolslex.lineNumber();
749                 bool breakout = false;
750                 if (symbolslex.next(true)) {
751                         if (symbolslex.lineNumber() != lineno) {
752                                 // line in old format without mathcommand and mathpreamble
753                                 getNextToken = false;
754                         } else {
755                                 info.mathcommand = symbolslex.getDocString();
756                                 if (symbolslex.next(true)) {
757                                         if (symbolslex.lineNumber() != lineno) {
758                                                 // line in new format with mathcommand only
759                                                 getNextToken = false;
760                                         } else {
761                                                 // line in new format with mathcommand and mathpreamble
762                                                 info.mathpreamble = symbolslex.getString();
763                                         }
764                                 } else
765                                         breakout = true;
766                         }
767                 } else {
768                         breakout = true;
769                 }
770
771                 if (!info.textpreamble.empty())
772                         info.textfeature = info.textpreamble[0] != '\\';
773                 if (!info.mathpreamble.empty())
774                         info.mathfeature = info.mathpreamble[0] != '\\';
775
776                 LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '"
777                         << to_utf8(info.textcommand) << "' '" << info.textpreamble
778                         << "' " << info.combining << ' ' << info.textfeature
779                         << " '" << to_utf8(info.mathcommand) << "' '"
780                         << info.mathpreamble << "' " << info.mathfeature);
781
782                 // we assume that at least one command is nonempty when using unicodesymbols
783                 if (!info.textcommand.empty() || !info.mathcommand.empty())
784                         unicodesymbols[symbol] = info;
785
786                 if (breakout)
787                         break;
788         }
789
790         // Now read the encodings
791         enum {
792                 et_encoding = 1,
793                 et_end,
794         };
795
796         LexerKeyword encodingtags[] = {
797                 { "encoding", et_encoding },
798                 { "end", et_end }
799         };
800
801         Lexer lex(encodingtags);
802         lex.setFile(encfile);
803         lex.setContext("Encodings::read");
804         while (lex.isOK()) {
805                 switch (lex.lex()) {
806                 case et_encoding:
807                 {
808                         lex.next();
809                         string const name = lex.getString();
810                         lex.next();
811                         string const latexname = lex.getString();
812                         lex.next();
813                         string const guiname = lex.getString();
814                         lex.next();
815                         string const iconvname = lex.getString();
816                         lex.next();
817                         string const width = lex.getString();
818                         bool fixedwidth = false;
819                         if (width == "fixed")
820                                 fixedwidth = true;
821                         else if (width == "variable")
822                                 fixedwidth = false;
823                         else
824                                 lex.printError("Unknown width");
825
826                         lex.next();
827                         string const p = lex.getString();
828                         Encoding::Package package = Encoding::none;
829                         if (p == "none")
830                                 package = Encoding::none;
831                         else if (p == "inputenc")
832                                 package = Encoding::inputenc;
833                         else if (p == "CJK")
834                                 package = Encoding::CJK;
835                         else if (p == "japanese")
836                                 package = Encoding::japanese;
837                         else
838                                 lex.printError("Unknown package");
839
840                         LYXERR(Debug::INFO, "Reading encoding " << name);
841                         encodinglist[name] = Encoding(name, latexname,
842                                 guiname, iconvname, fixedwidth, package);
843
844                         if (lex.lex() != et_end)
845                                 lex.printError("Missing end");
846                         break;
847                 }
848                 case et_end:
849                         lex.printError("Misplaced end");
850                         break;
851                 case Lexer::LEX_FEOF:
852                         break;
853                 default:
854                         lex.printError("Unknown tag");
855                         break;
856                 }
857         }
858 }
859
860
861 } // namespace lyx