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