]> git.lyx.org Git - lyx.git/blob - src/encoding.C
* src/tabular.[Ch]: simplify plaintext methods, because there
[lyx.git] / src / encoding.C
1 /**
2  * \file encoding.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Dekel Tsur
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "encoding.h"
16
17 #include "debug.h"
18 #include "LaTeXFeatures.h"
19 #include "lyxlex.h"
20 #include "lyxrc.h"
21
22 #include "support/filename.h"
23 #include "support/lstrings.h"
24 #include "support/unicode.h"
25
26 #include <sstream>
27
28
29 namespace lyx {
30
31 using support::FileName;
32
33 #ifndef CXX_GLOBAL_CSTD
34 using std::strtol;
35 #endif
36
37 using std::endl;
38 using std::string;
39
40
41 Encodings encodings;
42
43 namespace {
44
45 char_type arabic_table2[63][4] = {
46         {0x41, 0x41, 0x41, 0x41}, // 0xc1 = hamza
47         {0x42, 0xa1, 0x42, 0xa1}, // 0xc2 = ligature madda on alef
48         {0x43, 0xa2, 0x43, 0xa2}, // 0xc3 = ligature hamza on alef
49         {0x44, 0xa3, 0x44, 0xa3}, // 0xc4 = ligature hamza on waw
50         {0x45, 0xa4, 0x45, 0xa4}, // 0xc5 = ligature hamza under alef
51         {0x46, 0xf9, 0xf8, 0xa0}, // 0xc6 = ligature hamza on ya
52         {0x47, 0xa5, 0x47, 0xa5}, // 0xc7 = alef
53         {0x48, 0xae, 0xac, 0xad}, // 0xc8 = baa
54         {0x49, 0xb1, 0x49, 0xb1}, // 0xc9 = taa marbuta
55         {0x4a, 0xb4, 0xb2, 0xb3}, // 0xca = taa
56         {0x4b, 0xb7, 0xb5, 0xb6}, // 0xcb = thaa
57         {0x4c, 0xba, 0xb8, 0xb9}, // 0xcc = jeem
58         {0x4d, 0xbd, 0xbb, 0xbc}, // 0xcd = haa
59         {0x4e, 0xc0, 0xbe, 0xbf}, // 0xce = khaa
60         {0x4f, 0xa6, 0x4f, 0xa6}, // 0xcf = dal
61
62         {0x50, 0xa7, 0x50, 0xa7}, // 0xd0 = thal
63         {0x51, 0xa8, 0x51, 0xa8}, // 0xd1 = ra
64         {0x52, 0xa9, 0x52, 0xa9}, // 0xd2 = zain
65         {0x53, 0xc3, 0xc1, 0xc2}, // 0xd3 = seen
66         {0x54, 0xc6, 0xc4, 0xc5}, // 0xd4 = sheen
67         {0x55, 0xc9, 0xc7, 0xc8}, // 0xd5 = sad
68         {0x56, 0xcc, 0xca, 0xcb}, // 0xd6 = dad
69         {0x57, 0xcf, 0xcd, 0xce}, // 0xd7 = tah
70         {0x58, 0xd2, 0xd0, 0xd1}, // 0xd8 = zah
71         {0x59, 0xd5, 0xd3, 0xd4}, // 0xd9 = ain
72         {0x5a, 0xd8, 0xd6, 0xd7}, // 0xda = ghain
73         {0,0,0,0}, // 0xdb
74         {0,0,0,0}, // 0xdc
75         {0,0,0,0}, // 0xdd
76         {0,0,0,0}, // 0xde
77         {0,0,0,0}, // 0xdf
78
79         {0,0,0,0}, // 0xe0
80         {0x61, 0xdb, 0xd9, 0xda}, // 0xe1 = fa
81         {0x62, 0xde, 0xdc, 0xdd}, // 0xe2 = qaf
82         {0x63, 0xe1, 0xdf, 0xe0}, // 0xe3 = kaf
83         {0x64, 0xe4, 0xe2, 0xe3}, // 0xe4 = lam
84         {0x65, 0xe7, 0xe5, 0xe6}, // 0xe5 = meem
85         {0x66, 0xea, 0xe8, 0xe9}, // 0xe6 = noon
86         {0x67, 0xed, 0xeb, 0xec}, // 0xe7 = ha
87         {0x68, 0xaa, 0x68, 0xaa}, // 0xe8 = waw
88         {0x69, 0xab, 0x69, 0xab}, // 0xe9 = alef maksura
89         {0x6a, 0xf0, 0xee, 0xef}, // 0xea = ya
90         {0x6b, 0x6b, 0x6b, 0x6b}, // 0xeb = fathatan
91         {0x6c, 0x6c, 0x6c, 0x6c}, // 0xec = dammatan
92         {0x6d, 0x6d, 0x6d, 0x6d}, // 0xed = kasratan
93         {0x6e, 0x6e, 0x6e, 0x6e}, // 0xee = fatha
94         {0x6f, 0x6f, 0x6f, 0x6f}, // 0xef = damma
95
96         {0x70, 0x70, 0x70, 0x70}, // 0xf0 = kasra
97         {0x71, 0x71, 0x71, 0x71}, // 0xf1 = shadda
98         {0x72, 0x72, 0x72, 0x72}, // 0xf2 = sukun
99         {0,0,0,0}, // 0xf3
100         {0,0,0,0}, // 0xf4
101         {0,0,0,0}, // 0xf5
102         {0,0,0,0}, // 0xf6
103         {0,0,0,0}, // 0xf7
104         {0,0,0,0}, // 0xf8
105         {0,0,0,0}, // 0xf9
106         {0,0,0,0}, // 0xfa
107         {0,0,0,0}, // 0xfb
108         {0,0,0,0}, // 0xfc
109         {0,0,0,0}, // 0xfd
110         {0,0,0,0}, // 0xfe
111         {0,0,0,0}, // 0xff
112 };
113
114
115 char_type arabic_table[63][2] = {
116         {0xc1, 0xc1}, // 0xc1 = hamza
117         {0xc2, 0xc2}, // 0xc2 = ligature madda on alef
118         {0xc3, 0xc3}, // 0xc3 = ligature hamza on alef
119         {0xc4, 0xc4}, // 0xc4 = ligature hamza on waw
120         {0xc5, 0xc5}, // 0xc5 = ligature hamza under alef
121         {0xc6, 0xc0}, // 0xc6 = ligature hamza on ya
122         {0xc7, 0xc7}, // 0xc7 = alef
123         {0xc8, 0xeb}, // 0xc8 = baa
124         {0xc9, 0xc9}, // 0xc9 = taa marbuta
125         {0xca, 0xec}, // 0xca = taa
126         {0xcb, 0xed}, // 0xcb = thaa
127         {0xcc, 0xee}, // 0xcc = jeem
128         {0xcd, 0xef}, // 0xcd = haa
129         {0xce, 0xf0}, // 0xce = khaa
130         {0xcf, 0xcf}, // 0xcf = dal
131
132         {0xd0, 0xd0}, // 0xd0 = thal
133         {0xd1, 0xd1}, // 0xd1 = ra
134         {0xd2, 0xd2}, // 0xd2 = zain
135         {0xd3, 0xf1}, // 0xd3 = seen
136         {0xd4, 0xf2}, // 0xd4 = sheen
137         {0xd5, 0xf3}, // 0xd5 = sad
138         {0xd6, 0xf4}, // 0xd6 = dad
139         {0xd7, 0xd7}, // 0xd7 = tah
140         {0xd8, 0xd8}, // 0xd8 = zah
141         {0xd9, 0xf5}, // 0xd9 = ain
142         {0xda, 0xf6}, // 0xda = ghain
143         {0,0}, // 0xdb
144         {0,0}, // 0xdc
145         {0,0}, // 0xdd
146         {0,0}, // 0xde
147         {0,0}, // 0xdf
148
149         {0,0},  // 0xe0
150         {0xe1, 0xf7},   // 0xe1 = fa
151         {0xe2, 0xf8},   // 0xe2 = qaf
152         {0xe3, 0xf9},   // 0xe3 = kaf
153         {0xe4, 0xfa},   // 0xe4 = lam
154         {0xe5, 0xfb},   // 0xe5 = meem
155         {0xe6, 0xfc},   // 0xe6 = noon
156         {0xe7, 0xfd},   // 0xe7 = ha
157         {0xe8, 0xe8},   // 0xe8 = waw
158         {0xe9, 0xe9},   // 0xe9 = alef maksura
159         {0xea, 0xfe},   // 0xea = ya
160         {0xa8, 0xa8},   // 0xeb = fathatan
161         {0xa9, 0xa9},   // 0xec = dammatan
162         {0xaa, 0xaa},   // 0xed = kasratan
163         {0xab, 0xab},   // 0xee = fatha
164         {0xac, 0xac},   // 0xef = damma
165
166         {0xad, 0xad},   // 0xf0 = kasra
167         {0xae, 0xae},   // 0xf1 = shadda
168         {0xaf, 0xaf},   // 0xf2 = sukun
169         {0,0}, // 0xf3
170         {0,0}, // 0xf4
171         {0,0}, // 0xf5
172         {0,0}, // 0xf6
173         {0,0}, // 0xf7
174         {0,0}, // 0xf8
175         {0,0}, // 0xf9
176         {0,0}, // 0xfa
177         {0,0}, // 0xfb
178         {0,0}, // 0xfc
179         {0,0}, // 0xfd
180         {0,0}, // 0xfe
181         {0,0} // 0xff
182 };
183
184
185 char_type const arabic_start = 0xc1;
186
187
188 /// Information about a single UCS4 character
189 struct CharInfo {
190         /// LaTeX command for this character
191         docstring command;
192         /// Needed LaTeX preamble (or feature)
193         string preamble;
194         /// Is this a combining character?
195         bool combining;
196         /// Is \c preamble a feature known by LaTeXFeatures, or a raw LaTeX
197         /// command?
198         bool feature;
199         /// Always force the LaTeX command, even if the encoding contains
200         /// this character?
201         bool force;
202 };
203
204
205 typedef std::map<char_type, CharInfo> CharInfoMap;
206 CharInfoMap unicodesymbols;
207
208 } // namespace anon
209
210
211 Encoding::Encoding(string const & n, string const & l, string const & i)
212         : Name_(n), LatexName_(l), iconvName_(i)
213 {
214         if (n == "ascii")
215                 // ASCII can encode 128 code points and nothing else
216                 start_encodable_ = 128;
217         if (n == "utf8" || n == "utf8x")
218                 // UTF8 can encode all 1<<20 + 1<<16 UCS4 code points
219                 start_encodable_ = 0x110000;
220         else {
221                 start_encodable_ = 0;
222                 // temporarily switch off lyxerr, since we will generate iconv errors
223                 lyxerr.disable();
224                 for (unsigned short j = 0; j < 256; ++j) {
225                         char const c = j;
226                         std::vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, i);
227                         if (ucs4.size() == 1) {
228                                 char_type const c = ucs4[0];
229                                 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
230                                 if (it == unicodesymbols.end() || !it->second.force)
231                                         encodable_.insert(c);
232                         }
233                 }
234                 lyxerr.enable();
235                 CharSet::iterator it = encodable_.find(start_encodable_);
236                 while (it != encodable_.end()) {
237                         encodable_.erase(it);
238                         ++start_encodable_;
239                         it = encodable_.find(start_encodable_);
240                 }
241         }
242 }
243
244
245 docstring const Encoding::latexChar(char_type c) const
246 {
247         if (c < start_encodable_)
248                 return docstring(1, c);
249         if (encodable_.find(c) == encodable_.end()) {
250                 // c cannot be encoded in this encoding
251                 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
252                 if (it == unicodesymbols.end())
253                         lyxerr << "Could not find LaTeX command for character 0x"
254                                << std::hex << c << ".\nLaTeX export will fail."
255                                << endl;
256                 else
257                         return it->second.command;
258         }
259         return docstring(1, c);
260 }
261
262
263 void Encoding::validate(char_type c, LaTeXFeatures & features) const
264 {
265         // Add the preamble stuff even if c can be encoded in this encoding,
266         // since the inputenc package only maps the code point c to a command,
267         // it does not make this command available.
268         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
269         if (it != unicodesymbols.end() && !it->second.preamble.empty()) {
270                 if (it->second.feature)
271                         features.require(it->second.preamble);
272                 else
273                         features.addPreambleSnippet(it->second.preamble);
274         }
275 }
276
277
278 bool Encodings::isComposeChar_hebrew(char_type c)
279 {
280         return c <= 0xd2 && c >= 0xc0 &&
281                 c != 0xce && c != 0xd0;
282 }
283
284
285 // Special Arabic letters are ones that do not get connected from left
286 // they are hamza, alef_madda, alef_hamza, waw_hamza, alef_hamza_under,
287 // alef, tah_marbota, dal, thal, rah, zai, wow, alef_maksoura
288
289 bool Encodings::is_arabic_special(char_type c)
290 {
291         return  (c >= 0xc1 && c <= 0xc5) ||
292                  c == 0xc7 || c  == 0xc9  ||
293                  c == 0xcf || c  == 0xe8  ||
294                 (c >= 0xd0 && c <= 0xd2) ||
295                  c == 0xe9;
296 }
297
298 bool Encodings::isComposeChar_arabic(char_type c)
299 {
300         return c >= 0xeb && c <= 0xf2;
301 }
302
303
304 bool Encodings::is_arabic(char_type c)
305 {
306         return c >= arabic_start && arabic_table[c-arabic_start][0];
307 }
308
309
310 char_type Encodings::transformChar(char_type c,
311                                       Encodings::Letter_Form form)
312 {
313         if (!is_arabic(c))
314                 return c;
315
316         if (lyxrc.font_norm_type == LyXRC::ISO_10646_1)
317                 return arabic_table2[c-arabic_start][form];
318         else
319                 return arabic_table[c-arabic_start][form >> 1];
320 }
321
322
323 bool Encodings::isCombiningChar(char_type c)
324 {
325         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
326         if (it != unicodesymbols.end())
327                 return it->second.combining;
328         return false;
329 }
330
331
332 Encoding const * Encodings::getFromLyXName(string const & name) const
333 {
334         EncodingList::const_iterator it = encodinglist.find(name);
335         if (it != encodinglist.end())
336                 return &it->second;
337         else
338                 return 0;
339 }
340
341
342 Encoding const * Encodings::getFromLaTeXName(string const & name) const
343 {
344         // We don't use std::find_if because it makes copies of the pairs in
345         // the map.
346         // This linear search is OK since we don't have many encodings.
347         // Users could even optimize it by putting the encodings they use
348         // most at the top of lib/encodings.
349         EncodingList::const_iterator const end = encodinglist.end();
350         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
351                 if (it->second.latexName() == name)
352                         return &it->second;
353         return 0;
354 }
355
356
357 Encodings::Encodings()
358 {
359 }
360
361
362 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
363 {
364         // We must read the symbolsfile first, because the Encoding
365         // constructor depends on it.
366         LyXLex symbolslex(0, 0);
367         symbolslex.setFile(symbolsfile);
368         while (symbolslex.isOK()) {
369                 char_type symbol;
370                 CharInfo info;
371                 string flags;
372
373                 if (symbolslex.next(true)) {
374                         std::istringstream is(symbolslex.getString());
375                         // reading symbol directly does not work if
376                         // char_type == std::wchar_t.
377                         boost::uint32_t tmp;
378                         if(!(is >> std::hex >> tmp))
379                                 break;
380                         symbol = tmp;
381                 } else
382                         break;
383                 if (symbolslex.next(true))
384                         info.command = symbolslex.getDocString();
385                 else
386                         break;
387                 if (symbolslex.next(true))
388                         info.preamble = symbolslex.getString();
389                 else
390                         break;
391                 if (symbolslex.next(true))
392                         flags = symbolslex.getString();
393                 else
394                         break;
395
396                 info.combining = false;
397                 info.force = false;
398                 while (!flags.empty()) {
399                         string flag;
400                         flags = support::split(flags, flag, ',');
401                         if (flag == "combining")
402                                 info.combining = true;
403                         else if (flag == "force")
404                                 info.force = true;
405                         else
406                                 lyxerr << "Ignoring unknown flag `" << flag
407                                        << "' for symbol `0x" << std::hex
408                                        << symbol << "'." << endl;
409                 }
410
411                 if (!info.preamble.empty())
412                         info.feature = info.preamble[0] != '\\';
413
414                 lyxerr[Debug::INFO]
415                         << "Read unicode symbol " << symbol << " '"
416                         << to_utf8(info.command) << "' '" << info.preamble
417                         << "' " << info.combining << ' ' << info.feature
418                         << endl;
419                 unicodesymbols[symbol] = info;
420         }
421
422         // Now read the encodings
423         enum Encodingtags {
424                 et_encoding = 1,
425                 et_end,
426                 et_last
427         };
428
429         struct keyword_item encodingtags[et_last - 1] = {
430                 { "encoding", et_encoding },
431                 { "end", et_end }
432         };
433
434         LyXLex lex(encodingtags, et_last - 1);
435         lex.setFile(encfile);
436         while (lex.isOK()) {
437                 switch (lex.lex()) {
438                 case et_encoding:
439                 {
440                         lex.next();
441                         string const name = lex.getString();
442                         lex.next();
443                         string const latexname = lex.getString();
444                         lex.next();
445                         string const iconvname = lex.getString();
446                         lyxerr[Debug::INFO] << "Reading encoding " << name << endl;
447                         encodinglist[name] = Encoding(name, latexname, iconvname);
448                         if (lex.lex() != et_end)
449                                 lex.printError("Encodings::read: "
450                                                "missing end");
451                         break;
452                 }
453                 case et_end:
454                         lex.printError("Encodings::read: Misplaced end");
455                         break;
456                 case LyXLex::LEX_FEOF:
457                         break;
458                 default:
459                         lex.printError("Encodings::read: "
460                                        "Unknown tag: `$$Token'");
461                         break;
462                 }
463         }
464 }
465
466
467 } // namespace lyx