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