]> git.lyx.org Git - lyx.git/blob - src/Encoding.cpp
InsetInfo: enable inset dissolve
[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 <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
193         if (iconvName_ == "UTF-8" && package_ == none)
194                 return true;
195         if (c < start_encodable_ && !isForced(c))
196                 return true;
197         if (encodable_.find(c) != encodable_.end())
198                 return true;
199         return false;
200 }
201
202
203 pair<docstring, bool> Encoding::latexChar(char_type c) const
204 {
205         if (encodable(c))
206                 return make_pair(docstring(1, c), false);
207
208         // c cannot (or should not) be encoded in this encoding
209         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
210         if (it == unicodesymbols.end())
211                 throw EncodingException(c);
212         // at least one of mathcommand and textcommand is nonempty
213         if (it->second.textcommand().empty())
214                 return make_pair(
215                         "\\ensuremath{" + it->second.mathcommand() + '}', false);
216         return make_pair(it->second.textcommand(), !it->second.textnotermination());
217 }
218
219
220 pair<docstring, docstring> Encoding::latexString(docstring const & input, bool dryrun) const
221 {
222         docstring result;
223         docstring uncodable;
224         bool terminate = false;
225         for (size_t n = 0; n < input.size(); ++n) {
226                 try {
227                         char_type const c = input[n];
228                         pair<docstring, bool> latex_char = latexChar(c);
229                         docstring const latex = latex_char.first;
230                         if (terminate && !prefixIs(latex, '\\')
231                             && !prefixIs(latex, '{')
232                             && !prefixIs(latex, '}')) {
233                                         // Prevent eating of a following
234                                         // space or command corruption by
235                                         // following characters
236                                         if (latex == " ")
237                                                 result += "{}";
238                                         else
239                                                 result += " ";
240                                 }
241                         result += latex;
242                         terminate = latex_char.second;
243                 } catch (EncodingException & /* e */) {
244                         LYXERR0("Uncodable character in latexString!");
245                         if (dryrun) {
246                                 result += "<" + _("LyX Warning: ")
247                                            + _("uncodable character") + " '";
248                                 result += docstring(1, input[n]);
249                                 result += "'>";
250                         } else
251                                 uncodable += input[n];
252                 }
253         }
254         return make_pair(result, uncodable);
255 }
256
257
258 vector<char_type> Encoding::symbolsList() const
259 {
260         // assure the used encoding is properly initialized
261         init();
262
263         // first all those below start_encodable_
264         vector<char_type> symbols;
265         for (char_type c = 0; c < start_encodable_; ++c)
266                 symbols.push_back(c);
267         // add all encodable characters
268         copy(encodable_.begin(), encodable_.end(), back_inserter(symbols));
269         // now the ones from the unicodesymbols file that are not already there
270         for (pair<char_type, CharInfo> const & elem : unicodesymbols) {
271                 if (find(symbols.begin(), symbols.end(), elem.first) == symbols.end())
272                         symbols.push_back(elem.first);
273         }
274         // finally, sort the vector
275         sort(symbols.begin(), symbols.end());
276         return symbols;
277 }
278
279
280 bool Encodings::latexMathChar(char_type c, bool mathmode,
281                         Encoding const * encoding, docstring & command,
282                         bool & needsTermination)
283 {
284         command = empty_docstring();
285         if (encoding)
286                 if (encoding->encodable(c))
287                         command = docstring(1, c);
288         needsTermination = false;
289
290         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
291         if (it == unicodesymbols.end()) {
292                 if (!encoding || command.empty())
293                         throw EncodingException(c);
294                 if (mathmode)
295                         addMathSym(c);
296                 return false;
297         }
298         // at least one of mathcommand and textcommand is nonempty
299         bool use_math = (mathmode && !it->second.mathcommand().empty()) ||
300                         (!mathmode && it->second.textcommand().empty());
301         if (use_math) {
302                 command = it->second.mathcommand();
303                 needsTermination = !it->second.mathnotermination();
304                 addMathCmd(c);
305         } else {
306                 if (!encoding || command.empty()) {
307                         command = it->second.textcommand();
308                         needsTermination = !it->second.textnotermination();
309                         addTextCmd(c);
310                 }
311                 if (mathmode)
312                         addMathSym(c);
313         }
314         return use_math;
315 }
316
317
318 char_type Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
319                 bool & combining, bool & needsTermination, set<string> * req)
320 {
321         CharInfoMap::const_iterator const end = unicodesymbols.end();
322         CharInfoMap::const_iterator it = unicodesymbols.begin();
323         for (combining = false; it != end; ++it) {
324                 if (it->second.deprecated())
325                         continue;
326                 docstring const math = it->second.mathcommand();
327                 docstring const text = it->second.textcommand();
328                 if ((cmdtype & MATH_CMD) && math == cmd) {
329                         combining = it->second.combining();
330                         needsTermination = !it->second.mathnotermination();
331                         if (req && it->second.mathfeature() &&
332                             !it->second.mathpreamble().empty())
333                                 req->insert(it->second.mathpreamble());
334                         return it->first;
335                 }
336                 if ((cmdtype & TEXT_CMD) && text == cmd) {
337                         combining = it->second.combining();
338                         needsTermination = !it->second.textnotermination();
339                         if (req && it->second.textfeature() &&
340                             !it->second.textpreamble().empty())
341                                 req->insert(it->second.textpreamble());
342                         return it->first;
343                 }
344         }
345         needsTermination = false;
346         return 0;
347 }
348
349
350 docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
351                 bool & needsTermination, docstring & rem, set<string> * req)
352 {
353         needsTermination = false;
354         rem = empty_docstring();
355         bool const mathmode = cmdtype & MATH_CMD;
356         bool const textmode = cmdtype & TEXT_CMD;
357         docstring symbols;
358         size_t const cmdend = cmd.size();
359         size_t prefix = 0;
360         CharInfoMap::const_iterator const uniend = unicodesymbols.end();
361         for (size_t i = 0, j = 0; j < cmdend; ++j) {
362                 // Also get the char after a backslash
363                 if (j + 1 < cmdend && cmd[j] == '\\') {
364                         ++j;
365                         prefix = 1;
366                         // Detect things like \=*{e} as well
367                         if (j + 3 < cmdend && cmd[j+1] == '*' &&
368                             cmd[j+2] == '{') {
369                                 ++j;
370                                 prefix = 2;
371                         }
372                 }
373                 // position of the last character before a possible macro
374                 // argument
375                 size_t m = j;
376                 // If a macro argument follows, get it, too
377                 // Do it here only for single character commands. Other
378                 // combining commands need this too, but they are handled in
379                 // the loop below for performance reasons.
380                 if (j + 1 < cmdend && cmd[j + 1] == '{') {
381                         size_t k = j + 1;
382                         int count = 1;
383                         while (k < cmdend && count) {
384                                 k = cmd.find_first_of(from_ascii("{}"), k + 1);
385                                 // braces may not be balanced
386                                 if (k == docstring::npos)
387                                         break;
388                                 if (cmd[k] == '{')
389                                         ++count;
390                                 else
391                                         --count;
392                         }
393                         if (k != docstring::npos)
394                                 j = k;
395                 } else if (m + 1 < cmdend && isAlphaASCII(cmd[m])) {
396                         while (m + 2 < cmdend && isAlphaASCII(cmd[m+1]))
397                                 m++;
398                 }
399                 // Start with this substring and try augmenting it when it is
400                 // the prefix of some command in the unicodesymbols file
401                 docstring subcmd = cmd.substr(i, j - i + 1);
402
403                 CharInfoMap::const_iterator it = unicodesymbols.begin();
404                 // First part of subcmd which might be a combining character
405                 docstring combcmd = (m == j) ? docstring() : cmd.substr(i, m - i + 1);
406                 // The combining character of combcmd if it exists
407                 CharInfoMap::const_iterator combining = uniend;
408                 size_t unicmd_size = 0;
409                 char_type c = 0;
410                 for (; it != uniend; ++it) {
411                         if (it->second.deprecated())
412                                 continue;
413                         docstring const math = mathmode ? it->second.mathcommand()
414                                                         : docstring();
415                         docstring const text = textmode ? it->second.textcommand()
416                                                         : docstring();
417                         if (!combcmd.empty() && it->second.combining() &&
418                             (math == combcmd || text == combcmd))
419                                 combining = it;
420                         size_t cur_size = max(math.size(), text.size());
421                         // The current math or text unicode command cannot
422                         // match, or we already matched a longer one
423                         if (cur_size < subcmd.size() || cur_size <= unicmd_size)
424                                 continue;
425
426                         docstring tmp = subcmd;
427                         size_t k = j;
428                         while (prefixIs(math, tmp) || prefixIs(text, tmp)) {
429                                 ++k;
430                                 if (k >= cmdend || cur_size <= tmp.size())
431                                         break;
432                                 tmp += cmd[k];
433                         }
434                         // No match
435                         if (k == j)
436                                 continue;
437
438                         // The last added char caused a mismatch, because
439                         // we didn't exhaust the chars in cmd and didn't
440                         // exceed the maximum size of the current unicmd
441                         if (k < cmdend && cur_size > tmp.size())
442                                 tmp.resize(tmp.size() - 1);
443
444                         // If this is an exact match, we found a (longer)
445                         // matching entry in the unicodesymbols file.
446                         if (math != tmp && text != tmp)
447                                 continue;
448                         // If we found a combining command, we need to append
449                         // the macro argument if this has not been done above.
450                         if (tmp == combcmd && combining != uniend &&
451                             k < cmdend && cmd[k] == '{') {
452                                 size_t l = k;
453                                 int count = 1;
454                                 while (l < cmdend && count) {
455                                         l = cmd.find_first_of(from_ascii("{}"), l + 1);
456                                         // braces may not be balanced
457                                         if (l == docstring::npos)
458                                                 break;
459                                         if (cmd[l] == '{')
460                                                 ++count;
461                                         else
462                                                 --count;
463                                 }
464                                 if (l != docstring::npos) {
465                                         j = l;
466                                         subcmd = cmd.substr(i, j - i + 1);
467                                 }
468                         }
469                         // If the entry doesn't start with '\', we take note
470                         // of the match and continue (this is not a ultimate
471                         // acceptance, as some other entry may match a longer
472                         // portion of the cmd string). However, if the entry
473                         // does start with '\', we accept the match only if
474                         // this is a valid macro, i.e., either it is a single
475                         // (nonletter) char macro, or nothing else follows,
476                         // or what follows is a nonletter char, or the last
477                         // character is a }.
478                         else if (tmp[0] != '\\'
479                                    || (tmp.size() == prefix + 1 &&
480                                        !isAlphaASCII(tmp[1]) &&
481                                        (prefix == 1 || !isAlphaASCII(tmp[2])))
482                                    || k == cmdend
483                                    || !isAlphaASCII(cmd[k])
484                                    || tmp[tmp.size() - 1] == '}'
485                                  ) {
486                                 c = it->first;
487                                 j = k - 1;
488                                 i = j + 1;
489                                 unicmd_size = cur_size;
490                                 if (math == tmp)
491                                         needsTermination = !it->second.mathnotermination();
492                                 else
493                                         needsTermination = !it->second.textnotermination();
494                                 if (req) {
495                                         if (math == tmp && it->second.mathfeature() &&
496                                             !it->second.mathpreamble().empty())
497                                                 req->insert(it->second.mathpreamble());
498                                         if (text == tmp && it->second.textfeature() &&
499                                             !it->second.textpreamble().empty())
500                                                 req->insert(it->second.textpreamble());
501                                 }
502                         }
503                 }
504                 if (unicmd_size)
505                         symbols += c;
506                 else if (combining != uniend &&
507                          prefixIs(subcmd, combcmd + '{')) {
508                         // We know that subcmd starts with combcmd and
509                         // contains an argument in braces.
510                         docstring const arg = subcmd.substr(
511                                 combcmd.length() + 1,
512                                 subcmd.length() - combcmd.length() - 2);
513                         // If arg is a single character we can construct a
514                         // combining sequence.
515                         char_type a;
516                         bool argcomb = false;
517                         if (arg.size() == 1 && isAlnumASCII(arg[0]))
518                                 a = arg[0];
519                         else {
520                                 // Use the version of fromLaTeXCommand() that
521                                 // parses only one command, since we cannot
522                                 // use more than one character.
523                                 bool dummy = false;
524                                 set<string> r;
525                                 a = fromLaTeXCommand(arg, cmdtype, argcomb,
526                                                      dummy, &r);
527                                 if (a && req && !argcomb)
528                                         req->insert(r.begin(), r.end());
529                         }
530                         if (a && !argcomb) {
531                                 // In unicode the combining character comes
532                                 // after its base
533                                 symbols += a;
534                                 symbols += combining->first;
535                                 i = j + 1;
536                                 unicmd_size = 2;
537                         }
538                 }
539                 if (j + 1 == cmdend && !unicmd_size) {
540                         // No luck. Return what remains
541                         rem = cmd.substr(i);
542                         if (needsTermination && !rem.empty()) {
543                                 if (rem.substr(0, 2) == "{}") {
544                                         rem = rem.substr(2);
545                                         needsTermination = false;
546                                 } else if (rem[0] == ' ') {
547                                         needsTermination = false;
548                                         // LaTeX would swallow all spaces
549                                         rem = ltrim(rem);
550                                 }
551                         }
552                 }
553         }
554         return symbols;
555 }
556
557
558 CharInfo const & Encodings::unicodeCharInfo(char_type c)
559 {
560         static CharInfo empty;
561         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
562         return it != unicodesymbols.end() ? it->second : empty;
563 }
564
565
566 bool Encodings::isCombiningChar(char_type c)
567 {
568         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
569         if (it != unicodesymbols.end())
570                 return it->second.combining();
571         return false;
572 }
573
574
575 string const Encodings::TIPAShortcut(char_type c)
576 {
577         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
578         if (it != unicodesymbols.end())
579                 return it->second.tipashortcut();
580         return string();
581 }
582
583
584 bool Encodings::isKnownScriptChar(char_type const c, string & preamble)
585 {
586         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
587
588         if (it == unicodesymbols.end())
589                 return false;
590
591         if (it->second.textpreamble() != "textgreek"
592             && it->second.textpreamble() != "textcyrillic")
593                 return false;
594
595         if (preamble.empty()) {
596                 preamble = it->second.textpreamble();
597                 return true;
598         }
599         return it->second.textpreamble() == preamble;
600 }
601
602
603 bool Encodings::needsScriptWrapper(string const & script, string const & fontenc)
604 {
605         if (script == "textgreek")
606                 return (fontenc != "LGR");
607         if (script == "textcyrillic") {
608                 return (fontenc != "T2A" && fontenc != "T2B"
609                         && fontenc != "T2C" && fontenc != "X2");
610         }
611         return false;
612 }
613
614
615 bool Encodings::isMathAlpha(char_type c)
616 {
617         return mathalpha.count(c);
618 }
619
620
621 bool Encodings::isUnicodeTextOnly(char_type c)
622 {
623         if (isASCII(c) || isMathAlpha(c))
624                 return false;
625
626         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
627         return it == unicodesymbols.end() || it->second.mathcommand().empty();
628 }
629
630
631 Encoding const *
632 Encodings::fromLyXName(string const & name, bool allowUnsafe) const
633 {
634         EncodingList::const_iterator const it = encodinglist.find(name);
635         if (it == encodinglist.end())
636                 return 0;
637         if (!allowUnsafe && it->second.unsafe())
638                 return 0;
639         return &it->second;
640 }
641
642
643 Encoding const *
644 Encodings::fromLaTeXName(string const & n, int const & p, bool allowUnsafe) const
645 {
646         string name = n;
647         // FIXME: if we have to test for too many of these synonyms,
648         // we should instead extend the format of lib/encodings
649         if (n == "ansinew")
650                 name = "cp1252";
651
652         // We don't use find_if because it makes copies of the pairs in
653         // the map.
654         // This linear search is OK since we don't have many encodings.
655         // Users could even optimize it by putting the encodings they use
656         // most at the top of lib/encodings.
657         EncodingList::const_iterator const end = encodinglist.end();
658         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
659                 if ((it->second.latexName() == name) && (it->second.package() & p)
660                                 && (!it->second.unsafe() || allowUnsafe))
661                         return &it->second;
662         return 0;
663 }
664
665
666 Encoding const *
667 Encodings::fromIconvName(string const & n, int const & p, bool allowUnsafe) const
668 {
669         EncodingList::const_iterator const end = encodinglist.end();
670         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
671                 if ((it->second.iconvName() == n) && (it->second.package() & p)
672                                 && (!it->second.unsafe() || allowUnsafe))
673                         return &it->second;
674         return 0;
675 }
676
677
678 Encodings::Encodings()
679 {}
680
681
682 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
683 {
684         // We must read the symbolsfile first, because the Encoding
685         // constructor depends on it.
686         CharSetMap forcednotselected;
687         Lexer symbolslex;
688         symbolslex.setFile(symbolsfile);
689         bool getNextToken = true;
690         while (symbolslex.isOK()) {
691                 char_type symbol;
692
693                 if (getNextToken) {
694                         if (!symbolslex.next(true))
695                                 break;
696                 } else
697                         getNextToken = true;
698
699                 istringstream is(symbolslex.getString());
700                 // reading symbol directly does not work if
701                 // char_type == wchar_t.
702                 boost::uint32_t tmp;
703                 if(!(is >> hex >> tmp))
704                         break;
705                 symbol = tmp;
706
707                 if (!symbolslex.next(true))
708                         break;
709                 docstring textcommand = symbolslex.getDocString();
710                 if (!symbolslex.next(true))
711                         break;
712                 string textpreamble = symbolslex.getString();
713                 if (!symbolslex.next(true))
714                         break;
715                 string sflags = symbolslex.getString();
716
717                 string tipashortcut;
718                 int flags = 0;
719
720                 if (suffixIs(textcommand, '}'))
721                         flags |= CharInfoTextNoTermination;
722                 while (!sflags.empty()) {
723                         string flag;
724                         sflags = split(sflags, flag, ',');
725                         if (flag == "combining") {
726                                 flags |= CharInfoCombining;
727                         } else if (flag == "force") {
728                                 flags |= CharInfoForce;
729                                 forced.insert(symbol);
730                         } else if (prefixIs(flag, "force=")) {
731                                 vector<string> encs =
732                                         getVectorFromString(flag.substr(6), ";");
733                                 for (size_t i = 0; i < encs.size(); ++i)
734                                         forcedselected[encs[i]].insert(symbol);
735                                 flags |= CharInfoForceSelected;
736                         } else if (prefixIs(flag, "force!=")) {
737                                 vector<string> encs =
738                                         getVectorFromString(flag.substr(7), ";");
739                                 for (size_t i = 0; i < encs.size(); ++i)
740                                         forcednotselected[encs[i]].insert(symbol);
741                                 flags |= CharInfoForceSelected;
742                         } else if (flag == "mathalpha") {
743                                 mathalpha.insert(symbol);
744                         } else if (flag == "notermination=text") {
745                                 flags |= CharInfoTextNoTermination;
746                         } else if (flag == "notermination=math") {
747                                 flags |= CharInfoMathNoTermination;
748                         } else if (flag == "notermination=both") {
749                                 flags |= CharInfoTextNoTermination;
750                                 flags |= CharInfoMathNoTermination;
751                         } else if (flag == "notermination=none") {
752                                 flags &= ~CharInfoTextNoTermination;
753                                 flags &= ~CharInfoMathNoTermination;
754                         } else if (contains(flag, "tipashortcut=")) {
755                                 tipashortcut = split(flag, '=');
756                         } else if (flag == "deprecated") {
757                                 flags |= CharInfoDeprecated;
758                         } else {
759                                 lyxerr << "Ignoring unknown flag `" << flag
760                                        << "' for symbol `0x"
761                                        << hex << symbol << dec
762                                        << "'." << endl;
763                         }
764                 }
765                 // mathcommand and mathpreamble have been added for 1.6.0.
766                 // make them optional so that old files still work.
767                 int const lineno = symbolslex.lineNumber();
768                 bool breakout = false;
769                 docstring mathcommand;
770                 string mathpreamble;
771                 if (symbolslex.next(true)) {
772                         if (symbolslex.lineNumber() != lineno) {
773                                 // line in old format without mathcommand and mathpreamble
774                                 getNextToken = false;
775                         } else {
776                                 mathcommand = symbolslex.getDocString();
777                                 if (suffixIs(mathcommand, '}'))
778                                         flags |= CharInfoMathNoTermination;
779                                 if (symbolslex.next(true)) {
780                                         if (symbolslex.lineNumber() != lineno) {
781                                                 // line in new format with mathcommand only
782                                                 getNextToken = false;
783                                         } else {
784                                                 // line in new format with mathcommand and mathpreamble
785                                                 mathpreamble = symbolslex.getString();
786                                         }
787                                 } else
788                                         breakout = true;
789                         }
790                 } else {
791                         breakout = true;
792                 }
793
794                 // backward compatibility
795                 if (mathpreamble == "esintoramsmath")
796                         mathpreamble = "esint|amsmath";
797
798                 if (!textpreamble.empty())
799                         if (textpreamble[0] != '\\')
800                                 flags |= CharInfoTextFeature;
801                 if (!mathpreamble.empty())
802                         if (mathpreamble[0] != '\\')
803                                 flags |= CharInfoMathFeature;
804
805                 CharInfo info = CharInfo(
806                         textcommand, mathcommand,
807                         textpreamble, mathpreamble,
808                         tipashortcut, flags);
809                 LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '"
810                            << to_utf8(info.textcommand()) << "' '" << info.textpreamble()
811                            << " '" << info.textfeature() << ' ' << info.textnotermination()
812                            << ' ' << to_utf8(info.mathcommand()) << "' '" << info.mathpreamble()
813                            << "' " << info.mathfeature() << ' ' << info.mathnotermination()
814                            << ' ' << info.combining() << ' ' << info.force()
815                            << ' ' << info.forceselected());
816
817                 // we assume that at least one command is nonempty when using unicodesymbols
818                 if (info.isUnicodeSymbol()) {
819                         unicodesymbols[symbol] = info;
820                 }
821
822                 if (breakout)
823                         break;
824         }
825
826         // Now read the encodings
827         enum {
828                 et_encoding = 1,
829                 et_end
830         };
831
832         LexerKeyword encodingtags[] = {
833                 { "encoding", et_encoding },
834                 { "end", et_end }
835         };
836
837         Lexer lex(encodingtags);
838         lex.setFile(encfile);
839         lex.setContext("Encodings::read");
840         while (lex.isOK()) {
841                 switch (lex.lex()) {
842                 case et_encoding:
843                 {
844                         lex.next();
845                         string const name = lex.getString();
846                         lex.next();
847                         string const latexname = lex.getString();
848                         lex.next();
849                         string const guiname = lex.getString();
850                         lex.next();
851                         string const iconvname = lex.getString();
852                         lex.next();
853                         string const width = lex.getString();
854                         bool fixedwidth = false;
855                         bool unsafe = false;
856                         if (width == "fixed")
857                                 fixedwidth = true;
858                         else if (width == "variable")
859                                 fixedwidth = false;
860                         else if (width == "variableunsafe") {
861                                 fixedwidth = false;
862                                 unsafe = true;
863                         }
864                         else
865                                 lex.printError("Unknown width");
866
867                         lex.next();
868                         string const p = lex.getString();
869                         Encoding::Package package = Encoding::none;
870                         if (p == "none")
871                                 package = Encoding::none;
872                         else if (p == "inputenc")
873                                 package = Encoding::inputenc;
874                         else if (p == "CJK")
875                                 package = Encoding::CJK;
876                         else if (p == "japanese")
877                                 package = Encoding::japanese;
878                         else
879                                 lex.printError("Unknown package");
880
881                         LYXERR(Debug::INFO, "Reading encoding " << name);
882                         encodinglist[name] = Encoding(name, latexname,
883                                 guiname, iconvname, fixedwidth, unsafe,
884                                 package);
885
886                         if (lex.lex() != et_end)
887                                 lex.printError("Missing end");
888                         break;
889                 }
890                 case et_end:
891                         lex.printError("Misplaced end");
892                         break;
893                 case Lexer::LEX_FEOF:
894                         break;
895                 default:
896                         lex.printError("Unknown tag");
897                         break;
898                 }
899         }
900
901         // Move all information from forcednotselected to forcedselected
902         for (CharSetMap::const_iterator it1 = forcednotselected.begin(); it1 != forcednotselected.end(); ++it1) {
903                 for (CharSetMap::iterator it2 = forcedselected.begin(); it2 != forcedselected.end(); ++it2) {
904                         if (it2->first != it1->first)
905                                 it2->second.insert(it1->second.begin(), it1->second.end());
906                 }
907         }
908
909 }
910
911
912 } // namespace lyx