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() throw() {}
32 virtual const char * what() const throw();
34 char_type failed_char;
42 CharInfoCombining = 1,
44 CharInfoTextFeature = 2,
46 CharInfoMathFeature = 4,
50 CharInfoTextNoTermination = 16,
52 CharInfoMathNoTermination = 32,
54 CharInfoForceSelected = 64,
58 /// Information about a single UCS4 character
61 CharInfo() : flags_(0) {}
63 docstring const & textcommand, docstring const & mathcommand,
64 std::string const & textpreamble, std::string const & mathpreamble,
65 std::string const & tipashortcut, unsigned int flags);
66 // we assume that at least one command is nonempty when using unicodesymbols
67 bool isUnicodeSymbol() const { return !textcommand_.empty() || !mathcommand_.empty(); }
68 /// LaTeX command (text mode) for this character
69 docstring const textcommand() const { return textcommand_; }
70 /// LaTeX command (math mode) for this character
71 docstring mathcommand() const { return mathcommand_; }
72 /// Needed LaTeX preamble (or feature) for text mode
73 std::string textpreamble() const { return textpreamble_; }
74 /// Needed LaTeX preamble (or feature) for math mode
75 std::string mathpreamble() const { return mathpreamble_; }
76 /// Is this a combining character?
77 bool combining() const { return flags_ & CharInfoCombining ? true : false; }
78 /// Is \c textpreamble a feature known by LaTeXFeatures, or a raw LaTeX
80 bool textfeature() const { return flags_ & CharInfoTextFeature ? true : false; }
81 /// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX
83 bool mathfeature() const { return flags_ & CharInfoMathFeature ? true : false; }
84 /// Always force the LaTeX command, even if the encoding contains
86 bool force() const { return flags_ & CharInfoForce ? true : false; }
87 /// Force the LaTeX command for some encodings?
88 bool forceselected() const { return flags_ & CharInfoForceSelected ? true : false; }
90 std::string const tipashortcut() const { return tipashortcut_; }
91 /// \c textcommand needs no termination (such as {} or space).
92 bool textnotermination() const { return flags_ & CharInfoTextNoTermination ? true : false; }
93 /// \c mathcommand needs no termination (such as {} or space).
94 bool mathnotermination() const { return flags_ & CharInfoMathNoTermination ? true : false; }
97 /// LaTeX command (text mode) for this character
98 trivdocstring textcommand_;
99 /// LaTeX command (math mode) for this character
100 trivdocstring mathcommand_;
101 /// Needed LaTeX preamble (or feature) for text mode
102 trivstring textpreamble_;
103 /// Needed LaTeX preamble (or feature) for math mode
104 trivstring mathpreamble_;
106 trivstring tipashortcut_;
113 * An encoding as defined in lib/encodings.
114 * All const methods are thread-safe, so the caller does not need any locking.
115 * This property must be kept when changing the class.
119 /// Which LaTeX package handles this encoding?
126 /// Represent any of the above packages
127 static int const any;
129 Encoding() : fixedwidth_(true), unsafe_(false), start_encodable_(0),
130 package_(none), complete_(false) {}
132 Encoding(std::string const & n, std::string const & l,
133 std::string const & g, std::string const & i,
134 bool f, bool u, Package p);
138 std::string const name() const { return name_; }
140 std::string const latexName() const { return latexName_; }
142 std::string const guiName() const { return guiName_; }
144 std::string const iconvName() const { return iconvName_; }
146 bool hasFixedWidth() const { return fixedwidth_; }
148 bool unsafe() const { return unsafe_; }
149 /// \p c is representable in this encoding without a LaTeX macro
150 bool encodable(char_type c) const;
152 * Convert \p c to something that LaTeX can understand.
153 * This is either the character itself (if it is representable
154 * in this encoding), or a LaTeX macro.
155 * If the character is not representable in this encoding, but no
156 * LaTeX macro is known, a warning is given of lyxerr, and the
157 * character is returned.
158 * \return the converted character and a flag indicating whether
159 * the command needs to be terminated by {} or a space.
161 std::pair<docstring, bool> latexChar(char_type c) const;
163 * Convert \p input to something that LaTeX can understand.
164 * This is either the string itself (if it is representable
165 * in this encoding), or a LaTeX macro.
166 * If a character is not representable in this encoding, but no
167 * LaTeX macro is known, a warning is given of lyxerr, and the
168 * character is returned in the second string of the pair and
169 * omitted in the first.
170 * \p dryrun specifies whether the string is used within source
171 * preview (which yields a special warning).
173 std::pair<docstring, docstring> latexString(docstring const & input,
174 bool dryrun = false) const;
175 /// Which LaTeX package handles this encoding?
176 Package package() const { return package_; }
177 /// A list of all characters usable in this encoding
178 std::vector<char_type> symbolsList() const;
181 * Do we have to output this character as LaTeX command in any case?
182 * This is true if the "force" flag is set.
183 * We need this if the inputencoding does not support a certain glyph.
185 bool isForced(char_type c) const;
189 trivstring latexName_;
193 trivstring iconvName_;
194 /// Is this a fixed width encoding?
196 /// Is this encoding TeX unsafe, e.g. control characters like {, }
197 /// and \\ may appear in high bytes?
200 typedef std::set<char_type> CharSet;
201 /// Set of UCS4 characters that we can encode (for singlebyte
204 /// Set of UCS4 characters that we can't encode
205 CharSet const * forced_;
206 /// All code points below this are encodable. This helps us to avoid
207 /// lokup of ASCII characters in encodable_ and gives about 1 sec
208 /// speedup on export of the Userguide.
209 char_type start_encodable_;
210 /// Which LaTeX package handles this encoding?
213 * If this is true the stored information about the encoding covers
214 * all encodable characters. We set this to false initially so that
215 * we only need to query iconv for the actually used encodings.
216 * This is needed especially for the multibyte encodings, if we
217 * complete all encoding info on startup it takes 2-3 minutes.
225 typedef std::set<char_type> MathCommandSet;
227 typedef std::set<char_type> TextCommandSet;
229 typedef std::set<char_type> MathSymbolSet;
231 typedef std::map<trivstring, Encoding> EncodingList;
232 /// iterator to iterate over all encodings.
233 /// We hide the fact that our encoding list is implemented as a map.
234 class const_iterator : public EncodingList::const_iterator {
235 typedef EncodingList::const_iterator base;
237 const_iterator() : base() {}
238 const_iterator(base const & b) : base(b) {}
239 Encoding const & operator*() const { return base::operator*().second; }
240 Encoding const * operator->() const { return &(base::operator*().second); }
244 /// Read the encodings.
245 /// \param encfile encodings definition file
246 /// \param symbolsfile unicode->LaTeX mapping file
247 void read(support::FileName const & encfile,
248 support::FileName const & symbolsfile);
249 /// Get encoding from LyX name \p name
251 fromLyXName(std::string const & name, bool allowUnsafe = false) const;
252 /// Get encoding from LaTeX name \p name and package \p package
253 Encoding const * fromLaTeXName(std::string const & name,
254 int const & package = Encoding::any, bool allowUnsafe = false) const;
255 /// Get encoding from iconv name \p name and package \p package
256 Encoding const * fromIconvName(std::string const & name,
257 int const & package = Encoding::any, bool allowUnsafe = false) const;
260 const_iterator begin() const { return encodinglist.begin(); }
262 const_iterator end() const { return encodinglist.end(); }
264 /// Accessor for the unicode information table.
265 static CharInfo const & unicodeCharInfo(char_type c);
266 /// Is this a combining char?
267 static bool isCombiningChar(char_type c);
268 /// Return the TIPA shortcut
269 static std::string const TIPAShortcut(char_type c);
271 * Is this a known char from some language?
272 * If \p preamble is empty and code point \p c is known to belong
273 * to a supported script, true is returned and \p preamble is set
274 * to the corresponding entry in the unicodesymbols file.
275 * If \p preamble is not empty, a check is made whether code point
276 * \p c is a known character matching the preamble entry.
278 static bool isKnownScriptChar(char_type const c, std::string & preamble);
280 * Do we have to display in italics this character when in mathmode?
281 * This is true if the "mathalpha" flag is set. We use this for
282 * letters and accented characters that are output as math commands.
284 static bool isMathAlpha(char_type c);
286 * Register \p c as a mathmode command.
288 static void addMathCmd(char_type c) { mathcmd.insert(c); }
290 * Register \p c as a textmode command.
292 static void addTextCmd(char_type c) { textcmd.insert(c); }
294 * Register \p c as a mathmode symbol.
296 static void addMathSym(char_type c) { mathsym.insert(c); }
298 * Tell whether \p c is registered as a mathmode command.
300 static bool isMathCmd(char_type c) { return mathcmd.count(c); }
302 * Tell whether \p c is registered as a textmode command.
304 static bool isTextCmd(char_type c) { return textcmd.count(c); }
306 * Tell whether \p c is registered as a mathmode symbol.
308 static bool isMathSym(char_type c) { return mathsym.count(c); }
310 * If \p c cannot be encoded in the given \p encoding, convert
311 * it to something that LaTeX can understand in mathmode.
312 * \p needsTermination indicates whether the command needs to be
313 * terminated by {} or a space.
314 * \return whether \p command is a mathmode command
316 static bool latexMathChar(char_type c, bool mathmode,
317 Encoding const * encoding, docstring & command,
318 bool & needsTermination);
320 * Convert the LaTeX command in \p cmd to the corresponding unicode
321 * point and set \p combining to true if it is a combining symbol.
322 * \p needsTermination indicates whether the command needs to be
323 * terminated by {} or a space.
325 static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
326 bool & combining, bool & needsTermination,
327 std::set<std::string> * req = 0);
336 * Convert the LaTeX commands in \p cmd and \return a docstring
337 * of corresponding unicode points. The conversion stops at the
338 * first command which could not be converted, and the remaining
339 * unconverted commands are returned in \p rem.
340 * The \p cmdtype parameter can be used to limit recognized
341 * commands to math or text mode commands only.
342 * \p needsTermination indicates whether the command needs to be
343 * terminated by {} or a space.
345 static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
346 bool & needsTermination, docstring & rem,
347 std::set<std::string> * req = 0);
351 EncodingList encodinglist;
353 static MathCommandSet mathcmd;
355 static TextCommandSet textcmd;
357 static MathSymbolSet mathsym;
360 extern Encodings encodings;