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