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