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/types.h"
25 namespace support { class FileName; }
27 class EncodingException : public std::exception {
29 EncodingException(char_type c);
30 virtual ~EncodingException() throw() {}
31 virtual const char * what() const throw();
33 char_type failed_char;
41 CharInfoCombining = 1,
43 CharInfoTextFeature = 2,
45 CharInfoMathFeature = 4,
49 CharInfoTextNoTermination = 16,
51 CharInfoMathNoTermination = 32,
53 CharInfoForceSelected = 64,
57 /// Information about a single UCS4 character
60 CharInfo() : flags_(0) {}
62 docstring const textcommand, docstring const mathcommand,
63 std::string const textpreamble, std::string const mathpreamble,
64 std::string const tipashortcut, unsigned int flags);
65 // we assume that at least one command is nonempty when using unicodesymbols
66 bool isUnicodeSymbol() const { return !textcommand_.empty() || !mathcommand_.empty(); }
67 /// LaTeX command (text mode) for this character
68 docstring const textcommand() const { return textcommand_; }
69 /// LaTeX command (math mode) for this character
70 docstring mathcommand() const { return mathcommand_; }
71 /// Needed LaTeX preamble (or feature) for text mode
72 std::string textpreamble() const { return textpreamble_; }
73 /// Needed LaTeX preamble (or feature) for math mode
74 std::string mathpreamble() const { return mathpreamble_; }
75 /// Is this a combining character?
76 bool combining() const { return flags_ & CharInfoCombining ? true : false; }
77 /// Is \c textpreamble a feature known by LaTeXFeatures, or a raw LaTeX
79 bool textfeature() const { return flags_ & CharInfoTextFeature ? true : false; }
80 /// Is \c mathpreamble a feature known by LaTeXFeatures, or a raw LaTeX
82 bool mathfeature() const { return flags_ & CharInfoMathFeature ? true : false; }
83 /// Always force the LaTeX command, even if the encoding contains
85 bool force() const { return flags_ & CharInfoForce ? true : false; }
86 /// Force the LaTeX command for some encodings?
87 bool forceselected() const { return flags_ & CharInfoForceSelected ? true : false; }
89 std::string const tipashortcut() const { return tipashortcut_; }
90 /// \c textcommand needs no termination (such as {} or space).
91 bool textnotermination() const { return flags_ & CharInfoTextNoTermination ? true : false; }
92 /// \c mathcommand needs no termination (such as {} or space).
93 bool mathnotermination() const { return flags_ & CharInfoMathNoTermination ? true : false; }
96 /// LaTeX command (text mode) for this character
97 docstring textcommand_;
98 /// LaTeX command (math mode) for this character
99 docstring mathcommand_;
100 /// Needed LaTeX preamble (or feature) for text mode
101 std::string textpreamble_;
102 /// Needed LaTeX preamble (or feature) for math mode
103 std::string mathpreamble_;
105 std::string tipashortcut_;
114 /// Which LaTeX package handles this encoding?
121 /// Represent any of the above packages
122 static int const any;
124 Encoding() : fixedwidth_(true), unsafe_(false), complete_(false) {}
126 Encoding(std::string const & n, std::string const & l,
127 std::string const & g, std::string const & i,
128 bool f, bool u, Package p);
132 std::string const & name() const { return name_; }
134 std::string const & latexName() const { return latexName_; }
136 std::string const & guiName() const { return guiName_; }
138 std::string const & iconvName() const { return iconvName_; }
140 bool hasFixedWidth() const { return fixedwidth_; }
142 bool unsafe() const { return unsafe_; }
143 /// \p c is representable in this encoding without a LaTeX macro
144 bool encodable(char_type c) const;
146 * Convert \p c to something that LaTeX can understand.
147 * This is either the character itself (if it is representable
148 * in this encoding), or a LaTeX macro.
149 * If the character is not representable in this encoding, but no
150 * LaTeX macro is known, a warning is given of lyxerr, and the
151 * character is returned.
152 * \return the converted character and a flag indicating whether
153 * the command needs to be terminated by {} or a space.
155 std::pair<docstring, bool> latexChar(char_type c) const;
157 * Convert \p input to something that LaTeX can understand.
158 * This is either the string itself (if it is representable
159 * in this encoding), or a LaTeX macro.
160 * If a character is not representable in this encoding, but no
161 * LaTeX macro is known, a warning is given of lyxerr, and the
162 * character is returned in the second string of the pair and
163 * omitted in the first.
164 * \p dryrun specifies whether the string is used within source
165 * preview (which yields a special warning).
167 std::pair<docstring, docstring> latexString(docstring const input,
168 bool dryrun = false) const;
169 /// Which LaTeX package handles this encoding?
170 Package package() const { return package_; }
171 /// A list of all characters usable in this encoding
172 std::vector<char_type> symbolsList() const;
175 * Do we have to output this character as LaTeX command in any case?
176 * This is true if the "force" flag is set.
177 * We need this if the inputencoding does not support a certain glyph.
179 bool isForced(char_type c) const;
183 std::string latexName_;
185 std::string guiName_;
187 std::string iconvName_;
188 /// Is this a fixed width encoding?
190 /// Is this encoding TeX unsafe, e.g. control characters like {, }
191 /// and \\ may appear in high bytes?
194 typedef std::set<char_type> CharSet;
195 /// Set of UCS4 characters that we can encode (for singlebyte
197 mutable CharSet encodable_;
198 /// Set of UCS4 characters that we can't encode
199 CharSet const * forced_;
200 /// All code points below this are encodable. This helps us to avoid
201 /// lokup of ASCII characters in encodable_ and gives about 1 sec
202 /// speedup on export of the Userguide.
203 mutable char_type start_encodable_;
204 /// Which LaTeX package handles this encoding?
207 * If this is true the stored information about the encoding covers
208 * all encodable characters. We set this to false initially so that
209 * we only need to query iconv for the actually used encodings.
210 * This is needed especially for the multibyte encodings, if we
211 * complete all encoding info on startup it takes 2-3 minutes.
213 mutable bool complete_;
219 typedef std::set<char_type> MathCommandSet;
221 typedef std::set<char_type> TextCommandSet;
223 typedef std::set<char_type> MathSymbolSet;
225 typedef std::map<std::string, Encoding> EncodingList;
226 /// iterator to iterate over all encodings.
227 /// We hide the fact that our encoding list is implemented as a map.
228 class const_iterator : public EncodingList::const_iterator {
229 typedef EncodingList::const_iterator base;
231 const_iterator() : base() {}
232 const_iterator(base const & b) : base(b) {}
233 Encoding const & operator*() const { return base::operator*().second; }
234 Encoding const * operator->() const { return &(base::operator*().second); }
238 /// Read the encodings.
239 /// \param encfile encodings definition file
240 /// \param symbolsfile unicode->LaTeX mapping file
241 void read(support::FileName const & encfile,
242 support::FileName const & symbolsfile);
243 /// Get encoding from LyX name \p name
245 fromLyXName(std::string const & name, bool allowUnsafe = false) const;
246 /// Get encoding from LaTeX name \p name and package \p package
247 Encoding const * fromLaTeXName(std::string const & name,
248 int const & package = Encoding::any, bool allowUnsafe = false) const;
249 /// Get encoding from iconv name \p name and package \p package
250 Encoding const * fromIconvName(std::string const & name,
251 int const & package = Encoding::any, bool allowUnsafe = false) const;
254 const_iterator begin() const { return encodinglist.begin(); }
256 const_iterator end() const { return encodinglist.end(); }
270 static bool isHebrewComposeChar(char_type c);
272 static bool isArabicComposeChar(char_type c);
274 static bool isArabicSpecialChar(char_type c);
276 static bool isArabicChar(char_type c);
277 /// Accessor for the unicode information table.
278 static CharInfo const & unicodeCharInfo(char_type c);
280 static char_type transformChar(char_type c, LetterForm form);
281 /// Is this a combining char?
282 static bool isCombiningChar(char_type c);
283 /// Return the TIPA shortcut
284 static std::string const TIPAShortcut(char_type c);
286 * Is this a known char from some language?
287 * If \p preamble is empty and code point \p c is known to belong
288 * to a supported script, true is returned and \p preamble is set
289 * to the corresponding entry in the unicodesymbols file.
290 * If \p preamble is not empty, a check is made whether code point
291 * \p c is a known character matching the preamble entry.
293 static bool isKnownScriptChar(char_type const c, std::string & preamble);
295 * Do we have to display in italics this character when in mathmode?
296 * This is true if the "mathalpha" flag is set. We use this for
297 * letters and accented characters that are output as math commands.
299 static bool isMathAlpha(char_type c);
301 * Register \p c as a mathmode command.
303 static void addMathCmd(char_type c) { mathcmd.insert(c); }
305 * Register \p c as a textmode command.
307 static void addTextCmd(char_type c) { textcmd.insert(c); }
309 * Register \p c as a mathmode symbol.
311 static void addMathSym(char_type c) { mathsym.insert(c); }
313 * Tell whether \p c is registered as a mathmode command.
315 static bool isMathCmd(char_type c) { return mathcmd.count(c); }
317 * Tell whether \p c is registered as a textmode command.
319 static bool isTextCmd(char_type c) { return textcmd.count(c); }
321 * Tell whether \p c is registered as a mathmode symbol.
323 static bool isMathSym(char_type c) { return mathsym.count(c); }
325 * If \p c cannot be encoded in the given \p encoding, convert
326 * it to something that LaTeX can understand in mathmode.
327 * \p needsTermination indicates whether the command needs to be
328 * terminated by {} or a space.
329 * \return whether \p command is a mathmode command
331 static bool latexMathChar(char_type c, bool mathmode,
332 Encoding const * encoding, docstring & command,
333 bool & needsTermination);
335 * Convert the LaTeX command in \p cmd to the corresponding unicode
336 * point and set \p combining to true if it is a combining symbol.
337 * \p needsTermination indicates whether the command needs to be
338 * terminated by {} or a space.
340 static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
341 bool & combining, bool & needsTermination,
342 std::set<std::string> * req = 0);
351 * Convert the LaTeX commands in \p cmd and \return a docstring
352 * of corresponding unicode points. The conversion stops at the
353 * first command which could not be converted, and the remaining
354 * unconverted commands are returned in \p rem.
355 * The \p cmdtype parameter can be used to limit recognized
356 * commands to math or text mode commands only.
357 * \p needsTermination indicates whether the command needs to be
358 * terminated by {} or a space.
360 static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
361 bool & needsTermination, docstring & rem,
362 std::set<std::string> * req = 0);
366 EncodingList encodinglist;
368 static MathCommandSet mathcmd;
370 static TextCommandSet textcmd;
372 static MathSymbolSet mathsym;
375 extern Encodings encodings;