]> git.lyx.org Git - features.git/blob - src/Encoding.cpp
Don't force encoding switch for Japanese "listings" auto-strings.
[features.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 "Lexer.h"
18
19 #include "support/debug.h"
20 #include "support/gettext.h"
21 #include "support/lstrings.h"
22 #include "support/mutex.h"
23 #include "support/textutils.h"
24 #include "support/unicode.h"
25
26 #include <boost/cstdint.hpp>
27
28 #include <iterator>
29 #include <algorithm>
30 #include <sstream>
31
32 using namespace std;
33 using namespace lyx::support;
34
35 namespace lyx {
36
37 int const Encoding::any = -1;
38
39 Encodings encodings;
40
41 Encodings::MathCommandSet Encodings::mathcmd;
42 Encodings::TextCommandSet Encodings::textcmd;
43 Encodings::MathSymbolSet  Encodings::mathsym;
44
45 namespace {
46
47 typedef map<char_type, CharInfo> CharInfoMap;
48 CharInfoMap unicodesymbols;
49
50 typedef set<char_type> CharSet;
51 typedef map<string, CharSet> CharSetMap;
52 CharSet forced;
53 CharSetMap forcedselected;
54
55 typedef set<char_type> MathAlphaSet;
56 MathAlphaSet mathalpha;
57
58
59 /// The highest code point in UCS4 encoding (1<<20 + 1<<16)
60 char_type const max_ucs4 = 0x110000;
61
62 } // namespace
63
64
65 EncodingException::EncodingException(char_type c)
66         : failed_char(c), par_id(0), pos(0)
67 {
68 }
69
70
71 const char * EncodingException::what() const throw()
72 {
73         return "Could not find LaTeX command for a character";
74 }
75
76
77 CharInfo::CharInfo(
78         docstring const & textcommand, docstring const & mathcommand,
79         std::string const & textpreamble, std::string const & mathpreamble,
80         std::string const & tipashortcut, unsigned int flags)
81         : textcommand_(textcommand), mathcommand_(mathcommand),
82           textpreamble_(textpreamble), mathpreamble_(mathpreamble),
83           tipashortcut_(tipashortcut), flags_(flags)
84 {
85 }
86
87
88 Encoding::Encoding(string const & n, string const & l, string const & g,
89                    string const & i, bool f, bool u, Encoding::Package p)
90         : name_(n), latexName_(l), guiName_(g), iconvName_(i), fixedwidth_(f),
91           unsafe_(u), forced_(&forcedselected[n]), package_(p)
92 {
93         if (n == "ascii") {
94                 // ASCII can encode 128 code points and nothing else
95                 start_encodable_ = 128;
96                 complete_ = true;
97         } else if (i == "UTF-8") {
98                 // UTF8 can encode all UCS4 code points
99                 start_encodable_ = max_ucs4;
100                 complete_ = true;
101         } else {
102                 start_encodable_ = 0;
103                 complete_ = false;
104         }
105 }
106
107
108 void Encoding::init() const
109 {
110         // Since the the constructor is the only method which sets complete_
111         // to false the test for complete_ is thread-safe without mutex.
112         if (complete_)
113                 return;
114
115         static Mutex mutex;
116         Mutex::Locker lock(&mutex);
117
118         // We need to test again for complete_, since another thread could
119         // have set it to true while we were waiting for the lock and we must
120         // not modify an encoding which is already complete.
121         if (complete_)
122                 return;
123
124         // We do not make any member mutable  so that it can be easily verified
125         // that all const methods are thread-safe: init() is the only const
126         // method which changes complete_, encodable_ and start_encodable_, and
127         // it uses a mutex to ensure thread-safety.
128         CharSet & encodable = const_cast<Encoding *>(this)->encodable_;
129         char_type & start_encodable = const_cast<Encoding *>(this)->start_encodable_;
130
131         start_encodable = 0;
132         // temporarily switch off lyxerr, since we will generate iconv errors
133         lyxerr.disable();
134         if (fixedwidth_) {
135                 // We do not need to check all UCS4 code points, it is enough
136                 // if we check all 256 code points of this encoding.
137                 for (unsigned short j = 0; j < 256; ++j) {
138                         char const c = char(j);
139                         vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, iconvName_);
140                         if (ucs4.size() != 1)
141                                 continue;
142                         char_type const uc = ucs4[0];
143                         CharInfoMap::const_iterator const it = unicodesymbols.find(uc);
144                         if (it == unicodesymbols.end())
145                                 encodable.insert(uc);
146                         else if (!it->second.force()) {
147                                 if (forced_->empty() || forced_->find(uc) == forced_->end())
148                                         encodable.insert(uc);
149                         }
150                 }
151         } else {
152                 // We do not know how many code points this encoding has, and
153                 // they do not have a direct representation as a single byte,
154                 // therefore we need to check all UCS4 code points.
155                 // This is expensive!
156                 for (char_type c = 0; c < max_ucs4; ++c) {
157                         vector<char> const eightbit = ucs4_to_eightbit(&c, 1, iconvName_);
158                         if (!eightbit.empty()) {
159                                 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
160                                 if (it == unicodesymbols.end())
161                                         encodable.insert(c);
162                                 else if (!it->second.force()) {
163                                         if (forced_->empty() || forced_->find(c) == forced_->end())
164                                                 encodable.insert(c);
165                                 }
166                         }
167                 }
168         }
169         lyxerr.enable();
170         CharSet::iterator it = encodable.find(start_encodable);
171         while (it != encodable.end()) {
172                 encodable.erase(it);
173                 ++start_encodable;
174                 it = encodable.find(start_encodable);
175         }
176         const_cast<Encoding *>(this)->complete_ = true;
177 }
178
179
180 bool Encoding::isForced(char_type c) const
181 {
182         if (!forced.empty() && forced.find(c) != forced.end())
183                 return true;
184         return !forced_->empty() && forced_->find(c) != forced_->end();
185 }
186
187
188 bool Encoding::encodable(char_type c) const
189 {
190         // assure the used encoding is properly initialized
191         init();
192         if (iconvName_ == "UTF-8" && package_ == none)
193                 return true;
194         // platex does not load inputenc: force conversion of supported characters
195         if (package_ == Encoding::japanese
196             && ((0xb7 <= c && c <= 0x05ff) // Latin-1 Supplement ... Hebrew
197                         || (0x1d00 <= c && c <= 0x218f) // Phonetic Extensions ... Number Forms
198                         || (0x2193 <= c && c <= 0x2aff) // Arrows ... Supplemental Mathematical Operators
199                         || (0xfb00 <= c && c <= 0xfb4f) // Alphabetic Presentation Forms
200                         || (0x1d400 <= c && c <= 0x1d7ff))) // Mathematical Alphanumeric Symbols
201                 return false;
202         if (c < start_encodable_ && !isForced(c))
203                 return true;
204         if (encodable_.find(c) != encodable_.end())
205                 return true;
206         return false;
207 }
208
209
210 pair<docstring, bool> Encoding::latexChar(char_type c) const
211 {
212         if (encodable(c))
213                 return make_pair(docstring(1, c), false);
214
215         // c cannot (or should not) be encoded in this encoding
216         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
217         if (it == unicodesymbols.end())
218                 throw EncodingException(c);
219         // at least one of mathcommand and textcommand is nonempty
220         if (it->second.textcommand().empty())
221                 return make_pair(
222                         "\\ensuremath{" + it->second.mathcommand() + '}', false);
223         return make_pair(it->second.textcommand(), !it->second.textnotermination());
224 }
225
226
227 pair<docstring, docstring> Encoding::latexString(docstring const & input, bool dryrun) const
228 {
229         docstring result;
230         docstring uncodable;
231         bool terminate = false;
232         for (size_t n = 0; n < input.size(); ++n) {
233                 char_type const c = input[n];
234                 try {
235                         pair<docstring, bool> latex_char = latexChar(c);
236                         docstring const latex = latex_char.first;
237                         if (terminate && !prefixIs(latex, '\\')
238                             && !prefixIs(latex, '{')
239                             && !prefixIs(latex, '}')) {
240                                         // Prevent eating of a following
241                                         // space or command corruption by
242                                         // following characters
243                                         if (latex == " ")
244                                                 result += "{}";
245                                         else
246                                                 result += " ";
247                                 }
248                         result += latex;
249                         terminate = latex_char.second;
250                 } catch (EncodingException & /* e */) {
251                         LYXERR0("Uncodable character <" << docstring(1, c) 
252                                         << "> in latexString!");
253                         if (dryrun) {
254                                 result += "<" + _("LyX Warning: ")
255                                            + _("uncodable character") + " '";
256                                 result += docstring(1, input[n]);
257                                 result += "'>";
258                         } else
259                                 uncodable += input[n];
260                 }
261         }
262         return make_pair(result, uncodable);
263 }
264
265
266 vector<char_type> Encoding::symbolsList() const
267 {
268         // assure the used encoding is properly initialized
269         init();
270
271         // first all those below start_encodable_
272         vector<char_type> symbols;
273         for (char_type c = 0; c < start_encodable_; ++c)
274                 symbols.push_back(c);
275         // add all encodable characters
276         copy(encodable_.begin(), encodable_.end(), back_inserter(symbols));
277         // now the ones from the unicodesymbols file that are not already there
278         for (pair<char_type, CharInfo> const & elem : unicodesymbols) {
279                 if (find(symbols.begin(), symbols.end(), elem.first) == symbols.end())
280                         symbols.push_back(elem.first);
281         }
282         // finally, sort the vector
283         sort(symbols.begin(), symbols.end());
284         return symbols;
285 }
286
287
288 bool Encodings::latexMathChar(char_type c, bool mathmode,
289                         Encoding const * encoding, docstring & command,
290                         bool & needsTermination)
291 {
292         command = empty_docstring();
293         if (encoding)
294                 if (encoding->encodable(c))
295                         command = docstring(1, c);
296         needsTermination = false;
297
298         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
299         if (it == unicodesymbols.end()) {
300                 if (!encoding || command.empty())
301                         throw EncodingException(c);
302                 if (mathmode)
303                         addMathSym(c);
304                 return false;
305         }
306         // at least one of mathcommand and textcommand is nonempty
307         bool use_math = (mathmode && !it->second.mathcommand().empty()) ||
308                         (!mathmode && it->second.textcommand().empty());
309         if (use_math) {
310                 command = it->second.mathcommand();
311                 needsTermination = !it->second.mathnotermination();
312                 addMathCmd(c);
313         } else {
314                 if (!encoding || command.empty()) {
315                         command = it->second.textcommand();
316                         needsTermination = !it->second.textnotermination();
317                 }
318                 if (mathmode)
319                         addMathSym(c);
320                 else
321                         addTextCmd(c);
322         }
323         return use_math;
324 }
325
326
327 char_type Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
328                 bool & combining, bool & needsTermination, set<string> * req)
329 {
330         CharInfoMap::const_iterator const end = unicodesymbols.end();
331         CharInfoMap::const_iterator it = unicodesymbols.begin();
332         for (combining = false; it != end; ++it) {
333                 if (it->second.deprecated())
334                         continue;
335                 docstring const math = it->second.mathcommand();
336                 docstring const text = it->second.textcommand();
337                 if ((cmdtype & MATH_CMD) && math == cmd) {
338                         combining = it->second.combining();
339                         needsTermination = !it->second.mathnotermination();
340                         if (req && it->second.mathfeature() &&
341                             !it->second.mathpreamble().empty())
342                                 req->insert(it->second.mathpreamble());
343                         return it->first;
344                 }
345                 if ((cmdtype & TEXT_CMD) && text == cmd) {
346                         combining = it->second.combining();
347                         needsTermination = !it->second.textnotermination();
348                         if (req && it->second.textfeature() &&
349                             !it->second.textpreamble().empty())
350                                 req->insert(it->second.textpreamble());
351                         return it->first;
352                 }
353         }
354         needsTermination = false;
355         return 0;
356 }
357
358
359 docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
360                 bool & needsTermination, docstring & rem, set<string> * req)
361 {
362         needsTermination = false;
363         rem = empty_docstring();
364         bool const mathmode = cmdtype & MATH_CMD;
365         bool const textmode = cmdtype & TEXT_CMD;
366         docstring symbols;
367         size_t const cmdend = cmd.size();
368         size_t prefix = 0;
369         CharInfoMap::const_iterator const uniend = unicodesymbols.end();
370         for (size_t i = 0, j = 0; j < cmdend; ++j) {
371                 // Also get the char after a backslash
372                 if (j + 1 < cmdend && cmd[j] == '\\') {
373                         ++j;
374                         prefix = 1;
375                         // Detect things like \=*{e} as well
376                         if (j + 3 < cmdend && cmd[j+1] == '*' &&
377                             cmd[j+2] == '{') {
378                                 ++j;
379                                 prefix = 2;
380                         }
381                 }
382                 // position of the last character before a possible macro
383                 // argument
384                 size_t m = j;
385                 // If a macro argument follows, get it, too
386                 // Do it here only for single character commands. Other
387                 // combining commands need this too, but they are handled in
388                 // the loop below for performance reasons.
389                 if (j + 1 < cmdend && cmd[j + 1] == '{') {
390                         size_t k = j + 1;
391                         int count = 1;
392                         while (k < cmdend && count) {
393                                 k = cmd.find_first_of(from_ascii("{}"), k + 1);
394                                 // braces may not be balanced
395                                 if (k == docstring::npos)
396                                         break;
397                                 if (cmd[k] == '{')
398                                         ++count;
399                                 else
400                                         --count;
401                         }
402                         if (k != docstring::npos)
403                                 j = k;
404                 } else if (m + 1 < cmdend && isAlphaASCII(cmd[m])) {
405                         while (m + 2 < cmdend && isAlphaASCII(cmd[m+1]))
406                                 m++;
407                 }
408                 // Start with this substring and try augmenting it when it is
409                 // the prefix of some command in the unicodesymbols file
410                 docstring subcmd = cmd.substr(i, j - i + 1);
411
412                 CharInfoMap::const_iterator it = unicodesymbols.begin();
413                 // First part of subcmd which might be a combining character
414                 docstring combcmd = (m == j) ? docstring() : cmd.substr(i, m - i + 1);
415                 // The combining character of combcmd if it exists
416                 CharInfoMap::const_iterator combining = uniend;
417                 size_t unicmd_size = 0;
418                 char_type c = 0;
419                 for (; it != uniend; ++it) {
420                         if (it->second.deprecated())
421                                 continue;
422                         docstring const math = mathmode ? it->second.mathcommand()
423                                                         : docstring();
424                         docstring const text = textmode ? it->second.textcommand()
425                                                         : docstring();
426                         if (!combcmd.empty() && it->second.combining() &&
427                             (math == combcmd || text == combcmd))
428                                 combining = it;
429                         size_t cur_size = max(math.size(), text.size());
430                         // The current math or text unicode command cannot
431                         // match, or we already matched a longer one
432                         if (cur_size < subcmd.size() || cur_size <= unicmd_size)
433                                 continue;
434
435                         docstring tmp = subcmd;
436                         size_t k = j;
437                         while (prefixIs(math, tmp) || prefixIs(text, tmp)) {
438                                 ++k;
439                                 if (k >= cmdend || cur_size <= tmp.size())
440                                         break;
441                                 tmp += cmd[k];
442                         }
443                         // No match
444                         if (k == j)
445                                 continue;
446
447                         // The last added char caused a mismatch, because
448                         // we didn't exhaust the chars in cmd and didn't
449                         // exceed the maximum size of the current unicmd
450                         if (k < cmdend && cur_size > tmp.size())
451                                 tmp.resize(tmp.size() - 1);
452
453                         // If this is an exact match, we found a (longer)
454                         // matching entry in the unicodesymbols file.
455                         if (math != tmp && text != tmp)
456                                 continue;
457                         // If we found a combining command, we need to append
458                         // the macro argument if this has not been done above.
459                         if (tmp == combcmd && combining != uniend &&
460                             k < cmdend && cmd[k] == '{') {
461                                 size_t l = k;
462                                 int count = 1;
463                                 while (l < cmdend && count) {
464                                         l = cmd.find_first_of(from_ascii("{}"), l + 1);
465                                         // braces may not be balanced
466                                         if (l == docstring::npos)
467                                                 break;
468                                         if (cmd[l] == '{')
469                                                 ++count;
470                                         else
471                                                 --count;
472                                 }
473                                 if (l != docstring::npos) {
474                                         j = l;
475                                         subcmd = cmd.substr(i, j - i + 1);
476                                 }
477                         }
478                         // If the entry doesn't start with '\', we take note
479                         // of the match and continue (this is not a ultimate
480                         // acceptance, as some other entry may match a longer
481                         // portion of the cmd string). However, if the entry
482                         // does start with '\', we accept the match only if
483                         // this is a valid macro, i.e., either it is a single
484                         // (nonletter) char macro, or nothing else follows,
485                         // or what follows is a nonletter char, or the last
486                         // character is a }.
487                         else if (tmp[0] != '\\'
488                                    || (tmp.size() == prefix + 1 &&
489                                        !isAlphaASCII(tmp[1]) &&
490                                        (prefix == 1 || !isAlphaASCII(tmp[2])))
491                                    || k == cmdend
492                                    || !isAlphaASCII(cmd[k])
493                                    || tmp[tmp.size() - 1] == '}'
494                                  ) {
495                                 c = it->first;
496                                 j = k - 1;
497                                 i = j + 1;
498                                 unicmd_size = cur_size;
499                                 if (math == tmp)
500                                         needsTermination = !it->second.mathnotermination();
501                                 else
502                                         needsTermination = !it->second.textnotermination();
503                                 if (req) {
504                                         if (math == tmp && it->second.mathfeature() &&
505                                             !it->second.mathpreamble().empty())
506                                                 req->insert(it->second.mathpreamble());
507                                         if (text == tmp && it->second.textfeature() &&
508                                             !it->second.textpreamble().empty())
509                                                 req->insert(it->second.textpreamble());
510                                 }
511                         }
512                 }
513                 if (unicmd_size)
514                         symbols += c;
515                 else if (combining != uniend &&
516                          prefixIs(subcmd, combcmd + '{')) {
517                         // We know that subcmd starts with combcmd and
518                         // contains an argument in braces.
519                         docstring const arg = subcmd.substr(
520                                 combcmd.length() + 1,
521                                 subcmd.length() - combcmd.length() - 2);
522                         // If arg is a single character we can construct a
523                         // combining sequence.
524                         char_type a;
525                         bool argcomb = false;
526                         if (arg.size() == 1 && isAlnumASCII(arg[0]))
527                                 a = arg[0];
528                         else {
529                                 // Use the version of fromLaTeXCommand() that
530                                 // parses only one command, since we cannot
531                                 // use more than one character.
532                                 bool dummy = false;
533                                 set<string> r;
534                                 a = fromLaTeXCommand(arg, cmdtype, argcomb,
535                                                      dummy, &r);
536                                 if (a && req && !argcomb)
537                                         req->insert(r.begin(), r.end());
538                         }
539                         if (a && !argcomb) {
540                                 // In unicode the combining character comes
541                                 // after its base
542                                 symbols += a;
543                                 symbols += combining->first;
544                                 i = j + 1;
545                                 unicmd_size = 2;
546                         }
547                 }
548                 if (j + 1 == cmdend && !unicmd_size) {
549                         // No luck. Return what remains
550                         rem = cmd.substr(i);
551                         if (needsTermination && !rem.empty()) {
552                                 if (rem.substr(0, 2) == "{}") {
553                                         rem = rem.substr(2);
554                                         needsTermination = false;
555                                 } else if (rem[0] == ' ') {
556                                         needsTermination = false;
557                                         // LaTeX would swallow all spaces
558                                         rem = ltrim(rem);
559                                 }
560                         }
561                 }
562         }
563         return symbols;
564 }
565
566
567 CharInfo const & Encodings::unicodeCharInfo(char_type c)
568 {
569         static CharInfo empty;
570         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
571         return it != unicodesymbols.end() ? it->second : empty;
572 }
573
574
575 bool Encodings::isCombiningChar(char_type c)
576 {
577         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
578         if (it != unicodesymbols.end())
579                 return it->second.combining();
580         return false;
581 }
582
583
584 string const Encodings::TIPAShortcut(char_type c)
585 {
586         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
587         if (it != unicodesymbols.end())
588                 return it->second.tipashortcut();
589         return string();
590 }
591
592
593 string const Encodings::isKnownScriptChar(char_type const c)
594 {
595         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
596
597         if (it == unicodesymbols.end())
598                 return string();
599         // FIXME: parse complex textpreamble (may be list or alternatives,
600         //                e.g., "subscript,textgreek" or "textcomp|textgreek")
601         if (it->second.textpreamble() == "textgreek"
602                 || it->second.textpreamble() == "textcyrillic")
603                 return it->second.textpreamble();
604         return string();
605 }
606
607
608 bool Encodings::fontencSupportsScript(string const & fontenc, string const & script)
609 {
610         if (script == "textgreek")
611                 return (fontenc == "LGR" || fontenc == "TU");
612         if (script == "textcyrillic")
613                 return (fontenc == "T2A" || fontenc == "T2B" || fontenc == "T2C"
614                                 || fontenc == "X2" || fontenc == "TU");
615         return false;
616 }
617
618
619 bool Encodings::isMathAlpha(char_type c)
620 {
621         return mathalpha.count(c);
622 }
623
624
625 bool Encodings::isUnicodeTextOnly(char_type c)
626 {
627         if (isASCII(c) || isMathAlpha(c))
628                 return false;
629
630         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
631         return it == unicodesymbols.end() || it->second.mathcommand().empty();
632 }
633
634
635 Encoding const *
636 Encodings::fromLyXName(string const & name, bool allowUnsafe) const
637 {
638         EncodingList::const_iterator const it = encodinglist.find(name);
639         if (it == encodinglist.end())
640                 return 0;
641         if (!allowUnsafe && it->second.unsafe())
642                 return 0;
643         return &it->second;
644 }
645
646
647 Encoding const *
648 Encodings::fromLaTeXName(string const & n, int const & p, bool allowUnsafe) const
649 {
650         string name = n;
651         // FIXME: if we have to test for too many of these synonyms,
652         // we should instead extend the format of lib/encodings
653         if (n == "ansinew")
654                 name = "cp1252";
655
656         // We don't use find_if because it makes copies of the pairs in
657         // the map.
658         // This linear search is OK since we don't have many encodings.
659         // Users could even optimize it by putting the encodings they use
660         // most at the top of lib/encodings.
661         EncodingList::const_iterator const end = encodinglist.end();
662         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
663                 if ((it->second.latexName() == name) && (it->second.package() & p)
664                                 && (!it->second.unsafe() || allowUnsafe))
665                         return &it->second;
666         return 0;
667 }
668
669
670 Encoding const *
671 Encodings::fromIconvName(string const & n, int const & p, bool allowUnsafe) const
672 {
673         EncodingList::const_iterator const end = encodinglist.end();
674         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
675                 if ((it->second.iconvName() == n) && (it->second.package() & p)
676                                 && (!it->second.unsafe() || allowUnsafe))
677                         return &it->second;
678         return 0;
679 }
680
681
682 Encodings::Encodings()
683 {}
684
685
686 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
687 {
688         // We must read the symbolsfile first, because the Encoding
689         // constructor depends on it.
690         CharSetMap forcednotselected;
691         Lexer symbolslex;
692         symbolslex.setFile(symbolsfile);
693         bool getNextToken = true;
694         while (symbolslex.isOK()) {
695                 char_type symbol;
696
697                 if (getNextToken) {
698                         if (!symbolslex.next(true))
699                                 break;
700                 } else
701                         getNextToken = true;
702
703                 istringstream is(symbolslex.getString());
704                 // reading symbol directly does not work if
705                 // char_type == wchar_t.
706                 boost::uint32_t tmp;
707                 if(!(is >> hex >> tmp))
708                         break;
709                 symbol = tmp;
710
711                 if (!symbolslex.next(true))
712                         break;
713                 docstring textcommand = symbolslex.getDocString();
714                 if (!symbolslex.next(true))
715                         break;
716                 string textpreamble = symbolslex.getString();
717                 if (!symbolslex.next(true))
718                         break;
719                 string sflags = symbolslex.getString();
720
721                 string tipashortcut;
722                 int flags = 0;
723
724                 if (suffixIs(textcommand, '}'))
725                         flags |= CharInfoTextNoTermination;
726                 while (!sflags.empty()) {
727                         string flag;
728                         sflags = split(sflags, flag, ',');
729                         if (flag == "combining") {
730                                 flags |= CharInfoCombining;
731                         } else if (flag == "force") {
732                                 flags |= CharInfoForce;
733                                 forced.insert(symbol);
734                         } else if (prefixIs(flag, "force=")) {
735                                 vector<string> encs =
736                                         getVectorFromString(flag.substr(6), ";");
737                                 for (size_t i = 0; i < encs.size(); ++i)
738                                         forcedselected[encs[i]].insert(symbol);
739                                 flags |= CharInfoForceSelected;
740                         } else if (prefixIs(flag, "force!=")) {
741                                 vector<string> encs =
742                                         getVectorFromString(flag.substr(7), ";");
743                                 for (size_t i = 0; i < encs.size(); ++i)
744                                         forcednotselected[encs[i]].insert(symbol);
745                                 flags |= CharInfoForceSelected;
746                         } else if (flag == "mathalpha") {
747                                 mathalpha.insert(symbol);
748                         } else if (flag == "notermination=text") {
749                                 flags |= CharInfoTextNoTermination;
750                         } else if (flag == "notermination=math") {
751                                 flags |= CharInfoMathNoTermination;
752                         } else if (flag == "notermination=both") {
753                                 flags |= CharInfoTextNoTermination;
754                                 flags |= CharInfoMathNoTermination;
755                         } else if (flag == "notermination=none") {
756                                 flags &= ~CharInfoTextNoTermination;
757                                 flags &= ~CharInfoMathNoTermination;
758                         } else if (contains(flag, "tipashortcut=")) {
759                                 tipashortcut = split(flag, '=');
760                         } else if (flag == "deprecated") {
761                                 flags |= CharInfoDeprecated;
762                         } else {
763                                 lyxerr << "Ignoring unknown flag `" << flag
764                                        << "' for symbol `0x"
765                                        << hex << symbol << dec
766                                        << "'." << endl;
767                         }
768                 }
769                 // mathcommand and mathpreamble have been added for 1.6.0.
770                 // make them optional so that old files still work.
771                 int const lineno = symbolslex.lineNumber();
772                 bool breakout = false;
773                 docstring mathcommand;
774                 string mathpreamble;
775                 if (symbolslex.next(true)) {
776                         if (symbolslex.lineNumber() != lineno) {
777                                 // line in old format without mathcommand and mathpreamble
778                                 getNextToken = false;
779                         } else {
780                                 mathcommand = symbolslex.getDocString();
781                                 if (suffixIs(mathcommand, '}'))
782                                         flags |= CharInfoMathNoTermination;
783                                 if (symbolslex.next(true)) {
784                                         if (symbolslex.lineNumber() != lineno) {
785                                                 // line in new format with mathcommand only
786                                                 getNextToken = false;
787                                         } else {
788                                                 // line in new format with mathcommand and mathpreamble
789                                                 mathpreamble = symbolslex.getString();
790                                         }
791                                 } else
792                                         breakout = true;
793                         }
794                 } else {
795                         breakout = true;
796                 }
797
798                 // backward compatibility
799                 if (mathpreamble == "esintoramsmath")
800                         mathpreamble = "esint|amsmath";
801
802                 if (!textpreamble.empty())
803                         if (textpreamble[0] != '\\')
804                                 flags |= CharInfoTextFeature;
805                 if (!mathpreamble.empty())
806                         if (mathpreamble[0] != '\\')
807                                 flags |= CharInfoMathFeature;
808
809                 CharInfo info = CharInfo(
810                         textcommand, mathcommand,
811                         textpreamble, mathpreamble,
812                         tipashortcut, flags);
813                 LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '"
814                            << to_utf8(info.textcommand()) << "' '" << info.textpreamble()
815                            << " '" << info.textfeature() << ' ' << info.textnotermination()
816                            << ' ' << to_utf8(info.mathcommand()) << "' '" << info.mathpreamble()
817                            << "' " << info.mathfeature() << ' ' << info.mathnotermination()
818                            << ' ' << info.combining() << ' ' << info.force()
819                            << ' ' << info.forceselected());
820
821                 // we assume that at least one command is nonempty when using unicodesymbols
822                 if (info.isUnicodeSymbol()) {
823                         unicodesymbols[symbol] = info;
824                 }
825
826                 if (breakout)
827                         break;
828         }
829
830         // Now read the encodings
831         enum {
832                 et_encoding = 1,
833                 et_end
834         };
835
836         LexerKeyword encodingtags[] = {
837                 { "encoding", et_encoding },
838                 { "end", et_end }
839         };
840
841         Lexer lex(encodingtags);
842         lex.setFile(encfile);
843         lex.setContext("Encodings::read");
844         while (lex.isOK()) {
845                 switch (lex.lex()) {
846                 case et_encoding:
847                 {
848                         lex.next();
849                         string const name = lex.getString();
850                         lex.next();
851                         string const latexname = lex.getString();
852                         lex.next();
853                         string const guiname = lex.getString();
854                         lex.next();
855                         string const iconvname = lex.getString();
856                         lex.next();
857                         string const width = lex.getString();
858                         bool fixedwidth = false;
859                         bool unsafe = false;
860                         if (width == "fixed")
861                                 fixedwidth = true;
862                         else if (width == "variable")
863                                 fixedwidth = false;
864                         else if (width == "variableunsafe") {
865                                 fixedwidth = false;
866                                 unsafe = true;
867                         }
868                         else
869                                 lex.printError("Unknown width");
870
871                         lex.next();
872                         string const p = lex.getString();
873                         Encoding::Package package = Encoding::none;
874                         if (p == "none")
875                                 package = Encoding::none;
876                         else if (p == "inputenc")
877                                 package = Encoding::inputenc;
878                         else if (p == "CJK")
879                                 package = Encoding::CJK;
880                         else if (p == "japanese")
881                                 package = Encoding::japanese;
882                         else
883                                 lex.printError("Unknown package");
884
885                         LYXERR(Debug::INFO, "Reading encoding " << name);
886                         encodinglist[name] = Encoding(name, latexname,
887                                 guiname, iconvname, fixedwidth, unsafe,
888                                 package);
889
890                         if (lex.lex() != et_end)
891                                 lex.printError("Missing end");
892                         break;
893                 }
894                 case et_end:
895                         lex.printError("Misplaced end");
896                         break;
897                 case Lexer::LEX_FEOF:
898                         break;
899                 default:
900                         lex.printError("Unknown tag");
901                         break;
902                 }
903         }
904
905         // Move all information from forcednotselected to forcedselected
906         for (CharSetMap::const_iterator it1 = forcednotselected.begin(); it1 != forcednotselected.end(); ++it1) {
907                 for (CharSetMap::iterator it2 = forcedselected.begin(); it2 != forcedselected.end(); ++it2) {
908                         if (it2->first != it1->first)
909                                 it2->second.insert(it1->second.begin(), it1->second.end());
910                 }
911         }
912
913 }
914
915
916 } // namespace lyx