]> git.lyx.org Git - lyx.git/blob - src/Encoding.cpp
Fix initial focus problem with outline and view-source panes.
[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
251 /// The highest code point in UCS4 encoding (1<<20 + 1<<16)
252 char_type const max_ucs4 = 0x110000;
253
254 } // namespace anon
255
256
257 EncodingException::EncodingException(char_type c)
258         : failed_char(c), par_id(0), pos(0)
259 {
260 }
261
262
263 const char * EncodingException::what() const throw()
264 {
265         return "Could not find LaTeX command for a character";
266 }
267
268
269 Encoding::Encoding(string const & n, string const & l, string const & i,
270                    bool f, Encoding::Package p)
271         : name_(n), latexName_(l), iconvName_(i), fixedwidth_(f), package_(p)
272 {
273         if (n == "ascii") {
274                 // ASCII can encode 128 code points and nothing else
275                 start_encodable_ = 128;
276                 complete_ = true;
277         } else if (i == "UTF-8") {
278                 // UTF8 can encode all UCS4 code points
279                 start_encodable_ = max_ucs4;
280                 complete_ = true;
281         } else {
282                 complete_ = false;
283         }
284 }
285
286
287 void Encoding::init() const
288 {
289         if (complete_)
290                 return;
291
292         start_encodable_ = 0;
293         // temporarily switch off lyxerr, since we will generate iconv errors
294         lyxerr.disable();
295         if (fixedwidth_) {
296                 // We do not need to check all UCS4 code points, it is enough
297                 // if we check all 256 code points of this encoding.
298                 for (unsigned short j = 0; j < 256; ++j) {
299                         char const c = char(j);
300                         vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, iconvName_);
301                         if (ucs4.size() != 1)
302                                 continue;
303                         char_type const uc = ucs4[0];
304                         CharInfoMap::const_iterator const it = unicodesymbols.find(uc);
305                         if (it == unicodesymbols.end() || !it->second.force)
306                                 encodable_.insert(uc);
307                 }
308         } else {
309                 // We do not know how many code points this encoding has, and
310                 // they do not have a direct representation as a single byte,
311                 // therefore we need to check all UCS4 code points.
312                 // This is expensive!
313                 for (char_type c = 0; c < max_ucs4; ++c) {
314                         vector<char> const eightbit = ucs4_to_eightbit(&c, 1, iconvName_);
315                         if (!eightbit.empty()) {
316                                 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
317                                 if (it == unicodesymbols.end() || !it->second.force)
318                                         encodable_.insert(c);
319                         }
320                 }
321         }
322         lyxerr.enable();
323         CharSet::iterator it = encodable_.find(start_encodable_);
324         while (it != encodable_.end()) {
325                 encodable_.erase(it);
326                 ++start_encodable_;
327                 it = encodable_.find(start_encodable_);
328         }
329         complete_ = true;
330 }
331
332
333 docstring Encoding::latexChar(char_type c) const
334 {
335         // assure the used encoding is properly initialized
336         init();
337
338         if (c < start_encodable_)
339                 return docstring(1, c);
340         if (encodable_.find(c) != encodable_.end())
341                 return docstring(1, c);
342
343         // c cannot (or should not) be encoded in this encoding
344         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
345         if (it == unicodesymbols.end())
346                 throw EncodingException(c);
347         // at least one of mathcommand and textcommand is nonempty
348         if (it->second.textcommand.empty())
349                 return "\\ensuremath{" + it->second.mathcommand + '}';
350         return it->second.textcommand;
351 }
352
353
354 vector<char_type> Encoding::symbolsList() const
355 {
356         // assure the used encoding is properly initialized
357         init();
358
359         // first all encodable characters
360         vector<char_type> symbols(encodable_.begin(), encodable_.end());
361         // add those below start_encodable_
362         for (char_type c = 0; c < start_encodable_; ++c)
363                 symbols.push_back(c);
364         // now the ones from the unicodesymbols file
365         CharInfoMap::const_iterator const end = unicodesymbols.end();
366         CharInfoMap::const_iterator it = unicodesymbols.begin();
367         for (; it != end; ++it)
368                 symbols.push_back(it->first);
369         return symbols;
370 }
371
372
373 bool Encodings::latexMathChar(char_type c, docstring & command)
374 {
375         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
376         if (it == unicodesymbols.end())
377                 throw EncodingException(c);
378         if (it->second.mathcommand.empty()) {
379                 if (it->second.textcommand.empty())
380                         throw EncodingException(c);
381                 command = it->second.textcommand;
382                 return false;
383         }
384         command = it->second.mathcommand;
385         return true;
386 }
387
388
389 void Encodings::validate(char_type c, LaTeXFeatures & features, bool for_mathed)
390 {
391         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
392         if (it != unicodesymbols.end()) {
393                 // at least one of mathcommand and textcommand is nonempty
394                 bool const use_math = (for_mathed && !it->second.mathcommand.empty()) ||
395                                       (!for_mathed && it->second.textcommand.empty());
396                 if (use_math) {
397                         if (!it->second.mathpreamble.empty()) {
398                                 if (it->second.mathfeature)
399                                         features.require(it->second.mathpreamble);
400                                 else
401                                         features.addPreambleSnippet(it->second.mathpreamble);
402                         }
403                 } else {
404                         if (!it->second.textpreamble.empty()) {
405                                 if (it->second.textfeature)
406                                         features.require(it->second.textpreamble);
407                                 else
408                                         features.addPreambleSnippet(it->second.textpreamble);
409                         }
410                         if (for_mathed) {
411                                 features.require("relsize");
412                                 features.require("lyxmathsym");
413                         }
414                 }
415         }
416 }
417
418
419 bool Encodings::isHebrewComposeChar(char_type c)
420 {
421         return c <= 0x05c2 && c >= 0x05b0 && c != 0x05be && c != 0x05c0;
422 }
423
424
425 // Special Arabic letters are ones that do not get connected from left
426 // they are hamza, alef_madda, alef_hamza, waw_hamza, alef_hamza_under,
427 // alef, tah_marbota, dal, thal, rah, zai, wow, alef_maksoura
428
429 bool Encodings::isArabicSpecialChar(char_type c)
430 {
431         return (c >= 0x0621 && c <= 0x0625) || (c >= 0x0630 && c <= 0x0632)
432                 || c == 0x0627 || c == 0x0629 || c == 0x062f || c == 0x0648
433                 || c == 0x0649 || c == 0x0698;
434 }
435
436
437 bool Encodings::isArabicComposeChar(char_type c)
438 {
439         return c >= 0x064b && c <= 0x0652;
440 }
441
442
443 bool Encodings::isArabicChar(char_type c)
444 {
445         return c >= arabic_start && c <= arabic_end
446                 && arabic_table[c-arabic_start][0];
447 }
448
449
450 char_type Encodings::transformChar(char_type c, Encodings::LetterForm form)
451 {
452         return isArabicChar(c) ? arabic_table[c-arabic_start][form] : c;
453 }
454
455
456 bool Encodings::isCombiningChar(char_type c)
457 {
458         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
459         if (it != unicodesymbols.end())
460                 return it->second.combining;
461         return false;
462 }
463
464
465 bool Encodings::isKnownScriptChar(char_type const c, string & preamble)
466 {
467         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
468
469         if (it == unicodesymbols.end())
470                 return false;
471
472         if (it->second.textpreamble != "textgreek" && it->second.textpreamble != "textcyr")
473                 return false;
474
475         if (preamble.empty()) {
476                 preamble = it->second.textpreamble;
477                 return true;
478         }
479         return it->second.textpreamble == preamble;
480 }
481
482
483 Encoding const * Encodings::fromLyXName(string const & name) const
484 {
485         EncodingList::const_iterator const it = encodinglist.find(name);
486         return it != encodinglist.end() ? &it->second : 0;
487 }
488
489
490 Encoding const * Encodings::fromLaTeXName(string const & name) const
491 {
492         // We don't use find_if because it makes copies of the pairs in
493         // the map.
494         // This linear search is OK since we don't have many encodings.
495         // Users could even optimize it by putting the encodings they use
496         // most at the top of lib/encodings.
497         EncodingList::const_iterator const end = encodinglist.end();
498         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
499                 if (it->second.latexName() == name)
500                         return &it->second;
501         return 0;
502 }
503
504
505 Encodings::Encodings()
506 {
507 }
508
509
510 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
511 {
512         // We must read the symbolsfile first, because the Encoding
513         // constructor depends on it.
514         Lexer symbolslex;
515         symbolslex.setFile(symbolsfile);
516         bool getNextToken = true;
517         while (symbolslex.isOK()) {
518                 char_type symbol;
519                 CharInfo info;
520                 string flags;
521
522                 if (getNextToken) {
523                         if (!symbolslex.next(true))
524                                 break;
525                 } else
526                         getNextToken = true;
527
528                 istringstream is(symbolslex.getString());
529                 // reading symbol directly does not work if
530                 // char_type == wchar_t.
531                 boost::uint32_t tmp;
532                 if(!(is >> hex >> tmp))
533                         break;
534                 symbol = tmp;
535
536                 if (!symbolslex.next(true))
537                         break;
538                 info.textcommand = symbolslex.getDocString();
539                 if (!symbolslex.next(true))
540                         break;
541                 info.textpreamble = symbolslex.getString();
542                 if (!symbolslex.next(true))
543                         break;
544                 flags = symbolslex.getString();
545
546                 info.combining = false;
547                 info.textfeature = false;
548                 info.force = false;
549                 while (!flags.empty()) {
550                         string flag;
551                         flags = split(flags, flag, ',');
552                         if (flag == "combining")
553                                 info.combining = true;
554                         else if (flag == "force")
555                                 info.force = true;
556                         else
557                                 lyxerr << "Ignoring unknown flag `" << flag
558                                        << "' for symbol `0x"
559                                        << hex << symbol << dec
560                                        << "'." << endl;
561                 }
562                 // mathcommand and mathpreamble have been added for 1.6.0.
563                 // make them optional so that old files still work.
564                 int const lineno = symbolslex.lineNumber();
565                 bool breakout = false;
566                 if (symbolslex.next(true)) {
567                         if (symbolslex.lineNumber() != lineno) {
568                                 // line in old format without mathcommand and mathpreamble
569                                 getNextToken = false;
570                         } else {
571                                 info.mathcommand = symbolslex.getDocString();
572                                 if (symbolslex.next(true)) {
573                                         if (symbolslex.lineNumber() != lineno) {
574                                                 // line in new format with mathcommand only
575                                                 getNextToken = false;
576                                         } else {
577                                                 // line in new format with mathcommand and mathpreamble
578                                                 info.mathpreamble = symbolslex.getString();
579                                         }
580                                 } else
581                                         breakout = true;
582                         }
583                 } else {
584                         breakout = true;
585                 }
586
587                 if (!info.textpreamble.empty())
588                         info.textfeature = info.textpreamble[0] != '\\';
589                 if (!info.mathpreamble.empty())
590                         info.mathfeature = info.mathpreamble[0] != '\\';
591
592                 LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '"
593                         << to_utf8(info.textcommand) << "' '" << info.textpreamble
594                         << "' " << info.combining << ' ' << info.textfeature
595                         << " '" << to_utf8(info.mathcommand) << "' '"
596                         << info.mathpreamble << "' " << info.mathfeature);
597
598                 // we assume that at least one command is nonempty when using unicodesymbols
599                 if (!info.textcommand.empty() || !info.mathcommand.empty())
600                         unicodesymbols[symbol] = info;
601
602                 if (breakout)
603                         break;
604         }
605
606         // Now read the encodings
607         enum {
608                 et_encoding = 1,
609                 et_end,
610         };
611
612         LexerKeyword encodingtags[] = {
613                 { "encoding", et_encoding },
614                 { "end", et_end }
615         };
616
617         Lexer lex(encodingtags);
618         lex.setFile(encfile);
619         lex.setContext("Encodings::read");
620         while (lex.isOK()) {
621                 switch (lex.lex()) {
622                 case et_encoding:
623                 {
624                         lex.next();
625                         string const name = lex.getString();
626                         lex.next();
627                         string const latexname = lex.getString();
628                         lex.next();
629                         string const iconvname = lex.getString();
630                         lex.next();
631                         string const width = lex.getString();
632                         bool fixedwidth = false;
633                         if (width == "fixed")
634                                 fixedwidth = true;
635                         else if (width == "variable")
636                                 fixedwidth = false;
637                         else
638                                 lex.printError("Unknown width");
639
640                         lex.next();
641                         string const p = lex.getString();
642                         Encoding::Package package = Encoding::none;
643                         if (p == "none")
644                                 package = Encoding::none;
645                         else if (p == "inputenc")
646                                 package = Encoding::inputenc;
647                         else if (p == "CJK")
648                                 package = Encoding::CJK;
649                         else
650                                 lex.printError("Unknown package");
651
652                         LYXERR(Debug::INFO, "Reading encoding " << name);
653                         encodinglist[name] = Encoding(name, latexname,
654                                 iconvname, fixedwidth, package);
655
656                         if (lex.lex() != et_end)
657                                 lex.printError("Missing end");
658                         break;
659                 }
660                 case et_end:
661                         lex.printError("Misplaced end");
662                         break;
663                 case Lexer::LEX_FEOF:
664                         break;
665                 default:
666                         lex.printError("Unknown tag");
667                         break;
668                 }
669         }
670 }
671
672
673 } // namespace lyx