]> git.lyx.org Git - features.git/blob - src/Encoding.h
Clean includes using the output of iwyu tool
[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/strfwd.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 EncodingException : public std::exception {
28 public:
29         EncodingException(char_type c);
30         virtual ~EncodingException() noexcept {}
31         virtual const char * what() const noexcept override;
32
33         char_type failed_char;
34         int par_id;
35         pos_type pos;
36 };
37
38
39 enum CharInfoFlags {
40         ///
41         CharInfoCombining = 1,
42         ///
43         CharInfoTextFeature = 2,
44         ///
45         CharInfoMathFeature = 4,
46         ///
47         CharInfoForce = 8,
48         ///
49         CharInfoTextNoTermination = 16,
50         ///
51         CharInfoMathNoTermination = 32,
52         ///
53         CharInfoForceSelected = 64,
54         ///
55         CharInfoDeprecated = 128
56 };
57
58
59 /// Information about a single UCS4 character
60 class CharInfo {
61 public:
62         CharInfo() : flags_(0) {}
63         CharInfo(
64                 docstring const & textcommand, docstring const & mathcommand,
65                 std::string const & textpreamble, std::string const & mathpreamble,
66                 std::string const & tipashortcut, unsigned int flags);
67         // we assume that at least one command is nonempty when using unicodesymbols
68         bool isUnicodeSymbol() const { return !textcommand_.empty() || !mathcommand_.empty(); }
69         /// LaTeX command (text mode) for this character
70         docstring const textcommand() const { return textcommand_; }
71         /// LaTeX command (math mode) for this character
72         docstring mathcommand() const { return mathcommand_; }
73         /// Needed LaTeX preamble (or feature) for text mode
74         std::string textpreamble() const { return textpreamble_; }
75         /// Needed LaTeX preamble (or feature) for math mode
76         std::string mathpreamble() const { return mathpreamble_; }
77         /// Is this a combining character?
78         bool combining() const { return flags_ & CharInfoCombining; }
79         /// Is \c textpreamble a feature known by LaTeXFeatures, or a raw LaTeX
80         /// command?
81         bool textfeature() const { return flags_ & CharInfoTextFeature; }
82         /// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX
83         /// command?
84         bool mathfeature() const { return flags_ & CharInfoMathFeature; }
85         /// Always force the LaTeX command, even if the encoding contains
86         /// this character?
87         bool force() const { return flags_ & CharInfoForce; }
88         /// Force the LaTeX command for some encodings?
89         bool forceselected() const { return flags_ & CharInfoForceSelected; }
90         /// Disable LaTeX command => char_type conversion for this deprecated symbol?
91         bool deprecated() const { return flags_ & CharInfoDeprecated; }
92         /// TIPA shortcut
93         std::string const tipashortcut() const { return tipashortcut_; }
94         /// \c textcommand needs no termination (such as {} or space).
95         bool textnotermination() const { return flags_ & CharInfoTextNoTermination; }
96         /// \c mathcommand needs no termination (such as {} or space).
97         bool mathnotermination() const { return flags_ & CharInfoMathNoTermination; }
98         ///
99 private:
100         /// LaTeX command (text mode) for this character
101         trivdocstring textcommand_;
102         /// LaTeX command (math mode) for this character
103         trivdocstring mathcommand_;
104         /// Needed LaTeX preamble (or feature) for text mode
105         trivstring textpreamble_;
106         /// Needed LaTeX preamble (or feature) for math mode
107         trivstring mathpreamble_;
108         /// TIPA shortcut
109         trivstring tipashortcut_;
110         /// feature flags
111         unsigned int flags_;
112 };
113
114
115 /**
116  * An encoding as defined in lib/encodings.
117  * All const methods are thread-safe, so the caller does not need any locking.
118  * This property must be kept when changing the class.
119  */
120 class Encoding {
121 public:
122         /// Which LaTeX package handles this encoding?
123         enum Package {
124                 none = 1,
125                 inputenc = 2,
126                 CJK = 4,
127                 japanese = 8
128         };
129         /// Represent any of the above packages
130         static int const any;
131         ///
132         Encoding() : fixedwidth_(true), unsafe_(false), forced_(nullptr),
133                      start_encodable_(0), package_(none), complete_(false) {}
134         ///
135         Encoding(std::string const & n, std::string const & l,
136                  std::string const & g, std::string const & i,
137                  bool f, bool u, Package p);
138         ///
139         void init() const;
140         ///
141         std::string const name() const { return name_; }
142         ///
143         std::string const latexName() const { return latexName_; }
144         ///
145         std::string const guiName() const { return guiName_; }
146         ///
147         std::string const iconvName() const { return iconvName_; }
148         ///
149         bool hasFixedWidth() const { return fixedwidth_; }
150         ///
151         bool unsafe() const { return unsafe_; }
152         /// \p c is representable in this encoding without a LaTeX macro
153         bool encodable(char_type c) const;
154         /**
155          * Convert \p c to something that LaTeX can understand.
156          * This is either the character itself (if it is representable
157          * in this encoding), or a LaTeX macro.
158          * If the character is not representable in this encoding, but no
159          * LaTeX macro is known, a warning is given of lyxerr, and the
160          * character is returned.
161          * \return the converted character and a flag indicating whether
162          * the command needs to be terminated by {} or a space.
163          */
164         std::pair<docstring, bool> latexChar(char_type c) const;
165         /**
166          * Convert \p input to something that LaTeX can understand.
167          * This is either the string itself (if it is representable
168          * in this encoding), or a LaTeX macro.
169          * If a character is not representable in this encoding, but no
170          * LaTeX macro is known, a warning is given of lyxerr, and the
171          * character is returned in the second string of the pair and
172          * omitted in the first.
173          * \p dryrun specifies whether the string is used within source
174          * preview (which yields a special warning).
175          */
176         std::pair<docstring, docstring> latexString(docstring const & input,
177                                                     bool dryrun = false) const;
178         /// Which LaTeX package handles this encoding?
179         Package package() const { return package_; }
180         /// A list of all characters usable in this encoding
181         std::vector<char_type> symbolsList() const;
182 private:
183         /**
184          * Do we have to output this character as LaTeX command in any case?
185          * This is true if the "force" flag is set.
186          * We need this if the inputencoding does not support a certain glyph.
187          */
188         bool isForced(char_type c) const;
189         ///
190         trivstring name_;
191         ///
192         trivstring latexName_;
193         ///
194         trivstring guiName_;
195         ///
196         trivstring iconvName_;
197         /// Is this a fixed width encoding?
198         bool fixedwidth_;
199         /// Is this encoding TeX unsafe, e.g. control characters like {, }
200         /// and \\ may appear in high bytes?
201         bool unsafe_;
202         ///
203         typedef std::set<char_type> CharSet;
204         /// Set of UCS4 characters that we can encode (for singlebyte
205         /// encodings only)
206         CharSet encodable_;
207         /// Set of UCS4 characters that we can't encode
208         CharSet const * forced_;
209         /// All code points below this are encodable. This helps us to avoid
210         /// lokup of ASCII characters in encodable_ and gives about 1 sec
211         /// speedup on export of the Userguide.
212         char_type start_encodable_;
213         /// Which LaTeX package handles this encoding?
214         Package package_;
215         /**
216          * If this is true the stored information about the encoding covers
217          * all encodable characters. We set this to false initially so that
218          * we only need to query iconv for the actually used encodings.
219          * This is needed especially for the multibyte encodings, if we
220          * complete all encoding info on startup it takes 2-3 minutes.
221          */
222         bool complete_;
223 };
224
225 class Encodings {
226 public:
227         ///
228         typedef std::set<char_type> MathCommandSet;
229         ///
230         typedef std::set<char_type> TextCommandSet;
231         ///
232         typedef std::set<char_type> MathSymbolSet;
233         ///
234         typedef std::map<trivstring, Encoding> EncodingList;
235         /// iterator to iterate over all encodings.
236         /// We hide the fact that our encoding list is implemented as a map.
237         class const_iterator : public EncodingList::const_iterator {
238                 typedef EncodingList::const_iterator base;
239         public:
240                 const_iterator() : base() {}
241                 const_iterator(base const & b) : base(b) {}
242                 Encoding const & operator*() const { return base::operator*().second; }
243                 Encoding const * operator->() const { return &(base::operator*().second); }
244         };
245         ///
246         Encodings();
247         /// Read the encodings.
248         /// \param encfile encodings definition file
249         /// \param symbolsfile unicode->LaTeX mapping file
250         void read(support::FileName const & encfile,
251                   support::FileName const & symbolsfile);
252         /// Get encoding from LyX name \p name
253         Encoding const *
254         fromLyXName(std::string const & name, bool allowUnsafe = false) const;
255         /// Get encoding from LaTeX name \p name and package \p package
256         Encoding const * fromLaTeXName(std::string const & name,
257                 int package = Encoding::any, bool allowUnsafe = false) const;
258         /// Get encoding from iconv name \p name and package \p package
259         Encoding const * fromIconvName(std::string const & name,
260                 int package = Encoding::any, bool allowUnsafe = false) const;
261
262         ///
263         const_iterator begin() const { return encodinglist.begin(); }
264         ///
265         const_iterator end() const { return encodinglist.end(); }
266
267         /// Accessor for the unicode information table.
268         static CharInfo const & unicodeCharInfo(char_type c);
269         /// Is this a combining char?
270         static bool isCombiningChar(char_type c);
271         /// Return the TIPA shortcut
272         static std::string const TIPAShortcut(char_type c);
273         /**
274          * Test, if \p c is a supported Greek or Cyrillic letter.
275          * Return script macro name or empty string.
276          */
277         static std::string const isKnownScriptChar(char_type const c);
278         /// Does \p fontenc support characters in \p script?
279         static bool fontencSupportsScript(std::string const & fontenc,
280                                                                           std::string const & script);
281         /**
282          * Do we have to display in italics this character when in mathmode?
283          * This is true if the "mathalpha" flag is set. We use this for
284          * letters and accented characters that are output as math commands.
285          */
286         static bool isMathAlpha(char_type c);
287         /**
288          * Do we have to wrap in \text this character when in mathmode?
289          * This is true if \p c is not ascii and the "mathalpha" flag is not
290          * set and a mathcommand is not defined in the unicodesymbols file.
291          */
292         static bool isUnicodeTextOnly(char_type c);
293         /**
294          * Register \p c as a mathmode command.
295          */
296         static void addMathCmd(char_type c) { mathcmd.insert(c); }
297         /**
298          * Register \p c as a textmode command.
299          */
300         static void addTextCmd(char_type c) { textcmd.insert(c); }
301         /**
302          * Register \p c as a mathmode symbol.
303          */
304         static void addMathSym(char_type c) { mathsym.insert(c); }
305         /**
306          * Tell whether \p c is registered as a mathmode command.
307          */
308         static bool isMathCmd(char_type c) { return mathcmd.count(c); }
309         /**
310          * Tell whether \p c is registered as a textmode command.
311          */
312         static bool isTextCmd(char_type c) { return textcmd.count(c); }
313         /**
314          * Tell whether \p c is registered as a mathmode symbol.
315          */
316         static bool isMathSym(char_type c) { return mathsym.count(c); }
317         /**
318          * If \p c cannot be encoded in the given \p encoding, convert
319          * it to something that LaTeX can understand in mathmode.
320          * \p needsTermination indicates whether the command needs to be
321          * terminated by {} or a space.
322          * \return whether \p command is a mathmode command
323          */
324         static bool latexMathChar(char_type c, bool mathmode,
325                         Encoding const * encoding, docstring & command,
326                         bool & needsTermination);
327         /**
328          * Convert the LaTeX command in \p cmd to the corresponding unicode
329          * point and set \p combining to true if it is a combining symbol.
330          * \p needsTermination indicates whether the command needs to be
331          * terminated by {} or a space.
332          */
333         static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
334                         bool & combining, bool & needsTermination,
335                         std::set<std::string> * req = nullptr);
336         ///
337         enum LatexCmd {
338                 ///
339                 MATH_CMD = 1,
340                 ///
341                 TEXT_CMD = 2
342         };
343         /**
344          * Convert the LaTeX commands in \p cmd and \return a docstring
345          * of corresponding unicode points. The conversion stops at the
346          * first command which could not be converted, and the remaining
347          * unconverted commands are returned in \p rem.
348          * The \p cmdtype parameter can be used to limit recognized
349          * commands to math or text mode commands only.
350          * \p needsTermination indicates whether the command needs to be
351          * terminated by {} or a space.
352          */
353         static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
354                         bool & needsTermination, docstring & rem,
355                         std::set<std::string> * req = nullptr);
356
357 protected:
358         ///
359         EncodingList encodinglist;
360         ///
361         static MathCommandSet mathcmd;
362         ///
363         static TextCommandSet textcmd;
364         ///
365         static MathSymbolSet mathsym;
366 };
367
368 extern Encodings encodings;
369
370
371 } // namespace lyx
372
373 #endif