]> git.lyx.org Git - lyx.git/blob - src/Encoding.h
tex2lyx/TODO.txt: add a feature that is not yet supported
[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/trivstring.h"
18 #include "support/types.h"
19
20 #include <map>
21 #include <set>
22 #include <vector>
23
24 namespace lyx {
25
26 namespace support { class FileName; }
27
28 class EncodingException : public std::exception {
29 public:
30         EncodingException(char_type c);
31         virtual ~EncodingException() throw() {}
32         virtual const char * what() const throw();
33
34         char_type failed_char;
35         int par_id;
36         pos_type pos;
37 };
38
39
40 enum CharInfoFlags {
41         ///
42         CharInfoCombining = 1,
43         ///
44         CharInfoTextFeature = 2,
45         ///
46         CharInfoMathFeature = 4,
47         ///
48         CharInfoForce = 8,
49         ///
50         CharInfoTextNoTermination = 16,
51         ///
52         CharInfoMathNoTermination = 32,
53         ///
54         CharInfoForceSelected = 64,
55 };
56
57
58 /// Information about a single UCS4 character
59 class CharInfo {
60 public:
61         CharInfo() : flags_(0) {}
62         CharInfo(
63                 docstring const & textcommand, docstring const & mathcommand,
64                 std::string const & textpreamble, std::string const & mathpreamble,
65                 std::string const & tipashortcut, unsigned int flags);
66         // we assume that at least one command is nonempty when using unicodesymbols
67         bool isUnicodeSymbol() const { return !textcommand_.empty() || !mathcommand_.empty(); }
68         /// LaTeX command (text mode) for this character
69         docstring const textcommand() const { return textcommand_; }
70         /// LaTeX command (math mode) for this character
71         docstring mathcommand() const { return mathcommand_; }
72         /// Needed LaTeX preamble (or feature) for text mode
73         std::string textpreamble() const { return textpreamble_; }
74         /// Needed LaTeX preamble (or feature) for math mode
75         std::string mathpreamble() const { return mathpreamble_; }
76         /// Is this a combining character?
77         bool combining() const { return flags_ & CharInfoCombining ? true : false; }
78         /// Is \c textpreamble a feature known by LaTeXFeatures, or a raw LaTeX
79         /// command?
80         bool textfeature() const { return flags_ & CharInfoTextFeature ? true : false; }
81         /// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX
82         /// command?
83         bool mathfeature() const { return flags_ & CharInfoMathFeature ? true : false; }
84         /// Always force the LaTeX command, even if the encoding contains
85         /// this character?
86         bool force() const { return flags_ & CharInfoForce ? true : false; }
87         /// Force the LaTeX command for some encodings?
88         bool forceselected() const { return flags_ & CharInfoForceSelected ? true : false; }
89         /// TIPA shortcut
90         std::string const tipashortcut() const { return tipashortcut_; }
91         /// \c textcommand needs no termination (such as {} or space).
92         bool textnotermination() const { return flags_ & CharInfoTextNoTermination ? true : false; }
93         /// \c mathcommand needs no termination (such as {} or space).
94         bool mathnotermination() const { return flags_ & CharInfoMathNoTermination ? true : false; }
95         ///
96 private:
97         /// LaTeX command (text mode) for this character
98         trivdocstring textcommand_;
99         /// LaTeX command (math mode) for this character
100         trivdocstring mathcommand_;
101         /// Needed LaTeX preamble (or feature) for text mode
102         trivstring textpreamble_;
103         /// Needed LaTeX preamble (or feature) for math mode
104         trivstring mathpreamble_;
105         /// TIPA shortcut
106         trivstring tipashortcut_;
107         /// feature flags
108         unsigned int flags_;
109 };
110
111
112 /**
113  * An encoding as defined in lib/encodings.
114  * All const methods are thread-safe, so the caller does not need any locking.
115  * This property must be kept when changing the class.
116  */
117 class Encoding {
118 public:
119         /// Which LaTeX package handles this encoding?
120         enum Package {
121                 none = 1,
122                 inputenc = 2,
123                 CJK = 4,
124                 japanese = 8
125         };
126         /// Represent any of the above packages
127         static int const any;
128         ///
129         Encoding() : fixedwidth_(true), unsafe_(false), complete_(false) {}
130         ///
131         Encoding(std::string const & n, std::string const & l,
132                  std::string const & g, std::string const & i,
133                  bool f, bool u, Package p);
134         ///
135         void init() const;
136         ///
137         std::string const name() const { return name_; }
138         ///
139         std::string const latexName() const { return latexName_; }
140         ///
141         std::string const guiName() const { return guiName_; }
142         ///
143         std::string const iconvName() const { return iconvName_; }
144         ///
145         bool hasFixedWidth() const { return fixedwidth_; }
146         ///
147         bool unsafe() const { return unsafe_; }
148         /// \p c is representable in this encoding without a LaTeX macro
149         bool encodable(char_type c) const;
150         /**
151          * Convert \p c to something that LaTeX can understand.
152          * This is either the character itself (if it is representable
153          * in this encoding), or a LaTeX macro.
154          * If the character is not representable in this encoding, but no
155          * LaTeX macro is known, a warning is given of lyxerr, and the
156          * character is returned.
157          * \return the converted character and a flag indicating whether
158          * the command needs to be terminated by {} or a space.
159          */
160         std::pair<docstring, bool> latexChar(char_type c) const;
161         /**
162          * Convert \p input to something that LaTeX can understand.
163          * This is either the string itself (if it is representable
164          * in this encoding), or a LaTeX macro.
165          * If a character is not representable in this encoding, but no
166          * LaTeX macro is known, a warning is given of lyxerr, and the
167          * character is returned in the second string of the pair and
168          * omitted in the first.
169          * \p dryrun specifies whether the string is used within source
170          * preview (which yields a special warning).
171          */
172         std::pair<docstring, docstring> latexString(docstring const & input,
173                                                     bool dryrun = false) const;
174         /// Which LaTeX package handles this encoding?
175         Package package() const { return package_; }
176         /// A list of all characters usable in this encoding
177         std::vector<char_type> symbolsList() const;
178 private:
179         /**
180          * Do we have to output this character as LaTeX command in any case?
181          * This is true if the "force" flag is set.
182          * We need this if the inputencoding does not support a certain glyph.
183          */
184         bool isForced(char_type c) const;
185         ///
186         trivstring name_;
187         ///
188         trivstring latexName_;
189         ///
190         trivstring guiName_;
191         ///
192         trivstring iconvName_;
193         /// Is this a fixed width encoding?
194         bool fixedwidth_;
195         /// Is this encoding TeX unsafe, e.g. control characters like {, }
196         /// and \\ may appear in high bytes?
197         bool unsafe_;
198         ///
199         typedef std::set<char_type> CharSet;
200         /// Set of UCS4 characters that we can encode (for singlebyte
201         /// encodings only)
202         CharSet encodable_;
203         /// Set of UCS4 characters that we can't encode
204         CharSet const * forced_;
205         /// All code points below this are encodable. This helps us to avoid
206         /// lokup of ASCII characters in encodable_ and gives about 1 sec
207         /// speedup on export of the Userguide.
208         char_type start_encodable_;
209         /// Which LaTeX package handles this encoding?
210         Package package_;
211         /**
212          * If this is true the stored information about the encoding covers
213          * all encodable characters. We set this to false initially so that
214          * we only need to query iconv for the actually used encodings.
215          * This is needed especially for the multibyte encodings, if we
216          * complete all encoding info on startup it takes 2-3 minutes.
217          */
218         bool complete_;
219 };
220
221 class Encodings {
222 public:
223         ///
224         typedef std::set<char_type> MathCommandSet;
225         ///
226         typedef std::set<char_type> TextCommandSet;
227         ///
228         typedef std::set<char_type> MathSymbolSet;
229         ///
230         typedef std::map<trivstring, Encoding> EncodingList;
231         /// iterator to iterate over all encodings.
232         /// We hide the fact that our encoding list is implemented as a map.
233         class const_iterator : public EncodingList::const_iterator {
234                 typedef EncodingList::const_iterator base;
235         public:
236                 const_iterator() : base() {}
237                 const_iterator(base const & b) : base(b) {}
238                 Encoding const & operator*() const { return base::operator*().second; }
239                 Encoding const * operator->() const { return &(base::operator*().second); }
240         };
241         ///
242         Encodings();
243         /// Read the encodings.
244         /// \param encfile encodings definition file
245         /// \param symbolsfile unicode->LaTeX mapping file
246         void read(support::FileName const & encfile,
247                   support::FileName const & symbolsfile);
248         /// Get encoding from LyX name \p name
249         Encoding const *
250         fromLyXName(std::string const & name, bool allowUnsafe = false) const;
251         /// Get encoding from LaTeX name \p name and package \p package
252         Encoding const * fromLaTeXName(std::string const & name,
253                 int const & package = Encoding::any, bool allowUnsafe = false) const;
254         /// Get encoding from iconv name \p name and package \p package
255         Encoding const * fromIconvName(std::string const & name,
256                 int const & package = Encoding::any, bool allowUnsafe = false) const;
257
258         ///
259         const_iterator begin() const { return encodinglist.begin(); }
260         ///
261         const_iterator end() const { return encodinglist.end(); }
262
263         /// Accessor for the unicode information table.
264         static CharInfo const & unicodeCharInfo(char_type c);
265         /// Is this a combining char?
266         static bool isCombiningChar(char_type c);
267         /// Return the TIPA shortcut
268         static std::string const TIPAShortcut(char_type c);
269         /**
270          * Is this a known char from some language?
271          * If \p preamble is empty and code point \p c is known to belong
272          * to a supported script, true is returned and \p preamble is set
273          * to the corresponding entry in the unicodesymbols file.
274          * If \p preamble is not empty, a check is made whether code point
275          * \p c is a known character matching the preamble entry.
276          */
277         static bool isKnownScriptChar(char_type const c, std::string & preamble);
278         /**
279          * Do we have to display in italics this character when in mathmode?
280          * This is true if the "mathalpha" flag is set. We use this for
281          * letters and accented characters that are output as math commands.
282          */
283         static bool isMathAlpha(char_type c);
284         /**
285          * Register \p c as a mathmode command.
286          */
287         static void addMathCmd(char_type c) { mathcmd.insert(c); }
288         /**
289          * Register \p c as a textmode command.
290          */
291         static void addTextCmd(char_type c) { textcmd.insert(c); }
292         /**
293          * Register \p c as a mathmode symbol.
294          */
295         static void addMathSym(char_type c) { mathsym.insert(c); }
296         /**
297          * Tell whether \p c is registered as a mathmode command.
298          */
299         static bool isMathCmd(char_type c) { return mathcmd.count(c); }
300         /**
301          * Tell whether \p c is registered as a textmode command.
302          */
303         static bool isTextCmd(char_type c) { return textcmd.count(c); }
304         /**
305          * Tell whether \p c is registered as a mathmode symbol.
306          */
307         static bool isMathSym(char_type c) { return mathsym.count(c); }
308         /**
309          * If \p c cannot be encoded in the given \p encoding, convert
310          * it to something that LaTeX can understand in mathmode.
311          * \p needsTermination indicates whether the command needs to be
312          * terminated by {} or a space.
313          * \return whether \p command is a mathmode command
314          */
315         static bool latexMathChar(char_type c, bool mathmode,
316                         Encoding const * encoding, docstring & command,
317                         bool & needsTermination);
318         /**
319          * Convert the LaTeX command in \p cmd to the corresponding unicode
320          * point and set \p combining to true if it is a combining symbol.
321          * \p needsTermination indicates whether the command needs to be
322          * terminated by {} or a space.
323          */
324         static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
325                         bool & combining, bool & needsTermination,
326                         std::set<std::string> * req = 0);
327         ///
328         enum LatexCmd {
329                 ///
330                 MATH_CMD = 1,
331                 ///
332                 TEXT_CMD = 2
333         };
334         /**
335          * Convert the LaTeX commands in \p cmd and \return a docstring
336          * of corresponding unicode points. The conversion stops at the
337          * first command which could not be converted, and the remaining
338          * unconverted commands are returned in \p rem.
339          * The \p cmdtype parameter can be used to limit recognized
340          * commands to math or text mode commands only.
341          * \p needsTermination indicates whether the command needs to be
342          * terminated by {} or a space.
343          */
344         static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
345                         bool & needsTermination, docstring & rem,
346                         std::set<std::string> * req = 0);
347
348 protected:
349         ///
350         EncodingList encodinglist;
351         ///
352         static MathCommandSet mathcmd;
353         ///
354         static TextCommandSet textcmd;
355         ///
356         static MathSymbolSet mathsym;
357 };
358
359 extern Encodings encodings;
360
361
362 } // namespace lyx
363
364 #endif