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; }
30 class EncodingException : public std::exception {
32 EncodingException(char_type c);
33 virtual ~EncodingException() throw() {}
34 virtual const char * what() const throw();
36 char_type failed_char;
45 /// Which LaTeX package handles this encoding?
55 Encoding(std::string const & n, std::string const & l,
56 std::string const & g, std::string const & i,
61 std::string const & name() const { return name_; }
63 std::string const & latexName() const { return latexName_; }
65 std::string const & guiName() const { return guiName_; }
67 std::string const & iconvName() const { return iconvName_; }
69 bool const & hasFixedWidth() const { return fixedwidth_; }
70 /// \p c is representable in this encoding without a LaTeX macro
71 bool encodable(char_type c) const;
73 * Convert \p c to something that LaTeX can understand.
74 * This is either the character itself (if it is representable
75 * in this encoding), or a LaTeX macro.
76 * If the character is not representable in this encoding, but no
77 * LaTeX macro is known, a warning is given of lyxerr, and the
78 * character is returned.
79 * \return the converted character and a flag indicating whether
80 * the command needs to be terminated by {} or a space.
82 std::pair<docstring, bool> latexChar(char_type c) const;
84 * Convert \p input to something that LaTeX can understand.
85 * This is either the string itself (if it is representable
86 * in this encoding), or a LaTeX macro.
87 * If a character is not representable in this encoding, but no
88 * LaTeX macro is known, a warning is given of lyxerr, and the
89 * character is returned in the second string of the pair and
90 * omitted in the first.
91 * \p dryrun specifies whether the string is used within source
92 * preview (which yields a special warning).
94 std::pair<docstring, docstring> latexString(docstring const input,
95 bool dryrun = false) const;
96 /// Which LaTeX package handles this encoding?
97 Package package() const { return package_; }
98 /// A list of all characters usable in this encoding
99 std::vector<char_type> symbolsList() const;
104 std::string latexName_;
106 std::string guiName_;
108 std::string iconvName_;
109 /// Is this a fixed width encoding?
112 typedef std::set<char_type> CharSet;
113 /// Set of UCS4 characters that we can encode (for singlebyte
115 mutable CharSet encodable_;
116 /// All code points below this are encodable. This helps us to avoid
117 /// lokup of ASCII characters in encodable_ and gives about 1 sec
118 /// speedup on export of the Userguide.
119 mutable char_type start_encodable_;
120 /// Which LaTeX package handles this encoding?
123 * If this is true the stored information about the encoding covers
124 * all encodable characters. We set this to false initially so that
125 * we only need to query iconv for the actually used encodings.
126 * This is needed especially for the multibyte encodings, if we
127 * complete all encoding info on startup it takes 2-3 minutes.
129 mutable bool complete_;
135 typedef std::set<char_type> MathCommandSet;
137 typedef std::set<char_type> TextCommandSet;
139 typedef std::set<char_type> MathSymbolSet;
141 typedef std::map<std::string, Encoding> EncodingList;
142 /// iterator to iterate over all encodings.
143 /// We hide the fact that our encoding list is implemented as a map.
144 class const_iterator : public EncodingList::const_iterator {
145 typedef EncodingList::const_iterator base;
147 const_iterator() : base() {}
148 const_iterator(base const & b) : base(b) {}
149 Encoding const & operator*() const { return base::operator*().second; }
150 Encoding const * operator->() const { return &(base::operator*().second); }
154 /// Read the encodings.
155 /// \param encfile encodings definition file
156 /// \param symbolsfile unicode->LaTeX mapping file
157 void read(support::FileName const & encfile,
158 support::FileName const & symbolsfile);
159 /// Get encoding from LyX name \p name
160 Encoding const * fromLyXName(std::string const & name) const;
161 /// Get encoding from LaTeX name \p name
162 Encoding const * fromLaTeXName(std::string const & name) const;
165 const_iterator begin() const { return encodinglist.begin(); }
167 const_iterator end() const { return encodinglist.end(); }
181 static bool isHebrewComposeChar(char_type c);
183 static bool isArabicComposeChar(char_type c);
185 static bool isArabicSpecialChar(char_type c);
187 static bool isArabicChar(char_type c);
189 static char_type transformChar(char_type c, LetterForm form);
190 /// Is this a combining char?
191 static bool isCombiningChar(char_type c);
192 /// Return the TIPA shortcut
193 static std::string const TIPAShortcut(char_type c);
195 * Is this a known char from some language?
196 * If \p preamble is empty and code point \p c is known to belong
197 * to a supported script, true is returned and \p preamble is set
198 * to the corresponding entry in the unicodesymbols file.
199 * If \p preamble is not empty, a check is made whether code point
200 * \p c is a known character matching the preamble entry.
202 static bool isKnownScriptChar(char_type const c, std::string & preamble);
204 * Do we have to output this character as LaTeX command in any case?
205 * This is true if the "force" flag is set.
206 * We need this if the inputencoding does not support a certain glyph.
208 static bool isForced(char_type c);
210 * Do we have to display in italics this character when in mathmode?
211 * This is true if the "mathalpha" flag is set. We use this for
212 * letters and accented characters that are output as math commands.
214 static bool isMathAlpha(char_type c);
216 * Register \p c as a mathmode command.
218 static void addMathCmd(char_type c) { mathcmd.insert(c); }
220 * Register \p c as a textmode command.
222 static void addTextCmd(char_type c) { textcmd.insert(c); }
224 * Register \p c as a mathmode symbol.
226 static void addMathSym(char_type c) { mathsym.insert(c); }
228 * Tell whether \p c is registered as a mathmode command.
230 static bool isMathCmd(char_type c) { return mathcmd.count(c); }
232 * Tell whether \p c is registered as a textmode command.
234 static bool isTextCmd(char_type c) { return textcmd.count(c); }
236 * Tell whether \p c is registered as a mathmode symbol.
238 static bool isMathSym(char_type c) { return mathsym.count(c); }
240 * Initialize mathcmd, textcmd, and mathsym sets.
242 static void initUnicodeMath(Buffer const & buffer, bool for_master = true);
244 * If \p c cannot be encoded in the given \p encoding, convert
245 * it to something that LaTeX can understand in mathmode.
246 * \p needsTermination indicates whether the command needs to be
247 * terminated by {} or a space.
248 * \return whether \p command is a mathmode command
250 static bool latexMathChar(char_type c, bool mathmode,
251 Encoding const * encoding, docstring & command,
252 bool & needsTermination);
254 * Convert the LaTeX command in \p cmd to the corresponding unicode
255 * point and set \p combining to true if it is a combining symbol.
256 * \p needsTermination indicates whether the command needs to be
257 * terminated by {} or a space.
259 static char_type fromLaTeXCommand(docstring const & cmd, int cmdtype,
260 bool & combining, bool & needsTermination,
261 std::set<std::string> * req = 0);
270 * Convert the LaTeX commands in \p cmd and \return a docstring
271 * of corresponding unicode points. The conversion stops at the
272 * first command which could not be converted, and the remaining
273 * unconverted commands are returned in \p rem.
274 * The \p cmdtype parameter can be used to limit recognized
275 * commands to math or text mode commands only.
276 * \p needsTermination indicates whether the command needs to be
277 * terminated by {} or a space.
279 static docstring fromLaTeXCommand(docstring const & cmd, int cmdtype,
280 bool & needsTermination, docstring & rem,
281 std::set<std::string> * req = 0);
283 * Add the preamble snippet needed for the output of \p c to
285 * This does not depend on the used encoding, since the inputenc
286 * package only maps the code point \p c to a command, it does not
287 * make this command available.
289 static void validate(char_type c, LaTeXFeatures & features, bool for_mathed = false);
293 EncodingList encodinglist;
295 static MathCommandSet mathcmd;
297 static TextCommandSet textcmd;
299 static MathSymbolSet mathsym;
302 extern Encodings encodings;