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