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.
17 #include "LaTeXFeatures.h"
21 #include "support/debug.h"
22 #include "support/FileName.h"
23 #include "support/lstrings.h"
24 #include "support/unicode.h"
26 #include <boost/cstdint.hpp>
30 #ifndef CXX_GLOBAL_CSTD
39 using support::FileName;
45 char_type arabic_table[172][4] = {
46 {0xfe80, 0xfe80, 0xfe80, 0xfe80}, // 0x0621 = hamza
47 {0xfe81, 0xfe82, 0xfe81, 0xfe82}, // 0x0622 = ligature madda on alef
48 {0xfe83, 0xfe84, 0xfe83, 0xfe84}, // 0x0623 = ligature hamza on alef
49 {0xfe85, 0xfe86, 0xfe85, 0xfe86}, // 0x0624 = ligature hamza on waw
50 {0xfe87, 0xfe88, 0xfe87, 0xfe88}, // 0x0625 = ligature hamza under alef
51 {0xfe89, 0xfe8a, 0xfe8b, 0xfe8c}, // 0x0626 = ligature hamza on ya
52 {0xfe8d, 0xfe8e, 0xfe8d, 0xfe8e}, // 0x0627 = alef
53 {0xfe8f, 0xfe90, 0xfe91, 0xfe92}, // 0x0628 = baa
54 {0xfe93, 0xfe94, 0xfe93, 0xfe94}, // 0x0629 = taa marbuta
55 {0xfe95, 0xfe96, 0xfe97, 0xfe98}, // 0x062a = taa
56 {0xfe99, 0xfe9a, 0xfe9b, 0xfe9c}, // 0x062b = thaa
57 {0xfe9d, 0xfe9e, 0xfe9f, 0xfea0}, // 0x062c = jeem
58 {0xfea1, 0xfea2, 0xfea3, 0xfea4}, // 0x062d = haa
59 {0xfea5, 0xfea6, 0xfea7, 0xfea8}, // 0x062e = khaa
60 {0xfea9, 0xfeaa, 0xfea9, 0xfeaa}, // 0x062f = dal
62 {0xfeab, 0xfeac, 0xfeab, 0xfeac}, // 0x0630 = thal
63 {0xfead, 0xfeae, 0xfead, 0xfeae}, // 0x0631 = ra
64 {0xfeaf, 0xfeb0, 0xfeaf, 0xfeb0}, // 0x0632 = zain
65 {0xfeb1, 0xfeb2, 0xfeb3, 0xfeb4}, // 0x0633 = seen
66 {0xfeb5, 0xfeb6, 0xfeb7, 0xfeb8}, // 0x0634 = sheen
67 {0xfeb9, 0xfeba, 0xfebb, 0xfebc}, // 0x0635 = sad
68 {0xfebd, 0xfebe, 0xfebf, 0xfec0}, // 0x0636 = dad
69 {0xfec1, 0xfec2, 0xfec3, 0xfec4}, // 0x0637 = tah
70 {0xfec5, 0xfec6, 0xfec7, 0xfec8}, // 0x0638 = zah
71 {0xfec9, 0xfeca, 0xfecb, 0xfecc}, // 0x0639 = ain
72 {0xfecd, 0xfece, 0xfecf, 0xfed0}, // 0x063a = ghain
73 {0, 0, 0, 0}, // 0x063b
74 {0, 0, 0, 0}, // 0x063c
75 {0, 0, 0, 0}, // 0x063d
76 {0, 0, 0, 0}, // 0x063e
77 {0, 0, 0, 0}, // 0x063f
79 {0, 0, 0, 0}, // 0x0640
80 {0xfed1, 0xfed2, 0xfed3, 0xfed4}, // 0x0641 = fa
81 {0xfed5, 0xfed6, 0xfed7, 0xfed8}, // 0x0642 = qaf
82 {0xfed9, 0xfeda, 0xfedb, 0xfedc}, // 0x0643 = kaf
83 {0xfedd, 0xfede, 0xfedf, 0xfee0}, // 0x0644 = lam
84 {0xfee1, 0xfee2, 0xfee3, 0xfee4}, // 0x0645 = meem
85 {0xfee5, 0xfee6, 0xfee7, 0xfee8}, // 0x0646 = noon
86 {0xfee9, 0xfeea, 0xfeeb, 0xfeec}, // 0x0647 = ha
87 {0xfeed, 0xfeee, 0xfeed, 0xfeee}, // 0x0648 = waw
88 {0xfeef, 0xfef0, 0xfeef, 0xfef0}, // 0x0649 = alef maksura
89 {0xfef1, 0xfef2, 0xfef3, 0xfef4}, // 0x064a = ya
90 {0x065b, 0x065b, 0x065b, 0x065b}, // 0x064b = fathatan
91 {0x065c, 0x065c, 0x065c, 0x065c}, // 0x064c = dammatan
92 {0x064d, 0x064d, 0x064d, 0x064d}, // 0x064d = kasratan
93 {0x064e, 0x064e, 0x064e, 0x064e}, // 0x064e = fatha
94 {0x064f, 0x064f, 0x064f, 0x064f}, // 0x064f = damma
96 {0x0650, 0x0650, 0x0650, 0x0650}, // 0x0650 = kasra
97 {0x0651, 0x0651, 0x0651, 0x0651}, // 0x0651 = shadda
98 {0x0652, 0x0652, 0x0652, 0x0652}, // 0x0652 = sukun
100 {0, 0, 0, 0}, // 0x0653
101 {0, 0, 0, 0}, // 0x0654
102 {0, 0, 0, 0}, // 0x0655
103 {0, 0, 0, 0}, // 0x0656
104 {0, 0, 0, 0}, // 0x0657
105 {0, 0, 0, 0}, // 0x0658
106 {0, 0, 0, 0}, // 0x0659
107 {0, 0, 0, 0}, // 0x065a
108 {0, 0, 0, 0}, // 0x065b
109 {0, 0, 0, 0}, // 0x065c
110 {0, 0, 0, 0}, // 0x065d
111 {0, 0, 0, 0}, // 0x065e
112 {0, 0, 0, 0}, // 0x065f
113 {0, 0, 0, 0}, // 0x0660
114 {0, 0, 0, 0}, // 0x0661
115 {0, 0, 0, 0}, // 0x0662
116 {0, 0, 0, 0}, // 0x0663
117 {0, 0, 0, 0}, // 0x0664
118 {0, 0, 0, 0}, // 0x0665
119 {0, 0, 0, 0}, // 0x0666
120 {0, 0, 0, 0}, // 0x0667
121 {0, 0, 0, 0}, // 0x0668
122 {0, 0, 0, 0}, // 0x0669
123 {0, 0, 0, 0}, // 0x066a
124 {0, 0, 0, 0}, // 0x066b
125 {0, 0, 0, 0}, // 0x066c
126 {0, 0, 0, 0}, // 0x066d
127 {0, 0, 0, 0}, // 0x066e
128 {0, 0, 0, 0}, // 0x066f
129 {0, 0, 0, 0}, // 0x0670
130 {0, 0, 0, 0}, // 0x0671
131 {0, 0, 0, 0}, // 0x0672
132 {0, 0, 0, 0}, // 0x0673
133 {0, 0, 0, 0}, // 0x0674
134 {0, 0, 0, 0}, // 0x0675
135 {0, 0, 0, 0}, // 0x0676
136 {0, 0, 0, 0}, // 0x0677
137 {0, 0, 0, 0}, // 0x0678
138 {0, 0, 0, 0}, // 0x0679
139 {0, 0, 0, 0}, // 0x067a
140 {0, 0, 0, 0}, // 0x067b
141 {0, 0, 0, 0}, // 0x067c
142 {0, 0, 0, 0}, // 0x067d
143 {0xfb56, 0xfb57, 0xfb58, 0xfb59}, // 0x067e = peh
144 {0, 0, 0, 0}, // 0x067f
145 {0, 0, 0, 0}, // 0x0680
146 {0, 0, 0, 0}, // 0x0681
147 {0, 0, 0, 0}, // 0x0682
148 {0, 0, 0, 0}, // 0x0683
149 {0, 0, 0, 0}, // 0x0684
150 {0, 0, 0, 0}, // 0x0685
151 {0xfb7a, 0xfb7b, 0xfb7c, 0xfb7d}, // 0x0686 = tcheh
152 {0, 0, 0, 0}, // 0x0687
153 {0, 0, 0, 0}, // 0x0688
154 {0, 0, 0, 0}, // 0x0689
155 {0, 0, 0, 0}, // 0x068a
156 {0, 0, 0, 0}, // 0x068b
157 {0, 0, 0, 0}, // 0x068c
158 {0, 0, 0, 0}, // 0x068d
159 {0, 0, 0, 0}, // 0x068e
160 {0, 0, 0, 0}, // 0x068f
161 {0, 0, 0, 0}, // 0x0690
162 {0, 0, 0, 0}, // 0x0691
163 {0, 0, 0, 0}, // 0x0692
164 {0, 0, 0, 0}, // 0x0693
165 {0, 0, 0, 0}, // 0x0694
166 {0, 0, 0, 0}, // 0x0695
167 {0, 0, 0, 0}, // 0x0696
168 {0, 0, 0, 0}, // 0x0697
169 {0xfb8a, 0xfb8b, 0xfb8a, 0xfb8b}, // 0x0698 = jeh
170 {0, 0, 0, 0}, // 0x0699
171 {0, 0, 0, 0}, // 0x069a
172 {0, 0, 0, 0}, // 0x069b
173 {0, 0, 0, 0}, // 0x069c
174 {0, 0, 0, 0}, // 0x069d
175 {0, 0, 0, 0}, // 0x069e
176 {0, 0, 0, 0}, // 0x069f
177 {0, 0, 0, 0}, // 0x06a0
178 {0, 0, 0, 0}, // 0x06a1
179 {0, 0, 0, 0}, // 0x06a2
180 {0, 0, 0, 0}, // 0x06a3
181 {0, 0, 0, 0}, // 0x06a4
182 {0, 0, 0, 0}, // 0x06a5
183 {0, 0, 0, 0}, // 0x06a6
184 {0, 0, 0, 0}, // 0x06a7
185 {0, 0, 0, 0}, // 0x06a8
186 {0xfb8e, 0xfb8f, 0xfb90, 0xfb91}, // 0x06a9 = farsi kaf
187 {0, 0, 0, 0}, // 0x06aa
188 {0, 0, 0, 0}, // 0x06ab
189 {0, 0, 0, 0}, // 0x06ac
190 {0, 0, 0, 0}, // 0x06ad
191 {0, 0, 0, 0}, // 0x06ae
192 {0xfb92, 0xfb93, 0xfb94, 0xfb95}, // 0x06af = gaf
193 {0, 0, 0, 0}, // 0x06b0
194 {0, 0, 0, 0}, // 0x06b1
195 {0, 0, 0, 0}, // 0x06b2
196 {0, 0, 0, 0}, // 0x06b3
197 {0, 0, 0, 0}, // 0x06b4
198 {0, 0, 0, 0}, // 0x06b5
199 {0, 0, 0, 0}, // 0x06b6
200 {0, 0, 0, 0}, // 0x06b7
201 {0, 0, 0, 0}, // 0x06b8
202 {0, 0, 0, 0}, // 0x06b9
203 {0, 0, 0, 0}, // 0x06ba
204 {0, 0, 0, 0}, // 0x06bb
205 {0, 0, 0, 0}, // 0x06bc
206 {0, 0, 0, 0}, // 0x06bd
207 {0, 0, 0, 0}, // 0x06be
208 {0, 0, 0, 0}, // 0x06bf
209 {0, 0, 0, 0}, // 0x06c0
210 {0, 0, 0, 0}, // 0x06c1
211 {0, 0, 0, 0}, // 0x06c2
212 {0, 0, 0, 0}, // 0x06c3
213 {0, 0, 0, 0}, // 0x06c4
214 {0, 0, 0, 0}, // 0x06c5
215 {0, 0, 0, 0}, // 0x06c6
216 {0, 0, 0, 0}, // 0x06c7
217 {0, 0, 0, 0}, // 0x06c8
218 {0, 0, 0, 0}, // 0x06c9
219 {0, 0, 0, 0}, // 0x06ca
220 {0, 0, 0, 0}, // 0x06cb
221 {0xfbfc, 0xfbfd, 0xfbfe, 0xfbff} // 0x06cc = farsi yeh
225 char_type const arabic_start = 0x0621;
226 char_type const arabic_end = 0x06cc;
229 /// Information about a single UCS4 character
231 /// LaTeX command for this character
233 /// Needed LaTeX preamble (or feature)
235 /// Is this a combining character?
237 /// Is \c preamble a feature known by LaTeXFeatures, or a raw LaTeX
240 /// Always force the LaTeX command, even if the encoding contains
246 typedef std::map<char_type, CharInfo> CharInfoMap;
247 CharInfoMap unicodesymbols;
250 /// The highest code point in UCS4 encoding (1<<20 + 1<<16)
251 char_type const max_ucs4 = 0x110000;
256 Encoding::Encoding(string const & n, string const & l, string const & i,
257 bool f, Encoding::Package p)
258 : Name_(n), LatexName_(l), iconvName_(i), fixedwidth_(f), package_(p)
261 // ASCII can encode 128 code points and nothing else
262 start_encodable_ = 128;
264 } else if (i == "UTF-8") {
265 // UTF8 can encode all UCS4 code points
266 start_encodable_ = max_ucs4;
274 void Encoding::init() const
276 start_encodable_ = 0;
277 // temporarily switch off lyxerr, since we will generate iconv errors
280 // We do not need to check all UCS4 code points, it is enough
281 // if we check all 256 code points of this encoding.
282 for (unsigned short j = 0; j < 256; ++j) {
283 char const c = char(j);
284 std::vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, iconvName_);
285 if (ucs4.size() == 1) {
286 char_type const c = ucs4[0];
287 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
288 if (it == unicodesymbols.end() || !it->second.force)
289 encodable_.insert(c);
293 // We do not know how many code points this encoding has, and
294 // they do not have a direct representation as a single byte,
295 // therefore we need to check all UCS4 code points.
296 // This is expensive!
297 for (char_type c = 0; c < max_ucs4; ++c) {
298 std::vector<char> const eightbit = ucs4_to_eightbit(&c, 1, iconvName_);
299 if (!eightbit.empty()) {
300 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
301 if (it == unicodesymbols.end() || !it->second.force)
302 encodable_.insert(c);
307 CharSet::iterator it = encodable_.find(start_encodable_);
308 while (it != encodable_.end()) {
309 encodable_.erase(it);
311 it = encodable_.find(start_encodable_);
317 docstring const Encoding::latexChar(char_type c) const
319 // assure the used encoding is properly initialized
322 BOOST_ASSERT(complete_);
324 if (c < start_encodable_)
325 return docstring(1, c);
326 if (encodable_.find(c) == encodable_.end()) {
327 // c cannot be encoded in this encoding
328 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
329 if (it == unicodesymbols.end())
330 lyxerr << "Could not find LaTeX command for character 0x"
331 << std::hex << c << std::dec
332 << ".\nLaTeX export will fail."
335 return it->second.command;
337 return docstring(1, c);
341 void Encodings::validate(char_type c, LaTeXFeatures & features)
343 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
344 if (it != unicodesymbols.end() && !it->second.preamble.empty()) {
345 if (it->second.feature)
346 features.require(it->second.preamble);
348 features.addPreambleSnippet(it->second.preamble);
353 bool Encodings::isComposeChar_hebrew(char_type c)
355 return c <= 0x05c2 && c >= 0x05b0 &&
356 c != 0x05be && c != 0x05c0;
360 // Special Arabic letters are ones that do not get connected from left
361 // they are hamza, alef_madda, alef_hamza, waw_hamza, alef_hamza_under,
362 // alef, tah_marbota, dal, thal, rah, zai, wow, alef_maksoura
364 bool Encodings::is_arabic_special(char_type c)
366 return (c >= 0x0621 && c <= 0x0625) ||
367 c == 0x0627 || c == 0x0629 ||
368 c == 0x062f || c == 0x0648 ||
369 (c >= 0x0630 && c <= 0x0632) ||
370 c == 0x0649 || c == 0x0698;
374 bool Encodings::isComposeChar_arabic(char_type c)
376 return c >= 0x064b && c <= 0x0652;
380 bool Encodings::is_arabic(char_type c)
382 return c >= arabic_start && c <= arabic_end &&
383 arabic_table[c-arabic_start][0];
387 char_type Encodings::transformChar(char_type c,
388 Encodings::Letter_Form form)
393 return arabic_table[c-arabic_start][form];
397 bool Encodings::isCombiningChar(char_type c)
399 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
400 if (it != unicodesymbols.end())
401 return it->second.combining;
406 bool Encodings::isKnownScriptChar(char_type const c, string & preamble)
408 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
410 if (it == unicodesymbols.end())
413 if (it->second.preamble != "textgreek" &&
414 it->second.preamble != "textcyr")
417 if (preamble.empty()) {
418 preamble = it->second.preamble;
421 return it->second.preamble == preamble;
425 Encoding const * Encodings::getFromLyXName(string const & name) const
427 EncodingList::const_iterator it = encodinglist.find(name);
428 if (it != encodinglist.end())
435 Encoding const * Encodings::getFromLaTeXName(string const & name) const
437 // We don't use std::find_if because it makes copies of the pairs in
439 // This linear search is OK since we don't have many encodings.
440 // Users could even optimize it by putting the encodings they use
441 // most at the top of lib/encodings.
442 EncodingList::const_iterator const end = encodinglist.end();
443 for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
444 if (it->second.latexName() == name)
450 Encodings::Encodings()
455 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
457 // We must read the symbolsfile first, because the Encoding
458 // constructor depends on it.
459 Lexer symbolslex(0, 0);
460 symbolslex.setFile(symbolsfile);
461 while (symbolslex.isOK()) {
466 if (symbolslex.next(true)) {
467 std::istringstream is(symbolslex.getString());
468 // reading symbol directly does not work if
469 // char_type == std::wchar_t.
471 if(!(is >> std::hex >> tmp))
476 if (symbolslex.next(true))
477 info.command = symbolslex.getDocString();
480 if (symbolslex.next(true))
481 info.preamble = symbolslex.getString();
484 if (symbolslex.next(true))
485 flags = symbolslex.getString();
489 info.combining = false;
490 info.feature = false;
492 while (!flags.empty()) {
494 flags = support::split(flags, flag, ',');
495 if (flag == "combining")
496 info.combining = true;
497 else if (flag == "force")
500 lyxerr << "Ignoring unknown flag `" << flag
501 << "' for symbol `0x"
502 << std::hex << symbol << std::dec
506 if (!info.preamble.empty())
507 info.feature = info.preamble[0] != '\\';
509 LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '"
510 << to_utf8(info.command) << "' '" << info.preamble
511 << "' " << info.combining << ' ' << info.feature);
512 unicodesymbols[symbol] = info;
515 // Now read the encodings
522 struct keyword_item encodingtags[et_last - 1] = {
523 { "encoding", et_encoding },
527 Lexer lex(encodingtags, et_last - 1);
528 lex.setFile(encfile);
534 string const name = lex.getString();
536 string const latexname = lex.getString();
538 string const iconvname = lex.getString();
540 string const width = lex.getString();
541 bool fixedwidth = false;
542 if (width == "fixed")
544 else if (width == "variable")
547 lex.printError("Encodings::read: "
548 "Unknown width: `$$Token'");
552 string const p = lex.getString();
553 Encoding::Package package = Encoding::none;
555 package = Encoding::none;
556 else if (p == "inputenc")
557 package = Encoding::inputenc;
559 package = Encoding::CJK;
561 lex.printError("Encodings::read: "
562 "Unknown package: `$$Token'");
565 LYXERR(Debug::INFO, "Reading encoding " << name);
566 encodinglist[name] = Encoding(name, latexname,
567 iconvname, fixedwidth,
569 if (lex.lex() != et_end)
570 lex.printError("Encodings::read: "
575 lex.printError("Encodings::read: Misplaced end");
577 case Lexer::LEX_FEOF:
580 lex.printError("Encodings::read: "
581 "Unknown tag: `$$Token'");