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), complete_(false) {}
131 Encoding(std::string const & n, std::string const & l,
132 std::string const & g, std::string const & i,
133 bool f, bool u, Package p);
137 std::string const name() const { return name_; }
139 std::string const latexName() const { return latexName_; }
141 std::string const guiName() const { return guiName_; }
143 std::string const iconvName() const { return iconvName_; }
145 bool hasFixedWidth() const { return fixedwidth_; }
147 bool unsafe() const { return unsafe_; }
148 /// \p c is representable in this encoding without a LaTeX macro
149 bool encodable(char_type c) const;
151 * Convert \p c to something that LaTeX can understand.
152 * This is either the character itself (if it is representable
153 * in this encoding), or a LaTeX macro.
154 * If the character is not representable in this encoding, but no
155 * LaTeX macro is known, a warning is given of lyxerr, and the
156 * character is returned.
157 * \return the converted character and a flag indicating whether
158 * the command needs to be terminated by {} or a space.
160 std::pair<docstring, bool> latexChar(char_type c) const;
162 * Convert \p input to something that LaTeX can understand.
163 * This is either the string itself (if it is representable
164 * in this encoding), or a LaTeX macro.
165 * If a character is not representable in this encoding, but no
166 * LaTeX macro is known, a warning is given of lyxerr, and the
167 * character is returned in the second string of the pair and
168 * omitted in the first.
169 * \p dryrun specifies whether the string is used within source
170 * preview (which yields a special warning).
172 std::pair<docstring, docstring> latexString(docstring const & input,
173 bool dryrun = false) const;
174 /// Which LaTeX package handles this encoding?
175 Package package() const { return package_; }
176 /// A list of all characters usable in this encoding
177 std::vector<char_type> symbolsList() const;
180 * Do we have to output this character as LaTeX command in any case?
181 * This is true if the "force" flag is set.
182 * We need this if the inputencoding does not support a certain glyph.
184 bool isForced(char_type c) const;
188 trivstring latexName_;
192 trivstring iconvName_;
193 /// Is this a fixed width encoding?
195 /// Is this encoding TeX unsafe, e.g. control characters like {, }
196 /// and \\ may appear in high bytes?
199 typedef std::set<char_type> CharSet;
200 /// Set of UCS4 characters that we can encode (for singlebyte
203 /// Set of UCS4 characters that we can't encode
204 CharSet const * forced_;
205 /// All code points below this are encodable. This helps us to avoid
206 /// lokup of ASCII characters in encodable_ and gives about 1 sec
207 /// speedup on export of the Userguide.
208 char_type start_encodable_;
209 /// Which LaTeX package handles this encoding?
212 * If this is true the stored information about the encoding covers
213 * all encodable characters. We set this to false initially so that
214 * we only need to query iconv for the actually used encodings.
215 * This is needed especially for the multibyte encodings, if we
216 * complete all encoding info on startup it takes 2-3 minutes.
224 typedef std::set<char_type> MathCommandSet;
226 typedef std::set<char_type> TextCommandSet;
228 typedef std::set<char_type> MathSymbolSet;
230 typedef std::map<trivstring, Encoding> EncodingList;
231 /// iterator to iterate over all encodings.
232 /// We hide the fact that our encoding list is implemented as a map.
233 class const_iterator : public EncodingList::const_iterator {
234 typedef EncodingList::const_iterator base;
236 const_iterator() : base() {}
237 const_iterator(base const & b) : base(b) {}
238 Encoding const & operator*() const { return base::operator*().second; }
239 Encoding const * operator->() const { return &(base::operator*().second); }
243 /// Read the encodings.
244 /// \param encfile encodings definition file
245 /// \param symbolsfile unicode->LaTeX mapping file
246 void read(support::FileName const & encfile,
247 support::FileName const & symbolsfile);
248 /// Get encoding from LyX name \p name
250 fromLyXName(std::string const & name, bool allowUnsafe = false) const;
251 /// Get encoding from LaTeX name \p name and package \p package
252 Encoding const * fromLaTeXName(std::string const & name,
253 int const & package = Encoding::any, bool allowUnsafe = false) const;
254 /// Get encoding from iconv name \p name and package \p package
255 Encoding const * fromIconvName(std::string const & name,
256 int const & package = Encoding::any, bool allowUnsafe = false) const;
259 const_iterator begin() const { return encodinglist.begin(); }
261 const_iterator end() const { return encodinglist.end(); }
263 /// Accessor for the unicode information table.
264 static CharInfo const & unicodeCharInfo(char_type c);
265 /// Is this a combining char?
266 static bool isCombiningChar(char_type c);
267 /// Return the TIPA shortcut
268 static std::string const TIPAShortcut(char_type c);
270 * Is this a known char from some language?
271 * If \p preamble is empty and code point \p c is known to belong
272 * to a supported script, true is returned and \p preamble is set
273 * to the corresponding entry in the unicodesymbols file.
274 * If \p preamble is not empty, a check is made whether code point
275 * \p c is a known character matching the preamble entry.
277 static bool isKnownScriptChar(char_type const c, std::string & preamble);
279 * Do we have to display in italics this character when in mathmode?
280 * This is true if the "mathalpha" flag is set. We use this for
281 * letters and accented characters that are output as math commands.
283 static bool isMathAlpha(char_type c);
285 * Register \p c as a mathmode command.
287 static void addMathCmd(char_type c) { mathcmd.insert(c); }
289 * Register \p c as a textmode command.
291 static void addTextCmd(char_type c) { textcmd.insert(c); }
293 * Register \p c as a mathmode symbol.
295 static void addMathSym(char_type c) { mathsym.insert(c); }
297 * Tell whether \p c is registered as a mathmode command.
299 static bool isMathCmd(char_type c) { return mathcmd.count(c); }
301 * Tell whether \p c is registered as a textmode command.
303 static bool isTextCmd(char_type c) { return textcmd.count(c); }
305 * Tell whether \p c is registered as a mathmode symbol.
307 static bool isMathSym(char_type c) { return mathsym.count(c); }
309 * If \p c cannot be encoded in the given \p encoding, convert
310 * it to something that LaTeX can understand in mathmode.
311 * \p needsTermination indicates whether the command needs to be
312 * terminated by {} or a space.
313 * \return whether \p command is a mathmode command
315 static bool latexMathChar(char_type c, bool mathmode,
316 Encoding const * encoding, docstring & command,
317 bool & needsTermination);
319 * Convert the LaTeX command in \p cmd to the corresponding unicode
320 * point and set \p combining to true if it is a combining symbol.
321 * \p needsTermination indicates whether the command needs to be
322 * terminated by {} or a space.
324 static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
325 bool & combining, bool & needsTermination,
326 std::set<std::string> * req = 0);
335 * Convert the LaTeX commands in \p cmd and \return a docstring
336 * of corresponding unicode points. The conversion stops at the
337 * first command which could not be converted, and the remaining
338 * unconverted commands are returned in \p rem.
339 * The \p cmdtype parameter can be used to limit recognized
340 * commands to math or text mode commands only.
341 * \p needsTermination indicates whether the command needs to be
342 * terminated by {} or a space.
344 static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
345 bool & needsTermination, docstring & rem,
346 std::set<std::string> * req = 0);
350 EncodingList encodinglist;
352 static MathCommandSet mathcmd;
354 static TextCommandSet textcmd;
356 static MathSymbolSet mathsym;
359 extern Encodings encodings;