]> git.lyx.org Git - lyx.git/blob - src/Encoding.h
e0480df98c46bc5f15ecbb4627e1bb9331a46924
[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, bool u, 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 hasFixedWidth() const { return fixedwidth_; }
70         ///
71         bool unsafe() const { return unsafe_; }
72         /// \p c is representable in this encoding without a LaTeX macro
73         bool encodable(char_type c) const;
74         /**
75          * Convert \p c to something that LaTeX can understand.
76          * This is either the character itself (if it is representable
77          * in this encoding), or a LaTeX macro.
78          * If the character is not representable in this encoding, but no
79          * LaTeX macro is known, a warning is given of lyxerr, and the
80          * character is returned.
81          * \return the converted character and a flag indicating whether
82          * the command needs to be terminated by {} or a space.
83          */
84         std::pair<docstring, bool> latexChar(char_type c) const;
85         /**
86          * Convert \p input to something that LaTeX can understand.
87          * This is either the string itself (if it is representable
88          * in this encoding), or a LaTeX macro.
89          * If a character is not representable in this encoding, but no
90          * LaTeX macro is known, a warning is given of lyxerr, and the
91          * character is returned in the second string of the pair and
92          * omitted in the first.
93          * \p dryrun specifies whether the string is used within source
94          * preview (which yields a special warning).
95          */
96         std::pair<docstring, docstring> latexString(docstring const input,
97                                                     bool dryrun = false) const;
98         /// Which LaTeX package handles this encoding?
99         Package package() const { return package_; }
100         /// A list of all characters usable in this encoding
101         std::vector<char_type> symbolsList() const;
102 private:
103         /**
104          * Do we have to output this character as LaTeX command in any case?
105          * This is true if the "force" flag is set.
106          * We need this if the inputencoding does not support a certain glyph.
107          */
108         bool isForced(char_type c) const;
109         ///
110         std::string name_;
111         ///
112         std::string latexName_;
113         ///
114         std::string guiName_;
115         ///
116         std::string iconvName_;
117         /// Is this a fixed width encoding?
118         bool fixedwidth_;
119         /// Is this encoding TeX unsafe, e.g. control characters like {, }
120         /// and \\ may appear in high bytes?
121         bool unsafe_;
122         ///
123         typedef std::set<char_type> CharSet;
124         /// Set of UCS4 characters that we can encode (for singlebyte
125         /// encodings only)
126         mutable CharSet encodable_;
127         /// Set of UCS4 characters that we can't encode
128         CharSet const * forced_;
129         /// All code points below this are encodable. This helps us to avoid
130         /// lokup of ASCII characters in encodable_ and gives about 1 sec
131         /// speedup on export of the Userguide.
132         mutable char_type start_encodable_;
133         /// Which LaTeX package handles this encoding?
134         Package package_;
135         /**
136          * If this is true the stored information about the encoding covers
137          * all encodable characters. We set this to false initially so that
138          * we only need to query iconv for the actually used encodings.
139          * This is needed especially for the multibyte encodings, if we
140          * complete all encoding info on startup it takes 2-3 minutes.
141          */
142         mutable bool complete_;
143 };
144
145 class Encodings {
146 public:
147         ///
148         typedef std::set<char_type> MathCommandSet;
149         ///
150         typedef std::set<char_type> TextCommandSet;
151         ///
152         typedef std::set<char_type> MathSymbolSet;
153         ///
154         typedef std::map<std::string, Encoding> EncodingList;
155         /// iterator to iterate over all encodings.
156         /// We hide the fact that our encoding list is implemented as a map.
157         class const_iterator : public EncodingList::const_iterator {
158                 typedef EncodingList::const_iterator base;
159         public:
160                 const_iterator() : base() {}
161                 const_iterator(base const & b) : base(b) {}
162                 Encoding const & operator*() const { return base::operator*().second; }
163                 Encoding const * operator->() const { return &(base::operator*().second); }
164         };
165         ///
166         Encodings();
167         /// Read the encodings.
168         /// \param encfile encodings definition file
169         /// \param symbolsfile unicode->LaTeX mapping file
170         void read(support::FileName const & encfile,
171                   support::FileName const & symbolsfile);
172         /// Get encoding from LyX name \p name
173         Encoding const *
174         fromLyXName(std::string const & name, bool allowUnsafe = false) const;
175         /// Get encoding from LaTeX name \p name
176         Encoding const *
177         fromLaTeXName(std::string const & name, bool allowUnsafe = false) const;
178
179         ///
180         const_iterator begin() const { return encodinglist.begin(); }
181         ///
182         const_iterator end() const { return encodinglist.end(); }
183
184         ///
185         enum LetterForm {
186                 ///
187                 FORM_ISOLATED,
188                 ///
189                 FORM_FINAL,
190                 ///
191                 FORM_INITIAL,
192                 ///
193                 FORM_MEDIAL
194         };
195         ///
196         static bool isHebrewComposeChar(char_type c);
197         ///
198         static bool isArabicComposeChar(char_type c);
199         ///
200         static bool isArabicSpecialChar(char_type c);
201         ///
202         static bool isArabicChar(char_type c);
203         ///
204         static char_type transformChar(char_type c, LetterForm form);
205         /// Is this a combining char?
206         static bool isCombiningChar(char_type c);
207         /// Return the TIPA shortcut
208         static std::string const TIPAShortcut(char_type c);
209         /**
210          * Is this a known char from some language?
211          * If \p preamble is empty and code point \p c is known to belong
212          * to a supported script, true is returned and \p preamble is set
213          * to the corresponding entry in the unicodesymbols file.
214          * If \p preamble is not empty, a check is made whether code point
215          * \p c is a known character matching the preamble entry.
216          */
217         static bool isKnownScriptChar(char_type const c, std::string & preamble);
218         /**
219          * Do we have to display in italics this character when in mathmode?
220          * This is true if the "mathalpha" flag is set. We use this for
221          * letters and accented characters that are output as math commands.
222          */
223         static bool isMathAlpha(char_type c);
224         /**
225          * Register \p c as a mathmode command.
226          */
227         static void addMathCmd(char_type c) { mathcmd.insert(c); }
228         /**
229          * Register \p c as a textmode command.
230          */
231         static void addTextCmd(char_type c) { textcmd.insert(c); }
232         /**
233          * Register \p c as a mathmode symbol.
234          */
235         static void addMathSym(char_type c) { mathsym.insert(c); }
236         /**
237          * Tell whether \p c is registered as a mathmode command.
238          */
239         static bool isMathCmd(char_type c) { return mathcmd.count(c); }
240         /**
241          * Tell whether \p c is registered as a textmode command.
242          */
243         static bool isTextCmd(char_type c) { return textcmd.count(c); }
244         /**
245          * Tell whether \p c is registered as a mathmode symbol.
246          */
247         static bool isMathSym(char_type c) { return mathsym.count(c); }
248         /**
249          * Initialize mathcmd, textcmd, and mathsym sets.
250          */
251         static void initUnicodeMath(Buffer const & buffer, bool for_master = true);
252         /**
253          * If \p c cannot be encoded in the given \p encoding, convert
254          * it to something that LaTeX can understand in mathmode.
255          * \p needsTermination indicates whether the command needs to be
256          * terminated by {} or a space.
257          * \return whether \p command is a mathmode command
258          */
259         static bool latexMathChar(char_type c, bool mathmode,
260                         Encoding const * encoding, docstring & command,
261                         bool & needsTermination);
262         /**
263          * Convert the LaTeX command in \p cmd to the corresponding unicode
264          * point and set \p combining to true if it is a combining symbol.
265          * \p needsTermination indicates whether the command needs to be
266          * terminated by {} or a space.
267          */
268         static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
269                         bool & combining, bool & needsTermination,
270                         std::set<std::string> * req = 0);
271         ///
272         enum LatexCmd {
273                 ///
274                 MATH_CMD = 1,
275                 ///
276                 TEXT_CMD = 2
277         };
278         /**
279          * Convert the LaTeX commands in \p cmd and \return a docstring
280          * of corresponding unicode points. The conversion stops at the
281          * first command which could not be converted, and the remaining
282          * unconverted commands are returned in \p rem.
283          * The \p cmdtype parameter can be used to limit recognized
284          * commands to math or text mode commands only.
285          * \p needsTermination indicates whether the command needs to be
286          * terminated by {} or a space.
287          */
288         static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
289                         bool & needsTermination, docstring & rem,
290                         std::set<std::string> * req = 0);
291         /**
292          * Add the preamble snippet needed for the output of \p c to
293          * \p features.
294          * This does not depend on the used encoding, since the inputenc
295          * package only maps the code point \p c to a command, it does not
296          * make this command available.
297          */
298         static void validate(char_type c, LaTeXFeatures & features, bool for_mathed = false);
299
300 private:
301         ///
302         EncodingList encodinglist;
303         ///
304         static MathCommandSet mathcmd;
305         ///
306         static TextCommandSet textcmd;
307         ///
308         static MathSymbolSet mathsym;
309 };
310
311 extern Encodings encodings;
312
313
314 } // namespace lyx
315
316 #endif