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