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 & 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);
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); }
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
95 bool textFeature() const { return flags_ & CharInfoTextFeature; }
96 /// Is \c mathPreamble a feature known by LaTeXFeatures, or a raw LaTeX
98 bool mathFeature() const { return flags_ & CharInfoMathFeature; }
99 /// Always force the LaTeX command, even if the encoding contains
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; }
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; }
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_;
125 trivstring tipa_shortcut_;
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.
138 /// Which LaTeX package handles this encoding?
145 /// Represent any of the above packages
146 static int const any;
148 Encoding() : fixedwidth_(true), unsafe_(false), forced_(nullptr),
149 start_encodable_(0), package_(none), complete_(false) {}
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);
157 std::string const name() const { return name_; }
159 std::string const latexName() const { return latexName_; }
161 std::string const guiName() const { return guiName_; }
163 std::string const iconvName() const { return iconvName_; }
165 bool hasFixedWidth() const { return fixedwidth_; }
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;
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.
180 std::pair<docstring, bool> latexChar(char_type c) const;
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).
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;
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.
204 bool isForced(char_type c) const;
208 trivstring latexName_;
212 trivstring iconvName_;
213 /// Is this a fixed width encoding?
215 /// Is this encoding TeX unsafe, e.g. control characters like {, }
216 /// and \\ may appear in high bytes?
219 typedef std::set<char_type> CharSet;
220 /// Set of UCS4 characters that we can encode (for singlebyte
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?
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.
244 typedef std::set<char_type> MathCommandSet;
246 typedef std::set<char_type> TextCommandSet;
248 typedef std::set<char_type> MathSymbolSet;
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;
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); }
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
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;
279 const_iterator begin() const { return encodinglist.begin(); }
281 const_iterator end() const { return encodinglist.end(); }
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);
290 * Test, if \p c is a supported Greek or Cyrillic letter.
291 * Return script macro name or empty string.
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);
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.
302 static bool isMathAlpha(char_type c);
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.
308 static bool isUnicodeTextOnly(char_type c);
310 * Register \p c as a mathmode command.
312 static void addMathCmd(char_type c) { mathcmd.insert(c); }
314 * Register \p c as a textmode command.
316 static void addTextCmd(char_type c) { textcmd.insert(c); }
318 * Register \p c as a mathmode symbol.
320 static void addMathSym(char_type c) { mathsym.insert(c); }
322 * Tell whether \p c is registered as a mathmode command.
324 static bool isMathCmd(char_type c) { return mathcmd.count(c); }
326 * Tell whether \p c is registered as a textmode command.
328 static bool isTextCmd(char_type c) { return textcmd.count(c); }
330 * Tell whether \p c is registered as a mathmode symbol.
332 static bool isMathSym(char_type c) { return mathsym.count(c); }
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
340 static bool latexMathChar(char_type c, bool mathmode,
341 Encoding const * encoding, docstring & command,
342 bool & needsTermination);
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.
349 static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
350 bool & combining, bool & needsTermination,
351 std::set<std::string> * req = nullptr);
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.
369 static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
370 bool & needsTermination, docstring & rem,
371 std::set<std::string> * req = nullptr);
375 EncodingList encodinglist;
377 static MathCommandSet mathcmd;
379 static TextCommandSet textcmd;
381 static MathSymbolSet mathsym;
384 extern Encodings encodings;