]> git.lyx.org Git - lyx.git/blob - src/Encoding.h
b23e50b954a94942a435742063410e117e4cefad
[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
22 namespace lyx {
23
24 namespace support { class FileName; }
25
26 class LaTeXFeatures;
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 ///
41 class Encoding {
42 public:
43         /// Which LaTeX package handles this encoding?
44         enum Package {
45                 none,
46                 inputenc,
47                 CJK
48         };
49         ///
50         Encoding() {}
51         ///
52         Encoding(std::string const & n, std::string const & l,
53                  std::string const & i, bool f, Package p);
54         ///
55         void init() const;
56         ///
57         std::string const & name() const { return Name_; }
58         ///
59         std::string const & latexName() const { return LatexName_; }
60         ///
61         std::string const & iconvName() const { return iconvName_; }
62         /**
63          * Convert \p c to something that LaTeX can understand.
64          * This is either the character itself (if it is representable
65          * in this encoding), or a LaTeX macro.
66          * If the character is not representable in this encoding, but no
67          * LaTeX macro is known, a warning is given of lyxerr, and the
68          * character is returned.
69          */
70         docstring const latexChar(char_type c) const;
71         /// Which LaTeX package handles this encoding?
72         Package package() const { return package_; }
73 private:
74         ///
75         std::string Name_;
76         ///
77         std::string LatexName_;
78         ///
79         std::string iconvName_;
80         /// Is this a fixed width encoding?
81         bool fixedwidth_;
82         ///
83         typedef std::set<char_type> CharSet;
84         /// Set of UCS4 characters that we can encode (for singlebyte
85         /// encodings only)
86         mutable CharSet encodable_;
87         /// All code points below this are encodable. This helps us to avoid
88         /// lokup of ASCII characters in encodable_ and gives about 1 sec
89         /// speedup on export of the Userguide.
90         mutable char_type start_encodable_;
91         /// Which LaTeX package handles this encoding?
92         Package package_;
93         /**
94          * If this is true the stored information about the encoding covers
95          * all encodable characters. We set this to false initially so that
96          * we only need to query iconv for the actually used encodings.
97          * This is needed especially for the multibyte encodings, if we
98          * complete all encoding info on startup it takes 2-3 minutes.
99          */
100         mutable bool complete_;
101 };
102
103 class Encodings {
104 public:
105         ///
106         typedef std::map<std::string, Encoding> EncodingList;
107         /// iterator to iterate over all encodings.
108         /// We hide the fact that our encoding list is implemented as a map.
109         class const_iterator : public EncodingList::const_iterator {
110                 typedef EncodingList::const_iterator base;
111         public:
112                 const_iterator() : base() {}
113                 const_iterator(base const & b) : base(b) {}
114                 Encoding const & operator*() const { return base::operator*().second; }
115                 Encoding const * operator->() const { return &(base::operator*().second); }
116         };
117         ///
118         Encodings();
119         /// Read the encodings.
120         /// \param encfile encodings definition file
121         /// \param symbolsfile unicode->LaTeX mapping file
122         void read(support::FileName const & encfile,
123                   support::FileName const & symbolsfile);
124         /// Get encoding from LyX name \p name
125         Encoding const * getFromLyXName(std::string const & name) const;
126         /// Get encoding from LaTeX name \p name
127         Encoding const * getFromLaTeXName(std::string const & name) const;
128
129         ///
130         const_iterator begin() const { return encodinglist.begin(); }
131         ///
132         const_iterator end() const { return encodinglist.end(); }
133
134         ///
135         enum Letter_Form {
136                 ///
137                 FORM_ISOLATED,
138                 ///
139                 FORM_FINAL,
140                 ///
141                 FORM_INITIAL,
142                 ///
143                 FORM_MEDIAL
144         };
145         ///
146         static bool isComposeChar_hebrew(char_type c);
147         ///
148         static bool isComposeChar_arabic(char_type c);
149         ///
150         static bool is_arabic_special(char_type c);
151         ///
152         static bool is_arabic(char_type c);
153         ///
154         static char_type transformChar(char_type c, Letter_Form form);
155         /// Is this a combining char?
156         static bool isCombiningChar(char_type c);
157         /**
158          * Is this a known char from some language?
159          * If \p preamble is empty and code point \p c is known to belong
160          * to a supported script, true is returned and \p preamble is set
161          * to the corresponding entry in the unicodesymbols file.
162          * If \p preamble is not empty, a check is made whether code point
163          * \p c is a known character matching the preamble entry.
164          */
165         static bool isKnownScriptChar(char_type const c, std::string & preamble);
166         /**
167          * Add the preamble snippet needed for the output of \p c to
168          * \p features.
169          * This does not depend on the used encoding, since the inputenc
170          * package only maps the code point \p c to a command, it does not
171          * make this command available.
172          */
173         static void validate(char_type c, LaTeXFeatures & features);
174
175 private:
176         ///
177         EncodingList encodinglist;
178 };
179
180 extern Encodings encodings;
181
182
183 } // namespace lyx
184
185 #endif