]> git.lyx.org Git - lyx.git/blob - src/encoding.C
* src/frontends/controllers/ControlAboutlyx.[Ch]:
[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_table[50][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
61
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
78
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
95
96         {0x0650, 0x0650, 0x0650, 0x0650}, // 0x0650 = kasra
97         {0x0651, 0x0651, 0x0651, 0x0651}, // 0x0651 = shadda
98         {0x0652, 0x0652, 0x0652, 0x0652}, // 0x0652 = sukun
99 };
100
101
102 char_type const arabic_start = 0x0621;
103 char_type const arabic_end = 0x0652;
104
105
106 /// Information about a single UCS4 character
107 struct CharInfo {
108         /// LaTeX command for this character
109         docstring command;
110         /// Needed LaTeX preamble (or feature)
111         string preamble;
112         /// Is this a combining character?
113         bool combining;
114         /// Is \c preamble a feature known by LaTeXFeatures, or a raw LaTeX
115         /// command?
116         bool feature;
117         /// Always force the LaTeX command, even if the encoding contains
118         /// this character?
119         bool force;
120 };
121
122
123 typedef std::map<char_type, CharInfo> CharInfoMap;
124 CharInfoMap unicodesymbols;
125
126 } // namespace anon
127
128
129 Encoding::Encoding(string const & n, string const & l, string const & i)
130         : Name_(n), LatexName_(l), iconvName_(i)
131 {
132         if (n == "ascii")
133                 // ASCII can encode 128 code points and nothing else
134                 start_encodable_ = 128;
135         else if (i == "UTF-8")
136                 // UTF8 can encode all 1<<20 + 1<<16 UCS4 code points
137                 start_encodable_ = 0x110000;
138         else {
139                 start_encodable_ = 0;
140                 // temporarily switch off lyxerr, since we will generate iconv errors
141                 lyxerr.disable();
142                 for (unsigned short j = 0; j < 256; ++j) {
143                         char const c = j;
144                         std::vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, i);
145                         if (ucs4.size() == 1) {
146                                 char_type const c = ucs4[0];
147                                 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
148                                 if (it == unicodesymbols.end() || !it->second.force)
149                                         encodable_.insert(c);
150                         }
151                 }
152                 lyxerr.enable();
153                 CharSet::iterator it = encodable_.find(start_encodable_);
154                 while (it != encodable_.end()) {
155                         encodable_.erase(it);
156                         ++start_encodable_;
157                         it = encodable_.find(start_encodable_);
158                 }
159         }
160 }
161
162
163 docstring const Encoding::latexChar(char_type c) const
164 {
165         if (c < start_encodable_)
166                 return docstring(1, c);
167         if (encodable_.find(c) == encodable_.end()) {
168                 // c cannot be encoded in this encoding
169                 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
170                 if (it == unicodesymbols.end())
171                         lyxerr << "Could not find LaTeX command for character 0x"
172                                << std::hex << c << ".\nLaTeX export will fail."
173                                << endl;
174                 else
175                         return it->second.command;
176         }
177         return docstring(1, c);
178 }
179
180
181 void Encoding::validate(char_type c, LaTeXFeatures & features) const
182 {
183         // Add the preamble stuff even if c can be encoded in this encoding,
184         // since the inputenc package only maps the code point c to a command,
185         // it does not make this command available.
186         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
187         if (it != unicodesymbols.end() && !it->second.preamble.empty()) {
188                 if (it->second.feature)
189                         features.require(it->second.preamble);
190                 else
191                         features.addPreambleSnippet(it->second.preamble);
192         }
193 }
194
195
196 bool Encodings::isComposeChar_hebrew(char_type c)
197 {
198         return c <= 0x05c2 && c >= 0x05b0 &&
199                c != 0x05be && c != 0x05c0;
200 }
201
202
203 // Special Arabic letters are ones that do not get connected from left
204 // they are hamza, alef_madda, alef_hamza, waw_hamza, alef_hamza_under,
205 // alef, tah_marbota, dal, thal, rah, zai, wow, alef_maksoura
206
207 bool Encodings::is_arabic_special(char_type c)
208 {
209         return (c >= 0x0621 && c <= 0x0625) ||
210                 c == 0x0627 || c == 0x0629  ||
211                 c == 0x062f || c == 0x0648  ||
212                (c >= 0x0630 && c <= 0x0632) ||
213                 c == 0x0649;
214 }
215
216
217 bool Encodings::isComposeChar_arabic(char_type c)
218 {
219         return c >= 0x064b && c <= 0x0652;
220 }
221
222
223 bool Encodings::is_arabic(char_type c)
224 {
225         return c >= arabic_start && c <= arabic_end &&
226                arabic_table[c-arabic_start][0];
227 }
228
229
230 char_type Encodings::transformChar(char_type c,
231                                       Encodings::Letter_Form form)
232 {
233         if (!is_arabic(c))
234                 return c;
235
236         return arabic_table[c-arabic_start][form];
237 }
238
239
240 bool Encodings::isCombiningChar(char_type c)
241 {
242         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
243         if (it != unicodesymbols.end())
244                 return it->second.combining;
245         return false;
246 }
247
248
249 Encoding const * Encodings::getFromLyXName(string const & name) const
250 {
251         EncodingList::const_iterator it = encodinglist.find(name);
252         if (it != encodinglist.end())
253                 return &it->second;
254         else
255                 return 0;
256 }
257
258
259 Encoding const * Encodings::getFromLaTeXName(string const & name) const
260 {
261         // We don't use std::find_if because it makes copies of the pairs in
262         // the map.
263         // This linear search is OK since we don't have many encodings.
264         // Users could even optimize it by putting the encodings they use
265         // most at the top of lib/encodings.
266         EncodingList::const_iterator const end = encodinglist.end();
267         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
268                 if (it->second.latexName() == name)
269                         return &it->second;
270         return 0;
271 }
272
273
274 Encodings::Encodings()
275 {
276 }
277
278
279 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
280 {
281         // We must read the symbolsfile first, because the Encoding
282         // constructor depends on it.
283         LyXLex symbolslex(0, 0);
284         symbolslex.setFile(symbolsfile);
285         while (symbolslex.isOK()) {
286                 char_type symbol;
287                 CharInfo info;
288                 string flags;
289
290                 if (symbolslex.next(true)) {
291                         std::istringstream is(symbolslex.getString());
292                         // reading symbol directly does not work if
293                         // char_type == std::wchar_t.
294                         boost::uint32_t tmp;
295                         if(!(is >> std::hex >> tmp))
296                                 break;
297                         symbol = tmp;
298                 } else
299                         break;
300                 if (symbolslex.next(true))
301                         info.command = symbolslex.getDocString();
302                 else
303                         break;
304                 if (symbolslex.next(true))
305                         info.preamble = symbolslex.getString();
306                 else
307                         break;
308                 if (symbolslex.next(true))
309                         flags = symbolslex.getString();
310                 else
311                         break;
312
313                 info.combining = false;
314                 info.feature = false;
315                 info.force = false;
316                 while (!flags.empty()) {
317                         string flag;
318                         flags = support::split(flags, flag, ',');
319                         if (flag == "combining")
320                                 info.combining = true;
321                         else if (flag == "force")
322                                 info.force = true;
323                         else
324                                 lyxerr << "Ignoring unknown flag `" << flag
325                                        << "' for symbol `0x" << std::hex
326                                        << symbol << "'." << endl;
327                 }
328
329                 if (!info.preamble.empty())
330                         info.feature = info.preamble[0] != '\\';
331
332                 lyxerr[Debug::INFO]
333                         << "Read unicode symbol " << symbol << " '"
334                         << to_utf8(info.command) << "' '" << info.preamble
335                         << "' " << info.combining << ' ' << info.feature
336                         << endl;
337                 unicodesymbols[symbol] = info;
338         }
339
340         // Now read the encodings
341         enum Encodingtags {
342                 et_encoding = 1,
343                 et_end,
344                 et_last
345         };
346
347         struct keyword_item encodingtags[et_last - 1] = {
348                 { "encoding", et_encoding },
349                 { "end", et_end }
350         };
351
352         LyXLex lex(encodingtags, et_last - 1);
353         lex.setFile(encfile);
354         while (lex.isOK()) {
355                 switch (lex.lex()) {
356                 case et_encoding:
357                 {
358                         lex.next();
359                         string const name = lex.getString();
360                         lex.next();
361                         string const latexname = lex.getString();
362                         lex.next();
363                         string const iconvname = lex.getString();
364                         lyxerr[Debug::INFO] << "Reading encoding " << name << endl;
365                         encodinglist[name] = Encoding(name, latexname, iconvname);
366                         if (lex.lex() != et_end)
367                                 lex.printError("Encodings::read: "
368                                                "missing end");
369                         break;
370                 }
371                 case et_end:
372                         lex.printError("Encodings::read: Misplaced end");
373                         break;
374                 case LyXLex::LEX_FEOF:
375                         break;
376                 default:
377                         lex.printError("Encodings::read: "
378                                        "Unknown tag: `$$Token'");
379                         break;
380                 }
381         }
382 }
383
384
385 } // namespace lyx