4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
16 #include "support/strfwd.h"
17 #include "support/types.h"
25 namespace support { class FileName; }
27 class EncodingException : public std::exception {
29 EncodingException(char_type c);
30 virtual ~EncodingException() noexcept {}
31 virtual const char * what() const noexcept override;
33 char_type failed_char;
41 CharInfoCombining = 1,
43 CharInfoTextFeature = 2,
45 CharInfoMathFeature = 4,
49 CharInfoTextNoTermination = 16,
51 CharInfoMathNoTermination = 32,
53 CharInfoForceSelected = 64,
55 CharInfoDeprecated = 128
59 /// Information about a single UCS4 character
62 CharInfo() : flags_(0) {}
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
81 bool textfeature() const { return flags_ & CharInfoTextFeature; }
82 /// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX
84 bool mathfeature() const { return flags_ & CharInfoMathFeature; }
85 /// Always force the LaTeX command, even if the encoding contains
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; }
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; }
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_;
109 trivstring tipashortcut_;
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.
122 /// Which LaTeX package handles this encoding?
129 /// Represent any of the above packages
130 static int const any;
132 Encoding() : fixedwidth_(true), unsafe_(false), forced_(nullptr),
133 start_encodable_(0), package_(none), complete_(false) {}
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);
141 std::string const name() const { return name_; }
143 std::string const latexName() const { return latexName_; }
145 std::string const guiName() const { return guiName_; }
147 std::string const iconvName() const { return iconvName_; }
149 bool hasFixedWidth() const { return fixedwidth_; }
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;
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.
164 std::pair<docstring, bool> latexChar(char_type c) const;
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).
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;
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.
188 bool isForced(char_type c) const;
192 trivstring latexName_;
196 trivstring iconvName_;
197 /// Is this a fixed width encoding?
199 /// Is this encoding TeX unsafe, e.g. control characters like {, }
200 /// and \\ may appear in high bytes?
203 typedef std::set<char_type> CharSet;
204 /// Set of UCS4 characters that we can encode (for singlebyte
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?
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.
228 typedef std::set<char_type> MathCommandSet;
230 typedef std::set<char_type> TextCommandSet;
232 typedef std::set<char_type> MathSymbolSet;
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;
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); }
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
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;
263 const_iterator begin() const { return encodinglist.begin(); }
265 const_iterator end() const { return encodinglist.end(); }
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);
274 * Test, if \p c is a supported Greek or Cyrillic letter.
275 * Return script macro name or empty string.
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);
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.
286 static bool isMathAlpha(char_type c);
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.
292 static bool isUnicodeTextOnly(char_type c);
294 * Register \p c as a mathmode command.
296 static void addMathCmd(char_type c) { mathcmd.insert(c); }
298 * Register \p c as a textmode command.
300 static void addTextCmd(char_type c) { textcmd.insert(c); }
302 * Register \p c as a mathmode symbol.
304 static void addMathSym(char_type c) { mathsym.insert(c); }
306 * Tell whether \p c is registered as a mathmode command.
308 static bool isMathCmd(char_type c) { return mathcmd.count(c); }
310 * Tell whether \p c is registered as a textmode command.
312 static bool isTextCmd(char_type c) { return textcmd.count(c); }
314 * Tell whether \p c is registered as a mathmode symbol.
316 static bool isMathSym(char_type c) { return mathsym.count(c); }
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
324 static bool latexMathChar(char_type c, bool mathmode,
325 Encoding const * encoding, docstring & command,
326 bool & needsTermination);
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.
333 static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
334 bool & combining, bool & needsTermination,
335 std::set<std::string> * req = nullptr);
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.
353 static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
354 bool & needsTermination, docstring & rem,
355 std::set<std::string> * req = nullptr);
359 EncodingList encodinglist;
361 static MathCommandSet mathcmd;
363 static TextCommandSet textcmd;
365 static MathSymbolSet mathsym;
368 extern Encodings encodings;