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