]> git.lyx.org Git - lyx.git/blob - src/Encoding.h
Fix monolithic build (bug #8079)
[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 Buffer;
28 class LaTeXFeatures;
29
30 class EncodingException : public std::exception {
31 public:
32         EncodingException(char_type c);
33         virtual ~EncodingException() throw() {}
34         virtual const char * what() const throw();
35  
36         char_type failed_char;
37         int par_id;
38         pos_type pos;
39 };
40
41
42 ///
43 class Encoding {
44 public:
45         /// Which LaTeX package handles this encoding?
46         enum Package {
47                 none,
48                 inputenc,
49                 CJK,
50                 japanese
51         };
52         ///
53         Encoding() {}
54         ///
55         Encoding(std::string const & n, std::string const & l,
56                  std::string const & g, std::string const & i,
57                  bool f, Package p);
58         ///
59         void init() const;
60         ///
61         std::string const & name() const { return name_; }
62         ///
63         std::string const & latexName() const { return latexName_; }
64         ///
65         std::string const & guiName() const { return guiName_; }
66         ///
67         std::string const & iconvName() const { return iconvName_; }
68         ///
69         bool const & hasFixedWidth() const { return fixedwidth_; }
70         /**
71          * Convert \p c to something that LaTeX can understand.
72          * This is either the character itself (if it is representable
73          * in this encoding), or a LaTeX macro.
74          * If the character is not representable in this encoding, but no
75          * LaTeX macro is known, a warning is given of lyxerr, and the
76          * character is returned.
77          */
78         docstring latexChar(char_type c, bool no_commands = false) const;
79         /// Which LaTeX package handles this encoding?
80         Package package() const { return package_; }
81         /// A list of all characters usable in this encoding
82         std::vector<char_type> symbolsList() const;
83 private:
84         ///
85         std::string name_;
86         ///
87         std::string latexName_;
88         ///
89         std::string guiName_;
90         ///
91         std::string iconvName_;
92         /// Is this a fixed width encoding?
93         bool fixedwidth_;
94         ///
95         typedef std::set<char_type> CharSet;
96         /// Set of UCS4 characters that we can encode (for singlebyte
97         /// encodings only)
98         mutable CharSet encodable_;
99         /// All code points below this are encodable. This helps us to avoid
100         /// lokup of ASCII characters in encodable_ and gives about 1 sec
101         /// speedup on export of the Userguide.
102         mutable char_type start_encodable_;
103         /// Which LaTeX package handles this encoding?
104         Package package_;
105         /**
106          * If this is true the stored information about the encoding covers
107          * all encodable characters. We set this to false initially so that
108          * we only need to query iconv for the actually used encodings.
109          * This is needed especially for the multibyte encodings, if we
110          * complete all encoding info on startup it takes 2-3 minutes.
111          */
112         mutable bool complete_;
113 };
114
115 class Encodings {
116 public:
117         ///
118         typedef std::set<char_type> MathCommandSet;
119         ///
120         typedef std::set<char_type> TextCommandSet;
121         ///
122         typedef std::set<char_type> MathSymbolSet;
123         ///
124         typedef std::map<std::string, Encoding> EncodingList;
125         /// iterator to iterate over all encodings.
126         /// We hide the fact that our encoding list is implemented as a map.
127         class const_iterator : public EncodingList::const_iterator {
128                 typedef EncodingList::const_iterator base;
129         public:
130                 const_iterator() : base() {}
131                 const_iterator(base const & b) : base(b) {}
132                 Encoding const & operator*() const { return base::operator*().second; }
133                 Encoding const * operator->() const { return &(base::operator*().second); }
134         };
135         ///
136         Encodings();
137         /// Read the encodings.
138         /// \param encfile encodings definition file
139         /// \param symbolsfile unicode->LaTeX mapping file
140         void read(support::FileName const & encfile,
141                   support::FileName const & symbolsfile);
142         /// Get encoding from LyX name \p name
143         Encoding const * fromLyXName(std::string const & name) const;
144         /// Get encoding from LaTeX name \p name
145         Encoding const * fromLaTeXName(std::string const & name) const;
146
147         ///
148         const_iterator begin() const { return encodinglist.begin(); }
149         ///
150         const_iterator end() const { return encodinglist.end(); }
151
152         ///
153         enum LetterForm {
154                 ///
155                 FORM_ISOLATED,
156                 ///
157                 FORM_FINAL,
158                 ///
159                 FORM_INITIAL,
160                 ///
161                 FORM_MEDIAL
162         };
163         ///
164         static bool isHebrewComposeChar(char_type c);
165         ///
166         static bool isArabicComposeChar(char_type c);
167         ///
168         static bool isArabicSpecialChar(char_type c);
169         ///
170         static bool isArabicChar(char_type c);
171         ///
172         static char_type transformChar(char_type c, LetterForm form);
173         /// Is this a combining char?
174         static bool isCombiningChar(char_type c);
175         /// Return the TIPA shortcut
176         static std::string const TIPAShortcut(char_type c);
177         /**
178          * Is this a known char from some language?
179          * If \p preamble is empty and code point \p c is known to belong
180          * to a supported script, true is returned and \p preamble is set
181          * to the corresponding entry in the unicodesymbols file.
182          * If \p preamble is not empty, a check is made whether code point
183          * \p c is a known character matching the preamble entry.
184          */
185         static bool isKnownScriptChar(char_type const c, std::string & preamble);
186         /**
187          * Do we have to output this character as LaTeX command in any case?
188          * This is true if the "force" flag is set.
189          * We need this if the inputencoding does not support a certain glyph.
190          */
191         static bool isForced(char_type c);
192         /**
193          * Do we have to display in italics this character when in mathmode?
194          * This is true if the "mathalpha" flag is set. We use this for
195          * letters and accented characters that are output as math commands.
196          */
197         static bool isMathAlpha(char_type c);
198         /**
199          * Do we need to terminate this command (by {} or space)?
200          * This is true if the "notermination" flag is not set.
201          */
202         static bool needsTermination(char_type c);
203         /**
204          * Register \p c as a mathmode command.
205          */
206         static void addMathCmd(char_type c) { mathcmd.insert(c); }
207         /**
208          * Register \p c as a textmode command.
209          */
210         static void addTextCmd(char_type c) { textcmd.insert(c); }
211         /**
212          * Register \p c as a mathmode symbol.
213          */
214         static void addMathSym(char_type c) { mathsym.insert(c); }
215         /**
216          * Tell whether \p c is registered as a mathmode command.
217          */
218         static bool isMathCmd(char_type c) { return mathcmd.count(c); }
219         /**
220          * Tell whether \p c is registered as a textmode command.
221          */
222         static bool isTextCmd(char_type c) { return textcmd.count(c); }
223         /**
224          * Tell whether \p c is registered as a mathmode symbol.
225          */
226         static bool isMathSym(char_type c) { return mathsym.count(c); }
227         /**
228          * Initialize mathcmd, textcmd, and mathsym sets.
229          */
230         static void initUnicodeMath(Buffer const & buffer, bool for_master = true);
231         /**
232          * If \p c cannot be encoded in the given \p encoding, convert
233          * it to something that LaTeX can understand in mathmode.
234          * \return whether \p command is a mathmode command
235          */
236         static bool latexMathChar(char_type c, bool mathmode,
237                         Encoding const * encoding, docstring & command);
238         /**
239          * Convert the LaTeX command in \p cmd to the corresponding unicode
240          * point and set \p combining to true if it is a combining symbol
241          */
242         static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
243                         bool & combining, std::set<std::string> * req = 0);
244         ///
245         enum LatexCmd {
246                 ///
247                 MATH_CMD = 1,
248                 ///
249                 TEXT_CMD = 2
250         };
251         /**
252          * Convert the LaTeX commands in \p cmd and \return a docstring
253          * of corresponding unicode points. The conversion stops at the
254          * first command which could not be converted, and the remaining
255          * unconverted commands are returned in \p rem.
256          * The \p cmdtype parameter can be used to limit recognized
257          * commands to math or text mode commands only.
258          */
259         static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
260                         docstring & rem, std::set<std::string> * req = 0);
261         /**
262          * Add the preamble snippet needed for the output of \p c to
263          * \p features.
264          * This does not depend on the used encoding, since the inputenc
265          * package only maps the code point \p c to a command, it does not
266          * make this command available.
267          */
268         static void validate(char_type c, LaTeXFeatures & features, bool for_mathed = false);
269
270 private:
271         ///
272         EncodingList encodinglist;
273         ///
274         static MathCommandSet mathcmd;
275         ///
276         static TextCommandSet textcmd;
277         ///
278         static MathSymbolSet mathsym;
279 };
280
281 extern Encodings encodings;
282
283
284 } // namespace lyx
285
286 #endif