]> git.lyx.org Git - lyx.git/blob - src/Encoding.h
Correct tex2lyx/roundtrip/test-structure.tex test.
[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), start_encodable_(0),
130                      package_(none), complete_(false) {}
131         ///
132         Encoding(std::string const & n, std::string const & l,
133                  std::string const & g, std::string const & i,
134                  bool f, bool u, Package p);
135         ///
136         void init() const;
137         ///
138         std::string const name() const { return name_; }
139         ///
140         std::string const latexName() const { return latexName_; }
141         ///
142         std::string const guiName() const { return guiName_; }
143         ///
144         std::string const iconvName() const { return iconvName_; }
145         ///
146         bool hasFixedWidth() const { return fixedwidth_; }
147         ///
148         bool unsafe() const { return unsafe_; }
149         /// \p c is representable in this encoding without a LaTeX macro
150         bool encodable(char_type c) const;
151         /**
152          * Convert \p c to something that LaTeX can understand.
153          * This is either the character itself (if it is representable
154          * in this encoding), or a LaTeX macro.
155          * If the character is not representable in this encoding, but no
156          * LaTeX macro is known, a warning is given of lyxerr, and the
157          * character is returned.
158          * \return the converted character and a flag indicating whether
159          * the command needs to be terminated by {} or a space.
160          */
161         std::pair<docstring, bool> latexChar(char_type c) const;
162         /**
163          * Convert \p input to something that LaTeX can understand.
164          * This is either the string itself (if it is representable
165          * in this encoding), or a LaTeX macro.
166          * If a character is not representable in this encoding, but no
167          * LaTeX macro is known, a warning is given of lyxerr, and the
168          * character is returned in the second string of the pair and
169          * omitted in the first.
170          * \p dryrun specifies whether the string is used within source
171          * preview (which yields a special warning).
172          */
173         std::pair<docstring, docstring> latexString(docstring const & input,
174                                                     bool dryrun = false) const;
175         /// Which LaTeX package handles this encoding?
176         Package package() const { return package_; }
177         /// A list of all characters usable in this encoding
178         std::vector<char_type> symbolsList() const;
179 private:
180         /**
181          * Do we have to output this character as LaTeX command in any case?
182          * This is true if the "force" flag is set.
183          * We need this if the inputencoding does not support a certain glyph.
184          */
185         bool isForced(char_type c) const;
186         ///
187         trivstring name_;
188         ///
189         trivstring latexName_;
190         ///
191         trivstring guiName_;
192         ///
193         trivstring iconvName_;
194         /// Is this a fixed width encoding?
195         bool fixedwidth_;
196         /// Is this encoding TeX unsafe, e.g. control characters like {, }
197         /// and \\ may appear in high bytes?
198         bool unsafe_;
199         ///
200         typedef std::set<char_type> CharSet;
201         /// Set of UCS4 characters that we can encode (for singlebyte
202         /// encodings only)
203         CharSet encodable_;
204         /// Set of UCS4 characters that we can't encode
205         CharSet const * forced_;
206         /// All code points below this are encodable. This helps us to avoid
207         /// lokup of ASCII characters in encodable_ and gives about 1 sec
208         /// speedup on export of the Userguide.
209         char_type start_encodable_;
210         /// Which LaTeX package handles this encoding?
211         Package package_;
212         /**
213          * If this is true the stored information about the encoding covers
214          * all encodable characters. We set this to false initially so that
215          * we only need to query iconv for the actually used encodings.
216          * This is needed especially for the multibyte encodings, if we
217          * complete all encoding info on startup it takes 2-3 minutes.
218          */
219         bool complete_;
220 };
221
222 class Encodings {
223 public:
224         ///
225         typedef std::set<char_type> MathCommandSet;
226         ///
227         typedef std::set<char_type> TextCommandSet;
228         ///
229         typedef std::set<char_type> MathSymbolSet;
230         ///
231         typedef std::map<trivstring, Encoding> EncodingList;
232         /// iterator to iterate over all encodings.
233         /// We hide the fact that our encoding list is implemented as a map.
234         class const_iterator : public EncodingList::const_iterator {
235                 typedef EncodingList::const_iterator base;
236         public:
237                 const_iterator() : base() {}
238                 const_iterator(base const & b) : base(b) {}
239                 Encoding const & operator*() const { return base::operator*().second; }
240                 Encoding const * operator->() const { return &(base::operator*().second); }
241         };
242         ///
243         Encodings();
244         /// Read the encodings.
245         /// \param encfile encodings definition file
246         /// \param symbolsfile unicode->LaTeX mapping file
247         void read(support::FileName const & encfile,
248                   support::FileName const & symbolsfile);
249         /// Get encoding from LyX name \p name
250         Encoding const *
251         fromLyXName(std::string const & name, bool allowUnsafe = false) const;
252         /// Get encoding from LaTeX name \p name and package \p package
253         Encoding const * fromLaTeXName(std::string const & name,
254                 int const & package = Encoding::any, bool allowUnsafe = false) const;
255         /// Get encoding from iconv name \p name and package \p package
256         Encoding const * fromIconvName(std::string const & name,
257                 int const & package = Encoding::any, bool allowUnsafe = false) const;
258
259         ///
260         const_iterator begin() const { return encodinglist.begin(); }
261         ///
262         const_iterator end() const { return encodinglist.end(); }
263
264         /// Accessor for the unicode information table.
265         static CharInfo const & unicodeCharInfo(char_type c);
266         /// Is this a combining char?
267         static bool isCombiningChar(char_type c);
268         /// Return the TIPA shortcut
269         static std::string const TIPAShortcut(char_type c);
270         /**
271          * Is this a known char from some language?
272          * If \p preamble is empty and code point \p c is known to belong
273          * to a supported script, true is returned and \p preamble is set
274          * to the corresponding entry in the unicodesymbols file.
275          * If \p preamble is not empty, a check is made whether code point
276          * \p c is a known character matching the preamble entry.
277          */
278         static bool isKnownScriptChar(char_type const c, std::string & preamble);
279         /**
280          * Do we have to display in italics this character when in mathmode?
281          * This is true if the "mathalpha" flag is set. We use this for
282          * letters and accented characters that are output as math commands.
283          */
284         static bool isMathAlpha(char_type c);
285         /**
286          * Register \p c as a mathmode command.
287          */
288         static void addMathCmd(char_type c) { mathcmd.insert(c); }
289         /**
290          * Register \p c as a textmode command.
291          */
292         static void addTextCmd(char_type c) { textcmd.insert(c); }
293         /**
294          * Register \p c as a mathmode symbol.
295          */
296         static void addMathSym(char_type c) { mathsym.insert(c); }
297         /**
298          * Tell whether \p c is registered as a mathmode command.
299          */
300         static bool isMathCmd(char_type c) { return mathcmd.count(c); }
301         /**
302          * Tell whether \p c is registered as a textmode command.
303          */
304         static bool isTextCmd(char_type c) { return textcmd.count(c); }
305         /**
306          * Tell whether \p c is registered as a mathmode symbol.
307          */
308         static bool isMathSym(char_type c) { return mathsym.count(c); }
309         /**
310          * If \p c cannot be encoded in the given \p encoding, convert
311          * it to something that LaTeX can understand in mathmode.
312          * \p needsTermination indicates whether the command needs to be
313          * terminated by {} or a space.
314          * \return whether \p command is a mathmode command
315          */
316         static bool latexMathChar(char_type c, bool mathmode,
317                         Encoding const * encoding, docstring & command,
318                         bool & needsTermination);
319         /**
320          * Convert the LaTeX command in \p cmd to the corresponding unicode
321          * point and set \p combining to true if it is a combining symbol.
322          * \p needsTermination indicates whether the command needs to be
323          * terminated by {} or a space.
324          */
325         static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
326                         bool & combining, bool & needsTermination,
327                         std::set<std::string> * req = 0);
328         ///
329         enum LatexCmd {
330                 ///
331                 MATH_CMD = 1,
332                 ///
333                 TEXT_CMD = 2
334         };
335         /**
336          * Convert the LaTeX commands in \p cmd and \return a docstring
337          * of corresponding unicode points. The conversion stops at the
338          * first command which could not be converted, and the remaining
339          * unconverted commands are returned in \p rem.
340          * The \p cmdtype parameter can be used to limit recognized
341          * commands to math or text mode commands only.
342          * \p needsTermination indicates whether the command needs to be
343          * terminated by {} or a space.
344          */
345         static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
346                         bool & needsTermination, docstring & rem,
347                         std::set<std::string> * req = 0);
348
349 protected:
350         ///
351         EncodingList encodinglist;
352         ///
353         static MathCommandSet mathcmd;
354         ///
355         static TextCommandSet textcmd;
356         ///
357         static MathSymbolSet mathsym;
358 };
359
360 extern Encodings encodings;
361
362
363 } // namespace lyx
364
365 #endif