]> git.lyx.org Git - lyx.git/blob - src/Encoding.h
Merge branch 'master' of lyx:lyx
[lyx.git] / src / Encoding.h
1 // -*- C++ -*-
2 /**
3  * \file Encoding.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef ENCODING_H
14 #define ENCODING_H
15
16 #include "support/docstring.h"
17 #include "support/types.h"
18
19 #include <map>
20 #include <set>
21 #include <vector>
22
23 namespace lyx {
24
25 namespace support { class FileName; }
26
27 class Buffer;
28 class LaTeXFeatures;
29
30 class EncodingException : public std::exception {
31 public:
32         EncodingException(char_type c);
33         virtual ~EncodingException() throw() {}
34         virtual const char * what() const throw();
35  
36         char_type failed_char;
37         int par_id;
38         pos_type pos;
39 };
40
41
42 ///
43 class Encoding {
44 public:
45         /// Which LaTeX package handles this encoding?
46         enum Package {
47                 none,
48                 inputenc,
49                 CJK,
50                 japanese
51         };
52         ///
53         Encoding() {}
54         ///
55         Encoding(std::string const & n, std::string const & l,
56                  std::string const & g, std::string const & i,
57                  bool f, Package p);
58         ///
59         void init() const;
60         ///
61         std::string const & name() const { return name_; }
62         ///
63         std::string const & latexName() const { return latexName_; }
64         ///
65         std::string const & guiName() const { return guiName_; }
66         ///
67         std::string const & iconvName() const { return iconvName_; }
68         ///
69         bool const & hasFixedWidth() const { return fixedwidth_; }
70         /// \p c is representable in this encoding without a LaTeX macro
71         bool encodable(char_type c) const;
72         /**
73          * Convert \p c to something that LaTeX can understand.
74          * This is either the character itself (if it is representable
75          * in this encoding), or a LaTeX macro.
76          * If the character is not representable in this encoding, but no
77          * LaTeX macro is known, a warning is given of lyxerr, and the
78          * character is returned.
79          * \return the converted character and a flag indicating whether
80          * the command needs to be terminated by {} or a space.
81          */
82         std::pair<docstring, bool> latexChar(char_type c) const;
83         /// Which LaTeX package handles this encoding?
84         Package package() const { return package_; }
85         /// A list of all characters usable in this encoding
86         std::vector<char_type> symbolsList() const;
87 private:
88         ///
89         std::string name_;
90         ///
91         std::string latexName_;
92         ///
93         std::string guiName_;
94         ///
95         std::string iconvName_;
96         /// Is this a fixed width encoding?
97         bool fixedwidth_;
98         ///
99         typedef std::set<char_type> CharSet;
100         /// Set of UCS4 characters that we can encode (for singlebyte
101         /// encodings only)
102         mutable CharSet encodable_;
103         /// All code points below this are encodable. This helps us to avoid
104         /// lokup of ASCII characters in encodable_ and gives about 1 sec
105         /// speedup on export of the Userguide.
106         mutable char_type start_encodable_;
107         /// Which LaTeX package handles this encoding?
108         Package package_;
109         /**
110          * If this is true the stored information about the encoding covers
111          * all encodable characters. We set this to false initially so that
112          * we only need to query iconv for the actually used encodings.
113          * This is needed especially for the multibyte encodings, if we
114          * complete all encoding info on startup it takes 2-3 minutes.
115          */
116         mutable bool complete_;
117 };
118
119 class Encodings {
120 public:
121         ///
122         typedef std::set<char_type> MathCommandSet;
123         ///
124         typedef std::set<char_type> TextCommandSet;
125         ///
126         typedef std::set<char_type> MathSymbolSet;
127         ///
128         typedef std::map<std::string, Encoding> EncodingList;
129         /// iterator to iterate over all encodings.
130         /// We hide the fact that our encoding list is implemented as a map.
131         class const_iterator : public EncodingList::const_iterator {
132                 typedef EncodingList::const_iterator base;
133         public:
134                 const_iterator() : base() {}
135                 const_iterator(base const & b) : base(b) {}
136                 Encoding const & operator*() const { return base::operator*().second; }
137                 Encoding const * operator->() const { return &(base::operator*().second); }
138         };
139         ///
140         Encodings();
141         /// Read the encodings.
142         /// \param encfile encodings definition file
143         /// \param symbolsfile unicode->LaTeX mapping file
144         void read(support::FileName const & encfile,
145                   support::FileName const & symbolsfile);
146         /// Get encoding from LyX name \p name
147         Encoding const * fromLyXName(std::string const & name) const;
148         /// Get encoding from LaTeX name \p name
149         Encoding const * fromLaTeXName(std::string const & name) const;
150
151         ///
152         const_iterator begin() const { return encodinglist.begin(); }
153         ///
154         const_iterator end() const { return encodinglist.end(); }
155
156         ///
157         enum LetterForm {
158                 ///
159                 FORM_ISOLATED,
160                 ///
161                 FORM_FINAL,
162                 ///
163                 FORM_INITIAL,
164                 ///
165                 FORM_MEDIAL
166         };
167         ///
168         static bool isHebrewComposeChar(char_type c);
169         ///
170         static bool isArabicComposeChar(char_type c);
171         ///
172         static bool isArabicSpecialChar(char_type c);
173         ///
174         static bool isArabicChar(char_type c);
175         ///
176         static char_type transformChar(char_type c, LetterForm form);
177         /// Is this a combining char?
178         static bool isCombiningChar(char_type c);
179         /// Return the TIPA shortcut
180         static std::string const TIPAShortcut(char_type c);
181         /**
182          * Is this a known char from some language?
183          * If \p preamble is empty and code point \p c is known to belong
184          * to a supported script, true is returned and \p preamble is set
185          * to the corresponding entry in the unicodesymbols file.
186          * If \p preamble is not empty, a check is made whether code point
187          * \p c is a known character matching the preamble entry.
188          */
189         static bool isKnownScriptChar(char_type const c, std::string & preamble);
190         /**
191          * Do we have to output this character as LaTeX command in any case?
192          * This is true if the "force" flag is set.
193          * We need this if the inputencoding does not support a certain glyph.
194          */
195         static bool isForced(char_type c);
196         /**
197          * Do we have to display in italics this character when in mathmode?
198          * This is true if the "mathalpha" flag is set. We use this for
199          * letters and accented characters that are output as math commands.
200          */
201         static bool isMathAlpha(char_type c);
202         /**
203          * Register \p c as a mathmode command.
204          */
205         static void addMathCmd(char_type c) { mathcmd.insert(c); }
206         /**
207          * Register \p c as a textmode command.
208          */
209         static void addTextCmd(char_type c) { textcmd.insert(c); }
210         /**
211          * Register \p c as a mathmode symbol.
212          */
213         static void addMathSym(char_type c) { mathsym.insert(c); }
214         /**
215          * Tell whether \p c is registered as a mathmode command.
216          */
217         static bool isMathCmd(char_type c) { return mathcmd.count(c); }
218         /**
219          * Tell whether \p c is registered as a textmode command.
220          */
221         static bool isTextCmd(char_type c) { return textcmd.count(c); }
222         /**
223          * Tell whether \p c is registered as a mathmode symbol.
224          */
225         static bool isMathSym(char_type c) { return mathsym.count(c); }
226         /**
227          * Initialize mathcmd, textcmd, and mathsym sets.
228          */
229         static void initUnicodeMath(Buffer const & buffer, bool for_master = true);
230         /**
231          * If \p c cannot be encoded in the given \p encoding, convert
232          * it to something that LaTeX can understand in mathmode.
233          * \p needsTermination indicates whether the command needs to be
234          * terminated by {} or a space.
235          * \return whether \p command is a mathmode command
236          */
237         static bool latexMathChar(char_type c, bool mathmode,
238                         Encoding const * encoding, docstring & command,
239                         bool & needsTermination);
240         /**
241          * Convert the LaTeX command in \p cmd to the corresponding unicode
242          * point and set \p combining to true if it is a combining symbol.
243          * \p needsTermination indicates whether the command needs to be
244          * terminated by {} or a space.
245          */
246         static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
247                         bool & combining, bool & needsTermination,
248                         std::set<std::string> * req = 0);
249         ///
250         enum LatexCmd {
251                 ///
252                 MATH_CMD = 1,
253                 ///
254                 TEXT_CMD = 2
255         };
256         /**
257          * Convert the LaTeX commands in \p cmd and \return a docstring
258          * of corresponding unicode points. The conversion stops at the
259          * first command which could not be converted, and the remaining
260          * unconverted commands are returned in \p rem.
261          * The \p cmdtype parameter can be used to limit recognized
262          * commands to math or text mode commands only.
263          * \p needsTermination indicates whether the command needs to be
264          * terminated by {} or a space.
265          */
266         static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
267                         bool & needsTermination, docstring & rem,
268                         std::set<std::string> * req = 0);
269         /**
270          * Add the preamble snippet needed for the output of \p c to
271          * \p features.
272          * This does not depend on the used encoding, since the inputenc
273          * package only maps the code point \p c to a command, it does not
274          * make this command available.
275          */
276         static void validate(char_type c, LaTeXFeatures & features, bool for_mathed = false);
277
278 private:
279         ///
280         EncodingList encodinglist;
281         ///
282         static MathCommandSet mathcmd;
283         ///
284         static TextCommandSet textcmd;
285         ///
286         static MathSymbolSet mathsym;
287 };
288
289 extern Encodings encodings;
290
291
292 } // namespace lyx
293
294 #endif