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/FileName.h"
26 #include "support/lstrings.h"
27 #include "support/textutils.h"
28 #include "support/unicode.h"
30 #include <boost/cstdint.hpp>
35 using namespace lyx::support;
41 Encodings::MathCommandSet Encodings::mathcmd;
42 Encodings::TextCommandSet Encodings::textcmd;
43 Encodings::MathSymbolSet Encodings::mathsym;
47 char_type arabic_table[172][4] = {
48 {0xfe80, 0xfe80, 0xfe80, 0xfe80}, // 0x0621 = hamza
49 {0xfe81, 0xfe82, 0xfe81, 0xfe82}, // 0x0622 = ligature madda on alef
50 {0xfe83, 0xfe84, 0xfe83, 0xfe84}, // 0x0623 = ligature hamza on alef
51 {0xfe85, 0xfe86, 0xfe85, 0xfe86}, // 0x0624 = ligature hamza on waw
52 {0xfe87, 0xfe88, 0xfe87, 0xfe88}, // 0x0625 = ligature hamza under alef
53 {0xfe89, 0xfe8a, 0xfe8b, 0xfe8c}, // 0x0626 = ligature hamza on ya
54 {0xfe8d, 0xfe8e, 0xfe8d, 0xfe8e}, // 0x0627 = alef
55 {0xfe8f, 0xfe90, 0xfe91, 0xfe92}, // 0x0628 = baa
56 {0xfe93, 0xfe94, 0xfe93, 0xfe94}, // 0x0629 = taa marbuta
57 {0xfe95, 0xfe96, 0xfe97, 0xfe98}, // 0x062a = taa
58 {0xfe99, 0xfe9a, 0xfe9b, 0xfe9c}, // 0x062b = thaa
59 {0xfe9d, 0xfe9e, 0xfe9f, 0xfea0}, // 0x062c = jeem
60 {0xfea1, 0xfea2, 0xfea3, 0xfea4}, // 0x062d = haa
61 {0xfea5, 0xfea6, 0xfea7, 0xfea8}, // 0x062e = khaa
62 {0xfea9, 0xfeaa, 0xfea9, 0xfeaa}, // 0x062f = dal
64 {0xfeab, 0xfeac, 0xfeab, 0xfeac}, // 0x0630 = thal
65 {0xfead, 0xfeae, 0xfead, 0xfeae}, // 0x0631 = ra
66 {0xfeaf, 0xfeb0, 0xfeaf, 0xfeb0}, // 0x0632 = zain
67 {0xfeb1, 0xfeb2, 0xfeb3, 0xfeb4}, // 0x0633 = seen
68 {0xfeb5, 0xfeb6, 0xfeb7, 0xfeb8}, // 0x0634 = sheen
69 {0xfeb9, 0xfeba, 0xfebb, 0xfebc}, // 0x0635 = sad
70 {0xfebd, 0xfebe, 0xfebf, 0xfec0}, // 0x0636 = dad
71 {0xfec1, 0xfec2, 0xfec3, 0xfec4}, // 0x0637 = tah
72 {0xfec5, 0xfec6, 0xfec7, 0xfec8}, // 0x0638 = zah
73 {0xfec9, 0xfeca, 0xfecb, 0xfecc}, // 0x0639 = ain
74 {0xfecd, 0xfece, 0xfecf, 0xfed0}, // 0x063a = ghain
75 {0, 0, 0, 0}, // 0x063b
76 {0, 0, 0, 0}, // 0x063c
77 {0, 0, 0, 0}, // 0x063d
78 {0, 0, 0, 0}, // 0x063e
79 {0, 0, 0, 0}, // 0x063f
81 {0, 0, 0, 0}, // 0x0640
82 {0xfed1, 0xfed2, 0xfed3, 0xfed4}, // 0x0641 = fa
83 {0xfed5, 0xfed6, 0xfed7, 0xfed8}, // 0x0642 = qaf
84 {0xfed9, 0xfeda, 0xfedb, 0xfedc}, // 0x0643 = kaf
85 {0xfedd, 0xfede, 0xfedf, 0xfee0}, // 0x0644 = lam
86 {0xfee1, 0xfee2, 0xfee3, 0xfee4}, // 0x0645 = meem
87 {0xfee5, 0xfee6, 0xfee7, 0xfee8}, // 0x0646 = noon
88 {0xfee9, 0xfeea, 0xfeeb, 0xfeec}, // 0x0647 = ha
89 {0xfeed, 0xfeee, 0xfeed, 0xfeee}, // 0x0648 = waw
90 {0xfeef, 0xfef0, 0xfeef, 0xfef0}, // 0x0649 = alef maksura
91 {0xfef1, 0xfef2, 0xfef3, 0xfef4}, // 0x064a = ya
92 {0x065b, 0x065b, 0x065b, 0x065b}, // 0x064b = fathatan
93 {0x065c, 0x065c, 0x065c, 0x065c}, // 0x064c = dammatan
94 {0x064d, 0x064d, 0x064d, 0x064d}, // 0x064d = kasratan
95 {0x064e, 0x064e, 0x064e, 0x064e}, // 0x064e = fatha
96 {0x064f, 0x064f, 0x064f, 0x064f}, // 0x064f = damma
98 {0x0650, 0x0650, 0x0650, 0x0650}, // 0x0650 = kasra
99 {0x0651, 0x0651, 0x0651, 0x0651}, // 0x0651 = shadda
100 {0x0652, 0x0652, 0x0652, 0x0652}, // 0x0652 = sukun
102 {0, 0, 0, 0}, // 0x0653
103 {0, 0, 0, 0}, // 0x0654
104 {0, 0, 0, 0}, // 0x0655
105 {0, 0, 0, 0}, // 0x0656
106 {0, 0, 0, 0}, // 0x0657
107 {0, 0, 0, 0}, // 0x0658
108 {0, 0, 0, 0}, // 0x0659
109 {0, 0, 0, 0}, // 0x065a
110 {0, 0, 0, 0}, // 0x065b
111 {0, 0, 0, 0}, // 0x065c
112 {0, 0, 0, 0}, // 0x065d
113 {0, 0, 0, 0}, // 0x065e
114 {0, 0, 0, 0}, // 0x065f
115 {0, 0, 0, 0}, // 0x0660
116 {0, 0, 0, 0}, // 0x0661
117 {0, 0, 0, 0}, // 0x0662
118 {0, 0, 0, 0}, // 0x0663
119 {0, 0, 0, 0}, // 0x0664
120 {0, 0, 0, 0}, // 0x0665
121 {0, 0, 0, 0}, // 0x0666
122 {0, 0, 0, 0}, // 0x0667
123 {0, 0, 0, 0}, // 0x0668
124 {0, 0, 0, 0}, // 0x0669
125 {0, 0, 0, 0}, // 0x066a
126 {0, 0, 0, 0}, // 0x066b
127 {0, 0, 0, 0}, // 0x066c
128 {0, 0, 0, 0}, // 0x066d
129 {0, 0, 0, 0}, // 0x066e
130 {0, 0, 0, 0}, // 0x066f
131 {0, 0, 0, 0}, // 0x0670
132 {0, 0, 0, 0}, // 0x0671
133 {0, 0, 0, 0}, // 0x0672
134 {0, 0, 0, 0}, // 0x0673
135 {0, 0, 0, 0}, // 0x0674
136 {0, 0, 0, 0}, // 0x0675
137 {0, 0, 0, 0}, // 0x0676
138 {0, 0, 0, 0}, // 0x0677
139 {0, 0, 0, 0}, // 0x0678
140 {0, 0, 0, 0}, // 0x0679
141 {0, 0, 0, 0}, // 0x067a
142 {0, 0, 0, 0}, // 0x067b
143 {0, 0, 0, 0}, // 0x067c
144 {0, 0, 0, 0}, // 0x067d
145 {0xfb56, 0xfb57, 0xfb58, 0xfb59}, // 0x067e = peh
146 {0, 0, 0, 0}, // 0x067f
147 {0, 0, 0, 0}, // 0x0680
148 {0, 0, 0, 0}, // 0x0681
149 {0, 0, 0, 0}, // 0x0682
150 {0, 0, 0, 0}, // 0x0683
151 {0, 0, 0, 0}, // 0x0684
152 {0, 0, 0, 0}, // 0x0685
153 {0xfb7a, 0xfb7b, 0xfb7c, 0xfb7d}, // 0x0686 = tcheh
154 {0, 0, 0, 0}, // 0x0687
155 {0, 0, 0, 0}, // 0x0688
156 {0, 0, 0, 0}, // 0x0689
157 {0, 0, 0, 0}, // 0x068a
158 {0, 0, 0, 0}, // 0x068b
159 {0, 0, 0, 0}, // 0x068c
160 {0, 0, 0, 0}, // 0x068d
161 {0, 0, 0, 0}, // 0x068e
162 {0, 0, 0, 0}, // 0x068f
163 {0, 0, 0, 0}, // 0x0690
164 {0, 0, 0, 0}, // 0x0691
165 {0, 0, 0, 0}, // 0x0692
166 {0, 0, 0, 0}, // 0x0693
167 {0, 0, 0, 0}, // 0x0694
168 {0, 0, 0, 0}, // 0x0695
169 {0, 0, 0, 0}, // 0x0696
170 {0, 0, 0, 0}, // 0x0697
171 {0xfb8a, 0xfb8b, 0xfb8a, 0xfb8b}, // 0x0698 = jeh
172 {0, 0, 0, 0}, // 0x0699
173 {0, 0, 0, 0}, // 0x069a
174 {0, 0, 0, 0}, // 0x069b
175 {0, 0, 0, 0}, // 0x069c
176 {0, 0, 0, 0}, // 0x069d
177 {0, 0, 0, 0}, // 0x069e
178 {0, 0, 0, 0}, // 0x069f
179 {0, 0, 0, 0}, // 0x06a0
180 {0, 0, 0, 0}, // 0x06a1
181 {0, 0, 0, 0}, // 0x06a2
182 {0, 0, 0, 0}, // 0x06a3
183 {0, 0, 0, 0}, // 0x06a4
184 {0, 0, 0, 0}, // 0x06a5
185 {0, 0, 0, 0}, // 0x06a6
186 {0, 0, 0, 0}, // 0x06a7
187 {0, 0, 0, 0}, // 0x06a8
188 {0xfb8e, 0xfb8f, 0xfb90, 0xfb91}, // 0x06a9 = farsi kaf
189 {0, 0, 0, 0}, // 0x06aa
190 {0, 0, 0, 0}, // 0x06ab
191 {0, 0, 0, 0}, // 0x06ac
192 {0, 0, 0, 0}, // 0x06ad
193 {0, 0, 0, 0}, // 0x06ae
194 {0xfb92, 0xfb93, 0xfb94, 0xfb95}, // 0x06af = gaf
195 {0, 0, 0, 0}, // 0x06b0
196 {0, 0, 0, 0}, // 0x06b1
197 {0, 0, 0, 0}, // 0x06b2
198 {0, 0, 0, 0}, // 0x06b3
199 {0, 0, 0, 0}, // 0x06b4
200 {0, 0, 0, 0}, // 0x06b5
201 {0, 0, 0, 0}, // 0x06b6
202 {0, 0, 0, 0}, // 0x06b7
203 {0, 0, 0, 0}, // 0x06b8
204 {0, 0, 0, 0}, // 0x06b9
205 {0, 0, 0, 0}, // 0x06ba
206 {0, 0, 0, 0}, // 0x06bb
207 {0, 0, 0, 0}, // 0x06bc
208 {0, 0, 0, 0}, // 0x06bd
209 {0, 0, 0, 0}, // 0x06be
210 {0, 0, 0, 0}, // 0x06bf
211 {0, 0, 0, 0}, // 0x06c0
212 {0, 0, 0, 0}, // 0x06c1
213 {0, 0, 0, 0}, // 0x06c2
214 {0, 0, 0, 0}, // 0x06c3
215 {0, 0, 0, 0}, // 0x06c4
216 {0, 0, 0, 0}, // 0x06c5
217 {0, 0, 0, 0}, // 0x06c6
218 {0, 0, 0, 0}, // 0x06c7
219 {0, 0, 0, 0}, // 0x06c8
220 {0, 0, 0, 0}, // 0x06c9
221 {0, 0, 0, 0}, // 0x06ca
222 {0, 0, 0, 0}, // 0x06cb
223 {0xfbfc, 0xfbfd, 0xfbfe, 0xfbff} // 0x06cc = farsi yeh
227 char_type const arabic_start = 0x0621;
228 char_type const arabic_end = 0x06cc;
231 /// Information about a single UCS4 character
233 /// LaTeX command (text mode) for this character
234 docstring textcommand;
235 /// LaTeX command (math mode) for this character
236 docstring mathcommand;
237 /// Needed LaTeX preamble (or feature) for text mode
239 /// Needed LaTeX preamble (or feature) for math mode
241 /// Is this a combining character?
243 /// Is \c textpreamble a feature known by LaTeXFeatures, or a raw LaTeX
246 /// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX
249 /// Always force the LaTeX command, even if the encoding contains
254 /// This macro needs no termination (such as {} or space).
259 typedef map<char_type, CharInfo> CharInfoMap;
260 CharInfoMap unicodesymbols;
262 typedef std::set<char_type> CharSet;
265 typedef std::set<char_type> MathAlphaSet;
266 MathAlphaSet mathalpha;
269 /// The highest code point in UCS4 encoding (1<<20 + 1<<16)
270 char_type const max_ucs4 = 0x110000;
275 EncodingException::EncodingException(char_type c)
276 : failed_char(c), par_id(0), pos(0)
281 const char * EncodingException::what() const throw()
283 return "Could not find LaTeX command for a character";
287 Encoding::Encoding(string const & n, string const & l, string const & g,
288 string const & i, bool f, Encoding::Package p)
289 : name_(n), latexName_(l), guiName_(g), iconvName_(i), fixedwidth_(f), package_(p)
292 // ASCII can encode 128 code points and nothing else
293 start_encodable_ = 128;
295 } else if (i == "UTF-8") {
296 // UTF8 can encode all UCS4 code points
297 start_encodable_ = max_ucs4;
305 void Encoding::init() const
310 start_encodable_ = 0;
311 // temporarily switch off lyxerr, since we will generate iconv errors
314 // We do not need to check all UCS4 code points, it is enough
315 // if we check all 256 code points of this encoding.
316 for (unsigned short j = 0; j < 256; ++j) {
317 char const c = char(j);
318 vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, iconvName_);
319 if (ucs4.size() != 1)
321 char_type const uc = ucs4[0];
322 CharInfoMap::const_iterator const it = unicodesymbols.find(uc);
323 if (it == unicodesymbols.end() || !it->second.force)
324 encodable_.insert(uc);
327 // We do not know how many code points this encoding has, and
328 // they do not have a direct representation as a single byte,
329 // therefore we need to check all UCS4 code points.
330 // This is expensive!
331 for (char_type c = 0; c < max_ucs4; ++c) {
332 vector<char> const eightbit = ucs4_to_eightbit(&c, 1, iconvName_);
333 if (!eightbit.empty()) {
334 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
335 if (it == unicodesymbols.end() || !it->second.force)
336 encodable_.insert(c);
341 CharSet::iterator it = encodable_.find(start_encodable_);
342 while (it != encodable_.end()) {
343 encodable_.erase(it);
345 it = encodable_.find(start_encodable_);
351 docstring Encoding::latexChar(char_type c, bool no_commands) const
353 // assure the used encoding is properly initialized
356 if (iconvName_ == "UTF-8" && package_ == none)
357 return docstring(1, c);
358 if (c < start_encodable_ && !encodings.isForced(c))
359 return docstring(1, c);
360 if (encodable_.find(c) != encodable_.end())
361 return docstring(1, c);
365 // c cannot (or should not) be encoded in this encoding
366 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
367 if (it == unicodesymbols.end())
368 throw EncodingException(c);
369 // at least one of mathcommand and textcommand is nonempty
370 if (it->second.textcommand.empty())
371 return "\\ensuremath{" + it->second.mathcommand + '}';
372 return it->second.textcommand;
376 vector<char_type> Encoding::symbolsList() const
378 // assure the used encoding is properly initialized
381 // first all encodable characters
382 vector<char_type> symbols(encodable_.begin(), encodable_.end());
383 // add those below start_encodable_
384 for (char_type c = 0; c < start_encodable_; ++c)
385 symbols.push_back(c);
386 // now the ones from the unicodesymbols file
387 CharInfoMap::const_iterator const end = unicodesymbols.end();
388 CharInfoMap::const_iterator it = unicodesymbols.begin();
389 for (; it != end; ++it)
390 symbols.push_back(it->first);
395 bool Encodings::latexMathChar(char_type c, bool mathmode,
396 Encoding const * encoding, docstring & command)
399 command = encoding->latexChar(c, true);
401 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
402 if (it == unicodesymbols.end()) {
403 if (!encoding || command.empty())
404 throw EncodingException(c);
409 // at least one of mathcommand and textcommand is nonempty
410 bool use_math = (mathmode && !it->second.mathcommand.empty()) ||
411 (!mathmode && it->second.textcommand.empty());
413 command = it->second.mathcommand;
416 if (!encoding || command.empty()) {
417 command = it->second.textcommand;
427 char_type Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
428 bool & combining, set<string> * req)
430 CharInfoMap::const_iterator const end = unicodesymbols.end();
431 CharInfoMap::const_iterator it = unicodesymbols.begin();
432 for (combining = false; it != end; ++it) {
433 docstring const math = it->second.mathcommand;
434 docstring const text = it->second.textcommand;
435 if ((cmdtype && MATH_CMD) && math == cmd) {
436 combining = it->second.combining;
437 if (req && it->second.mathfeature &&
438 !it->second.mathpreamble.empty())
439 req->insert(it->second.mathpreamble);
442 if ((cmdtype & TEXT_CMD) && text == cmd) {
443 combining = it->second.combining;
444 if (req && it->second.textfeature &&
445 !it->second.textpreamble.empty())
446 req->insert(it->second.textpreamble);
454 docstring Encodings::fromLaTeXCommand(docstring const & cmd, int cmdtype,
455 docstring & rem, set<string> * req)
457 bool const mathmode = cmdtype & MATH_CMD;
458 bool const textmode = cmdtype & TEXT_CMD;
461 size_t const cmdend = cmd.size();
462 CharInfoMap::const_iterator const uniend = unicodesymbols.end();
463 for (size_t j = 0; j < cmdend; ++j) {
464 // Also get the char after a backslash
465 if (j + 1 < cmdend && cmd[j] == '\\')
467 // If a macro argument follows, get it, too
468 if (j + 1 < cmdend && cmd[j + 1] == '{') {
471 while (k < cmdend && count && k != docstring::npos) {
472 k = cmd.find_first_of(from_ascii("{}"), k + 1);
478 if (k != docstring::npos)
481 // Start with this substring and try augmenting it when it is
482 // the prefix of some command in the unicodesymbols file
483 docstring const subcmd = cmd.substr(i, j - i + 1);
485 CharInfoMap::const_iterator it = unicodesymbols.begin();
486 size_t unicmd_size = 0;
488 for (; it != uniend; ++it) {
489 docstring const math = mathmode ? it->second.mathcommand
491 docstring const text = textmode ? it->second.textcommand
493 size_t cur_size = max(math.size(), text.size());
494 // The current math or text unicode command cannot
495 // match, or we already matched a longer one
496 if (cur_size < subcmd.size() || cur_size <= unicmd_size)
499 docstring tmp = subcmd;
501 while (prefixIs(math, tmp) || prefixIs(text, tmp)) {
503 if (k >= cmdend || cur_size <= tmp.size())
511 // The last added char caused a mismatch, because
512 // we didn't exhaust the chars in cmd and didn't
513 // exceed the maximum size of the current unicmd
514 if (k < cmdend && cur_size > tmp.size())
515 tmp.resize(tmp.size() - 1);
517 // If this is an exact match, we found a (longer)
518 // matching entry in the unicodesymbols file.
519 // If the entry doesn't start with '\', we take note
520 // of the match and continue (this is not a ultimate
521 // acceptance, as some other entry may match a longer
522 // portion of the cmd string). However, if the entry
523 // does start with '\', we accept the match only if
524 // this is a valid macro, i.e., either it is a single
525 // (nonletter) char macro, or nothing else follows,
526 // or what follows is a nonletter char, or the last
528 if ((math == tmp || text == tmp)
530 || (tmp.size() == 2 && !isAlphaASCII(tmp[1]))
532 || !isAlphaASCII(cmd[k])
533 || tmp[tmp.size() - 1] == '}')
538 unicmd_size = cur_size;
540 if (math == tmp && it->second.mathfeature &&
541 !it->second.mathpreamble.empty())
542 req->insert(it->second.mathpreamble);
543 if (text == tmp && it->second.textfeature &&
544 !it->second.textpreamble.empty())
545 req->insert(it->second.textpreamble);
551 else if (j + 1 == cmdend)
552 // No luck. Return what remains
559 void Encodings::initUnicodeMath(Buffer const & buffer, bool for_master)
562 // The code below is not needed in tex2lyx and requires additional stuff
573 Inset & inset = buffer.inset();
574 InsetIterator it = inset_iterator_begin(inset);
575 InsetIterator const end = inset_iterator_end(inset);
576 for (; it != end; ++it)
577 it->initUnicodeMath();
583 ListOfBuffers blist = buffer.getDescendents();
584 ListOfBuffers::const_iterator bit = blist.begin();
585 ListOfBuffers::const_iterator const bend = blist.end();
586 for (; bit != bend; ++bit)
587 initUnicodeMath(**bit, false);
592 void Encodings::validate(char_type c, LaTeXFeatures & features, bool for_mathed)
595 // The code below is not needed in tex2lyx and requires additional stuff
600 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
601 if (it != unicodesymbols.end()) {
602 // In mathed, c could be used both in textmode and mathmode
603 bool const math_mode = for_mathed && isMathCmd(c);
604 bool const use_math = math_mode ||
605 (!for_mathed && it->second.textcommand.empty());
606 bool const use_text = (for_mathed && isTextCmd(c)) ||
607 (!for_mathed && !it->second.textcommand.empty());
608 bool const plain_utf8 = (features.runparams().encoding->name() == "utf8-plain");
609 // with utf8-plain, we only load packages when in mathed (see #7766)
610 if (math_mode || (use_math && !plain_utf8)) {
611 if (!it->second.mathpreamble.empty()) {
612 if (it->second.mathfeature) {
613 string feats = it->second.mathpreamble;
614 while (!feats.empty()) {
616 feats = split(feats, feat, ',');
617 features.require(feat);
620 features.addPreambleSnippet(it->second.mathpreamble);
623 // with utf8-plain, we do not load packages (see #7766)
624 if (use_text && !plain_utf8) {
625 if (!it->second.textpreamble.empty()) {
626 if (it->second.textfeature) {
627 string feats = it->second.textpreamble;
628 while (!feats.empty()) {
630 feats = split(feats, feat, ',');
631 features.require(feat);
634 features.addPreambleSnippet(it->second.textpreamble);
638 if (for_mathed && isMathSym(c)) {
639 features.require("amstext");
640 features.require("lyxmathsym");
646 bool Encodings::isHebrewComposeChar(char_type c)
648 return c <= 0x05c2 && c >= 0x05b0 && c != 0x05be && c != 0x05c0;
652 // Special Arabic letters are ones that do not get connected from left
653 // they are hamza, alef_madda, alef_hamza, waw_hamza, alef_hamza_under,
654 // alef, tah_marbota, dal, thal, rah, zai, wow, alef_maksoura
656 bool Encodings::isArabicSpecialChar(char_type c)
658 return (c >= 0x0621 && c <= 0x0625) || (c >= 0x0630 && c <= 0x0632)
659 || c == 0x0627 || c == 0x0629 || c == 0x062f || c == 0x0648
660 || c == 0x0649 || c == 0x0698;
664 bool Encodings::isArabicComposeChar(char_type c)
666 return c >= 0x064b && c <= 0x0652;
670 bool Encodings::isArabicChar(char_type c)
672 return c >= arabic_start && c <= arabic_end
673 && arabic_table[c-arabic_start][0];
677 char_type Encodings::transformChar(char_type c, Encodings::LetterForm form)
679 return isArabicChar(c) ? arabic_table[c-arabic_start][form] : c;
683 bool Encodings::isCombiningChar(char_type c)
685 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
686 if (it != unicodesymbols.end())
687 return it->second.combining;
692 string const Encodings::TIPAShortcut(char_type c)
694 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
695 if (it != unicodesymbols.end())
696 return it->second.tipashortcut;
701 bool Encodings::isKnownScriptChar(char_type const c, string & preamble)
703 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
705 if (it == unicodesymbols.end())
708 if (it->second.textpreamble != "textgreek" && it->second.textpreamble != "textcyr")
711 if (preamble.empty()) {
712 preamble = it->second.textpreamble;
715 return it->second.textpreamble == preamble;
719 bool Encodings::isForced(char_type c)
721 return (!forced.empty() && forced.find(c) != forced.end());
725 bool Encodings::isMathAlpha(char_type c)
727 return mathalpha.count(c);
731 bool Encodings::needsTermination(char_type c)
733 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
734 if (it != unicodesymbols.end())
735 return !it->second.notermination;
740 Encoding const * Encodings::fromLyXName(string const & name) const
742 EncodingList::const_iterator const it = encodinglist.find(name);
743 return it != encodinglist.end() ? &it->second : 0;
747 Encoding const * Encodings::fromLaTeXName(string const & n) const
750 // FIXME: if we have to test for too many of these synonyms,
751 // we should instead extend the format of lib/encodings
755 // We don't use find_if because it makes copies of the pairs in
757 // This linear search is OK since we don't have many encodings.
758 // Users could even optimize it by putting the encodings they use
759 // most at the top of lib/encodings.
760 EncodingList::const_iterator const end = encodinglist.end();
761 for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
762 if (it->second.latexName() == name)
768 Encodings::Encodings()
773 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
775 // We must read the symbolsfile first, because the Encoding
776 // constructor depends on it.
778 symbolslex.setFile(symbolsfile);
779 bool getNextToken = true;
780 while (symbolslex.isOK()) {
786 if (!symbolslex.next(true))
791 istringstream is(symbolslex.getString());
792 // reading symbol directly does not work if
793 // char_type == wchar_t.
795 if(!(is >> hex >> tmp))
799 if (!symbolslex.next(true))
801 info.textcommand = symbolslex.getDocString();
802 if (!symbolslex.next(true))
804 info.textpreamble = symbolslex.getString();
805 if (!symbolslex.next(true))
807 flags = symbolslex.getString();
809 info.combining = false;
810 info.textfeature = false;
812 info.notermination = false;
813 while (!flags.empty()) {
815 flags = split(flags, flag, ',');
816 if (flag == "combining") {
817 info.combining = true;
818 } else if (flag == "force") {
820 forced.insert(symbol);
821 } else if (flag == "mathalpha") {
822 mathalpha.insert(symbol);
823 } else if (flag == "notermination") {
824 info.notermination = true;
825 } else if (contains(flag, "tipashortcut=")) {
826 info.tipashortcut = split(flag, '=');
828 lyxerr << "Ignoring unknown flag `" << flag
829 << "' for symbol `0x"
830 << hex << symbol << dec
834 // mathcommand and mathpreamble have been added for 1.6.0.
835 // make them optional so that old files still work.
836 int const lineno = symbolslex.lineNumber();
837 bool breakout = false;
838 if (symbolslex.next(true)) {
839 if (symbolslex.lineNumber() != lineno) {
840 // line in old format without mathcommand and mathpreamble
841 getNextToken = false;
843 info.mathcommand = symbolslex.getDocString();
844 if (symbolslex.next(true)) {
845 if (symbolslex.lineNumber() != lineno) {
846 // line in new format with mathcommand only
847 getNextToken = false;
849 // line in new format with mathcommand and mathpreamble
850 info.mathpreamble = symbolslex.getString();
859 // backward compatibility
860 if (info.mathpreamble == "esintoramsmath")
861 info.mathpreamble = "esint|amsmath";
863 if (!info.textpreamble.empty())
864 info.textfeature = info.textpreamble[0] != '\\';
865 if (!info.mathpreamble.empty())
866 info.mathfeature = info.mathpreamble[0] != '\\';
868 LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '"
869 << to_utf8(info.textcommand) << "' '" << info.textpreamble
870 << "' " << info.combining << ' ' << info.textfeature
871 << " '" << to_utf8(info.mathcommand) << "' '"
872 << info.mathpreamble << "' " << info.mathfeature);
874 // we assume that at least one command is nonempty when using unicodesymbols
875 if (!info.textcommand.empty() || !info.mathcommand.empty())
876 unicodesymbols[symbol] = info;
882 // Now read the encodings
888 LexerKeyword encodingtags[] = {
889 { "encoding", et_encoding },
893 Lexer lex(encodingtags);
894 lex.setFile(encfile);
895 lex.setContext("Encodings::read");
901 string const name = lex.getString();
903 string const latexname = lex.getString();
905 string const guiname = lex.getString();
907 string const iconvname = lex.getString();
909 string const width = lex.getString();
910 bool fixedwidth = false;
911 if (width == "fixed")
913 else if (width == "variable")
916 lex.printError("Unknown width");
919 string const p = lex.getString();
920 Encoding::Package package = Encoding::none;
922 package = Encoding::none;
923 else if (p == "inputenc")
924 package = Encoding::inputenc;
926 package = Encoding::CJK;
927 else if (p == "japanese")
928 package = Encoding::japanese;
930 lex.printError("Unknown package");
932 LYXERR(Debug::INFO, "Reading encoding " << name);
933 encodinglist[name] = Encoding(name, latexname,
934 guiname, iconvname, fixedwidth, package);
936 if (lex.lex() != et_end)
937 lex.printError("Missing end");
941 lex.printError("Misplaced end");
943 case Lexer::LEX_FEOF:
946 lex.printError("Unknown tag");