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