3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
18 #include "InsetIterator.h"
19 #include "LaTeXFeatures.h"
23 #include "support/debug.h"
24 #include "support/FileName.h"
25 #include "support/lstrings.h"
26 #include "support/textutils.h"
27 #include "support/unicode.h"
29 #include <boost/cstdint.hpp>
34 using namespace lyx::support;
40 Encodings::MathCommandSet Encodings::mathcmd;
41 Encodings::TextCommandSet Encodings::textcmd;
42 Encodings::MathSymbolSet Encodings::mathsym;
46 char_type arabic_table[172][4] = {
47 {0xfe80, 0xfe80, 0xfe80, 0xfe80}, // 0x0621 = hamza
48 {0xfe81, 0xfe82, 0xfe81, 0xfe82}, // 0x0622 = ligature madda on alef
49 {0xfe83, 0xfe84, 0xfe83, 0xfe84}, // 0x0623 = ligature hamza on alef
50 {0xfe85, 0xfe86, 0xfe85, 0xfe86}, // 0x0624 = ligature hamza on waw
51 {0xfe87, 0xfe88, 0xfe87, 0xfe88}, // 0x0625 = ligature hamza under alef
52 {0xfe89, 0xfe8a, 0xfe8b, 0xfe8c}, // 0x0626 = ligature hamza on ya
53 {0xfe8d, 0xfe8e, 0xfe8d, 0xfe8e}, // 0x0627 = alef
54 {0xfe8f, 0xfe90, 0xfe91, 0xfe92}, // 0x0628 = baa
55 {0xfe93, 0xfe94, 0xfe93, 0xfe94}, // 0x0629 = taa marbuta
56 {0xfe95, 0xfe96, 0xfe97, 0xfe98}, // 0x062a = taa
57 {0xfe99, 0xfe9a, 0xfe9b, 0xfe9c}, // 0x062b = thaa
58 {0xfe9d, 0xfe9e, 0xfe9f, 0xfea0}, // 0x062c = jeem
59 {0xfea1, 0xfea2, 0xfea3, 0xfea4}, // 0x062d = haa
60 {0xfea5, 0xfea6, 0xfea7, 0xfea8}, // 0x062e = khaa
61 {0xfea9, 0xfeaa, 0xfea9, 0xfeaa}, // 0x062f = dal
63 {0xfeab, 0xfeac, 0xfeab, 0xfeac}, // 0x0630 = thal
64 {0xfead, 0xfeae, 0xfead, 0xfeae}, // 0x0631 = ra
65 {0xfeaf, 0xfeb0, 0xfeaf, 0xfeb0}, // 0x0632 = zain
66 {0xfeb1, 0xfeb2, 0xfeb3, 0xfeb4}, // 0x0633 = seen
67 {0xfeb5, 0xfeb6, 0xfeb7, 0xfeb8}, // 0x0634 = sheen
68 {0xfeb9, 0xfeba, 0xfebb, 0xfebc}, // 0x0635 = sad
69 {0xfebd, 0xfebe, 0xfebf, 0xfec0}, // 0x0636 = dad
70 {0xfec1, 0xfec2, 0xfec3, 0xfec4}, // 0x0637 = tah
71 {0xfec5, 0xfec6, 0xfec7, 0xfec8}, // 0x0638 = zah
72 {0xfec9, 0xfeca, 0xfecb, 0xfecc}, // 0x0639 = ain
73 {0xfecd, 0xfece, 0xfecf, 0xfed0}, // 0x063a = ghain
74 {0, 0, 0, 0}, // 0x063b
75 {0, 0, 0, 0}, // 0x063c
76 {0, 0, 0, 0}, // 0x063d
77 {0, 0, 0, 0}, // 0x063e
78 {0, 0, 0, 0}, // 0x063f
80 {0, 0, 0, 0}, // 0x0640
81 {0xfed1, 0xfed2, 0xfed3, 0xfed4}, // 0x0641 = fa
82 {0xfed5, 0xfed6, 0xfed7, 0xfed8}, // 0x0642 = qaf
83 {0xfed9, 0xfeda, 0xfedb, 0xfedc}, // 0x0643 = kaf
84 {0xfedd, 0xfede, 0xfedf, 0xfee0}, // 0x0644 = lam
85 {0xfee1, 0xfee2, 0xfee3, 0xfee4}, // 0x0645 = meem
86 {0xfee5, 0xfee6, 0xfee7, 0xfee8}, // 0x0646 = noon
87 {0xfee9, 0xfeea, 0xfeeb, 0xfeec}, // 0x0647 = ha
88 {0xfeed, 0xfeee, 0xfeed, 0xfeee}, // 0x0648 = waw
89 {0xfeef, 0xfef0, 0xfeef, 0xfef0}, // 0x0649 = alef maksura
90 {0xfef1, 0xfef2, 0xfef3, 0xfef4}, // 0x064a = ya
91 {0x065b, 0x065b, 0x065b, 0x065b}, // 0x064b = fathatan
92 {0x065c, 0x065c, 0x065c, 0x065c}, // 0x064c = dammatan
93 {0x064d, 0x064d, 0x064d, 0x064d}, // 0x064d = kasratan
94 {0x064e, 0x064e, 0x064e, 0x064e}, // 0x064e = fatha
95 {0x064f, 0x064f, 0x064f, 0x064f}, // 0x064f = damma
97 {0x0650, 0x0650, 0x0650, 0x0650}, // 0x0650 = kasra
98 {0x0651, 0x0651, 0x0651, 0x0651}, // 0x0651 = shadda
99 {0x0652, 0x0652, 0x0652, 0x0652}, // 0x0652 = sukun
101 {0, 0, 0, 0}, // 0x0653
102 {0, 0, 0, 0}, // 0x0654
103 {0, 0, 0, 0}, // 0x0655
104 {0, 0, 0, 0}, // 0x0656
105 {0, 0, 0, 0}, // 0x0657
106 {0, 0, 0, 0}, // 0x0658
107 {0, 0, 0, 0}, // 0x0659
108 {0, 0, 0, 0}, // 0x065a
109 {0, 0, 0, 0}, // 0x065b
110 {0, 0, 0, 0}, // 0x065c
111 {0, 0, 0, 0}, // 0x065d
112 {0, 0, 0, 0}, // 0x065e
113 {0, 0, 0, 0}, // 0x065f
114 {0, 0, 0, 0}, // 0x0660
115 {0, 0, 0, 0}, // 0x0661
116 {0, 0, 0, 0}, // 0x0662
117 {0, 0, 0, 0}, // 0x0663
118 {0, 0, 0, 0}, // 0x0664
119 {0, 0, 0, 0}, // 0x0665
120 {0, 0, 0, 0}, // 0x0666
121 {0, 0, 0, 0}, // 0x0667
122 {0, 0, 0, 0}, // 0x0668
123 {0, 0, 0, 0}, // 0x0669
124 {0, 0, 0, 0}, // 0x066a
125 {0, 0, 0, 0}, // 0x066b
126 {0, 0, 0, 0}, // 0x066c
127 {0, 0, 0, 0}, // 0x066d
128 {0, 0, 0, 0}, // 0x066e
129 {0, 0, 0, 0}, // 0x066f
130 {0, 0, 0, 0}, // 0x0670
131 {0, 0, 0, 0}, // 0x0671
132 {0, 0, 0, 0}, // 0x0672
133 {0, 0, 0, 0}, // 0x0673
134 {0, 0, 0, 0}, // 0x0674
135 {0, 0, 0, 0}, // 0x0675
136 {0, 0, 0, 0}, // 0x0676
137 {0, 0, 0, 0}, // 0x0677
138 {0, 0, 0, 0}, // 0x0678
139 {0, 0, 0, 0}, // 0x0679
140 {0, 0, 0, 0}, // 0x067a
141 {0, 0, 0, 0}, // 0x067b
142 {0, 0, 0, 0}, // 0x067c
143 {0, 0, 0, 0}, // 0x067d
144 {0xfb56, 0xfb57, 0xfb58, 0xfb59}, // 0x067e = peh
145 {0, 0, 0, 0}, // 0x067f
146 {0, 0, 0, 0}, // 0x0680
147 {0, 0, 0, 0}, // 0x0681
148 {0, 0, 0, 0}, // 0x0682
149 {0, 0, 0, 0}, // 0x0683
150 {0, 0, 0, 0}, // 0x0684
151 {0, 0, 0, 0}, // 0x0685
152 {0xfb7a, 0xfb7b, 0xfb7c, 0xfb7d}, // 0x0686 = tcheh
153 {0, 0, 0, 0}, // 0x0687
154 {0, 0, 0, 0}, // 0x0688
155 {0, 0, 0, 0}, // 0x0689
156 {0, 0, 0, 0}, // 0x068a
157 {0, 0, 0, 0}, // 0x068b
158 {0, 0, 0, 0}, // 0x068c
159 {0, 0, 0, 0}, // 0x068d
160 {0, 0, 0, 0}, // 0x068e
161 {0, 0, 0, 0}, // 0x068f
162 {0, 0, 0, 0}, // 0x0690
163 {0, 0, 0, 0}, // 0x0691
164 {0, 0, 0, 0}, // 0x0692
165 {0, 0, 0, 0}, // 0x0693
166 {0, 0, 0, 0}, // 0x0694
167 {0, 0, 0, 0}, // 0x0695
168 {0, 0, 0, 0}, // 0x0696
169 {0, 0, 0, 0}, // 0x0697
170 {0xfb8a, 0xfb8b, 0xfb8a, 0xfb8b}, // 0x0698 = jeh
171 {0, 0, 0, 0}, // 0x0699
172 {0, 0, 0, 0}, // 0x069a
173 {0, 0, 0, 0}, // 0x069b
174 {0, 0, 0, 0}, // 0x069c
175 {0, 0, 0, 0}, // 0x069d
176 {0, 0, 0, 0}, // 0x069e
177 {0, 0, 0, 0}, // 0x069f
178 {0, 0, 0, 0}, // 0x06a0
179 {0, 0, 0, 0}, // 0x06a1
180 {0, 0, 0, 0}, // 0x06a2
181 {0, 0, 0, 0}, // 0x06a3
182 {0, 0, 0, 0}, // 0x06a4
183 {0, 0, 0, 0}, // 0x06a5
184 {0, 0, 0, 0}, // 0x06a6
185 {0, 0, 0, 0}, // 0x06a7
186 {0, 0, 0, 0}, // 0x06a8
187 {0xfb8e, 0xfb8f, 0xfb90, 0xfb91}, // 0x06a9 = farsi kaf
188 {0, 0, 0, 0}, // 0x06aa
189 {0, 0, 0, 0}, // 0x06ab
190 {0, 0, 0, 0}, // 0x06ac
191 {0, 0, 0, 0}, // 0x06ad
192 {0, 0, 0, 0}, // 0x06ae
193 {0xfb92, 0xfb93, 0xfb94, 0xfb95}, // 0x06af = gaf
194 {0, 0, 0, 0}, // 0x06b0
195 {0, 0, 0, 0}, // 0x06b1
196 {0, 0, 0, 0}, // 0x06b2
197 {0, 0, 0, 0}, // 0x06b3
198 {0, 0, 0, 0}, // 0x06b4
199 {0, 0, 0, 0}, // 0x06b5
200 {0, 0, 0, 0}, // 0x06b6
201 {0, 0, 0, 0}, // 0x06b7
202 {0, 0, 0, 0}, // 0x06b8
203 {0, 0, 0, 0}, // 0x06b9
204 {0, 0, 0, 0}, // 0x06ba
205 {0, 0, 0, 0}, // 0x06bb
206 {0, 0, 0, 0}, // 0x06bc
207 {0, 0, 0, 0}, // 0x06bd
208 {0, 0, 0, 0}, // 0x06be
209 {0, 0, 0, 0}, // 0x06bf
210 {0, 0, 0, 0}, // 0x06c0
211 {0, 0, 0, 0}, // 0x06c1
212 {0, 0, 0, 0}, // 0x06c2
213 {0, 0, 0, 0}, // 0x06c3
214 {0, 0, 0, 0}, // 0x06c4
215 {0, 0, 0, 0}, // 0x06c5
216 {0, 0, 0, 0}, // 0x06c6
217 {0, 0, 0, 0}, // 0x06c7
218 {0, 0, 0, 0}, // 0x06c8
219 {0, 0, 0, 0}, // 0x06c9
220 {0, 0, 0, 0}, // 0x06ca
221 {0, 0, 0, 0}, // 0x06cb
222 {0xfbfc, 0xfbfd, 0xfbfe, 0xfbff} // 0x06cc = farsi yeh
226 char_type const arabic_start = 0x0621;
227 char_type const arabic_end = 0x06cc;
230 /// Information about a single UCS4 character
232 /// LaTeX command (text mode) for this character
233 docstring textcommand;
234 /// LaTeX command (math mode) for this character
235 docstring mathcommand;
236 /// Needed LaTeX preamble (or feature) for text mode
238 /// Needed LaTeX preamble (or feature) for math mode
240 /// Is this a combining character?
242 /// Is \c textpreamble a feature known by LaTeXFeatures, or a raw LaTeX
245 /// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX
248 /// Always force the LaTeX command, even if the encoding contains
254 typedef map<char_type, CharInfo> CharInfoMap;
255 CharInfoMap unicodesymbols;
257 typedef std::set<char_type> CharSet;
260 typedef std::set<char_type> MathAlphaSet;
261 MathAlphaSet mathalpha;
264 /// The highest code point in UCS4 encoding (1<<20 + 1<<16)
265 char_type const max_ucs4 = 0x110000;
270 EncodingException::EncodingException(char_type c)
271 : failed_char(c), par_id(0), pos(0)
276 const char * EncodingException::what() const throw()
278 return "Could not find LaTeX command for a character";
282 Encoding::Encoding(string const & n, string const & l, string const & g,
283 string const & i, bool f, Encoding::Package p)
284 : name_(n), latexName_(l), guiName_(g), iconvName_(i), fixedwidth_(f), package_(p)
287 // ASCII can encode 128 code points and nothing else
288 start_encodable_ = 128;
290 } else if (i == "UTF-8") {
291 // UTF8 can encode all UCS4 code points
292 start_encodable_ = max_ucs4;
300 void Encoding::init() const
305 start_encodable_ = 0;
306 // temporarily switch off lyxerr, since we will generate iconv errors
309 // We do not need to check all UCS4 code points, it is enough
310 // if we check all 256 code points of this encoding.
311 for (unsigned short j = 0; j < 256; ++j) {
312 char const c = char(j);
313 vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, iconvName_);
314 if (ucs4.size() != 1)
316 char_type const uc = ucs4[0];
317 CharInfoMap::const_iterator const it = unicodesymbols.find(uc);
318 if (it == unicodesymbols.end() || !it->second.force)
319 encodable_.insert(uc);
322 // We do not know how many code points this encoding has, and
323 // they do not have a direct representation as a single byte,
324 // therefore we need to check all UCS4 code points.
325 // This is expensive!
326 for (char_type c = 0; c < max_ucs4; ++c) {
327 vector<char> const eightbit = ucs4_to_eightbit(&c, 1, iconvName_);
328 if (!eightbit.empty()) {
329 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
330 if (it == unicodesymbols.end() || !it->second.force)
331 encodable_.insert(c);
336 CharSet::iterator it = encodable_.find(start_encodable_);
337 while (it != encodable_.end()) {
338 encodable_.erase(it);
340 it = encodable_.find(start_encodable_);
346 docstring Encoding::latexChar(char_type c, bool for_mathed) const
348 // assure the used encoding is properly initialized
351 if (iconvName_ == "UTF-8" && package_ == none)
352 return docstring(1, c);
353 if (c < start_encodable_ && !encodings.isForced(c))
354 return docstring(1, c);
355 if (encodable_.find(c) != encodable_.end())
356 return docstring(1, c);
360 // c cannot (or should not) be encoded in this encoding
361 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
362 if (it == unicodesymbols.end())
363 throw EncodingException(c);
364 // at least one of mathcommand and textcommand is nonempty
365 if (it->second.textcommand.empty())
366 return "\\ensuremath{" + it->second.mathcommand + '}';
367 return it->second.textcommand;
371 vector<char_type> Encoding::symbolsList() const
373 // assure the used encoding is properly initialized
376 // first all encodable characters
377 vector<char_type> symbols(encodable_.begin(), encodable_.end());
378 // add those below start_encodable_
379 for (char_type c = 0; c < start_encodable_; ++c)
380 symbols.push_back(c);
381 // now the ones from the unicodesymbols file
382 CharInfoMap::const_iterator const end = unicodesymbols.end();
383 CharInfoMap::const_iterator it = unicodesymbols.begin();
384 for (; it != end; ++it)
385 symbols.push_back(it->first);
390 bool Encodings::latexMathChar(char_type c, bool mathmode,
391 Encoding const * encoding, docstring & command)
394 command = encoding->latexChar(c, true);
396 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
397 if (it == unicodesymbols.end()) {
398 if (!encoding || command.empty())
399 throw EncodingException(c);
404 // at least one of mathcommand and textcommand is nonempty
405 bool use_math = (mathmode && !it->second.mathcommand.empty()) ||
406 (!mathmode && it->second.textcommand.empty());
408 command = it->second.mathcommand;
411 if (!encoding || command.empty()) {
412 command = it->second.textcommand;
422 char_type Encodings::fromLaTeXCommand(docstring const & cmd, bool & combining)
424 CharInfoMap::const_iterator const end = unicodesymbols.end();
425 CharInfoMap::const_iterator it = unicodesymbols.begin();
426 for (combining = false; it != end; ++it) {
427 docstring const math = it->second.mathcommand;
428 docstring const text = it->second.textcommand;
429 if (math == cmd || text == cmd) {
430 combining = it->second.combining;
438 docstring Encodings::fromLaTeXCommand(docstring const & cmd, docstring & rem)
442 size_t const cmdend = cmd.size();
443 CharInfoMap::const_iterator const uniend = unicodesymbols.end();
444 for (size_t j = 0; j < cmdend; ++j) {
445 // Also get the char after a backslash
446 if (j + 1 < cmdend && cmd[j] == '\\')
448 // If a macro argument follows, get it, too
449 if (j + 1 < cmdend && cmd[j + 1] == '{') {
452 while (k < cmdend && count && k != docstring::npos) {
453 k = cmd.find_first_of(from_ascii("{}"), k + 1);
459 if (k != docstring::npos)
462 // Start with this substring and try augmenting it when it is
463 // the prefix of some command in the unicodesymbols file
464 docstring const subcmd = cmd.substr(i, j - i + 1);
466 CharInfoMap::const_iterator it = unicodesymbols.begin();
467 size_t unicmd_size = 0;
469 for (; it != uniend; ++it) {
470 docstring const math = it->second.mathcommand;
471 docstring const text = it->second.textcommand;
472 size_t cur_size = max(math.size(), text.size());
473 // The current math or text unicode command cannot
474 // match, or we already matched a longer one
475 if (cur_size < subcmd.size() || cur_size <= unicmd_size)
478 docstring tmp = subcmd;
480 while (prefixIs(math, tmp) || prefixIs(text, tmp)) {
482 if (k >= cmdend || cur_size <= tmp.size())
490 // The last added char caused a mismatch, because
491 // we didn't exhaust the chars in cmd and didn't
492 // exceed the maximum size of the current unicmd
493 if (k < cmdend && cur_size > tmp.size())
494 tmp.resize(tmp.size() - 1);
496 // If this is an exact match, we found a (longer)
497 // matching entry in the unicodesymbols file.
498 // If the entry doesn't start with '\', we take note
499 // of the match and continue (this is not a ultimate
500 // acceptance, as some other entry may match a longer
501 // portion of the cmd string). However, if the entry
502 // does start with '\', we accept the match only if
503 // this is a valid macro, i.e., either it is a single
504 // (nonletter) char macro, or nothing else follows,
505 // or what follows is a nonletter char.
506 if ((math == tmp || text == tmp)
508 || (tmp.size() == 2 && !isAlphaASCII(tmp[1]))
509 || k == cmdend || !isAlphaASCII(cmd[k]))) {
513 unicmd_size = cur_size;
518 else if (j + 1 == cmdend)
519 // No luck. Return what remains
526 void Encodings::initUnicodeMath(Buffer const & buffer)
529 // The code below is not needed in tex2lyx and requires additional stuff
536 Inset & inset = buffer.inset();
537 InsetIterator it = inset_iterator_begin(inset);
538 InsetIterator const end = inset_iterator_end(inset);
540 for (; it != end; ++it)
541 it->initUnicodeMath();
546 void Encodings::validate(char_type c, LaTeXFeatures & features, bool for_mathed)
549 // The code below is not needed in tex2lyx and requires additional stuff
554 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
555 if (it != unicodesymbols.end()) {
556 // In mathed, c could be used both in textmode and mathmode
557 bool const use_math = (for_mathed && isMathCmd(c)) ||
558 (!for_mathed && it->second.textcommand.empty());
559 bool const use_text = (for_mathed && isTextCmd(c)) ||
560 (!for_mathed && !it->second.textcommand.empty());
562 if (!it->second.mathpreamble.empty()) {
563 if (it->second.mathfeature) {
564 string feats = it->second.mathpreamble;
565 while (!feats.empty()) {
567 feats = split(feats, feat, ',');
568 features.require(feat);
571 features.addPreambleSnippet(it->second.mathpreamble);
575 if (!it->second.textpreamble.empty()) {
576 if (it->second.textfeature) {
577 string feats = it->second.textpreamble;
578 while (!feats.empty()) {
580 feats = split(feats, feat, ',');
581 features.require(feat);
584 features.addPreambleSnippet(it->second.textpreamble);
588 if (for_mathed && isMathSym(c)) {
589 features.require("relsize");
590 features.require("lyxmathsym");
596 bool Encodings::isHebrewComposeChar(char_type c)
598 return c <= 0x05c2 && c >= 0x05b0 && c != 0x05be && c != 0x05c0;
602 // Special Arabic letters are ones that do not get connected from left
603 // they are hamza, alef_madda, alef_hamza, waw_hamza, alef_hamza_under,
604 // alef, tah_marbota, dal, thal, rah, zai, wow, alef_maksoura
606 bool Encodings::isArabicSpecialChar(char_type c)
608 return (c >= 0x0621 && c <= 0x0625) || (c >= 0x0630 && c <= 0x0632)
609 || c == 0x0627 || c == 0x0629 || c == 0x062f || c == 0x0648
610 || c == 0x0649 || c == 0x0698;
614 bool Encodings::isArabicComposeChar(char_type c)
616 return c >= 0x064b && c <= 0x0652;
620 bool Encodings::isArabicChar(char_type c)
622 return c >= arabic_start && c <= arabic_end
623 && arabic_table[c-arabic_start][0];
627 char_type Encodings::transformChar(char_type c, Encodings::LetterForm form)
629 return isArabicChar(c) ? arabic_table[c-arabic_start][form] : c;
633 bool Encodings::isCombiningChar(char_type c)
635 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
636 if (it != unicodesymbols.end())
637 return it->second.combining;
642 bool Encodings::isKnownScriptChar(char_type const c, string & preamble)
644 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
646 if (it == unicodesymbols.end())
649 if (it->second.textpreamble != "textgreek" && it->second.textpreamble != "textcyr")
652 if (preamble.empty()) {
653 preamble = it->second.textpreamble;
656 return it->second.textpreamble == preamble;
660 bool Encodings::isForced(char_type c)
662 return (!forced.empty() && forced.find(c) != forced.end());
666 bool Encodings::isMathAlpha(char_type c)
668 return mathalpha.count(c);
672 Encoding const * Encodings::fromLyXName(string const & name) const
674 EncodingList::const_iterator const it = encodinglist.find(name);
675 return it != encodinglist.end() ? &it->second : 0;
679 Encoding const * Encodings::fromLaTeXName(string const & name) const
681 // We don't use find_if because it makes copies of the pairs in
683 // This linear search is OK since we don't have many encodings.
684 // Users could even optimize it by putting the encodings they use
685 // most at the top of lib/encodings.
686 EncodingList::const_iterator const end = encodinglist.end();
687 for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
688 if (it->second.latexName() == name)
694 Encodings::Encodings()
699 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
701 // We must read the symbolsfile first, because the Encoding
702 // constructor depends on it.
704 symbolslex.setFile(symbolsfile);
705 bool getNextToken = true;
706 while (symbolslex.isOK()) {
712 if (!symbolslex.next(true))
717 istringstream is(symbolslex.getString());
718 // reading symbol directly does not work if
719 // char_type == wchar_t.
721 if(!(is >> hex >> tmp))
725 if (!symbolslex.next(true))
727 info.textcommand = symbolslex.getDocString();
728 if (!symbolslex.next(true))
730 info.textpreamble = symbolslex.getString();
731 if (!symbolslex.next(true))
733 flags = symbolslex.getString();
735 info.combining = false;
736 info.textfeature = false;
738 while (!flags.empty()) {
740 flags = split(flags, flag, ',');
741 if (flag == "combining") {
742 info.combining = true;
743 } else if (flag == "force") {
745 forced.insert(symbol);
746 } else if (flag == "mathalpha") {
747 mathalpha.insert(symbol);
749 lyxerr << "Ignoring unknown flag `" << flag
750 << "' for symbol `0x"
751 << hex << symbol << dec
755 // mathcommand and mathpreamble have been added for 1.6.0.
756 // make them optional so that old files still work.
757 int const lineno = symbolslex.lineNumber();
758 bool breakout = false;
759 if (symbolslex.next(true)) {
760 if (symbolslex.lineNumber() != lineno) {
761 // line in old format without mathcommand and mathpreamble
762 getNextToken = false;
764 info.mathcommand = symbolslex.getDocString();
765 if (symbolslex.next(true)) {
766 if (symbolslex.lineNumber() != lineno) {
767 // line in new format with mathcommand only
768 getNextToken = false;
770 // line in new format with mathcommand and mathpreamble
771 info.mathpreamble = symbolslex.getString();
780 if (!info.textpreamble.empty())
781 info.textfeature = info.textpreamble[0] != '\\';
782 if (!info.mathpreamble.empty())
783 info.mathfeature = info.mathpreamble[0] != '\\';
785 LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '"
786 << to_utf8(info.textcommand) << "' '" << info.textpreamble
787 << "' " << info.combining << ' ' << info.textfeature
788 << " '" << to_utf8(info.mathcommand) << "' '"
789 << info.mathpreamble << "' " << info.mathfeature);
791 // we assume that at least one command is nonempty when using unicodesymbols
792 if (!info.textcommand.empty() || !info.mathcommand.empty())
793 unicodesymbols[symbol] = info;
799 // Now read the encodings
805 LexerKeyword encodingtags[] = {
806 { "encoding", et_encoding },
810 Lexer lex(encodingtags);
811 lex.setFile(encfile);
812 lex.setContext("Encodings::read");
818 string const name = lex.getString();
820 string const latexname = lex.getString();
822 string const guiname = lex.getString();
824 string const iconvname = lex.getString();
826 string const width = lex.getString();
827 bool fixedwidth = false;
828 if (width == "fixed")
830 else if (width == "variable")
833 lex.printError("Unknown width");
836 string const p = lex.getString();
837 Encoding::Package package = Encoding::none;
839 package = Encoding::none;
840 else if (p == "inputenc")
841 package = Encoding::inputenc;
843 package = Encoding::CJK;
844 else if (p == "japanese")
845 package = Encoding::japanese;
847 lex.printError("Unknown package");
849 LYXERR(Debug::INFO, "Reading encoding " << name);
850 encodinglist[name] = Encoding(name, latexname,
851 guiname, iconvname, fixedwidth, package);
853 if (lex.lex() != et_end)
854 lex.printError("Missing end");
858 lex.printError("Misplaced end");
860 case Lexer::LEX_FEOF:
863 lex.printError("Unknown tag");