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