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