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