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/docstring.h"
17 #include "support/trivstring.h"
18 #include "support/types.h"
26 namespace support { class FileName; }
28 class EncodingException : public std::exception {
30 EncodingException(char_type c);
31 virtual ~EncodingException() noexcept {}
32 virtual const char * what() const noexcept override;
34 char_type failed_char;
42 CharInfoCombining = 1,
44 CharInfoTextFeature = 2,
46 CharInfoMathFeature = 4,
50 CharInfoTextNoTermination = 16,
52 CharInfoMathNoTermination = 32,
54 CharInfoForceSelected = 64,
56 CharInfoDeprecated = 128
60 /// Information about a single UCS4 character
63 CharInfo() : flags_(0) {}
65 docstring const & textcommand, docstring const & mathcommand,
66 std::string const & textpreamble, std::string const & mathpreamble,
67 std::string const & tipashortcut, unsigned int flags);
68 // we assume that at least one command is nonempty when using unicodesymbols
69 bool isUnicodeSymbol() const { return !textcommand_.empty() || !mathcommand_.empty(); }
70 /// LaTeX command (text mode) for this character
71 docstring const textcommand() const { return textcommand_; }
72 /// LaTeX command (math mode) for this character
73 docstring mathcommand() const { return mathcommand_; }
74 /// Needed LaTeX preamble (or feature) for text mode
75 std::string textpreamble() const { return textpreamble_; }
76 /// Needed LaTeX preamble (or feature) for math mode
77 std::string mathpreamble() const { return mathpreamble_; }
78 /// Is this a combining character?
79 bool combining() const { return flags_ & CharInfoCombining; }
80 /// Is \c textpreamble a feature known by LaTeXFeatures, or a raw LaTeX
82 bool textfeature() const { return flags_ & CharInfoTextFeature; }
83 /// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX
85 bool mathfeature() const { return flags_ & CharInfoMathFeature; }
86 /// Always force the LaTeX command, even if the encoding contains
88 bool force() const { return flags_ & CharInfoForce; }
89 /// Force the LaTeX command for some encodings?
90 bool forceselected() const { return flags_ & CharInfoForceSelected; }
91 /// Disable LaTeX command => char_type conversion for this deprecated symbol?
92 bool deprecated() const { return flags_ & CharInfoDeprecated; }
94 std::string const tipashortcut() const { return tipashortcut_; }
95 /// \c textcommand needs no termination (such as {} or space).
96 bool textnotermination() const { return flags_ & CharInfoTextNoTermination; }
97 /// \c mathcommand needs no termination (such as {} or space).
98 bool mathnotermination() const { return flags_ & CharInfoMathNoTermination; }
101 /// LaTeX command (text mode) for this character
102 trivdocstring textcommand_;
103 /// LaTeX command (math mode) for this character
104 trivdocstring mathcommand_;
105 /// Needed LaTeX preamble (or feature) for text mode
106 trivstring textpreamble_;
107 /// Needed LaTeX preamble (or feature) for math mode
108 trivstring mathpreamble_;
110 trivstring tipashortcut_;
117 * An encoding as defined in lib/encodings.
118 * All const methods are thread-safe, so the caller does not need any locking.
119 * This property must be kept when changing the class.
123 /// Which LaTeX package handles this encoding?
130 /// Represent any of the above packages
131 static int const any;
133 Encoding() : fixedwidth_(true), unsafe_(false), forced_(nullptr),
134 start_encodable_(0), package_(none), complete_(false) {}
136 Encoding(std::string const & n, std::string const & l,
137 std::string const & g, std::string const & i,
138 bool f, bool u, Package p);
142 std::string const name() const { return name_; }
144 std::string const latexName() const { return latexName_; }
146 std::string const guiName() const { return guiName_; }
148 std::string const iconvName() const { return iconvName_; }
150 bool hasFixedWidth() const { return fixedwidth_; }
152 bool unsafe() const { return unsafe_; }
153 /// \p c is representable in this encoding without a LaTeX macro
154 bool encodable(char_type c) const;
156 * Convert \p c to something that LaTeX can understand.
157 * This is either the character itself (if it is representable
158 * in this encoding), or a LaTeX macro.
159 * If the character is not representable in this encoding, but no
160 * LaTeX macro is known, a warning is given of lyxerr, and the
161 * character is returned.
162 * \return the converted character and a flag indicating whether
163 * the command needs to be terminated by {} or a space.
165 std::pair<docstring, bool> latexChar(char_type c) const;
167 * Convert \p input to something that LaTeX can understand.
168 * This is either the string itself (if it is representable
169 * in this encoding), or a LaTeX macro.
170 * If a character is not representable in this encoding, but no
171 * LaTeX macro is known, a warning is given of lyxerr, and the
172 * character is returned in the second string of the pair and
173 * omitted in the first.
174 * \p dryrun specifies whether the string is used within source
175 * preview (which yields a special warning).
177 std::pair<docstring, docstring> latexString(docstring const & input,
178 bool dryrun = false) const;
179 /// Which LaTeX package handles this encoding?
180 Package package() const { return package_; }
181 /// A list of all characters usable in this encoding
182 std::vector<char_type> symbolsList() const;
185 * Do we have to output this character as LaTeX command in any case?
186 * This is true if the "force" flag is set.
187 * We need this if the inputencoding does not support a certain glyph.
189 bool isForced(char_type c) const;
193 trivstring latexName_;
197 trivstring iconvName_;
198 /// Is this a fixed width encoding?
200 /// Is this encoding TeX unsafe, e.g. control characters like {, }
201 /// and \\ may appear in high bytes?
204 typedef std::set<char_type> CharSet;
205 /// Set of UCS4 characters that we can encode (for singlebyte
208 /// Set of UCS4 characters that we can't encode
209 CharSet const * forced_;
210 /// All code points below this are encodable. This helps us to avoid
211 /// lokup of ASCII characters in encodable_ and gives about 1 sec
212 /// speedup on export of the Userguide.
213 char_type start_encodable_;
214 /// Which LaTeX package handles this encoding?
217 * If this is true the stored information about the encoding covers
218 * all encodable characters. We set this to false initially so that
219 * we only need to query iconv for the actually used encodings.
220 * This is needed especially for the multibyte encodings, if we
221 * complete all encoding info on startup it takes 2-3 minutes.
229 typedef std::set<char_type> MathCommandSet;
231 typedef std::set<char_type> TextCommandSet;
233 typedef std::set<char_type> MathSymbolSet;
235 typedef std::map<trivstring, Encoding> EncodingList;
236 /// iterator to iterate over all encodings.
237 /// We hide the fact that our encoding list is implemented as a map.
238 class const_iterator : public EncodingList::const_iterator {
239 typedef EncodingList::const_iterator base;
241 const_iterator() : base() {}
242 const_iterator(base const & b) : base(b) {}
243 Encoding const & operator*() const { return base::operator*().second; }
244 Encoding const * operator->() const { return &(base::operator*().second); }
248 /// Read the encodings.
249 /// \param encfile encodings definition file
250 /// \param symbolsfile unicode->LaTeX mapping file
251 void read(support::FileName const & encfile,
252 support::FileName const & symbolsfile);
253 /// Get encoding from LyX name \p name
255 fromLyXName(std::string const & name, bool allowUnsafe = false) const;
256 /// Get encoding from LaTeX name \p name and package \p package
257 Encoding const * fromLaTeXName(std::string const & name,
258 int package = Encoding::any, bool allowUnsafe = false) const;
259 /// Get encoding from iconv name \p name and package \p package
260 Encoding const * fromIconvName(std::string const & name,
261 int package = Encoding::any, bool allowUnsafe = false) const;
264 const_iterator begin() const { return encodinglist.begin(); }
266 const_iterator end() const { return encodinglist.end(); }
268 /// Accessor for the unicode information table.
269 static CharInfo const & unicodeCharInfo(char_type c);
270 /// Is this a combining char?
271 static bool isCombiningChar(char_type c);
272 /// Return the TIPA shortcut
273 static std::string const TIPAShortcut(char_type c);
275 * Test, if \p c is a supported Greek or Cyrillic letter.
276 * Return script macro name or empty string.
278 static std::string const isKnownScriptChar(char_type const c);
279 /// Does \p fontenc support characters in \p script?
280 static bool fontencSupportsScript(std::string const & fontenc,
281 std::string const & script);
283 * Do we have to display in italics this character when in mathmode?
284 * This is true if the "mathalpha" flag is set. We use this for
285 * letters and accented characters that are output as math commands.
287 static bool isMathAlpha(char_type c);
289 * Do we have to wrap in \text this character when in mathmode?
290 * This is true if \p c is not ascii and the "mathalpha" flag is not
291 * set and a mathcommand is not defined in the unicodesymbols file.
293 static bool isUnicodeTextOnly(char_type c);
295 * Register \p c as a mathmode command.
297 static void addMathCmd(char_type c) { mathcmd.insert(c); }
299 * Register \p c as a textmode command.
301 static void addTextCmd(char_type c) { textcmd.insert(c); }
303 * Register \p c as a mathmode symbol.
305 static void addMathSym(char_type c) { mathsym.insert(c); }
307 * Tell whether \p c is registered as a mathmode command.
309 static bool isMathCmd(char_type c) { return mathcmd.count(c); }
311 * Tell whether \p c is registered as a textmode command.
313 static bool isTextCmd(char_type c) { return textcmd.count(c); }
315 * Tell whether \p c is registered as a mathmode symbol.
317 static bool isMathSym(char_type c) { return mathsym.count(c); }
319 * If \p c cannot be encoded in the given \p encoding, convert
320 * it to something that LaTeX can understand in mathmode.
321 * \p needsTermination indicates whether the command needs to be
322 * terminated by {} or a space.
323 * \return whether \p command is a mathmode command
325 static bool latexMathChar(char_type c, bool mathmode,
326 Encoding const * encoding, docstring & command,
327 bool & needsTermination);
329 * Convert the LaTeX command in \p cmd to the corresponding unicode
330 * point and set \p combining to true if it is a combining symbol.
331 * \p needsTermination indicates whether the command needs to be
332 * terminated by {} or a space.
334 static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
335 bool & combining, bool & needsTermination,
336 std::set<std::string> * req = nullptr);
345 * Convert the LaTeX commands in \p cmd and \return a docstring
346 * of corresponding unicode points. The conversion stops at the
347 * first command which could not be converted, and the remaining
348 * unconverted commands are returned in \p rem.
349 * The \p cmdtype parameter can be used to limit recognized
350 * commands to math or text mode commands only.
351 * \p needsTermination indicates whether the command needs to be
352 * terminated by {} or a space.
354 static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
355 bool & needsTermination, docstring & rem,
356 std::set<std::string> * req = nullptr);
360 EncodingList encodinglist;
362 static MathCommandSet mathcmd;
364 static TextCommandSet textcmd;
366 static MathSymbolSet mathsym;
369 extern Encodings encodings;