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 "BufferList.h"
19 #include "InsetIterator.h"
20 #include "LaTeXFeatures.h"
24 #include "support/debug.h"
25 #include "support/gettext.h"
26 #include "support/FileName.h"
27 #include "support/lstrings.h"
28 #include "support/textutils.h"
29 #include "support/unicode.h"
31 #include <boost/cstdint.hpp>
36 using namespace lyx::support;
40 int const Encoding::any = -1;
44 Encodings::MathCommandSet Encodings::mathcmd;
45 Encodings::TextCommandSet Encodings::textcmd;
46 Encodings::MathSymbolSet Encodings::mathsym;
50 char_type arabic_table[172][4] = {
51 {0xfe80, 0xfe80, 0xfe80, 0xfe80}, // 0x0621 = hamza
52 {0xfe81, 0xfe82, 0xfe81, 0xfe82}, // 0x0622 = ligature madda on alef
53 {0xfe83, 0xfe84, 0xfe83, 0xfe84}, // 0x0623 = ligature hamza on alef
54 {0xfe85, 0xfe86, 0xfe85, 0xfe86}, // 0x0624 = ligature hamza on waw
55 {0xfe87, 0xfe88, 0xfe87, 0xfe88}, // 0x0625 = ligature hamza under alef
56 {0xfe89, 0xfe8a, 0xfe8b, 0xfe8c}, // 0x0626 = ligature hamza on ya
57 {0xfe8d, 0xfe8e, 0xfe8d, 0xfe8e}, // 0x0627 = alef
58 {0xfe8f, 0xfe90, 0xfe91, 0xfe92}, // 0x0628 = baa
59 {0xfe93, 0xfe94, 0xfe93, 0xfe94}, // 0x0629 = taa marbuta
60 {0xfe95, 0xfe96, 0xfe97, 0xfe98}, // 0x062a = taa
61 {0xfe99, 0xfe9a, 0xfe9b, 0xfe9c}, // 0x062b = thaa
62 {0xfe9d, 0xfe9e, 0xfe9f, 0xfea0}, // 0x062c = jeem
63 {0xfea1, 0xfea2, 0xfea3, 0xfea4}, // 0x062d = haa
64 {0xfea5, 0xfea6, 0xfea7, 0xfea8}, // 0x062e = khaa
65 {0xfea9, 0xfeaa, 0xfea9, 0xfeaa}, // 0x062f = dal
67 {0xfeab, 0xfeac, 0xfeab, 0xfeac}, // 0x0630 = thal
68 {0xfead, 0xfeae, 0xfead, 0xfeae}, // 0x0631 = ra
69 {0xfeaf, 0xfeb0, 0xfeaf, 0xfeb0}, // 0x0632 = zain
70 {0xfeb1, 0xfeb2, 0xfeb3, 0xfeb4}, // 0x0633 = seen
71 {0xfeb5, 0xfeb6, 0xfeb7, 0xfeb8}, // 0x0634 = sheen
72 {0xfeb9, 0xfeba, 0xfebb, 0xfebc}, // 0x0635 = sad
73 {0xfebd, 0xfebe, 0xfebf, 0xfec0}, // 0x0636 = dad
74 {0xfec1, 0xfec2, 0xfec3, 0xfec4}, // 0x0637 = tah
75 {0xfec5, 0xfec6, 0xfec7, 0xfec8}, // 0x0638 = zah
76 {0xfec9, 0xfeca, 0xfecb, 0xfecc}, // 0x0639 = ain
77 {0xfecd, 0xfece, 0xfecf, 0xfed0}, // 0x063a = ghain
78 {0, 0, 0, 0}, // 0x063b
79 {0, 0, 0, 0}, // 0x063c
80 {0, 0, 0, 0}, // 0x063d
81 {0, 0, 0, 0}, // 0x063e
82 {0, 0, 0, 0}, // 0x063f
84 {0, 0, 0, 0}, // 0x0640
85 {0xfed1, 0xfed2, 0xfed3, 0xfed4}, // 0x0641 = fa
86 {0xfed5, 0xfed6, 0xfed7, 0xfed8}, // 0x0642 = qaf
87 {0xfed9, 0xfeda, 0xfedb, 0xfedc}, // 0x0643 = kaf
88 {0xfedd, 0xfede, 0xfedf, 0xfee0}, // 0x0644 = lam
89 {0xfee1, 0xfee2, 0xfee3, 0xfee4}, // 0x0645 = meem
90 {0xfee5, 0xfee6, 0xfee7, 0xfee8}, // 0x0646 = noon
91 {0xfee9, 0xfeea, 0xfeeb, 0xfeec}, // 0x0647 = ha
92 {0xfeed, 0xfeee, 0xfeed, 0xfeee}, // 0x0648 = waw
93 {0xfeef, 0xfef0, 0xfeef, 0xfef0}, // 0x0649 = alef maksura
94 {0xfef1, 0xfef2, 0xfef3, 0xfef4}, // 0x064a = ya
95 {0x065b, 0x065b, 0x065b, 0x065b}, // 0x064b = fathatan
96 {0x065c, 0x065c, 0x065c, 0x065c}, // 0x064c = dammatan
97 {0x064d, 0x064d, 0x064d, 0x064d}, // 0x064d = kasratan
98 {0x064e, 0x064e, 0x064e, 0x064e}, // 0x064e = fatha
99 {0x064f, 0x064f, 0x064f, 0x064f}, // 0x064f = damma
101 {0x0650, 0x0650, 0x0650, 0x0650}, // 0x0650 = kasra
102 {0x0651, 0x0651, 0x0651, 0x0651}, // 0x0651 = shadda
103 {0x0652, 0x0652, 0x0652, 0x0652}, // 0x0652 = sukun
105 {0, 0, 0, 0}, // 0x0653
106 {0, 0, 0, 0}, // 0x0654
107 {0, 0, 0, 0}, // 0x0655
108 {0, 0, 0, 0}, // 0x0656
109 {0, 0, 0, 0}, // 0x0657
110 {0, 0, 0, 0}, // 0x0658
111 {0, 0, 0, 0}, // 0x0659
112 {0, 0, 0, 0}, // 0x065a
113 {0, 0, 0, 0}, // 0x065b
114 {0, 0, 0, 0}, // 0x065c
115 {0, 0, 0, 0}, // 0x065d
116 {0, 0, 0, 0}, // 0x065e
117 {0, 0, 0, 0}, // 0x065f
118 {0, 0, 0, 0}, // 0x0660
119 {0, 0, 0, 0}, // 0x0661
120 {0, 0, 0, 0}, // 0x0662
121 {0, 0, 0, 0}, // 0x0663
122 {0, 0, 0, 0}, // 0x0664
123 {0, 0, 0, 0}, // 0x0665
124 {0, 0, 0, 0}, // 0x0666
125 {0, 0, 0, 0}, // 0x0667
126 {0, 0, 0, 0}, // 0x0668
127 {0, 0, 0, 0}, // 0x0669
128 {0, 0, 0, 0}, // 0x066a
129 {0, 0, 0, 0}, // 0x066b
130 {0, 0, 0, 0}, // 0x066c
131 {0, 0, 0, 0}, // 0x066d
132 {0, 0, 0, 0}, // 0x066e
133 {0, 0, 0, 0}, // 0x066f
134 {0, 0, 0, 0}, // 0x0670
135 {0, 0, 0, 0}, // 0x0671
136 {0, 0, 0, 0}, // 0x0672
137 {0, 0, 0, 0}, // 0x0673
138 {0, 0, 0, 0}, // 0x0674
139 {0, 0, 0, 0}, // 0x0675
140 {0, 0, 0, 0}, // 0x0676
141 {0, 0, 0, 0}, // 0x0677
142 {0, 0, 0, 0}, // 0x0678
143 {0, 0, 0, 0}, // 0x0679
144 {0, 0, 0, 0}, // 0x067a
145 {0, 0, 0, 0}, // 0x067b
146 {0, 0, 0, 0}, // 0x067c
147 {0, 0, 0, 0}, // 0x067d
148 {0xfb56, 0xfb57, 0xfb58, 0xfb59}, // 0x067e = peh
149 {0, 0, 0, 0}, // 0x067f
150 {0, 0, 0, 0}, // 0x0680
151 {0, 0, 0, 0}, // 0x0681
152 {0, 0, 0, 0}, // 0x0682
153 {0, 0, 0, 0}, // 0x0683
154 {0, 0, 0, 0}, // 0x0684
155 {0, 0, 0, 0}, // 0x0685
156 {0xfb7a, 0xfb7b, 0xfb7c, 0xfb7d}, // 0x0686 = tcheh
157 {0, 0, 0, 0}, // 0x0687
158 {0, 0, 0, 0}, // 0x0688
159 {0, 0, 0, 0}, // 0x0689
160 {0, 0, 0, 0}, // 0x068a
161 {0, 0, 0, 0}, // 0x068b
162 {0, 0, 0, 0}, // 0x068c
163 {0, 0, 0, 0}, // 0x068d
164 {0, 0, 0, 0}, // 0x068e
165 {0, 0, 0, 0}, // 0x068f
166 {0, 0, 0, 0}, // 0x0690
167 {0, 0, 0, 0}, // 0x0691
168 {0, 0, 0, 0}, // 0x0692
169 {0, 0, 0, 0}, // 0x0693
170 {0, 0, 0, 0}, // 0x0694
171 {0, 0, 0, 0}, // 0x0695
172 {0, 0, 0, 0}, // 0x0696
173 {0, 0, 0, 0}, // 0x0697
174 {0xfb8a, 0xfb8b, 0xfb8a, 0xfb8b}, // 0x0698 = jeh
175 {0, 0, 0, 0}, // 0x0699
176 {0, 0, 0, 0}, // 0x069a
177 {0, 0, 0, 0}, // 0x069b
178 {0, 0, 0, 0}, // 0x069c
179 {0, 0, 0, 0}, // 0x069d
180 {0, 0, 0, 0}, // 0x069e
181 {0, 0, 0, 0}, // 0x069f
182 {0, 0, 0, 0}, // 0x06a0
183 {0, 0, 0, 0}, // 0x06a1
184 {0, 0, 0, 0}, // 0x06a2
185 {0, 0, 0, 0}, // 0x06a3
186 {0, 0, 0, 0}, // 0x06a4
187 {0, 0, 0, 0}, // 0x06a5
188 {0, 0, 0, 0}, // 0x06a6
189 {0, 0, 0, 0}, // 0x06a7
190 {0, 0, 0, 0}, // 0x06a8
191 {0xfb8e, 0xfb8f, 0xfb90, 0xfb91}, // 0x06a9 = farsi kaf
192 {0, 0, 0, 0}, // 0x06aa
193 {0, 0, 0, 0}, // 0x06ab
194 {0, 0, 0, 0}, // 0x06ac
195 {0, 0, 0, 0}, // 0x06ad
196 {0, 0, 0, 0}, // 0x06ae
197 {0xfb92, 0xfb93, 0xfb94, 0xfb95}, // 0x06af = gaf
198 {0, 0, 0, 0}, // 0x06b0
199 {0, 0, 0, 0}, // 0x06b1
200 {0, 0, 0, 0}, // 0x06b2
201 {0, 0, 0, 0}, // 0x06b3
202 {0, 0, 0, 0}, // 0x06b4
203 {0, 0, 0, 0}, // 0x06b5
204 {0, 0, 0, 0}, // 0x06b6
205 {0, 0, 0, 0}, // 0x06b7
206 {0, 0, 0, 0}, // 0x06b8
207 {0, 0, 0, 0}, // 0x06b9
208 {0, 0, 0, 0}, // 0x06ba
209 {0, 0, 0, 0}, // 0x06bb
210 {0, 0, 0, 0}, // 0x06bc
211 {0, 0, 0, 0}, // 0x06bd
212 {0, 0, 0, 0}, // 0x06be
213 {0, 0, 0, 0}, // 0x06bf
214 {0, 0, 0, 0}, // 0x06c0
215 {0, 0, 0, 0}, // 0x06c1
216 {0, 0, 0, 0}, // 0x06c2
217 {0, 0, 0, 0}, // 0x06c3
218 {0, 0, 0, 0}, // 0x06c4
219 {0, 0, 0, 0}, // 0x06c5
220 {0, 0, 0, 0}, // 0x06c6
221 {0, 0, 0, 0}, // 0x06c7
222 {0, 0, 0, 0}, // 0x06c8
223 {0, 0, 0, 0}, // 0x06c9
224 {0, 0, 0, 0}, // 0x06ca
225 {0, 0, 0, 0}, // 0x06cb
226 {0xfbfc, 0xfbfd, 0xfbfe, 0xfbff} // 0x06cc = farsi yeh
230 char_type const arabic_start = 0x0621;
231 char_type const arabic_end = 0x06cc;
236 CharInfoCombining = 1,
238 CharInfoTextFeature = 2,
240 CharInfoMathFeature = 4,
244 CharInfoTextNoTermination = 16,
246 CharInfoMathNoTermination = 32,
248 CharInfoForceSelected = 64,
251 /// Information about a single UCS4 character
253 /// LaTeX command (text mode) for this character
254 docstring textcommand;
255 /// LaTeX command (math mode) for this character
256 docstring mathcommand;
257 /// Needed LaTeX preamble (or feature) for text mode
259 /// Needed LaTeX preamble (or feature) for math mode
261 /// Is this a combining character?
262 bool combining() const { return flags & CharInfoCombining ? true : false; }
263 /// Is \c textpreamble a feature known by LaTeXFeatures, or a raw LaTeX
265 bool textfeature() const { return flags & CharInfoTextFeature ? true : false; }
266 /// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX
268 bool mathfeature() const { return flags & CharInfoMathFeature ? true : false; }
269 /// Always force the LaTeX command, even if the encoding contains
271 bool force() const { return flags & CharInfoForce ? true : false; }
272 /// Force the LaTeX command for some encodings?
273 bool forceselected() const { return flags & CharInfoForceSelected ? true : false; }
276 /// \c textcommand needs no termination (such as {} or space).
277 bool textnotermination() const { return flags & CharInfoTextNoTermination ? true : false; }
278 /// \c mathcommand needs no termination (such as {} or space).
279 bool mathnotermination() const { return flags & CharInfoMathNoTermination ? true : false; }
285 typedef map<char_type, CharInfo> CharInfoMap;
286 CharInfoMap unicodesymbols;
288 typedef std::set<char_type> CharSet;
289 typedef std::map<string, CharSet> CharSetMap;
291 CharSetMap forcedselected;
293 typedef std::set<char_type> MathAlphaSet;
294 MathAlphaSet mathalpha;
297 /// The highest code point in UCS4 encoding (1<<20 + 1<<16)
298 char_type const max_ucs4 = 0x110000;
303 EncodingException::EncodingException(char_type c)
304 : failed_char(c), par_id(0), pos(0)
309 const char * EncodingException::what() const throw()
311 return "Could not find LaTeX command for a character";
315 Encoding::Encoding(string const & n, string const & l, string const & g,
316 string const & i, bool f, bool u, Encoding::Package p)
317 : name_(n), latexName_(l), guiName_(g), iconvName_(i), fixedwidth_(f),
318 unsafe_(u), forced_(&forcedselected[n]), package_(p)
321 // ASCII can encode 128 code points and nothing else
322 start_encodable_ = 128;
324 } else if (i == "UTF-8") {
325 // UTF8 can encode all UCS4 code points
326 start_encodable_ = max_ucs4;
329 start_encodable_ = 0;
335 void Encoding::init() const
340 start_encodable_ = 0;
341 // temporarily switch off lyxerr, since we will generate iconv errors
344 // We do not need to check all UCS4 code points, it is enough
345 // if we check all 256 code points of this encoding.
346 for (unsigned short j = 0; j < 256; ++j) {
347 char const c = char(j);
348 vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, iconvName_);
349 if (ucs4.size() != 1)
351 char_type const uc = ucs4[0];
352 CharInfoMap::const_iterator const it = unicodesymbols.find(uc);
353 if (it == unicodesymbols.end())
354 encodable_.insert(uc);
355 else if (!it->second.force()) {
356 if (forced_->empty() || forced_->find(uc) == forced_->end())
357 encodable_.insert(uc);
361 // We do not know how many code points this encoding has, and
362 // they do not have a direct representation as a single byte,
363 // therefore we need to check all UCS4 code points.
364 // This is expensive!
365 for (char_type c = 0; c < max_ucs4; ++c) {
366 vector<char> const eightbit = ucs4_to_eightbit(&c, 1, iconvName_);
367 if (!eightbit.empty()) {
368 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
369 if (it == unicodesymbols.end())
370 encodable_.insert(c);
371 else if (!it->second.force()) {
372 if (forced_->empty() || forced_->find(c) == forced_->end())
373 encodable_.insert(c);
379 CharSet::iterator it = encodable_.find(start_encodable_);
380 while (it != encodable_.end()) {
381 encodable_.erase(it);
383 it = encodable_.find(start_encodable_);
389 bool Encoding::isForced(char_type c) const
391 if (!forced.empty() && forced.find(c) != forced.end())
393 return !forced_->empty() && forced_->find(c) != forced_->end();
397 bool Encoding::encodable(char_type c) const
399 // assure the used encoding is properly initialized
402 if (iconvName_ == "UTF-8" && package_ == none)
404 if (c < start_encodable_ && !isForced(c))
406 if (encodable_.find(c) != encodable_.end())
412 pair<docstring, bool> Encoding::latexChar(char_type c) const
415 return make_pair(docstring(1, c), false);
417 // c cannot (or should not) be encoded in this encoding
418 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
419 if (it == unicodesymbols.end())
420 throw EncodingException(c);
421 // at least one of mathcommand and textcommand is nonempty
422 if (it->second.textcommand.empty())
424 "\\ensuremath{" + it->second.mathcommand + '}', false);
425 return make_pair(it->second.textcommand, !it->second.textnotermination());
429 pair<docstring, docstring> Encoding::latexString(docstring const input, bool dryrun) const
433 bool terminate = false;
434 for (size_t n = 0; n < input.size(); ++n) {
436 char_type const c = input[n];
437 pair<docstring, bool> latex_char = latexChar(c);
438 docstring const latex = latex_char.first;
439 if (terminate && !prefixIs(latex, '\\')
440 && !prefixIs(latex, '{')
441 && !prefixIs(latex, '}')) {
442 // Prevent eating of a following
443 // space or command corruption by
444 // following characters
451 terminate = latex_char.second;
452 } catch (EncodingException & /* e */) {
453 LYXERR0("Uncodable character in latexString!");
455 result += "<" + _("LyX Warning: ")
456 + _("uncodable character") + " '";
457 result += docstring(1, input[n]);
460 uncodable += input[n];
463 return make_pair(result, uncodable);
467 vector<char_type> Encoding::symbolsList() const
469 // assure the used encoding is properly initialized
472 // first all encodable characters
473 vector<char_type> symbols(encodable_.begin(), encodable_.end());
474 // add those below start_encodable_
475 for (char_type c = 0; c < start_encodable_; ++c)
476 symbols.push_back(c);
477 // now the ones from the unicodesymbols file
478 CharInfoMap::const_iterator const end = unicodesymbols.end();
479 CharInfoMap::const_iterator it = unicodesymbols.begin();
480 for (; it != end; ++it)
481 symbols.push_back(it->first);
486 bool Encodings::latexMathChar(char_type c, bool mathmode,
487 Encoding const * encoding, docstring & command,
488 bool & needsTermination)
490 command = empty_docstring();
492 if (encoding->encodable(c))
493 command = docstring(1, c);
494 needsTermination = false;
496 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
497 if (it == unicodesymbols.end()) {
498 if (!encoding || command.empty())
499 throw EncodingException(c);
504 // at least one of mathcommand and textcommand is nonempty
505 bool use_math = (mathmode && !it->second.mathcommand.empty()) ||
506 (!mathmode && it->second.textcommand.empty());
508 command = it->second.mathcommand;
509 needsTermination = !it->second.mathnotermination();
512 if (!encoding || command.empty()) {
513 command = it->second.textcommand;
514 needsTermination = !it->second.textnotermination();
524 char_type Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
525 bool & combining, bool & needsTermination, set<string> * req)
527 CharInfoMap::const_iterator const end = unicodesymbols.end();
528 CharInfoMap::const_iterator it = unicodesymbols.begin();
529 for (combining = false; it != end; ++it) {
530 docstring const math = it->second.mathcommand;
531 docstring const text = it->second.textcommand;
532 if ((cmdtype & MATH_CMD) && math == cmd) {
533 combining = it->second.combining();
534 needsTermination = !it->second.mathnotermination();
535 if (req && it->second.mathfeature() &&
536 !it->second.mathpreamble.empty())
537 req->insert(it->second.mathpreamble);
540 if ((cmdtype & TEXT_CMD) && text == cmd) {
541 combining = it->second.combining();
542 needsTermination = !it->second.textnotermination();
543 if (req && it->second.textfeature() &&
544 !it->second.textpreamble.empty())
545 req->insert(it->second.textpreamble);
549 needsTermination = false;
554 docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
555 bool & needsTermination, docstring & rem, set<string> * req)
557 needsTermination = false;
558 rem = empty_docstring();
559 bool const mathmode = cmdtype & MATH_CMD;
560 bool const textmode = cmdtype & TEXT_CMD;
562 size_t const cmdend = cmd.size();
564 CharInfoMap::const_iterator const uniend = unicodesymbols.end();
565 for (size_t i = 0, j = 0; j < cmdend; ++j) {
566 // Also get the char after a backslash
567 if (j + 1 < cmdend && cmd[j] == '\\') {
570 // Detect things like \=*{e} as well
571 if (j + 3 < cmdend && cmd[j+1] == '*' &&
577 // position of the last character before a possible macro
580 // If a macro argument follows, get it, too
581 // Do it here only for single character commands. Other
582 // combining commands need this too, but they are handled in
583 // the loop below for performance reasons.
584 if (j + 1 < cmdend && cmd[j + 1] == '{') {
587 while (k < cmdend && count && k != docstring::npos) {
588 k = cmd.find_first_of(from_ascii("{}"), k + 1);
594 if (k != docstring::npos)
596 } else if (m + 1 < cmdend && isAlphaASCII(cmd[m])) {
597 while (m + 2 < cmdend && isAlphaASCII(cmd[m+1]))
600 // Start with this substring and try augmenting it when it is
601 // the prefix of some command in the unicodesymbols file
602 docstring subcmd = cmd.substr(i, j - i + 1);
604 CharInfoMap::const_iterator it = unicodesymbols.begin();
605 // First part of subcmd which might be a combining character
606 docstring combcmd = (m == j) ? docstring() : cmd.substr(i, m - i + 1);
607 // The combining character of combcmd if it exists
608 CharInfoMap::const_iterator combining = uniend;
609 size_t unicmd_size = 0;
611 for (; it != uniend; ++it) {
612 docstring const math = mathmode ? it->second.mathcommand
614 docstring const text = textmode ? it->second.textcommand
616 if (!combcmd.empty() && it->second.combining() &&
617 (math == combcmd || text == combcmd))
619 size_t cur_size = max(math.size(), text.size());
620 // The current math or text unicode command cannot
621 // match, or we already matched a longer one
622 if (cur_size < subcmd.size() || cur_size <= unicmd_size)
625 docstring tmp = subcmd;
627 while (prefixIs(math, tmp) || prefixIs(text, tmp)) {
629 if (k >= cmdend || cur_size <= tmp.size())
637 // The last added char caused a mismatch, because
638 // we didn't exhaust the chars in cmd and didn't
639 // exceed the maximum size of the current unicmd
640 if (k < cmdend && cur_size > tmp.size())
641 tmp.resize(tmp.size() - 1);
643 // If this is an exact match, we found a (longer)
644 // matching entry in the unicodesymbols file.
645 if (math != tmp && text != tmp)
647 // If we found a combining command, we need to append
648 // the macro argument if this has not been done above.
649 if (tmp == combcmd && combining != uniend &&
650 k < cmdend && cmd[k] == '{') {
653 while (l < cmdend && count && l != docstring::npos) {
654 l = cmd.find_first_of(from_ascii("{}"), l + 1);
660 if (l != docstring::npos) {
662 subcmd = cmd.substr(i, j - i + 1);
665 // If the entry doesn't start with '\', we take note
666 // of the match and continue (this is not a ultimate
667 // acceptance, as some other entry may match a longer
668 // portion of the cmd string). However, if the entry
669 // does start with '\', we accept the match only if
670 // this is a valid macro, i.e., either it is a single
671 // (nonletter) char macro, or nothing else follows,
672 // or what follows is a nonletter char, or the last
674 else if (tmp[0] != '\\'
675 || (tmp.size() == prefix + 1 &&
676 !isAlphaASCII(tmp[1]) &&
677 (prefix == 1 || !isAlphaASCII(tmp[2])))
679 || !isAlphaASCII(cmd[k])
680 || tmp[tmp.size() - 1] == '}'
685 unicmd_size = cur_size;
687 needsTermination = !it->second.mathnotermination();
689 needsTermination = !it->second.textnotermination();
691 if (math == tmp && it->second.mathfeature() &&
692 !it->second.mathpreamble.empty())
693 req->insert(it->second.mathpreamble);
694 if (text == tmp && it->second.textfeature() &&
695 !it->second.textpreamble.empty())
696 req->insert(it->second.textpreamble);
702 else if (combining != uniend &&
703 prefixIs(subcmd, combcmd + '{')) {
704 // We know that subcmd starts with combcmd and
705 // contains an argument in braces.
706 docstring const arg = subcmd.substr(
707 combcmd.length() + 1,
708 subcmd.length() - combcmd.length() - 2);
709 // If arg is a single character we can construct a
710 // combining sequence.
712 bool argcomb = false;
713 if (arg.size() == 1 && isAlnumASCII(arg[0]))
716 // Use the version of fromLaTeXCommand() that
717 // parses only one command, since we cannot
718 // use more than one character.
721 a = fromLaTeXCommand(arg, cmdtype, argcomb,
723 if (a && req && !argcomb)
724 req->insert(r.begin(), r.end());
727 // In unicode the combining character comes
730 symbols += combining->first;
735 if (j + 1 == cmdend && !unicmd_size) {
736 // No luck. Return what remains
738 if (needsTermination && !rem.empty()) {
739 if (rem.substr(0, 2) == "{}") {
741 needsTermination = false;
742 } else if (rem[0] == ' ') {
743 needsTermination = false;
744 // LaTeX would swallow all spaces
754 void Encodings::initUnicodeMath(Buffer const & buffer, bool for_master)
757 // The code below is not needed in tex2lyx and requires additional stuff
768 Inset & inset = buffer.inset();
769 InsetIterator it = inset_iterator_begin(inset);
770 InsetIterator const end = inset_iterator_end(inset);
771 for (; it != end; ++it)
772 it->initUnicodeMath();
778 ListOfBuffers blist = buffer.getDescendents();
779 ListOfBuffers::const_iterator bit = blist.begin();
780 ListOfBuffers::const_iterator const bend = blist.end();
781 for (; bit != bend; ++bit)
782 initUnicodeMath(**bit, false);
787 void Encodings::validate(char_type c, LaTeXFeatures & features, bool for_mathed)
790 // The code below is not needed in tex2lyx and requires additional stuff
795 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
796 if (it != unicodesymbols.end()) {
797 // In mathed, c could be used both in textmode and mathmode
798 bool const math_mode = for_mathed && isMathCmd(c);
799 bool const use_math = math_mode ||
800 (!for_mathed && it->second.textcommand.empty());
801 bool const use_text = (for_mathed && isTextCmd(c)) ||
802 (!for_mathed && !it->second.textcommand.empty());
803 bool const plain_utf8 = (features.runparams().encoding->name() == "utf8-plain");
804 bool const unicode_math = (features.isRequired("unicode-math")
805 && features.isAvailable("unicode-math"));
806 // with utf8-plain, we only load packages when in mathed (see #7766)
807 // and if we do not use unicode-math
808 if ((math_mode && !unicode_math)
809 || (use_math && !plain_utf8)) {
810 if (!it->second.mathpreamble.empty()) {
811 if (it->second.mathfeature()) {
812 string feats = it->second.mathpreamble;
813 while (!feats.empty()) {
815 feats = split(feats, feat, ',');
816 features.require(feat);
819 features.addPreambleSnippet(it->second.mathpreamble);
822 // with utf8-plain, we do not load packages (see #7766)
823 if (use_text && !plain_utf8) {
824 if (!it->second.textpreamble.empty()) {
825 if (it->second.textfeature()) {
826 string feats = it->second.textpreamble;
827 while (!feats.empty()) {
829 feats = split(feats, feat, ',');
830 features.require(feat);
833 features.addPreambleSnippet(it->second.textpreamble);
837 if (for_mathed && isMathSym(c)) {
838 features.require("amstext");
839 features.require("lyxmathsym");
845 bool Encodings::isHebrewComposeChar(char_type c)
847 return c <= 0x05c2 && c >= 0x05b0 && c != 0x05be && c != 0x05c0;
851 // Special Arabic letters are ones that do not get connected from left
852 // they are hamza, alef_madda, alef_hamza, waw_hamza, alef_hamza_under,
853 // alef, tah_marbota, dal, thal, rah, zai, wow, alef_maksoura
855 bool Encodings::isArabicSpecialChar(char_type c)
857 return (c >= 0x0621 && c <= 0x0625) || (c >= 0x0630 && c <= 0x0632)
858 || c == 0x0627 || c == 0x0629 || c == 0x062f || c == 0x0648
859 || c == 0x0649 || c == 0x0698;
863 bool Encodings::isArabicComposeChar(char_type c)
865 return c >= 0x064b && c <= 0x0652;
869 bool Encodings::isArabicChar(char_type c)
871 return c >= arabic_start && c <= arabic_end
872 && arabic_table[c-arabic_start][0];
876 char_type Encodings::transformChar(char_type c, Encodings::LetterForm form)
878 return isArabicChar(c) ? arabic_table[c-arabic_start][form] : c;
882 bool Encodings::isCombiningChar(char_type c)
884 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
885 if (it != unicodesymbols.end())
886 return it->second.combining();
891 string const Encodings::TIPAShortcut(char_type c)
893 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
894 if (it != unicodesymbols.end())
895 return it->second.tipashortcut;
900 bool Encodings::isKnownScriptChar(char_type const c, string & preamble)
902 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
904 if (it == unicodesymbols.end())
907 if (it->second.textpreamble != "textgreek" && it->second.textpreamble != "textcyr")
910 if (preamble.empty()) {
911 preamble = it->second.textpreamble;
914 return it->second.textpreamble == preamble;
918 bool Encodings::isMathAlpha(char_type c)
920 return mathalpha.count(c);
925 Encodings::fromLyXName(string const & name, bool allowUnsafe) const
927 EncodingList::const_iterator const it = encodinglist.find(name);
928 if (!allowUnsafe && it->second.unsafe())
930 return it != encodinglist.end() ? &it->second : 0;
935 Encodings::fromLaTeXName(string const & n, int const & p, bool allowUnsafe) const
938 // FIXME: if we have to test for too many of these synonyms,
939 // we should instead extend the format of lib/encodings
943 // We don't use find_if because it makes copies of the pairs in
945 // This linear search is OK since we don't have many encodings.
946 // Users could even optimize it by putting the encodings they use
947 // most at the top of lib/encodings.
948 EncodingList::const_iterator const end = encodinglist.end();
949 for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
950 if ((it->second.latexName() == name) && (it->second.package() & p)
951 && (!it->second.unsafe() || allowUnsafe))
958 Encodings::fromIconvName(string const & n, int const & p, bool allowUnsafe) const
960 EncodingList::const_iterator const end = encodinglist.end();
961 for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
962 if ((it->second.iconvName() == n) && (it->second.package() & p)
963 && (!it->second.unsafe() || allowUnsafe))
969 Encodings::Encodings()
973 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
975 // We must read the symbolsfile first, because the Encoding
976 // constructor depends on it.
977 CharSetMap forcednotselected;
979 symbolslex.setFile(symbolsfile);
980 bool getNextToken = true;
981 while (symbolslex.isOK()) {
987 if (!symbolslex.next(true))
992 istringstream is(symbolslex.getString());
993 // reading symbol directly does not work if
994 // char_type == wchar_t.
996 if(!(is >> hex >> tmp))
1000 if (!symbolslex.next(true))
1002 info.textcommand = symbolslex.getDocString();
1003 if (!symbolslex.next(true))
1005 info.textpreamble = symbolslex.getString();
1006 if (!symbolslex.next(true))
1008 flags = symbolslex.getString();
1011 if (suffixIs(info.textcommand, '}'))
1012 info.flags |= CharInfoTextNoTermination;
1013 if (suffixIs(info.mathcommand, '}'))
1014 info.flags |= CharInfoMathNoTermination;
1015 while (!flags.empty()) {
1017 flags = split(flags, flag, ',');
1018 if (flag == "combining") {
1019 info.flags |= CharInfoCombining;
1020 } else if (flag == "force") {
1021 info.flags |= CharInfoForce;
1022 forced.insert(symbol);
1023 } else if (prefixIs(flag, "force=")) {
1024 vector<string> encodings =
1025 getVectorFromString(flag.substr(6), ";");
1026 for (size_t i = 0; i < encodings.size(); ++i)
1027 forcedselected[encodings[i]].insert(symbol);
1028 info.flags |= CharInfoForceSelected;
1029 } else if (prefixIs(flag, "force!=")) {
1030 vector<string> encodings =
1031 getVectorFromString(flag.substr(7), ";");
1032 for (size_t i = 0; i < encodings.size(); ++i)
1033 forcednotselected[encodings[i]].insert(symbol);
1034 info.flags |= CharInfoForceSelected;
1035 } else if (flag == "mathalpha") {
1036 mathalpha.insert(symbol);
1037 } else if (flag == "notermination=text") {
1038 info.flags |= CharInfoTextNoTermination;
1039 } else if (flag == "notermination=math") {
1040 info.flags |= CharInfoMathNoTermination;
1041 } else if (flag == "notermination=both") {
1042 info.flags |= CharInfoTextNoTermination;
1043 info.flags |= CharInfoMathNoTermination;
1044 } else if (flag == "notermination=none") {
1045 info.flags &= ~CharInfoTextNoTermination;
1046 info.flags &= ~CharInfoMathNoTermination;
1047 } else if (contains(flag, "tipashortcut=")) {
1048 info.tipashortcut = split(flag, '=');
1050 lyxerr << "Ignoring unknown flag `" << flag
1051 << "' for symbol `0x"
1052 << hex << symbol << dec
1056 // mathcommand and mathpreamble have been added for 1.6.0.
1057 // make them optional so that old files still work.
1058 int const lineno = symbolslex.lineNumber();
1059 bool breakout = false;
1060 if (symbolslex.next(true)) {
1061 if (symbolslex.lineNumber() != lineno) {
1062 // line in old format without mathcommand and mathpreamble
1063 getNextToken = false;
1065 info.mathcommand = symbolslex.getDocString();
1066 if (symbolslex.next(true)) {
1067 if (symbolslex.lineNumber() != lineno) {
1068 // line in new format with mathcommand only
1069 getNextToken = false;
1071 // line in new format with mathcommand and mathpreamble
1072 info.mathpreamble = symbolslex.getString();
1081 // backward compatibility
1082 if (info.mathpreamble == "esintoramsmath")
1083 info.mathpreamble = "esint|amsmath";
1085 if (!info.textpreamble.empty())
1086 if (info.textpreamble[0] != '\\')
1087 info.flags |= CharInfoTextFeature;
1088 if (!info.mathpreamble.empty())
1089 if (info.mathpreamble[0] != '\\')
1090 info.flags |= CharInfoMathFeature;
1092 LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '"
1093 << to_utf8(info.textcommand) << "' '" << info.textpreamble
1094 << " '" << info.textfeature() << ' ' << info.textnotermination()
1095 << ' ' << to_utf8(info.mathcommand) << "' '" << info.mathpreamble
1096 << "' " << info.mathfeature() << ' ' << info.mathnotermination()
1097 << ' ' << info.combining() << ' ' << info.force()
1098 << ' ' << info.forceselected());
1100 // we assume that at least one command is nonempty when using unicodesymbols
1101 if (!info.textcommand.empty() || !info.mathcommand.empty())
1102 unicodesymbols[symbol] = info;
1108 // Now read the encodings
1114 LexerKeyword encodingtags[] = {
1115 { "encoding", et_encoding },
1119 Lexer lex(encodingtags);
1120 lex.setFile(encfile);
1121 lex.setContext("Encodings::read");
1122 while (lex.isOK()) {
1123 switch (lex.lex()) {
1127 string const name = lex.getString();
1129 string const latexname = lex.getString();
1131 string const guiname = lex.getString();
1133 string const iconvname = lex.getString();
1135 string const width = lex.getString();
1136 bool fixedwidth = false;
1137 bool unsafe = false;
1138 if (width == "fixed")
1140 else if (width == "variable")
1142 else if (width == "variableunsafe") {
1147 lex.printError("Unknown width");
1150 string const p = lex.getString();
1151 Encoding::Package package = Encoding::none;
1153 package = Encoding::none;
1154 else if (p == "inputenc")
1155 package = Encoding::inputenc;
1156 else if (p == "CJK")
1157 package = Encoding::CJK;
1158 else if (p == "japanese")
1159 package = Encoding::japanese;
1161 lex.printError("Unknown package");
1163 LYXERR(Debug::INFO, "Reading encoding " << name);
1164 encodinglist[name] = Encoding(name, latexname,
1165 guiname, iconvname, fixedwidth, unsafe,
1168 if (lex.lex() != et_end)
1169 lex.printError("Missing end");
1173 lex.printError("Misplaced end");
1175 case Lexer::LEX_FEOF:
1178 lex.printError("Unknown tag");
1183 // Move all information from forcednotselected to forcedselected
1184 for (CharSetMap::const_iterator it1 = forcednotselected.begin(); it1 != forcednotselected.end(); ++it1) {
1185 for (CharSetMap::iterator it2 = forcedselected.begin(); it2 != forcedselected.end(); ++it2) {
1186 if (it2->first != it1->first)
1187 it2->second.insert(it1->second.begin(), it1->second.end());