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