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