]> git.lyx.org Git - lyx.git/blob - src/Encoding.cpp
5df64457708bc36e897d171f54c815cf6f4e378e
[lyx.git] / src / Encoding.cpp
1 /**
2  * \file Encoding.cpp
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 "Buffer.h"
18 #include "BufferList.h"
19 #include "InsetIterator.h"
20 #include "LaTeXFeatures.h"
21 #include "Lexer.h"
22 #include "LyXRC.h"
23
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"
29
30 #include <boost/cstdint.hpp>
31
32 #include <sstream>
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38
39 Encodings encodings;
40
41 Encodings::MathCommandSet Encodings::mathcmd;
42 Encodings::TextCommandSet Encodings::textcmd;
43 Encodings::MathSymbolSet  Encodings::mathsym;
44
45 namespace {
46
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
63
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
80
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
97
98         {0x0650, 0x0650, 0x0650, 0x0650}, // 0x0650 = kasra
99         {0x0651, 0x0651, 0x0651, 0x0651}, // 0x0651 = shadda
100         {0x0652, 0x0652, 0x0652, 0x0652}, // 0x0652 = sukun
101
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  
224 };
225
226
227 char_type const arabic_start = 0x0621;
228 char_type const arabic_end = 0x06cc;
229
230
231 /// Information about a single UCS4 character
232 struct CharInfo {
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
238         string textpreamble;
239         /// Needed LaTeX preamble (or feature) for math mode
240         string mathpreamble;
241         /// Is this a combining character?
242         bool combining;
243         /// Is \c textpreamble a feature known by LaTeXFeatures, or a raw LaTeX
244         /// command?
245         bool textfeature;
246         /// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX
247         /// command?
248         bool mathfeature;
249         /// Always force the LaTeX command, even if the encoding contains
250         /// this character?
251         bool force;
252 };
253
254
255 typedef map<char_type, CharInfo> CharInfoMap;
256 CharInfoMap unicodesymbols;
257
258 typedef std::set<char_type> CharSet;
259 CharSet forced;
260
261 typedef std::set<char_type> MathAlphaSet;
262 MathAlphaSet mathalpha;
263
264
265 /// The highest code point in UCS4 encoding (1<<20 + 1<<16)
266 char_type const max_ucs4 = 0x110000;
267
268 } // namespace anon
269
270
271 EncodingException::EncodingException(char_type c)
272         : failed_char(c), par_id(0), pos(0)
273 {
274 }
275
276
277 const char * EncodingException::what() const throw()
278 {
279         return "Could not find LaTeX command for a character";
280 }
281
282
283 Encoding::Encoding(string const & n, string const & l, string const & g,
284                    string const & i, bool f, Encoding::Package p)
285         : name_(n), latexName_(l), guiName_(g), iconvName_(i), fixedwidth_(f), package_(p)
286 {
287         if (n == "ascii") {
288                 // ASCII can encode 128 code points and nothing else
289                 start_encodable_ = 128;
290                 complete_ = true;
291         } else if (i == "UTF-8") {
292                 // UTF8 can encode all UCS4 code points
293                 start_encodable_ = max_ucs4;
294                 complete_ = true;
295         } else {
296                 complete_ = false;
297         }
298 }
299
300
301 void Encoding::init() const
302 {
303         if (complete_)
304                 return;
305
306         start_encodable_ = 0;
307         // temporarily switch off lyxerr, since we will generate iconv errors
308         lyxerr.disable();
309         if (fixedwidth_) {
310                 // We do not need to check all UCS4 code points, it is enough
311                 // if we check all 256 code points of this encoding.
312                 for (unsigned short j = 0; j < 256; ++j) {
313                         char const c = char(j);
314                         vector<char_type> const ucs4 = eightbit_to_ucs4(&c, 1, iconvName_);
315                         if (ucs4.size() != 1)
316                                 continue;
317                         char_type const uc = ucs4[0];
318                         CharInfoMap::const_iterator const it = unicodesymbols.find(uc);
319                         if (it == unicodesymbols.end() || !it->second.force)
320                                 encodable_.insert(uc);
321                 }
322         } else {
323                 // We do not know how many code points this encoding has, and
324                 // they do not have a direct representation as a single byte,
325                 // therefore we need to check all UCS4 code points.
326                 // This is expensive!
327                 for (char_type c = 0; c < max_ucs4; ++c) {
328                         vector<char> const eightbit = ucs4_to_eightbit(&c, 1, iconvName_);
329                         if (!eightbit.empty()) {
330                                 CharInfoMap::const_iterator const it = unicodesymbols.find(c);
331                                 if (it == unicodesymbols.end() || !it->second.force)
332                                         encodable_.insert(c);
333                         }
334                 }
335         }
336         lyxerr.enable();
337         CharSet::iterator it = encodable_.find(start_encodable_);
338         while (it != encodable_.end()) {
339                 encodable_.erase(it);
340                 ++start_encodable_;
341                 it = encodable_.find(start_encodable_);
342         }
343         complete_ = true;
344 }
345
346
347 docstring Encoding::latexChar(char_type c, bool for_mathed) const
348 {
349         // assure the used encoding is properly initialized
350         init();
351
352         if (iconvName_ == "UTF-8" && package_ == none)
353                 return docstring(1, c);
354         if (c < start_encodable_ && !encodings.isForced(c))
355                 return docstring(1, c);
356         if (encodable_.find(c) != encodable_.end())
357                 return docstring(1, c);
358         if (for_mathed)
359                 return docstring();
360
361         // c cannot (or should not) be encoded in this encoding
362         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
363         if (it == unicodesymbols.end())
364                 throw EncodingException(c);
365         // at least one of mathcommand and textcommand is nonempty
366         if (it->second.textcommand.empty())
367                 return "\\ensuremath{" + it->second.mathcommand + '}';
368         return it->second.textcommand;
369 }
370
371
372 vector<char_type> Encoding::symbolsList() const
373 {
374         // assure the used encoding is properly initialized
375         init();
376
377         // first all encodable characters
378         vector<char_type> symbols(encodable_.begin(), encodable_.end());
379         // add those below start_encodable_
380         for (char_type c = 0; c < start_encodable_; ++c)
381                 symbols.push_back(c);
382         // now the ones from the unicodesymbols file
383         CharInfoMap::const_iterator const end = unicodesymbols.end();
384         CharInfoMap::const_iterator it = unicodesymbols.begin();
385         for (; it != end; ++it)
386                 symbols.push_back(it->first);
387         return symbols;
388 }
389
390
391 bool Encodings::latexMathChar(char_type c, bool mathmode,
392                         Encoding const * encoding, docstring & command)
393 {
394         if (encoding)
395                 command = encoding->latexChar(c, true);
396
397         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
398         if (it == unicodesymbols.end()) {
399                 if (!encoding || command.empty())
400                         throw EncodingException(c);
401                 if (mathmode)
402                         addMathSym(c);
403                 return false;
404         }
405         // at least one of mathcommand and textcommand is nonempty
406         bool use_math = (mathmode && !it->second.mathcommand.empty()) ||
407                         (!mathmode && it->second.textcommand.empty());
408         if (use_math) {
409                 command = it->second.mathcommand;
410                 addMathCmd(c);
411         } else {
412                 if (!encoding || command.empty()) {
413                         command = it->second.textcommand;
414                         addTextCmd(c);
415                 }
416                 if (mathmode)
417                         addMathSym(c);
418         }
419         return use_math;
420 }
421
422
423 char_type Encodings::fromLaTeXCommand(docstring const & cmd, bool & combining)
424 {
425         CharInfoMap::const_iterator const end = unicodesymbols.end();
426         CharInfoMap::const_iterator it = unicodesymbols.begin();
427         for (combining = false; it != end; ++it) {
428                 docstring const math = it->second.mathcommand;
429                 docstring const text = it->second.textcommand;
430                 if (math == cmd || text == cmd) {
431                         combining = it->second.combining;
432                         return it->first;
433                 }
434         }
435         return 0;
436 }
437
438
439 docstring Encodings::fromLaTeXCommand(docstring const & cmd, docstring & rem)
440 {
441         docstring symbols;
442         size_t i = 0;
443         size_t const cmdend = cmd.size();
444         CharInfoMap::const_iterator const uniend = unicodesymbols.end();
445         for (size_t j = 0; j < cmdend; ++j) {
446                 // Also get the char after a backslash
447                 if (j + 1 < cmdend && cmd[j] == '\\')
448                         ++j;
449                 // If a macro argument follows, get it, too
450                 if (j + 1 < cmdend && cmd[j + 1] == '{') {
451                         size_t k = j + 1;
452                         int count = 1;
453                         while (k < cmdend && count && k != docstring::npos) {
454                                 k = cmd.find_first_of(from_ascii("{}"), k + 1);
455                                 if (cmd[k] == '{')
456                                         ++count;
457                                 else
458                                         --count;
459                         }
460                         if (k != docstring::npos)
461                                 j = k;
462                 }
463                 // Start with this substring and try augmenting it when it is
464                 // the prefix of some command in the unicodesymbols file
465                 docstring const subcmd = cmd.substr(i, j - i + 1);
466
467                 CharInfoMap::const_iterator it = unicodesymbols.begin();
468                 size_t unicmd_size = 0;
469                 char_type c = 0;
470                 for (; it != uniend; ++it) {
471                         docstring const math = it->second.mathcommand;
472                         docstring const text = it->second.textcommand;
473                         size_t cur_size = max(math.size(), text.size());
474                         // The current math or text unicode command cannot
475                         // match, or we already matched a longer one
476                         if (cur_size < subcmd.size() || cur_size <= unicmd_size)
477                                 continue;
478
479                         docstring tmp = subcmd;
480                         size_t k = j;
481                         while (prefixIs(math, tmp) || prefixIs(text, tmp)) {
482                                 ++k;
483                                 if (k >= cmdend || cur_size <= tmp.size())
484                                         break;
485                                 tmp += cmd[k];
486                         }
487                         // No match
488                         if (k == j)
489                                 continue;
490
491                         // The last added char caused a mismatch, because
492                         // we didn't exhaust the chars in cmd and didn't
493                         // exceed the maximum size of the current unicmd
494                         if (k < cmdend && cur_size > tmp.size())
495                                 tmp.resize(tmp.size() - 1);
496
497                         // If this is an exact match, we found a (longer)
498                         // matching entry in the unicodesymbols file.
499                         // If the entry doesn't start with '\', we take note
500                         // of the match and continue (this is not a ultimate
501                         // acceptance, as some other entry may match a longer
502                         // portion of the cmd string). However, if the entry
503                         // does start with '\', we accept the match only if
504                         // this is a valid macro, i.e., either it is a single
505                         // (nonletter) char macro, or nothing else follows,
506                         // or what follows is a nonletter char, or the last
507                         // character is a }.
508                         if ((math == tmp || text == tmp)
509                             && (tmp[0] != '\\'
510                                    || (tmp.size() == 2 && !isAlphaASCII(tmp[1]))
511                                    || k == cmdend 
512                                    || !isAlphaASCII(cmd[k])
513                                    || tmp[tmp.size() - 1] == '}')
514                                  ) {
515                                 c = it->first;
516                                 j = k - 1;
517                                 i = j + 1;
518                                 unicmd_size = cur_size;
519                         }
520                 }
521                 if (unicmd_size)
522                         symbols += c;
523                 else if (j + 1 == cmdend)
524                         // No luck. Return what remains
525                         rem = cmd.substr(i);
526         }
527         return symbols;
528 }
529
530
531 void Encodings::initUnicodeMath(Buffer const & buffer, bool clear_sets)
532 {
533 #ifdef TEX2LYX
534         // The code below is not needed in tex2lyx and requires additional stuff
535         (void)buffer;
536 #else
537         if (clear_sets) {
538                 mathcmd.clear();
539                 textcmd.clear();
540                 mathsym.clear();
541         }
542
543         // Check master
544         Inset & inset = buffer.inset();
545         InsetIterator it = inset_iterator_begin(inset);
546         InsetIterator const end = inset_iterator_end(inset);
547         for (; it != end; ++it)
548                 it->initUnicodeMath();
549
550         // Check children
551         BufferList::iterator bit = theBufferList().begin();
552         BufferList::iterator const bend = theBufferList().end();
553         for (; bit != bend; ++bit)
554                 if (buffer.isChild(*bit))
555                         initUnicodeMath(**bit, false);
556 #endif
557 }
558
559
560 void Encodings::validate(char_type c, LaTeXFeatures & features, bool for_mathed)
561 {
562 #ifdef TEX2LYX
563         // The code below is not needed in tex2lyx and requires additional stuff
564         (void)c;
565         (void)features;
566         (void)for_mathed;
567 #else
568         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
569         if (it != unicodesymbols.end()) {
570                 // In mathed, c could be used both in textmode and mathmode
571                 bool const use_math = (for_mathed && isMathCmd(c)) ||
572                                       (!for_mathed && it->second.textcommand.empty());
573                 bool const use_text = (for_mathed && isTextCmd(c)) ||
574                                       (!for_mathed && !it->second.textcommand.empty());
575                 if (use_math) {
576                         if (!it->second.mathpreamble.empty()) {
577                                 if (it->second.mathfeature) {
578                                         string feats = it->second.mathpreamble;
579                                         while (!feats.empty()) {
580                                                 string feat;
581                                                 feats = split(feats, feat, ',');
582                                                 features.require(feat);
583                                         }
584                                 } else
585                                         features.addPreambleSnippet(it->second.mathpreamble);
586                         }
587                 }
588                 if (use_text) {
589                         if (!it->second.textpreamble.empty()) {
590                                 if (it->second.textfeature) {
591                                         string feats = it->second.textpreamble;
592                                         while (!feats.empty()) {
593                                                 string feat;
594                                                 feats = split(feats, feat, ',');
595                                                 features.require(feat);
596                                         }
597                                 } else
598                                         features.addPreambleSnippet(it->second.textpreamble);
599                         }
600                 }
601         }
602         if (for_mathed && isMathSym(c)) {
603                 features.require("relsize");
604                 features.require("lyxmathsym");
605         }
606 #endif
607 }
608
609
610 bool Encodings::isHebrewComposeChar(char_type c)
611 {
612         return c <= 0x05c2 && c >= 0x05b0 && c != 0x05be && c != 0x05c0;
613 }
614
615
616 // Special Arabic letters are ones that do not get connected from left
617 // they are hamza, alef_madda, alef_hamza, waw_hamza, alef_hamza_under,
618 // alef, tah_marbota, dal, thal, rah, zai, wow, alef_maksoura
619
620 bool Encodings::isArabicSpecialChar(char_type c)
621 {
622         return (c >= 0x0621 && c <= 0x0625) || (c >= 0x0630 && c <= 0x0632)
623                 || c == 0x0627 || c == 0x0629 || c == 0x062f || c == 0x0648
624                 || c == 0x0649 || c == 0x0698;
625 }
626
627
628 bool Encodings::isArabicComposeChar(char_type c)
629 {
630         return c >= 0x064b && c <= 0x0652;
631 }
632
633
634 bool Encodings::isArabicChar(char_type c)
635 {
636         return c >= arabic_start && c <= arabic_end
637                 && arabic_table[c-arabic_start][0];
638 }
639
640
641 char_type Encodings::transformChar(char_type c, Encodings::LetterForm form)
642 {
643         return isArabicChar(c) ? arabic_table[c-arabic_start][form] : c;
644 }
645
646
647 bool Encodings::isCombiningChar(char_type c)
648 {
649         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
650         if (it != unicodesymbols.end())
651                 return it->second.combining;
652         return false;
653 }
654
655
656 bool Encodings::isKnownScriptChar(char_type const c, string & preamble)
657 {
658         CharInfoMap::const_iterator const it = unicodesymbols.find(c);
659
660         if (it == unicodesymbols.end())
661                 return false;
662
663         if (it->second.textpreamble != "textgreek" && it->second.textpreamble != "textcyr")
664                 return false;
665
666         if (preamble.empty()) {
667                 preamble = it->second.textpreamble;
668                 return true;
669         }
670         return it->second.textpreamble == preamble;
671 }
672
673
674 bool Encodings::isForced(char_type c)
675 {
676         return (!forced.empty() && forced.find(c) != forced.end());
677 }
678
679
680 bool Encodings::isMathAlpha(char_type c)
681 {
682         return mathalpha.count(c);
683 }
684
685
686 Encoding const * Encodings::fromLyXName(string const & name) const
687 {
688         EncodingList::const_iterator const it = encodinglist.find(name);
689         return it != encodinglist.end() ? &it->second : 0;
690 }
691
692
693 Encoding const * Encodings::fromLaTeXName(string const & name) const
694 {
695         // We don't use find_if because it makes copies of the pairs in
696         // the map.
697         // This linear search is OK since we don't have many encodings.
698         // Users could even optimize it by putting the encodings they use
699         // most at the top of lib/encodings.
700         EncodingList::const_iterator const end = encodinglist.end();
701         for (EncodingList::const_iterator it = encodinglist.begin(); it != end; ++it)
702                 if (it->second.latexName() == name)
703                         return &it->second;
704         return 0;
705 }
706
707
708 Encodings::Encodings()
709 {
710 }
711
712
713 void Encodings::read(FileName const & encfile, FileName const & symbolsfile)
714 {
715         // We must read the symbolsfile first, because the Encoding
716         // constructor depends on it.
717         Lexer symbolslex;
718         symbolslex.setFile(symbolsfile);
719         bool getNextToken = true;
720         while (symbolslex.isOK()) {
721                 char_type symbol;
722                 CharInfo info;
723                 string flags;
724
725                 if (getNextToken) {
726                         if (!symbolslex.next(true))
727                                 break;
728                 } else
729                         getNextToken = true;
730
731                 istringstream is(symbolslex.getString());
732                 // reading symbol directly does not work if
733                 // char_type == wchar_t.
734                 boost::uint32_t tmp;
735                 if(!(is >> hex >> tmp))
736                         break;
737                 symbol = tmp;
738
739                 if (!symbolslex.next(true))
740                         break;
741                 info.textcommand = symbolslex.getDocString();
742                 if (!symbolslex.next(true))
743                         break;
744                 info.textpreamble = symbolslex.getString();
745                 if (!symbolslex.next(true))
746                         break;
747                 flags = symbolslex.getString();
748
749                 info.combining = false;
750                 info.textfeature = false;
751                 info.force = false;
752                 while (!flags.empty()) {
753                         string flag;
754                         flags = split(flags, flag, ',');
755                         if (flag == "combining") {
756                                 info.combining = true;
757                         } else if (flag == "force") {
758                                 info.force = true;
759                                 forced.insert(symbol);
760                         } else if (flag == "mathalpha") {
761                                 mathalpha.insert(symbol);
762                         } else {
763                                 lyxerr << "Ignoring unknown flag `" << flag
764                                        << "' for symbol `0x"
765                                        << hex << symbol << dec
766                                        << "'." << endl;
767                         }
768                 }
769                 // mathcommand and mathpreamble have been added for 1.6.0.
770                 // make them optional so that old files still work.
771                 int const lineno = symbolslex.lineNumber();
772                 bool breakout = false;
773                 if (symbolslex.next(true)) {
774                         if (symbolslex.lineNumber() != lineno) {
775                                 // line in old format without mathcommand and mathpreamble
776                                 getNextToken = false;
777                         } else {
778                                 info.mathcommand = symbolslex.getDocString();
779                                 if (symbolslex.next(true)) {
780                                         if (symbolslex.lineNumber() != lineno) {
781                                                 // line in new format with mathcommand only
782                                                 getNextToken = false;
783                                         } else {
784                                                 // line in new format with mathcommand and mathpreamble
785                                                 info.mathpreamble = symbolslex.getString();
786                                         }
787                                 } else
788                                         breakout = true;
789                         }
790                 } else {
791                         breakout = true;
792                 }
793
794                 if (!info.textpreamble.empty())
795                         info.textfeature = info.textpreamble[0] != '\\';
796                 if (!info.mathpreamble.empty())
797                         info.mathfeature = info.mathpreamble[0] != '\\';
798
799                 LYXERR(Debug::INFO, "Read unicode symbol " << symbol << " '"
800                         << to_utf8(info.textcommand) << "' '" << info.textpreamble
801                         << "' " << info.combining << ' ' << info.textfeature
802                         << " '" << to_utf8(info.mathcommand) << "' '"
803                         << info.mathpreamble << "' " << info.mathfeature);
804
805                 // we assume that at least one command is nonempty when using unicodesymbols
806                 if (!info.textcommand.empty() || !info.mathcommand.empty())
807                         unicodesymbols[symbol] = info;
808
809                 if (breakout)
810                         break;
811         }
812
813         // Now read the encodings
814         enum {
815                 et_encoding = 1,
816                 et_end,
817         };
818
819         LexerKeyword encodingtags[] = {
820                 { "encoding", et_encoding },
821                 { "end", et_end }
822         };
823
824         Lexer lex(encodingtags);
825         lex.setFile(encfile);
826         lex.setContext("Encodings::read");
827         while (lex.isOK()) {
828                 switch (lex.lex()) {
829                 case et_encoding:
830                 {
831                         lex.next();
832                         string const name = lex.getString();
833                         lex.next();
834                         string const latexname = lex.getString();
835                         lex.next();
836                         string const guiname = lex.getString();
837                         lex.next();
838                         string const iconvname = lex.getString();
839                         lex.next();
840                         string const width = lex.getString();
841                         bool fixedwidth = false;
842                         if (width == "fixed")
843                                 fixedwidth = true;
844                         else if (width == "variable")
845                                 fixedwidth = false;
846                         else
847                                 lex.printError("Unknown width");
848
849                         lex.next();
850                         string const p = lex.getString();
851                         Encoding::Package package = Encoding::none;
852                         if (p == "none")
853                                 package = Encoding::none;
854                         else if (p == "inputenc")
855                                 package = Encoding::inputenc;
856                         else if (p == "CJK")
857                                 package = Encoding::CJK;
858                         else if (p == "japanese")
859                                 package = Encoding::japanese;
860                         else
861                                 lex.printError("Unknown package");
862
863                         LYXERR(Debug::INFO, "Reading encoding " << name);
864                         encodinglist[name] = Encoding(name, latexname,
865                                 guiname, iconvname, fixedwidth, package);
866
867                         if (lex.lex() != et_end)
868                                 lex.printError("Missing end");
869                         break;
870                 }
871                 case et_end:
872                         lex.printError("Misplaced end");
873                         break;
874                 case Lexer::LEX_FEOF:
875                         break;
876                 default:
877                         lex.printError("Unknown tag");
878                         break;
879                 }
880         }
881 }
882
883
884 } // namespace lyx