]> git.lyx.org Git - lyx.git/blob - src/Encoding.h
Fix overflow of inset over text (see #9224)
[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() : flags_(0) {}
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() : fixedwidth_(true), unsafe_(false), complete_(false) {}
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         /// Accessor for the unicode information table.
259         static CharInfo const & unicodeCharInfo(char_type c);
260         /// Is this a combining char?
261         static bool isCombiningChar(char_type c);
262         /// Return the TIPA shortcut
263         static std::string const TIPAShortcut(char_type c);
264         /**
265          * Is this a known char from some language?
266          * If \p preamble is empty and code point \p c is known to belong
267          * to a supported script, true is returned and \p preamble is set
268          * to the corresponding entry in the unicodesymbols file.
269          * If \p preamble is not empty, a check is made whether code point
270          * \p c is a known character matching the preamble entry.
271          */
272         static bool isKnownScriptChar(char_type const c, std::string & preamble);
273         /**
274          * Do we have to display in italics this character when in mathmode?
275          * This is true if the "mathalpha" flag is set. We use this for
276          * letters and accented characters that are output as math commands.
277          */
278         static bool isMathAlpha(char_type c);
279         /**
280          * Register \p c as a mathmode command.
281          */
282         static void addMathCmd(char_type c) { mathcmd.insert(c); }
283         /**
284          * Register \p c as a textmode command.
285          */
286         static void addTextCmd(char_type c) { textcmd.insert(c); }
287         /**
288          * Register \p c as a mathmode symbol.
289          */
290         static void addMathSym(char_type c) { mathsym.insert(c); }
291         /**
292          * Tell whether \p c is registered as a mathmode command.
293          */
294         static bool isMathCmd(char_type c) { return mathcmd.count(c); }
295         /**
296          * Tell whether \p c is registered as a textmode command.
297          */
298         static bool isTextCmd(char_type c) { return textcmd.count(c); }
299         /**
300          * Tell whether \p c is registered as a mathmode symbol.
301          */
302         static bool isMathSym(char_type c) { return mathsym.count(c); }
303         /**
304          * If \p c cannot be encoded in the given \p encoding, convert
305          * it to something that LaTeX can understand in mathmode.
306          * \p needsTermination indicates whether the command needs to be
307          * terminated by {} or a space.
308          * \return whether \p command is a mathmode command
309          */
310         static bool latexMathChar(char_type c, bool mathmode,
311                         Encoding const * encoding, docstring & command,
312                         bool & needsTermination);
313         /**
314          * Convert the LaTeX command in \p cmd to the corresponding unicode
315          * point and set \p combining to true if it is a combining symbol.
316          * \p needsTermination indicates whether the command needs to be
317          * terminated by {} or a space.
318          */
319         static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
320                         bool & combining, bool & needsTermination,
321                         std::set<std::string> * req = 0);
322         ///
323         enum LatexCmd {
324                 ///
325                 MATH_CMD = 1,
326                 ///
327                 TEXT_CMD = 2
328         };
329         /**
330          * Convert the LaTeX commands in \p cmd and \return a docstring
331          * of corresponding unicode points. The conversion stops at the
332          * first command which could not be converted, and the remaining
333          * unconverted commands are returned in \p rem.
334          * The \p cmdtype parameter can be used to limit recognized
335          * commands to math or text mode commands only.
336          * \p needsTermination indicates whether the command needs to be
337          * terminated by {} or a space.
338          */
339         static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
340                         bool & needsTermination, docstring & rem,
341                         std::set<std::string> * req = 0);
342
343 protected:
344         ///
345         EncodingList encodinglist;
346         ///
347         static MathCommandSet mathcmd;
348         ///
349         static TextCommandSet textcmd;
350         ///
351         static MathSymbolSet mathsym;
352 };
353
354 extern Encodings encodings;
355
356
357 } // namespace lyx
358
359 #endif