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