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;
42 Encodings::MathCommandSet Encodings::mathcmd;
43 Encodings::TextCommandSet Encodings::textcmd;
44 Encodings::MathSymbolSet Encodings::mathsym;
48 char_type arabic_table[172][4] = {
49 {0xfe80, 0xfe80, 0xfe80, 0xfe80}, // 0x0621 = hamza
50 {0xfe81, 0xfe82, 0xfe81, 0xfe82}, // 0x0622 = ligature madda on alef
51 {0xfe83, 0xfe84, 0xfe83, 0xfe84}, // 0x0623 = ligature hamza on alef
52 {0xfe85, 0xfe86, 0xfe85, 0xfe86}, // 0x0624 = ligature hamza on waw
53 {0xfe87, 0xfe88, 0xfe87, 0xfe88}, // 0x0625 = ligature hamza under alef
54 {0xfe89, 0xfe8a, 0xfe8b, 0xfe8c}, // 0x0626 = ligature hamza on ya
55 {0xfe8d, 0xfe8e, 0xfe8d, 0xfe8e}, // 0x0627 = alef
56 {0xfe8f, 0xfe90, 0xfe91, 0xfe92}, // 0x0628 = baa
57 {0xfe93, 0xfe94, 0xfe93, 0xfe94}, // 0x0629 = taa marbuta
58 {0xfe95, 0xfe96, 0xfe97, 0xfe98}, // 0x062a = taa
59 {0xfe99, 0xfe9a, 0xfe9b, 0xfe9c}, // 0x062b = thaa
60 {0xfe9d, 0xfe9e, 0xfe9f, 0xfea0}, // 0x062c = jeem
61 {0xfea1, 0xfea2, 0xfea3, 0xfea4}, // 0x062d = haa
62 {0xfea5, 0xfea6, 0xfea7, 0xfea8}, // 0x062e = khaa
63 {0xfea9, 0xfeaa, 0xfea9, 0xfeaa}, // 0x062f = dal
65 {0xfeab, 0xfeac, 0xfeab, 0xfeac}, // 0x0630 = thal
66 {0xfead, 0xfeae, 0xfead, 0xfeae}, // 0x0631 = ra
67 {0xfeaf, 0xfeb0, 0xfeaf, 0xfeb0}, // 0x0632 = zain
68 {0xfeb1, 0xfeb2, 0xfeb3, 0xfeb4}, // 0x0633 = seen
69 {0xfeb5, 0xfeb6, 0xfeb7, 0xfeb8}, // 0x0634 = sheen
70 {0xfeb9, 0xfeba, 0xfebb, 0xfebc}, // 0x0635 = sad
71 {0xfebd, 0xfebe, 0xfebf, 0xfec0}, // 0x0636 = dad
72 {0xfec1, 0xfec2, 0xfec3, 0xfec4}, // 0x0637 = tah
73 {0xfec5, 0xfec6, 0xfec7, 0xfec8}, // 0x0638 = zah
74 {0xfec9, 0xfeca, 0xfecb, 0xfecc}, // 0x0639 = ain
75 {0xfecd, 0xfece, 0xfecf, 0xfed0}, // 0x063a = ghain
76 {0, 0, 0, 0}, // 0x063b
77 {0, 0, 0, 0}, // 0x063c
78 {0, 0, 0, 0}, // 0x063d
79 {0, 0, 0, 0}, // 0x063e
80 {0, 0, 0, 0}, // 0x063f
82 {0, 0, 0, 0}, // 0x0640
83 {0xfed1, 0xfed2, 0xfed3, 0xfed4}, // 0x0641 = fa
84 {0xfed5, 0xfed6, 0xfed7, 0xfed8}, // 0x0642 = qaf
85 {0xfed9, 0xfeda, 0xfedb, 0xfedc}, // 0x0643 = kaf
86 {0xfedd, 0xfede, 0xfedf, 0xfee0}, // 0x0644 = lam
87 {0xfee1, 0xfee2, 0xfee3, 0xfee4}, // 0x0645 = meem
88 {0xfee5, 0xfee6, 0xfee7, 0xfee8}, // 0x0646 = noon
89 {0xfee9, 0xfeea, 0xfeeb, 0xfeec}, // 0x0647 = ha
90 {0xfeed, 0xfeee, 0xfeed, 0xfeee}, // 0x0648 = waw
91 {0xfeef, 0xfef0, 0xfeef, 0xfef0}, // 0x0649 = alef maksura
92 {0xfef1, 0xfef2, 0xfef3, 0xfef4}, // 0x064a = ya
93 {0x065b, 0x065b, 0x065b, 0x065b}, // 0x064b = fathatan
94 {0x065c, 0x065c, 0x065c, 0x065c}, // 0x064c = dammatan
95 {0x064d, 0x064d, 0x064d, 0x064d}, // 0x064d = kasratan
96 {0x064e, 0x064e, 0x064e, 0x064e}, // 0x064e = fatha
97 {0x064f, 0x064f, 0x064f, 0x064f}, // 0x064f = damma
99 {0x0650, 0x0650, 0x0650, 0x0650}, // 0x0650 = kasra
100 {0x0651, 0x0651, 0x0651, 0x0651}, // 0x0651 = shadda
101 {0x0652, 0x0652, 0x0652, 0x0652}, // 0x0652 = sukun
103 {0, 0, 0, 0}, // 0x0653
104 {0, 0, 0, 0}, // 0x0654
105 {0, 0, 0, 0}, // 0x0655
106 {0, 0, 0, 0}, // 0x0656
107 {0, 0, 0, 0}, // 0x0657
108 {0, 0, 0, 0}, // 0x0658
109 {0, 0, 0, 0}, // 0x0659
110 {0, 0, 0, 0}, // 0x065a
111 {0, 0, 0, 0}, // 0x065b
112 {0, 0, 0, 0}, // 0x065c
113 {0, 0, 0, 0}, // 0x065d
114 {0, 0, 0, 0}, // 0x065e
115 {0, 0, 0, 0}, // 0x065f
116 {0, 0, 0, 0}, // 0x0660
117 {0, 0, 0, 0}, // 0x0661
118 {0, 0, 0, 0}, // 0x0662
119 {0, 0, 0, 0}, // 0x0663
120 {0, 0, 0, 0}, // 0x0664
121 {0, 0, 0, 0}, // 0x0665
122 {0, 0, 0, 0}, // 0x0666
123 {0, 0, 0, 0}, // 0x0667
124 {0, 0, 0, 0}, // 0x0668
125 {0, 0, 0, 0}, // 0x0669
126 {0, 0, 0, 0}, // 0x066a
127 {0, 0, 0, 0}, // 0x066b
128 {0, 0, 0, 0}, // 0x066c
129 {0, 0, 0, 0}, // 0x066d
130 {0, 0, 0, 0}, // 0x066e
131 {0, 0, 0, 0}, // 0x066f
132 {0, 0, 0, 0}, // 0x0670
133 {0, 0, 0, 0}, // 0x0671
134 {0, 0, 0, 0}, // 0x0672
135 {0, 0, 0, 0}, // 0x0673
136 {0, 0, 0, 0}, // 0x0674
137 {0, 0, 0, 0}, // 0x0675
138 {0, 0, 0, 0}, // 0x0676
139 {0, 0, 0, 0}, // 0x0677
140 {0, 0, 0, 0}, // 0x0678
141 {0, 0, 0, 0}, // 0x0679
142 {0, 0, 0, 0}, // 0x067a
143 {0, 0, 0, 0}, // 0x067b
144 {0, 0, 0, 0}, // 0x067c
145 {0, 0, 0, 0}, // 0x067d
146 {0xfb56, 0xfb57, 0xfb58, 0xfb59}, // 0x067e = peh
147 {0, 0, 0, 0}, // 0x067f
148 {0, 0, 0, 0}, // 0x0680
149 {0, 0, 0, 0}, // 0x0681
150 {0, 0, 0, 0}, // 0x0682
151 {0, 0, 0, 0}, // 0x0683
152 {0, 0, 0, 0}, // 0x0684
153 {0, 0, 0, 0}, // 0x0685
154 {0xfb7a, 0xfb7b, 0xfb7c, 0xfb7d}, // 0x0686 = tcheh
155 {0, 0, 0, 0}, // 0x0687
156 {0, 0, 0, 0}, // 0x0688
157 {0, 0, 0, 0}, // 0x0689
158 {0, 0, 0, 0}, // 0x068a
159 {0, 0, 0, 0}, // 0x068b
160 {0, 0, 0, 0}, // 0x068c
161 {0, 0, 0, 0}, // 0x068d
162 {0, 0, 0, 0}, // 0x068e
163 {0, 0, 0, 0}, // 0x068f
164 {0, 0, 0, 0}, // 0x0690
165 {0, 0, 0, 0}, // 0x0691
166 {0, 0, 0, 0}, // 0x0692
167 {0, 0, 0, 0}, // 0x0693
168 {0, 0, 0, 0}, // 0x0694
169 {0, 0, 0, 0}, // 0x0695
170 {0, 0, 0, 0}, // 0x0696
171 {0, 0, 0, 0}, // 0x0697
172 {0xfb8a, 0xfb8b, 0xfb8a, 0xfb8b}, // 0x0698 = jeh
173 {0, 0, 0, 0}, // 0x0699
174 {0, 0, 0, 0}, // 0x069a
175 {0, 0, 0, 0}, // 0x069b
176 {0, 0, 0, 0}, // 0x069c
177 {0, 0, 0, 0}, // 0x069d
178 {0, 0, 0, 0}, // 0x069e
179 {0, 0, 0, 0}, // 0x069f
180 {0, 0, 0, 0}, // 0x06a0
181 {0, 0, 0, 0}, // 0x06a1
182 {0, 0, 0, 0}, // 0x06a2
183 {0, 0, 0, 0}, // 0x06a3
184 {0, 0, 0, 0}, // 0x06a4
185 {0, 0, 0, 0}, // 0x06a5
186 {0, 0, 0, 0}, // 0x06a6
187 {0, 0, 0, 0}, // 0x06a7
188 {0, 0, 0, 0}, // 0x06a8
189 {0xfb8e, 0xfb8f, 0xfb90, 0xfb91}, // 0x06a9 = farsi kaf
190 {0, 0, 0, 0}, // 0x06aa
191 {0, 0, 0, 0}, // 0x06ab
192 {0, 0, 0, 0}, // 0x06ac
193 {0, 0, 0, 0}, // 0x06ad
194 {0, 0, 0, 0}, // 0x06ae
195 {0xfb92, 0xfb93, 0xfb94, 0xfb95}, // 0x06af = gaf
196 {0, 0, 0, 0}, // 0x06b0
197 {0, 0, 0, 0}, // 0x06b1
198 {0, 0, 0, 0}, // 0x06b2
199 {0, 0, 0, 0}, // 0x06b3
200 {0, 0, 0, 0}, // 0x06b4
201 {0, 0, 0, 0}, // 0x06b5
202 {0, 0, 0, 0}, // 0x06b6
203 {0, 0, 0, 0}, // 0x06b7
204 {0, 0, 0, 0}, // 0x06b8
205 {0, 0, 0, 0}, // 0x06b9
206 {0, 0, 0, 0}, // 0x06ba
207 {0, 0, 0, 0}, // 0x06bb
208 {0, 0, 0, 0}, // 0x06bc
209 {0, 0, 0, 0}, // 0x06bd
210 {0, 0, 0, 0}, // 0x06be
211 {0, 0, 0, 0}, // 0x06bf
212 {0, 0, 0, 0}, // 0x06c0
213 {0, 0, 0, 0}, // 0x06c1
214 {0, 0, 0, 0}, // 0x06c2
215 {0, 0, 0, 0}, // 0x06c3
216 {0, 0, 0, 0}, // 0x06c4
217 {0, 0, 0, 0}, // 0x06c5
218 {0, 0, 0, 0}, // 0x06c6
219 {0, 0, 0, 0}, // 0x06c7
220 {0, 0, 0, 0}, // 0x06c8
221 {0, 0, 0, 0}, // 0x06c9
222 {0, 0, 0, 0}, // 0x06ca
223 {0, 0, 0, 0}, // 0x06cb
224 {0xfbfc, 0xfbfd, 0xfbfe, 0xfbff} // 0x06cc = farsi yeh
228 char_type const arabic_start = 0x0621;
229 char_type const arabic_end = 0x06cc;
234 CharInfoCombining = 1,
236 CharInfoTextFeature = 2,
238 CharInfoMathFeature = 4,
242 CharInfoTextNoTermination = 16,
244 CharInfoMathNoTermination = 32,
246 CharInfoForceSelected = 64,
249 /// Information about a single UCS4 character
251 /// LaTeX command (text mode) for this character
252 docstring textcommand;
253 /// LaTeX command (math mode) for this character
254 docstring mathcommand;
255 /// Needed LaTeX preamble (or feature) for text mode
257 /// Needed LaTeX preamble (or feature) for math mode
259 /// Is this a combining character?
260 bool combining() const { return flags & CharInfoCombining ? true : false; }
261 /// Is \c textpreamble a feature known by LaTeXFeatures, or a raw LaTeX
263 bool textfeature() const { return flags & CharInfoTextFeature ? true : false; }
264 /// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX
266 bool mathfeature() const { return flags & CharInfoMathFeature ? true : false; }
267 /// Always force the LaTeX command, even if the encoding contains
269 bool force() const { return flags & CharInfoForce ? true : false; }
270 /// Force the LaTeX command for some encodings?
271 bool forceselected() const { return flags & CharInfoForceSelected ? true : false; }
274 /// \c textcommand needs no termination (such as {} or space).
275 bool textnotermination() const { return flags & CharInfoTextNoTermination ? true : false; }
276 /// \c mathcommand needs no termination (such as {} or space).
277 bool mathnotermination() const { return flags & CharInfoMathNoTermination ? true : false; }
283 typedef map<char_type, CharInfo> CharInfoMap;
284 CharInfoMap unicodesymbols;
286 typedef std::set<char_type> CharSet;
287 typedef std::map<string, CharSet> CharSetMap;
289 CharSetMap forcedselected;
291 typedef std::set<char_type> MathAlphaSet;
292 MathAlphaSet mathalpha;
295 /// The highest code point in UCS4 encoding (1<<20 + 1<<16)
296 char_type const max_ucs4 = 0x110000;
301 EncodingException::EncodingException(char_type c)
302 : failed_char(c), par_id(0), pos(0)
307 const char * EncodingException::what() const throw()
309 return "Could not find LaTeX command for a character";
313 Encoding::Encoding(string const & n, string const & l, string const & g,
314 string const & i, bool f, bool u, Encoding::Package p)
315 : name_(n), latexName_(l), guiName_(g), iconvName_(i), fixedwidth_(f),
316 unsafe_(u), forced_(&forcedselected[n]), package_(p)
319 // ASCII can encode 128 code points and nothing else
320 start_encodable_ = 128;
322 } else if (i == "UTF-8") {
323 // UTF8 can encode all UCS4 code points
324 start_encodable_ = max_ucs4;
327 start_encodable_ = 0;
333 void Encoding::init() const
338 start_encodable_ = 0;
339 // temporarily switch off lyxerr, since we will generate iconv errors
342 // We do not need to check all UCS4 code points, it is enough
343 // if we check all 256 code points of this encoding.
344 for (unsigned short j = 0; j < 256; ++j) {
345 char const c = char(j);
346 vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, iconvName_);
347 if (ucs4.size() != 1)
349 char_type const uc = ucs4[0];
350 CharInfoMap::const_iterator const it = unicodesymbols.find(uc);
351 if (it == unicodesymbols.end())
352 encodable_.insert(uc);
353 else if (!it->second.force()) {
354 if (forced_->empty() || forced_->find(uc) == forced_->end())
355 encodable_.insert(uc);
359 // We do not know how many code points this encoding has, and
360 // they do not have a direct representation as a single byte,
361 // therefore we need to check all UCS4 code points.
362 // This is expensive!
363 for (char_type c = 0; c < max_ucs4; ++c) {
364 vector<char> const eightbit = ucs4_to_eightbit(&c, 1, iconvName_);
365 if (!eightbit.empty()) {
366 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
367 if (it == unicodesymbols.end())
368 encodable_.insert(c);
369 else if (!it->second.force()) {
370 if (forced_->empty() || forced_->find(c) == forced_->end())
371 encodable_.insert(c);
377 CharSet::iterator it = encodable_.find(start_encodable_);
378 while (it != encodable_.end()) {
379 encodable_.erase(it);
381 it = encodable_.find(start_encodable_);
387 bool Encoding::isForced(char_type c) const
389 if (!forced.empty() && forced.find(c) != forced.end())
391 return !forced_->empty() && forced_->find(c) != forced_->end();
395 bool Encoding::encodable(char_type c) const
397 // assure the used encoding is properly initialized
400 if (iconvName_ == "UTF-8" && package_ == none)
402 if (c < start_encodable_ && !isForced(c))
404 if (encodable_.find(c) != encodable_.end())
410 pair<docstring, bool> Encoding::latexChar(char_type c) const
413 return make_pair(docstring(1, c), false);
415 // c cannot (or should not) be encoded in this encoding
416 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
417 if (it == unicodesymbols.end())
418 throw EncodingException(c);
419 // at least one of mathcommand and textcommand is nonempty
420 if (it->second.textcommand.empty())
422 "\\ensuremath{" + it->second.mathcommand + '}', false);
423 return make_pair(it->second.textcommand, !it->second.textnotermination());
427 pair<docstring, docstring> Encoding::latexString(docstring const input, bool dryrun) const
431 bool terminate = false;
432 for (size_t n = 0; n < input.size(); ++n) {
434 char_type const c = input[n];
435 pair<docstring, bool> latex_char = latexChar(c);
436 docstring const latex = latex_char.first;
437 if (terminate && !prefixIs(latex, '\\')
438 && !prefixIs(latex, '{')
439 && !prefixIs(latex, '}')) {
440 // Prevent eating of a following
441 // space or command corruption by
442 // following characters
449 terminate = latex_char.second;
450 } catch (EncodingException & /* e */) {
451 LYXERR0("Uncodable character in latexString!");
453 result += "<" + _("LyX Warning: ")
454 + _("uncodable character") + " '";
455 result += docstring(1, input[n]);
458 uncodable += input[n];
461 return make_pair(result, uncodable);
465 vector<char_type> Encoding::symbolsList() const
467 // assure the used encoding is properly initialized
470 // first all encodable characters
471 vector<char_type> symbols(encodable_.begin(), encodable_.end());
472 // add those below start_encodable_
473 for (char_type c = 0; c < start_encodable_; ++c)
474 symbols.push_back(c);
475 // now the ones from the unicodesymbols file
476 CharInfoMap::const_iterator const end = unicodesymbols.end();
477 CharInfoMap::const_iterator it = unicodesymbols.begin();
478 for (; it != end; ++it)
479 symbols.push_back(it->first);
484 bool Encodings::latexMathChar(char_type c, bool mathmode,
485 Encoding const * encoding, docstring & command,
486 bool & needsTermination)
488 command = empty_docstring();
490 if (encoding->encodable(c))
491 command = docstring(1, c);
492 needsTermination = false;
494 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
495 if (it == unicodesymbols.end()) {
496 if (!encoding || command.empty())
497 throw EncodingException(c);
502 // at least one of mathcommand and textcommand is nonempty
503 bool use_math = (mathmode && !it->second.mathcommand.empty()) ||
504 (!mathmode && it->second.textcommand.empty());
506 command = it->second.mathcommand;
507 needsTermination = !it->second.mathnotermination();
510 if (!encoding || command.empty()) {
511 command = it->second.textcommand;
512 needsTermination = !it->second.textnotermination();
522 char_type Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
523 bool & combining, bool & needsTermination, set<string> * req)
525 CharInfoMap::const_iterator const end = unicodesymbols.end();
526 CharInfoMap::const_iterator it = unicodesymbols.begin();
527 for (combining = false; it != end; ++it) {
528 docstring const math = it->second.mathcommand;
529 docstring const text = it->second.textcommand;
530 if ((cmdtype & MATH_CMD) && math == cmd) {
531 combining = it->second.combining();
532 needsTermination = !it->second.mathnotermination();
533 if (req && it->second.mathfeature() &&
534 !it->second.mathpreamble.empty())
535 req->insert(it->second.mathpreamble);
538 if ((cmdtype & TEXT_CMD) && text == cmd) {
539 combining = it->second.combining();
540 needsTermination = !it->second.textnotermination();
541 if (req && it->second.textfeature() &&
542 !it->second.textpreamble.empty())
543 req->insert(it->second.textpreamble);
547 needsTermination = false;
552 docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
553 bool & needsTermination, docstring & rem, set<string> * req)
555 needsTermination = false;
556 rem = empty_docstring();
557 bool const mathmode = cmdtype & MATH_CMD;
558 bool const textmode = cmdtype & TEXT_CMD;
561 size_t const cmdend = cmd.size();
562 CharInfoMap::const_iterator const uniend = unicodesymbols.end();
563 for (size_t j = 0; j < cmdend; ++j) {
564 // Also get the char after a backslash
565 if (j + 1 < cmdend && cmd[j] == '\\')
567 // If a macro argument follows, get it, too
568 if (j + 1 < cmdend && cmd[j + 1] == '{') {
571 while (k < cmdend && count && k != docstring::npos) {
572 k = cmd.find_first_of(from_ascii("{}"), k + 1);
578 if (k != docstring::npos)
581 // Start with this substring and try augmenting it when it is
582 // the prefix of some command in the unicodesymbols file
583 docstring const subcmd = cmd.substr(i, j - i + 1);
585 CharInfoMap::const_iterator it = unicodesymbols.begin();
586 size_t unicmd_size = 0;
588 for (; it != uniend; ++it) {
589 docstring const math = mathmode ? it->second.mathcommand
591 docstring const text = textmode ? it->second.textcommand
593 size_t cur_size = max(math.size(), text.size());
594 // The current math or text unicode command cannot
595 // match, or we already matched a longer one
596 if (cur_size < subcmd.size() || cur_size <= unicmd_size)
599 docstring tmp = subcmd;
601 while (prefixIs(math, tmp) || prefixIs(text, tmp)) {
603 if (k >= cmdend || cur_size <= tmp.size())
611 // The last added char caused a mismatch, because
612 // we didn't exhaust the chars in cmd and didn't
613 // exceed the maximum size of the current unicmd
614 if (k < cmdend && cur_size > tmp.size())
615 tmp.resize(tmp.size() - 1);
617 // If this is an exact match, we found a (longer)
618 // matching entry in the unicodesymbols file.
619 // If the entry doesn't start with '\', we take note
620 // of the match and continue (this is not a ultimate
621 // acceptance, as some other entry may match a longer
622 // portion of the cmd string). However, if the entry
623 // does start with '\', we accept the match only if
624 // this is a valid macro, i.e., either it is a single
625 // (nonletter) char macro, or nothing else follows,
626 // or what follows is a nonletter char, or the last
628 if ((math == tmp || text == tmp)
630 || (tmp.size() == 2 && !isAlphaASCII(tmp[1]))
632 || !isAlphaASCII(cmd[k])
633 || tmp[tmp.size() - 1] == '}')
638 unicmd_size = cur_size;
640 needsTermination = !it->second.mathnotermination();
642 needsTermination = !it->second.textnotermination();
644 if (math == tmp && it->second.mathfeature() &&
645 !it->second.mathpreamble.empty())
646 req->insert(it->second.mathpreamble);
647 if (text == tmp && it->second.textfeature() &&
648 !it->second.textpreamble.empty())
649 req->insert(it->second.textpreamble);
655 else if (j + 1 == cmdend) {
656 // No luck. Return what remains
658 if (needsTermination && !rem.empty()) {
659 if (rem.substr(0, 2) == "{}") {
661 needsTermination = false;
662 } else if (rem[0] == ' ') {
663 needsTermination = false;
664 // LaTeX would swallow all spaces
674 void Encodings::initUnicodeMath(Buffer const & buffer, bool for_master)
677 // The code below is not needed in tex2lyx and requires additional stuff
688 Inset & inset = buffer.inset();
689 InsetIterator it = inset_iterator_begin(inset);
690 InsetIterator const end = inset_iterator_end(inset);
691 for (; it != end; ++it)
692 it->initUnicodeMath();
698 ListOfBuffers blist = buffer.getDescendents();
699 ListOfBuffers::const_iterator bit = blist.begin();
700 ListOfBuffers::const_iterator const bend = blist.end();
701 for (; bit != bend; ++bit)
702 initUnicodeMath(**bit, false);
707 void Encodings::validate(char_type c, LaTeXFeatures & features, bool for_mathed)
710 // The code below is not needed in tex2lyx and requires additional stuff
715 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
716 if (it != unicodesymbols.end()) {
717 // In mathed, c could be used both in textmode and mathmode
718 bool const math_mode = for_mathed && isMathCmd(c);
719 bool const use_math = math_mode ||
720 (!for_mathed && it->second.textcommand.empty());
721 bool const use_text = (for_mathed && isTextCmd(c)) ||
722 (!for_mathed && !it->second.textcommand.empty());
723 bool const plain_utf8 = (features.runparams().encoding->name() == "utf8-plain");
724 bool const unicode_math = (features.isRequired("unicode-math")
725 && features.isAvailable("unicode-math"));
726 // with utf8-plain, we only load packages when in mathed (see #7766)
727 // and if we do not use unicode-math
728 if ((math_mode && !unicode_math)
729 || (use_math && !plain_utf8)) {
730 if (!it->second.mathpreamble.empty()) {
731 if (it->second.mathfeature()) {
732 string feats = it->second.mathpreamble;
733 while (!feats.empty()) {
735 feats = split(feats, feat, ',');
736 features.require(feat);
739 features.addPreambleSnippet(it->second.mathpreamble);
742 // with utf8-plain, we do not load packages (see #7766)
743 if (use_text && !plain_utf8) {
744 if (!it->second.textpreamble.empty()) {
745 if (it->second.textfeature()) {
746 string feats = it->second.textpreamble;
747 while (!feats.empty()) {
749 feats = split(feats, feat, ',');
750 features.require(feat);
753 features.addPreambleSnippet(it->second.textpreamble);
757 if (for_mathed && isMathSym(c)) {
758 features.require("amstext");
759 features.require("lyxmathsym");
765 bool Encodings::isHebrewComposeChar(char_type c)
767 return c <= 0x05c2 && c >= 0x05b0 && c != 0x05be && c != 0x05c0;
771 // Special Arabic letters are ones that do not get connected from left
772 // they are hamza, alef_madda, alef_hamza, waw_hamza, alef_hamza_under,
773 // alef, tah_marbota, dal, thal, rah, zai, wow, alef_maksoura
775 bool Encodings::isArabicSpecialChar(char_type c)
777 return (c >= 0x0621 && c <= 0x0625) || (c >= 0x0630 && c <= 0x0632)
778 || c == 0x0627 || c == 0x0629 || c == 0x062f || c == 0x0648
779 || c == 0x0649 || c == 0x0698;
783 bool Encodings::isArabicComposeChar(char_type c)
785 return c >= 0x064b && c <= 0x0652;
789 bool Encodings::isArabicChar(char_type c)
791 return c >= arabic_start && c <= arabic_end
792 && arabic_table[c-arabic_start][0];
796 char_type Encodings::transformChar(char_type c, Encodings::LetterForm form)
798 return isArabicChar(c) ? arabic_table[c-arabic_start][form] : c;
802 bool Encodings::isCombiningChar(char_type c)
804 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
805 if (it != unicodesymbols.end())
806 return it->second.combining();
811 string const Encodings::TIPAShortcut(char_type c)
813 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
814 if (it != unicodesymbols.end())
815 return it->second.tipashortcut;
820 bool Encodings::isKnownScriptChar(char_type const c, string & preamble)
822 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
824 if (it == unicodesymbols.end())
827 if (it->second.textpreamble != "textgreek" && it->second.textpreamble != "textcyr")
830 if (preamble.empty()) {
831 preamble = it->second.textpreamble;
834 return it->second.textpreamble == preamble;
838 bool Encodings::isMathAlpha(char_type c)
840 return mathalpha.count(c);
845 Encodings::fromLyXName(string const & name, bool allowUnsafe) const
847 EncodingList::const_iterator const it = encodinglist.find(name);
848 if (!allowUnsafe && it->second.unsafe())
850 return it != encodinglist.end() ? &it->second : 0;
855 Encodings::fromLaTeXName(string const & n, int const & p, bool allowUnsafe) const
858 // FIXME: if we have to test for too many of these synonyms,
859 // we should instead extend the format of lib/encodings
863 // We don't use find_if because it makes copies of the pairs in
865 // This linear search is OK since we don't have many encodings.
866 // Users could even optimize it by putting the encodings they use
867 // most at the top of lib/encodings.
868 EncodingList::const_iterator const end = encodinglist.end();
869 for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
870 if ((it->second.latexName() == name) && (it->second.package() & p)
871 && (!it->second.unsafe() || allowUnsafe))
878 Encodings::fromIconvName(string const & n, int const & p, bool allowUnsafe) const
880 EncodingList::const_iterator const end = encodinglist.end();
881 for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
882 if ((it->second.iconvName() == n) && (it->second.package() & p)
883 && (!it->second.unsafe() || allowUnsafe))
889 Encodings::Encodings()
893 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
895 // We must read the symbolsfile first, because the Encoding
896 // constructor depends on it.
898 symbolslex.setFile(symbolsfile);
899 bool getNextToken = true;
900 while (symbolslex.isOK()) {
906 if (!symbolslex.next(true))
911 istringstream is(symbolslex.getString());
912 // reading symbol directly does not work if
913 // char_type == wchar_t.
915 if(!(is >> hex >> tmp))
919 if (!symbolslex.next(true))
921 info.textcommand = symbolslex.getDocString();
922 if (!symbolslex.next(true))
924 info.textpreamble = symbolslex.getString();
925 if (!symbolslex.next(true))
927 flags = symbolslex.getString();
930 if (suffixIs(info.textcommand, '}'))
931 info.flags |= CharInfoTextNoTermination;
932 if (suffixIs(info.mathcommand, '}'))
933 info.flags |= CharInfoMathNoTermination;
934 while (!flags.empty()) {
936 flags = split(flags, flag, ',');
937 if (flag == "combining") {
938 info.flags |= CharInfoCombining;
939 } else if (flag == "force") {
940 info.flags |= CharInfoForce;
941 forced.insert(symbol);
942 } else if (prefixIs(flag, "force=")) {
943 vector<string> encodings =
944 getVectorFromString(flag.substr(6));
945 for (size_t i = 0; i < encodings.size(); ++i)
946 forcedselected[encodings[i]].insert(symbol);
947 info.flags |= CharInfoForceSelected;
948 } else if (flag == "mathalpha") {
949 mathalpha.insert(symbol);
950 } else if (flag == "notermination=text") {
951 info.flags |= CharInfoTextNoTermination;
952 } else if (flag == "notermination=math") {
953 info.flags |= CharInfoMathNoTermination;
954 } else if (flag == "notermination=both") {
955 info.flags |= CharInfoTextNoTermination;
956 info.flags |= CharInfoMathNoTermination;
957 } else if (flag == "notermination=none") {
958 info.flags &= ~CharInfoTextNoTermination;
959 info.flags &= ~CharInfoMathNoTermination;
960 } else if (contains(flag, "tipashortcut=")) {
961 info.tipashortcut = split(flag, '=');
963 lyxerr << "Ignoring unknown flag `" << flag
964 << "' for symbol `0x"
965 << hex << symbol << dec
969 // mathcommand and mathpreamble have been added for 1.6.0.
970 // make them optional so that old files still work.
971 int const lineno = symbolslex.lineNumber();
972 bool breakout = false;
973 if (symbolslex.next(true)) {
974 if (symbolslex.lineNumber() != lineno) {
975 // line in old format without mathcommand and mathpreamble
976 getNextToken = false;
978 info.mathcommand = symbolslex.getDocString();
979 if (symbolslex.next(true)) {
980 if (symbolslex.lineNumber() != lineno) {
981 // line in new format with mathcommand only
982 getNextToken = false;
984 // line in new format with mathcommand and mathpreamble
985 info.mathpreamble = symbolslex.getString();
994 // backward compatibility
995 if (info.mathpreamble == "esintoramsmath")
996 info.mathpreamble = "esint|amsmath";
998 if (!info.textpreamble.empty())
999 if (info.textpreamble[0] != '\\')
1000 info.flags |= CharInfoTextFeature;
1001 if (!info.mathpreamble.empty())
1002 if (info.mathpreamble[0] != '\\')
1003 info.flags |= CharInfoMathFeature;
1005 LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '"
1006 << to_utf8(info.textcommand) << "' '" << info.textpreamble
1007 << " '" << info.textfeature() << ' ' << info.textnotermination()
1008 << ' ' << to_utf8(info.mathcommand) << "' '" << info.mathpreamble
1009 << "' " << info.mathfeature() << ' ' << info.mathnotermination()
1010 << ' ' << info.combining() << ' ' << info.force()
1011 << ' ' << info.forceselected());
1013 // we assume that at least one command is nonempty when using unicodesymbols
1014 if (!info.textcommand.empty() || !info.mathcommand.empty())
1015 unicodesymbols[symbol] = info;
1021 // Now read the encodings
1027 LexerKeyword encodingtags[] = {
1028 { "encoding", et_encoding },
1032 Lexer lex(encodingtags);
1033 lex.setFile(encfile);
1034 lex.setContext("Encodings::read");
1035 while (lex.isOK()) {
1036 switch (lex.lex()) {
1040 string const name = lex.getString();
1042 string const latexname = lex.getString();
1044 string const guiname = lex.getString();
1046 string const iconvname = lex.getString();
1048 string const width = lex.getString();
1049 bool fixedwidth = false;
1050 bool unsafe = false;
1051 if (width == "fixed")
1053 else if (width == "variable")
1055 else if (width == "variableunsafe") {
1060 lex.printError("Unknown width");
1063 string const p = lex.getString();
1064 Encoding::Package package = Encoding::none;
1066 package = Encoding::none;
1067 else if (p == "inputenc")
1068 package = Encoding::inputenc;
1069 else if (p == "CJK")
1070 package = Encoding::CJK;
1071 else if (p == "japanese")
1072 package = Encoding::japanese;
1074 lex.printError("Unknown package");
1076 LYXERR(Debug::INFO, "Reading encoding " << name);
1077 encodinglist[name] = Encoding(name, latexname,
1078 guiname, iconvname, fixedwidth, unsafe,
1081 if (lex.lex() != et_end)
1082 lex.printError("Missing end");
1086 lex.printError("Misplaced end");
1088 case Lexer::LEX_FEOF:
1091 lex.printError("Unknown tag");